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