Belle II Software development
tracked_event_generation.py
1#!/usr/bin/env python3
2
3
10
11import basf2
12import tracking
13
14from tracking.metamodules import IfMCParticlesPresentModule
15from tracking.run import utilities
16from tracking.run.event_generation import ReadOrGenerateEventsRun
17
18import logging
19
20
21def get_logger():
22 return logging.getLogger(__name__)
23
24
26 """ Apply tracking to presimulated events or events generated on the fly """
27
28
29 description = "Apply tracking to presimulated events or events generated on the fly."
30
31
32 finder_module = None
33
34
35 tracking_coverage = {
36 'UsePXDHits': True,
37 'UseSVDHits': True,
38 'UseCDCHits': True,
39 'UseOnlyAxialCDCHits': False,
40 'UseOnlyBeforeTOP': True,
41 'UseReassignedHits': True,
42 'UseNLoops': 1,
43 'WhichParticles': [],
44 }
45
46
47 fit_tracks = False
48
49
50 mc_tracking = True
51
52 def create_argument_parser(self, **kwds):
53 """Convert command-line arguments to basf2 argument list"""
54 argument_parser = super().create_argument_parser(**kwds)
55
56 tracking_argument_group = argument_parser.add_argument_group("Tracking setup arguments")
57
58 tracking_argument_group.add_argument(
59 '-f',
60 '--finder',
61 choices=utilities.NonstrictChoices(finder_modules_by_short_name.keys()),
62 default=self.finder_module,
63 dest='finder_module',
64 help='Name of the finder module to be evaluated.',)
65
66 tracking_argument_group.add_argument(
67 '--fit',
68 action="store_true",
69 default=self.fit_tracks,
70 dest='fit_tracks',
71 help='Apply the fitting to the found tracks'
72 )
73
74 return argument_parser
75
76 def create_path(self):
77 """Sets up a path that plays back pregenerated events or generates events
78 based on the properties in the base class."""
79 path = super().create_path()
80
81 # setting up fitting is only necessary when testing
82 # track finding comonenst ex-situ
83 if self.fit_tracks:
84 if 'SetupGenfitExtrapolation' not in path:
85 # Prepare Genfit extrapolation
86 path.add_module('SetupGenfitExtrapolation')
87
88 if self.finder_module is not None:
89 # Setup track finder
90 utilities.extend_path(path,
91 self.finder_module,
92 finder_modules_by_short_name,
93 allow_function_import=True)
94
95 # determine which sub-detector hits will be used
96 tracking_coverage = dict(self.tracking_coverage)
97
98 matching_coverage = {key: value for key, value in tracking_coverage.items()
99 if key in ('UsePXDHits', 'UseSVDHits', 'UseCDCHits', 'MinimalEfficiency', 'MinimalPurity')}
100 # Removing minimal efficiency and purity as they are only parameters of the matching
101 if "MinimalEfficiency" in tracking_coverage:
102 tracking_coverage.pop("MinimalEfficiency")
103 if "MinimalPurity" in tracking_coverage:
104 tracking_coverage.pop("MinimalPurity")
105
106 # Include the mc tracks if the monte carlo data is presentx
107 if self.mc_tracking and 'MCRecoTracksMatcher' not in path:
108 # Reference Monte Carlo tracks
109 track_finder_mc_truth_module = basf2.register_module('TrackFinderMCTruthRecoTracks')
110
111 # Track matcher
112 mc_track_matcher_module = basf2.register_module('MCRecoTracksMatcher')
113
114 path.add_module(IfMCParticlesPresentModule(track_finder_mc_truth_module))
115 path.add_module(IfMCParticlesPresentModule(mc_track_matcher_module))
116
117 # this ensures that the parameters are set in both cases (if the modules have been added or are already in the path)
118 # only check for containment to also cope with the "IfMCParticlesPresentModule" cases correctly
119 for module in path.modules():
120 if 'MCRecoTracksMatcher' in module.name():
121 module.param({
122 'mcRecoTracksStoreArrayName': 'MCRecoTracks',
123 'MinimalPurity': 0.66,
124 'prRecoTracksStoreArrayName': "RecoTracks",
125 **matching_coverage
126 })
127 if 'TrackFinderMCTruthRecoTracks' in module.name():
128 module.param({
129 'RecoTracksStoreArrayName': 'MCRecoTracks',
130 **tracking_coverage
131 })
132
133 if self.fit_tracks:
134 # Fit tracks
135 gen_fitter_module = basf2.register_module('DAFRecoFitter')
136 gen_fitter_module.param({'pdgCodesToUseForFitting': [211]})
137 path.add_module(gen_fitter_module)
138 trackbuilder = basf2.register_module('TrackCreator', pdgCodes=[211])
139 path.add_module(trackbuilder)
140
141 return path
142
143
144def add_standard_finder(path):
145 """adds the standard track finding to the path"""
146
147 import tracking
148 components = None
149 for module in path.modules():
150 if module.type() == "Geometry":
151 components = utilities.get_module_param(module, "components")
152 if not components:
153 components = None
154
155 if 'SetupGenfitExtrapolation' not in path:
156 path.add_module('SetupGenfitExtrapolation', energyLossBrems=False, noiseBrems=False)
157
158 tracking.add_track_finding(path, components=components)
159
160
161def add_cosmics_finder(path):
162 import tracking
163 components = None
164 for module in path.modules():
165 if module.type() == "Geometry":
166 components = utilities.get_module_param(module, "components")
167 if not components:
168 components = None
169
170 if 'SetupGenfitExtrapolation' not in path:
171 path.add_module('SetupGenfitExtrapolation', energyLossBrems=False, noiseBrems=False)
172
173 tracking.add_cr_tracking_reconstruction(path, components=components)
174
175
176def add_standard_reconstruction(path):
177 import reconstruction
178 components = None
179 for module in path.modules():
180 if module.type() == "Geometry":
181 components = utilities.get_module_param(module, "components")
182 if not components:
183 components = None
184 reconstruction.add_reconstruction(path, components=components)
185
186
187def add_cosmics_reconstruction(path):
188 import reconstruction
189 components = None
190 for module in path.modules():
191 if module.type() == "Geometry":
192 components = utilities.get_module_param(module, "components")
193 if not components:
194 components = None
195 reconstruction.add_cosmics_reconstruction(path, components=components)
196
197
198finder_modules_by_short_name = {
199 'MC': 'TrackFinderMCTruthRecoTracks',
200 'Reconstruction': add_standard_reconstruction,
201 'CosmicsReconstruction': add_cosmics_reconstruction,
202 'TrackFinder': add_standard_finder,
203 'CosmicsTrackFinder': add_cosmics_finder,
204 'TrackFinderVXD': tracking.add_vxd_track_finding_vxdtf2,
205 'TFCDC': lambda path: tracking.add_cdc_track_finding(path, with_ca=True),
206 'TFCDC_Cosmics': lambda path: tracking.add_cdc_cr_track_finding(path),
207 'TFCDC_Global': tracking.add_cdc_track_finding,
208 'TFCDC_Ca': lambda path: (path.add_module('TFCDC_WireHitPreparer',
209 flightTimeEstimation="outwards"),
210 path.add_module('TFCDC_ClusterPreparer',
211 SuperClusterDegree=3,
212 SuperClusterExpandOverApogeeGap=True),
213 path.add_module('TFCDC_SegmentFinderFacetAutomaton'),
214 path.add_module("TFCDC_TrackFinderSegmentPairAutomaton"),
215 path.add_module("TFCDC_TrackCreatorSingleSegments",
216 MinimalHitsBySuperLayerId={0: 15}),
217 path.add_module('TFCDC_TrackExporter')),
218 'TFCDC_Axial': lambda path: (path.add_module('TFCDC_WireHitPreparer',
219 flightTimeEstimation="outwards"),
220 path.add_module('TFCDC_ClusterPreparer'),
221 path.add_module('TFCDC_AxialTrackFinderLegendre'),
222 path.add_module('TFCDC_TrackExporter')),
223 'TFCDC_Segments': lambda path: (path.add_module('TFCDC_WireHitPreparer',
224 flightTimeEstimation="outwards"),
225 path.add_module('TFCDC_ClusterPreparer'),
226 path.add_module('TFCDC_SegmentFinderFacetAutomaton'),
227 path.add_module('TFCDC_TrackCreatorSingleSegments',
228 MinimalHitsBySuperLayerId={sl_id: 0 for sl_id in range(9)}),
229 path.add_module('TFCDC_TrackExporter')),
230 'TFCDC_MCSegments': lambda path: (path.add_module('TFCDC_WireHitPreparer',
231 flightTimeEstimation="outwards"),
232 path.add_module('TFCDC_SegmentCreatorMCTruth'),
233 path.add_module('TFCDC_SegmentLinker',
234 segments="CDCSegment2DVector",
235 filter="truth"),
236 path.add_module('TFCDC_TrackCreatorSingleSegments',
237 MinimalHitsBySuperLayerId={sl_id: 0 for sl_id in range(9)}),
238 path.add_module('TFCDC_TrackExporter')),
239 'FirstLoop': lambda path: path.add_module('TFCDC_WireHitPreparer', UseNLoops=1.0),
240}
241
242
244 """Generate, simulate and reconstruct events"""
245
246 generator_module = 'EvtGenInput'
247
248 def finder_module(self, path):
249 """Add track reconstruction to the basf2 path"""
250 tracking.add_tracking_reconstruction(path, components=self.components)
None components
By default, do specific components.
None finder_module
Name of the finder module to be used - can be everything that is accepted by tracking....
bool mc_tracking
By default, do MC track finding and track matching.
dict tracking_coverage
States which detectors the finder module covers like as a dictionary like.
bool fit_tracks
By default, do not add the track fitting to the execution.
def add_reconstruction(path, components=None, pruneTracks=True, add_trigger_calculation=True, skipGeometryAdding=False, trackFitHypotheses=None, addClusterExpertModules=True, use_second_cdc_hits=False, add_muid_hits=False, reconstruct_cdst=None, event_abort=default_event_abort, use_random_numbers_for_hlt_prescale=True, pxd_filtering_offline=False, create_intercepts_for_pxd_ckf=False, append_full_grid_cdc_eventt0=True, legacy_ecl_charged_pid=False, emulate_HLT=False, skip_full_grid_cdc_eventt0_if_svd_time_present=True)
def add_cosmics_reconstruction(path, components=None, pruneTracks=True, skipGeometryAdding=False, eventTimingExtraction=True, addClusterExpertModules=True, merge_tracks=True, use_second_cdc_hits=False, add_muid_hits=False, reconstruct_cdst=False, posttracking=True, legacy_ecl_charged_pid=False)
def add_track_finding(path, components=None, reco_tracks="RecoTracks", prune_temporary_tracks=True, use_second_cdc_hits=False, use_mc_truth=False, svd_ckf_mode="SVD_after", add_both_directions=True, svd_standalone_mode="VXDTF2", use_svd_to_cdc_ckf=True, use_ecl_to_cdc_ckf=False, add_cdcTrack_QI=True, add_vxdTrack_QI=False, pxd_filtering_offline=False, use_HLT_ROIs=False, create_intercepts_for_pxd_ckf=False)
Definition: __init__.py:443
def add_cr_tracking_reconstruction(path, components=None, prune_tracks=False, skip_geometry_adding=False, event_time_extraction=True, merge_tracks=True, use_second_cdc_hits=False)
Definition: __init__.py:367