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