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