Belle II Software development
SimulationTiming.py
1#!/usr/bin/env python3
2
3
10
11"""
12This script determines the time spent in each part of the simulation. It does
13that by assigning regions to the geometry created by each creator and then
14measuring the time spent in these regions using the G4VSteppingVerbose
15facilities provided by Geant4. These get compiled out in the opt version of the
16externals so this only works with the debug externals. It was intended as a
17validation script but due to this problem it was moved to examples
18
19Generate detailed timing information of the simulation when simulating
20100 EvtGen events.
21WARNING: Does not work in parallel processing mode
22"""
23
24import os
25import basf2 as b2
26import ROOT
27
28if os.environ.get("BELLE2_EXTERNALS_OPTION", "opt") != "debug":
29 b2.B2FATAL("This script needs to be run with debug externals, otherwise it "
30 "cannot determine the time spent in each volume.")
31
32# limit output
33b2.logging.log_level = b2.LogLevel.WARNING
34# disable multi processing
35b2.set_nprocesses(0)
36
37main = b2.create_path()
38# create 100 events
39main.add_module("EventInfoSetter", evtNumList=[100])
40# using standard evtgen
41main.add_module("EvtGenInput")
42# and parameters
43main.add_module("Gearbox")
44# and the geometry with assigned regions for each creator
45main.add_module("Geometry", assignRegions=True)
46# as well as the simulation
47main.add_module("FullSim")
48# including the timing module
49main.add_module("FullSimTiming", rootFile="EvtGenTiming.root", logLevel=b2.LogLevel.INFO)
50
51# and run it
52b2.process(main)
53
54
55def add_info(obj, title, text):
56 """Add a description item to a TH object"""
57 obj.GetListOfFunctions().Add(ROOT.TNamed(title, text))
58
59
60# now open the created root file and update some things
61root_file = ROOT.TFile("EvtGenTiming.root", "UPDATE")
62timing = root_file.Get("SimulationTiming")
63# like set the minimum to zero
64timing.SetMinimum(0)
65# lets update the "DefaultRegionOfTheWorld" name to something more useful
66for i in range(timing.GetNbinsX()):
67 if timing.GetXaxis().GetBinLabel(i + 1) == "DefaultRegionForTheWorld":
68 timing.GetXaxis().SetBinLabel(i + 1, "Top Volume")
69
70# add the required descriptions
71add_info(timing, "Description",
72 "Time spent in each sub detector when simulating EvtGen events "
73 "(default settings). This includes tracking through the volume, "
74 "calculating of all physics processes and calling the sensitive "
75 "detector implementations (if any). It does not include the "
76 "overall Geant4/framework overhead (event action, generation, "
77 "stacking action, tracking action, stepping action and so forth) "
78 "<br>The errors are standard deviations of the stepping time per "
79 "event/region")
80add_info(timing, "Check", "Dunno, mostly for informational purpose but "
81 "large deviations might point to problematic geometry/sensitive "
82 "detector updates")
83add_info(timing, "Contact", "ritter")
84
85timing.Write()
86root_file.Close()