delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
Trax.cpp
Go to the documentation of this file.
1// TraxWriter — implementation. Runs in both passes, before the track writers.
2//
3// PA.TRAX (label 20) holds the track extrapolated onto a set of named
4// surfaces: its own first measured point, the surface just in front of it, and
5// the named detectors, ordered by increasing R in the barrel and |Z| in the
6// endcap (dst_content.txt:1917-1945).
7//
8// Module layout, 1-based from the LPHPA address:
9// +2 number of surfaces
10// then per point, the first at +3:
11// +0 number of following words, 8 without a covariance or 23 with one
12// +1 detector id +2 IBAR, 1 = (R, R*Phi, z) / 0 = (x, y, z)
13// +3..+5 coordinates (cm) +6 theta +7 phi +8 signed 1/p
14// +9..+23 covariance
15// the next point starts one word past the last, the count word not being
16// included in its own count.
17//
18// Each point becomes an edm4hep::TrackState on the track built from the same
19// PA, so a reader walks TRAC_Tracks -> trackStates rather than a side
20// collection. The states carry no detector field, but `location` distinguishes
21// the first measured point and the calorimeter crossings, and the surfaces are
22// separable by radius in any case.
23
25
28
29#include <edm4hep/TrackState.h>
30
31#include <cmath>
32#include <limits>
33#include <string>
34
35namespace ph = phdst;
36
37namespace delphi_edm4hep::trax {
38
39namespace {
40
41// A point record is the count word plus that many words.
42constexpr int kCountWord = 1;
43constexpr int kWordsNoCov = 8;
44constexpr int kWordsWithCov = 23;
45constexpr int kCovWords = 15;
46constexpr float kNotMeasured = std::numeric_limits<float>::quiet_NaN();
47
48// TANAGRA detector id -> edm4hep TrackState location. The calorimeter surfaces
49// are HPC(9), HAB(13), HAF(22) and EMF(26); id 0 is the track's own first
50// measured point; TOF(11) and the muon chambers (14, 17, 30) have no dedicated
51// location. Ids are the ones PXTRAX writes (pxdst34.car:15991-15994), named
52// through TANAGRA's own table (tanagra322.car:12164-12176).
53int locationForDetector(int det_id) {
54 switch (det_id) {
55 case 0: return edm4hep::TrackState::AtFirstHit;
56 case 9: case 13: case 22: case 26:
57 return edm4hep::TrackState::AtCalorimeter;
58 default: return edm4hep::TrackState::AtOther;
59 }
60}
61
62// The 15 covariance words are a 5x5 lower-triangular row-major matrix over the
63// free parameters: the three stored coordinates minus the one the surface
64// fixes, then theta, phi and 1/p (exx.car:5035-5036).
65// cylinder: (R*Phi, z, theta, phi, 1/p) plane: (x, y, theta, phi, 1/p)
66// Scatter it into the TE basis Helix::fromTrackElement reads.
67CovMatrix6 covariance(int lpt, bool cylindrical) {
68 static constexpr int kCylinder[5] = {1, 2, 3, 4, 5};
69 static constexpr int kPlane [5] = {0, 1, 3, 4, 5};
70 const int* idx = cylindrical ? kCylinder : kPlane;
71
72 CovMatrix6 cov{};
73 int slot = 0;
74 for (int i = 0; i < 5; ++i) {
75 for (int j = 0; j <= i; ++j, ++slot) {
76 cov[covOffset(idx[i], idx[j])] = ph::Q(lpt + kWordsNoCov + 1 + slot);
77 }
78 }
79 return cov;
80}
81
82int nint(float x) { return static_cast<int>(std::lround(x)); }
83
84} // namespace
85
87{
88 Output out;
89
90 // Field strength as the event recorded it, so both passes agree.
91 const double B = frame_.getParameter<float>(
92 makeName("EVT", "BField")).value_or(0.f);
93
94 pawalk::forEachPA([&](int lpa, int paIdx) {
95 if (paIdx >= static_cast<int>(out.pa_to_states.size())) {
96 out.pa_to_states.resize(paIdx + 1);
97 }
98 const int ltrax = pawalk::lphpa("TRAX", lpa);
99 if (ltrax <= 0) return;
100
101 const int blen = pawalk::iphreq();
102 const int npts = nint(ph::Q(ltrax + 2));
103 const int q = pawalk::trueCharge(lpa);
104
105 int lpt = ltrax + 3;
106 for (int ip = 0; ip < npts; ++ip) {
107 const int n_words = nint(ph::Q(lpt));
108 if (n_words < kWordsNoCov) break; // malformed
109 if (lpt + n_words > ltrax + blen) break; // past the bank
110
111 const int det_id = nint(ph::Q(lpt + 1));
112 const bool cylindrical = nint(ph::Q(lpt + 2)) != 0; // IBAR
113 const bool has_cov = n_words >= kWordsWithCov;
114
115 // The central extrapolator writes 1/p signed geometrically, but the
116 // muon/calorimeter one (EXXTR: TOF, HAB, HAF, MUB, MUF, SMC) writes the
117 // unsigned 1/|p| (exx.car:9858, 9902) despite the bank documentation. Its
118 // covariance row still refers to the signed word, so restore that sign:
119 // geometric = -charge. Harmless on the points already signed.
120 const double word = ph::Q(lpt + 8);
121 const double invP = q != 0 ? -q * std::fabs(word) : word;
122
123 const auto helix = Helix::fromTrackElement(
124 ph::Q(lpt + 3), ph::Q(lpt + 4), ph::Q(lpt + 5),
125 ph::Q(lpt + 6), ph::Q(lpt + 7), invP,
126 /*invPt=*/false,
127 cylindrical,
128 has_cov ? covariance(lpt, cylindrical) : CovMatrix6{},
129 B);
130
131 auto state = helix.toTrackState(locationForDetector(det_id));
132 // Not every TRAX point carries a covariance; mark it as not measured
133 // rather than leaving zeros that read as exact.
134 if (!has_cov) state.covMatrix.values.fill(kNotMeasured);
135 out.pa_to_states[paIdx].push_back(state);
136
137 lpt += n_words + kCountWord;
138 }
139 });
140
141 ctx_.trax = std::move(out);
142}
143
144} // namespace delphi_edm4hep::trax
std::string makeName(std::string_view bank, std::string_view readable) 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)
Definition Helix.cpp:158
edm4hep::TrackState toTrackState(int location) const
Definition Helix.cpp:248
void forEachPA(F &&fn)
Definition PaWalk.h:67
int trueCharge(int lpa)
Definition PaWalk.h:50
int lphpa(const char *iddp, int lpa, int nump=0)
Definition PaWalk.h:34
int iphreq(int nump=1)
Definition PaWalk.h:42
std::array< float, 21 > CovMatrix6
Definition Helix.h:36
constexpr int covOffset(int i, int j)
Definition Helix.h:38
Definition PaWalk.h:21
std::vector< std::vector< edm4hep::TrackState > > pa_to_states
Definition TraxData.h:17