Belle II Software  release-05-01-25
__init__.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 from basf2 import *
5 from tracking.path_utils import *
6 
7 
8 def add_tracking_reconstruction(path, components=None, pruneTracks=False, skipGeometryAdding=False,
9  mcTrackFinding=False, trackFitHypotheses=None,
10  reco_tracks="RecoTracks", prune_temporary_tracks=True, fit_tracks=True,
11  use_second_cdc_hits=False, skipHitPreparerAdding=False,
12  use_svd_to_cdc_ckf=True, use_ecl_to_cdc_ckf=False,
13  add_cdcTrack_QI=True, add_vxdTrack_QI=False, add_recoTrack_QI=False):
14  """
15  This function adds the standard reconstruction modules for tracking
16  to a path.
17 
18  :param path: The path to add the tracking reconstruction modules to
19  :param components: the list of geometry components in use or None for all components.
20  :param pruneTracks: Delete all hits except the first and the last in the found tracks.
21  :param skipGeometryAdding: Advances flag: The tracking modules need the geometry module and will add it,
22  if it is not already present in the path. In a setup with multiple (conditional) paths however, it can not
23  determine, if the geometry is already loaded. This flag can be used o just turn off the geometry adding at
24  all (but you will have to add it on your own then).
25  :param skipHitPreparerAdding: Advanced flag: do not add the hit preparation (esp. VXD cluster creation
26  modules. This is useful if they have been added before already.
27  :param mcTrackFinding: Use the MC track finders instead of the realistic ones.
28  :param reco_tracks: Name of the StoreArray where the reco tracks should be stored
29  :param prune_temporary_tracks: If false, store all information of the single CDC and VXD tracks before merging.
30  If true, prune them.
31  :param fit_tracks: If false, the final track find and the TrackCreator module will no be executed
32  :param use_second_cdc_hits: If true, the second hit information will be used in the CDC track finding.
33  :param trackFitHypotheses: Which pdg hypothesis to fit. Defaults to [211, 321, 2212].
34  :param use_svd_to_cdc_ckf: if true, add SVD to CDC CKF module.
35  :param use_ecl_to_cdc_ckf: if true, add ECL to CDC CKF module.
36  :param add_cdcTrack_QI: If true, add the MVA track quality estimation
37  to the path that sets the quality indicator property of the found CDC standalone tracks
38  :param add_vxdTrack_QI: If true, add the MVA track quality estimation
39  to the path that sets the quality indicator property of the found VXDTF2 tracks
40  (ATTENTION: Standard triplet QI of VXDTF2 is replaced in this case
41  -> setting this option to 'True' will have some influence on the final track collection)
42  :param add_recoTrack_QI: If true, add the MVA track quality estimation
43  to the path that sets the quality indicator property of all found reco tracks
44  (Both other QIs needed as input.)
45  """
46 
47  if not is_svd_used(components) and not is_cdc_used(components):
48  return
49 
50  if (add_cdcTrack_QI or add_vxdTrack_QI or add_recoTrack_QI) and not fit_tracks:
51  B2ERROR("MVA track qualiy indicator requires `fit_tracks` to be enabled. Turning all off.")
52  add_cdcTrack_QI = False
53  add_vxdTrack_QI = False
54  add_recoTrack_QI = False
55 
56  if add_recoTrack_QI and (not add_cdcTrack_QI or not add_vxdTrack_QI):
57  B2ERROR("RecoTrack qualiy indicator requires CDC and VXD QI as input. Turning it all of.")
58  add_cdcTrack_QI = False
59  add_vxdTrack_QI = False
60  add_recoTrack_QI = False
61 
62  if not skipGeometryAdding:
63  add_geometry_modules(path, components=components)
64 
65  if not skipHitPreparerAdding:
66  add_hit_preparation_modules(path, components=components)
67 
68  # Material effects for all track extrapolations
69  if 'SetupGenfitExtrapolation' not in path:
70  path.add_module('SetupGenfitExtrapolation',
71  energyLossBrems=False, noiseBrems=False)
72 
73  if mcTrackFinding:
74  add_mc_track_finding(path, components=components, reco_tracks=reco_tracks,
75  use_second_cdc_hits=use_second_cdc_hits)
76  else:
77  add_track_finding(path, components=components, reco_tracks=reco_tracks,
78  prune_temporary_tracks=prune_temporary_tracks,
79  use_second_cdc_hits=use_second_cdc_hits,
80  use_svd_to_cdc_ckf=use_svd_to_cdc_ckf,
81  use_ecl_to_cdc_ckf=use_ecl_to_cdc_ckf,
82  add_cdcTrack_QI=add_cdcTrack_QI, add_vxdTrack_QI=add_vxdTrack_QI)
83 
84  # Only run the track time extraction on the full reconstruction chain for now. Later, we may
85  # consider to do the CDC-hit based method already during the fast reconstruction stage
86  add_time_extraction(path, components=components)
87 
88  add_mc_matcher(path, components=components, reco_tracks=reco_tracks,
89  use_second_cdc_hits=use_second_cdc_hits)
90 
91  if fit_tracks:
92  add_track_fit_and_track_creator(path, components=components, pruneTracks=pruneTracks,
93  trackFitHypotheses=trackFitHypotheses,
94  reco_tracks=reco_tracks,
95  add_mva_quality_indicator=add_recoTrack_QI)
96 
97  if prune_temporary_tracks or pruneTracks:
98  path.add_module("PruneRecoHits")
99 
100 
101 def add_time_extraction(path, components=None):
102  """
103  Add time extraction components via tracking
104  """
105 
106  if is_cdc_used(components):
107  path.add_module("FullGridChi2TrackTimeExtractor")
108 
109 
110 def add_cr_tracking_reconstruction(path, components=None, prune_tracks=False,
111  skip_geometry_adding=False, event_time_extraction=True,
112  data_taking_period="early_phase3", top_in_counter=False,
113  merge_tracks=True, use_second_cdc_hits=False):
114  """
115  This function adds the reconstruction modules for cr tracking to a path.
116 
117  :param path: The path to which to add the tracking reconstruction modules
118  :param data_taking_period: The cosmics generation will be added using the
119  parameters, that where used in this period of data taking. The periods can be found in cdc/cr/__init__.py.
120 
121  :param components: the list of geometry components in use or None for all components.
122  :param prune_tracks: Delete all hits except the first and the last in the found tracks.
123 
124  :param skip_geometry_adding: Advanced flag: The tracking modules need the geometry module and will add it,
125  if it is not already present in the path. In a setup with multiple (conditional) paths however, it cannot
126  determine if the geometry is already loaded. This flag can be used to just turn off the geometry adding
127  (but you will have to add it on your own).
128  :param event_time_extraction: extract the event time
129  :param merge_tracks: The upper and lower half of the tracks should be merged together in one track
130  :param use_second_cdc_hits: If true, the second hit information will be used in the CDC track finding.
131 
132  :param top_in_counter: time of propagation from the hit point to the PMT in the trigger counter is subtracted
133  (assuming PMT is put at -z of the counter).
134  """
135  # make sure CDC is used
136  if not is_cdc_used(components):
137  return
138 
139  if not skip_geometry_adding:
140  add_geometry_modules(path, components)
141 
142  add_hit_preparation_modules(path, components=components)
143 
144  # Material effects for all track extrapolations
145  if 'SetupGenfitExtrapolation' not in path:
146  path.add_module('SetupGenfitExtrapolation',
147  energyLossBrems=False, noiseBrems=False)
148 
149  # track finding
150  add_cr_track_finding(path, reco_tracks="RecoTracks", components=components, data_taking_period=data_taking_period,
151  merge_tracks=merge_tracks, use_second_cdc_hits=use_second_cdc_hits)
152 
153  # track fitting
154  # if tracks were merged, use the unmerged collection for time extraction
155  add_cr_track_fit_and_track_creator(path, components=components, prune_tracks=prune_tracks,
156  event_timing_extraction=event_time_extraction,
157  data_taking_period=data_taking_period,
158  top_in_counter=top_in_counter)
159 
160  if merge_tracks:
161  # Do also fit the not merged tracks
162  add_cr_track_fit_and_track_creator(path, components=components, prune_tracks=prune_tracks,
163  event_timing_extraction=False,
164  data_taking_period=data_taking_period, top_in_counter=top_in_counter,
165  reco_tracks="NonMergedRecoTracks", tracks="NonMergedTracks")
166 
167 
168 def add_mc_tracking_reconstruction(path, components=None, pruneTracks=False, use_second_cdc_hits=False):
169  """
170  This function adds the standard reconstruction modules for MC tracking
171  to a path.
172 
173  :param path: The path to add the tracking reconstruction modules to
174  :param components: the list of geometry components in use or None for all components.
175  :param pruneTracks: Delete all hits expect the first and the last from the found tracks.
176  :param use_second_cdc_hits: If true, the second hit information will be used in the CDC track finding.
177  """
178  add_tracking_reconstruction(path,
179  components=components,
180  pruneTracks=pruneTracks,
181  mcTrackFinding=True,
182  use_second_cdc_hits=use_second_cdc_hits)
183 
184 
185 def add_track_finding(path, components=None, reco_tracks="RecoTracks",
186  prune_temporary_tracks=True, use_second_cdc_hits=False,
187  use_mc_truth=False, svd_ckf_mode="VXDTF2_after", add_both_directions=True,
188  use_svd_to_cdc_ckf=True, use_ecl_to_cdc_ckf=False,
189  add_cdcTrack_QI=True, add_vxdTrack_QI=False):
190  """
191  Add the CKF to the path with all the track finding related to and needed for it.
192  :param path: The path to add the tracking reconstruction modules to
193  :param reco_tracks: The store array name where to output all tracks
194  :param use_mc_truth: Use the truth information in the CKF modules
195  :param svd_ckf_mode: how to apply the CKF (with VXDTF2 or without). Defaults to "VXDTF2_after".
196  :param add_both_directions: Curlers may be found in the wrong orientation by the CDC track finder, so try to
197  extrapolate also in the other direction.
198  :param use_second_cdc_hits: whether to use the secondary CDC hit during CDC track finding or not
199  :param components: the list of geometry components in use or None for all components.
200  :param prune_temporary_tracks: If false, store all information of the single CDC and VXD tracks before merging.
201  If true, prune them.
202  :param use_svd_to_cdc_ckf: if true, add SVD to CDC CKF module.
203  :param use_ecl_to_cdc_ckf: if true, add ECL to CDC CKF module.
204  :param add_cdcTrack_QI: If true, add the MVA track quality estimation
205  to the path that sets the quality indicator property of the found CDC standalone tracks
206  :param add_vxdTrack_QI: If true, add the MVA track quality estimation
207  to the path that sets the quality indicator property of the found VXDTF2 tracks
208  (ATTENTION: Standard triplet QI of VXDTF2 is replaced in this case
209  -> setting this option to 'True' will have some influence on the final track collection)
210  """
211  if not is_svd_used(components) and not is_cdc_used(components):
212  return
213 
214  if use_ecl_to_cdc_ckf and not is_cdc_used(components):
215  B2WARNING("ECL CKF cannot be used without CDC. Turning it off.")
216  use_ecl_to_cdc_ckf = False
217 
218  if use_ecl_to_cdc_ckf and not is_ecl_used(components):
219  B2ERROR("ECL CKF cannot be used without ECL. Turning it off.")
220  use_ecl_to_cdc_ckf = False
221 
222  # register EventTrackingInfo
223  if 'RegisterEventLevelTrackingInfo' not in path:
224  path.add_module('RegisterEventLevelTrackingInfo')
225 
226  # output tracks
227  cdc_reco_tracks = "CDCRecoTracks"
228  svd_cdc_reco_tracks = "SVDCDCRecoTracks"
229  ecl_reco_tracks = "ECLRecoTracks"
230  combined_ecl_reco_tracks = "combinedECLRecoTracks"
231 
232  # temporary collections
233  svd_reco_tracks = "SVDRecoTracks"
234  pxd_reco_tracks = "PXDRecoTracks"
235 
236  # collections that will be pruned
237  temporary_reco_track_list = []
238 
239  # the name of the most recent track collection
240  latest_reco_tracks = None
241 
242  if not is_pxd_used(components):
243  if use_ecl_to_cdc_ckf and is_cdc_used(components):
244  combined_ecl_reco_tracks = reco_tracks
245  elif (not use_ecl_to_cdc_ckf) and is_svd_used(components):
246  svd_cdc_reco_tracks = reco_tracks
247  elif (not use_ecl_to_cdc_ckf) and (not is_svd_used(components)) and is_cdc_used(components):
248  cdc_reco_tracks = reco_tracks
249 
250  if is_cdc_used(components):
251  add_cdc_track_finding(path, use_second_hits=use_second_cdc_hits, output_reco_tracks=cdc_reco_tracks,
252  add_mva_quality_indicator=add_cdcTrack_QI)
253  temporary_reco_track_list.append(cdc_reco_tracks)
254  latest_reco_tracks = cdc_reco_tracks
255 
256  if is_svd_used(components):
257  add_svd_track_finding(path, components=components, input_reco_tracks=latest_reco_tracks,
258  output_reco_tracks=svd_cdc_reco_tracks, use_mc_truth=use_mc_truth,
259  temporary_reco_tracks=svd_reco_tracks,
260  svd_ckf_mode=svd_ckf_mode, add_both_directions=add_both_directions,
261  use_svd_to_cdc_ckf=use_svd_to_cdc_ckf, prune_temporary_tracks=prune_temporary_tracks,
262  add_mva_quality_indicator=add_vxdTrack_QI)
263  temporary_reco_track_list.append(svd_reco_tracks)
264  temporary_reco_track_list.append(svd_cdc_reco_tracks)
265  latest_reco_tracks = svd_cdc_reco_tracks
266 
267  if use_ecl_to_cdc_ckf and is_cdc_used(components):
268  add_eclcdc_track_finding(path, components=components, output_reco_tracks=ecl_reco_tracks,
269  prune_temporary_tracks=prune_temporary_tracks)
270 
271  # TODO: add another merging step? (SVD track found by vxdtf2, and CDC track found by ECL CKF)?
272 
273  path.add_module("RecoTrackStoreArrayCombiner",
274  Temp1RecoTracksStoreArrayName=latest_reco_tracks,
275  Temp2RecoTracksStoreArrayName=ecl_reco_tracks,
276  recoTracksStoreArrayName=combined_ecl_reco_tracks)
277  temporary_reco_track_list.append(ecl_reco_tracks)
278  temporary_reco_track_list.append(combined_ecl_reco_tracks)
279  latest_reco_tracks = combined_ecl_reco_tracks
280 
281  if is_pxd_used(components):
282  add_pxd_track_finding(path, components=components, input_reco_tracks=latest_reco_tracks,
283  use_mc_truth=use_mc_truth, output_reco_tracks=reco_tracks,
284  temporary_reco_tracks=pxd_reco_tracks,
285  add_both_directions=add_both_directions)
286  temporary_reco_track_list.append(pxd_reco_tracks)
287 
288  if prune_temporary_tracks:
289  for temporary_reco_track_name in temporary_reco_track_list:
290  if temporary_reco_track_name != reco_tracks:
291  path.add_module('PruneRecoTracks', storeArrayName=temporary_reco_track_name)
292 
293 
294 def add_cr_track_finding(path, reco_tracks="RecoTracks", components=None, data_taking_period='early_phase3',
295  merge_tracks=True, use_second_cdc_hits=False):
296  import cdc.cr as cosmics_setup
297 
298  # register EventTrackingInfo
299  if 'RegisterEventLevelTrackingInfo' not in path:
300  path.add_module('RegisterEventLevelTrackingInfo')
301 
302  if data_taking_period not in ["phase2", "early_phase3", "phase3"]:
303  cosmics_setup.set_cdc_cr_parameters(data_taking_period)
304 
305  # track finding
306  add_cdc_cr_track_finding(path, merge_tracks=merge_tracks, use_second_cdc_hits=use_second_cdc_hits,
307  trigger_point=tuple(cosmics_setup.triggerPos))
308 
309  else:
310  if not is_cdc_used(components):
311  B2FATAL("CDC must be in components")
312 
313  reco_tracks_from_track_finding = reco_tracks
314  if merge_tracks:
315  reco_tracks_from_track_finding = "NonMergedRecoTracks"
316 
317  cdc_reco_tracks = "CDCRecoTracks"
318  if not is_pxd_used(components) and not is_svd_used(components):
319  cdc_reco_tracks = reco_tracks_from_track_finding
320 
321  svd_cdc_reco_tracks = "SVDCDCRecoTracks"
322  if not is_pxd_used(components):
323  svd_cdc_reco_tracks = reco_tracks_from_track_finding
324 
325  full_reco_tracks = reco_tracks_from_track_finding
326 
327  # CDC track finding with default settings
328  add_cdc_cr_track_finding(path, merge_tracks=False, use_second_cdc_hits=use_second_cdc_hits,
329  output_reco_tracks=cdc_reco_tracks)
330 
331  latest_reco_tracks = cdc_reco_tracks
332 
333  if is_svd_used(components):
334  add_svd_track_finding(path, components=components, input_reco_tracks=latest_reco_tracks,
335  output_reco_tracks=svd_cdc_reco_tracks,
336  svd_ckf_mode="cosmics", add_both_directions=True)
337  latest_reco_tracks = svd_cdc_reco_tracks
338 
339  if is_pxd_used(components):
340  add_pxd_cr_track_finding(path, components=components, input_reco_tracks=latest_reco_tracks,
341  output_reco_tracks=full_reco_tracks, add_both_directions=True,
342  filter_cut=0.01)
343 
344  if merge_tracks:
345  # merge the tracks together
346  path.add_module("CosmicsTrackMerger", inputRecoTracks=reco_tracks_from_track_finding,
347  outputRecoTracks=reco_tracks)
348 
349 
350 def add_mc_track_finding(path, components=None, reco_tracks="RecoTracks", use_second_cdc_hits=False):
351  """
352  Add the MC based TrackFinder to the path.
353 
354  :param path: The path to add the tracking reconstruction modules to
355  :param components: the list of geometry components in use or None for all components.
356  :param reco_tracks: Name of the StoreArray where the reco tracks should be stored
357  :param use_second_cdc_hits: If true, the second hit information will be used in the CDC track finding.
358  """
359  if is_cdc_used(components) or is_pxd_used(components) or is_svd_used(components):
360  # find MCTracks in CDC, SVD and PXD (or a subset of it)
361  path.add_module('TrackFinderMCTruthRecoTracks',
362  RecoTracksStoreArrayName=reco_tracks,
363  UseSecondCDCHits=use_second_cdc_hits,
364  UsePXDHits=is_pxd_used(components),
365  UseSVDHits=is_svd_used(components),
366  UseCDCHits=is_cdc_used(components))
367 
368 
369 def add_tracking_for_PXDDataReduction_simulation(path, components, svd_cluster='__ROIsvdClusters'):
370  """
371  This function adds the standard reconstruction modules for tracking to be used for the simulation of PXD data
372  reduction to a path.
373 
374  :param path: The path to add the tracking reconstruction modules to
375  :param components: the list of geometry components in use or None for all components, always exclude the PXD.
376  """
377 
378  if not is_svd_used(components):
379  return
380 
381  # Material effects
382  if 'SetupGenfitExtrapolation' not in path:
383  material_effects = register_module('SetupGenfitExtrapolation')
384  material_effects.set_name(
385  'SetupGenfitExtrapolationForPXDDataReduction')
386  path.add_module(material_effects)
387 
388  # SET StoreArray names
389  svd_reco_tracks = "__ROIsvdRecoTracks"
390 
391  # SVD ONLY TRACK FINDING
392  add_vxd_track_finding_vxdtf2(path, components=['SVD'], reco_tracks=svd_reco_tracks, suffix="__ROI",
393  svd_clusters=svd_cluster)
394 
395  # TRACK FITTING
396  dafRecoFitter = register_module("DAFRecoFitter")
397  dafRecoFitter.set_name("SVD-only DAFRecoFitter")
398  dafRecoFitter.param('recoTracksStoreArrayName', svd_reco_tracks)
399  dafRecoFitter.param('svdHitsStoreArrayName', svd_cluster)
400  path.add_module(dafRecoFitter)
401 
402 
403 def add_vxd_standalone_cosmics_finder(
404  path,
405  reco_tracks="RecoTracks",
406  pxd_spacepoints_name="PXDSpacePoints",
407  svd_spacepoints_name="SVDSpacePoints",
408  quality_cut=0.0001,
409  min_sps=3,
410  max_rejected_sps=5):
411  """
412  Convenience function for adding VXD standalone cosmics track finding for B = 0 Tesla
413  to the path.
414 
415  The result is a StoreArray with name @param reco_tracks containing one or zero reco tracks per event.
416  This track candidates have an arbitrary but large momentum seed in the direction of the fitted line.
417  The position and momentum seed is obtained using a principal component analysis method.
418 
419  :param path: basf2 path
420  :param reco_tracks: Name of the output RecoTracks; defaults to RecoTracks.
421  :param spacepoints_name: name of store array containing the spacepoints; defaults to SpacePoints
422  :param quality_cut: Cut on the chi squared value of the line fit; candidates with values above the cut will be
423  rejected; defaults to 0.0001
424  :param min_sps: Minimal number of SpacePoints required to build a track candidate; defaults to 3;
425  :param max_rejected_sps: Maximal number of retries to refit a track after the worst spacepoint was removed;
426  defaults to 5;
427  """
428 
429  # register EventTrackingInfo
430  if 'RegisterEventLevelTrackingInfo' not in path:
431  path.add_module('RegisterEventLevelTrackingInfo')
432 
433  sp_creator_pxd = register_module('PXDSpacePointCreator')
434  sp_creator_pxd.param('SpacePoints', pxd_spacepoints_name)
435  path.add_module(sp_creator_pxd)
436 
437  # SVDSpacePointCreator is applied in funtion add_svd_reconstruction
438 
439  track_finder = register_module('TrackFinderVXDCosmicsStandalone')
440  track_finder.param('SpacePointTrackCandArrayName', "")
441  track_finder.param('SpacePoints', [pxd_spacepoints_name, svd_spacepoints_name])
442  track_finder.param('QualityCut', quality_cut)
443  track_finder.param('MinSPs', min_sps)
444  track_finder.param('MaxRejectedSPs', max_rejected_sps)
445  path.add_module(track_finder)
446 
447  converter = register_module('SPTC2RTConverter')
448  converter.param('recoTracksStoreArrayName', reco_tracks)
449  path.add_module(converter)
tracking.path_utils
Definition: path_utils.py:1
cr