Belle II Software  release-05-01-25
modules.py
1 import basf2
2 
3 import ROOT
4 
5 import logging
6 
7 from ROOT import Belle2
8 
9 
10 class CDCHitUniqueAssumer(basf2.Module):
11 
12  """
13  A small helper module to look for double assigned hits.
14  Prints a summary after execution
15  """
16 
17  def initialize(self):
18  """Initialization signal at the start of the event processing"""
19 
21 
23 
25 
26  def event(self):
27  """Event method of the module"""
28  track_store_vector = Belle2.PyStoreObj('CDCTrackVector')
29 
30  if track_store_vector:
31  # Wrapper around std::vector like
32  wrapped_vector = track_store_vector.obj()
33  tracks = wrapped_vector.get()
34 
35  for track in tracks:
36  # Unset all taken flags
37  for recoHit3D in track:
38  if not recoHit3D.getWireHit().getAutomatonCell().hasTakenFlag():
40 
41  for i, track in enumerate(tracks):
42  # Now check that we only have every wire hit once
43  for j, innerTrack in enumerate(tracks):
44  if i == j:
45  continue
46  for recoHit3D in innerTrack:
47  recoHit3D.getWireHit().getAutomatonCell().setAssignedFlag()
48 
49  for recoHit3D in track:
50  self.number_of_total_hits += 1
51  if recoHit3D.getWireHit().getAutomatonCell().hasAssignedFlag():
52  self.number_of_doubled_hits += 1
53  recoHit3D.getWireHit().getAutomatonCell().setAssignedFlag()
54 
55  for innerTrack in tracks:
56  for recoHit3D in innerTrack:
57  recoHit3D.getWireHit().getAutomatonCell().unsetAssignedFlag()
58 
59  def terminate(self):
60  """Termination signal at the end of the event processing"""
61  print("Number of doubled hits:", self.number_of_doubled_hits)
62  print("Number of hits with wrong taken flag:", self.number_of_hits_with_wrong_flags)
63  print("Number of total hits:", self.number_of_total_hits)
64 
65 
66 class HitCleaner(basf2.Module):
67  """A small hit cleaner module to set the track information according to mc information.
68  This is surely not for later usage but for testing the genfitter module
69  """
70 
71  def __init__(self):
72  """Constructor"""
73  super(HitCleaner, self).__init__()
74 
75 
77 
79 
80  self.number_of_hits = 0
81 
82  def initialize(self):
83  """Initialization signal at the start of the event processing"""
84 
86 
87  self.mc_matcher_lookup = Belle2.TrackMatchLookUp("MCTrackCands", "TrackCands")
88 
89  def event(self):
90  """Event method of the module"""
91  tracks = Belle2.PyStoreArray("TrackCands")
92  cdc_hits = Belle2.PyStoreArray("CDCHits")
93  mc_particles = Belle2.PyStoreArray("MCParticles")
94 
95  cdc_hit_lookup = self.cdc_hit_look_up
96  cdc_hit_lookup.fill()
97 
98  mc_matcher_lookup = self.mc_matcher_lookup
99 
100  self.number_of_tracks += tracks.getEntries()
101 
102  for track in tracks:
103  # Store all Hit IDs and reset the track
104  hitIDs = track.getHitIDs(Belle2.Const.CDC)
105  hits = [cdc_hits[i] for i in hitIDs]
106  good_hits = []
107 
108  relation_track_particle = [0] * mc_particles.getEntries()
109 
110  # Now only add those hits which do belong to the track (with MC Info)
111  for i in hitIDs:
112  current_mc_track = cdc_hit_lookup.getMCTrackId(cdc_hits[i])
113  if 0 <= current_mc_track:
114  relation_track_particle[current_mc_track] += 1
115 
116  should_belong_to_track = np.argmax(relation_track_particle)
117 
118  deleted_hits = sum(relation_track_particle) - relation_track_particle[should_belong_to_track]
119 
120  plane_IDs_of_good_hits = []
121  for i, hitID in enumerate(hitIDs):
122  current_mc_track = cdc_hit_lookup.getMCTrackId(cdc_hits[hitID])
123  if current_mc_track == should_belong_to_track:
124  good_hits.append(hitID)
125 
126  self.number_of_deleted_hits += deleted_hits
127  self.number_of_hits += len(hitIDs)
128 
129  # Set the position and momentum
130  mc_track = mc_matcher_lookup.getMatchedMCRecoTrack(track)
131 
132  if mc_track:
133  mc_trajectory = Belle2.TrackFindingCDC.CDCTrajectory3D(Belle2.TrackFindingCDC.Vector3D(mc_track.getPosSeed()),
134  Belle2.TrackFindingCDC.Vector3D(mc_track.getMomSeed()),
135  mc_track.getChargeSeed())
136  startingPosition = Belle2.TrackFindingCDC.Vector3D(track.getPosSeed().X(), track.getPosSeed().Y(), 0)
137  sStartingPosition = mc_trajectory.calcArcLength2D(startingPosition)
138  zStartingPosition = mc_trajectory.getTrajectorySZ().mapSToZ(sStartingPosition)
139  mc_trajectory.setLocalOrigin(Belle2.TrackFindingCDC.Vector3D(startingPosition.xy(), zStartingPosition))
140 
141  pos = ROOT.TVector3(mc_trajectory.getSupport().x(), mc_trajectory.getSupport().y(), mc_trajectory.getSupport().z())
142  mom = ROOT.TVector3(
143  mc_trajectory.getMom3DAtSupport().x(),
144  mc_trajectory.getMom3DAtSupport().y(),
145  mc_trajectory.getMom3DAtSupport().z())
146 
147  # track.setPosMomSeedAndPdgCode(pos, mom , int(mc_track.getChargeSeed() * 211))
148  track.setPdgCode(int(track.getChargeSeed() * 211))
149 
150  else:
151  # track.reset()
152  track.setPdgCode(int(track.getChargeSeed() * 211))
153 
154  def terminate(self):
155  """Termination signal at the end of the event processing"""
156  print(("Number of tracks in total: %d" % self.number_of_tracks))
157  print(("Number of hits in total: %d" % self.number_of_hits))
158  print(("Number of deleted hits: %d" % self.number_of_deleted_hits))
159 
160  print(("Number of deleted hits per track: %f" % (1.0 * self.number_of_deleted_hits / self.number_of_tracks)))
161  print(("Ratio of deleted hits: %f" % (100.0 * self.number_of_deleted_hits / self.number_of_hits)))
trackfindingcdc.modules.HitCleaner.initialize
def initialize(self)
Definition: modules.py:82
trackfindingcdc.modules.CDCHitUniqueAssumer.event
def event(self)
Definition: modules.py:26
trackfindingcdc.modules.CDCHitUniqueAssumer.number_of_total_hits
number_of_total_hits
counter for total CDC hits
Definition: modules.py:22
trackfindingcdc.modules.HitCleaner.terminate
def terminate(self)
Definition: modules.py:154
Belle2::PyStoreObj
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:69
Belle2::TrackMatchLookUp
Class to provide convenient methods to look up matching information between pattern recognition and M...
Definition: TrackMatchLookUp.h:43
trackfindingcdc.modules.HitCleaner
Definition: modules.py:66
trackfindingcdc.modules.CDCHitUniqueAssumer.initialize
def initialize(self)
Definition: modules.py:17
trackfindingcdc.modules.HitCleaner.mc_matcher_lookup
mc_matcher_lookup
function to match track candidatess to MC track candidates
Definition: modules.py:87
trackfindingcdc.modules.HitCleaner.number_of_hits
number_of_hits
count the hits
Definition: modules.py:80
trackfindingcdc.modules.CDCHitUniqueAssumer.terminate
def terminate(self)
Definition: modules.py:59
trackfindingcdc.modules.HitCleaner.number_of_tracks
number_of_tracks
count the tracks
Definition: modules.py:76
trackfindingcdc.modules.HitCleaner.cdc_hit_look_up
cdc_hit_look_up
function to look up CDC MC hits
Definition: modules.py:85
Belle2::TrackFindingCDC::CDCMCHitLookUp
Interface class to the Monte Carlo information for individual hits.
Definition: CDCMCHitLookUp.h:41
Belle2::TrackFindingCDC::Vector3D
A three dimensional vector.
Definition: Vector3D.h:34
trackfindingcdc.modules.CDCHitUniqueAssumer.number_of_hits_with_wrong_flags
number_of_hits_with_wrong_flags
counter for CDC hits with wrong flags
Definition: modules.py:24
Belle2::PyStoreArray
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:58
trackfindingcdc.modules.CDCHitUniqueAssumer.number_of_doubled_hits
number_of_doubled_hits
counter for doubled CDC hits
Definition: modules.py:20
trackfindingcdc.modules.HitCleaner.__init__
def __init__(self)
Definition: modules.py:71
trackfindingcdc.modules.HitCleaner.event
def event(self)
Definition: modules.py:89
Belle2::TrackFindingCDC::CDCTrajectory3D
Particle full three dimensional trajectory.
Definition: CDCTrajectory3D.h:47
trackfindingcdc.modules.CDCHitUniqueAssumer
Definition: modules.py:10
trackfindingcdc.modules.HitCleaner.number_of_deleted_hits
number_of_deleted_hits
count the deleted hits
Definition: modules.py:78