File indexing completed on 2025-07-11 07:53:31
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include "CalorimeterHitDigi.h"
0014
0015 #include <DD4hep/Detector.h>
0016 #include <DD4hep/IDDescriptor.h>
0017 #include <DD4hep/Readout.h>
0018 #include <DD4hep/config.h>
0019 #include <DDSegmentation/BitFieldCoder.h>
0020 #include <Evaluator/DD4hepUnits.h>
0021 #include <algorithms/service.h>
0022 #include <edm4eic/MCRecoCalorimeterHitAssociationCollection.h>
0023 #include <edm4hep/CaloHitContributionCollection.h>
0024 #include <fmt/core.h>
0025 #include <podio/RelationRange.h>
0026 #include <algorithm>
0027 #include <cmath>
0028 #include <cstddef>
0029 #include <gsl/pointers>
0030 #include <limits>
0031 #include <map>
0032 #include <stdexcept>
0033 #include <string>
0034 #include <unordered_map>
0035 #include <utility>
0036 #include <vector>
0037
0038 #include "algorithms/calorimetry/CalorimeterHitDigiConfig.h"
0039 #include "services/evaluator/EvaluatorSvc.h"
0040
0041 using namespace dd4hep;
0042
0043 namespace eicrecon {
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053 void CalorimeterHitDigi::init() {
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070 if (m_cfg.eRes.empty()) {
0071 m_cfg.eRes.resize(3);
0072 } else if (m_cfg.eRes.size() != 3) {
0073 error("Invalid m_cfg.eRes.size()");
0074 throw std::runtime_error("Invalid m_cfg.eRes.size()");
0075 }
0076
0077
0078 tRes = m_cfg.tRes / dd4hep::ns;
0079 stepTDC = dd4hep::ns / m_cfg.resolutionTDC;
0080
0081
0082 if (m_cfg.readout.empty()) {
0083 error("readoutClass is not provided, it is needed to know the fields in readout ids");
0084 throw std::runtime_error("readoutClass is not provided");
0085 }
0086
0087
0088 try {
0089 id_spec = m_geo.detector()->readout(m_cfg.readout).idSpec();
0090 } catch (...) {
0091
0092
0093
0094 debug("Failed to load ID decoder for {}", m_cfg.readout);
0095 throw std::runtime_error(fmt::format("Failed to load ID decoder for {}", m_cfg.readout));
0096 }
0097
0098 decltype(id_mask) id_inverse_mask = 0;
0099
0100 if (!m_cfg.fields.empty()) {
0101 for (auto& field : m_cfg.fields) {
0102 id_inverse_mask |= id_spec.field(field)->mask();
0103 }
0104 debug("ID mask in {:s}: {:#064b}", m_cfg.readout, id_mask);
0105 }
0106 id_mask = ~id_inverse_mask;
0107
0108 std::function hit_to_map = [this](const edm4hep::SimCalorimeterHit& h) {
0109 std::unordered_map<std::string, double> params;
0110 for (const auto& p : id_spec.fields()) {
0111 const std::string& name = p.first;
0112 const dd4hep::IDDescriptor::Field* field = p.second;
0113 params.emplace(name, field->value(h.getCellID()));
0114 trace("{} = {}", name, field->value(h.getCellID()));
0115 }
0116 return params;
0117 };
0118
0119 auto& serviceSvc = algorithms::ServiceSvc::instance();
0120 corrMeanScale =
0121 serviceSvc.service<EvaluatorSvc>("EvaluatorSvc")->compile(m_cfg.corrMeanScale, hit_to_map);
0122
0123 std::map<std::string, readout_enum> readoutTypes{{"simple", kSimpleReadout},
0124 {"poisson_photon", kPoissonPhotonReadout},
0125 {"sipm", kSipmReadout}};
0126 if (not readoutTypes.count(m_cfg.readoutType)) {
0127 error("Invalid readoutType \"{}\"", m_cfg.readoutType);
0128 throw std::runtime_error(fmt::format("Invalid readoutType \"{}\"", m_cfg.readoutType));
0129 }
0130 readoutType = readoutTypes.at(m_cfg.readoutType);
0131 }
0132
0133 void CalorimeterHitDigi::process(const CalorimeterHitDigi::Input& input,
0134 const CalorimeterHitDigi::Output& output) const {
0135
0136 const auto [simhits] = input;
0137 auto [rawhits, rawassocs] = output;
0138
0139
0140 std::unordered_map<uint64_t, std::vector<std::size_t>> merge_map;
0141 std::size_t ix = 0;
0142 for (const auto& ahit : *simhits) {
0143 uint64_t hid = ahit.getCellID() & id_mask;
0144
0145 trace("org cell ID in {:s}: {:#064b}", m_cfg.readout, ahit.getCellID());
0146 trace("new cell ID in {:s}: {:#064b}", m_cfg.readout, hid);
0147
0148 merge_map[hid].push_back(ix);
0149
0150 ix++;
0151 }
0152
0153
0154
0155 for (const auto& [id, ixs] : merge_map) {
0156
0157
0158 edm4hep::MutableRawCalorimeterHit rawhit;
0159 std::vector<edm4eic::MutableMCRecoCalorimeterHitAssociation> rawassocs_staging;
0160
0161 double edep = 0;
0162 double time = std::numeric_limits<double>::max();
0163 double max_edep = 0;
0164 auto leading_hit = (*simhits)[ixs[0]];
0165
0166 for (unsigned long i : ixs) {
0167 auto hit = (*simhits)[i];
0168
0169 double timeC = std::numeric_limits<double>::max();
0170 for (const auto& c : hit.getContributions()) {
0171 if (c.getTime() <= timeC) {
0172 timeC = c.getTime();
0173 }
0174 }
0175 if (timeC > m_cfg.capTime) {
0176 continue;
0177 }
0178 edep += hit.getEnergy();
0179 trace("adding {} \t total: {}", hit.getEnergy(), edep);
0180
0181
0182 if (hit.getEnergy() > max_edep) {
0183 max_edep = hit.getEnergy();
0184 leading_hit = hit;
0185 if (timeC <= time) {
0186 time = timeC;
0187 }
0188 }
0189
0190 edm4eic::MutableMCRecoCalorimeterHitAssociation assoc;
0191 assoc.setRawHit(rawhit);
0192 assoc.setSimHit(hit);
0193 assoc.setWeight(hit.getEnergy());
0194 rawassocs_staging.push_back(assoc);
0195 }
0196 if (time > m_cfg.capTime) {
0197 continue;
0198 }
0199
0200
0201 const double eResRel =
0202 (edep > m_cfg.threshold)
0203 ? m_gaussian(m_generator) *
0204 std::sqrt(std::pow(m_cfg.eRes[0] / std::sqrt(edep), 2) +
0205 std::pow(m_cfg.eRes[1], 2) + std::pow(m_cfg.eRes[2] / (edep), 2))
0206 : 0;
0207
0208 double corrMeanScale_value = corrMeanScale(leading_hit);
0209
0210 double ped = m_cfg.pedMeanADC + m_gaussian(m_generator) * m_cfg.pedSigmaADC;
0211
0212
0213 unsigned long long adc;
0214 unsigned long long tdc = std::llround((time + m_gaussian(m_generator) * tRes) * stepTDC);
0215
0216 if (readoutType == kSimpleReadout) {
0217 adc = std::max(std::llround(ped + edep * corrMeanScale_value * (1.0 + eResRel) /
0218 m_cfg.dyRangeADC * m_cfg.capADC),
0219 0LL);
0220 } else if (readoutType == kPoissonPhotonReadout) {
0221 const long long int n_photons_mean =
0222 edep * m_cfg.lightYield * m_cfg.photonDetectionEfficiency;
0223 std::poisson_distribution<> n_photons_detected_dist(n_photons_mean);
0224 const long long int n_photons_detected = n_photons_detected_dist(m_generator);
0225 const long long int n_max_photons =
0226 m_cfg.dyRangeADC * m_cfg.lightYield * m_cfg.photonDetectionEfficiency;
0227 trace("n_photons_detected {}", n_photons_detected);
0228 adc = std::max(std::llround(ped + n_photons_detected * corrMeanScale_value * (1.0 + eResRel) /
0229 n_max_photons * m_cfg.capADC),
0230 0LL);
0231 } else if (readoutType == kSipmReadout) {
0232 const long long int n_photons = edep * m_cfg.lightYield;
0233 std::binomial_distribution<> n_photons_detected_dist(n_photons,
0234 m_cfg.photonDetectionEfficiency);
0235 const long long int n_photons_detected = n_photons_detected_dist(m_generator);
0236 const long long int n_pixels_fired =
0237 m_cfg.numEffectiveSipmPixels *
0238 (1 - exp(-n_photons_detected / (double)m_cfg.numEffectiveSipmPixels));
0239 const long long int n_max_photons =
0240 m_cfg.dyRangeADC * m_cfg.lightYield * m_cfg.photonDetectionEfficiency;
0241 trace("n_photons_detected {}, n_pixels_fired {}, n_max_photons {}", n_photons_detected,
0242 n_pixels_fired, n_max_photons);
0243 adc = std::max(std::llround(ped + n_pixels_fired * corrMeanScale_value * (1.0 + eResRel) /
0244 n_max_photons * m_cfg.capADC),
0245 0LL);
0246 }
0247
0248 if (edep > 1.e-3)
0249 trace("E sim {} \t adc: {} \t time: {}\t maxtime: {} \t tdc: {} \t corrMeanScale: {}", edep,
0250 adc, time, m_cfg.capTime, tdc, corrMeanScale_value);
0251
0252 rawhit.setCellID(leading_hit.getCellID());
0253 rawhit.setAmplitude(adc > m_cfg.capADC ? m_cfg.capADC : adc);
0254 rawhit.setTimeStamp(tdc);
0255 rawhits->push_back(rawhit);
0256
0257 for (auto& assoc : rawassocs_staging) {
0258 assoc.setWeight(assoc.getWeight() / edep);
0259 rawassocs->push_back(assoc);
0260 }
0261 }
0262 }
0263
0264 }