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