GGS(GenericGEANT4Simulation)Software  2.6.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
GGSRunManager.cpp
Go to the documentation of this file.
1 /*
2  * GGSRunManager.cpp
3  *
4  * Created on: 19/ott/2013
5  * Author: mori
6  */
7 
10 #include "utils/GGSSmartLog.h"
12 
13 #include "G4GenericMessenger.hh"
14 #include "G4LogicalVolumeStore.hh"
15 #include "G4EventManager.hh"
16 #include "Randomize.hh"
17 
18 GGSRunManager* GGSRunManager::_runManager = 0;
19 
20 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
21 
23  if (!_runManager) {
24  G4Exception("GGSRunManager::GetRunManager()", "GGS", FatalException, "GGSRunManager not yet constructed.");
25  }
26  return _runManager;
27 }
28 
29 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
30 
32  G4RunManager(), _ggsGeneratorAction(NULL), _nDiscardedEvsInKilledEvs(0), _nKilledEvs(0), _isCurrEvKilled(false), _simAgainKilledEv(
33  false) {
34  if (_runManager) {
35  G4Exception("GGSRunManager::GGSRunManager()", "GGS", FatalException, "GGSRunManager constructed twice.");
36  }
37  _runManager = this;
38 
39  _messenger = new G4GenericMessenger(this, "/GGS/");
40  _messenger->DeclareMethod("printLogVols", &GGSRunManager::PrintLogVols, "Print a list of logical volumes.");
41  _currEvSeeds[0] = _currEvSeeds[1] = 0;
42 }
43 
44 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
45 
47  _runManager = NULL;
48  delete _messenger;
49 }
50 
51 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
52 
53 void GGSRunManager::DoEventLoop(G4int n_event, const char* macroFile, G4int n_select) {
54  static const std::string routineName("GGSRunManager::DoEventLoop");
55  InitializeEventLoop(n_event, macroFile, n_select);
56  _isCurrEvKilled = false;
57  _simAgainKilledEv = false;
58  _nKilledEvs = 0;
59 
60  // Event loop
61  for (G4int i_event = 0; i_event < n_event; i_event++) {
62  // Store random seeds at begin of event
63  _currEvSeeds[0] = CLHEP::HepRandom::getTheSeeds()[0];
64  _currEvSeeds[1] = CLHEP::HepRandom::getTheSeeds()[1];
65 
66  ProcessOneEvent(i_event);
67  TerminateOneEvent();
68  if (runAborted)
69  break;
70 
71  // Check if the event has been killed
72  if (_isCurrEvKilled) {
73  COUT(DEEPDEB) << "Event " << i_event << " has been killed. " << ENDL;
74  if(_ggsGeneratorAction) {
75  CCOUT(DEEPDEB) << "Discarded at generation: " << _ggsGeneratorAction->GetNDiscarded() << " (total: " << _nDiscardedEvsInKilledEvs <<")" << ENDL;
76  }
77  if (_simAgainKilledEv) {
78  i_event--;
79  CCOUT(DEEPDEB) << "Will be simulated again." << ENDL;
80  }
81  }
82  else {
83  COUT(DEEPDEB) << "Finished simulating event " << i_event << ENDL;
84  if(_ggsGeneratorAction) {
85  CCOUT(DEEPDEB) << "Discarded at generation: " << _ggsGeneratorAction->GetNDiscarded() << " (total: " << GetNDiscardedEvents() <<")" << ENDL;
86  }
87  CCOUT(DEEPDEB) << "Killed: " << _nKilledEvs << ENDL;
88  // Reset discarded and killed event counters
89  _nDiscardedEvsInKilledEvs = 0;
90  _nKilledEvs = 0;
91  if (_simAgainKilledEv) {
92  static bool printDone = false;
93  if (!printDone) {
94  COUT(WARNING) << "Request to re-simulate an event which has not been flagged as killed. Ignoring." << ENDL;
95  CCOUT(WARNING) << "This message will be printed only once." << ENDL;
96  printDone = true;
97  }
98  }
99  }
100  _isCurrEvKilled = false;
101  _simAgainKilledEv = false;
102  }
103 
104  TerminateEventLoop();
105 }
106 
107 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
108 
110  static const std::string routineName("GGSRunManager::PrintLogVols");
111  G4LogicalVolumeStore *logVolStore = G4LogicalVolumeStore::GetInstance();
112 
113  COUT(INFO) << "Logical volumes in current geometry:" << ENDL;
114  for (G4LogicalVolumeStore::const_iterator iter = logVolStore->begin(); iter != logVolStore->end(); iter++) {
115  CCOUT(INFO) << " * " << (*iter)->GetName() << ENDL;
116  }
117 }
118 
119 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
120 
122  static const std::string routineName("GGSRunManager::KillEvent");
123  if (!_isCurrEvKilled) {
124  COUT(DEEPDEB) << "Kill event" << std::endl;
125  G4EventManager::GetEventManager()->AbortCurrentEvent();
126  _isCurrEvKilled = true;
127  _nKilledEvs++;
128  if (_ggsGeneratorAction)
129  _nDiscardedEvsInKilledEvs += _ggsGeneratorAction->GetNDiscarded();
130  }
131 }
132 
133 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
134 
136  _simAgainKilledEv = true;
137 }
138 
139 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
140 
142  return _isCurrEvKilled;
143 }
~GGSRunManager()
Destructor.
#define ENDL
Definition: GGSSmartLog.h:93
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:66
void DoEventLoop(G4int n_event, const char *macroFile, G4int n_select)
Override of G4RunManager::DoEventLoop.
GGSRunManager()
Constructor.
void PrintLogVols()
Print a list of logical volumes in current geometries.
void SimulateAgainKilledEvent()
Simulates again a killed event.
#define CCOUT(level)
Smart log utility which prints no header at the beginning of the line.
Definition: GGSSmartLog.h:88
bool IsCurrentEventKilled()
Getter method for killed event flag.
static GGSRunManager * GetRunManager()
Static getter function the run manager.
int GetNDiscardedEvents()
Getter method for number of discarded events.
Definition: GGSRunManager.h:86
void KillEvent()
Kills the current event.
unsigned int GetNDiscarded() const
Returns the number of discarded events for the current event generation.
A run manager for GGS simulations.
Definition: GGSRunManager.h:23