GEMC  1.8
Geant4 Monte-Carlo Framework
gemc.cc
Go to the documentation of this file.
1 
40 // %%%%%%%%%%
41 // G4 headers
42 // %%%%%%%%%%
43 #include "G4RunManager.hh"
44 #include "G4UImanager.hh"
45 #include "G4UIterminal.hh"
46 #include "G4VisExecutive.hh"
47 #include "G4PhysListFactory.hh"
48 #include "G4VModularPhysicsList.hh"
49 #include "G4PropagatorInField.hh"
50 #include "G4TransportationManager.hh"
51 #include "G4UIQt.hh"
52 #include "G4Qt.hh"
53 
54 // %%%%%%%%%%
55 // Qt headers
56 // %%%%%%%%%%
57 #include <QApplication>
58 #include <QSplashScreen>
59 
60 // %%%%%%%%%%%%%
61 // gemc headers
62 // %%%%%%%%%%%%%
63 #include "HitProcess_MapRegister.h"
64 #include "detector.h"
65 #include "gemc_MainGui.h"
66 #include "images/CLAS12_cad.h"
67 #include "MagneticField.h"
68 #include "MBankdefs.h"
69 #include "MDetectorConstruction.h"
70 #include "MEventAction.h"
71 #include "MOutputBaseClass.h"
72 #include "MPHBaseClass.h"
73 #include "MPhysicsList.h"
75 #include "MSteppingAction.h"
76 #include "Output_Register.h"
77 #include "usage.h"
78 #include "run_conditions.h"
79 #include "material_factory.h"
80 #include "parameter_factory.h"
81 #include "string_utilities.h"
82 
83 // c++ headers
84 #include <unistd.h> // needed for get_pid
85 
86 
87 
104 
105 
106 // get_pid is useful only on the farm
107 // don't need it in Windows environment
108 // (but, why _getpid is not found?)
109 #ifdef _MSC_VER
110 #include <stdio.h>
111 #include <process.h>
112  //int get_pid(){return __get_pid();}
113  int get_pid(){return 0;}
114 #endif
115 
116 
117 
118 int main( int argc, char **argv )
119 {
120 
121  gemc_opts gemcOpt;
122  gemcOpt.Set(argc, argv);
123  string hd_msg = gemcOpt.args["LOG_MSG"].args + " Init: >> " ;
124 
125  cout << endl << hd_msg << " Geant4 MonteCarlo" << endl;
126  double use_qt = gemcOpt.args["USE_QT"].arg;
127 
129  QApplication gemc_gui( argc, argv, (bool) use_qt );
130  // setting style if different than defulat "no"
131  string guistyle = gemcOpt.args["QTSTYLE"].args;
132  if(guistyle == "QPlastiqueStyle") gemc_gui.setStyle(new QPlastiqueStyle);
133  if(guistyle == "QCleanlooksStyle") gemc_gui.setStyle(new QCleanlooksStyle);
134  if(guistyle == "QWindowsStyle") gemc_gui.setStyle(new QWindowsStyle);
135  if(guistyle == "QMotifStyle") gemc_gui.setStyle(new QMotifStyle);
136 
137  QPixmap *splash_i = NULL;
138  QSplashScreen *splash = NULL;
139 
140 
141 #ifdef Q_OS_MAC
142 //
143 // If you are on a Mac, add the gemc.app/Contents/plugins directory to the list of paths
144 // that are searched for dynamic plugins.
145 //
146  QDir dir(QApplication::applicationDirPath());
147  dir.cdUp();
148  dir.cd("PlugIns");
149  QStringList dir_list(QLibraryInfo::location(QLibraryInfo::PluginsPath));
150  dir_list.append(dir.absolutePath());
151  QApplication::setLibraryPaths(dir_list);
152 
153 // To print the path that is searched for plugins, uncomment:
154 //
155 // foreach (QString path, gemc_gui.libraryPaths())
156 // qDebug() << "Path: " << path << endl;
157 #endif
158 
159 
161  if(use_qt)
162  {
163  splash_i = new QPixmap(CLAS12_cad);
164  splash = new QSplashScreen(*splash_i);
165  splash->show();
166  gemc_gui.processEvents();
167  }
168 
169  string msg;
170 
172  msg = " Setting CLHEP Random Engine...";
173  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
174  // CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine);
175  CLHEP::HepRandom::setTheEngine(new CLHEP::MTwistEngine);
176  string RAND = gemcOpt.args["RANDOM"].args;
177  G4int seed;
178 
179  if(RAND=="TIME")
180  {
181  msg = " Initializing CLHEP Random Engine from time, cpu clock and process id...";
182  seed = time(NULL)-clock()-getpid() ;
183  }
184  else
185  {
186  seed = atoi(RAND.c_str());
187  msg = " Initializing CLHEP Random Engine from user seed...";
188  }
189  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
190  char seeds[100];
191  sprintf(seeds, "%d", seed);
192  msg = " Random Seed Initialized to: " ;
193  msg.append(seeds);
194 
195  CLHEP::HepRandom::setTheSeed(seed);
196 
197  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
198 
200  if(gemcOpt.args["gcard"].args != "no")
201  {
202  msg = " Opening GCARD file " + gemcOpt.args["gcard"].args + "...";
203  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
204  }
205  run_conditions RunConditions(gemcOpt);
206 
207 
209  map<string, string> sim_condition;
210  // filling gcard option content
211  for(map<string, opts>::iterator it = gemcOpt.args.begin(); it != gemcOpt.args.end(); it++)
212  {
213  string key = "option: " + it->first;
214  if(it->second.type == 0) sim_condition[key] = stringify(it->second.arg);
215  else sim_condition[key] = it->second.args;
216  }
217  // filling detectors from gcard
218  for(unsigned int i=0; i<RunConditions.gTab_Vec.size(); i++)
219  {
220  string key = "detector: " + RunConditions.gTab_Vec[i];
221  sim_condition[key] = "is present";
222  }
223  // filling rotation, position modifications from gcard
224  for(map<string, gcard_detector>::iterator it = RunConditions.gDet_Map.begin(); it != RunConditions.gDet_Map.end(); it++)
225  {
226  if(it->second.get_position().mag2() != 0)
227  {
228  string key = "local shift for " + it->first;
229  sim_condition[key] = "(" + stringify(it->second.get_position().x()/mm) + ", "
230  + stringify(it->second.get_position().y()/mm) + ", "
231  + stringify(it->second.get_position().z()/mm) + ")mm";
232  }
233 
234  if(it->second.get_vrotation().mag2() != 0)
235  {
236  string key = "local rotation for " + it->first;
237  sim_condition[key] = "(" + stringify(it->second.get_vrotation().x()/degree) + ", "
238  + stringify(it->second.get_vrotation().y()/degree) + ", "
239  + stringify(it->second.get_vrotation().z()/degree) + ")deg";
240  }
241  }
242 
244  msg = " Instantiating Run Manager...";
245  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
246  G4RunManager *runManager = new G4RunManager;
247 
248 
250  msg = " Retrieving gemc Detector Map...";
251  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
252  map<string, detector> Hall_Map = read_detector(gemcOpt, RunConditions);
253 
255  msg = " Material factory selected: " + gemcOpt.args["MATERIALSDB"].args;
256  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
257  map<string, materialFactory> materialFactoriesMap = registerMaterialFactories();
258  materials *materialSelectedFactory = getMaterialFactory(&materialFactoriesMap, gemcOpt.args["MATERIALSDB"].args);
259  map<string, G4Material*> mats = materialSelectedFactory->initMaterials(RunConditions, gemcOpt);
260 
262  msg = " Parameter factory selected: " + gemcOpt.args["PARAMETERSDB"].args;
263  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
264  map<string, parameterFactory> parameterFactoriesMap = registerParameterFactories();
265  parameters *parameterSelectedFactory = getParameterFactory(&parameterFactoriesMap, gemcOpt.args["PARAMETERSDB"].args);
266  map<string, double> gParameters = parameterSelectedFactory->initParameters(RunConditions, gemcOpt);
267  // filling parameters map in sim. condition map
268  for(map<string, double>::iterator it = gParameters.begin(); it != gParameters.end(); it++)
269  {
270  string key = "parameter: " + it->first;
271  sim_condition[key] = stringify(it->second);
272  }
273 
275  msg = " Building gemc Process Hit Factory...";
276  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
277  map<string, MPHB_Factory> MProcessHit_Map = HitProcess_Map(gemcOpt.args["HIT_PROCESS_LIST"].args);
278 
280  msg = " Retrieving gemc Banks Map...";
281  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
282  map<string, MBank> MBank_Map = read_banks(gemcOpt, MProcessHit_Map);
283 
285  msg = " Retrieving gemc Magnetic Fields Map...";
286  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
287  map<string, MagneticField> FieldMap = get_magnetic_Fields(gemcOpt);
288 
289 
290 
292  MDetectorConstruction* ExpHall = new MDetectorConstruction(gemcOpt);
293  ExpHall->Hall_Map = &Hall_Map;
294  ExpHall->mats = &mats;
295  ExpHall->FieldMap = &FieldMap;
296  runManager->SetUserInitialization(ExpHall);
297 
298 
300  string phys_list = gemcOpt.args["USE_PHYSICSL"].args ;
301  msg = " Initializing Physics List " + phys_list + "...";
302  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
303 
304  G4VUserPhysicsList* physics = NULL;
305  G4VModularPhysicsList* phys = NULL;
306  G4PhysListFactory factory;
307 
308  if(phys_list == "gemc")
309  {
310  physics = new MPhysicsList(gemcOpt);
311  runManager->SetUserInitialization(physics);
312  }
313  else
314  {
315  if(factory.IsReferencePhysList(phys_list))
316  phys = factory.GetReferencePhysList(phys_list);
317 
318  if(!phys) phys = factory.ReferencePhysList();
319 
320  runManager->SetUserInitialization(phys);
321  }
322 
323  // need to use flags for both these here
324  // physics->SetDefaultCutValue(0.1*mm);
325 
326  // Setting Max step for all the simulation. This is historically needed to limit
327  // the step in magnetic field in vacuum
328  double max_step = gemcOpt.args["MAX_FIELD_STEP"].arg;
329  if(max_step != 0)
330  G4TransportationManager::GetTransportationManager()->GetPropagatorInField()->SetLargestAcceptableStep(max_step);
331 
333  msg = " Initializing Primary Generator Action...";
334  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
335  MPrimaryGeneratorAction* gen_action = new MPrimaryGeneratorAction(&gemcOpt);
336  runManager->SetUserAction(gen_action);
337 
338 
340  msg = " Initializing Event Action...";
341  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
342  MEventAction* event_action = new MEventAction(gemcOpt, gParameters);
343  event_action->SetEvtNumber((int) gemcOpt.args["EVN"].arg);
344  runManager->SetUserAction(event_action);
345 
346 
348  msg = " Initializing Stepping Action...";
349  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
350  MSteppingAction* SteppingAction = new MSteppingAction(gemcOpt);
351  runManager->SetUserAction(SteppingAction);
352 
354  // need UIQt to see Qt Open GL - have to comment line 136 of visualization/OpenGL/src/G4OpenGLQtViewer.cc
355  msg = " Initializing User Interface...";
356  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
357  G4UIsession *session = NULL;
358  if(use_qt)
359  session = new G4UIQt(argc,argv);
360 
362  G4VisManager *visManager = NULL;
363  if(use_qt)
364  {
365  visManager = new G4VisExecutive;
366  visManager->Initialize();
367  }
368 
370  msg = " Initializing Run Manager...\n";
371  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
372  runManager->Initialize();
373 
374  // Getting UI manager, restoring G4Out to cout
375  G4UImanager* UImanager = G4UImanager::GetUIpointer();
376  UImanager->SetCoutDestination(NULL);
377 
380  msg = " Initializing Output Action...";
381  MOutputs MOutput(gemcOpt);
382  map<string, MOutput_Factory> MProcessOutput_Map = Output_Map();
383 
384  // saving simulation condition in the output file
385  if(MOutput.outType != "no")
386  {
387  MOutputBaseClass *ProcessOutput = GetMOutputClass(&MProcessOutput_Map, MOutput.outType);
388  ProcessOutput->SaveSimConditions(&MOutput, sim_condition);
389  delete ProcessOutput;
390  }
391 
392  event_action->MOut = &MOutput;
393  event_action->Out = &MProcessOutput_Map;
394  event_action->MProcessHit_Map = &MProcessHit_Map;
395  event_action->SeDe_Map = ExpHall->SeDe_Map;
396  event_action->MBank_Map = &MBank_Map;
397  event_action->gen_action = gen_action;
398 
400  map<string, MSensitiveDetector*>::iterator it;
401  for(it = ExpHall->SeDe_Map.begin(); it != ExpHall->SeDe_Map.end(); it++)
402  it->second->MProcessHit_Map = &MProcessHit_Map;
403 
404 
405  msg = " Executing initial directives...\n";
406  if(use_qt) splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
407  vector<string> init_commands = init_dmesg(gemcOpt);
408  for(unsigned int i=0; i<init_commands.size(); i++)
409  UImanager->ApplyCommand(init_commands[i].c_str());
410 
411  string exec_macro = "/control/execute " + gemcOpt.args["EXEC_MACRO"].args;
412 
413 
414  if(use_qt)
415  {
416  splash->showMessage("Starting GUI...");
417  gemc_gui.processEvents();
418 
419  gemcMainWidget gemcW(runManager, visManager, &gemcOpt, ExpHall->SeDe_Map);
420  gemcW.Hall_Map = &Hall_Map;
421  gemcW.SeDe_Map = ExpHall->SeDe_Map;
422  gemcW.mats = &mats;
423 
424  gemcW.setWindowTitle( " gemc" );
425 
426  gemcW.show();
427  splash->finish(&gemcW);
428 
429  msg = " Executing initial visual directives...\n";
430  splash->showMessage(msg.c_str()); gemc_gui.processEvents(); cout << hd_msg << msg << endl;
431  vector<string> init_vcommands = init_dvmesg(gemcOpt, visManager);
432  for(unsigned int i=0; i<init_vcommands.size(); i++)
433  {
434  cout << " >> Now executing: " << init_vcommands[i].c_str() << endl;
435  UImanager->ApplyCommand(init_vcommands[i].c_str());
436  }
437 
438  if(exec_macro != "/control/execute no") UImanager->ApplyCommand(exec_macro.c_str());
439  if(gemcOpt.args["N"].arg>0)
440  {
441  char command[100];
442  sprintf(command, "/run/beamOn %d", (int) gemcOpt.args["N"].arg);
443  UImanager->ApplyCommand(command);
444  }
445 
446  return gemc_gui.exec();
447  // deleting the visManager and runManager is now taken care
448  // in the gemc_quit slot
449  // since qApp->quit() also calls an exit
450  cout << " >> Exiting gemc qApp." << endl;
451  delete visManager;
452  if(session != NULL) delete session;
453  }
454  else
455  {
456  if(gemcOpt.args["N"].arg>0)
457  {
458  char command[100];
459  sprintf(command, "/run/beamOn %d", (int) gemcOpt.args["N"].arg);
460  UImanager->ApplyCommand(command);
461  }
462  if(exec_macro != "/control/execute no") UImanager->ApplyCommand(exec_macro.c_str());
463  }
464 
465  delete runManager;
466  return 1;
467 }
468 
469 
470 
471 
472 
473 
474 
475 
vector< string > init_dmesg(gemc_opts gemcOpt)
General Initialization Routine.
Definition: dmesg_init.cc:21
vector< string > init_dvmesg(gemc_opts gemcOpt, G4VisManager *VM)
Initialization Routine for Visualization.
Definition: dmesg_init.cc:102
void SetEvtNumber(int N)
Sets Event Number.
Definition: MEventAction.h:109
materials * getMaterialFactory(map< string, materialFactory > *factory, string materialsMethod)
parameters * getParameterFactory(map< string, parameterFactory > *factory, string parametersMethod)
vector< string > gTab_Vec
Vector of SQL tables names.
map< string, MOutput_Factory > Output_Map()
map< string, MagneticField > get_magnetic_Fields(gemc_opts Opt)
Fills magnetic field maps from Database.
map< string, MBank > read_banks(gemc_opts gemcOpt, map< string, MPHB_Factory > Map)
Fills bank maps according to Hit Process Map.
Definition: MBankdefs.cc:21
map< string, MSensitiveDetector * > SeDe_Map
Sensitive detector Map.
Definition: gemc_MainGui.h:54
map< string, materialFactory > registerMaterialFactories()
map< string, MagneticField > * FieldMap
map< string, G4Material * > * mats
map< string, detector > read_detector(gemc_opts, run_conditions)
Reads detector map from database.
map< string, G4Material * > * mats
Materials Map.
Definition: gemc_MainGui.h:53
MPrimaryGeneratorAction * gen_action
Generator Action.
Definition: MEventAction.h:96
map< string, gcard_detector > gDet_Map
Map of gcard_detector. Map Key = detector name.
map< string, MBank > * MBank_Map
Bank Map.
Definition: MEventAction.h:94
string stringify(double x)
MOutputBaseClass * GetMOutputClass(map< string, MOutput_Factory > *MProcessOutput_Map, string outputType)
Instantiates MOutputBaseClass.
map< string, MOutput_Factory > * Out
MOutput_Factory map.
Definition: MEventAction.h:91
string outType
virtual void SaveSimConditions(MOutputs *, map< string, string >)=0
Pure Virtual Method to save the simulation conditions on the file.
MOutputs * MOut
MOutputs class - contains the output format.
Definition: MEventAction.h:90
virtual map< string, G4Material * > initMaterials(run_conditions, gemc_opts)=0
map< string, opts > args
Options map.
Definition: usage.h:68
map< string, detector > * Hall_Map
Detectors Map.
Definition: gemc_MainGui.h:52
map< string, MSensitiveDetector * > SeDe_Map
Sensitive detector Map.
Definition: MEventAction.h:92
virtual map< string, double > initParameters(run_conditions, gemc_opts)=0
map< string, detector > * Hall_Map
map< string, parameterFactory > registerParameterFactories()
map< string, MSensitiveDetector * > SeDe_Map
int main(int argc, char **argv)
Main Program
Definition: gemc.cc:118
map< string, MPHB_Factory > HitProcess_Map(string experiments)
< Hit in Converter
map< string, MPHB_Factory > * MProcessHit_Map
Hit Process Routine Factory Map.
Definition: MEventAction.h:93
int Set(int argc, char **args)
Sets map from command line arguments.
Definition: usage.cc:718