delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
Btag.cpp
Go to the documentation of this file.
1// Btag.cpp — b-tagging domain implementation.
2//
3// Reads PSCBTG (event/hemisphere probabilities) and, when AABTAG was
4// actually rerun, the AAMAIN / AAMNVX commons (per-track impact
5// parameters, per-track probabilities, VD quality, and AABTAG's own
6// primary vertex), the AAJETS / AASCND / AAJESV commons (AABTAG's jets and
7// the secondary vertices its AAFSEC search found), and -- after calling the
8// combined tag of DELPHI 97-094 (AACMBT, AACMZ0) -- AACTVR / AACTRS.
9
11
15
16#include "skelana/functions.hpp"
17#include "skelana/pscbsp.hpp"
18#include "skelana/pscbtg.hpp"
19#include "skelana/pscflg.hpp"
20
21#include <edm4hep/MutableParticleID.h>
22#include <edm4hep/ParticleIDCollection.h>
23#include <edm4hep/MutableReconstructedParticle.h>
24#include <edm4hep/MutableVertex.h>
25#include <edm4hep/ReconstructedParticleCollection.h>
26#include <edm4hep/VertexCollection.h>
27#include <podio/UserDataCollection.h>
28
29#include <algorithm>
30#include <cmath>
31#include <cstdint>
32#include <limits>
33#include <optional>
34#include <string>
35#include <unordered_map>
36#include <vector>
37
38namespace sk = skelana;
39namespace aa = delphi_edm4hep::aabtag;
40
41namespace delphi_edm4hep::btag {
42
43namespace {
44
45constexpr double kCm2Mm = 10.0;
46constexpr float kCm2Mm2_f = static_cast<float>(kCm2Mm * kCm2Mm);
47constexpr float kNaN = std::numeric_limits<float>::quiet_NaN();
48
49// algorithmType tag for AABTAG's primary vertex. Distinct from the values
50// Vertex.cpp uses (0 primary, 1 secondary, 2 beamspot, 4 simulation,
51// 10 V0, 11 photon conversion) so the two PVs are never confused.
52constexpr int kAlgoBtagPV = 3;
53
54// algorithmType for the per-track b-tag row.
55constexpr int kAlgoBtagTag = 4;
56
57// algorithmType for AABTAG's secondary-vertex hypotheses (AAFSEC).
58constexpr int kAlgoBtagSV = 5;
59
60// algorithmType for the per-jet combined-tag row (AACMBT).
61constexpr int kAlgoBtagCtr = 6;
62
63// Map the "not computed" sentinel to NaN so a consumer that forgets to check
64// cannot silently average it in.
65// PSFBTG pre-fills every PSCBTG word with 2.0 and only overwrites on success,
66// so 2.0 means "not computed". Neither a probability nor a direction cosine
67// can legitimately reach it.
68float prob(float v) { return (v >= 1.999f) ? kNaN : v; }
69
70// LUTHRU reports failure with THRVAL=-1 or -2; PSCBTG otherwise uses the 2.0
71// prefill sentinel. Physical thrust is in [0,1], so map both failure domains.
72float thrustValue(float v) { return (v < 0.f || v >= 1.999f) ? kNaN : v; }
73
74} // namespace
75
76// The PSCBTG common is a single output buffer: PSHBTG fills it from the
77// stored BTAG bank and PSFBTG from the recalculation, so whichever ran last
78// is what it holds.
79void BtagWriter::emitEventLevel(std::string_view bank, Provenance prov,
80 bool valid)
81{
82 const auto eventProb = [&](float value) { return valid ? prob(value) : kNaN; };
83 // Index order within each triplet is (hemisphere 1, hemisphere 2, whole
84 // event), matching QBTPRN/QBTPRP/QBTPRS(1..3).
85 putParameter(bank, "ProbNegIP",
86 std::vector<float>{eventProb(sk::QBTPRN(1)),
87 eventProb(sk::QBTPRN(2)),
88 eventProb(sk::QBTPRN(3))}, prov);
89 putParameter(bank, "ProbPosIP",
90 std::vector<float>{eventProb(sk::QBTPRP(1)),
91 eventProb(sk::QBTPRP(2)),
92 eventProb(sk::QBTPRP(3))}, prov);
93 putParameter(bank, "ProbAllIP",
94 std::vector<float>{eventProb(sk::QBTPRS(1)),
95 eventProb(sk::QBTPRS(2)),
96 eventProb(sk::QBTPRS(3))}, prov);
97 // The thrust axis gets the same sentinel treatment: VFILL sets it to 2.0
98 // as well, and a direction cosine can never legitimately exceed 1, so an
99 // un-mapped 2.0 here would be a sentinel masquerading as data.
100 putParameter(bank, "ThrustAxis",
101 std::vector<float>{eventProb(sk::QBTTHR(1)),
102 eventProb(sk::QBTTHR(2)),
103 eventProb(sk::QBTTHR(3))}, prov);
104 // QBTTHR(4) is the thrust VALUE, not an axis component.
105 putParameter(bank, "ThrustValue",
106 valid ? thrustValue(sk::QBTTHR(4)) : kNaN, prov);
107}
108
110{
111 putParameter("BTAGCFG", "SourcePrefix",
112 std::string(fromFullDst() ? "fDST" : "sDST"), Provenance::Custom);
113 putParameter("BTAGCFG", "BeamSpotErrorCode", sk::IERRBS,
115
116 // AAFLAG is meaningful only when PSFBTG actually called AABTGS. PSFBTG
117 // skips that call when IERRBS != 0 and leaves IBAD (and the rich COMMON
118 // arrays) stale. Failed AABTAG events can retain derived values too, so gate
119 // the entire rich payload on the combined current-event status rather than
120 // sanitizing one field at a time.
121 const auto status = aa::eventStatus(sk::IERRBS, aa::IBAD());
122 const bool tagValid = status.valid;
123
124 // Both tags are emitted. PSHORT has already run PSFBTG, so the recalculated
125 // values are the ones currently in PSCBTG; read them before PSHBTG refills
126 // the common from the stored bank. The stored tag needs no validity gate --
127 // an absent bank leaves the 2.0 prefill, which prob() maps to NaN.
128 emitEventLevel("AABTAG", Provenance::Derived, tagValid);
129 sk::PSHBTG();
130 emitEventLevel("BTG", Provenance::Transcribed, /*valid=*/true);
131
132 // The per-track arrays and AABTAG's vertex come from AAMAIN / AAMNVX, which
133 // PSHBTG does not touch, so they still hold the recalculation.
134 const std::string_view bank = "AABTAG";
135 const Provenance prov = Provenance::Derived;
136
137 // ---- AABTAG primary-vertex output (AAMNVX) -------------------------
138 // Emitted as its own collection rather than replacing the DELANA PV.
139 // The collection is empty when Valid != 1. A nonempty entry still
140 // needs NDF/NTracksAttached checks before it is described as track-fitted;
141 // a status-zero result can be a beamspot-only constraint.
142 // With IFLPVT = Keep (the default) SKELANA never overwrites QVTX, so a
143 // consumer gets both vertices and picks; nothing is destroyed.
144 // Built here but put after the per-track loop, so the tracks AABTAG
145 // attached to this vertex can be linked while it is still mutable.
146 edm4hep::VertexCollection btagPv;
147 std::optional<edm4hep::MutableVertex> pv;
148 if (tagValid) {
149 pv = btagPv.create();
150 pv->setPrimary(true);
151 pv->setAlgorithmType(kAlgoBtagPV);
152 pv->setPosition({static_cast<float>(aa::POSVX(1) * kCm2Mm),
153 static_cast<float>(aa::POSVX(2) * kCm2Mm),
154 static_cast<float>(aa::POSVX(3) * kCm2Mm)});
155 pv->setChi2(aa::CHI2VX());
156 pv->setNdf(aa::NDOFVX());
157 pv->setCovMatrix({aa::COVVX(1) * kCm2Mm2_f, aa::COVVX(2) * kCm2Mm2_f,
158 aa::COVVX(3) * kCm2Mm2_f, aa::COVVX(4) * kCm2Mm2_f,
159 aa::COVVX(5) * kCm2Mm2_f, aa::COVVX(6) * kCm2Mm2_f});
160 }
161
162 // ---- Per-track quantities (AAMAIN + AAMNVX) ------------------------
163 // AABTAG's arrays are dimensioned kMaxTracks. Its NTRK common saturates at
164 // that capacity, so exact truncation is not observable; record capacity
165 // saturation conservatively rather than claiming that extra tracks existed.
166 // NTRK belongs to the current event only if AABTGS was called. On the
167 // beam-spot bypass it is stale by construction, so serialize zero rather
168 // than mislabelling a preceding event's count as raw current-event data.
169 const int ntrk_raw = status.algorithmInvoked ? aa::NTRK() : 0;
170 const int ntrk = tagValid ? std::clamp(ntrk_raw, 0, aa::kMaxTracks) : 0;
171 // BadEventCode deliberately preserves the raw AAFLAG/IBAD snapshot. It is
172 // current-event status only when AlgorithmInvoked=1; on the PSFBTG beamspot
173 // bypass it can be stale. Valid is the authoritative combined gate.
174 putParameter(bank, "BadEventCode", status.badEventCode, Provenance::Derived);
175 putParameter(bank, "AlgorithmInvoked", status.algorithmInvoked ? 1 : 0, Provenance::Custom);
176 putParameter(bank, "Valid", status.valid ? 1 : 0, Provenance::Custom);
177 putParameter(bank, "NTracksRaw", std::clamp(ntrk_raw, 0, aa::kMaxTracks), Provenance::Custom);
178 putParameter(bank, "NTracks", ntrk, Provenance::Custom);
179 putParameter(bank, "NTracksAttached", tagValid ? aa::NATTVX() : 0, Provenance::Custom);
180 // Retain the established field name for campaign compatibility. Its value
181 // is deliberately conservative: 1 means the common reached capacity and
182 // additional eligible tracks may (but cannot be proven to) have been lost.
183 putParameter(bank, "Truncated",
184 tagValid && ntrk_raw >= aa::kMaxTracks ? 1 : 0, Provenance::Custom);
185
186 // lpa -> PA-walk index, so IADTR (a ZEBRA L-address) can be resolved to
187 // the Particle the Tracking domain emitted for that PA.
188 std::unordered_map<int, int> lpa_to_pa;
189 pawalk::forEachPA([&](int lpa, int paIdx) { lpa_to_pa.emplace(lpa, paIdx); });
190
191 // One row per track AABTAG used, in its own 1..NTRK ordering, linked to
192 // the particle it belongs to. The impact parameters are NOT here: they are
193 // a property of the track and ride on it as a TrackState at AABTAG's
194 // vertex (see Tracking.cpp). Reach them from a row via
195 // getParticle() -> getTracks() -> the AtVertex state.
196 edm4hep::ParticleIDCollection tags;
197
198 for (int i = 1; i <= ntrk; ++i) {
199 auto tag = tags.create();
200 tag.setAlgorithmType(kAlgoBtagTag);
201
202 // AAMNVX defines CHI2TR for attached tracks only. The slot is not cleared
203 // for the rest, so it holds whatever the fit last left there -- often the
204 // preceding event's value. Publish NaN rather than that.
205 const bool attached = aa::INMVX(i);
206
207 // AATPRB leaves the probabilities at 1.0 for tracks it could not use.
208 tag.addToParameters(aa::TRPR (i));
209 tag.addToParameters(aa::TRPRZ(i));
210 tag.addToParameters(aa::CHI2VD(i));
211 tag.addToParameters(attached ? aa::CHI2TR(i) : kNaN);
212 tag.addToParameters(aa::PMOM (i));
213 // These count-like legacy values are signed: AAP* efficiency/acceptance
214 // corrections negate them to mark rejection; abs(value) is the count.
215 tag.addToParameters(static_cast<float>(aa::NVDP (i)));
216 tag.addToParameters(static_cast<float>(aa::NVDPZ(i)));
217 tag.addToParameters(static_cast<float>(aa::NLAY (i)));
218 tag.addToParameters(static_cast<float>(aa::NLAYZ(i)));
219 tag.addToParameters(static_cast<float>(aa::ISRT(i))); // 0 = unused
220 tag.addToParameters(static_cast<float>(attached ? 1 : 0));
221 // [11..21] jet / hemisphere / sign bookkeeping from AAJETS, AASCND,
222 // AAJESV and the AAMAIN status word. Appended after the original 11 so
223 // existing readers keep their indices; BtagCheckDomains.h names the slots.
224 // Everything here that refers to "the primary vertex" means AABTAG's own,
225 // emitted as AABTAG_PrimaryVertex (POSVX in AAMNVX) -- the beam-spot
226 // constrained fit AABTAG performs for itself. It is NOT PV_PrimaryVertex,
227 // which is DELPHI's standard vertex, and the two differ by order 10 um.
228 tag.addToParameters(static_cast<float>(aa::IJET(i))); // CombinedTagRow row, 1-based
229 tag.addToParameters(static_cast<float>(aa::ITHR(i))); // thrust hemisphere (1/2)
230 // PHIV is not a sign, despite what the AAMAIN header says. AASIGN fills it
231 // through AASGNT -> AADCAJ -> AADIST -> POINTF, which solves
232 // s1 = PV + al * jet_direction for the two points of closest approach
233 // between the track helix and the jet axis and returns `al`: the signed
234 // distance ALONG THE JET AXIS, in cm, from the primary vertex to that
235 // point. Its sign is what signs the impact parameter -- which is all the
236 // DELPHI comment ever meant -- and AADCAJ's own header says so.
237 //
238 // It is emitted as two slots rather than one, because the quantity is two
239 // things at once: wherever AASGNT abandons the calculation (same rows as
240 // the NaN distance below) it degrades to a bare +-1 carrying only a sign,
241 // and a blanket cm -> mm would turn that into a +-10 that reads as a 1 cm
242 // measurement. So the sign goes in its own slot, valid on every row, and
243 // the length goes in mm like every other length in the file, NaN on
244 // exactly the rows where DISTJ is NaN.
245 const bool jetDistMeasured = aa::DISTJ(i) != 0.f && aa::ERRTJ(i) < 100.f;
246 tag.addToParameters(std::signbit(aa::PHIV(i)) ? -1.f : 1.f);
247 tag.addToParameters(jetDistMeasured ? static_cast<float>(aa::PHIV(i) * kCm2Mm) : kNaN);
248 tag.addToParameters(aa::RPDT(i)); // rapidity w.r.t. its jet
249 // AASGNT abandons the track-jet distance in two places and leaves a
250 // placeholder rather than a measurement: a track with no VD z-hits or a
251 // primary vertex with a bad z-covariance gives dist = 0, err = 100 cm, and
252 // AADCAJ bails out on a null jet momentum with dist = 0, err = sqrt(200)
253 // cm. Together that is a third of all tracks (34% over 70k tracks of 94c
254 // data), and their err = 1000 mm drags the mean of the column from 0.27 mm
255 // to 336 mm. Map both to NaN, as prob() does for PSCBTG's 2.0; in those
256 // rows only the SIGN of PHIV above carries information.
257 tag.addToParameters(jetDistMeasured ? static_cast<float>(aa::DISTJ(i) * kCm2Mm) : kNaN);
258 tag.addToParameters(jetDistMeasured ? static_cast<float>(aa::ERRTJ(i) * kCm2Mm) : kNaN);
259 tag.addToParameters(static_cast<float>(aa::INSV(i))); // SV hypothesis using it (+100 flags)
260 // Track-quality code as AASTRK / AASLCT / AAIMPC leave it, i.e. before the
261 // combined tag runs: -98 no VD hits (or p > 1.1 Ebeam), -90 passed an
262 // NLAY/CHI2VD class, 10 within AAIMPC's impact-parameter significance cut,
263 // -99 AASTRK's initial value where nothing reclassified it. AAK0LS can also
264 // write 99 (not in the track list, rewritten to -90 by the combined tag)
265 // and 200/300/400 (K0 / Lambda / conversion daughter), but neither appeared
266 // in 70k tracks of 94c data -- see docs/README.md.
267 tag.addToParameters(static_cast<float>(aa::IST(i)));
268 tag.addToParameters(static_cast<float>(aa::IJSV(i))); // ditto, after the SV redefinition
269 // LUTHRU's track index. The value points into a JETSET record this
270 // converter does not ship, so only ILUND != 0 is meaningful: it says the
271 // track entered the thrust and oblateness calculation, without which
272 // AABTAG_ThrustValue / ThrustAxis and Oblateness cannot be reproduced.
273 tag.addToParameters(static_cast<float>(aa::ILUND(i)));
274
275 if (auto it = lpa_to_pa.find(aa::IADTR(i)); it != lpa_to_pa.end()) {
276 if (const auto particle = particleForPa(it->second)) {
277 tag.setParticle(*particle);
278 // Tracks AABTAG attached to its own vertex, as a relation rather
279 // than a flag to re-derive.
280 if (pv && attached) pv->addToParticles(*particle);
281 }
282 }
283 }
284
285 put(std::move(tags), bank, "TrackTag", prov);
286 put(std::move(btagPv), bank, "PrimaryVertex", prov);
287
288 emitCombinedTag(tagValid, lpa_to_pa);
289}
290
291// ---------------------------------------------------------------------------
292// Secondary vertices and the combined tag.
293//
294// AABTGS (called by PSFBTG) already ran AAFSEC, DELPHI's secondary-vertex
295// search, and AASIGN used it to re-sign the impact parameters; the vertices
296// themselves stay in the AASCND common and are never written to a bank. The
297// combined tag (DELPHI 97-094) is a separate entry point, AACMBT, that PSFBTG
298// never calls; it consumes those commons and fills AACTVR / AACTRS per jet,
299// and AACMZ0 turns the per-jet values into the event tag. This is the same
300// sequence BSAURUS's PXBTAG('RUN') uses.
301//
302// Output (all Derived, bank AABTAG):
303// SecondaryVertices one Vertex per AAFSEC hypothesis, accepted or not
304// (parameters[0] = ITSEC says which); position mm,
305// covariance mm^2, particles = the fitted tracks. chi2
306// is NaN and ndf 0: AABTAG does not keep the fit chi2
307// (see the AASCND comment). CHI2SV, the vertex's
308// displacement significance, is in the parameters.
309// CombinedTagRow one ParticleID per AABTAG jet, in jet order: type =
310// category JTAG, likelihood = X_jet, parameters as
311// documented below. AABTAG's own jets are deliberately
312// NOT emitted as ReconstructedParticles -- EDM4hep has
313// no agreed jet type, and this JADE clustering
314// (y_min = 0.01) is not one anybody would use today --
315// but the axis every variable refers to is kept as the
316// first four parameters of the row, and the tracks are
317// kept through the per-track IJET word on TrackTag.
318// CombinedTagEvent X_ev (AACMZ0); CombinedTagHemisphere the largest
319// X_jet per thrust hemisphere (BSAURUS's BTAG(29,30)).
320void BtagWriter::emitCombinedTag(bool valid,
321 const std::unordered_map<int, int>& lpa_to_pa)
322{
323 const std::string_view bank = "AABTAG";
324 const Provenance prov = Provenance::Derived;
325
326 // AABTAG track index (1..NTRK) -> its particle, or empty.
327 const auto particleForTrack =
328 [&](int i) -> std::optional<edm4hep::ReconstructedParticle> {
329 if (i < 1 || i > aa::kMaxTracks) return std::nullopt;
330 const auto it = lpa_to_pa.find(aa::IADTR(i));
331 if (it == lpa_to_pa.end()) return std::nullopt;
332 return particleForPa(it->second);
333 };
334
335 // AACMSV bails out on IBAD != 0 but AACMNS does not, and on the beam-spot
336 // bypass every common is stale; so call into AABTAG only on a valid event.
337 int njet = 0, nhypo = 0;
338 if (valid) {
339 // On MC, AALINT (lepton-ID tuning inside AACMBT) consumes RNDM numbers
340 // that the next event's impact-parameter smearing would otherwise have
341 // used; restore the seed so the lifetime tag stays bit-identical to the
342 // unmodified converter. See the note on rdmout_/rdmin_ in AabtagCommons.h.
343 std::int32_t seed[2] = {0, 0}; // not filled by the routine we link;
344 aa::rdmout_(seed); // SXRNU keeps the state in its own slot 3
345 aa::aacmbt_();
346 aa::aacmz0_();
347 aa::rdmin_(seed);
348 njet = std::clamp(aa::NJET(), 0, aa::kMaxJets);
349 nhypo = std::clamp(aa::NHYPO(), 0, aa::kMaxHypo);
350 }
351
352 // ---- secondary-vertex hypotheses (AASCND) ------------------------------
353 // PV below is AABTAG's own primary vertex, emitted as AABTAG_PrimaryVertex
354 // (POSVX in AAMNVX, which AAFSEC hands to AADCAN) -- not PV_PrimaryVertex.
355 // parameters: [0] ITSEC [1] NSEC [2] NSVRT [3] NRAP
356 // [4] JETSV, the CombinedTagRow row this hypothesis belongs to (1-based)
357 // [5] IF3DSV [6] PRBSEC [7] CHI2SV
358 // [8] CSEC, always NaN: AABTAG never fills it (see the AASCND comment);
359 // the slot is kept so the indices below do not move
360 // [9..12] PSEC px py pz E (GeV) [13..16] PRAP px py pz E (GeV)
361 // [17,18] TPHSEC theta, phi of the PV->SV direction (rad)
362 // [19..21] VTPHS its covariance
363 // [22..26] DCASEC d_rphi d_z (mm), var_rphi cov var_z (mm^2): the impact
364 // parameters of the SV pseudo-particle w.r.t. the PV, in the EDM4hep D0 /
365 // Z0 convention of PV_Tracks_d0PV / z0PV. AADCAN's dr = (PV - SV) .
366 // (sin phi, -cos phi) already is D0. Its dz is PV_z minus the z of the SV
367 // line at closest approach, the negative of Z0, so d_z and the r-phi/z
368 // covariance are negated here.
369 // [27 .. 27+NSEC-1] ISEC, signed AABTAG track indices (see AASCND)
370 // [27+NSEC .. +NRAP-1] IRAP, AABTAG track indices of the rapidity tracks
371 edm4hep::VertexCollection svCol;
372 int nAccepted = 0;
373 for (int h = 1; h <= nhypo; ++h) {
374 auto sv = svCol.create();
375 sv.setPrimary(false);
376 sv.setAlgorithmType(kAlgoBtagSV);
377 sv.setPosition({static_cast<float>(aa::VSEC(1, h) * kCm2Mm),
378 static_cast<float>(aa::VSEC(2, h) * kCm2Mm),
379 static_cast<float>(aa::VSEC(3, h) * kCm2Mm)});
380 sv.setCovMatrix({aa::SSEC(1, h) * kCm2Mm2_f, aa::SSEC(2, h) * kCm2Mm2_f,
381 aa::SSEC(3, h) * kCm2Mm2_f, aa::SSEC(4, h) * kCm2Mm2_f,
382 aa::SSEC(5, h) * kCm2Mm2_f, aa::SSEC(6, h) * kCm2Mm2_f});
383 sv.setChi2(kNaN); // AABTAG keeps no fit chi2; CHI2SV is parameters[7]
384 sv.setNdf(0);
385 const int itsec = aa::ITSEC(h);
386 if (itsec >= 0) ++nAccepted;
387 const int nsec = std::clamp(aa::NSEC(h), 0, aa::kMaxSvTracks);
388 const int nrap = std::clamp(aa::NRAP(h), 0, aa::kMaxSvTracks);
389 sv.addToParameters(static_cast<float>(itsec));
390 sv.addToParameters(static_cast<float>(nsec));
391 sv.addToParameters(static_cast<float>(aa::NSVRT(h)));
392 sv.addToParameters(static_cast<float>(nrap));
393 sv.addToParameters(static_cast<float>(aa::JETSV(h)));
394 sv.addToParameters(static_cast<float>(aa::IF3DSV(h)));
395 sv.addToParameters(aa::PRBSEC(h));
396 sv.addToParameters(aa::CHI2SV(h));
397 sv.addToParameters(kNaN); // CSEC
398 for (int i = 1; i <= 4; ++i) sv.addToParameters(aa::PSEC(i, h));
399 for (int i = 1; i <= 4; ++i) sv.addToParameters(aa::PRAP(i, h));
400 sv.addToParameters(aa::TPHSEC(1, h));
401 sv.addToParameters(aa::TPHSEC(2, h));
402 for (int i = 1; i <= 3; ++i) sv.addToParameters(aa::VTPHS(i, h));
403 sv.addToParameters(static_cast<float>(aa::DCASEC(1, h) * kCm2Mm));
404 sv.addToParameters(static_cast<float>(-aa::DCASEC(2, h) * kCm2Mm));
405 sv.addToParameters(aa::DCASEC(3, h) * kCm2Mm2_f);
406 sv.addToParameters(-aa::DCASEC(4, h) * kCm2Mm2_f);
407 sv.addToParameters(aa::DCASEC(5, h) * kCm2Mm2_f);
408 for (int k = 1; k <= nsec; ++k) {
409 const int idx = aa::ISEC(k, h);
410 sv.addToParameters(static_cast<float>(idx));
411 if (const auto p = particleForTrack(std::abs(idx))) sv.addToParticles(*p);
412 }
413 for (int k = 1; k <= nrap; ++k)
414 sv.addToParameters(static_cast<float>(aa::IRAP(k, h)));
415 }
416
417 // ---- the per-jet combined tag (AAJETS, AACTVR / AACTRS) ----------------
418 // One row per AABTAG jet, in jet order, so the per-track IJET / IJSV words
419 // and the per-hypothesis JETSV word index it directly (1-based).
420 // CombinedTagRow parameters:
421 // [0..3] PJET px py pz E, the jet axis every variable below refers to
422 // [4] JTAG [5] XEFFJ [6] thrust hemisphere (1/2)
423 // [7..12] TAGV(1..6) [13..18] RATVQ(1..6) [19..24] RATVC(1..6)
424 // [25] RATCQ [26] RATCC [27] NTRS [28] RTTVQ [29] RTTVC
425 // [30..33] PJSV px py pz E, the jet direction after the SV redefinition
426 // [34 .. 34+4*NTRS-1] per rapidity track: INVTS (AABTAG track index),
427 // TGVT (rapidity), RTTVQI, RTTVCI
428 // JTAG, also the row's type: 1 has an SV, 2 no SV but >= 2 significant
429 // tracks, 3 lifetime only, 4 lifetime only with no rapidity track (NTRS =
430 // 0), 0 not tagged. Category 4 is common (70% of rows on the 94c data
431 // identity sample, 15% on the 94c qqbar simulation one), and AACMZ0 treats
432 // it as 3.
433 // The ParticleID carries no particle relation: the object it would point
434 // at is exactly the jet we are not emitting.
435 edm4hep::ParticleIDCollection jetTags;
436 // Largest X_jet per thrust hemisphere over the tagged jets (JTAG > 0, so
437 // including 4), as PXBTAG does for BTAG(29,30); NaN when the hemisphere has
438 // no tagged jet.
439 float hemiBest[2] = {kNaN, kNaN};
440 for (int j = 1; j <= njet; ++j) {
441 // Collection position + 1 is the AABTAG jet number.
442 const float cosjt = aa::PJET(1, j) * aa::PTHR(1) + aa::PJET(2, j) * aa::PTHR(2)
443 + aa::PJET(3, j) * aa::PTHR(3);
444 const int hemi = cosjt >= 0.f ? 1 : 2;
445 const int jtag = aa::JTAG(j);
446 const float xeffj = aa::XEFFJ(j);
447 if (jtag > 0 && (std::isnan(hemiBest[hemi - 1]) || xeffj > hemiBest[hemi - 1]))
448 hemiBest[hemi - 1] = xeffj;
449
450 auto tag = jetTags.create();
451 tag.setAlgorithmType(kAlgoBtagCtr);
452 tag.setType(jtag);
453 tag.setLikelihood(xeffj);
454 for (int i = 1; i <= 4; ++i) tag.addToParameters(aa::PJET(i, j));
455 tag.addToParameters(static_cast<float>(jtag));
456 tag.addToParameters(xeffj);
457 tag.addToParameters(static_cast<float>(hemi));
458 for (int i = 1; i <= aa::kNTagVars; ++i) tag.addToParameters(aa::TAGV (i, j));
459 for (int i = 1; i <= aa::kNTagVars; ++i) tag.addToParameters(aa::RATVQ(i, j));
460 for (int i = 1; i <= aa::kNTagVars; ++i) tag.addToParameters(aa::RATVC(i, j));
461 tag.addToParameters(aa::RATCQ(j));
462 tag.addToParameters(aa::RATCC(j));
463 const int ntrs = std::clamp(aa::NTRS(j), 0, aa::kMaxRapTracks);
464 tag.addToParameters(static_cast<float>(ntrs));
465 tag.addToParameters(aa::RTTVQ(j));
466 tag.addToParameters(aa::RTTVC(j));
467 for (int i = 1; i <= 4; ++i) tag.addToParameters(aa::PJSV(i, j));
468 for (int k = 1; k <= ntrs; ++k) {
469 tag.addToParameters(static_cast<float>(aa::INVTS(k, j)));
470 tag.addToParameters(aa::TGVT (k, j));
471 tag.addToParameters(aa::RTTVQI(k, j));
472 tag.addToParameters(aa::RTTVCI(k, j));
473 }
474 }
475
476 putParameter(bank, "CombinedTagEvent", valid ? aa::XEFFEV() : kNaN, prov);
477 putParameter(bank, "CombinedTagHemisphere", std::vector<float>{hemiBest[0], hemiBest[1]}, prov);
478 putParameter(bank, "Oblateness", valid ? aa::OBLVAL() : kNaN, prov);
479 putParameter(bank, "NCombinedTagRows", njet, Provenance::Custom);
480 putParameter(bank, "NSecondaryVertexHypotheses", nhypo, Provenance::Custom);
481 putParameter(bank, "NSecondaryVertices", nAccepted, Provenance::Custom);
482
483 put(std::move(svCol), bank, "SecondaryVertices", prov);
484 put(std::move(jetTags), bank, "CombinedTagRow", prov);
485}
486
487} // namespace delphi_edm4hep::btag
void putParameter(std::string_view bank, std::string_view key, T &&value, Provenance prov)
std::optional< edm4hep::ReconstructedParticle > particleForPa(int paIdx) const
const Coll & put(Coll &&coll, std::string_view bank, std::string_view readable, Provenance prov)
std::int32_t & NTRK()
float & VTPHS(int i, int h)
std::int32_t & NLAY(int i)
std::int32_t & IJSV(int i)
std::int32_t & NSEC(int h)
std::int32_t & NHYPO()
float & PJET(int i, int j)
std::int32_t & IBAD()
constexpr int kMaxRapTracks
std::int32_t & IST(int i)
std::int32_t & NTRS(int j)
void rdmin_(std::int32_t seed[2])
float & DCASEC(int i, int h)
std::int32_t & IJET(int i)
std::int32_t & INVTS(int k, int j)
std::int32_t & INSV(int i)
float & TGVT(int k, int j)
std::int32_t & NDOFVX()
float & VSEC(int i, int h)
std::int32_t & ISEC(int k, int h)
std::int32_t & INMVX(int i)
float & RTTVQI(int k, int j)
std::int32_t & NLAYZ(int i)
std::int32_t & NVDP(int i)
float & PJSV(int i, int j)
std::int32_t & ITHR(int i)
constexpr EventStatus eventStatus(int beamSpotErrorCode, int rawBadEventCode)
constexpr int kMaxSvTracks
std::int32_t & JETSV(int h)
void rdmout_(std::int32_t seed[2])
std::int32_t & IADTR(int i)
float & RATVQ(int i, int j)
std::int32_t & IRAP(int k, int h)
float & TAGV(int i, int j)
float & PSEC(int i, int h)
std::int32_t & NJET()
std::int32_t & NATTVX()
float & TPHSEC(int i, int h)
constexpr int kMaxTracks
std::int32_t & JTAG(int j)
std::int32_t & NVDPZ(int i)
std::int32_t & IF3DSV(int h)
float & PRAP(int i, int h)
float & RTTVCI(int k, int j)
std::int32_t & ITSEC(int h)
std::int32_t & NSVRT(int h)
std::int32_t & ISRT(int i)
float & RATVC(int i, int j)
float & SSEC(int i, int h)
std::int32_t & ILUND(int i)
std::int32_t & NRAP(int h)
void forEachPA(F &&fn)
Definition PaWalk.h:67