ATLAS Offline Software
Loading...
Searching...
No Matches
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#include <G4Threading.hh>
8
9// Infrastructure includes
10
11namespace G4UA
12{
13
14 //---------------------------------------------------------------------------
15 // Constructor for timing results struct
16 //---------------------------------------------------------------------------
20
21 //---------------------------------------------------------------------------
22 // Calculate the mean and sample standard deviation
23 //---------------------------------------------------------------------------
24 std::pair<double, double> G4SimTimer::Report::meanAndSigma()
25 {
26 double mean = nEvent > 0 ? eventTime / nEvent : -1.;
27 double sigma = -1.;
28 if(nEvent > 1)
29 sigma = sqrt( (eventTimeSquared/nEvent - mean*mean) / (nEvent-1) );
30 return std::make_pair(mean, sigma);
31 }
32
33 //---------------------------------------------------------------------------
34 // G4SimTimer constructor
35 //---------------------------------------------------------------------------
37 : AthMessaging("G4SimTimer"),
38 m_firstEvent(true)
39 {}
40
41 //---------------------------------------------------------------------------
42 // Begin-run action
43 //---------------------------------------------------------------------------
45 {
46 if(G4Threading::IsMasterThread()){
47 m_results.runtime.Start();
48 }
49 }
50
51 //---------------------------------------------------------------------------
52 // End-run action
53 //---------------------------------------------------------------------------
54 void G4SimTimer::EndOfRunAction(const G4Run*)
55 {
56 if(G4Threading::IsMasterThread()){
57 m_results.runtime.Stop();
58 }
59 }
60
61 //---------------------------------------------------------------------------
62 // Begin-event action
63 //---------------------------------------------------------------------------
64 void G4SimTimer::BeginOfEventAction(const G4Event* /*event*/)
65 {
66 //ATH_MSG_INFO("beginOfEvent");
67 m_eventTimer.Start();
68 }
69
70 //---------------------------------------------------------------------------
71 // End-event action
72 //---------------------------------------------------------------------------
73 void G4SimTimer::EndOfEventAction(const G4Event* /*event*/)
74 {
75 m_eventTimer.Stop();
76 // We define time as user+system time.
77 auto eventTime = m_eventTimer.GetUserElapsed() + m_eventTimer.GetSystemElapsed();
78 // Skip the first event
79 if(!m_firstEvent) {
80 m_results.nEvent++;
81 m_results.eventTime += eventTime;
82 m_results.eventTimeSquared += eventTime*eventTime;
83 auto meanSigma = m_results.meanAndSigma();
84 ATH_MSG_INFO("Event " << m_results.nEvent << " took " <<
85 std::setprecision(4) << eventTime << " s. New average " <<
86 std::setprecision(4) << meanSigma.first << " +- " <<
87 std::setprecision(4) << meanSigma.second);
88 }
89 else m_firstEvent = false;
90 }
91
92} // namespace G4UA
#define ATH_MSG_INFO(x)
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
virtual void EndOfRunAction(const G4Run *run) override final
End timing this Geant4 run.
G4Timer m_eventTimer
My private instance of an event timer.
Definition G4SimTimer.h:99
virtual void EndOfEventAction(const G4Event *event) override final
Finish timing this Geant4 event.
virtual void BeginOfRunAction(const G4Run *run) override final
Start timing this Geant4 run.
Report m_results
My timing results.
Definition G4SimTimer.h:102
G4SimTimer()
Constructor.
virtual void BeginOfEventAction(const G4Event *event) override final
Start timing this Geant4 event.
bool m_firstEvent
Used to skip the first event.
Definition G4SimTimer.h:105
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="")
std::pair< double, double > meanAndSigma()
Calculate the mean and sample std dev.
double eventTime
Accumulated event time.
Definition G4SimTimer.h:59
Report()
Initializes the variables.
unsigned int nEvent
Number of timed G4 events (we skip the first).
Definition G4SimTimer.h:57
double eventTimeSquared
Accumulated squared event time.
Definition G4SimTimer.h:61