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