FairShip
Loading...
Searching...
No Matches
test_straw_reco.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2# SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP Collaboration
3
4"""Hit bookkeeping of the ACTS straw reconstruction, with a stand-in tracker."""
5
6from recoConstants import MIN_HITS_PER_TRACK
7from strawReco import StrawHit, runTracking
8
9UNKNOWN_STRAW = -1
10
11
12def make_hit(detector_id, station, view, layer, z, drift=0.5, track_id=0):
13 return StrawHit(
14 detector_id=detector_id,
15 station=station,
16 view=view,
17 layer=layer,
18 track_id=track_id,
19 z=z,
20 drift=drift,
21 )
22
23
25 def __init__(self, charge=1):
26 self._charge = charge
27
28 def nMeasurements(self):
29 return MIN_HITS_PER_TRACK
30
31 def nDoF(self):
32 return 1.0
33
34 def chi2(self):
35 return 1.0
36
37 def charge(self):
38 return self._charge
39
40
42 """Records what runTracking hands to SHiP::ActsStrawTracker.
43
44 FitCandidate drops the last measurement of each candidate, as the real
45 tracker drops measurements that do not fit the track.
46 """
47
48 def __init__(self, found=(), charges=()):
49 self.measurements = []
50 self.candidates = []
51 self.tracks = []
52 self.vertices = []
53 self._found = found
54 self._charges = charges
55
56 def ClearEvent(self):
57 self.measurements.clear()
58 self.tracks.clear()
59
60 def AddMeasurement(self, detector_id, drift, resolution):
61 if detector_id == UNKNOWN_STRAW:
62 return -1
63 self.measurements.append((detector_id, drift, resolution))
64 return len(self.measurements) - 1
65
66 def FindTracks(self):
67 self.tracks = [list(t) for t in self._found]
68 return len(self.tracks)
69
70 def FitCandidate(self, measurements):
71 self.candidates.append(list(measurements))
72 self.tracks.append(list(measurements)[:-1])
73 return len(self.tracks) - 1
74
75 def GetNTracks(self):
76 return len(self.tracks)
77
78 def GetTrack(self, index):
79 return FakeTrack(self._charges[index] if self._charges else 1)
80
81 def GetTrackMeasurements(self, index):
82 return self.tracks[index]
83
84 def FitVertex(self, tracks, vertex):
85 self.vertices.append(list(tracks))
86 return True
87
88
90 # Hit 0 is an unknown straw, so measurement indices are hit indices - 1.
91 # The last hit shares a straw layer with hit 2 but sits at smaller z, so
92 # it is kept and hit 2 is not.
93 hits = [make_hit(UNKNOWN_STRAW, 1, 0, 0, z=0.0)]
94 hits += [make_hit(1000 + i, 1, i // 2, i % 2, z=10.0 * i) for i in range(MIN_HITS_PER_TRACK + 1)]
95 hits.append(make_hit(9999, 1, 0, 1, z=5.0))
96 tracker = FakeTracker()
97 tracks, vertices, hit_indices = runTracking(
98 tracker, [{"indices": list(reversed(range(len(hits))))}], hits, 0.012, fit_vertex=False
99 )
100
101 assert len(tracks) == 1
102 assert vertices == []
103 used = [m + 1 for m in tracker.candidates[0]]
104 assert used == sorted(used, key=lambda i: hits[i].z)
105 assert 0 not in used
106 assert len({hits[i].layer_key for i in used}) == len(used)
107 assert len(hits) - 1 in used and 2 not in used
108 # The hit indices of the track are those the tracker kept
109 assert hit_indices[0] == used[:-1]
110
111
113 hits = [make_hit(1000 + i, 1, i // 2, i % 2, z=float(i), drift=0.1 * i) for i in range(MIN_HITS_PER_TRACK)]
114 tracker = FakeTracker()
115 runTracking(tracker, [{"indices": list(range(len(hits)))}], hits, 0.012, fit_vertex=False)
116 assert [m[1] for m in tracker.measurements] == [h.drift for h in hits]
117
118
120 hits = [make_hit(1000 + i, 1, 0, i, z=float(i)) for i in range(2)]
121 tracker = FakeTracker()
122 tracks, _, hit_indices = runTracking(tracker, [{"indices": [0, 1]}], hits, 0.012, fit_vertex=False)
123 assert tracks == [] and hit_indices == [] and tracker.candidates == []
124
125
127 # With no candidates, the tracker finds the tracks itself
128 hits = [make_hit(UNKNOWN_STRAW, 1, 0, 0, z=0.0)] + [make_hit(1000 + i, 1, 0, 0, z=float(i)) for i in range(4)]
129 tracker = FakeTracker(found=[[0, 2], [1, 3]])
130 tracks, _, hit_indices = runTracking(tracker, None, hits, 0.012, fit_vertex=False)
131 assert len(tracks) == 2
132 assert hit_indices == [[1, 3], [2, 4]]
133
134
136 hits = [make_hit(1000 + i, 1, 0, 0, z=float(i)) for i in range(3)]
137 tracker = FakeTracker(found=[[0], [1], [2]], charges=[1, -1, 1])
138 _, vertices, _ = runTracking(tracker, None, hits, 0.012, fit_vertex=True)
139 assert tracker.vertices == [[0, 1], [1, 2]]
140 assert len(vertices) == 2
FitVertex(self, tracks, vertex)
AddMeasurement(self, detector_id, drift, resolution)
FitCandidate(self, measurements)
__init__(self, found=(), charges=())
make_hit(detector_id, station, view, layer, z, drift=0.5, track_id=0)
test_vertices_are_fitted_for_opposite_charge_pairs()
test_drift_distances_are_passed_unsigned()
test_candidate_hits_are_ordered_in_z_with_one_per_layer()