62def runTracking(tracker, candidates, hits, resolution, fit_vertex=True):
63 """Find or fit tracks and, if the event qualifies, fit their vertex.
64
65 With ``candidates`` None, the tracker's own pattern recognition finds
66 the tracks. Otherwise ``candidates`` are dicts with ``indices`` into
67 ``hits`` (sorted by hit z in place), and each is fitted on its own.
68
69 Returns the fitted tracks (``SHiP::RecoTrack``, in the order of the
70 tracker's track indices), the fitted vertices (``SHiP::RecoVertex``) and,
71 per fitted track, the indices into ``hits`` that were used in the fit.
72 """
73 tracker.ClearEvent()
74 measurements = [tracker.AddMeasurement(h.detector_id, h.drift, resolution) for h in hits]
75 hit_of_measurement = {m: i for i, m in enumerate(measurements) if m >= 0}
76
77 if candidates is None:
78 tracker.FindTracks()
79 candidates = []
80 for cand in candidates:
81 cand["indices"].sort(key=lambda i: hits[i].z)
82
83
84 used = []
85 seen_layers = set()
86 for idx in cand["indices"]:
87 if measurements[idx] < 0:
88 logger.warning("No ACTS surface for straw %d", hits[idx].detector_id)
89 continue
90 if hits[idx].layer_key not in seen_layers:
91 used.append(idx)
92 seen_layers.add(hits[idx].layer_key)
93
94 if len(used) < MIN_HITS_PER_TRACK:
95 logger.debug("Skipping track with too few hits: %d", len(used))
96 continue
97
98 tracker.FitCandidate(ROOT.std.vector("int")([measurements[i] for i in used]))
99
100 tracks = [tracker.GetTrack(i) for i in range(tracker.GetNTracks())]
101 track_hit_indices = [
102 [hit_of_measurement[m] for m in tracker.GetTrackMeasurements(i)] for i in range(tracker.GetNTracks())
103 ]
104
105 good = [
106 i
107 for i, track in enumerate(tracks)
108 if track.nMeasurements() >= MIN_HITS_PER_TRACK
109 and track.nDoF() > 0
110 and track.chi2() / track.nDoF() <= MAX_CHI2_NDF
111 ]
112
113
114 vertices = []
115 if fit_vertex:
116 for i, j in combinations(good, 2):
117 if tracks[i].charge() * tracks[j].charge() >= 0:
118 continue
119 vertex = ROOT.SHiP.RecoVertex()
120 if tracker.FitVertex(ROOT.std.vector("int")([i, j]), vertex):
121 vertices.append(vertex)
122 else:
123 logger.debug("Vertex fit failed for tracks %d and %d", i, j)
124
125 return tracks, vertices, track_hit_indices
126
127