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