delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
PerigeeMatch.cpp
Go to the documentation of this file.
1// PerigeeMatch.cpp — implementation.
2//
3// Convert sDST tracks back to DELPHI cm-basis and apply the legacy
4// converter's cost formula + tolerance. Implementation note: we
5// iterate the whole sDST collection per call; for ~50 tracks/event
6// this is fast enough that a build-once index isn't needed yet.
7
9
10#include <edm4hep/TrackState.h>
11
12#include <cmath>
13#include <limits>
14
16
17namespace {
18
19constexpr double kMm2Cm = 0.1; // mm -> cm
20
21// Pull the AtIP TrackState (by value) from a Track. EDM4hep returns
22// TrackStates by value via the range view, so we can't safely keep a
23// pointer or reference — we copy. found=false if the Track has no
24// TrackStates at all.
25struct AtIPLookup { edm4hep::TrackState state; bool found; };
26AtIPLookup findAtIPState(const edm4hep::Track& trk) {
27 for (auto ts : trk.getTrackStates()) {
28 if (ts.location == edm4hep::TrackState::AtIP) return {ts, true};
29 }
30 // Fallback: first entry. Tracking always emits AtIP first, so this
31 // catches the case where location wasn't set explicitly.
32 if (trk.trackStates_size() > 0) return {trk.getTrackStates()[0], true};
33 return {{}, false};
34}
35
36} // namespace
37
39 const edm4hep::TrackCollection& sdst_tracks,
40 double fdst_D0_mm,
41 double fdst_Z0_mm,
42 double fdst_omega_per_mm,
43 int fdst_charge_signed,
44 double tolerance_cm2)
45{
46 // Convert fDST candidate to DELPHI cm-basis.
47 const double fdst_d0_cm = fdst_D0_mm * kMm2Cm;
48 const double fdst_z0_cm = fdst_Z0_mm * kMm2Cm;
49 const double fdst_invR_pc = fdst_omega_per_mm / kMm2Cm; // 1/mm -> 1/cm
50
51 int best_idx = -1;
52 double best_cost = std::numeric_limits<double>::infinity();
53
54 const int n = static_cast<int>(sdst_tracks.size());
55 for (int i = 0; i < n; ++i) {
56 const auto t = sdst_tracks[i];
57 const auto lk = findAtIPState(t);
58 if (!lk.found) continue;
59 const auto& ts = lk.state;
60
61 // Charge gate (intentionally weak): both sides are converted by
62 // Helix::fromPerigee, so omega carries the sign of the charge on
63 // each. Rather than gate on it, we trust the geometric uniqueness of the
64 // perigee within tolerance — same physical track gives identical
65 // (d0, z0, invR) on both DST levels to float precision. The
66 // legacy converter took the same approach (gate by PA.MAIN
67 // charge code, not by omega sign). With ~100 μm tolerance the
68 // collision rate among same-event tracks is negligible.
69 (void)fdst_charge_signed;
70
71 const double sdst_d0_cm = static_cast<double>(ts.D0) * kMm2Cm;
72 const double sdst_z0_cm = static_cast<double>(ts.Z0) * kMm2Cm;
73 const double sdst_invR_pc = static_cast<double>(ts.omega) / kMm2Cm;
74
75 const double dd0 = fdst_d0_cm - sdst_d0_cm;
76 const double dz0 = fdst_z0_cm - sdst_z0_cm;
77 const double diR = (fdst_invR_pc - sdst_invR_pc) * 1000.0;
78 const double cost = dd0*dd0 + dz0*dz0 + diR*diR;
79
80 if (cost < best_cost) {
81 best_cost = cost;
82 best_idx = i;
83 }
84 }
85
86 return (best_cost < tolerance_cm2) ? best_idx : -1;
87}
88
89} // namespace delphi_edm4hep::perigee_match
int findMatch(const edm4hep::TrackCollection &sdst_tracks, double fdst_D0_mm, double fdst_Z0_mm, double fdst_omega_per_mm, int fdst_charge_signed, double tolerance_cm2=1.0e-4)