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