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