delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
Calorimeter.cpp
Go to the documentation of this file.
1// Calorimeter domain — shower clusters from the PA calorimeter modules.
2//
3// Walks the PA chain and emits one Cluster per shower. Cluster.type bits
4// encode the sub-detector:
5// bit 0 = HPC (EMNC, idet 9)
6// bit 1 = FEMC (EMNC, idet 26)
7// bit 2 = HCAL (HCNC or HCAL module)
8//
9// Hadron showers come from two modules, and which the production wrote is a
10// per-processing choice: HCNC(23), HCAL(3), or both. Both are emitted. Where
11// both are present HCNC supersedes HCAL — it is HCAL re-associated by HACCOR
12// (ecorrxx.car:273, "HCNC module overwrites module 3 info"). The README
13// states the consequence for analysis.
14//
15// One collection per source module. Charged/neutral is not split here — it
16// is inferable from the owning Particle's `clusters` relation.
17//
18// Module layouts are documented in the ShortDST content note, DELPHI 97-146
19// PROG-221, doi:10.7483/OPENDATA.DELPHI.LJAQ.LMZ0, under "PA extra-module
20// EMNC(22)" and "HCNC(23)". Word numbers below refer to those tables.
21
23
25
26#include <edm4hep/CalorimeterHit.h>
27#include <edm4hep/ClusterCollection.h>
28#include <edm4hep/MutableCluster.h>
29#include <edm4hep/ReconstructedParticleCollection.h>
30#include <podio/Frame.h>
31
32#include <algorithm>
33#include <cstdint>
34#include <string>
35
37
38namespace {
39
40// DELPHI cm -> EDM4hep mm.
41constexpr double kCm2Mm = 10.0;
42
43// Cluster.type bit encoding (see file header).
44constexpr std::int32_t kTypeBitHPC = 0x01;
45constexpr std::int32_t kTypeBitEMF = 0x02;
46constexpr std::int32_t kTypeBitHCAL = 0x04;
47
48// Detector identifiers carried in EMNC word +2.
49constexpr int kIdetHPC = 9;
50constexpr int kIdetEMF = 26;
51
52// Defensive bound on the per-shower layer count. Both modules document
53// layers 1..4.
54constexpr int kMaxHcalLayers = 200;
55
56// Shower header: energy (GeV) at word `e`, then x, y, z (cm).
57void setHeaderEnergyPosition(edm4hep::MutableCluster clu,
58 const banks::Record& shower, int e) {
59 clu.setEnergy(shower.real(e));
60 clu.setPosition({
61 static_cast<float>(shower.real(e + 1) * kCm2Mm),
62 static_cast<float>(shower.real(e + 2) * kCm2Mm),
63 static_cast<float>(shower.real(e + 3) * kCm2Mm),
64 });
65}
66
67// Electromagnetic showers for one PA. Emits one Cluster per shower and
68// attaches each to the owning particle.
69void walkEMNC(int lpa,
70 edm4hep::MutableReconstructedParticle pfo,
71 edm4hep::ClusterCollection& cluCol,
72 const std::vector<std::vector<edm4hep::CalorimeterHit>>* pa_hits)
73{
74 const auto emnc = banks::find(lpa, "EMNC");
75 if (!emnc) return;
76
77 // Module word +2 packs both counts: nshowr + 100 * idet. idet is uniform
78 // within one module.
79 const int nsidet = emnc->integer(2);
80 const int nshowr = nsidet % 100;
81 const int idet = nsidet / 100;
82 if (nshowr <= 0) return;
83
84 const std::int32_t type_bit =
85 (idet == kIdetHPC) ? kTypeBitHPC :
86 (idet == kIdetEMF) ? kTypeBitEMF :
87 0; // unknown detector — emit anyway
88
89 // The HPC has ten layers.
90 constexpr int kMaxHpcLayers = 10;
91
92 auto shower = emnc->subRecord(2);
93 for (int ns = 0; ns < nshowr; ++ns) {
94 auto clu = cluCol.create();
95 clu.setType(type_bit);
96
97 // Calorimeter hits of the same shower, decoded by EmcaWriter from the
98 // same PA. PA.EMCA and PA.EMNC list a PA's showers in the same order.
99 if (pa_hits && ns < static_cast<int>(pa_hits->size())) {
100 for (const auto& hit : (*pa_hits)[static_cast<std::size_t>(ns)]) {
101 clu.addToHits(hit);
102 }
103 }
104 setHeaderEnergyPosition(clu, shower, 1);
105
106 int stride = 4; // FEMC carries no layer detail here
107
108 if (idet == kIdetHPC) {
109 // HPC continuation: +5 = layers hit, +6 = layer bit pattern,
110 // +7.. = energy of each layer present, in pattern order.
111 const int nlay = shower.integer(5);
112 const int patt = shower.integer(6);
113 if (nlay > 0 && nlay <= kMaxHpcLayers) {
114 // Expanded to a dense ten-element profile, zero for layers absent
115 // from the pattern, so subdetectorEnergies has a fixed length per
116 // HPC cluster.
117 int filled = 0;
118 for (int nl = 1; nl <= kMaxHpcLayers && filled < nlay; ++nl) {
119 const bool hit = ((patt >> (nl - 1)) & 1) != 0;
120 float layerE = 0.f;
121 if (hit) {
122 ++filled;
123 layerE = shower.real(6 + filled);
124 }
125 clu.addToSubdetectorEnergies(layerE);
126 }
127 }
128 stride = 6 + nlay;
129 }
130 // FEMC per-block detail lives in PA module EL(5), which is fullDST only.
131
132 pfo.addToClusters(clu);
133 shower = shower.subRecord(stride);
134 }
135}
136
137// Hadronic showers for one PA. Per-layer energies go to
138// subdetectorEnergies and the matching layer indices to shapeParameters,
139// so the two stay index-parallel without a second collection.
140void walkHCNC(int lpa,
141 edm4hep::MutableReconstructedParticle pfo,
142 edm4hep::ClusterCollection& cluCol)
143{
144 const auto hcnc = banks::find(lpa, "HCNC");
145 if (!hcnc) return;
146
147 // Module word +2 = number of showers.
148 const int nshowr = hcnc->integer(2);
149 if (nshowr <= 0) return;
150
151 auto shower = hcnc->subRecord(2);
152 for (int ns = 0; ns < nshowr; ++ns) {
153 auto clu = cluCol.create();
154 clu.setType(kTypeBitHCAL);
155 setHeaderEnergyPosition(clu, shower, 1);
156
157 // +5 = layer hits. Each hit is two words:
158 // +5 + 2n - 1 energy
159 // +5 + 2n 1000 * layer + channels in that layer
160 const int nlay = shower.integer(5);
161 if (nlay > 0 && nlay < kMaxHcalLayers) {
162 for (int nl = 1; nl <= nlay; ++nl) {
163 const float hitE = shower.real(5 + 2 * nl - 1);
164 const int layer = shower.integer(5 + 2 * nl) / 1000;
165 clu.addToSubdetectorEnergies(hitE);
166 clu.addToShapeParameters(static_cast<float>(layer));
167 }
168 }
169
170 pfo.addToClusters(clu);
171 shower = shower.subRecord(5 + 2 * std::max(0, nlay));
172 }
173}
174
175// Hadronic showers from the HCAL module. Same content as walkHCNC at
176// different offsets, so the emitted fields match. Showers are named
177// sub-records rather than a fixed stride (PSHHAC, skelana.car:3504).
178void walkHCAL(int lpa,
179 edm4hep::MutableReconstructedParticle pfo,
180 edm4hep::ClusterCollection& cluCol)
181{
182 const auto hcal = banks::find(lpa, "HCAL");
183 if (!hcal) return;
184
185 // Module word +2 = number of showers.
186 const int nshowr = hcal->integer(2);
187
188 for (int ns = 1; ns <= nshowr; ++ns) {
189 const auto shower = hcal->subRecord("HCAL.SHOWER", ns);
190 if (!shower) break;
191
192 auto clu = cluCol.create();
193 clu.setType(kTypeBitHCAL);
194 setHeaderEnergyPosition(clu, *shower, 3);
195
196 // +10 = layers hit, +11 = layer bit pattern. Each hit is two words:
197 // +11 + 2n - 1 energy
198 // +11 + 2n 1000 * layer + active channels in that layer
199 const int nlay = shower->integer(10);
200 if (nlay > 0 && nlay < kMaxHcalLayers) {
201 for (int nl = 1; nl <= nlay; ++nl) {
202 clu.addToSubdetectorEnergies(shower->real(11 + 2 * nl - 1));
203 clu.addToShapeParameters(
204 static_cast<float>(shower->integer(11 + 2 * nl) / 1000));
205 }
206 }
207
208 pfo.addToClusters(clu);
209 }
210}
211
212} // namespace
213
215{
216 edm4hep::ClusterCollection emncCol;
217 edm4hep::ClusterCollection hcncCol;
218 edm4hep::ClusterCollection hcalCol;
219
220 if (ctx_.tracking) {
221 const auto& tracking = *ctx_.tracking;
222 pawalk::forEachPA([&](int lpa, int paIdx) {
223 if (paIdx >= static_cast<int>(tracking.pa_to_particle.size())) return;
224 const int particle_idx = tracking.pa_to_particle[paIdx];
225 if (particle_idx < 0) return;
226 auto pfo = tracking.particle_handles[particle_idx];
227
228 const std::vector<std::vector<edm4hep::CalorimeterHit>>* pa_hits = nullptr;
229 if (ctx_.emca && paIdx < static_cast<int>(ctx_.emca->pa_shower_hits.size())) {
230 pa_hits = &ctx_.emca->pa_shower_hits[static_cast<std::size_t>(paIdx)];
231 }
232 walkEMNC(lpa, pfo, emncCol, pa_hits);
233 walkHCNC(lpa, pfo, hcncCol);
234 walkHCAL(lpa, pfo, hcalCol);
235 });
236 }
237
238 // Emitted even when empty, so the schema does not depend on whether
239 // tracking ran.
240 put(std::move(emncCol), "EMNC", "Showers", Provenance::Transcribed);
241 put(std::move(hcncCol), "HCNC", "Showers", Provenance::Transcribed);
242 put(std::move(hcalCol), "HCAL", "Showers", Provenance::Transcribed);
243}
244
245} // namespace delphi_edm4hep::calorimeter
const Coll & put(Coll &&coll, std::string_view bank, std::string_view readable, Provenance prov)
std::optional< Record > find(int lpa, const char *name)
Definition BankReader.cpp:7
void forEachPA(F &&fn)
Definition PaWalk.h:67