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 )
356 if pruneTracks:
357 add_prune_tracks(
358 path,
359 components
360 )
361 return
362
363 # Add postfilter posttracking modules
364 add_postfilter_posttracking_reconstruction(
365 path,
366 components=components,
367 addClusterExpertModules=addClusterExpertModules,
368 legacy_ecl_charged_pid=legacy_ecl_charged_pid,
369 run_klm_dnn=run_klm_dnn
370 )
371
372 # Prune tracks
373 if pruneTracks:
374 add_prune_tracks(
375 path,
376 components
377 )
378
379 # Statistics summary
380 path.add_module('StatisticsSummary').set_name('Sum_Postfilter_PostTracking')
381
382
383def add_cosmics_reconstruction(
384 path,
385 components=None,
386 pruneTracks=True,
387 skipGeometryAdding=False,
388 eventTimingExtraction=True,
389 addClusterExpertModules=True,
390 merge_tracks=True,
391 use_second_cdc_hits=False,
392 add_muid_hits=False,
393 reconstruct_cdst=False,
394 posttracking=True,
395 legacy_ecl_charged_pid=False,
396 ):
397 """
398 This function adds the standard reconstruction modules for cosmic data to a path.
399 Consists of tracking and the functionality provided by :func:`add_posttracking_reconstruction()`,
400 plus the modules to calculate the software trigger cuts.
401
402 :param path: Add the modules to this path.
403 :param components: list of geometry components to include reconstruction for, or None for all components.
404 :param pruneTracks: Delete all hits except the first and last of the tracks after the dEdX modules.
405 :param skipGeometryAdding: Advances flag: The tracking modules need the geometry module and will add it,
406 if it is not already present in the path. In a setup with multiple (conditional) paths however, it can not
407 determine, if the geometry is already loaded. This flag can be used to just turn off the geometry adding at
408 all (but you will have to add it on your own then).
409
410 :param eventTimingExtraction: extract the event time
411 :param addClusterExpertModules: Add the cluster expert modules in the KLM and ECL. Turn this off to reduce
412 execution time.
413
414 :param merge_tracks: The upper and lower half of the tracks should be merged together in one track
415 :param use_second_cdc_hits: If true, the second hit information will be used in the CDC track finding.
416
417 :param add_muid_hits: Add the found KLM hits to the RecoTrack. Make sure to refit the track afterwards.
418
419 :param reconstruct_cdst: run only the minimal reconstruction needed to produce the cdsts (raw+tracking+dE/dx)
420 :param posttracking: run reconstruction for outer detectors.
421 :param legacy_ecl_charged_pid: Bool denoting whether to use the legacy EoP based charged particleID in the ECL (true) or
422 MVA based charged particle ID (false).
423 """
424
425 # Set the run for cosmics data
426 basf2.declare_cosmics()
427
428 # Check components.
429 check_components(components)
430
431 # Add modules that have to be run before track reconstruction
432 add_prefilter_pretracking_reconstruction(path,
433 components=components)
434
435 # Add cdc tracking reconstruction modules
436 add_cr_tracking_reconstruction(path,
437 components=components,
438 prune_tracks=False,
439 skip_geometry_adding=skipGeometryAdding,
440 event_time_extraction=eventTimingExtraction,
441 merge_tracks=merge_tracks,
442 use_second_cdc_hits=use_second_cdc_hits)
443
444 # Statistics summary
445 path.add_module('StatisticsSummary').set_name('Sum_Tracking')
446
447 if posttracking:
448 if reconstruct_cdst:
449 add_special_vxd_modules(path, components=components)
450 add_dedx_modules(path, components=components)
451 add_prune_tracks(path, components=components)
452
453 else:
454 # Add further reconstruction modules
455 add_posttracking_reconstruction(path,
456 components=components,
457 pruneTracks=pruneTracks,
458 addClusterExpertModules=addClusterExpertModules,
459 add_muid_hits=add_muid_hits,
460 cosmics=True,
461 legacy_ecl_charged_pid=legacy_ecl_charged_pid)
462
463
464def add_mc_reconstruction(path, components=None, pruneTracks=True, addClusterExpertModules=True,
465 use_second_cdc_hits=False, add_muid_hits=False, legacy_ecl_charged_pid=False):
466 """
467 This function adds the standard reconstruction modules with MC tracking
468 to a path.
469
470 @param components list of geometry components to include reconstruction for, or None for all components.
471 @param use_second_cdc_hits: If true, the second hit information will be used in the CDC track finding.
472 :param add_muid_hits: Add the found KLM hits to the RecoTrack. Make sure to refit the track afterwards.
473 :param legacy_ecl_charged_pid: Bool denoting whether to use the legacy EoP based charged particleID in the ECL (true) or
474 MVA based charged particle ID (false).
475 """
476
477 # Set the run for beam data
478 basf2.declare_beam()
479
480 # Add modules that have to be run before track reconstruction
481 add_prefilter_pretracking_reconstruction(path,
482 components=components)
483
484 # tracking
485 add_mc_tracking_reconstruction(path,
486 components=components,
487 pruneTracks=False,
488 use_second_cdc_hits=use_second_cdc_hits)
489
490 # Statistics summary
491 path.add_module('StatisticsSummary').set_name('Sum_MC_Tracking')
492
493 # add further reconstruction modules
494 add_posttracking_reconstruction(path,
495 components=components,
496 pruneTracks=pruneTracks,
497 add_muid_hits=add_muid_hits,
498 addClusterExpertModules=addClusterExpertModules,
499 legacy_ecl_charged_pid=legacy_ecl_charged_pid)
500
501
502def add_prefilter_pretracking_reconstruction(path, components=None):
503 """
504 This function adds the standard reconstruction modules BEFORE tracking
505 to a path.
506
507 :param path: The path to add the modules to.
508 :param components: list of geometry components to include reconstruction for, or None for all components.
509 """
510
511 add_ecl_modules(path, components)
512
513 # Statistics summary
514 path.add_module('StatisticsSummary').set_name('Sum_Clustering')
515
516
517def add_prefilter_posttracking_reconstruction(path,
518 components=None,
519 add_muid_hits=False,
520 for_cdst_analysis=False,
521 add_eventt0_combiner_for_cdst=False):
522 """
523 This function adds to the path the standard reconstruction modules after prefilter tracking
524 whoose outputs are also needed in the filter.
525
526 :param path: The path to add the modules to.
527 :param components: list of geometry components to include reconstruction for, or None for all components.
528 :param add_muid_hits: Add the found KLM hits to the RecoTrack. Make sure to refit the track afterwards.
529 :param for_cdst_analysis: if True, EventT0Combiner is not added to path.
530 This is only needed by prepare_cdst_analysis().
531 :param add_eventt0_combiner_for_cdst: if True, the EventT0Combiner module is added to the path even if
532 for_cdst_analysis is False. This is useful for validation purposes for avoiding to run the full
533 add_reconstruction(). Note that, with the default settings (for_cdst_analysis=False and
534 add_eventt0_combiner_for_cdst=False), the EventT0Combiner module is added to the path.
535 """
536
537 add_ext_module(path, components)
538
539 # Add EventT0Combiner, if this function is not called from prepare_cdst_analysis() or if requested also there.
540 if not for_cdst_analysis or add_eventt0_combiner_for_cdst:
541 path.add_module("EventT0Combiner")
542 add_ecl_finalizer_module(path, components)
543 add_ecl_mc_matcher_module(path, components)
544 add_klm_modules(path, components)
545 add_klm_mc_matcher_module(path, components)
546 add_muid_module(path, add_hits_to_reco_track=add_muid_hits, components=components)
547 add_ecl_track_cluster_modules(path, components)
548 add_ecl_cluster_properties_modules(path, components)
549
550
551def add_postfilter_posttracking_reconstruction(path,
552 components=None,
553 addClusterExpertModules=True,
554 cosmics=False,
555 for_cdst_analysis=False,
556 legacy_ecl_charged_pid=False,
557 run_klm_dnn=True):
558 """
559 This function adds to the path the standard reconstruction modules whoose outputs are not needed in the filter.
560
561 :param path: The path to add the modules to.
562 :param components: list of geometry components to include reconstruction for, or None for all components.
563 :param addClusterExpertModules: Add the cluster expert modules in the KLM and ECL. Turn this off to reduce
564 execution time.
565 :param cosmics: if True, steer TOP for cosmic reconstruction.
566 :param for_cdst_analysis: if True, the OnlineEventT0Creator module is not added to the path.
567 This is only needed by prepare_cdst_analysis().
568 :param legacy_ecl_charged_pid: Bool denoting whether to use the legacy EoP based charged particleID in the ECL (true) or
569 MVA based charged particle ID (false). This flag is automatically set to true on HLT and ExpressReco.
570 :param run_klm_dnn: If True, add the ``KLMMuonIDDNNExpert`` module to the path. This flag is automatically set to
571 false on HLT and ExpressReco.
572 """
573
574 add_dedx_modules(path, components, for_cdst_analysis=for_cdst_analysis)
575 add_top_modules(path, components, cosmics=cosmics)
576 add_arich_modules(path, components)
577
578 # only add the OnlineEventT0Creator if not preparing cDST
579 if not for_cdst_analysis:
580 path.add_module("OnlineEventT0Creator")
581
582 add_ecl_chargedpid_module(path, components, legacy_ecl_charged_pid)
583 add_pid_module(path, components, run_klm_dnn)
584
585 if addClusterExpertModules:
586 # FIXME: Disabled for HLT until execution time bug is fixed
587 add_cluster_expert_modules(path, components)
588
589 add_ecl_track_brem_finder(path, components)
590
591
592def add_posttracking_reconstruction(path,
593 components=None,
594 pruneTracks=True,
595 addClusterExpertModules=True,
596 add_muid_hits=False,
597 cosmics=False,
598 for_cdst_analysis=False,
599 add_eventt0_combiner_for_cdst=False,
600 legacy_ecl_charged_pid=False):
601 """
602 This function adds the standard reconstruction modules after tracking
603 to a path.
604
605 :param path: The path to add the modules to.
606 :param components: list of geometry components to include reconstruction for, or None for all components.
607 :param pruneTracks: Delete all hits except the first and last after the post-tracking modules.
608 :param addClusterExpertModules: Add the cluster expert modules in the KLM and ECL. Turn this off to reduce
609 execution time.
610 :param add_muid_hits: Add the found KLM hits to the RecoTrack. Make sure to refit the track afterwards.
611 :param cosmics: if True, steer TOP for cosmic reconstruction.
612 :param for_cdst_analysis: if True, the dEdx and PruneTracks modules are not added to the path, as well
613 as all EventT0 related modules.
614 This is only needed by prepare_cdst_analysis().
615 :param add_eventt0_combiner_for_cdst: if True, the EventT0Combiner module is added to the path even if
616 for_cdst_analysis is True. This is useful for validation purposes for avoiding to run the full
617 add_reconstruction(). Note that, with the default settings (for_cdst_analysis=False and
618 add_eventt0_combiner_for_cdst=False), the EventT0Combiner module is added to the path.
619 :param legacy_ecl_charged_pid: Bool denoting whether to use the legacy EoP based charged particleID in the ECL (true) or
620 MVA based charged particle ID (false).
621 """
622
623 add_prefilter_posttracking_reconstruction(path,
624 components=components,
625 add_muid_hits=add_muid_hits,
626 for_cdst_analysis=for_cdst_analysis,
627 add_eventt0_combiner_for_cdst=add_eventt0_combiner_for_cdst)
628
629 add_postfilter_posttracking_reconstruction(path,
630 components=components,
631 addClusterExpertModules=addClusterExpertModules,
632 cosmics=cosmics,
633 for_cdst_analysis=for_cdst_analysis,
634 legacy_ecl_charged_pid=legacy_ecl_charged_pid)
635
636 # Prune tracks as soon as the post-tracking steps are complete
637 # Not add prune tracks modules in prepare_cdst_analysis()
638 if not for_cdst_analysis:
639 if pruneTracks:
640 add_prune_tracks(path, components)
641
642 path.add_module('StatisticsSummary').set_name('Sum_Posttracking_Reconstruction')
643
644
645def add_cdst_output(path,
646 mc=True,
647 filename='cdst.root',
648 additionalBranches=None,
649 dataDescription=None,
650 ignoreInputModulesCheck=False):
651 """
652 This function adds the `RootOutput` module to a path with the settings needed to produce a cDST output.
653 The actual cDST output content depends on the value of the parameter `mc`:
654 * if `mc` is `False` (default setting), the cDST content is raw + tracking dataobjects;
655 * if `mc` is `True`, the cDST content is digits + MCParticles + tracking dataobjects.
656
657 @param path Path to add modules to.
658 @param mc Define the type of cDST output content: `False` for raw + tracking dataobjects, `True` for digits +
659 MCParticles + tracking dataobjects.
660 @param filename Output file name.
661 @param additionalBranches Additional objects/arrays of event durability to save
662 @param dataDescription Additional key->value pairs to be added as data description
663 fields to the output FileMetaData.
664 @param ignoreInputModulesCheck If True, do not enforce check on missing PXD modules in the input path.
665 Needed when a conditional path is passed as input.
666 """
667
668 branches = list(CDST_TRACKING_OBJECTS)
669 persistentBranches = ['FileMetaData']
670
671 if not mc:
672 branches += ALWAYS_SAVE_OBJECTS + RAWDATA_OBJECTS
673 else:
674 branches += list(DIGITS_OBJECTS) + [
675 'MCParticles',
676 'EventLevelTriggerTimeInfo',
677 'SoftwareTriggerResult',
678 'TRGSummary']
679 persistentBranches += ['BackgroundInfo']
680
681 if not ignoreInputModulesCheck and "PXDClustersFromTracks" not in [module.name() for module in path.modules()]:
682 basf2.B2ERROR("PXDClustersFromTracks is required in CDST output but its module is not found in the input path!")
683
684 if dataDescription is None:
685 dataDescription = {}
686 dataDescription.setdefault("dataLevel", "cdst")
687
688 if additionalBranches is not None:
689 branches += additionalBranches
690
691 return path.add_module("RootOutput", outputFileName=filename, branchNames=branches,
692 branchNamesPersistent=persistentBranches, additionalDataDescription=dataDescription)
693
694
695def add_arich_modules(path, components=None):
696 """
697 Add the ARICH reconstruction to the path.
698
699 :param path: The path to add the modules to.
700 :param components: The components to use or None to use all standard components.
701 """
702 if components is None or 'ARICH' in components:
703 path.add_module('ARICHFillHits')
704 path.add_module('ARICHReconstructor',
705 storePhotons=1) # enabled for ARICH DQM plots
706
707
708def add_top_modules(path, components=None, cosmics=False):
709 """
710 Add the TOP reconstruction to the path.
711
712 :param path: The path to add the modules to.
713 :param components: The components to use or None to use all standard components.
714 :param cosmics: if True, steer TOP for cosmic reconstruction
715 """
716 if components is None or 'TOP' in components:
717 path.add_module('TOPChannelMasker')
718 if cosmics:
719 path.add_module('TOPCosmicT0Finder')
720 else:
721 path.add_module('TOPBunchFinder')
722 path.add_module('TOPReconstructor')
723
724
725def add_cluster_expert_modules(path, components=None):
726 """
727 Add the KLMExpert and ClusterMatcher modules to the path.
728
729 :param path: The path to add the modules to.
730 :param components: The components to use or None to use all standard components.
731 """
732 if components is None or ('KLM' in components and 'ECL' in components):
733 path.add_module('KLMExpert')
734 path.add_module('ClusterMatcher')
735
736
737def add_pid_module(path, components=None, run_klm_dnn=True):
738 """
739 Add the PID modules to the path.
740
741 :param path: The path to add the modules to.
742 :param components: The components to use or None to use all standard components.
743 :param run_klm_dnn: If True, add the ``KLMMuonIDDNNExpert`` module to the path.
744 This flag is automatically set to false on HLT and ExpressReco.
745 """
746 if components is None or 'SVD' in components or 'CDC' in components:
747 path.add_module('MdstPID')
748 if (components is None or 'KLM' in components) and run_klm_dnn:
749 path.add_module('KLMMuonIDDNNExpert')
750
751
752def add_klm_modules(path, components=None):
753 """
754 Add the KLM reconstruction modules to the path.
755
756 :param path: The path to add the modules to.
757 :param components: The components to use or None to use all standard components.
758 """
759 if components is None or 'KLM' in components:
760 path.add_module('KLMReconstructor')
761 path.add_module('KLMClustersReconstructor')
762 path.add_module('KLMClusterAna')
763
764
765def add_klm_mc_matcher_module(path, components=None):
766 """
767 Add the KLM mc matcher module to the path.
768
769 :param path: The path to add the modules to.
770 :param components: The components to use or None to use all standard components.
771 """
772 if components is None or 'KLM' in components:
773 path.add_module('MCMatcherKLMClusters')
774
775
776def add_muid_module(path, add_hits_to_reco_track=False, components=None):
777 """
778 Add the MuID module to the path.
779
780 :param path: The path to add the modules to.
781 :param add_hits_to_reco_track: Add the found KLM hits also to the RecoTrack. Make sure to refit the track afterwards.
782 :param components: The components to use or None to use all standard components.
783 """
784 # Muid is needed for muonID computation AND ECLCluster-Track matching.
785 if components is None or ('CDC' in components and 'ECL' in components and 'KLM' in components):
786 path.add_module('Muid',
787 addHitsToRecoTrack=add_hits_to_reco_track)
788 if components is not None and 'CDC' in components:
789 if ('ECL' not in components and 'KLM' in components):
790 basf2.B2WARNING('You added KLM to the components list but not ECL: the module Muid, that is necessary '
791 'for correct muonID computation, will not be added to your reconstruction path. '
792 'Make sure that this is fine for your purposes, otherwise please include also ECL.')
793 if ('ECL' in components and 'KLM' not in components):
794 basf2.B2WARNING('You added ECL to the components list but not KLM: the module Muid, that is necessary '
795 'for correct ECLCluster-Track matching, will not be added to your reconstruction path. '
796 ' Make sure that this is fine for your purposes, otherwise please include also KLM.')
797
798
799def add_ecl_modules(path, components=None):
800 """
801 Add the ECL reconstruction modules to the path.
802
803 :param path: The path to add the modules to.
804 :param components: The components to use or None to use all standard components.
805 """
806 if components is None or 'ECL' in components:
807 path.add_module('ECLWaveformFit')
808 path.add_module('ECLDigitCalibrator')
809 path.add_module('ECLEventT0')
810 path.add_module('ECLCRFinder') # connected region finder
811 path.add_module('ECLLocalMaximumFinder')
812 path.add_module('ECLSplitterN1')
813 path.add_module('ECLSplitterN2')
814 path.add_module('ECLShowerCorrector')
815 path.add_module('ECLShowerShape')
816 path.add_module('ECLClusterPSD')
817 path.add_module('ECLCovarianceMatrix')
818 # The module ECLFinalizer must run after EventT0Combiner
819
820
821def add_ecl_finalizer_module(path, components=None):
822 """
823 Add the ECL finalizer module to the path.
824
825 :param path: The path to add the modules to.
826 :param components: The components to use or None to use all standard components.
827 """
828
829 if components is None or 'ECL' in components:
830 path.add_module('ECLFinalizer')
831
832
833def add_ecl_track_cluster_modules(path, components=None):
834 """
835 Add the ECL track cluster matching module to the path.
836
837 :param path: The path to add the modules to.
838 :param components: The components to use or None to use all standard components.
839 """
840 if components is None or ('ECL' in components and ('PXD' in components or 'SVD' in components or 'CDC' in components)):
841 path.add_module('ECLTrackClusterMatching')
842
843
844def add_ecl_cluster_properties_modules(path, components=None):
845 """
846 Add the ECL cluster properties module to the path.
847
848 :param path: The path to add the modules to.
849 :param components: The components to use or None to use all standard components.
850 """
851 if components is None or ('ECL' in components and ('PXD' in components or 'SVD' in components or 'CDC' in components)):
852 path.add_module('ECLClusterProperties')
853
854
855def add_ecl_track_brem_finder(path, components=None):
856 """
857 Add the bremsstrahlung finding module to the path.
858
859 :param path: The path to add the modules to.
860 :param components: The components to use or None to use all standard components.
861 """
862 if components is None or ('ECL' in components and ('PXD' in components or 'SVD' in components)):
863 path.add_module('ECLTrackBremFinder')
864
865
866def add_ecl_chargedpid_module(path, components=None, legacyMode=False):
867 """
868 Add the ECL charged PID module to the path.
869
870 :param path: The path to add the modules to.
871 :param components: The components to use or None to use all standard components.
872 :param legacyMode: Uses the simple E/p based charged PID instead of the MVA based charged PID.
873 This flag is automatically set to true on HLT and ExpressReco.
874 """
875 if components is None or 'ECL' in components:
876 # charged PID
877 if legacyMode:
878 path.add_module('ECLChargedPID')
879 else:
880 path.add_module('ECLFillCellIdMapping')
881 path.add_module('ECLChargedPIDMVA')
882
883
884def add_ecl_mc_matcher_module(path, components=None):
885 """
886 Add the ECL MC matcher module to the path.
887
888 :param path: The path to add the modules to.
889 :param components: The components to use or None to use all standard components.
890 """
891 if components is None or 'ECL' in components:
892 path.add_module('MCMatcherECLClusters')
893
894
895def add_ext_module(path, components=None):
896 """
897 Add the extrapolation module to the path.
898
899 :param path: The path to add the modules to.
900 :param components: The components to use or None to use all standard components.
901 """
902 if components is None or 'CDC' in components:
903 path.add_module('Ext')
904
905
906def add_dedx_modules(path, components=None, for_cdst_analysis=False):
907 """
908 Add the dE/dX reconstruction modules to the path.
909
910 :param path: The path to add the modules to.
911 :param components: The components to use or None to use all standard components.
912 :param for_cdst_analysis: if True, add only DedxPIDCreator module, otherwise add both
913 :param enableDebugOutput: enable/disable writing out debugging information to CDCDedxTracks
914 """
915 # CDC dE/dx PID
916 if components is None or 'CDC' in components:
917 if for_cdst_analysis:
918 path.add_module('CDCDedxPIDCreator')
919 else:
920 path.add_module('CDCDedxHitSaver')
921 path.add_module('CDCDedxPIDCreator')
922 # VXD dE/dx PID
923 # only run this if the SVD is enabled - PXD is disabled by default
924 if components is None or 'SVD' in components:
925 if for_cdst_analysis:
926 path.add_module('VXDDedxPIDRemaker')
927 else:
928 path.add_module('VXDDedxPID')
929
930
931def add_special_vxd_modules(path, components=None):
932 """
933 Add two modules that are not part of the standard reconstruction.
934
935 :param path: The path to add the modules to.
936 :param components: The components to use or None to use all standard components.
937 """
938
939 if not components or ('PXD' in components):
940 path.add_module("PXDClustersFromTracks")
941 if not components or ('SVD' in components):
942 path.add_module("SVDShaperDigitsFromTracks")
943
944
945def prepare_cdst_analysis(path, components=None, mc=False, add_eventt0_combiner=False, legacy_ecl_charged_pid=False):
946 """
947 Adds to a (analysis) path all the modules needed to analyse a cDST file in the raw+tracking format
948 for collisions/cosmics data or in the digits+tracking format for MC data.
949
950 :param path: The path to add the modules to.
951 :param components: The components to use or None to use all standard components.
952 :param mc: Are we running over MC data or not? If so, do not run the unpackers.
953 :param add_eventt0_combiner: If True, it adds the EventT0Combiner module when the post-tracking
954 reconstruction is run. This must NOT be used during the calibration, but it may be necessary
955 for validation purposes or for the user analyses.
956 :param legacy_ecl_charged_pid: Bool denoting whether to use the legacy EoP based charged particleID in the ECL (true) or
957 MVA based charged particle ID (false).
958 """
959 # Add the unpackers only if not running on MC, otherwise check the components and simply add
960 # the Gearbox and the Geometry modules
961 if not mc:
962 add_unpackers(path,
963 components=components)
964 else:
965 check_components(components)
966 path.add_module('Gearbox')
967 path.add_module('Geometry')
968
969 # This currently just calls add_ecl_modules
970 add_prefilter_pretracking_reconstruction(path,
971 components=components)
972
973 # Needed to retrieve the PXD and SVD clusters out of the raw data
974 if components is None or 'SVD' in components:
975 add_svd_reconstruction(path)
976 if components is None or 'PXD' in components:
977 add_pxd_reconstruction(path)
978
979 # check, this one may not be needed...
980 path.add_module('SetupGenfitExtrapolation',
981 energyLossBrems=False,
982 noiseBrems=False)
983
984 # Add the posttracking modules needed for the cDST analysis
985 add_posttracking_reconstruction(path,
986 components=components,
987 for_cdst_analysis=True,
988 add_eventt0_combiner_for_cdst=add_eventt0_combiner,
989 legacy_ecl_charged_pid=legacy_ecl_charged_pid)
990
991
992def prepare_user_cdst_analysis(path, components=None, mc=False):
993 """
994 Adds to a (analysis) path all the modules needed to analyse a cDST file in the raw+tracking format
995 for collisions/cosmics data or in the digits+tracking format for MC data.
996 Differently from prepare_cdst_analysis(), this function add the EventT0Combiner module to the path,
997 which makes this function suitable for all the users and not only for the calibration expertes.
998 Note that the EventT0Combiner module is necessary for applying the proper EventT0 correction to
999 our data.
1000
1001 :param path: The path to add the modules to.
1002 :param components: The components to use or None to use all standard components.
1003 :param mc: Are we running over MC data or not? If so, do not run the unpackers.
1004 """
1005 prepare_cdst_analysis(path=path, components=components, mc=mc, add_eventt0_combiner=True)