delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
Helix.cpp
Go to the documentation of this file.
1// Helix.cpp — implementation.
2//
3// The conversion kernels are transplanted verbatim from the former
4// BasisConversion.cpp (TE-basis 6×6 Jacobian) and PerigeeTrack.cpp
5// (perigee 5×5 Jacobian + weight-matrix inversion); the numbers are
6// unchanged, only the packaging differs.
7
9
10#include <edm4hep/Constants.h> // edm4hep::TrackParams
11
12#include <TMatrixD.h>
13#include <TMatrixDSym.h>
14
15#include <cmath>
16
17namespace delphi_edm4hep {
18
19namespace {
20
21constexpr double kCm2Mm = 10.0;
22
23// ---- perigee (raw DELPHI) -> helix 5×5 Jacobian -------------------------
24// raw : (d0, z0, theta, phi, invR)
25// helix : (D0, phi, omega, Z0, tanLambda)
26TMatrixD perigeeJacobian(double theta_dp) {
27 TMatrixD J(5, 5);
28 J.Zero();
29 const double s = std::sin(theta_dp);
30 const double inv_s2 = (s != 0.0) ? 1.0 / (s * s) : 0.0;
31 J(0, 0) = -kCm2Mm;
32 J(1, 3) = +1.0;
33 J(2, 4) = -1.0 / kCm2Mm; // omega = -invR/10, see fromPerigee
34 J(3, 1) = +kCm2Mm;
35 J(4, 2) = -inv_s2;
36 return J;
37}
38
39// ---- TE-basis -> helix 6×6 Jacobian -------------------------------------
40// TE : (c1, c2, c3, theta, phi, invP) [cartesian c1=x,c2=y,c3=z]
41// helix : (D0, phi, omega, Z0, tanLambda, time)
42TMatrixD teJacobian(double theta, double phi, double invP,
43 double B_tesla, bool invPt) {
44 TMatrixD J(6, 6);
45 J.Zero();
46 const double s = std::sin(theta);
47 const double c = std::cos(theta);
48 if (std::fabs(s) < 1e-9) return J;
49 const double inv_s2 = 1.0 / (s * s);
50 const double k = -kOmega * B_tesla; // omega = k * invP (/ sin theta)
51 J(0, 0) = -std::sin(phi) * kCm2Mm;
52 J(0, 1) = std::cos(phi) * kCm2Mm;
53 // The reference point is the measured point itself. Moving it along the
54 // track moves where the helix passes the reference: Z0 by -tanLambda and
55 // phi by +omega per unit transverse length.
56 const double omega = invPt ? k * invP : k * invP / s;
57 J(1, 0) = omega * std::cos(phi) * kCm2Mm;
58 J(1, 1) = omega * std::sin(phi) * kCm2Mm;
59 J(3, 0) = -(c / s) * std::cos(phi) * kCm2Mm;
60 J(3, 1) = -(c / s) * std::sin(phi) * kCm2Mm;
61 J(1, 4) = 1.0;
62 // omega = transverse curvature. The omega-row partials must match whichever
63 // momentum form fromTrackElement uses: omega = k*invP (no theta dep) when the
64 // bank word is 1/p_T, else omega = k*invP/sin(theta).
65 if (invPt) {
66 J(2, 3) = 0.0;
67 J(2, 5) = k;
68 } else {
69 J(2, 3) = -k * c * invP * inv_s2; // d/dtheta[k*invP/sin]
70 J(2, 5) = k / s; // d/d(invP)
71 }
72 J(3, 2) = kCm2Mm;
73 J(4, 3) = -inv_s2;
74 return J;
75}
76
77// Cylindrical -> Cartesian for the coordinate triple and its covariance.
78// Stored as (R, R*Phi, z), so the point's azimuth is Phi = (R*Phi)/R and
79// x = R cos Phi, y = R sin Phi, z = z.
80// R is fixed by the surface, so the free coordinates are (R*Phi, z) at TE-basis
81// indices 1 and 2. Their derivatives are
82// dx/d(R*Phi) = -sin Phi, dy/d(R*Phi) = cos Phi, dz/dz = 1,
83// the congruence applied to the covariance below; angles and 1/p pass through.
84TMatrixD cylToCartJacobian(double Phi) {
85 TMatrixD J(6, 6);
86 J.Zero();
87 J(0, 1) = -std::sin(Phi);
88 J(1, 1) = std::cos(Phi);
89 J(2, 2) = 1.0;
90 J(3, 3) = 1.0; // theta
91 J(4, 4) = 1.0; // phi
92 J(5, 5) = 1.0; // 1/p
93 return J;
94}
95
96TMatrixDSym covFromArray(const CovMatrix6& cov) {
97 TMatrixDSym out(6);
98 for (int i = 0; i < 6; ++i)
99 for (int j = 0; j <= i; ++j) {
100 const double v = static_cast<double>(cov[covOffset(i, j)]);
101 out(i, j) = v;
102 out(j, i) = v;
103 }
104 return out;
105}
106
107} // namespace
108
109Helix Helix::fromPerigee(float d0, float z0, float theta, float phi,
110 float invR, const std::array<float, 15>* W) {
111 Helix h;
112 h.p_.D0 = -d0 * static_cast<float>(kCm2Mm);
113 h.p_.phi = phi;
114 // DELPHI's 1/R is signed geometrically: positive is counter-clockwise seen
115 // from +z, i.e. opposite to the charge in DELPHI's field (DST content, MAIN:
116 // "with the sign opposite to the charge sign"). EDM4hep's omega carries the
117 // sign of the charge.
118 h.p_.omega = -invR / static_cast<float>(kCm2Mm);
119 h.p_.Z0 = z0 * static_cast<float>(kCm2Mm);
120 h.p_.time = 0.f;
121
122 const float s = std::sin(theta);
123 if (std::fabs(s) < 1e-6f) { // degenerate
124 h.p_.tanLambda = 0.f;
125 h.valid_ = false;
126 return h;
127 }
128 h.p_.tanLambda = std::cos(theta) / s;
129
130 if (!W) return h; // params only, no cov
131
132 // Build the 5×5 weight matrix (lower-tri, mirror), invert -> raw cov.
133 TMatrixDSym Wm(5);
134 {
135 int k = 0;
136 for (int i = 0; i < 5; ++i)
137 for (int j = 0; j <= i; ++j) { Wm(i, j) = (*W)[k]; Wm(j, i) = (*W)[k]; ++k; }
138 }
139 double det = 0.0;
140 TMatrixDSym C_dp(Wm);
141 C_dp.Invert(&det);
142 if (det == 0.0 || !std::isfinite(det)) {
143 h.hasCov_ = true; // zero cov, but a valid track
144 return h;
145 }
146 const TMatrixD J = perigeeJacobian(theta);
147 const TMatrixD JC (J, TMatrixD::kMult, C_dp);
148 const TMatrixD Jt (TMatrixD::kTransposed, J);
149 const TMatrixD Cf (JC, TMatrixD::kMult, Jt);
150 for (int i = 0; i < 5; ++i)
151 for (int j = 0; j <= i; ++j)
152 h.cov_[covOffset(i, j)] =
153 static_cast<float>(0.5 * (Cf(i, j) + Cf(j, i)));
154 h.hasCov_ = true;
155 return h;
156}
157
158Helix Helix::fromTrackElement(double c1, double c2, double c3,
159 double theta, double phi, double invP,
160 bool invPt, bool cylindrical,
161 const CovMatrix6& teCov,
162 double B) {
163 Helix h;
164
165 // On a cylinder the stored triple is (R, R*Phi, z); convert it, and its
166 // covariance, into the Cartesian basis the helix Jacobian expects. Phi is
167 // undefined on the axis, so guard R = 0.
168 double x = c1, y = c2;
169 CovMatrix6 cov = teCov;
170 if (cylindrical) {
171 // The stored second coordinate is the arc length R*Phi, so dividing by
172 // the radius recovers the azimuth of the point itself. This is the
173 // position angle, unrelated to `phi`, which is the track direction.
174 const double Phi = (c1 != 0.0) ? c2 / c1 : 0.0;
175 x = c1 * std::cos(Phi);
176 y = c1 * std::sin(Phi);
177
178 // Congruence C' = Jc * C * Jc^T, same construction as the helix step
179 // below: multiply, transpose, multiply, then symmetrise to kill the
180 // rounding asymmetry the two products leave behind.
181 const TMatrixD Jc = cylToCartJacobian(Phi);
182 const TMatrixDSym Cc = covFromArray(teCov);
183 const TMatrixD JC (Jc, TMatrixD::kMult, Cc);
184 const TMatrixD Jt (TMatrixD::kTransposed, Jc);
185 const TMatrixD Cx (JC, TMatrixD::kMult, Jt);
186 for (int i = 0; i < 6; ++i)
187 for (int j = 0; j <= i; ++j)
188 cov[covOffset(i, j)] = static_cast<float>(0.5 * (Cx(i, j) + Cx(j, i)));
189 }
190
191 h.refPoint_ = { static_cast<float>(x * kCm2Mm),
192 static_cast<float>(y * kCm2Mm),
193 static_cast<float>(c3 * kCm2Mm) };
194 h.p_.D0 = 0.f;
195 h.p_.Z0 = 0.f;
196 h.p_.time = 0.f;
197 h.p_.phi = static_cast<float>(phi);
198 h.hasCov_ = true;
199
200 const double s = std::sin(theta);
201 if (std::fabs(s) < 1e-9) {
202 h.p_.tanLambda = 0.f;
203 h.p_.omega = 0.f;
204 h.valid_ = false;
205 return h; // cov_ stays zero
206 }
207 h.p_.tanLambda = static_cast<float>(std::cos(theta) / s);
208 // invP is signed geometrically, as DELPHI writes it (positive =
209 // counter-clockwise), so omega = -kOmega*B*invP carries the sign of the
210 // charge; divided by sin(theta) when the word is 1/p rather than 1/p_T.
211 h.p_.omega = static_cast<float>(
212 invPt ? -kOmega * B * invP
213 : -kOmega * B * invP / s);
214
215 // Cov push-forward C_helix = J · C_te · J^T.
216 const TMatrixD J = teJacobian(theta, phi, invP, B, invPt);
217 const TMatrixDSym Ct = covFromArray(cov);
218 const TMatrixD JC (J, TMatrixD::kMult, Ct);
219 const TMatrixD Jt (TMatrixD::kTransposed, J);
220 const TMatrixD Cf (JC, TMatrixD::kMult, Jt);
221 for (int i = 0; i < 6; ++i)
222 for (int j = 0; j <= i; ++j)
223 h.cov_[covOffset(i, j)] =
224 static_cast<float>(0.5 * (Cf(i, j) + Cf(j, i)));
225 return h;
226}
227
228Helix Helix::fromHelix(float D0, float phi, float omega,
229 float Z0, float tanLambda) {
230 Helix h;
231 h.p_ = { D0, phi, omega, Z0, tanLambda, 0.f };
232 return h;
233}
234
235Momentum Helix::momentum(double B, int charge) const {
236 if (charge == 0 || std::fabs(B) < 1e-9 ||
237 std::fabs(p_.omega) < 1e-15) {
238 return Momentum{0.0, 0.0, 0.0, 0.0};
239 }
240 const double pT = kOmega * std::fabs(static_cast<double>(charge) * B)
241 / std::fabs(static_cast<double>(p_.omega));
242 const double px = pT * std::cos(p_.phi);
243 const double py = pT * std::sin(p_.phi);
244 const double pz = pT * static_cast<double>(p_.tanLambda);
245 return Momentum{px, py, pz, std::sqrt(px*px + py*py + pz*pz)};
246}
247
248edm4hep::TrackState Helix::toTrackState(int location) const {
249 edm4hep::TrackState ts;
250 ts.location = location;
251 ts.D0 = p_.D0;
252 ts.phi = p_.phi;
253 ts.omega = p_.omega;
254 ts.Z0 = p_.Z0;
255 ts.tanLambda = p_.tanLambda;
256 ts.time = p_.time;
257 ts.referencePoint = refPoint_;
258 if (hasCov_) {
259 using P = edm4hep::TrackParams;
260 static constexpr P kP[6] = {
261 P::d0, P::phi, P::omega, P::z0, P::tanLambda, P::time
262 };
263 for (int a = 0; a < 6; ++a)
264 for (int b = 0; b <= a; ++b) {
265 const float v = cov_[covOffset(a, b)];
266 ts.covMatrix.setValue(v, kP[a], kP[b]);
267 if (a != b) ts.covMatrix.setValue(v, kP[b], kP[a]);
268 }
269 }
270 return ts;
271}
272
273} // namespace delphi_edm4hep
static Helix fromHelix(float D0, float phi, float omega, float Z0, float tanLambda)
Definition Helix.cpp:228
Momentum momentum(double B, int charge) const
Definition Helix.cpp:235
const CovMatrix6 & cov() const
Definition Helix.h:100
static Helix fromTrackElement(double c1, double c2, double c3, double theta, double phi, double invP, bool invPt, bool cylindrical, const CovMatrix6 &teCov, double B)
Definition Helix.cpp:158
static Helix fromPerigee(float d0, float z0, float theta, float phi, float invR, const std::array< float, 15 > *W=nullptr)
Definition Helix.cpp:109
edm4hep::TrackState toTrackState(int location) const
Definition Helix.cpp:248
std::array< float, 21 > CovMatrix6
Definition Helix.h:36
constexpr double kOmega
Definition Helix.h:43
constexpr int covOffset(int i, int j)
Definition Helix.h:38