ATLAS Offline Software
TestActionTimer.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // //
7 // TestActionTimer //
8 // Code for text output (into the athena.out files) //
9 // of information about the time spent simulating //
10 // various pieces of the detector and particles. //
11 // //
12 // Written by Zachary Marshall //
13 // Caltech //
14 // zmarshal@caltech.edu //
15 // //
16 // Last update 08.07.08 //
17 // //
19 
20 
21 #include "TestActionTimer.h"
22 
23 #include "G4Version.hh"
24 #include "G4Run.hh"
25 #include "G4Event.hh"
26 #include "G4Step.hh"
27 
28 #include "G4String.hh"
29 #include "G4Timer.hh"
30 #include "G4Track.hh"
31 #include "G4Electron.hh"
32 #include "G4Gamma.hh"
33 #include "G4Positron.hh"
34 #include "G4Neutron.hh"
35 
36 // For file output
37 #include "GaudiKernel/ITHistSvc.h"
38 #include "TH1D.h"
39 #include <string_view>
40 
41 // #define _myDebug
42 
43 
44 namespace G4UA
45 {
46  // Helper for G4String changes
47  bool G4StrContains(const G4String& s, const char* v)
48  {
49 #if G4VERSION_NUMBER < 1100
50  return s.contains(v);
51 #else
52  return G4StrUtil::contains(s, v);
53 #endif
54  }
55 
56  bool G4StrContains(const std::string_view& s, const char* v)
57  {
58  return s.find(v) != std::string_view::npos;
59  }
60 
62  : m_report(),
63  m_runTimer(0),
64  m_eventTimer(0),
65  //m_runTime(0.),
66  m_eventTime(0.)//,
67  //m_histSvc("THistSvc",name)
68  {
69  m_report.timeName.resize(eMax);
70  m_report.timeName[eEMB] = "EMB";
71  m_report.timeName[eEMEC] = "EMEC";
72  m_report.timeName[eFC1] = "FC1";
73  m_report.timeName[eFC23] = "FC23";
74  m_report.timeName[eFCO] = "FCO";
75  m_report.timeName[eHEC] = "HEC";
76  m_report.timeName[eCry] = "Cry";
77  m_report.timeName[eLAr] = "LAr";
78  m_report.timeName[eHCB] = "HCB";
79  m_report.timeName[eITkPix] = "ITkPixel";
80  m_report.timeName[eITkStrip] = "ITkStrip";
81  m_report.timeName[ePre] = "Pre";
82  m_report.timeName[eMu] = "Mu";
83  m_report.timeName[ePx] = "Px";
84  m_report.timeName[eTrt] = "TRT";
85  m_report.timeName[eSev] = "IDServ";
86  m_report.timeName[eSct] = "SCT";
87  m_report.timeName[eOther] = "Other";
88 
89  m_report.timeName[eElec] = "Elec";
90  m_report.timeName[ePos] = "Pos";
91  m_report.timeName[eGam] = "Gam";
92  m_report.timeName[eNeut] = "Neut";
93 
94  G4cout<< "TestActionTimer::Constructor: Labels "
95  <<" Run Event ";
96  for (int i(0); i < eMax; ++i) G4cout << m_report.timeName[i] << " ";
97  G4cout << "Particle Dead" << G4endl;
98 
99  // init timers
100 
101  m_runTimer = new G4Timer();
102  m_runTimer->Start();
103 
104  // create all timers, start and stop, init counters
105  for (int i(0); i < eMax; ++i) {
106  G4Timer* timer = new G4Timer();
107  timer->Start();
108 
109  m_timer.push_back(timer);
110  m_report.time.push_back(0.);
111  }
112 
113  PPanic();
114  VPanic();
115 
116 #ifdef _myDebug
117  G4cout << "TestActionTimer::Constructor done" << G4endl;
118 #endif
119  }
120 
122  {
123  m_report.nev++;
124 
125  if (m_eventTimer != 0) {
126  delete m_eventTimer;
127  m_eventTimer = 0;
128  }
129  m_eventTime = 0.;
130  m_eventTimer = new G4Timer();
131  m_eventTimer->Start();
132 
133  m_runTimer->Start();
134  }
135 
137  {
140 
141  VPanic();
142  PPanic();
143  }
144 
146  {
147  m_report.runTime=0.;
148  m_runTimer->Start();
149  }
150 
152  {
153  std::cerr<<"TestActionTimer::EndOfRunAction "<< m_report.runTime <<std::endl;
155  VPanic();
156  PPanic();
157  }
158 
159  void TestActionTimer::UserSteppingAction(const G4Step* aStep)
160  {
161  // HERE IS WHERE WE BEGIN OUR CLOCKING
162 
163  // Get basic information about the event
164  G4Track* track = aStep->GetTrack();
165 
166  G4String thePrePVname = track->GetVolume()->GetName();
167  G4String thePostPVname;
168  if (track->GetNextVolume() != 0) {
169  thePostPVname = track->GetNextVolume()->GetName();
170  } else {
171  thePostPVname = "OutOfWorld";
172  }
173 
174  // Check what volume we are in. If the volume does not agree with the clock, kill the clock
175  G4Timer* timer = 0;
176  int preIndex = ClassifyVolume( thePrePVname );
177  int postIndex=-1;
178  if (thePrePVname != thePostPVname){
179  timer = m_timer[preIndex];
180  if (!timer->IsValid()){ m_report.time[preIndex] += TimerSum(timer); } else { VPanic(); }
181 
182  // Now start the appropriate clock
183  postIndex = ClassifyVolume( thePostPVname );
184  m_timer[postIndex]->Start();
185 
186  // Otherwise it's going into "out of world"
187  } else {
188  timer = m_timer[preIndex];
189  if (timer->IsValid()) {
190  VPanic(); timer->Start();
191  }
192  }
193 
194  // Now for the particle based timers
195  if (track->GetDefinition() == G4Electron::ElectronDefinition() ){
196  if ( m_timer[eElec]->IsValid() ){
197  m_timer[eElec]->Start();
198  }
199  } else if (!m_timer[eElec]->IsValid()){
201  }
202  if (track->GetDefinition() == G4Positron::PositronDefinition() ){
203  if ( m_timer[ePos]->IsValid() ){
204  m_timer[ePos]->Start();
205  }
206  } else if (!m_timer[ePos]->IsValid()){
208  }
209  if (track->GetDefinition() == G4Gamma::GammaDefinition() &&
210  m_timer[eGam]->IsValid()){
211  m_timer[eGam]->Start();
212  } else if ( !m_timer[eGam]->IsValid() ){
214  }
215  if (track->GetDefinition() == G4Neutron::NeutronDefinition() &&
216  m_timer[eNeut]->IsValid()){
217  m_timer[eNeut]->Start();
218  } else if ( !m_timer[eNeut]->IsValid() ){
220  }
221  }
222 
223 
224  double TestActionTimer::TimerSum(G4Timer* timer) const
225  {
226  if (timer == 0) return -999.;
227  timer->Stop();
228  return (timer->GetUserElapsed() + timer->GetSystemElapsed());
229  }
230 
232  {
233 #ifdef _myDebug
234  G4cout << "TestActionTimer::PPanic" << G4endl;
235 #endif
236 
237  // stop all particle counters and add time
238  for (int i(eElec); i < eMax; ++i) {
239  G4Timer* timer = m_timer[i];
240 #ifdef _myDebug
241  G4cout << "TestActionTimer::PPanic stopping counter i:" << i << " " << timer << G4endl;
242 #endif
243  if (!timer->IsValid()){
245  }
246  }
247 
248 #ifdef _myDebug
249  G4cout << "TestActionTimer::PPanic done" << G4endl;
250 #endif
251  }
252 
254  {
255 #ifdef _myDebug
256  G4cout << "TestActionTimer::VPanic" << G4endl;
257 #endif
258 
259  // stop all volume counters and add time
260  for (int i(0); i <= eOther; ++i) {
261  G4Timer* timer = m_timer[i];
262 #ifdef _myDebug
263  G4cout << "TestActionTimer::VPanic stopping counter i:" << i << " " << timer << G4endl;
264 #endif
265  if (!timer->IsValid()){
267  }
268  }
269 
270 #ifdef _myDebug
271  G4cout << "TestActionTimer::VPanic done" << G4endl;
272 #endif
273  }
274 
275  int TestActionTimer::ClassifyVolume( G4String& nomstr ) const
276  {
277  std::string_view nom(nomstr); //Avoid copying characters during comparison
278  if( nom.length() >= 17 &&
279  nom.substr(13,4) == "EMEC" ){
280  return eEMEC;
281  }
282  else if ( nom.length() >= 16 &&
283  nom.substr(13,3) == "EMB" ){
284  return eEMB;
285  }
286  else if( nom.length() >= 25 &&
287  nom.substr(21,4) == "Cryo" ) {
288  return eCry;
289  }
290  else if( nom.length() >= 26 &&
291  nom.substr(13,13) == "FCAL::Module1"){
292  return eFC1;
293  }
294  else if( nom.length() >= 25 &&
295  nom.substr(13,12) == "FCAL::Module" ){
296  return eFC23;
297  }
298  else if ( nom.length() >= 17 &&
299  nom.substr(13,4) == "FCAL" ){
300  return eFCO;
301  }
302  else if ( nom.length() >= 16 &&
303  nom.substr(13,3) == "HEC" ){
304  return eHEC;
305  }
306  else if( nom.length() >= 31 &&
307  nom.substr(21,10) == "Presampler" ) {
308  return ePre;
309  }
310  else if ( nom.length() >= 3 &&
311  nom.substr(0,3) == "LAr" ){
312  return eLAr;
313  }
314  else if( ( (nom.length() >= 4 &&
315  nom.substr(0,4) == "Muon") ||
316  nom.substr(0,4) == "MUON" ) ||
317  ( nom.length() >= 9 &&
318  nom.substr(0,9) == "DriftTube" ) ||
319  G4StrContains(nom, "MDT") ||
320  ( nom.length() >= 12 &&
321  nom.substr(0,12) == "SensitiveGas" ) ||
322  G4StrContains(nom, "MDT") ||
323  G4StrContains(nom, "station") ){
324  return eMu;
325  }
326  else if ( nom.length() >= 8 &&
327  nom.substr(0,8) == "ITkPixel" ){
328  return eITkPix;
329  }
330  else if ( nom.length() >= 8 &&
331  nom.substr(0,8) == "ITkStrip" ){
332  return eITkStrip;
333  }
334  else if ((nom.length() >= 5 &&
335  nom.substr(0,5) == "Pixel") ||
336  nom == "Outside Barrel Service"){
337  return ePx;
338  }
339  else if ( nom.length() >= 3 &&
340  nom.substr(0,3) == "SCT" ){
341  return eSct;
342  }
343  else if ( ( nom.length() >= 3 &&
344  nom.substr(0,3) == "TRT" ) ||
345  nom == "GasMANeg" ){
346  return eTrt;
347  }
348  else if ( nom.length() >= 4 &&
349  nom.substr(0,4) == "Tile"){
350  return eHCB;
351  }
352  else if ( ( nom.length() >= 12 &&
353  nom.substr(0,12) == "InDetServMat" ) ||
354  ( nom.length() >= 4 &&
355  nom.substr(0,4) == "IDET" ) ||
356  ( nom.length() >= 3 &&
357  nom.substr(0,3) == "ITK" ) ||
358  ( nom.length() >= 8 &&
359  nom.substr(0,8) == "BeamPipe" ) ||
360  ( nom.length() >= 7 &&
361  nom.substr(0,7) == "Section" ) ||
362  ( nom.length() >= 3 &&
363  ( nom.substr(0,3) == "BLM" ||
364  nom.substr(0,3) == "BCM" ||
365  nom.substr(0,3) == "PLR" ) ) ||
366  ( nom.length() >= 8 &&
367  nom.substr(0,8) == "BCMPrime" ) ){
368  return eSev;
369  }
370  return eOther;
371  }
372 
373 } // namespace G4UA
G4UA::TestActionTimer::Report::time
std::vector< double > time
Vector of timers for each of the enum.
Definition: TestActionTimer.h:67
G4UA::TestActionTimer::eCry
@ eCry
Definition: TestActionTimer.h:97
G4UA::TestActionTimer::TimerSum
double TimerSum(G4Timer *timer) const
Gets the appropriate time from the timer for adding to the sum.
Definition: TestActionTimer.cxx:224
G4UA::TestActionTimer::PPanic
void PPanic()
Method to shut down all particle timers.
Definition: TestActionTimer.cxx:231
G4UA::TestActionTimer::eEMEC
@ eEMEC
Definition: TestActionTimer.h:97
G4UA::TestActionTimer::eHCB
@ eHCB
Definition: TestActionTimer.h:97
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
G4UA::TestActionTimer::m_report
Report m_report
Definition: TestActionTimer.h:103
G4UA::TestActionTimer::Report::nev
int nev
Definition: TestActionTimer.h:66
G4UA::TestActionTimer::m_runTimer
G4Timer * m_runTimer
Timer for the entire run.
Definition: TestActionTimer.h:105
G4UA
for nSW
Definition: CalibrationDefaultProcessing.h:19
G4UA::TestActionTimer::m_timer
std::vector< G4Timer * > m_timer
Vector of timers for each of the enum.
Definition: TestActionTimer.h:109
G4UA::TestActionTimer::UserSteppingAction
virtual void UserSteppingAction(const G4Step *) override
Definition: TestActionTimer.cxx:159
G4UA::TestActionTimer::ePos
@ ePos
Definition: TestActionTimer.h:99
G4UA::TestActionTimer::EndOfEventAction
virtual void EndOfEventAction(const G4Event *) override
Definition: TestActionTimer.cxx:136
G4UA::TestActionTimer::eTrt
@ eTrt
Definition: TestActionTimer.h:98
G4UA::TestActionTimer::eFCO
@ eFCO
Definition: TestActionTimer.h:97
G4UA::TestActionTimer::eSev
@ eSev
Definition: TestActionTimer.h:98
G4UA::TestActionTimer::eNeut
@ eNeut
Definition: TestActionTimer.h:99
G4UA::TestActionTimer::EndOfRunAction
virtual void EndOfRunAction(const G4Run *) override
Definition: TestActionTimer.cxx:151
python.utils.AtlRunQueryTimer.timer
def timer(name, disabled=False)
Definition: AtlRunQueryTimer.py:86
G4UA::TestActionTimer::ePre
@ ePre
Definition: TestActionTimer.h:98
G4UA::TestActionTimer::m_eventTimer
G4Timer * m_eventTimer
Timer for this event.
Definition: TestActionTimer.h:106
G4UA::TestActionTimer::eGam
@ eGam
Definition: TestActionTimer.h:99
G4UA::TestActionTimer::eLAr
@ eLAr
Definition: TestActionTimer.h:97
G4UA::G4StrContains
bool G4StrContains(const G4String &s, const char *v)
Definition: TestActionTimer.cxx:47
TestActionTimer.h
lumiFormat.i
int i
Definition: lumiFormat.py:85
G4UA::TestActionTimer::eMax
@ eMax
Definition: TestActionTimer.h:99
G4UA::TestActionTimer::eHEC
@ eHEC
Definition: TestActionTimer.h:97
contains
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition: hcg.cxx:111
G4UA::TestActionTimer::TestActionTimer
TestActionTimer()
constructor
Definition: TestActionTimer.cxx:61
G4UA::TestActionTimer::eFC23
@ eFC23
Definition: TestActionTimer.h:97
G4UA::TestActionTimer::ePx
@ ePx
Definition: TestActionTimer.h:98
G4UA::TestActionTimer::eFC1
@ eFC1
Definition: TestActionTimer.h:97
PlotSFuncertainty.nom
nom
Definition: PlotSFuncertainty.py:141
G4UA::TestActionTimer::eITkStrip
@ eITkStrip
Definition: TestActionTimer.h:98
G4UA::TestActionTimer::BeginOfEventAction
virtual void BeginOfEventAction(const G4Event *) override
Definition: TestActionTimer.cxx:121
G4UA::TestActionTimer::eEMB
@ eEMB
Definition: TestActionTimer.h:97
python.PyAthena.v
v
Definition: PyAthena.py:154
G4UA::TestActionTimer::eSct
@ eSct
Definition: TestActionTimer.h:98
G4UA::TestActionTimer::VPanic
void VPanic()
Method to shut down all volume timers.
Definition: TestActionTimer.cxx:253
G4UA::TestActionTimer::eElec
@ eElec
Definition: TestActionTimer.h:99
G4UA::TestActionTimer::eMu
@ eMu
Definition: TestActionTimer.h:98
G4UA::TestActionTimer::eITkPix
@ eITkPix
Definition: TestActionTimer.h:98
G4UA::TestActionTimer::m_eventTime
double m_eventTime
Double for storing this event.
Definition: TestActionTimer.h:107
G4UA::TestActionTimer::ClassifyVolume
int ClassifyVolume(G4String &) const
Method to sort out which volume we are in.
Definition: TestActionTimer.cxx:275
G4UA::TestActionTimer::BeginOfRunAction
virtual void BeginOfRunAction(const G4Run *) override
Definition: TestActionTimer.cxx:145
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
G4UA::TestActionTimer::eOther
@ eOther
Definition: TestActionTimer.h:98
G4UA::TestActionTimer::Report::runTime
double runTime
Definition: TestActionTimer.h:69
G4UA::TestActionTimer::Report::timeName
std::vector< std::string > timeName
Vector of names for each of the timers.
Definition: TestActionTimer.h:68