9#include <simulation/modules/fullsim/FullSimTimingModule.h>
10#include <simulation/kernel/RunManager.h>
11#include <framework/gearbox/Unit.h>
12#include <G4EventManager.hh>
13#include <G4VSteppingVerbose.hh>
14#include <G4RegionStore.hh>
15#include <framework/logging/Logger.h>
34 setDescription(
"Provide more detailed timing info for FullSim module");
38 G4VSteppingVerbose* old = G4VSteppingVerbose::GetInstance();
41 G4VSteppingVerbose::SetInstance(
nullptr);
47 G4VSteppingVerbose::SetInstance(
m_timing);
51 " is given, no file will be created", std::string(
""));
71 if (runManager.GetUserPrimaryGeneratorAction()) {
82 G4SteppingManager* stepping = G4EventManager::GetEventManager()->GetTrackingManager()->GetSteppingManager();
86 stepping->SetVerboseLevel(1);
95 for (G4Region* region : * (G4RegionStore::GetInstance())) {
97 if (region->GetNumberOfRootVolumes() == 0)
continue;
103 "SimulationTiming",
"Simulation Timing;region;stepping time per event / ms",
128 const G4Region*
const region = volume->GetRegion();
145 B2RESULT(
"FullSimTiming: Total simulation stepping time: " << (
m_totalTime /
Unit::s) <<
" s");
146 B2RESULT(
"FullSimTiming: Number of simulated events: " <<
m_eventCount);
152 size_t regionWidth {6};
154 m_timingProfile->GetXaxis()->SetBinLabel(it.second + 1, it.first->GetName().c_str());
155 regionWidth = std::max(regionWidth, it.first->GetName().size());
167 size_t maxContentWidth {4};
168 size_t maxErrorWidth {4};
177 const size_t contentWidth = std::floor(std::log10(content > 0 ? content : 1) + 1);
178 const size_t errorWidth = std::floor(std::log10(error > 0 ? error : 1) + 1);
180 maxContentWidth = std::max(maxContentWidth, contentWidth + 3);
181 maxErrorWidth = std::max(maxErrorWidth, errorWidth + 3);
186 B2RESULT(
"FullSimTiming: Simulation stepping time per event and region");
189 << std::setw(regionWidth + 1) << std::left <<
m_timingProfile->GetXaxis()->GetBinLabel(i + 1) <<
" "
190 << std::right << std::fixed << std::setprecision(2)
191 << std::setw(maxContentWidth) <<
m_timingProfile->GetBinContent(i + 1) <<
" ms +- "
192 << std::setw(maxErrorWidth) <<
m_timingProfile->GetBinError(i + 1) <<
" ms ("
193 << std::setw(6) << (
m_timingProfile->GetBinContent(i + 1) / totalAvg) <<
" %)"
std::string m_rootFileName
filename of the root file to store the timing profile
double m_totalTime
total time spent stepping through the simulation geometry
bool m_isInitialized
remember whether we aleady initialized everything
std::map< constG4Region *, double >::iterator m_lastRegion
iterator pointing to the region of the last step to speed up lookup
Simulation::StepTiming * m_timing
instance of the StepTiming class which records the step times in geant4
virtual void initialize() override
Setup the timing structure.
virtual void event() override
Save the simulation time for each region in a profile.
FullSimTimingModule()
Constructor: Sets the description, the properties and the parameters of the module.
virtual void terminate() override
Save the output.
double m_eventTime
stepping time of the current event
void processStep(G4Track *track, G4LogicalVolume *volume, double time)
store the time it took a Geant4 track for one step inside a volume
unsigned int m_eventCount
number of events with m_eventTime>0
std::map< const G4Region *, int > m_regionIndices
map between region and and bin indices in the TProfile
std::map< const G4Region *, double > m_regionCache
map containing the amount of time spent in each region
TProfile * m_timingProfile
profile of the time spent per event in each region
void setDescription(const std::string &description)
Sets the description of the module.
The run manager controls the flow of the Geant4 program and manages the event loop(s) within a run.
static RunManager & Instance()
Static method to get a reference to the RunManager instance.
Class to perform more detailed timing studies of the Geant4 simulation.
static const double ms
[millisecond]
static const double s
[second]
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.