LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
ArtG4RunManager.cc
Go to the documentation of this file.
1 //
2 // Override the G4RunManager class so that the ArtG4 framework can drive
3 // the event loop.
4 //
5 // Original author Rob Kutschke
6 //
7 //
8 // Notes:
9 // 1) In G4RunManager the counter i_event is used as the event number.
10 // In this code it is taken from the event number of the art::event.
11 //
12 // Implementation file for @ArtG4RunManager@
13 
14 // ArtG4 includes.
16 
17 // Includes from G4.
18 #include "Geant4/G4Run.hh"
19 #include "Geant4/G4Timer.hh"
20 #include "Geant4/G4UImanager.hh"
21 
22 using namespace std;
23 
24 namespace artg4tk {
25 
26  // If the c'tor is called a second time, the c'tor of base will
27  // generate an exception.
28  ArtG4RunManager::ArtG4RunManager()
29  : G4RunManager()
30  , macroFile_(0)
31  , n_select_(-1)
32  , nProcessed_(0)
33  , realElapsed_(0.)
34  , systemElapsed_(0.)
35  , userElapsed_(0.)
36  , msg_("")
37  {}
38 
39  // Destructor of base is called automatically. No need to do anything.
41 
42  // Do the "begin run" parts of BeamOn.
43  void
44  ArtG4RunManager::BeamOnBeginRun(unsigned int runNumber, const char* macroFile, G4int n_select)
45  {
46 
47  SetRunIDCounter(runNumber);
48 
49  bool cond = ConfirmBeamOnCondition();
50  if (!cond) {
51  // throw here
52  return;
53  }
54 
55  // Initialize member data for the new run.
56  macroFile_ = macroFile;
57  n_select_ = n_select;
58  msg_ = "";
59  nProcessed_ = 0;
60  realElapsed_ = 0;
61  systemElapsed_ = 0;
62  userElapsed_ = 0;
63 
64  // Construct command to execute the macro.
65  if (macroFile_ != 0) {
66  msg_ = "/control/execute ";
67  msg_ += macroFile_;
68  } else {
69  n_select_ = -1;
70  }
71 
72  numberOfEventToBeProcessed = 1;
73  ConstructScoringWorlds();
74  RunInitialization();
75  }
76 
77  // Do the "per event" part of DoEventLoop.
78  void
80  {
81 
82  timer->Start();
83 
84  // This is the body of the event loop from DoEventLoop().
85  currentEvent = GenerateEvent(eventNumber);
86  eventManager->ProcessOneEvent(currentEvent);
87  AnalyzeEvent(currentEvent);
88  UpdateScoring();
89 
90  if (nProcessed_ < n_select_)
91  G4UImanager::GetUIpointer()->ApplyCommand(msg_);
92 
93  // Should pause, not stop, if I can do that.
94  timer->Stop();
95 
96  // Accumulate time spent in G4 for all events in this run.
97  realElapsed_ += timer->GetRealElapsed();
98  systemElapsed_ += timer->GetSystemElapsed();
99  userElapsed_ += timer->GetUserElapsed();
100 
101  if (verboseLevel > 0) {
102 
103  G4int oldPrecision = G4cout.precision(3);
104  std::ios::fmtflags oldFlags = G4cout.flags();
105  G4cout.setf(std::ios::fixed, std::ios::floatfield);
106 
107  G4cout << "TimeEvent> " << eventNumber << " "
108  << G4RunManager::GetRunManager()->GetCurrentRun()->GetRunID() << " "
109  << timer->GetRealElapsed() << " "
110  << timer->GetUserElapsed() + timer->GetSystemElapsed() << " "
111  << userElapsed_ + systemElapsed_ << G4endl;
112 
113  G4cout.setf(oldFlags);
114  G4cout.precision(oldPrecision);
115  }
116  }
117 
118  void
120  {
121 
122  StackPreviousEvent(currentEvent);
123  currentEvent = 0;
124 
125  ++nProcessed_;
126  }
127 
128  // Do the "end of run" parts of DoEventLoop and BeamOn.
129  void
131  {
132 
133  // From G4RunManager::DoEventLoop
134  if (verboseLevel > 0) {
135 
136  G4cout << "G4Run terminated." << G4endl;
137  G4cout << "G4Run Summary" << G4endl;
138  if (runAborted) {
139  G4cout << " G4Run Aborted after " << nProcessed_ << " events processed." << G4endl;
140  } else {
141  G4cout << " Number of events processed : " << nProcessed_ << G4endl;
142  }
143  G4cout << " User=" << userElapsed_ << "s Real=" << realElapsed_ << "s Sys=" << systemElapsed_
144  << G4endl;
145 
146  G4int oldPrecision = G4cout.precision(3);
147  std::ios::fmtflags oldFlags = G4cout.flags();
148  G4cout.setf(std::ios::fixed, std::ios::floatfield);
149 
150  G4cout << "TimeReport> Time report complete in ";
151  G4cout << realElapsed_;
152  G4cout << " seconds" << G4endl;
153 
154  G4cout.setf(oldFlags);
155  G4cout.precision(oldPrecision);
156  }
157 
158  // From G4RunManager::BeamOn.
159  RunTermination();
160  }
161 
162 } // end namespace artg4tk
virtual void BeamOnDoOneEvent(int eventNumber)
STL namespace.
virtual void BeamOnBeginRun(unsigned int runNumber, const char *macroFile=0, G4int n_select=-1)