delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
ParticleId.cpp
Go to the documentation of this file.
1// ParticleId domain — implementation S7 (pass-1).
2//
3// Walks the VECP charged-track range and emits per-track PID rows for
4// dE/dx (TPC), muon, electron, and hadron-RICH. All five collections
5// are kept index-aligned via `setParticle()` to the matching Particle
6// handle from tracking.vecp_to_particle.
7//
8// Sources (skelana.txt A.3.5/7/9/10/11/12):
9// PSCDEX : TPC dE/dx -> sDST_HAID_dEdx + RecDqdx companion
10// PSCMUD : muon ID -> sDST_MUID_MuonID
11// PSCELD : electron ID -> sDST_ELID_ElectronID
12// PSCHAD : combined hadron ID (KHAID + QHAID) } merged into one
13// PSCGRC : RICH gas (THEG/SIGG/NPHG/NEPG) } 18-param HadronID
14// PSCLRC : RICH liquid (THEL/SIGL/NPHL/NEPL) } collection
15
17
18#include "skelana/pscdex.hpp"
19#include "skelana/psceld.hpp"
20#include "skelana/pscgrc.hpp"
21#include "skelana/pschad.hpp"
22#include "skelana/psclrc.hpp"
23#include "skelana/pscmud.hpp"
24#include "skelana/pscvec.hpp" // sk::LVPART, sk::NVECP
25
27#include "phdst/uxcom.hpp" // IQ
28#include "phdst/uxlink.hpp" // LDTOP
29
30#include <edm4hep/MutableParticleID.h>
31#include <edm4hep/ParticleIDCollection.h>
32#include <edm4hep/Quantity.h>
33#include <edm4hep/RecDqdxCollection.h>
34#include <podio/Frame.h>
35
36#include <cassert>
37#include <limits>
38#include <string>
39#include <vector>
40
41namespace ph = phdst;
42namespace sk = skelana;
43
45
46extern "C" {
47// BBDXGET reads the MTPC module: measured dE/dx, gap size, untruncated wire
48// count and method. BBDXER returns the expected error and, in IQUAL, a quality
49// flag. Both live in dstana (dedxid.car).
50void bbdxget_(int* lpa, float* dedx, float* gap, int* nwir, int* nmeth);
51float bbdxer_(float* dedxth, float* gap, int* nwir, int* nmeth, int* iqual);
52}
53
54namespace {
55
56// algorithmType constants for the four pass-1 PID flavours.
57constexpr std::int32_t kAlgoDedx = 1;
58constexpr std::int32_t kAlgoMuon = 2;
59constexpr std::int32_t kAlgoElectron = 3;
60constexpr std::int32_t kAlgoHadronID = 4;
61
62// PXDST version at or above which SKELANA runs BBDXGET in place of GETDEDX.
63constexpr int kBbdxMinPxdst = 333;
64
65// BBDXGET replaced GETDEDX at PXDST version 333. Both fill PSCDEX; SKELANA runs
66// whichever matches the version word of the event, so the collection is named
67// for the one that ran. On the BBDXGET path KDEDX(3) is the untruncated wire
68// count scaled by 0.8, an estimate of the wires entering the truncated mean
69// rather than a count of them.
70const char* dedxAlgo() {
71 const bool bbdx = ph::LDTOP > 0 && ph::IQ(ph::LDTOP + 3) >= kBbdxMinPxdst;
72 return bbdx ? "BBDXGET" : "GETDEDX";
73}
74
75// dE/dx quality flag per emitted Particle: -1 no information, 0 poor .. 4
76// perfect. dedxid.car:1137 advises requiring > 1.
77//
78// SKELANA computes this flag at skelana.car:3886 and then discards it, so it is
79// recomputed here from the same inputs. The result is indexed by Particle, not
80// by track, because the PA walk and the VECP loop reach the same particles
81// through different orderings.
82std::vector<int> dedxQuality(const tracking::Output& tracking)
83{
84 std::vector<int> quality(tracking.particle_handles.size(), -1);
85
86 pawalk::forEachPA([&](int lpa, int paIdx) {
87 // PAs that produced no Particle have nothing to attach a flag to.
88 if (paIdx >= static_cast<int>(tracking.pa_to_particle.size())) return;
89 const int p = tracking.pa_to_particle[paIdx];
90 if (p < 0) return;
91
92 // Measured dE/dx, gap size, untruncated wire count and method, all read
93 // from the MTPC module of this PA.
94 float dedx = -1.f, gap = 0.f;
95 int nwir = 0, nmeth = 0;
96 bbdxget_(&lpa, &dedx, &gap, &nwir, &nmeth);
97
98 // BBDXGET reports -1 when the track has no dE/dx measurement.
99 if (dedx <= 0.f || nwir < 1) return;
100
101 // The return value is the expected error, which SKELANA already stores as
102 // QDEDX(5); the flag returned alongside it is what is taken here.
103 int iqual = -1;
104 bbdxer_(&dedx, &gap, &nwir, &nmeth, &iqual);
105 quality[p] = iqual;
106 });
107
108 return quality;
109}
110
111} // namespace
112
114{
115 edm4hep::ParticleIDCollection dedxCol;
116 edm4hep::RecDqdxCollection dqdxCol;
117 edm4hep::ParticleIDCollection muonCol;
118 edm4hep::ParticleIDCollection elecCol;
119 edm4hep::ParticleIDCollection hadrCol;
120
121 if (!ctx_.tracking) {
122 // No tracks emitted upstream — still emit empty collections so
123 // the schema remains stable across events.
124 put(std::move(dedxCol), dedxAlgo(), "Dedx", Provenance::Derived);
125 put(std::move(dqdxCol), dedxAlgo(), "DedxRecDqdx", Provenance::Derived);
126 put(std::move(muonCol), "MUID", "MuonID", Provenance::Transcribed);
127 put(std::move(elecCol), "ELID", "ElectronID", Provenance::Transcribed);
128 put(std::move(hadrCol), "HAID", "HadronID", Provenance::Transcribed);
129 return;
130 }
131 const auto& tracking = *ctx_.tracking;
132 const std::vector<int> quality = dedxQuality(tracking);
133
134 // Helper: create a per-particle PID row of the given algorithmType,
135 // attach to the matching Particle, and return the row for parameter
136 // population by the caller.
137 auto makePid = [&](edm4hep::ParticleIDCollection& coll,
138 int vecp_i,
139 std::int32_t algoType)
140 -> edm4hep::MutableParticleID
141 {
142 auto pid = coll.create();
143 pid.setAlgorithmType(algoType);
144 const int p_idx = tracking.vecp_to_particle[vecp_i];
145 pid.setParticle(tracking.particle_handles[p_idx]);
146 return pid;
147 };
148
149 // PSCVEC with IFLODR=1 orders charged particles at indices
150 // LVPART..NCVECP (= 1..NCVECP since LVPART=1), neutrals after.
151 // All pass-1 PID collections (dE/dx, MUID, ELID, HadronID) are
152 // **charged-only** per the SKELANA documentation — neutrals have
153 // no track in the TPC / muon chamber / RICH so their PSCDEX /
154 // PSCMUD / PSCELD / PSCHAD entries are all-zero. We bound the loop
155 // to NCVECP to keep the emitted collection size = charged-track count.
156 const int last_charged = sk::NCVECP; // 1-based inclusive
157 for (int i = sk::LVPART; i <= last_charged; ++i) {
158 if (i >= static_cast<int>(tracking.vecp_to_particle.size())) break;
159 if (tracking.vecp_to_particle[i] < 0) continue;
160
161 // ---- TPC dE/dx (PSCDEX) ----
162 // QDEDX(1) = dE/dx value (MIP-normalised)
163 // QDEDX(5) = error on the dE/dx measurement (sigma; NOT Landau width)
164 // KDEDX(3) = # wires used
165 // Per-track emit policy: emit when there's real data (dE/dx > 0 or
166 // wires > 0) — distinguishes "PSCDEX didn't run for this track"
167 // from "negative-tag result".
168 {
169 const float dedx = sk::QDEDX(1, i);
170 const float sigma = sk::QDEDX(5, i);
171 const int nWires = sk::KDEDX(3, i);
172 if (dedx > 0.f || nWires > 0) {
173 auto pid = makePid(dedxCol, i, kAlgoDedx);
174 pid.addToParameters(dedx);
175 pid.addToParameters(sigma);
176 pid.addToParameters(
177 static_cast<float>(quality[tracking.vecp_to_particle[i]]));
178
179 // RecDqdx parallel form (FCC-style consumers consume the typed
180 // Quantity rather than ParticleID parameters[]). Type=1 marks
181 // "TPC truncated-mean dE/dx".
182 auto dq = dqdxCol.create();
183 edm4hep::Quantity q;
184 q.type = 1;
185 q.value = dedx;
186 q.error = sigma;
187 dq.setDQdx(q);
188 // Link the RecDqdx to its Track via the charged Particle's
189 // Particle->Track relation (set in tracking; one track per charged
190 // PFO). PidHybrid re-points the clone onto the fDST track in pass 2.
191 const auto dq_part =
192 tracking.particle_handles[tracking.vecp_to_particle[i]];
193 for (const auto& trk : dq_part.getTracks()) { dq.setTrack(trk); break; }
194 }
195 }
196
197 // ---- Muon ID (PSCMUD) ----
198 // Slot layout and the tag bit masks are in Pid/ParticleId.h.
199 {
200 const int tag = sk::KMUID(1, i);
201 if (tag != 0) {
202 auto pid = makePid(muonCol, i, kAlgoMuon);
203 pid.addToParameters(static_cast<float>(tag));
204 pid.addToParameters(sk::QMUID(2, i));
205 pid.addToParameters(static_cast<float>(sk::KMUID(3, i)));
206 assert(pid.getParameters().size() == kMuCount);
207 }
208 }
209
210 // ---- Electron ID (PSCELD) ----
211 // KELID(1) = ELECID tag (0..5):
212 // 0 = no ID run; 1 = NOT electron;
213 // 2 = very loose; 3 = loose; 4 = standard; 5 = tight
214 // KELID(2) = gamma-conversion tag (1..3)
215 {
216 const int tag = sk::KELID(1, i);
217 if (tag != 0) {
218 auto pid = makePid(elecCol, i, kAlgoElectron);
219 pid.addToParameters(static_cast<float>(tag));
220 pid.addToParameters(static_cast<float>(sk::KELID(2, i)));
221 }
222 }
223
224 // ---- Hadron-RICH combined (PSCHAD + PSCGRC + PSCLRC) ----
225 // 18-element parameters in fixed order. Always emitted for any
226 // VECP-matched track so the collection length is parallel-ish to
227 // Particles for charged tracks (downstream consumers can index by
228 // setParticle() rather than position).
229 //
230 // Params [12] (gasFlag, FLAGG) and [17] (liqFlag, FLAGL) carry the
231 // RICH RING/VETO quality word. These are Fortran REALs whose value
232 // is the (small) integer flag (SKELANA does FLAGG(I)=KGRIC(5,I)),
233 // so they are emitted verbatim; read downstream via int(round(v)).
234 {
235 auto pid = makePid(hadrCol, i, kAlgoHadronID);
236 // PSCHAD KHAID / QHAID
237 pid.addToParameters(static_cast<float>(sk::KHAID(4, i))); // kaonRich
238 pid.addToParameters(static_cast<float>(sk::KHAID(5, i))); // protonRich
239 pid.addToParameters(static_cast<float>(sk::KHAID(6, i))); // pionRich
240 pid.addToParameters(static_cast<float>(sk::KHAID(2, i))); // kaonDedx
241 pid.addToParameters(static_cast<float>(sk::KHAID(3, i))); // protonDedx
242 pid.addToParameters(sk::QHAID(7, i)); // xKaon
243 pid.addToParameters(sk::QHAID(8, i)); // xProton
244 pid.addToParameters(static_cast<float>(sk::KHAID(9, i))); // richQuality
245 // PSCGRC (RICH gas)
246 pid.addToParameters(sk::THEG(i)); // θ_C gas (rad)
247 pid.addToParameters(sk::SIGG(i)); // σ_θ gas
248 pid.addToParameters(static_cast<float>(sk::NPHG(i))); // N_ph gas
249 pid.addToParameters(static_cast<float>(sk::NEPG(i))); // N_expected gas
250 // FLAGG = KGRIC(5,I): RICH-gas RING/VETO quality flag, a small
251 // integer carried in a Fortran REAL slot. Emit verbatim; read
252 // downstream via int(round(v)). Observed values {0,1,7,9,15,25,
253 // 31,39,47,63}. Bit semantics per the DELPHI HAID-module doc
254 // (longdes §3.04, "+4 quality flag"); consult it before cutting.
255 pid.addToParameters(sk::FLAGG(i)); // FLAGG quality word
256 // PSCLRC (RICH liquid)
257 pid.addToParameters(sk::THEL(i));
258 pid.addToParameters(sk::SIGL(i));
259 pid.addToParameters(static_cast<float>(sk::NPHL(i)));
260 pid.addToParameters(static_cast<float>(sk::NEPL(i))); // N_expected liquid
261 pid.addToParameters(sk::FLAGL(i)); // FLAGL quality word
262 }
263 }
264
265 put(std::move(dedxCol), dedxAlgo(), "Dedx", Provenance::Derived);
266 put(std::move(dqdxCol), dedxAlgo(), "DedxRecDqdx", Provenance::Derived);
267 put(std::move(muonCol), "MUID", "MuonID", Provenance::Transcribed);
268 put(std::move(elecCol), "ELID", "ElectronID", Provenance::Transcribed);
269 put(std::move(hadrCol), "HAID", "HadronID", Provenance::Transcribed);
270}
271
272// Pass-2 PID (TOF, MTPC-extended, TE FitQuality companion) will live in
273// a separate writer added during step-2 wiring.
274
275} // namespace delphi_edm4hep::particleid
const Coll & put(Coll &&coll, std::string_view bank, std::string_view readable, Provenance prov)
float bbdxer_(float *dedxth, float *gap, int *nwir, int *nmeth, int *iqual)
void bbdxget_(int *lpa, float *dedx, float *gap, int *nwir, int *nmeth)
void forEachPA(F &&fn)
Definition PaWalk.h:67
Definition PaWalk.h:21