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
8// Infrastructure includes
9
10namespace G4UA
11{
12
13 //---------------------------------------------------------------------------
14 // Constructor for timing results struct
15 //---------------------------------------------------------------------------
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
#define ATH_MSG_INFO(x)
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
G4Timer m_eventTimer
My private instance of an event timer.
Definition G4SimTimer.h:83
virtual void EndOfEventAction(const G4Event *event) override final
Finish timing this Geant4 event.
Report m_results
My timing results.
Definition G4SimTimer.h:86
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:89
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:54
Report()
Initializes the variables.
unsigned int nEvent
Number of timed G4 events (we skip the first).
Definition G4SimTimer.h:52
double eventTimeSquared
Accumulated squared event time.
Definition G4SimTimer.h:56