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