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