delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
Emca.cpp
Go to the documentation of this file.
1// EmcaWriter — implementation. Runs in both passes.
2//
3// PA.EMCA bank layout (per shower, header at show_ofs):
4// show_ofs+1 len (length in words of this shower record)
5// show_ofs+2 idet (9 = HPC barrel, 26 = FEMC endcap)
6// show_ofs+3 E (GeV)
7// show_ofs+4..+6 X, Y, Z (cm) — shower-level centroid
8// show_ofs+10 nclu (per-pad or per-layer count)
9// show_ofs+11 nwdcl (words per cluster: 3 = HPC, 2 = FEMC)
10// show_ofs+11+k*nwdcl.. k-th per-cluster record
11//
12// HPC clusters use the 3-word PXHGET packing decoded by
13// internal::hpc::padDecode → (E, layer, X, Y, Z, sigma_z).
14// FEMC layers are (energy, packed=layer*1000+nhits).
15
17
20
21#include <edm4hep/CalorimeterHitCollection.h>
22#include <edm4hep/MutableCalorimeterHit.h>
23
24#include <cmath>
25#include <cstdint>
26#include <limits>
27
28namespace ph = phdst;
29
30namespace delphi_edm4hep::emca {
31
32namespace {
33
34constexpr float kNotMeasured = std::numeric_limits<float>::quiet_NaN();
35
36constexpr float kCm2Mm = 10.f;
37constexpr int kIdetHPC = 9;
38constexpr int kIdetFEMC = 26;
39constexpr int kNwdHPC = 3;
40constexpr int kNwdFEMC = 2;
41
42} // namespace
43
45{
46 edm4hep::CalorimeterHitCollection hpc_col;
47 edm4hep::CalorimeterHitCollection femc_col;
48
49 Output out;
50
51 auto record = [&](int paIdx, int ns, const edm4hep::CalorimeterHit& hit) {
52 if (paIdx < 0) return;
53 if (paIdx >= static_cast<int>(out.pa_shower_hits.size())) {
54 out.pa_shower_hits.resize(static_cast<std::size_t>(paIdx) + 1);
55 }
56 auto& showers = out.pa_shower_hits[static_cast<std::size_t>(paIdx)];
57 if (ns >= static_cast<int>(showers.size())) {
58 showers.resize(static_cast<std::size_t>(ns) + 1);
59 }
60 showers[static_cast<std::size_t>(ns)].push_back(hit);
61 };
62
63 pawalk::forEachPA([&](int lpa, int paIdx) {
64 const int lemca = pawalk::lphpa("EMCA", lpa);
65 if (lemca <= 0) return;
66 const int nsh = static_cast<int>(std::lround(ph::Q(lemca + 2)));
67 if (nsh <= 0) return;
68
69 int show_ofs = lemca + 2; // shower 1 starts here
70 for (int ns = 0; ns < nsh; ++ns) {
71 const int len = static_cast<int>(std::lround(ph::Q(show_ofs + 1)));
72 const int idet = static_cast<int>(std::lround(ph::Q(show_ofs + 2)));
73 const int nclu = static_cast<int>(std::lround(ph::Q(show_ofs + 10)));
74 const int nwdcl = static_cast<int>(std::lround(ph::Q(show_ofs + 11)));
75
76 if (idet == kIdetHPC && nclu > 0 && nclu < 200 && nwdcl == kNwdHPC) {
77 for (int nc = 0; nc < nclu; ++nc) {
78 const int base = show_ofs + 11 + kNwdHPC * nc;
79 const std::int32_t i1 = ph::IQ(base + 1);
80 const std::int32_t i2 = ph::IQ(base + 2);
81 const std::int32_t i3 = ph::IQ(base + 3);
82 const float w1 = ph::Q(base + 1);
83 const float w2 = ph::Q(base + 2);
84 const float w3 = ph::Q(base + 3);
85 float pe, pxc, pyc, pzc, psz; int player;
86 hpc::padDecode(i1, i2, i3, w1, w2, w3,
87 pe, player, pxc, pyc, pzc, psz);
88 if (pe <= 0.f) continue;
89 auto hit = hpc_col.create();
90 hit.setEnergy(pe);
91 hit.setEnergyError(kNotMeasured); // PXHGET gives no energy error
92 hit.setTime(0.f); // not available in PXHGET
93 hit.setType(player); // layer 1..10
94 hit.setPosition({
95 pxc * kCm2Mm,
96 pyc * kCm2Mm,
97 pzc * kCm2Mm,
98 });
99 record(paIdx, ns, hit);
100 }
101 } else if (idet == kIdetFEMC && nclu > 0 && nclu <= 10 && nwdcl == kNwdFEMC) {
102 // FEMC: per-layer (energy, packed = layer*1000 + nhits).
103 // Position is the shower-level centroid (FEMC bank doesn't
104 // store per-layer X/Y); type carries the layer index, cellID
105 // carries nhits.
106 const float showX = ph::Q(show_ofs + 4);
107 const float showY = ph::Q(show_ofs + 5);
108 const float showZ = ph::Q(show_ofs + 6);
109 for (int nc = 0; nc < nclu; ++nc) {
110 const int base = show_ofs + 11 + kNwdFEMC * nc;
111 const float layer_e = ph::Q(base + 1);
112 const int packed = static_cast<int>(std::lround(ph::Q(base + 2)));
113 const int layer = packed / 1000;
114 const int nhits = packed % 1000;
115 auto hit = femc_col.create();
116 hit.setEnergy(layer_e);
117 hit.setEnergyError(0.f);
118 hit.setTime(0.f);
119 hit.setType(layer);
120 hit.setCellID(static_cast<std::uint64_t>(nhits));
121 hit.setPosition({
122 showX * kCm2Mm,
123 showY * kCm2Mm,
124 showZ * kCm2Mm,
125 });
126 record(paIdx, ns, hit);
127 }
128 }
129
130 if (len <= 0) break;
131 show_ofs += len;
132 }
133 });
134
135 ctx_.emca = std::move(out);
136 put(std::move(hpc_col), "EMCA", "HPCClusters", Provenance::Transcribed);
137 put(std::move(femc_col), "EMCA", "FEMCLayers", Provenance::Transcribed);
138}
139
140} // namespace delphi_edm4hep::emca
const Coll & put(Coll &&coll, std::string_view bank, std::string_view readable, Provenance prov)
void padDecode(std::int32_t i1, std::int32_t i2, std::int32_t i3, float w1, float w2, float w3, float &E, int &layer, float &X, float &Y, float &Z, float &sigZ)
void forEachPA(F &&fn)
Definition PaWalk.h:67
int lphpa(const char *iddp, int lpa, int nump=0)
Definition PaWalk.h:34
Definition PaWalk.h:21
std::vector< std::vector< std::vector< edm4hep::CalorimeterHit > > > pa_shower_hits
Definition EmcaData.h:19