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