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