Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 // 26 /// \file extensibleFactory.cc 27 /// \brief Main program of the extensibleFactory example 28 // 29 // 30 // 31 // ------------------------------------------------------------- 32 // extensibleFactory 33 // 34 // Application demonstrating the extensible physics list factory 35 // 36 // Author of hadronic/Hadr00/Hadr00.cc 37 // V.Ivanchenko, 20 June 2008 (as hadronic/Hadr00/Hadr00.cc) 38 // Author of examples/extended/physicslists/factory/factory.cc 39 // I. Hrivnacova, 2017-09-26 40 41 // Modified from factory.cc 42 // R.Hatcher 2017-10-31 43 // copied from examples/extended/physicslists/factory 44 // modified to use alternative extensible physics list factory 45 // 46 // ------------------------------------------------------------- 47 // 48 // 49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 50 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 51 52 #include "ActionInitialization.hh" 53 #include "DetectorConstruction.hh" 54 #include "PrimaryGeneratorAction.hh" 55 56 #include "G4RunManagerFactory.hh" 57 58 ///////////////////////////////////////////////////////////////////////////// 59 // The following change is the _only_ required changed to move from 60 // the non-extensible factory to the exensible factory. All other changes 61 // relative to the "factory" example are there to demonstrate new features. 62 ///////////////////////////////////////////////////////////////////////////// 63 // non-extensible: #include "G4PhysListFactory.hh" 64 #include "G4PhysListFactoryAlt.hh" 65 // use this for drop-in replacement: using namespace g4alt; 66 67 ///////////////////////////////////////////////////////////////////////////// 68 // headers needed to demonstrate new featues 69 ///////////////////////////////////////////////////////////////////////////// 70 71 // allow ourselves to extend the short names for physics ctor addition/replace 72 // along the same lines as EMX, EMY, etc 73 #include "G4PhysListRegistry.hh" 74 75 // allow ourselves to give the user extra info about available physics ctors 76 #include "G4PhysicsConstructorFactory.hh" 77 78 // pull in a user defined physics list definition into the main program 79 // and register it with the factory (doesn't have to be the main program 80 // but the .o containing the declaration _must_ get linked/loaded) 81 #include "MySpecialPhysList.hh" 82 83 #include "G4PhysListStamper.hh" // defines macro for factory registration 84 G4_DECLARE_PHYSLIST_FACTORY(MySpecialPhysList); 85 86 ///////////////////////////////////////////////////////////////////////////// 87 88 #include "G4UIExecutive.hh" 89 #include "G4UImanager.hh" 90 #include "G4VModularPhysicsList.hh" 91 #include "G4VisExecutive.hh" 92 #include "Randomize.hh" 93 94 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 95 96 namespace 97 { 98 99 void PrintAvailable(G4int verbosity) 100 { 101 G4cout << G4endl; 102 G4cout << "extensibleFactory: here are the available physics lists:" << G4endl; 103 g4alt::G4PhysListFactory factory; 104 factory.PrintAvailablePhysLists(); 105 106 // if user asked for extra verbosity then print physics ctors as well 107 if (verbosity > 1) { 108 G4cout << G4endl; 109 G4cout << "extensibleFactory: " 110 << "here are the available physics ctors that can be added:" << G4endl; 111 G4PhysicsConstructorRegistry* g4pctorFactory = G4PhysicsConstructorRegistry::Instance(); 112 g4pctorFactory->PrintAvailablePhysicsConstructors(); 113 } 114 } 115 116 void PrintUsage(G4int verbosity) 117 { 118 G4cerr << " Usage: " << G4endl; 119 G4cerr << " extensibleFactory [-m macro ] [-p physList ]" 120 << " [-u UIsession] [-t nThreads]" << G4endl << " [-v | --verbose] [-h | --help]" 121 << G4endl; 122 G4cerr << " note: -t option is available only for multi-threaded mode." << G4endl; 123 G4cerr << " note: -v can be repeated to increase verbosity." << G4endl; 124 G4cerr << G4endl; 125 126 if (verbosity > 0) PrintAvailable(verbosity); 127 } 128 129 } // namespace 130 131 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 132 133 int main(int argc, char** argv) 134 { 135 // Evaluate arguments 136 // 137 if (argc > 13) { 138 PrintUsage(0); 139 return 1; 140 } 141 142 G4String macro; 143 G4String session; 144 G4String physListName; 145 char* physListNameEnv = nullptr; 146 G4String gdmlFileName; 147 #ifdef G4MULTITHREADED 148 G4int nofThreads = 0; 149 #endif 150 G4int verbosity = 0; 151 152 for (G4int i = 1; i < argc; i = i + 2) { 153 G4String g4argv(argv[i]); // convert only once 154 if (g4argv == "-m") 155 macro = argv[i + 1]; 156 else if (g4argv == "-u") 157 session = argv[i + 1]; 158 else if (g4argv == "-p") 159 physListName = argv[i + 1]; 160 #ifdef G4MULTITHREADED 161 else if (g4argv == "-t") { 162 nofThreads = G4UIcommand::ConvertToInt(argv[i + 1]); 163 } 164 #endif 165 else if (g4argv == "-v" || g4argv == "--verbose") { 166 ++verbosity; // verbose flag doesn't take an argument 167 --i; // don't increment argc by two, just the one 168 } 169 else if (g4argv == "-h" || g4argv == "--help") { 170 PrintUsage(verbosity + 1); 171 return 1; 172 } 173 else { 174 PrintUsage(0); 175 return 1; 176 } 177 } 178 179 // Detect interactive mode (if no arguments) and define UI session 180 // 181 G4UIExecutive* ui = nullptr; 182 if (!macro.size()) { 183 ui = new G4UIExecutive(argc, argv, session); 184 } 185 186 // Choose the Random engine //choose the Random engine 187 G4Random::setTheEngine(new CLHEP::RanecuEngine()); 188 189 // Construct the run manager 190 auto* runManager = G4RunManagerFactory::CreateRunManager(); 191 #ifdef G4MULTITHREADED 192 if (nofThreads > 0) { 193 runManager->SetNumberOfThreads(nofThreads); 194 } 195 #endif 196 197 // g4alt::G4PhysListFactoryAlt is the extensible factory 198 // including the G4PhysListFactoryAlt.hh header and the line: 199 // using namespace g4alt; 200 // would make this a drop-in replacement, but we'll list the explicit 201 // namespace here just for clarity 202 g4alt::G4PhysListFactory factory; 203 G4VModularPhysicsList* physList = nullptr; 204 205 // Show how an alternative default list could be set 206 // but set the default to the normal default FTFP_BERT. 207 // This is what is used when no -p flag is given and $PHYSLIST 208 // is not defined in the environment. 209 G4String defaultPhysListName = "FTFP_BERT"; 210 if (verbosity > 0) { 211 G4cout << "extensibleFactory: SetDefaultReferencePhysList to '" << defaultPhysListName 212 << "' ('' = system default)" << G4endl << G4endl; 213 } 214 factory.SetDefaultReferencePhysList(defaultPhysListName); 215 216 // set a short name for G4RadioactiveDecayPhysics 217 G4PhysListRegistry* plreg = G4PhysListRegistry::Instance(); 218 plreg->AddPhysicsExtension("RADIO", "G4RadioactiveDecayPhysics"); 219 plreg->AddPhysicsExtension("MYPHYSICS", "MyG4PhysicsPhysics"); 220 if (verbosity > 0) { 221 G4cout << "extensibleFactory: adding extensions" << G4endl 222 << " RADIO ===> G4RadioactiveDecayPhysics" << G4endl 223 << " MYPHYSICS ===> MyG4PhysicsPhysics" << G4endl << G4endl; 224 } 225 226 // Get Reference PhysicsList via its name, or if none given 227 // from environment varialb e$PHYSLIST, with fall back to a default 228 if (physListName.size()) { 229 if (verbosity > 0) { 230 G4cout << "extensibleFactory: explicitly using '" << physListName << "'" << G4endl; 231 } 232 physList = factory.GetReferencePhysList(physListName); 233 } 234 else { 235 if (verbosity > 0) { 236 G4cout << "extensibleFactory: no -p flag;" 237 << " using ReferencePhysList() ($PHYSLIST or default)" << G4endl; 238 } 239 physList = factory.ReferencePhysList(); 240 241 if (!physList) { 242 // failed? get what the user set, but we couldn't find 243 physListNameEnv = std::getenv("PHYSLIST"); 244 if (physListNameEnv) { 245 G4cout << "extensibleFactory: $PHYSLIST=" << physListNameEnv << G4endl; 246 } 247 } 248 } 249 250 // deal with failure to get what the user wanted 251 // print what they _could_ use 252 if (!physList) { 253 G4cerr << "extensibleFactory: PhysicsList '" 254 << (physListNameEnv ? physListNameEnv : physListName) 255 << "' was not available in g4alt::PhysListFactory." << G4endl; 256 PrintAvailable(verbosity); 257 258 // if we can't get what the user asked for... 259 // don't go on to use something else, that's confusing 260 G4ExceptionDescription ED; 261 ED << "The factory for the physicslist [" << (physListNameEnv ? physListNameEnv : physListName) 262 << "] does not exist!" << G4endl; 263 G4Exception("extensibleFactory", "extensibleFactory001", FatalException, ED); 264 exit(42); 265 } 266 267 // Set mandatory initialization classes 268 runManager->SetUserInitialization(new DetectorConstruction()); 269 runManager->SetUserInitialization(physList); 270 271 // set user action classes 272 auto actinit = new ActionInitialization("extensibleFactory"); 273 runManager->SetUserInitialization(actinit); 274 275 // Initialize visualization 276 G4VisManager* visManager = new G4VisExecutive; 277 // G4VisExecutive can take a verbosity argument - see /vis/verbose guidance. 278 // G4VisManager* visManager = new G4VisExecutive("Quiet"); 279 visManager->Initialize(); 280 281 // Get the pointer to the User Interface manager 282 G4UImanager* UImanager = G4UImanager::GetUIpointer(); 283 284 if (macro.size()) { 285 // batch mode 286 G4String command = "/control/execute "; 287 UImanager->ApplyCommand(command + macro); 288 } 289 else { 290 // interactive mode : define UI session 291 UImanager->ApplyCommand("/control/execute init_vis.mac"); 292 ui->SessionStart(); 293 delete ui; 294 } 295 296 // Job termination 297 // Free the store: user actions, physics_list and detector_description are 298 // owned and deleted by the run manager, so they should not be deleted 299 // in the main() program ! 300 301 delete visManager; 302 delete runManager; 303 } 304 305 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 306