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 // 27 // 28 // 29 // -------------------------------------------------------------- 30 // GEANT 4 - examplePar04 31 // -------------------------------------------------------------- 32 // Comments 33 // 34 // Example of a main program demonstrating inference in C++ 35 // for fast simulation in calorimeters. 36 // 37 //------------------------------------------------------------------- 38 #include "FTFP_BERT.hh" // for FTFP_BERT 39 #include "Par04ActionInitialisation.hh" // for Par04ActionInitialisation 40 #include "Par04DetectorConstruction.hh" // for Par04DetectorConstruction 41 #include "Par04ParallelFastWorld.hh" 42 #include "Par04ParallelFullWorld.hh" 43 44 #include "G4EmParameters.hh" // for G4EmParameters 45 #include "G4Exception.hh" // for G4Exception 46 #include "G4ExceptionSeverity.hh" // for FatalErrorInArgument 47 #include "G4FastSimulationPhysics.hh" // for G4FastSimulationPhysics 48 #include "G4HadronicProcessStore.hh" // for G4HadronicProcessStore 49 #include "G4ParallelWorldPhysics.hh" 50 #include "G4RunManager.hh" // for G4RunManager 51 #include "G4RunManagerFactory.hh" // for G4RunManagerFactory, G4RunMa... 52 #include "G4String.hh" // for G4String 53 #include "G4Types.hh" // for G4bool, G4int 54 #include "G4UIExecutive.hh" // for G4UIExecutive 55 #include "G4UImanager.hh" // for G4UImanager 56 #include "G4VisExecutive.hh" // for G4VisExecutive 57 #include "G4VisManager.hh" // for G4VisManager 58 #include "G4ios.hh" // for G4cout, G4endl 59 60 #include <ctime> // for time 61 #include <sstream> // for char_traits, operator<<, bas... 62 #include <string> // for allocator, operator+, operat... 63 64 int main(int argc, char** argv) 65 { 66 // Macro name from arguments 67 G4String batchMacroName; 68 G4bool useInteractiveMode = false; 69 G4int numOfThreadsOrTasks = 8; 70 G4int runManagerTypeInt = 0; 71 G4RunManagerType runManagerType = G4RunManagerType::Serial; 72 G4String helpMsg( 73 "Usage: " + G4String(argv[0]) + 74 " [option(s)] \n You need to specify the mode and the macro file.\nOptions:" 75 "\n\t-h\t\tdisplay this help message\n\t-m MACRO\ttriggers a batch mode " 76 "executing MACRO\n\t-i\t\truns interactive mode, use it together with <-m vis*mac> macros" 77 "\n\t-r\t\trun manager type (0=serial,1=MT,2=tasking)" 78 "\n\t-t\t\tnumber of threads for MT mode (no change for other modes)." 79 ); 80 if (argc < 2) { 81 G4Exception("main", "No arguments", FatalErrorInArgument, 82 ("No arguments passed to " + G4String(argv[0]) + "\n" + helpMsg).c_str()); 83 } 84 for (G4int i = 1; i < argc; ++i) { 85 G4String argument(argv[i]); 86 if (argument == "-h" || argument == "--help") { 87 G4cout << helpMsg << G4endl; 88 return 0; 89 } 90 else if (argument == "-m") { 91 batchMacroName = G4String(argv[i + 1]); 92 ++i; 93 } 94 else if (argument == "-i") { 95 useInteractiveMode = true; 96 } 97 else if (argument == "-r") { 98 G4int tmp = atoi(argv[i + 1]); 99 ++i; 100 switch (tmp) { 101 case 0: 102 runManagerTypeInt = tmp; 103 runManagerType = G4RunManagerType::Serial; 104 break; 105 case 1: 106 runManagerTypeInt = tmp; 107 runManagerType = G4RunManagerType::MTOnly; 108 break; 109 case 2: 110 runManagerTypeInt = tmp; 111 runManagerType = G4RunManagerType::Tasking; 112 break; 113 default: 114 G4Exception("main", "Wrong Run Manager type", FatalErrorInArgument, 115 "Choose 0 (serial, default), 1 (MT), 2 (tasking)"); 116 break; 117 } 118 } 119 else if (argument == "-t") { 120 numOfThreadsOrTasks = atoi(argv[i + 1]); 121 ++i; 122 } 123 else { 124 G4Exception( 125 "main", "Unknown argument", FatalErrorInArgument, 126 ("Unknown argument passed to " + G4String(argv[0]) + " : " + argument + "\n" + helpMsg) 127 .c_str()); 128 } 129 } 130 131 // choose the Random engine 132 CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine()); 133 // set random seed with system time 134 G4long seed = time(NULL); 135 CLHEP::HepRandom::setTheSeed(seed); 136 137 // Instantiate G4UIExecutive if interactive mode 138 G4UIExecutive* ui = nullptr; 139 140 if (useInteractiveMode) { 141 ui = new G4UIExecutive(argc, argv); 142 runManagerType = G4RunManagerType::Serial; 143 } 144 145 // Initialization of default Run manager 146 auto* runManager = G4RunManagerFactory::CreateRunManager(runManagerType); 147 if (runManagerTypeInt == 1) runManager->SetNumberOfThreads(numOfThreadsOrTasks); 148 // Detector geometry: 149 auto detector = new Par04DetectorConstruction(); 150 auto parallelWorldFull = new Par04ParallelFullWorld("parallelWorldFullSim", detector); 151 auto parallelWorldFast = 152 new Par04ParallelFastWorld("parallelWorldFastSim", detector, parallelWorldFull); 153 detector->RegisterParallelWorld(parallelWorldFull); 154 detector->RegisterParallelWorld(parallelWorldFast); 155 runManager->SetUserInitialization(detector); 156 157 // Physics list 158 auto physicsList = new FTFP_BERT(); 159 // Add fast simulation physics 160 auto fastSimulationPhysics = new G4FastSimulationPhysics(); 161 fastSimulationPhysics->BeVerbose(); 162 fastSimulationPhysics->ActivateFastSimulation("e-"); 163 fastSimulationPhysics->ActivateFastSimulation("e+"); 164 fastSimulationPhysics->ActivateFastSimulation("gamma"); 165 physicsList->RegisterPhysics(fastSimulationPhysics); 166 // Add parallel world for readout 167 physicsList->RegisterPhysics(new G4ParallelWorldPhysics("parallelWorldFullSim")); 168 physicsList->RegisterPhysics(new G4ParallelWorldPhysics("parallelWorldFastSim")); 169 // reduce verbosity of physics lists 170 G4EmParameters::Instance()->SetVerbose(0); 171 runManager->SetUserInitialization(physicsList); 172 173 //------------------------------- 174 // UserAction classes 175 //------------------------------- 176 runManager->SetUserInitialization(new Par04ActionInitialisation(detector, parallelWorldFull)); 177 //---------------- 178 // Visualization: 179 //---------------- 180 G4cout << "Instantiating Visualization Manager......." << G4endl; 181 G4VisManager* visManager = new G4VisExecutive; 182 visManager->Initialize(); 183 G4UImanager* UImanager = G4UImanager::GetUIpointer(); 184 185 if (useInteractiveMode) { 186 if (batchMacroName.empty()) { 187 G4Exception("main", "Unknown macro name", FatalErrorInArgument, 188 ("No macro name passed to " + G4String(argv[0])).c_str()); 189 } 190 G4String command = "/control/execute "; 191 UImanager->ApplyCommand(command + batchMacroName); 192 ui->SessionStart(); 193 delete ui; 194 } 195 else { 196 G4String command = "/control/execute "; 197 UImanager->ApplyCommand(command + batchMacroName); 198 } 199 200 // Free the store: user actions, physics_list and detector_description are 201 // owned and deleted by the run manager, so they should not 202 // be deleted in the main() program ! 203 204 delete visManager; 205 delete runManager; 206 207 return 0; 208 } 209