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