Belle II Software  release-08-01-10
reconstruction.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 # Limit the number of threads spawned by external libraries (e.g. XGBoost)
13 import os
14 os.environ['OMP_THREAD_LIMIT'] = "1" # noqa
15 
16 import basf2
17 
18 from geometry import check_components
19 
20 from svd import add_svd_reconstruction
21 from pxd import add_pxd_reconstruction
22 
23 from rawdata import add_unpackers
24 
25 from softwaretrigger.constants import ALWAYS_SAVE_OBJECTS, RAWDATA_OBJECTS, DEFAULT_HLT_COMPONENTS
26 
27 from tracking import (
28  add_mc_tracking_reconstruction,
29  add_prefilter_tracking_reconstruction,
30  add_postfilter_tracking_reconstruction,
31  add_cr_tracking_reconstruction,
32  add_prune_tracks,
33 )
34 
35 from softwaretrigger.path_utils import (
36  add_filter_software_trigger,
37  add_skim_software_trigger
38 )
39 
40 
41 CDST_TRACKING_OBJECTS = (
42  'EventLevelTrackingInfo',
43  'RecoTracks',
44  'Tracks',
45  'V0s',
46  'TrackFitResults',
47  'EventT0',
48  'CDCDedxTracks',
49  'SVDShaperDigitsFromTracks',
50  'PXDClustersFromTracks',
51  'VXDDedxTracks',
52  'CDCDedxLikelihoods',
53  'VXDDedxLikelihoods'
54 )
55 
56 
57 DIGITS_OBJECTS = (
58  'ARICHDigits',
59  'CDCHits',
60  'ECLDigits',
61  'ECLDsps',
62  'KLMDigits',
63  'PXDDigits',
64  'SVDEventInfoSim',
65  'SVDShaperDigits',
66  'TOPDigits'
67 )
68 
69 
70 def default_event_abort(module, condition, error_flag):
71  """Default event abort outside of HLT: Ignore the error flag and just stop
72  processing by giving an empty path"""
73  p = basf2.Path()
74  module.if_value(condition, p, basf2.AfterConditionPath.END)
75 
76 
77 def add_reconstruction(path, components=None, pruneTracks=True, add_trigger_calculation=True, skipGeometryAdding=False,
78  trackFitHypotheses=None, addClusterExpertModules=True,
79  use_second_cdc_hits=False, add_muid_hits=False, reconstruct_cdst=None,
80  event_abort=default_event_abort, use_random_numbers_for_hlt_prescale=True,
81  pxd_filtering_offline=False, append_full_grid_cdc_eventt0=False,
82  legacy_ecl_charged_pid=False, emulate_HLT=False):
83  """
84  This function adds the standard reconstruction modules to a path.
85  Consists of clustering, tracking and the PID modules essentially in this structure:
86 
87  | :func:`add_reconstruction()`
88  | ├── :func:`add_prefilter_reconstruction()`
89  | │ ├── :func:`add_prefilter_pretracking_reconstruction()` : Clustering
90  | │ ├── ``add_prefilter_tracking_reconstruction()`` : Tracking essential for HLT filter calculation
91  | │ └── :func:`add_prefilter_posttracking_reconstruction()` : PID and clustering essential for HLT
92  | └── :func:`add_postfilter_reconstruction()`
93  | ├── ``add_postfilter_tracking_reconstruction()`` : Rest of the tracking
94  | └── :func:`add_postfilter_posttracking_reconstruction()` : Rest of PID and clustering
95 
96  plus the modules to calculate the software trigger cuts.
97 
98  :param path: Add the modules to this path.
99  :param components: list of geometry components to include reconstruction for, or None for all components.
100  :param pruneTracks: Delete all hits except the first and last of the tracks after the V0Finder modules.
101  :param skipGeometryAdding: Advances flag: The tracking modules need the geometry module and will add it,
102  if it is not already present in the path. In a setup with multiple (conditional) paths however, it can not
103  determine, if the geometry is already loaded. This flag can be used to just turn off the geometry adding at
104  all (but you will have to add it on your own then).
105  :param trackFitHypotheses: Change the additional fitted track fit hypotheses. If no argument is given,
106  the fitted hypotheses are pion, muon and proton, i.e. [211, 321, 2212].
107  :param addClusterExpertModules: Add the cluster expert modules in the KLM and ECL. Turn this off to reduce
108  execution time.
109  :param use_second_cdc_hits: If true, the second hit information will be used in the CDC track finding.
110  :param add_muid_hits: Add the found KLM hits to the RecoTrack. Make sure to refit the track afterwards.
111  :param add_trigger_calculation: add the software trigger modules for monitoring (do not make any cut)
112  :param reconstruct_cdst: None for mdst, 'rawFormat' to reconstruct cdsts in rawFormat, 'fullFormat' for the
113  full (old) format. This parameter is needed when reconstructing cdsts, otherwise the
114  required PXD objects won't be added.
115  :param event_abort: A function to abort event processing at the given point. Should take three arguments: a module,
116  the condition and the error_flag to be set if these events are kept. If run on HLT this will not abort the event
117  but just remove all data except for the event information.
118  :param use_random_numbers_for_hlt_prescale: If True, the HLT filter prescales are applied using randomly
119  generated numbers, otherwise are applied using an internal counter.
120  :param pxd_filtering_offline: If True, PXD data reduction (ROI filtering) is applied during the track reconstruction.
121  The reconstructed SVD/CDC tracks are used to define the ROIs and reject all PXD clusters outside of these.
122  :param append_full_grid_cdc_eventt0: If True, the module FullGridChi2TrackTimeExtractor is added to the path
123  and provides the CDC temporary EventT0.
124  :param legacy_ecl_charged_pid: Bool denoting whether to use the legacy EoP based charged particleID in the ECL (true) or
125  MVA based charged particle ID (false).
126  :param emulate_HLT: if True, it runs the reconstruction as it is run on HLT (e.g. without PXD).
127  If you want to use this flag on raw data, you should also exclude the following branches from RootInput: ROIs, ROIpayload
128  """
129 
130  # By default, the FullGrid module is not used in the reconstruction chain.
131  # It is needed for detectors that perform post-tracking calibration with respect to CDC EventT0 using cDST
132  if reconstruct_cdst == 'rawFormat':
133  append_full_grid_cdc_eventt0 = True
134 
135  if emulate_HLT:
136  components = DEFAULT_HLT_COMPONENTS
137 
138  # pre-filter reconstruction
139  add_prefilter_reconstruction(path,
140  components=components,
141  add_modules_for_trigger_calculation=add_trigger_calculation,
142  skipGeometryAdding=skipGeometryAdding,
143  trackFitHypotheses=trackFitHypotheses,
144  use_second_cdc_hits=use_second_cdc_hits,
145  add_muid_hits=add_muid_hits,
146  reconstruct_cdst=reconstruct_cdst,
147  event_abort=event_abort,
148  pxd_filtering_offline=pxd_filtering_offline,
149  append_full_grid_cdc_eventt0=append_full_grid_cdc_eventt0)
150 
151  # Add the modules calculating the software trigger cuts (but not performing them)
152  if add_trigger_calculation and (not components or ("CDC" in components and "ECL" in components and "KLM" in components)):
153  add_filter_software_trigger(path,
154  use_random_numbers_for_prescale=use_random_numbers_for_hlt_prescale)
155 
156  # post-filter reconstruction
157  add_postfilter_reconstruction(path,
158  components=components,
159  pruneTracks=pruneTracks,
160  addClusterExpertModules=addClusterExpertModules,
161  reconstruct_cdst=reconstruct_cdst,
162  legacy_ecl_charged_pid=legacy_ecl_charged_pid)
163 
164  # Add the modules calculating the software trigger skims
165  if add_trigger_calculation and (not components or ("CDC" in components and "ECL" in components and "KLM" in components)):
166  add_skim_software_trigger(path)
167 
168 
169 def add_prefilter_reconstruction(path,
170  components=None,
171  add_modules_for_trigger_calculation=True,
172  skipGeometryAdding=False,
173  trackFitHypotheses=None,
174  use_second_cdc_hits=False,
175  add_muid_hits=False,
176  reconstruct_cdst=None,
177  event_abort=default_event_abort,
178  pxd_filtering_offline=False,
179  append_full_grid_cdc_eventt0=False):
180  """
181  This function adds only the reconstruction modules required to calculate HLT filter decision to a path.
182  Consists of essential tracking and the functionality provided by :func:`add_prefilter_posttracking_reconstruction()`.
183 
184  :param path: Add the modules to this path.
185  :param components: list of geometry components to include reconstruction for, or None for all components.
186  :param add_modules_for_trigger_calculation: add the modules necessary for computing the software trigger decision
187  during later stages (do not make any cut), relevant only when reconstruct_cdst is not None.
188  :param skipGeometryAdding: Advances flag: The tracking modules need the geometry module and will add it,
189  if it is not already present in the path. In a setup with multiple (conditional) paths however, it can not
190  determine, if the geometry is already loaded. This flag can be used to just turn off the geometry adding at
191  all (but you will have to add it on your own then).
192  :param trackFitHypotheses: Change the additional fitted track fit hypotheses. If no argument is given,
193  the fitted hypotheses are pion, muon and proton, i.e. [211, 321, 2212].
194  :param use_second_cdc_hits: If true, the second hit information will be used in the CDC track finding.
195  :param add_muid_hits: Add the found KLM hits to the RecoTrack. Make sure to refit the track afterwards.
196  :param reconstruct_cdst: None for mdst, 'rawFormat' to reconstruct cdsts in rawFormat, 'fullFormat' for the
197  full (old) format. This parameter is needed when reconstructing cdsts, otherwise the
198  required PXD objects won't be added.
199  :param event_abort: A function to abort event processing at the given point. Should take three arguments: a module,
200  the condition and the error_flag to be set if these events are kept. If run on HLT this will not abort the event
201  but just remove all data except for the event information.
202  :param pxd_filtering_offline: If True, PXD data reduction (ROI filtering) is applied during the track reconstruction.
203  The reconstructed SVD/CDC tracks are used to define the ROIs and reject all PXD clusters outside of these.
204  :param append_full_grid_cdc_eventt0: If True, the module FullGridChi2TrackTimeExtractor is added to the path
205  and provides the CDC temporary EventT0.
206  """
207 
208  # Always avoid the top-level 'import ROOT'.
209  from ROOT import Belle2 # noqa
210 
211  # Check components.
212  check_components(components)
213 
214  # Do not even attempt at reconstructing events w/ abnormally large occupancy.
215  doom = path.add_module("EventsOfDoomBuster")
216  event_abort(doom, ">=1", Belle2.EventMetaData.c_ReconstructionAbort)
217  path.add_module('StatisticsSummary').set_name('Sum_EventsofDoomBuster')
218 
219  # Add modules that have to be run BEFORE track reconstruction
220  add_prefilter_pretracking_reconstruction(path, components=components)
221 
222  # Add prefilter tracking reconstruction modules
223  add_prefilter_tracking_reconstruction(path,
224  components=components,
225  mcTrackFinding=False,
226  skipGeometryAdding=skipGeometryAdding,
227  trackFitHypotheses=trackFitHypotheses,
228  use_second_cdc_hits=use_second_cdc_hits,
229  pxd_filtering_offline=pxd_filtering_offline,
230  append_full_grid_cdc_eventt0=append_full_grid_cdc_eventt0)
231 
232  # Statistics summary
233  path.add_module('StatisticsSummary').set_name('Sum_Prefilter_Tracking')
234 
235  # In case of cdst reconstruction ...
236  if reconstruct_cdst:
237  add_special_vxd_modules(path, components=components)
238  if reconstruct_cdst == 'rawFormat' and not add_modules_for_trigger_calculation:
239  return
240 
241  # Add prefilter posttracking modules
242  add_prefilter_posttracking_reconstruction(path,
243  components=components,
244  add_muid_hits=add_muid_hits)
245 
246  # Statistics summary
247  path.add_module('StatisticsSummary').set_name('Sum_Prefilter_PostTracking')
248 
249 
250 def add_postfilter_reconstruction(path,
251  components=None,
252  pruneTracks=False,
253  addClusterExpertModules=True,
254  reconstruct_cdst=None,
255  legacy_ecl_charged_pid=False):
256  """
257  This function adds the reconstruction modules not required to calculate HLT filter decision to a path.
258 
259  :param path: Add the modules to this path.
260  :param components: list of geometry components to include reconstruction for, or None for all components.
261  :param pruneTracks: Delete all hits expect the first and the last from the found tracks.
262  :param addClusterExpertModules: Add the cluster expert modules in the KLM and ECL. Turn this off to
263  reduce execution time.
264  :param reconstruct_cdst: None for mdst, 'rawFormat' to reconstruct cdsts in rawFormat, 'fullFormat' for the
265  full (old) format. This parameter is needed when reconstructing cdsts, otherwise the
266  required PXD objects won't be added.
267  :param legacy_ecl_charged_pid: Bool denoting whether to use the legacy EoP based charged particleID in the ECL (true) or
268  MVA based charged particle ID (false).
269  """
270 
271  # Add postfilter tracking reconstruction modules
272  add_postfilter_tracking_reconstruction(path, components=components, pruneTracks=False)
273 
274  path.add_module('StatisticsSummary').set_name('Sum_Postfilter_Tracking')
275 
276  # Skip postfilter posttracking modules except dedx for raw format cdst reconstruction
277  if reconstruct_cdst == 'rawFormat':
278  add_dedx_modules(path, components=components)
279  if pruneTracks:
280  add_prune_tracks(path, components)
281  return
282 
283  # Add postfilter posttracking modules
284  add_postfilter_posttracking_reconstruction(path,
285  components=components,
286  addClusterExpertModules=addClusterExpertModules,
287  legacy_ecl_charged_pid=legacy_ecl_charged_pid)
288  # Prune tracks
289  if pruneTracks:
290  add_prune_tracks(path, components)
291 
292  # Statistics summary
293  path.add_module('StatisticsSummary').set_name('Sum_Postfilter_PostTracking')
294 
295 
296 def add_cosmics_reconstruction(
297  path,
298  components=None,
299  pruneTracks=True,
300  skipGeometryAdding=False,
301  eventTimingExtraction=True,
302  addClusterExpertModules=True,
303  merge_tracks=True,
304  use_second_cdc_hits=False,
305  add_muid_hits=False,
306  reconstruct_cdst=False,
307  posttracking=True,
308  eventt0_combiner_mode="prefer_cdc",
309  legacy_ecl_charged_pid=False,
310  ):
311  """
312  This function adds the standard reconstruction modules for cosmic data to a path.
313  Consists of tracking and the functionality provided by :func:`add_posttracking_reconstruction()`,
314  plus the modules to calculate the software trigger cuts.
315 
316  :param path: Add the modules to this path.
317  :param components: list of geometry components to include reconstruction for, or None for all components.
318  :param pruneTracks: Delete all hits except the first and last of the tracks after the dEdX modules.
319  :param skipGeometryAdding: Advances flag: The tracking modules need the geometry module and will add it,
320  if it is not already present in the path. In a setup with multiple (conditional) paths however, it can not
321  determine, if the geometry is already loaded. This flag can be used to just turn off the geometry adding at
322  all (but you will have to add it on your own then).
323 
324  :param eventTimingExtraction: extract the event time
325  :param addClusterExpertModules: Add the cluster expert modules in the KLM and ECL. Turn this off to reduce
326  execution time.
327 
328  :param merge_tracks: The upper and lower half of the tracks should be merged together in one track
329  :param use_second_cdc_hits: If true, the second hit information will be used in the CDC track finding.
330 
331  :param add_muid_hits: Add the found KLM hits to the RecoTrack. Make sure to refit the track afterwards.
332 
333  :param reconstruct_cdst: run only the minimal reconstruction needed to produce the cdsts (raw+tracking+dE/dx)
334  :param posttracking: run reconstruction for outer detectors.
335  :param eventt0_combiner_mode: Mode to combine the t0 values of the sub-detectors
336  :param legacy_ecl_charged_pid: Bool denoting whether to use the legacy EoP based charged particleID in the ECL (true) or
337  MVA based charged particle ID (false).
338  """
339 
340  # Check components.
341  check_components(components)
342 
343  # Add modules that have to be run before track reconstruction
344  add_prefilter_pretracking_reconstruction(path,
345  components=components)
346 
347  # Add cdc tracking reconstruction modules
348  add_cr_tracking_reconstruction(path,
349  components=components,
350  prune_tracks=False,
351  skip_geometry_adding=skipGeometryAdding,
352  event_time_extraction=eventTimingExtraction,
353  merge_tracks=merge_tracks,
354  use_second_cdc_hits=use_second_cdc_hits)
355 
356  # Statistics summary
357  path.add_module('StatisticsSummary').set_name('Sum_Tracking')
358 
359  if posttracking:
360  if reconstruct_cdst:
361  add_special_vxd_modules(path, components=components)
362  add_dedx_modules(path, components=components)
363  add_prune_tracks(path, components=components)
364 
365  else:
366  # Add further reconstruction modules
367  add_posttracking_reconstruction(path,
368  components=components,
369  pruneTracks=pruneTracks,
370  addClusterExpertModules=addClusterExpertModules,
371  add_muid_hits=add_muid_hits,
372  cosmics=True,
373  eventt0_combiner_mode=eventt0_combiner_mode,
374  legacy_ecl_charged_pid=legacy_ecl_charged_pid)
375 
376 
377 def add_mc_reconstruction(path, components=None, pruneTracks=True, addClusterExpertModules=True,
378  use_second_cdc_hits=False, add_muid_hits=False, legacy_ecl_charged_pid=False):
379  """
380  This function adds the standard reconstruction modules with MC tracking
381  to a path.
382 
383  @param components list of geometry components to include reconstruction for, or None for all components.
384  @param use_second_cdc_hits: If true, the second hit information will be used in the CDC track finding.
385  :param add_muid_hits: Add the found KLM hits to the RecoTrack. Make sure to refit the track afterwards.
386  :param legacy_ecl_charged_pid: Bool denoting whether to use the legacy EoP based charged particleID in the ECL (true) or
387  MVA based charged particle ID (false).
388  """
389 
390  # Add modules that have to be run before track reconstruction
391  add_prefilter_pretracking_reconstruction(path,
392  components=components)
393 
394  # tracking
395  add_mc_tracking_reconstruction(path,
396  components=components,
397  pruneTracks=False,
398  use_second_cdc_hits=use_second_cdc_hits)
399 
400  # Statistics summary
401  path.add_module('StatisticsSummary').set_name('Sum_MC_Tracking')
402 
403  # add further reconstruction modules
404  add_posttracking_reconstruction(path,
405  components=components,
406  pruneTracks=pruneTracks,
407  add_muid_hits=add_muid_hits,
408  addClusterExpertModules=addClusterExpertModules,
409  legacy_ecl_charged_pid=legacy_ecl_charged_pid)
410 
411 
412 def add_prefilter_pretracking_reconstruction(path, components=None):
413  """
414  This function adds the standard reconstruction modules BEFORE tracking
415  to a path.
416 
417  :param path: The path to add the modules to.
418  :param components: list of geometry components to include reconstruction for, or None for all components.
419  """
420 
421  add_ecl_modules(path, components)
422 
423  # Statistics summary
424  path.add_module('StatisticsSummary').set_name('Sum_Clustering')
425 
426 
427 def add_prefilter_posttracking_reconstruction(path,
428  components=None,
429  add_muid_hits=False,
430  for_cdst_analysis=False,
431  add_eventt0_combiner_for_cdst=False,
432  eventt0_combiner_mode="prefer_svd"):
433  """
434  This function adds to the path the standard reconstruction modules after prefilter tracking
435  whoose outputs are also needed in the filter.
436 
437  :param path: The path to add the modules to.
438  :param components: list of geometry components to include reconstruction for, or None for all components.
439  :param add_muid_hits: Add the found KLM hits to the RecoTrack. Make sure to refit the track afterwards.
440  :param for_cdst_analysis: if True, EventT0Combiner is not added to path.
441  This is only needed by prepare_cdst_analysis().
442  :param add_eventt0_combiner_for_cdst: if True, the EventT0Combiner module is added to the path even if
443  for_cdst_analysis is False. This is useful for validation purposes for avoiding to run the full
444  add_reconstruction(). Note that, with the default settings (for_cdst_analysis=False and
445  add_eventt0_combiner_for_cdst=False), the EventT0Combiner module is added to the path.
446  :param eventt0_combiner_mode: Mode to combine the t0 values of the sub-detectors
447  """
448 
449  add_ext_module(path, components)
450 
451  # Add EventT0Combiner, if this function is not called from prepare_cdst_analysis() or if requested also there.
452  if not for_cdst_analysis or add_eventt0_combiner_for_cdst:
453  path.add_module("EventT0Combiner", combinationLogic=eventt0_combiner_mode)
454  add_ecl_finalizer_module(path, components)
455  add_ecl_mc_matcher_module(path, components)
456  add_klm_modules(path, components)
457  add_klm_mc_matcher_module(path, components)
458  add_muid_module(path, add_hits_to_reco_track=add_muid_hits, components=components)
459  add_ecl_track_cluster_modules(path, components)
460  add_ecl_cluster_properties_modules(path, components)
461 
462 
463 def add_postfilter_posttracking_reconstruction(path,
464  components=None,
465  addClusterExpertModules=True,
466  cosmics=False,
467  for_cdst_analysis=False,
468  legacy_ecl_charged_pid=False):
469  """
470  This function adds to the path the standard reconstruction modules whoose outputs are not needed in the filter.
471 
472  :param path: The path to add the modules to.
473  :param components: list of geometry components to include reconstruction for, or None for all components.
474  :param addClusterExpertModules: Add the cluster expert modules in the KLM and ECL. Turn this off to reduce
475  execution time.
476  :param cosmics: if True, steer TOP for cosmic reconstruction.
477  :param for_cdst_analysis: if True, the OnlineEventT0Creator module is not added to the path.
478  This is only needed by prepare_cdst_analysis().
479  :param legacy_ecl_charged_pid: Bool denoting whether to use the legacy EoP based charged particleID in the ECL (true) or
480  MVA based charged particle ID (false).
481  """
482 
483  # Add dEdx modules, if this function is not called from prepare_cdst_analysis()
484  if not for_cdst_analysis:
485  add_dedx_modules(path, components)
486 
487  add_top_modules(path, components, cosmics=cosmics)
488  add_arich_modules(path, components)
489 
490  # only add the OnlineEventT0Creator if not preparing cDST
491  if not for_cdst_analysis:
492  path.add_module("OnlineEventT0Creator")
493 
494  add_ecl_chargedpid_module(path, components, legacy_ecl_charged_pid)
495  add_pid_module(path, components)
496 
497  if addClusterExpertModules:
498  # FIXME: Disabled for HLT until execution time bug is fixed
499  add_cluster_expert_modules(path, components)
500 
501  add_ecl_track_brem_finder(path, components)
502 
503 
504 def add_posttracking_reconstruction(path,
505  components=None,
506  pruneTracks=True,
507  addClusterExpertModules=True,
508  add_muid_hits=False,
509  cosmics=False,
510  for_cdst_analysis=False,
511  add_eventt0_combiner_for_cdst=False,
512  eventt0_combiner_mode="prefer_svd",
513  legacy_ecl_charged_pid=False):
514  """
515  This function adds the standard reconstruction modules after tracking
516  to a path.
517 
518  :param path: The path to add the modules to.
519  :param components: list of geometry components to include reconstruction for, or None for all components.
520  :param pruneTracks: Delete all hits except the first and last after the post-tracking modules.
521  :param addClusterExpertModules: Add the cluster expert modules in the KLM and ECL. Turn this off to reduce
522  execution time.
523  :param add_muid_hits: Add the found KLM hits to the RecoTrack. Make sure to refit the track afterwards.
524  :param cosmics: if True, steer TOP for cosmic reconstruction.
525  :param for_cdst_analysis: if True, the dEdx and PruneTracks modules are not added to the path, as well
526  as all EventT0 related modules.
527  This is only needed by prepare_cdst_analysis().
528  :param add_eventt0_combiner_for_cdst: if True, the EventT0Combiner module is added to the path even if
529  for_cdst_analysis is True. This is useful for validation purposes for avoiding to run the full
530  add_reconstruction(). Note that, with the default settings (for_cdst_analysis=False and
531  add_eventt0_combiner_for_cdst=False), the EventT0Combiner module is added to the path.
532  :param eventt0_combiner_mode: Mode to combine the t0 values of the sub-detectors
533  :param legacy_ecl_charged_pid: Bool denoting whether to use the legacy EoP based charged particleID in the ECL (true) or
534  MVA based charged particle ID (false).
535  """
536 
537  add_prefilter_posttracking_reconstruction(path,
538  components=components,
539  add_muid_hits=add_muid_hits,
540  for_cdst_analysis=for_cdst_analysis,
541  add_eventt0_combiner_for_cdst=add_eventt0_combiner_for_cdst,
542  eventt0_combiner_mode=eventt0_combiner_mode)
543 
544  add_postfilter_posttracking_reconstruction(path,
545  components=components,
546  addClusterExpertModules=addClusterExpertModules,
547  cosmics=cosmics,
548  for_cdst_analysis=for_cdst_analysis,
549  legacy_ecl_charged_pid=legacy_ecl_charged_pid)
550 
551  # Prune tracks as soon as the post-tracking steps are complete
552  # Not add prune tracks modules in prepare_cdst_analysis()
553  if not for_cdst_analysis:
554  if pruneTracks:
555  add_prune_tracks(path, components)
556 
557  path.add_module('StatisticsSummary').set_name('Sum_Posttracking_Reconstruction')
558 
559 
560 def add_mdst_output(*args, **kwargs):
561  """
562  .. deprecated:: release-08-00-00
563 
564  This function simply returns a FATAL message.
565 
566  Please use the equivalent function from the mdst package if you want to store
567  the output in a mDST file:
568 
569  .. code-block:: python
570 
571  import mdst
572  mdst.add_mdst_output(path=mypath)
573  """
574 
575  basf2.B2FATAL("This function is deprecated and it will be removed in release-09.\n"
576  "Please use the equivalent function from the mdst package.")
577 
578 
579 def add_cdst_output(path,
580  mc=True,
581  filename='cdst.root',
582  additionalBranches=None,
583  dataDescription=None,
584  ignoreInputModulesCheck=False):
585  """
586  This function adds the `RootOutput` module to a path with the settings needed to produce a cDST output.
587  The actual cDST output content depends on the value of the parameter `mc`:
588  * if `mc` is `False` (default setting), the cDST content is raw + tracking dataobjects;
589  * if `mc` is `True`, the cDST content is digits + MCParticles + tracking dataobjects.
590 
591  @param path Path to add modules to.
592  @param mc Define the type of cDST output content: `False` for raw + tracking dataobjects, `True` for digits +
593  MCParticles + tracking dataobjects.
594  @param filename Output file name.
595  @param additionalBranches Additional objects/arrays of event durability to save
596  @param dataDescription Additional key->value pairs to be added as data description
597  fields to the output FileMetaData.
598  @param ignoreInputModulesCheck If True, do not enforce check on missing PXD modules in the input path.
599  Needed when a conditional path is passed as input.
600  """
601 
602  branches = list(CDST_TRACKING_OBJECTS)
603  persistentBranches = ['FileMetaData']
604 
605  if not mc:
606  branches += ALWAYS_SAVE_OBJECTS + RAWDATA_OBJECTS
607  else:
608  branches += list(DIGITS_OBJECTS) + [
609  'MCParticles',
610  'EventLevelTriggerTimeInfo',
611  'SoftwareTriggerResult',
612  'TRGSummary']
613  persistentBranches += ['BackgroundInfo']
614 
615  if not ignoreInputModulesCheck and "PXDClustersFromTracks" not in [module.name() for module in path.modules()]:
616  basf2.B2ERROR("PXDClustersFromTracks is required in CDST output but its module is not found in the input path!")
617 
618  if dataDescription is None:
619  dataDescription = {}
620  dataDescription.setdefault("dataLevel", "cdst")
621 
622  if additionalBranches is not None:
623  branches += additionalBranches
624 
625  return path.add_module("RootOutput", outputFileName=filename, branchNames=branches,
626  branchNamesPersistent=persistentBranches, additionalDataDescription=dataDescription)
627 
628 
629 def add_arich_modules(path, components=None):
630  """
631  Add the ARICH reconstruction to the path.
632 
633  :param path: The path to add the modules to.
634  :param components: The components to use or None to use all standard components.
635  """
636  if components is None or 'ARICH' in components:
637  path.add_module('ARICHFillHits')
638  path.add_module('ARICHReconstructor',
639  storePhotons=1) # enabled for ARICH DQM plots
640 
641 
642 def add_top_modules(path, components=None, cosmics=False):
643  """
644  Add the TOP reconstruction to the path.
645 
646  :param path: The path to add the modules to.
647  :param components: The components to use or None to use all standard components.
648  :param cosmics: if True, steer TOP for cosmic reconstruction
649  """
650  if components is None or 'TOP' in components:
651  path.add_module('TOPChannelMasker')
652  if cosmics:
653  path.add_module('TOPCosmicT0Finder')
654  else:
655  path.add_module('TOPBunchFinder')
656  path.add_module('TOPReconstructor')
657 
658 
659 def add_cluster_expert_modules(path, components=None):
660  """
661  Add the KLMExpert and ClusterMatcher modules to the path.
662 
663  :param path: The path to add the modules to.
664  :param components: The components to use or None to use all standard components.
665  """
666  if components is None or ('KLM' in components and 'ECL' in components):
667  path.add_module('KLMExpert')
668  path.add_module('ClusterMatcher')
669 
670 
671 def add_pid_module(path, components=None):
672  """
673  Add the PID modules to the path.
674 
675  :param path: The path to add the modules to.
676  :param components: The components to use or None to use all standard components.
677  """
678  if components is None or 'SVD' in components or 'CDC' in components:
679  path.add_module('MdstPID')
680 
681 
682 def add_klm_modules(path, components=None):
683  """
684  Add the KLM reconstruction modules to the path.
685 
686  :param path: The path to add the modules to.
687  :param components: The components to use or None to use all standard components.
688  """
689  if components is None or 'KLM' in components:
690  path.add_module('KLMReconstructor')
691  path.add_module('KLMClustersReconstructor')
692 
693 
694 def add_klm_mc_matcher_module(path, components=None):
695  """
696  Add the KLM mc matcher module to the path.
697 
698  :param path: The path to add the modules to.
699  :param components: The components to use or None to use all standard components.
700  """
701  if components is None or 'KLM' in components:
702  path.add_module('MCMatcherKLMClusters')
703 
704 
705 def add_muid_module(path, add_hits_to_reco_track=False, components=None):
706  """
707  Add the MuID module to the path.
708 
709  :param path: The path to add the modules to.
710  :param add_hits_to_reco_track: Add the found KLM hits also to the RecoTrack. Make sure to refit the track afterwards.
711  :param components: The components to use or None to use all standard components.
712  """
713  # Muid is needed for muonID computation AND ECLCluster-Track matching.
714  if components is None or ('CDC' in components and 'ECL' in components and 'KLM' in components):
715  path.add_module('Muid',
716  addHitsToRecoTrack=add_hits_to_reco_track)
717  if components is not None and 'CDC' in components:
718  if ('ECL' not in components and 'KLM' in components):
719  basf2.B2WARNING('You added KLM to the components list but not ECL: the module Muid, that is necessary '
720  'for correct muonID computation, will not be added to your reconstruction path. '
721  'Make sure that this is fine for your purposes, otherwise please include also ECL.')
722  if ('ECL' in components and 'KLM' not in components):
723  basf2.B2WARNING('You added ECL to the components list but not KLM: the module Muid, that is necessary '
724  'for correct ECLCluster-Track matching, will not be added to your reconstruction path. '
725  ' Make sure that this is fine for your purposes, otherwise please include also KLM.')
726 
727 
728 def add_ecl_modules(path, components=None):
729  """
730  Add the ECL reconstruction modules to the path.
731 
732  :param path: The path to add the modules to.
733  :param components: The components to use or None to use all standard components.
734  """
735  if components is None or 'ECL' in components:
736  path.add_module('ECLWaveformFit')
737  path.add_module('ECLDigitCalibrator')
738  path.add_module('ECLEventT0')
739  path.add_module('ECLCRFinder') # connected region finder
740  path.add_module('ECLLocalMaximumFinder')
741  path.add_module('ECLSplitterN1')
742  path.add_module('ECLSplitterN2')
743  path.add_module('ECLShowerCorrector')
744  path.add_module('ECLShowerCalibrator')
745  path.add_module('ECLShowerShape')
746  path.add_module('ECLClusterPSD')
747  path.add_module('ECLCovarianceMatrix')
748  # The module ECLFinalizer must run after EventT0Combiner
749 
750 
751 def add_ecl_finalizer_module(path, components=None):
752  """
753  Add the ECL finalizer module to the path.
754 
755  :param path: The path to add the modules to.
756  :param components: The components to use or None to use all standard components.
757  """
758 
759  if components is None or 'ECL' in components:
760  path.add_module('ECLFinalizer')
761 
762 
763 def add_ecl_track_cluster_modules(path, components=None):
764  """
765  Add the ECL track cluster matching module to the path.
766 
767  :param path: The path to add the modules to.
768  :param components: The components to use or None to use all standard components.
769  """
770  if components is None or ('ECL' in components and ('PXD' in components or 'SVD' in components or 'CDC' in components)):
771  path.add_module('ECLTrackClusterMatching')
772 
773 
774 def add_ecl_cluster_properties_modules(path, components=None):
775  """
776  Add the ECL cluster properties module to the path.
777 
778  :param path: The path to add the modules to.
779  :param components: The components to use or None to use all standard components.
780  """
781  if components is None or ('ECL' in components and ('PXD' in components or 'SVD' in components or 'CDC' in components)):
782  path.add_module('ECLClusterProperties')
783 
784 
785 def add_ecl_track_brem_finder(path, components=None):
786  """
787  Add the bremsstrahlung finding module to the path.
788 
789  :param path: The path to add the modules to.
790  :param components: The components to use or None to use all standard components.
791  """
792  if components is None or ('ECL' in components and ('PXD' in components or 'SVD' in components)):
793  path.add_module('ECLTrackBremFinder')
794 
795 
796 def add_ecl_chargedpid_module(path, components=None, legacyMode=False):
797  """
798  Add the ECL charged PID module to the path.
799 
800  :param path: The path to add the modules to.
801  :param components: The components to use or None to use all standard components.
802  :param legacyMode: Uses the simple E/p based charged PID instead of the MVA based charged PID.
803  """
804  if components is None or 'ECL' in components:
805  # charged PID
806  if legacyMode:
807  path.add_module('ECLChargedPID')
808  else:
809  path.add_module('ECLChargedPIDMVA')
810 
811 
812 def add_ecl_mc_matcher_module(path, components=None):
813  """
814  Add the ECL MC matcher module to the path.
815 
816  :param path: The path to add the modules to.
817  :param components: The components to use or None to use all standard components.
818  """
819  if components is None or 'ECL' in components:
820  path.add_module('MCMatcherECLClusters')
821 
822 
823 def add_ext_module(path, components=None):
824  """
825  Add the extrapolation module to the path.
826 
827  :param path: The path to add the modules to.
828  :param components: The components to use or None to use all standard components.
829  """
830  if components is None or 'CDC' in components:
831  path.add_module('Ext')
832 
833 
834 def add_dedx_modules(path, components=None):
835  """
836  Add the dE/dX reconstruction modules to the path.
837 
838  :param path: The path to add the modules to.
839  :param components: The components to use or None to use all standard components.
840  """
841  # CDC dE/dx PID
842  if components is None or 'CDC' in components:
843  path.add_module('CDCDedxPID')
844  # VXD dE/dx PID
845  # only run this if the SVD is enabled - PXD is disabled by default
846  if components is None or 'SVD' in components:
847  path.add_module('VXDDedxPID')
848 
849 
850 def add_special_vxd_modules(path, components=None):
851  """
852  Add two modules that are not part of the standard reconstruction.
853 
854  :param path: The path to add the modules to.
855  :param components: The components to use or None to use all standard components.
856  """
857 
858  if not components or ('PXD' in components):
859  path.add_module("PXDClustersFromTracks")
860  if not components or ('SVD' in components):
861  path.add_module("SVDShaperDigitsFromTracks")
862 
863 
864 def prepare_cdst_analysis(path, components=None, mc=False, add_eventt0_combiner=False, legacy_ecl_charged_pid=False):
865  """
866  Adds to a (analysis) path all the modules needed to analyse a cDST file in the raw+tracking format
867  for collisions/cosmics data or in the digits+tracking format for MC data.
868 
869  :param path: The path to add the modules to.
870  :param components: The components to use or None to use all standard components.
871  :param mc: Are we running over MC data or not? If so, do not run the unpackers.
872  :param add_eventt0_combiner: If True, it adds the EventT0Combiner module when the post-tracking
873  reconstruction is run. This must NOT be used during the calibration, but it may be necessary
874  for validation purposes or for the user analyses.
875  :param legacy_ecl_charged_pid: Bool denoting whether to use the legacy EoP based charged particleID in the ECL (true) or
876  MVA based charged particle ID (false).
877  """
878  # Add the unpackers only if not running on MC, otherwise check the components and simply add
879  # the Gearbox and the Geometry modules
880  if not mc:
881  add_unpackers(path,
882  components=components)
883  else:
884  check_components(components)
885  path.add_module('Gearbox')
886  path.add_module('Geometry')
887 
888  # This currently just calls add_ecl_modules
889  add_prefilter_pretracking_reconstruction(path,
890  components=components)
891 
892  # Needed to retrieve the PXD and SVD clusters out of the raw data
893  if components is None or 'SVD' in components:
894  add_svd_reconstruction(path)
895  if components is None or 'PXD' in components:
896  add_pxd_reconstruction(path)
897 
898  # check, this one may not be needed...
899  path.add_module('SetupGenfitExtrapolation',
900  energyLossBrems=False,
901  noiseBrems=False)
902 
903  # Add the posttracking modules needed for the cDST analysis
904  add_posttracking_reconstruction(path,
905  components=components,
906  for_cdst_analysis=True,
907  add_eventt0_combiner_for_cdst=add_eventt0_combiner,
908  legacy_ecl_charged_pid=legacy_ecl_charged_pid)
909 
910 
911 def prepare_user_cdst_analysis(path, components=None, mc=False):
912  """
913  Adds to a (analysis) path all the modules needed to analyse a cDST file in the raw+tracking format
914  for collisions/cosmics data or in the digits+tracking format for MC data.
915  Differently from prepare_cdst_analysis(), this function add the EventT0Combiner module to the path,
916  which makes this function suitable for all the users and not only for the calibration expertes.
917  Note that the EventT0Combiner module is necessary for applying the proper EventT0 correction to
918  our data.
919 
920  :param path: The path to add the modules to.
921  :param components: The components to use or None to use all standard components.
922  :param mc: Are we running over MC data or not? If so, do not run the unpackers.
923  """
924  prepare_cdst_analysis(path=path, components=components, mc=mc, add_eventt0_combiner=True)