Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // This example is provided by the Geant4-DNA 26 // This example is provided by the Geant4-DNA collaboration 27 // Any report or published results obtained us 27 // Any report or published results obtained using the Geant4-DNA software 28 // shall cite the following Geant4-DNA collabo 28 // shall cite the following Geant4-DNA collaboration publication: 29 // Med. Phys. 37 (2010) 4692-4708 29 // Med. Phys. 37 (2010) 4692-4708 30 // J. Comput. Phys. 274 (2014) 841-882 30 // J. Comput. Phys. 274 (2014) 841-882 31 // Phys. Med. Biol. 63(10) (2018) 105014-12pp << 31 // Phys. Med. Biol. 63(10) (2018) 105014-12pp 32 // The Geant4-DNA web site is available at htt 32 // The Geant4-DNA web site is available at http://geant4-dna.org 33 // 33 // 34 // 34 // 35 /// \file chem5.cc 35 /// \file chem5.cc 36 /// \brief Chem5 example 36 /// \brief Chem5 example 37 37 38 #include "ActionInitialization.hh" << 39 #include "CommandLineParser.hh" << 40 #include "DetectorConstruction.hh" 38 #include "DetectorConstruction.hh" 41 #include "PhysicsList.hh" 39 #include "PhysicsList.hh" >> 40 #include "ActionInitialization.hh" >> 41 >> 42 #ifdef G4MULTITHREADED >> 43 #include "G4MTRunManager.hh" >> 44 #else >> 45 #include "G4RunManager.hh" >> 46 #endif 42 47 43 #include "G4DNAChemistryManager.hh" 48 #include "G4DNAChemistryManager.hh" 44 #include "G4RunManagerFactory.hh" << 45 #include "G4UIExecutive.hh" << 46 #include "G4UImanager.hh" 49 #include "G4UImanager.hh" >> 50 #include "G4UIExecutive.hh" 47 #include "G4VisExecutive.hh" 51 #include "G4VisExecutive.hh" 48 52 >> 53 #include "CommandLineParser.hh" >> 54 49 /* 55 /* 50 * WARNING : Geant4 was initially not intended 56 * WARNING : Geant4 was initially not intended for this kind of application 51 * This code is delivered as a prototype 57 * This code is delivered as a prototype 52 * We will be happy to hear from you, do not h 58 * We will be happy to hear from you, do not hesitate to send your feedback and 53 * communicate on the difficulties you may enc 59 * communicate on the difficulties you may encounter 54 * The user interface may change in the next r 60 * The user interface may change in the next releases since a reiteration of the 55 * code has started 61 * code has started 56 */ 62 */ 57 63 58 //....oooOO0OOooo........oooOO0OOooo........oo 64 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 59 65 60 using namespace G4DNAPARSER; 66 using namespace G4DNAPARSER; 61 CommandLineParser* parser(0); 67 CommandLineParser* parser(0); 62 long seed = 0; 68 long seed = 0; 63 69 64 unsigned int noise(); 70 unsigned int noise(); 65 void SetSeed(); 71 void SetSeed(); 66 72 67 void Parse(int& argc, char** argv); 73 void Parse(int& argc, char** argv); 68 74 69 //....oooOO0OOooo........oooOO0OOooo........oo 75 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 70 76 71 int main(int argc, char** argv) 77 int main(int argc, char** argv) 72 { 78 { 73 // Parse options given in commandLine 79 // Parse options given in commandLine 74 Parse(argc, argv); 80 Parse(argc, argv); 75 Command* commandLine(0); 81 Command* commandLine(0); 76 SetSeed(); 82 SetSeed(); 77 << 83 78 // Construct the run manager according to wh 84 // Construct the run manager according to whether MT is activated or not 79 // 85 // 80 auto* runManager = G4RunManagerFactory::Crea << 86 #ifdef G4MULTITHREADED 81 << 87 G4MTRunManager* runManager= new G4MTRunManager(); 82 if ((commandLine = parser->GetCommandIfActiv << 88 >> 89 if((commandLine = parser->GetCommandIfActive("-mt"))) >> 90 { 83 int nThreads = 2; 91 int nThreads = 2; 84 if (commandLine->GetOption() == "NMAX") { << 92 if(commandLine->GetOption() == "NMAX") >> 93 { 85 nThreads = G4Threading::G4GetNumberOfCor 94 nThreads = G4Threading::G4GetNumberOfCores(); 86 } 95 } 87 else { << 96 else >> 97 { 88 nThreads = G4UIcommand::ConvertToInt(com 98 nThreads = G4UIcommand::ConvertToInt(commandLine->GetOption()); 89 } 99 } 90 << 100 91 runManager->SetNumberOfThreads(nThreads); 101 runManager->SetNumberOfThreads(nThreads); 92 } 102 } 93 << 103 94 G4cout << "********************************* 104 G4cout << "**************************************************************" 95 << "******\n===== Chem5 is started wi << 105 << "******\n===== Chem5 is started with " >> 106 << runManager->GetNumberOfThreads() << " of " 96 << G4Threading::G4GetNumberOfCores() 107 << G4Threading::G4GetNumberOfCores() 97 << " available threads =====\n\n***** 108 << " available threads =====\n\n*************************************" 98 << "*******************************" << 109 <<"*******************************" 99 << 110 << G4endl; >> 111 #else >> 112 G4RunManager* runManager = new G4RunManager(); >> 113 #endif >> 114 100 // Set mandatory initialization classes 115 // Set mandatory initialization classes 101 runManager->SetUserInitialization(new Physic 116 runManager->SetUserInitialization(new PhysicsList()); 102 runManager->SetUserInitialization(new Detect 117 runManager->SetUserInitialization(new DetectorConstruction()); 103 runManager->SetUserInitialization(new Action 118 runManager->SetUserInitialization(new ActionInitialization()); 104 << 119 105 // Initialize G4 kernel 120 // Initialize G4 kernel 106 runManager->Initialize(); 121 runManager->Initialize(); 107 << 122 108 // Initialize visualization 123 // Initialize visualization 109 G4VisManager* visManager = new G4VisExecutiv 124 G4VisManager* visManager = new G4VisExecutive(); 110 // G4VisExecutive can take a verbosity argum 125 // G4VisExecutive can take a verbosity argument - see /vis/verbose guidance. 111 // G4VisManager* visManager = new G4VisExecu 126 // G4VisManager* visManager = new G4VisExecutive("Quiet"); 112 visManager->Initialize(); 127 visManager->Initialize(); 113 128 114 // Get the pointer to the User Interface man 129 // Get the pointer to the User Interface manager 115 G4UImanager* UImanager = G4UImanager::GetUIp 130 G4UImanager* UImanager = G4UImanager::GetUIpointer(); 116 G4UIExecutive* ui(0); 131 G4UIExecutive* ui(0); 117 << 132 118 // interactive mode : define UI session 133 // interactive mode : define UI session 119 if ((commandLine = parser->GetCommandIfActiv << 134 if((commandLine = parser->GetCommandIfActive("-gui"))) >> 135 { 120 ui = new G4UIExecutive(argc, argv, command 136 ui = new G4UIExecutive(argc, argv, commandLine->GetOption()); 121 << 137 122 if (ui->IsGUI()) UImanager->ApplyCommand(" 138 if (ui->IsGUI()) UImanager->ApplyCommand("/control/execute gui.mac"); 123 << 139 124 if (parser->GetCommandIfActive("-novis") = << 140 if (parser->GetCommandIfActive("-novis") == 0) >> 141 { 125 // visualization is used by default 142 // visualization is used by default 126 if ((commandLine = parser->GetCommandIfA << 143 if ((commandLine = parser->GetCommandIfActive("-vis"))) >> 144 { 127 // select a visualization driver if ne 145 // select a visualization driver if needed (e.g. HepFile) 128 UImanager->ApplyCommand(G4String("/vis << 146 UImanager->ApplyCommand >> 147 (G4String("/vis/open ") + commandLine->GetOption()); 129 } 148 } 130 else { << 149 else >> 150 { 131 // by default OGL is used 151 // by default OGL is used 132 UImanager->ApplyCommand("/vis/open OGL 152 UImanager->ApplyCommand("/vis/open OGL 800x600-0+0"); 133 } 153 } 134 UImanager->ApplyCommand("/control/execut 154 UImanager->ApplyCommand("/control/execute vis.mac"); 135 } 155 } 136 } 156 } 137 else { << 157 else >> 158 { 138 // to be use visualization file (= store t 159 // to be use visualization file (= store the visualization into 139 // an external file: 160 // an external file: 140 // ASCIITree ; DAWNFILE ; HepRepFile ; VR 161 // ASCIITree ; DAWNFILE ; HepRepFile ; VRML(1,2)FILE ; gMocrenFile ... 141 if ((commandLine = parser->GetCommandIfAct << 162 if((commandLine = parser->GetCommandIfActive("-vis"))) 142 UImanager->ApplyCommand(G4String("/vis/o << 163 { >> 164 UImanager->ApplyCommand(G4String("/vis/open ") >> 165 + commandLine->GetOption()); 143 UImanager->ApplyCommand("/control/execut 166 UImanager->ApplyCommand("/control/execute vis.mac"); 144 } 167 } 145 } 168 } 146 << 169 147 if ((commandLine = parser->GetCommandIfActiv << 170 if((commandLine = parser->GetCommandIfActive("-mac"))) >> 171 { 148 G4String command = "/control/execute "; 172 G4String command = "/control/execute "; 149 UImanager->ApplyCommand(command + commandL 173 UImanager->ApplyCommand(command + commandLine->GetOption()); 150 } 174 } 151 else { << 175 else >> 176 { 152 UImanager->ApplyCommand("/control/execute 177 UImanager->ApplyCommand("/control/execute beam.in"); 153 } 178 } 154 179 155 if ((commandLine = parser->GetCommandIfActiv << 180 if((commandLine = parser->GetCommandIfActive("-gui"))) >> 181 { 156 ui->SessionStart(); 182 ui->SessionStart(); 157 delete ui; 183 delete ui; 158 } 184 } 159 << 185 160 // Job termination 186 // Job termination 161 // Free the store: user actions, physics_lis 187 // Free the store: user actions, physics_list and detector_description are 162 // owned and deleted by the run manager, so 188 // owned and deleted by the run manager, so they should not be deleted 163 // in the main() program ! 189 // in the main() program ! 164 delete visManager; 190 delete visManager; 165 delete runManager; 191 delete runManager; 166 CommandLineParser::DeleteInstance(); 192 CommandLineParser::DeleteInstance(); 167 return 0; 193 return 0; 168 } 194 } 169 195 170 //....oooOO0OOooo........oooOO0OOooo........oo 196 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 171 197 172 bool IsBracket(char c) 198 bool IsBracket(char c) 173 { 199 { 174 switch (c) { << 200 switch(c) >> 201 { 175 case '[': 202 case '[': 176 case ']': 203 case ']': 177 return true; 204 return true; 178 default: 205 default: 179 return false; 206 return false; 180 } 207 } 181 } 208 } 182 209 183 //....oooOO0OOooo........oooOO0OOooo........oo 210 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 184 211 185 void SetSeed() 212 void SetSeed() 186 { << 213 { 187 Command* commandLine(0); 214 Command* commandLine(0); 188 << 215 189 if ((commandLine = parser->GetCommandIfActiv << 216 if((commandLine = parser->GetCommandIfActive("-seed"))) >> 217 { 190 seed = atoi(commandLine->GetOption().c_str 218 seed = atoi(commandLine->GetOption().c_str()); 191 } 219 } 192 << 220 193 if (seed == 0) // If no seed given in argum << 221 if(seed == 0) // If no seed given in argument, setup the seed 194 { 222 { 195 long jobID_int = 0; 223 long jobID_int = 0; 196 long noice = 0; 224 long noice = 0; 197 << 225 198 //____________________________________ 226 //____________________________________ 199 // In case on cluster 227 // In case on cluster 200 if ((commandLine = parser->GetCommandIfAct << 228 if((commandLine = parser->GetCommandIfActive("-cluster"))) 201 noice = labs((long)noise()); << 229 { 202 << 230 noice = labs((long) noise()); 203 const char* env = std::getenv("PBS_JOBID << 231 >> 232 const char * env = std::getenv("PBS_JOBID"); 204 233 205 if (env) { << 234 if(env) >> 235 { 206 G4String buffer(env); 236 G4String buffer(env); 207 G4String jobID_string = buffer.substr( << 237 G4String jobID_string = buffer.substr(0, buffer.find(".")); 208 jobID_string.erase(std::remove_if(jobI << 238 jobID_string.erase(std::remove_if(jobID_string.begin(), 209 jobID_string.end()) << 239 jobID_string.end(), >> 240 &IsBracket), >> 241 jobID_string.end()); 210 jobID_int = atoi(jobID_string.c_str()) 242 jobID_int = atoi(jobID_string.c_str()); 211 } 243 } 212 else { << 244 else >> 245 { 213 env = std::getenv("SGE_TASK_ID"); 246 env = std::getenv("SGE_TASK_ID"); 214 if (env) jobID_int = atoi(env); << 247 if(env) jobID_int = atoi(env); 215 } 248 } 216 } // end cluster << 249 } // end cluster 217 << 250 218 //____________________________________ 251 //____________________________________ 219 seed = ((long)time(NULL)) + jobID_int + no << 252 seed = ((long) time(NULL)) + jobID_int + noice; 220 } 253 } 221 << 254 222 G4cout << "Seed used : " << seed << G4endl; 255 G4cout << "Seed used : " << seed << G4endl; 223 G4Random::setTheEngine(new CLHEP::MixMaxRng( 256 G4Random::setTheEngine(new CLHEP::MixMaxRng()); 224 G4Random::setTheSeed(seed); 257 G4Random::setTheSeed(seed); 225 258 226 // Choose the Random engine 259 // Choose the Random engine 227 // CLHEP::HepRandom::setTheEngine(new CLHEP: 260 // CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine); 228 } 261 } 229 262 230 //....oooOO0OOooo........oooOO0OOooo........oo 263 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 231 264 232 unsigned int noise() 265 unsigned int noise() 233 { 266 { 234 #if defined(WIN32) || defined(_WIN32) || defin << 267 #if defined(WIN32)|| defined(_WIN32)|| defined(__WIN32)&&!defined(__CYGWIN__) 235 // TODO: MS Win 268 // TODO: MS Win 236 return std::time(0); 269 return std::time(0); 237 #else 270 #else 238 unsigned int random_seed, random_seed_a, ran 271 unsigned int random_seed, random_seed_a, random_seed_b; 239 std::ifstream file("/dev/urandom", std::ios: << 272 std::ifstream file ("/dev/urandom", std::ios::binary); 240 if (file.is_open()) { << 273 if (file.is_open()) 241 char* memblock; << 274 { >> 275 char * memblock; 242 int size = sizeof(int); 276 int size = sizeof(int); 243 memblock = new char[size]; << 277 memblock = new char [size]; 244 file.read(memblock, size); << 278 file.read (memblock, size); 245 file.close(); 279 file.close(); 246 random_seed_a = *reinterpret_cast<int*>(me 280 random_seed_a = *reinterpret_cast<int*>(memblock); 247 delete[] memblock; 281 delete[] memblock; 248 } // end if << 282 }// end if 249 else { << 283 else >> 284 { 250 random_seed_a = 0; 285 random_seed_a = 0; 251 } 286 } 252 random_seed_b = std::time(0); 287 random_seed_b = std::time(0); 253 random_seed = random_seed_a xor random_seed_ 288 random_seed = random_seed_a xor random_seed_b; 254 return random_seed; 289 return random_seed; 255 #endif 290 #endif 256 } 291 } 257 292 258 //....oooOO0OOooo........oooOO0OOooo........oo 293 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 259 294 260 void Parse(int& argc, char** argv) 295 void Parse(int& argc, char** argv) 261 { 296 { 262 ////////// 297 ////////// 263 // Parse options given in commandLine 298 // Parse options given in commandLine 264 // 299 // 265 parser = CommandLineParser::GetParser(); 300 parser = CommandLineParser::GetParser(); 266 << 301 267 parser->AddCommand("-gui", Command::OptionNo 302 parser->AddCommand("-gui", Command::OptionNotCompulsory, 268 "Select geant4 UI or just 303 "Select geant4 UI or just launch a geant4 terminal" 269 "session", 304 "session", 270 "qt"); 305 "qt"); 271 << 306 272 parser->AddCommand("-mac", Command::WithOpti << 307 parser->AddCommand("-mac", Command::WithOption, "Give a mac file to execute", 273 << 308 "macFile.mac"); 274 parser->AddCommand("-seed", Command::WithOpt << 309 275 "seed"); << 310 parser->AddCommand("-seed", 276 << 311 Command::WithOption, 277 parser->AddCommand("-mt", Command::WithOptio << 312 "Give a seed value in argument to be tested", "seed"); >> 313 >> 314 #ifdef G4MULTITHREADED >> 315 parser->AddCommand("-mt",Command::WithOption, 278 "Launch in MT mode (event 316 "Launch in MT mode (events computed in parallel," 279 " NOT RECOMMANDED WITH CH << 317 " NOT RECOMMANDED WITH CHEMISTRY)", "2"); 280 "2"); << 318 #endif 281 << 319 282 parser->AddCommand("-chemOFF", Command::With << 320 parser->AddCommand("-chemOFF", Command::WithoutOption, 283 << 321 "Deactivate chemistry"); 284 parser->AddCommand("-vis", Command::WithOpti << 322 285 "OGL 600x600-0+0"); << 323 parser->AddCommand("-vis", Command::WithOption, 286 << 324 "Select a visualization driver", "OGL 600x600-0+0"); 287 parser->AddCommand("-novis", Command::Withou << 325 288 << 326 parser->AddCommand("-novis", Command::WithoutOption, >> 327 "Deactivate visualization when using GUI"); >> 328 289 parser->AddCommand("-cluster", Command::With 329 parser->AddCommand("-cluster", Command::WithoutOption, 290 "Launch the code on a clu 330 "Launch the code on a cluster, avoid dupplicated seeds"); 291 << 331 292 ////////// 332 ////////// 293 // If -h or --help is given in option : prin 333 // If -h or --help is given in option : print help and exit 294 // 334 // 295 if (parser->Parse(argc, argv) != 0) // help << 335 if (parser->Parse(argc, argv) != 0) // help is being printed 296 { 336 { 297 // if you are using ROOT, create a TApplic 337 // if you are using ROOT, create a TApplication in this condition in order 298 // to print the help from ROOT as well 338 // to print the help from ROOT as well 299 CommandLineParser::DeleteInstance(); 339 CommandLineParser::DeleteInstance(); 300 std::exit(0); 340 std::exit(0); 301 } 341 } 302 << 342 303 /////////// 343 /////////// 304 // Kill application if wrong argument in com 344 // Kill application if wrong argument in command line 305 // 345 // 306 if (parser->CheckIfNotHandledOptionsExists(a << 346 if(parser->CheckIfNotHandledOptionsExists(argc, argv)) >> 347 { 307 // if you are using ROOT, you should initi 348 // if you are using ROOT, you should initialise your TApplication 308 // before this condition 349 // before this condition 309 std::exit(0); 350 std::exit(0); 310 } 351 } 311 } 352 } 312 353