Belle II Software development
tracking_efficiency_helpers.py
1#!/usr/bin/env python3
2
3
10
11
17
18from simulation import add_simulation
19from reconstruction import add_reconstruction
20import basf2 as b2
21import glob
22import os
23import sys
24
25
26def get_generated_pdg_code():
27 return 13
28
29
30def get_simulation_components():
31 return None
32
33
34def get_reconstruction_components():
35 # Could be different from get_simulation_components, if only testing subdetectors.
36 return get_simulation_components()
37
38
39def get_generated_pt_value(index):
40 """
41 List with generated pt values is created in this function. With the
42 parameter index, one is able to access single pt values. If index == -1,
43 the number of pt values in the list is returned.
44
45 @param index
46
47 @return double
48 """
49
50 # pt_values = [0.25, 0.5, 0.75, 1.0, 1.5, 2.0, 2.5, 3.0]
51 pt_values = [
52 0.05,
53 0.1,
54 0.25,
55 0.4,
56 0.6,
57 1.,
58 1.5,
59 2.,
60 3.,
61 4.,
62 ]
63
64 if index == -1:
65 return len(pt_values)
66 try:
67 return pt_values[index]
68 except IndexError:
69 print('ERROR in %s. Index is out of range. Only %d elements in list.'
70 % (get_generated_pt_value.__name__, len(pt_values)))
71 sys.exit(1)
72
73
74def get_generated_pt_values():
75 """
76 Collects all pt values with help of the function get_generated_pt_value
77 and stores them in a list, that is returned
78
79 @return list of pt values
80 """
81
82 list = []
83 for index in range(get_generated_pt_value(-1)):
84 list.append(get_generated_pt_value(index))
85
86 return list
87
88
89def additional_options(path):
90 """
91 This sets the specifics of simulation and reconstruction in order
92 to use consistent settings across the various involved modules.
93 """
94
95 realisticCDCGeo = 1
96 useInWirePropagation = 1
97 useTime = 1
98 useWireSag = 1
99 for m in path.modules():
100 if realisticCDCGeo:
101 if m.type() == 'CDCDigitizer':
102 m.param('AddInWirePropagationDelay', useInWirePropagation)
103 m.param('AddTimeOfFlight', useTime)
104 m.param('CorrectForWireSag', useWireSag)
105 else:
106 if m.type() == 'CDCDigitizer':
107 m.param('AddInWirePropagationDelay', 0)
108 m.param('AddTimeOfFlight', 0)
109 m.param('CorrectForWireSag', 0)
110
111 if m.type() == 'DAFRecoFitter':
112 m.param('pdgCodesToUseForFitting', [get_generated_pdg_code()])
113
114 if m.type() == "TrackCreator":
115 m.param('pdgCodes', [get_generated_pdg_code(), 211])
116
117
118def run_simulation(path, pt_value, output_filename=''):
119 """Add needed modules to the path and set parameters and start it"""
120
121 # evt meta
122 eventinfosetter = b2.register_module('EventInfoSetter')
123
124 # generate one event
125 eventinfosetter.param('expList', [0])
126 eventinfosetter.param('runList', [1])
127 eventinfosetter.param('evtNumList', [200])
128
129 path.add_module(eventinfosetter)
130
131 progress = b2.register_module('Progress')
132 path.add_module(progress)
133
134 # ParticleGun
135 pgun = b2.register_module('ParticleGun')
136 # choose the particles you want to simulate
137 param_pgun = {
138 'pdgCodes': [get_generated_pdg_code(), -get_generated_pdg_code()],
139 'nTracks': 10,
140 'varyNTracks': 0,
141 'momentumGeneration': 'uniformPt',
142 'momentumParams': [pt_value, pt_value],
143 'vertexGeneration': 'fixed',
144 'xVertexParams': [0.0],
145 'yVertexParams': [0.0],
146 'zVertexParams': [0.0],
147 'thetaGeneration': 'uniformCos',
148 }
149
150 pgun.param(param_pgun)
151
152 path.add_module(pgun)
153
154 background_files = []
155 if 'BELLE2_BACKGROUND_DIR' in os.environ:
156 background_files += glob.glob(os.environ['BELLE2_BACKGROUND_DIR'] + '/*.root')
157
158 print(f'Number of used background files ({len(background_files)}): ')
159
160 if len(background_files) == 0:
161 background_files = None
162
163 # add simulation modules to the path
164 add_simulation(path, get_simulation_components(), background_files)
165
166 additional_options(path)
167 # write output root file
168 if output_filename != '':
169 root_output = b2.register_module('RootOutput')
170 root_output.param('outputFileName', output_filename)
171 path.add_module(root_output)
172
173
174def run_reconstruction(path, output_file_name, input_file_name=''):
175 if input_file_name != '':
176 root_input = b2.register_module('RootInput')
177 root_input.param('inputFileNames', input_file_name)
178 path.add_module(root_input)
179
180 gearbox = b2.register_module('Gearbox')
181 path.add_module(gearbox)
182
183 geometry = b2.register_module('Geometry')
184 geometry.param("useDB", True)
185 path.add_module(geometry)
186
187 add_reconstruction(path, get_reconstruction_components(), pruneTracks=0)
188 # add_mc_reconstruction(path, get_reconstruction_components(), pruneTracks=0)
189
190 tracking_efficiency = b2.register_module('StandardTrackingPerformance')
191 # tracking_efficiency.logging.log_level = LogLevel.DEBUG
192 tracking_efficiency.param('outputFileName', output_file_name)
193 path.add_module(tracking_efficiency)
194
195 additional_options(path)