ATLAS Offline Software
G4SimTimer.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Local includes
6 #include "G4SimTimer.h"
7 
8 // Infrastructure includes
9 
10 namespace G4UA
11 {
12 
13  //---------------------------------------------------------------------------
14  // Constructor for timing results struct
15  //---------------------------------------------------------------------------
17  : nEvent(0), eventTime(0), eventTimeSquared(0)
18  {}
19 
20  //---------------------------------------------------------------------------
21  // Calculate the mean and sample standard deviation
22  //---------------------------------------------------------------------------
23  std::pair<double, double> G4SimTimer::Report::meanAndSigma()
24  {
25  double mean = nEvent > 0 ? eventTime / nEvent : -1.;
26  double sigma = -1.;
27  if(nEvent > 1)
28  sigma = sqrt( (eventTimeSquared/nEvent - mean*mean) / (nEvent-1) );
29  return std::make_pair(mean, sigma);
30  }
31 
32  //---------------------------------------------------------------------------
33  // G4SimTimer constructor
34  //---------------------------------------------------------------------------
36  : AthMessaging("G4SimTimer"),
37  m_firstEvent(true)
38  {}
39 
40  //---------------------------------------------------------------------------
41  // Begin-event action
42  //---------------------------------------------------------------------------
43  void G4SimTimer::BeginOfEventAction(const G4Event* /*event*/)
44  {
45  //ATH_MSG_INFO("beginOfEvent");
46  m_eventTimer.Start();
47  }
48 
49  //---------------------------------------------------------------------------
50  // End-event action
51  //---------------------------------------------------------------------------
52  void G4SimTimer::EndOfEventAction(const G4Event* /*event*/)
53  {
54  m_eventTimer.Stop();
55  // We define time as user+system time.
56  auto eventTime = m_eventTimer.GetUserElapsed() + m_eventTimer.GetSystemElapsed();
57  // Skip the first event
58  if(!m_firstEvent) {
59  m_results.nEvent++;
60  m_results.eventTime += eventTime;
61  m_results.eventTimeSquared += eventTime*eventTime;
62  auto meanSigma = m_results.meanAndSigma();
63  ATH_MSG_INFO("Event " << m_results.nEvent << " took " <<
64  std::setprecision(4) << eventTime << " s. New average " <<
65  std::setprecision(4) << meanSigma.first << " +- " <<
66  std::setprecision(4) << meanSigma.second);
67  }
68  else m_firstEvent = false;
69  }
70 
71 } // namespace G4UA
pdg_comparison.sigma
sigma
Definition: pdg_comparison.py:324
G4UA::G4SimTimer::Report::eventTime
double eventTime
Accumulated event time.
Definition: G4SimTimer.h:54
mean
void mean(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
Definition: dependence.cxx:254
G4UA::G4SimTimer::Report::eventTimeSquared
double eventTimeSquared
Accumulated squared event time.
Definition: G4SimTimer.h:56
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
G4UA::G4SimTimer::G4SimTimer
G4SimTimer()
Constructor.
Definition: G4SimTimer.cxx:35
G4UA
for nSW
Definition: CalibrationDefaultProcessing.h:19
G4UA::G4SimTimer::Report::nEvent
unsigned int nEvent
Number of timed G4 events (we skip the first).
Definition: G4SimTimer.h:52
G4UA::G4SimTimer::BeginOfEventAction
virtual void BeginOfEventAction(const G4Event *event) override final
Start timing this Geant4 event.
Definition: G4SimTimer.cxx:43
G4UA::G4SimTimer::Report::meanAndSigma
std::pair< double, double > meanAndSigma()
Calculate the mean and sample std dev.
Definition: G4SimTimer.cxx:23
G4UA::G4SimTimer::m_eventTimer
G4Timer m_eventTimer
My private instance of an event timer.
Definition: G4SimTimer.h:83
G4UA::G4SimTimer::EndOfEventAction
virtual void EndOfEventAction(const G4Event *event) override final
Finish timing this Geant4 event.
Definition: G4SimTimer.cxx:52
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
G4UA::G4SimTimer::m_results
Report m_results
My timing results.
Definition: G4SimTimer.h:86
G4UA::G4SimTimer::m_firstEvent
bool m_firstEvent
Used to skip the first event.
Definition: G4SimTimer.h:89
G4UA::G4SimTimer::Report::Report
Report()
Initializes the variables.
Definition: G4SimTimer.cxx:16
G4SimTimer.h