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