Belle II Software  release-06-02-00
eventwise_module.py
1 
8 
9 import ROOT
10 from ROOT import Belle2
11 
12 import numpy as np
13 
14 import tracking.validation.utilities as utilities
15 
16 import tracking.harvest.refiners as refiners
17 import tracking.harvest.harvesting as harvesting
18 ROOT.gSystem.Load("libtracking")
19 
20 
21 class EventwiseTrackingValidationModule(harvesting.HarvestingModule):
22  """Module to perform event-by-event tracking validation."""
23 
24  """ Expert level behavior:
25  expert_level <= default_expert_level: all figures and plots from this module except tree entries
26  expert_level > default_expert_level: everything including tree entries
27  """
28 
29 
30  default_expert_level = 10
31 
32  def __init__(self,
33  name,
34  contact,
35  output_file_name=None,
36  reco_tracks_name='RecoTracks',
37  mc_reco_tracks_name='MCRecoTracks',
38  expert_level=None):
39  """Constructor"""
40 
41  output_file_name = output_file_name or name + 'TrackingValidation.root'
42 
43  super().__init__(foreach="EventMetaData", # Dummy for on element per event
44  name=name,
45  output_file_name=output_file_name,
46  contact=contact,
47  expert_level=expert_level)
48 
49 
50  self.reco_tracks_namereco_tracks_name = reco_tracks_name
51 
52  self.mc_reco_tracks_namemc_reco_tracks_name = mc_reco_tracks_name
53 
54  self.cdc_hits_namecdc_hits_name = "CDCHits"
55 
56  def initialize(self):
57  """Initialization signal at the start of the event processing"""
58  super().initialize()
59 
60  self.track_match_look_uptrack_match_look_up = Belle2.TrackMatchLookUp(self.mc_reco_tracks_namemc_reco_tracks_name,
61  self.reco_tracks_namereco_tracks_name)
62 
63  def pick(self, event_meta_data=None):
64  """Always pick"""
65  return True
66 
67  def peel(self, event_meta_data=None):
68  """Peel information from the event"""
69  # Note event_meta_data is just used as a dummy.
70 
71  track_match_look_up = self.track_match_look_uptrack_match_look_up
72  reco_tracks = Belle2.PyStoreArray(self.reco_tracks_namereco_tracks_name)
73  mc_reco_tracks = Belle2.PyStoreArray(self.mc_reco_tracks_namemc_reco_tracks_name)
74  cdc_hits = Belle2.PyStoreArray(self.cdc_hits_namecdc_hits_name)
75  mc_particles = Belle2.PyStoreArray("MCParticles")
76 
77  # General object count in the event.
78  if mc_particles:
79  n_mc_particles = mc_particles.getEntries()
80  else:
81  n_mc_particles = -1
82 
83  if mc_reco_tracks:
84  n_mc_reco_tracks = mc_reco_tracks.getEntries()
85  else:
86  n_mc_reco_tracks = -1
87 
88  n_reco_tracks = reco_tracks.getEntries()
89 
90  # Aggregate information about Monte Carlo tracks
91  all_mc_tracks_det_hit_ids = set()
92  n_matched_mc_reco_tracks = 0
93  n_merged_mc_reco_tracks = 0
94  n_missing_mc_reco_tracks = 0
95  for mc_reco_track in mc_reco_tracks:
96  mc_reco_track_det_hit_ids = utilities.get_det_hit_ids(mc_reco_track)
97  all_mc_tracks_det_hit_ids.update(mc_reco_track_det_hit_ids)
98 
99  is_matched = track_match_look_up.isMatchedMCRecoTrack(mc_reco_track)
100  is_merged = track_match_look_up.isMergedMCRecoTrack(mc_reco_track)
101  is_missing = track_match_look_up.isMissingMCRecoTrack(mc_reco_track)
102 
103  if is_matched:
104  n_matched_mc_reco_tracks += 1
105  elif is_merged:
106  n_merged_mc_reco_tracks += 1
107  elif is_missing:
108  n_missing_mc_reco_tracks += 1
109 
110  # Aggregate information about pattern recognition tracks
111  n_matched_reco_tracks = 0
112  n_clone_reco_tracks = 0
113  n_background_reco_tracks = 0
114  n_ghost_reco_tracks = 0
115 
116  all_tracks_det_hit_ids = set()
117  n_matched_hits = 0
118  for reco_track in reco_tracks:
119  is_matched = track_match_look_up.isMatchedPRRecoTrack(reco_track)
120  is_clone = track_match_look_up.isClonePRRecoTrack(reco_track)
121  is_background = track_match_look_up.isBackgroundPRRecoTrack(reco_track)
122  is_ghost = track_match_look_up.isGhostPRRecoTrack(reco_track)
123 
124  if is_matched:
125  n_matched_reco_tracks += 1
126  elif is_clone:
127  n_clone_reco_tracks += 1
128  elif is_background:
129  n_background_reco_tracks += 1
130  elif is_ghost:
131  n_ghost_reco_tracks += 1
132 
133  reco_track_det_hit_ids = utilities.get_det_hit_ids(reco_track)
134 
135  all_tracks_det_hit_ids.update(reco_track_det_hit_ids)
136  if is_matched or is_clone:
137  mc_reco_track = self.track_match_look_uptrack_match_look_up.getRelatedMCRecoTrack(reco_track)
138 
139  mc_reco_track_det_hit_ids = utilities.get_det_hit_ids(mc_reco_track)
140  n_matched_hits += len(reco_track_det_hit_ids & mc_reco_track_det_hit_ids)
141 
142  return dict(
143  n_mc_particles=n_mc_particles,
144  n_mc_reco_tracks=n_mc_reco_tracks,
145  n_reco_tracks=n_reco_tracks,
146 
147  n_matched_mc_reco_tracks=n_matched_mc_reco_tracks,
148  n_merged_mc_reco_tracks=n_merged_mc_reco_tracks,
149  n_missing_mc_reco_tracks=n_missing_mc_reco_tracks,
150 
151  n_matched_reco_tracks=n_matched_reco_tracks,
152  n_clone_reco_tracks=n_clone_reco_tracks,
153  n_background_reco_tracks=n_background_reco_tracks,
154  n_ghost_reco_tracks=n_ghost_reco_tracks,
155 
156  n_cdc_hits=cdc_hits.getEntries(),
157  n_all_mc_track_hits=len(all_mc_tracks_det_hit_ids),
158  n_all_track_hits=len(all_tracks_det_hit_ids),
159  n_found_hits=len(all_mc_tracks_det_hit_ids & all_tracks_det_hit_ids),
160  n_matched_hits=n_matched_hits
161  )
162 
163  # Refiners to be executed on terminate #
164  # ==================================== #
165 
166 
167  save_tree = refiners.save_tree(
168  # using cond to suppress false doxygen warnings
169 
170  folder_name="event_tree",
171  name="event_tree",
172  above_expert_level=default_expert_level
173 
174  )
175 
176 
177  save_clone_rate = refiners.save_fom(
178  # using cond to suppress false doxygen warnings
179 
180  name="{module.id}_hit_figures_of_merit",
181  title="Hit sums in {module.title}",
182  description="", # to be given
183  select=[
184  "n_cdc_hits",
185  "n_all_mc_track_hits",
186  "n_all_track_hits",
187  "n_found_hits",
188  "n_matched_hits",
189  ],
190 
191  aggregation=np.sum,
192  key="{part_name}",
193 
194  )
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:56
Class to provide convenient methods to look up matching information between pattern recognition and M...
track_match_look_up
Reference to the track-match object that examines relation information from MCMatcherTracksModule.
def __init__(self, name, contact, output_file_name=None, reco_tracks_name='RecoTracks', mc_reco_tracks_name='MCRecoTracks', expert_level=None)