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