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