delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
SdstPaExtras.cpp
Go to the documentation of this file.
1// SdstPaExtrasWriter — pass-1 implementation.
2//
3// Per-track sDST PA modules emitted as ParticleID rows linked to their
4// particle. PA.PHOT (30), PA.ODHI (29) and PA.MUFI (27) are short lists of
5// scalars passed through from Q(l+2 ..), with the bank length from IPHREQ(1)
6// bounding the read. PA.HCRO (34) and PA.HCMU (35) are decoded instead: both
7// pack a count into a word and HCRO packs its per-plane hit pattern.
8
10
12
13#include <edm4hep/MutableParticleID.h>
14#include <edm4hep/ParticleIDCollection.h>
15
16#include <algorithm>
17#include <cmath>
18#include <cstdint>
19#include <limits>
20
21namespace ph = phdst;
22
24
25namespace {
26constexpr std::int32_t kAlgoPhot = 30;
27constexpr std::int32_t kAlgoOdhi = 29;
28constexpr std::int32_t kAlgoMufi = 27;
29constexpr std::int32_t kAlgoHcro = 34;
30constexpr std::int32_t kAlgoHcmu = 35;
31constexpr int kMaxParams = 7;
32constexpr int kMufiHeader = 17; // Q(+2..+18) fixed header words
33constexpr std::int32_t kAlgoMric = 6;
34constexpr int kHcroMuonWords = 5; // HATMUID block, Q(+9..+13)
35constexpr float kCm2Mm = 10.f;
36constexpr float kCm2Mm2 = 100.f; // for a variance in cm^2
37constexpr float kNotMeasured = std::numeric_limits<float>::quiet_NaN();
38
39int nint(float x) { return static_cast<int>(std::lround(x)); }
40} // namespace
41
43{
44 edm4hep::ParticleIDCollection photCol;
45 edm4hep::ParticleIDCollection odhiCol;
46 edm4hep::ParticleIDCollection mufiCol;
47 edm4hep::ParticleIDCollection hcroCol;
48 edm4hep::ParticleIDCollection hcroMuCol;
49 edm4hep::ParticleIDCollection hcmuCol;
50 edm4hep::ParticleIDCollection mricCol;
51
52 auto store = [&] {
53 put(std::move(photCol), "PHOT", "PhotonID", Provenance::Transcribed);
54 put(std::move(odhiCol), "ODHI", "OuterDetector", Provenance::Transcribed);
55 put(std::move(mufiCol), "MUFI", "RefitMuon", Provenance::Transcribed);
56 put(std::move(hcroCol), "HCRO", "HitPattern", Provenance::Transcribed);
57 put(std::move(hcroMuCol), "HCRO", "MuonTag", Provenance::Transcribed);
58 put(std::move(hcmuCol), "HCMU", "MuonID", Provenance::Transcribed);
59 put(std::move(mricCol), "MRIC", "RichExtrapolation", Provenance::Transcribed);
60 };
61
62 if (!ctx_.tracking) {
63 store();
64 return;
65 }
66 const auto& tracking = *ctx_.tracking;
67
68 // Emit Q(l+2 ..) up to `cap` words, bounded by the bank length.
69 auto emitParams = [&](edm4hep::MutableParticleID pid, int lbank, int cap) {
70 const int blen = pawalk::iphreq(1); // words in this sub-bank
71 const int n = std::min(cap, std::max(0, blen - 1)); // skip label
72 for (int k = 0; k < n; ++k) pid.addToParameters(ph::Q(lbank + 2 + k));
73 };
74
75 auto particleAt = [&](int paIdx) -> int {
76 if (paIdx < 0 || paIdx >= static_cast<int>(tracking.pa_to_particle.size()))
77 return -1;
78 return tracking.pa_to_particle[paIdx];
79 };
80
81 pawalk::forEachPA([&](int lpa, int paIdx) {
82 const int p_idx = particleAt(paIdx);
83 if (p_idx < 0) return;
84
85 if (const int lphot = pawalk::lphpa("PHOT", lpa); lphot > 0) {
86 auto pid = photCol.create();
87 pid.setAlgorithmType(kAlgoPhot);
88 emitParams(pid, lphot, kMaxParams);
89 pid.setParticle(tracking.particle_handles[p_idx]);
90 }
91
92 if (const int lodhi = pawalk::lphpa("ODHI", lpa); lodhi > 0) {
93 auto pid = odhiCol.create();
94 pid.setAlgorithmType(kAlgoOdhi);
95 emitParams(pid, lodhi, kMaxParams);
96 pid.setParticle(tracking.particle_handles[p_idx]);
97 }
98
99 // PA.MUFI (refitted-MU): emit the fixed header (det, nlay, ndf, chi2,
100 // extrap pos/dir + errors). Variable per-layer blocks not surfaced.
101 if (const int lmufi = pawalk::lphpa("MUFI", lpa); lmufi > 0) {
102 auto pid = mufiCol.create();
103 pid.setAlgorithmType(kAlgoMufi);
104 emitParams(pid, lmufi, kMufiHeader);
105 pid.setParticle(tracking.particle_handles[p_idx]);
106 }
107
108 // PA.HCRO: hadron-calorimeter read-out. Word 2 packs the module length
109 // and the total tube count as length + 1000*NHIT. Words 5..8 carry the
110 // hits per calorimeter plane, five planes to a word in base 16 with
111 // plane 1 in the most significant digit. Words 9..13 are the HATMUID
112 // muon block and exist only when the module length is 13.
113 if (const int lhcro = pawalk::lphpa("HCRO", lpa); lhcro > 0) {
114 const int packed = nint(ph::Q(lhcro + 2));
115 auto pid = hcroCol.create();
116 pid.setAlgorithmType(kAlgoHcro);
117 pid.addToParameters(static_cast<float>(packed / 1000));
118 pid.addToParameters(ph::Q(lhcro + 3) * kCm2Mm);
119 pid.addToParameters(ph::Q(lhcro + 4) * kCm2Mm);
120 for (int w = 0; w < 4; ++w) {
121 const int word = nint(ph::Q(lhcro + 5 + w));
122 for (int digit = 4; digit >= 0; --digit) {
123 pid.addToParameters(static_cast<float>((word >> (4 * digit)) & 0xF));
124 }
125 }
126 pid.setParticle(tracking.particle_handles[p_idx]);
127
128 if (packed % 1000 >= 13) {
129 auto mu = hcroMuCol.create();
130 mu.setAlgorithmType(kAlgoHcro);
131 for (int k = 0; k < kHcroMuonWords; ++k) {
132 mu.addToParameters(ph::Q(lhcro + 9 + k));
133 }
134 mu.setParticle(tracking.particle_handles[p_idx]);
135 }
136 }
137
138 // PA.HCMU: muon tag derived from that pattern. Word 2 packs the module
139 // length and the identification level as length + 1000*ID.
140 if (const int lhcmu = pawalk::lphpa("HCMU", lpa); lhcmu > 0) {
141 auto pid = hcmuCol.create();
142 pid.setAlgorithmType(kAlgoHcmu);
143 pid.addToParameters(static_cast<float>(nint(ph::Q(lhcmu + 2)) / 1000));
144 pid.addToParameters(ph::Q(lhcmu + 3) * kCm2Mm);
145 pid.addToParameters(ph::Q(lhcmu + 4) * kCm2Mm);
146 pid.setParticle(tracking.particle_handles[p_idx]);
147 }
148
149 // PA.MRIC — the RICH measurement PXDST records for this track: the
150 // refractive index of each radiator that fired, where the track entered
151 // the RICH and with what uncertainty, and how many ionization hits it
152 // left along the way. Distinct from the RICH values in PA.HAID, which is
153 // where SKELANA takes its Cherenkov angles from.
154 //
155 // Coordinates 1 and 2 are R*phi and z for a barrel track and x and y for
156 // a forward one; the module carries no flag saying which, so decide from
157 // the track's polar angle. The angles, 1/p and the variances mean the
158 // same either way.
159 if (const int lmric = pawalk::lphpa("MRIC", lpa); lmric > 0) {
160 // A radiator contributes four words led by its refractive index, or a
161 // single zero word when it did not fire.
162 float index[2] = {kNotMeasured, kNotMeasured};
163 int w = 2;
164 for (int radiator = 0; radiator < 2; ++radiator) {
165 const float n = ph::Q(lmric + w);
166 if (n == 0.f) { ++w; continue; }
167 index[radiator] = n;
168 w += 4;
169 }
170 const int tail = lmric + w - 1; // tail word k is at tail + k
171
172 auto pid = mricCol.create();
173 pid.setAlgorithmType(kAlgoMric);
174 pid.addToParameters(index[0]);
175 pid.addToParameters(index[1]);
176 pid.addToParameters(ph::Q(tail + 1) * kCm2Mm);
177 pid.addToParameters(ph::Q(tail + 2) * kCm2Mm);
178 pid.addToParameters(ph::Q(tail + 3));
179 pid.addToParameters(ph::Q(tail + 4));
180 pid.addToParameters(ph::Q(tail + 5));
181 pid.addToParameters(ph::Q(tail + 6) * kCm2Mm2);
182 pid.addToParameters(ph::Q(tail + 7) * kCm2Mm2);
183 pid.addToParameters(ph::Q(tail + 8));
184 pid.addToParameters(ph::Q(tail + 9));
185 pid.addToParameters(ph::Q(tail + 10));
186 pid.addToParameters(ph::Q(tail + 17));
187 pid.setParticle(tracking.particle_handles[p_idx]);
188 }
189 });
190
191 store();
192}
193
194} // namespace delphi_edm4hep::sdst_pa_extras
const Coll & put(Coll &&coll, std::string_view bank, std::string_view readable, Provenance prov)
void forEachPA(F &&fn)
Definition PaWalk.h:67
int lphpa(const char *iddp, int lpa, int nump=0)
Definition PaWalk.h:34
int iphreq(int nump=1)
Definition PaWalk.h:42
Definition PaWalk.h:21