Belle II Software  release-08-01-10
vxdCdcMergerValidationGenerateSample.py
1 #!/usr/bin/env python3
2 
3 
10 
11 """
12 <header>
13  <contact>software-tracking@belle2.org</contact>
14  <output>VxdCdcValidationHarvested.root</output>
15  <input>EvtGenSimNoBkg.root</input>
16  <description>This module generates events for the V0 validation.</description>
17 </header>
18 """
19 
20 import basf2
21 from ROOT import Belle2
22 from tracking import add_tracking_reconstruction
23 from tracking.harvest.harvesting import HarvestingModule
24 from tracking.harvest import refiners
25 
26 ACTIVE = True
27 
28 
29 def run():
30  """
31  Generate and reconstruct Generic BBar
32  and evaluate the merger performance
33  """
34  basf2.set_random_seed(1337)
35  path = basf2.create_path()
36 
37  rootinput = basf2.register_module('RootInput')
38  rootinput.param('inputFileName', "../EvtGenSimNoBkg.root")
39  path.add_module(rootinput)
40  path.add_module('Gearbox')
41 
42  add_tracking_reconstruction(path, components=["CDC", "SVD"], pruneTracks=False, prune_temporary_tracks=False)
43 
44  # add additional matching for vxd and cdc only tracks here
45 
46  # for VXD only track
47  mctrackmatcher_vxd = basf2.register_module('MCRecoTracksMatcher')
48  mctrackmatcher_vxd.param('mcRecoTracksStoreArrayName', 'MCRecoTracks')
49  mctrackmatcher_vxd.param('prRecoTracksStoreArrayName', 'SVDRecoTracks')
50  mctrackmatcher_vxd.param('UseCDCHits', False)
51  path.add_module(mctrackmatcher_vxd)
52 
53  # for CDC only tracks
54  mctrackmatcher_cdc = basf2.register_module('MCRecoTracksMatcher')
55  mctrackmatcher_cdc.param('mcRecoTracksStoreArrayName', 'MCRecoTracks')
56  mctrackmatcher_cdc.param('prRecoTracksStoreArrayName', 'CDCRecoTracks')
57  mctrackmatcher_cdc.param('UsePXDHits', False)
58  mctrackmatcher_cdc.param('UseSVDHits', False)
59  path.add_module(mctrackmatcher_cdc)
60 
61  path.add_module(VxdCdcMergerHarvester())
62 
63  path.add_module('Progress')
64  basf2.process(path)
65  print(basf2.statistics)
66 
67 
69  """
70  Harvester module to extract useful information from VXD-CDC track merging.
71  """
72 
73  def __init__(self):
74  """
75  Init harvester
76  """
77  HarvestingModule.__init__(self, foreach="MCParticles", output_file_name="../VxdCdcValidationHarvested.root")
78 
79 
80  self.mc_track_matchermc_track_matcher = Belle2.TrackMatchLookUp("MCRecoTracks")
81 
82  self.mc_track_matcher_cdcmc_track_matcher_cdc = Belle2.TrackMatchLookUp("MCRecoTracks", "CDCRecoTracks")
83 
84  self.mc_track_matcher_vxdmc_track_matcher_vxd = Belle2.TrackMatchLookUp("MCRecoTracks", "SVDRecoTracks")
85 
86  def pick(self, mc_particle):
87  """Select the MCParticle if it is related to an MCRecoTrack"""
88  # mc_track = mc_particle.getRelated("MCRecoTracks")
89  # return mc_track is not None
90  mc_track = mc_particle.getRelatedFrom("MCRecoTracks")
91  if mc_track:
92  return True
93  else:
94  return False
95 
96  def peel(self, mc_particle):
97  """
98  Extract the information.
99  """
100 
101  # mc is a genfit::TrackCand
102 
103  mc_track = mc_particle.getRelatedFrom("MCRecoTracks")
104 
105  mc_nhits = mc_track.getNumberOfTotalHits()
106  mc_pt = mc_particle.getMomentum().Rho()
107  mc_theta = mc_particle.getMomentum().Theta()
108 
109  # reco_tracks = Belle2.PyStoreArray('RecoTracks')
110 
111  this_best_track_cdc = self.mc_track_matcher_cdcmc_track_matcher_cdc.getRelatedPRRecoTrack(mc_track)
112  this_best_track_vxd = self.mc_track_matcher_vxdmc_track_matcher_vxd.getRelatedPRRecoTrack(mc_track)
113 
114  # here we know, the above tracks should have been merged because they stem
115  # from the same MC Track. Have they?
116 
117  this_best_track_merged = self.mc_track_matchermc_track_matcher.getRelatedPRRecoTrack(mc_track)
118 
119  good_merge = False
120  vxd_hits = 0
121  cdc_hits = 0
122 
123  merged_hits = 0
124 
125  if this_best_track_merged:
126  if this_best_track_cdc:
127  cdc_hits = this_best_track_cdc.getNumberOfTotalHits()
128  if this_best_track_vxd:
129  vxd_hits = this_best_track_vxd.getNumberOfTotalHits()
130 
131  merged_hits = vxd_hits + cdc_hits
132  good_merge = merged_hits == this_best_track_merged.getNumberOfTotalHits()
133 
134  return {
135  "MC_PT": mc_pt,
136  "MC_THETA": mc_theta,
137  "MC_NHITS": mc_nhits,
138  "PR_NHITS": merged_hits,
139  "PR_CDC_NHITS": cdc_hits,
140  "PR_VXD_NHITS": vxd_hits,
141  "GOOD_MERGE": good_merge
142  }
143 
144 
146  save_tree = refiners.SaveTreeRefiner()
147 
148 
149 if __name__ == '__main__':
150  if ACTIVE:
151  run()
152  else:
153  print("This validation deactivated and thus basf2 is not executed.\n"
154  "If you want to run this validation, please set the 'ACTIVE' flag above to 'True'.\n"
155  "Exiting.")
Class to provide convenient methods to look up matching information between pattern recognition and M...