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 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 (not components or ("CDC" in components and "ECL" in components and "KLM" in 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 (not components or ("CDC" in components and "ECL" in components and "KLM" in 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 components is None or 'ARICH' in 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 components is None or 'TOP' in 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 KLMExpert and ClusterMatcher modules 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 components is None or ('KLM' in components and 'ECL' in components):
731 path.add_module('KLMExpert')
732 path.add_module('ClusterMatcher')
733
734
735def add_pid_module(path, components=None, run_klm_dnn=True):
736 """
737 Add the PID modules to the path.
738
739 :param path: The path to add the modules to.
740 :param components: The components to use or None to use all standard components.
741 :param run_klm_dnn: If True, add the ``KLMMuonIDDNNExpert`` module to the path.
742 This flag is automatically set to false on HLT and ExpressReco.
743 """
744 if components is None or 'SVD' in components or 'CDC' in components:
745 path.add_module('MdstPID')
746 if (components is None or 'KLM' in components) and run_klm_dnn:
747 path.add_module('KLMMuonIDDNNExpert')
748
749
750def add_klm_modules(path, components=None):
751 """
752 Add the KLM reconstruction modules to the path.
753
754 :param path: The path to add the modules to.
755 :param components: The components to use or None to use all standard components.
756 """
757 if components is None or 'KLM' in components:
758 path.add_module('KLMReconstructor')
759 path.add_module('KLMClustersReconstructor')
760 path.add_module('KLMClusterAna')
761
762
763def add_klm_mc_matcher_module(path, components=None):
764 """
765 Add the KLM mc matcher module to the path.
766
767 :param path: The path to add the modules to.
768 :param components: The components to use or None to use all standard components.
769 """
770 if components is None or 'KLM' in components:
771 path.add_module('MCMatcherKLMClusters')
772
773
774def add_muid_module(path, add_hits_to_reco_track=False, components=None):
775 """
776 Add the MuID module to the path.
777
778 :param path: The path to add the modules to.
779 :param add_hits_to_reco_track: Add the found KLM hits also to the RecoTrack. Make sure to refit the track afterwards.
780 :param components: The components to use or None to use all standard components.
781 """
782 # Muid is needed for muonID computation AND ECLCluster-Track matching.
783 if components is None or ('CDC' in components and 'ECL' in components and 'KLM' in components):
784 path.add_module('Muid',
785 addHitsToRecoTrack=add_hits_to_reco_track)
786 if components is not None and 'CDC' in components:
787 if ('ECL' not in components and 'KLM' in components):
788 basf2.B2WARNING('You added KLM to the components list but not ECL: the module Muid, that is necessary '
789 'for correct muonID computation, will not be added to your reconstruction path. '
790 'Make sure that this is fine for your purposes, otherwise please include also ECL.')
791 if ('ECL' in components and 'KLM' not in components):
792 basf2.B2WARNING('You added ECL to the components list but not KLM: the module Muid, that is necessary '
793 'for correct ECLCluster-Track matching, will not be added to your reconstruction path. '
794 ' Make sure that this is fine for your purposes, otherwise please include also KLM.')
795
796
797def add_ecl_modules(path, components=None):
798 """
799 Add the ECL reconstruction modules to the path.
800
801 :param path: The path to add the modules to.
802 :param components: The components to use or None to use all standard components.
803 """
804 if components is None or 'ECL' in components:
805 path.add_module('ECLWaveformFit')
806 path.add_module('ECLDigitCalibrator')
807 path.add_module('ECLEventT0')
808 path.add_module('ECLCRFinder') # connected region finder
809 path.add_module('ECLLocalMaximumFinder')
810 path.add_module('ECLSplitterN1')
811 path.add_module('ECLSplitterN2')
812 path.add_module('ECLShowerCorrector')
813 path.add_module('ECLShowerShape')
814 path.add_module('ECLClusterPSD')
815 path.add_module('ECLCovarianceMatrix')
816 # The module ECLFinalizer must run after EventT0Combiner
817
818
819def add_ecl_finalizer_module(path, components=None):
820 """
821 Add the ECL finalizer module to the path.
822
823 :param path: The path to add the modules to.
824 :param components: The components to use or None to use all standard components.
825 """
826
827 if components is None or 'ECL' in components:
828 path.add_module('ECLFinalizer')
829
830
831def add_ecl_track_cluster_modules(path, components=None):
832 """
833 Add the ECL track cluster matching module to the path.
834
835 :param path: The path to add the modules to.
836 :param components: The components to use or None to use all standard components.
837 """
838 if components is None or ('ECL' in components and ('PXD' in components or 'SVD' in components or 'CDC' in components)):
839 path.add_module('ECLTrackClusterMatching')
840
841
842def add_ecl_cluster_properties_modules(path, components=None):
843 """
844 Add the ECL cluster properties module to the path.
845
846 :param path: The path to add the modules to.
847 :param components: The components to use or None to use all standard components.
848 """
849 if components is None or ('ECL' in components and ('PXD' in components or 'SVD' in components or 'CDC' in components)):
850 path.add_module('ECLClusterProperties')
851
852
853def add_ecl_track_brem_finder(path, components=None):
854 """
855 Add the bremsstrahlung finding module to the path.
856
857 :param path: The path to add the modules to.
858 :param components: The components to use or None to use all standard components.
859 """
860 if components is None or ('ECL' in components and ('PXD' in components or 'SVD' in components)):
861 path.add_module('ECLTrackBremFinder')
862
863
864def add_ecl_chargedpid_module(path, components=None, legacyMode=False):
865 """
866 Add the ECL charged PID module to the path.
867
868 :param path: The path to add the modules to.
869 :param components: The components to use or None to use all standard components.
870 :param legacyMode: Uses the simple E/p based charged PID instead of the MVA based charged PID.
871 This flag is automatically set to true on HLT and ExpressReco.
872 """
873 if components is None or 'ECL' in components:
874 # charged PID
875 if legacyMode:
876 path.add_module('ECLChargedPID')
877 else:
878 path.add_module('ECLFillCellIdMapping')
879 path.add_module('ECLChargedPIDMVA')
880
881
882def add_ecl_mc_matcher_module(path, components=None):
883 """
884 Add the ECL MC matcher module to the path.
885
886 :param path: The path to add the modules to.
887 :param components: The components to use or None to use all standard components.
888 """
889 if components is None or 'ECL' in components:
890 path.add_module('MCMatcherECLClusters')
891
892
893def add_ext_module(path, components=None):
894 """
895 Add the extrapolation module to the path.
896
897 :param path: The path to add the modules to.
898 :param components: The components to use or None to use all standard components.
899 """
900 if components is None or 'CDC' in components:
901 path.add_module('Ext')
902
903
904def add_dedx_modules(path, components=None, for_cdst_analysis=False):
905 """
906 Add the dE/dX reconstruction modules to the path.
907
908 :param path: The path to add the modules to.
909 :param components: The components to use or None to use all standard components.
910 :param for_cdst_analysis: if True, add only DedxPIDCreator module, otherwise add both
911 """
912 # CDC dE/dx PID
913 if components is None or 'CDC' in components:
914 if for_cdst_analysis:
915 path.add_module('CDCDedxPIDCreator')
916 else:
917 path.add_module('CDCDedxHitSaver')
918 path.add_module('CDCDedxPIDCreator')
919 # VXD dE/dx PID
920 # only run this if the SVD is enabled - PXD is disabled by default
921 if components is None or 'SVD' in components:
922 if for_cdst_analysis:
923 path.add_module('VXDDedxPIDRemaker')
924 else:
925 path.add_module('VXDDedxPID')
926
927
928def add_special_vxd_modules(path, components=None):
929 """
930 Add two modules that are not part of the standard reconstruction.
931
932 :param path: The path to add the modules to.
933 :param components: The components to use or None to use all standard components.
934 """
935
936 if not components or ('PXD' in components):
937 path.add_module("PXDClustersFromTracks")
938 if not components or ('SVD' in components):
939 path.add_module("SVDShaperDigitsFromTracks")
940
941
942def prepare_cdst_analysis(path, components=None, mc=False, add_eventt0_combiner=False, legacy_ecl_charged_pid=False):
943 """
944 Adds to a (analysis) path all the modules needed to analyse a cDST file in the raw+tracking format
945 for collisions/cosmics data or in the digits+tracking format for MC data.
946
947 :param path: The path to add the modules to.
948 :param components: The components to use or None to use all standard components.
949 :param mc: Are we running over MC data or not? If so, do not run the unpackers.
950 :param add_eventt0_combiner: If True, it adds the EventT0Combiner module when the post-tracking
951 reconstruction is run. This must NOT be used during the calibration, but it may be necessary
952 for validation purposes or for the user analyses.
953 :param legacy_ecl_charged_pid: Bool denoting whether to use the legacy EoP based charged particleID in the ECL (true) or
954 MVA based charged particle ID (false).
955 """
956 # Add the unpackers only if not running on MC, otherwise check the components and simply add
957 # the Gearbox and the Geometry modules
958 if not mc:
959 from rawdata import add_unpackers # noqa
960 add_unpackers(path,
961 components=components)
962 else:
963 check_components(components)
964 path.add_module('Gearbox')
965 path.add_module('Geometry')
966
967 # This currently just calls add_ecl_modules
968 add_prefilter_pretracking_reconstruction(path,
969 components=components)
970
971 # Needed to retrieve the PXD and SVD clusters out of the raw data
972 if components is None or 'SVD' in components:
973 add_svd_reconstruction(path)
974 if components is None or 'PXD' in components:
975 add_pxd_reconstruction(path)
976
977 # check, this one may not be needed...
978 path.add_module('SetupGenfitExtrapolation',
979 energyLossBrems=False,
980 noiseBrems=False)
981
982 # Add the posttracking modules needed for the cDST analysis
983 add_posttracking_reconstruction(path,
984 components=components,
985 for_cdst_analysis=True,
986 add_eventt0_combiner_for_cdst=add_eventt0_combiner,
987 legacy_ecl_charged_pid=legacy_ecl_charged_pid)
988
989
990def prepare_user_cdst_analysis(path, components=None, mc=False):
991 """
992 Adds to a (analysis) path all the modules needed to analyse a cDST file in the raw+tracking format
993 for collisions/cosmics data or in the digits+tracking format for MC data.
994 Differently from prepare_cdst_analysis(), this function add the EventT0Combiner module to the path,
995 which makes this function suitable for all the users and not only for the calibration expertes.
996 Note that the EventT0Combiner module is necessary for applying the proper EventT0 correction to
997 our data.
998
999 :param path: The path to add the modules to.
1000 :param components: The components to use or None to use all standard components.
1001 :param mc: Are we running over MC data or not? If so, do not run the unpackers.
1002 """
1003 prepare_cdst_analysis(path=path, components=components, mc=mc, add_eventt0_combiner=True)