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