GGS(GenericGEANT4Simulation)Software  2.7.1
 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 
11 #include "utils/GGSSmartLog.h"
12 
13 #include "G4EventManager.hh"
14 #include "G4GenericMessenger.hh"
15 #include "G4LogicalVolumeStore.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),
33  _simAgainKilledEv(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()
76  << " (total: " << _nDiscardedEvsInKilledEvs << ")" << ENDL;
77  }
78  if (_simAgainKilledEv) {
79  i_event--;
80  CCOUT(DEEPDEB) << "Will be simulated again." << ENDL;
81  }
82  } else {
83  COUT(DEEPDEB) << "Finished simulating event " << i_event << ENDL;
84  if (_ggsGeneratorAction) {
85  CCOUT(DEEPDEB) << "Discarded at generation: " << _ggsGeneratorAction->GetNDiscarded()
86  << " (total: " << GetNDiscardedEvents() << ")" << ENDL;
87  }
88  CCOUT(DEEPDEB) << "Killed: " << _nKilledEvs << ENDL;
89  // Reset discarded and killed event counters
90  _nDiscardedEvsInKilledEvs = 0;
91  _nKilledEvs = 0;
92  if (_simAgainKilledEv) {
93  static bool printDone = false;
94  if (!printDone) {
95  COUT(WARNING) << "Request to re-simulate an event which has not been flagged as killed. Ignoring." << ENDL;
96  CCOUT(WARNING) << "This message will be printed only once." << ENDL;
97  printDone = true;
98  }
99  }
100  }
101  _isCurrEvKilled = false;
102  _simAgainKilledEv = false;
103  }
104 
105  TerminateEventLoop();
106 }
107 
108 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
109 
111  static const std::string routineName("GGSRunManager::PrintLogVols");
112  G4LogicalVolumeStore *logVolStore = G4LogicalVolumeStore::GetInstance();
113 
114  COUT(INFO) << "Logical volumes in current geometry:" << ENDL;
115  for (G4LogicalVolumeStore::const_iterator iter = logVolStore->begin(); iter != logVolStore->end(); iter++) {
116  CCOUT(INFO) << " * " << (*iter)->GetName() << ENDL;
117  }
118 }
119 
120 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
121 
123  static const std::string routineName("GGSRunManager::KillEvent");
124  if (!_isCurrEvKilled) {
125  COUT(DEEPDEB) << "Kill event" << std::endl;
126  G4EventManager::GetEventManager()->AbortCurrentEvent();
127  _isCurrEvKilled = true;
128  _nKilledEvs++;
129  if (_ggsGeneratorAction)
130  _nDiscardedEvsInKilledEvs += _ggsGeneratorAction->GetNDiscarded();
131  }
132 }
133 
134 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
135 
136 void GGSRunManager::SimulateAgainKilledEvent() { _simAgainKilledEv = true; }
137 
138 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
139 
140 bool GGSRunManager::IsCurrentEventKilled() { return _isCurrEvKilled; }
~GGSRunManager()
Destructor.
#define ENDL
Definition: GGSSmartLog.h:105
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:76
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:100
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:85
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