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