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>
41constexpr double kCm2Mm = 10.0;
44constexpr std::int32_t kTypeBitHPC = 0x01;
45constexpr std::int32_t kTypeBitEMF = 0x02;
46constexpr std::int32_t kTypeBitHCAL = 0x04;
49constexpr int kIdetHPC = 9;
50constexpr int kIdetEMF = 26;
54constexpr int kMaxHcalLayers = 200;
57void setHeaderEnergyPosition(edm4hep::MutableCluster clu,
58 const banks::Record& shower,
int e) {
59 clu.setEnergy(shower.real(e));
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),
70 edm4hep::MutableReconstructedParticle pfo,
71 edm4hep::ClusterCollection& cluCol,
72 const std::vector<std::vector<edm4hep::CalorimeterHit>>* pa_hits)
79 const int nsidet = emnc->integer(2);
80 const int nshowr = nsidet % 100;
81 const int idet = nsidet / 100;
82 if (nshowr <= 0)
return;
84 const std::int32_t type_bit =
85 (idet == kIdetHPC) ? kTypeBitHPC :
86 (idet == kIdetEMF) ? kTypeBitEMF :
90 constexpr int kMaxHpcLayers = 10;
92 auto shower = emnc->subRecord(2);
93 for (
int ns = 0; ns < nshowr; ++ns) {
94 auto clu = cluCol.create();
95 clu.setType(type_bit);
99 if (pa_hits && ns <
static_cast<int>(pa_hits->size())) {
100 for (
const auto& hit : (*pa_hits)[
static_cast<std::size_t
>(ns)]) {
104 setHeaderEnergyPosition(clu, shower, 1);
108 if (idet == kIdetHPC) {
111 const int nlay = shower.integer(5);
112 const int patt = shower.integer(6);
113 if (nlay > 0 && nlay <= kMaxHpcLayers) {
118 for (
int nl = 1; nl <= kMaxHpcLayers && filled < nlay; ++nl) {
119 const bool hit = ((patt >> (nl - 1)) & 1) != 0;
123 layerE = shower.real(6 + filled);
125 clu.addToSubdetectorEnergies(layerE);
132 pfo.addToClusters(clu);
133 shower = shower.subRecord(stride);
140void walkHCNC(
int lpa,
141 edm4hep::MutableReconstructedParticle pfo,
142 edm4hep::ClusterCollection& cluCol)
148 const int nshowr = hcnc->integer(2);
149 if (nshowr <= 0)
return;
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);
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));
170 pfo.addToClusters(clu);
171 shower = shower.subRecord(5 + 2 * std::max(0, nlay));
178void walkHCAL(
int lpa,
179 edm4hep::MutableReconstructedParticle pfo,
180 edm4hep::ClusterCollection& cluCol)
186 const int nshowr = hcal->integer(2);
188 for (
int ns = 1; ns <= nshowr; ++ns) {
189 const auto shower = hcal->subRecord(
"HCAL.SHOWER", ns);
192 auto clu = cluCol.create();
193 clu.setType(kTypeBitHCAL);
194 setHeaderEnergyPosition(clu, *shower, 3);
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));
208 pfo.addToClusters(clu);
216 edm4hep::ClusterCollection emncCol;
217 edm4hep::ClusterCollection hcncCol;
218 edm4hep::ClusterCollection hcalCol;
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];
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)];
232 walkEMNC(lpa, pfo, emncCol, pa_hits);
233 walkHCNC(lpa, pfo, hcncCol);
234 walkHCAL(lpa, pfo, hcalCol);
const Coll & put(Coll &&coll, std::string_view bank, std::string_view readable, Provenance prov)
std::optional< Record > find(int lpa, const char *name)