10#include <edm4hep/Constants.h>
13#include <TMatrixDSym.h>
21constexpr double kCm2Mm = 10.0;
26TMatrixD perigeeJacobian(
double theta_dp) {
29 const double s = std::sin(theta_dp);
30 const double inv_s2 = (s != 0.0) ? 1.0 / (s * s) : 0.0;
33 J(2, 4) = -1.0 / kCm2Mm;
42TMatrixD teJacobian(
double theta,
double phi,
double invP,
43 double B_tesla,
bool invPt) {
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;
51 J(0, 0) = -std::sin(phi) * kCm2Mm;
52 J(0, 1) = std::cos(phi) * kCm2Mm;
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;
69 J(2, 3) = -k * c * invP * inv_s2;
84TMatrixD cylToCartJacobian(
double Phi) {
87 J(0, 1) = -std::sin(Phi);
88 J(1, 1) = std::cos(Phi);
96TMatrixDSym covFromArray(
const CovMatrix6& cov) {
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)]);
110 float invR,
const std::array<float, 15>* W) {
112 h.p_.
D0 = -d0 *
static_cast<float>(kCm2Mm);
118 h.p_.
omega = -invR /
static_cast<float>(kCm2Mm);
119 h.p_.
Z0 = z0 *
static_cast<float>(kCm2Mm);
122 const float s = std::sin(theta);
123 if (std::fabs(s) < 1e-6f) {
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; }
140 TMatrixDSym C_dp(Wm);
142 if (det == 0.0 || !std::isfinite(det)) {
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)
153 static_cast<float>(0.5 * (Cf(i, j) + Cf(j, i)));
159 double theta,
double phi,
double invP,
160 bool invPt,
bool cylindrical,
168 double x = c1, y = c2;
174 const double Phi = (c1 != 0.0) ? c2 / c1 : 0.0;
175 x = c1 * std::cos(Phi);
176 y = c1 * std::sin(Phi);
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)));
191 h.refPoint_ = {
static_cast<float>(x * kCm2Mm),
192 static_cast<float>(y * kCm2Mm),
193 static_cast<float>(c3 * kCm2Mm) };
197 h.p_.
phi =
static_cast<float>(phi);
200 const double s = std::sin(theta);
201 if (std::fabs(s) < 1e-9) {
207 h.p_.
tanLambda =
static_cast<float>(std::cos(theta) / s);
211 h.p_.
omega =
static_cast<float>(
212 invPt ? -
kOmega * B * invP
213 : -
kOmega * B * invP / s);
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)
224 static_cast<float>(0.5 * (Cf(i, j) + Cf(j, i)));
229 float Z0,
float tanLambda) {
231 h.p_ = { D0, phi, omega, Z0, tanLambda, 0.f };
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};
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)};
249 edm4hep::TrackState ts;
250 ts.location = location;
255 ts.tanLambda = p_.tanLambda;
257 ts.referencePoint = refPoint_;
259 using P = edm4hep::TrackParams;
260 static constexpr P kP[6] = {
261 P::d0, P::phi, P::omega, P::z0, P::tanLambda, P::time
263 for (
int a = 0; a < 6; ++a)
264 for (
int b = 0; b <= a; ++b) {
266 ts.covMatrix.setValue(v, kP[a], kP[b]);
267 if (a != b) ts.covMatrix.setValue(v, kP[b], kP[a]);
static Helix fromHelix(float D0, float phi, float omega, float Z0, float tanLambda)
Momentum momentum(double B, int charge) const
const CovMatrix6 & cov() const
static Helix fromTrackElement(double c1, double c2, double c3, double theta, double phi, double invP, bool invPt, bool cylindrical, const CovMatrix6 &teCov, double B)
static Helix fromPerigee(float d0, float z0, float theta, float phi, float invR, const std::array< float, 15 > *W=nullptr)
edm4hep::TrackState toTrackState(int location) const
std::array< float, 21 > CovMatrix6
constexpr int covOffset(int i, int j)