Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:07:18

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2022, 2023, Chao Peng, Thomas Britton, Christopher Dilks, Luigi Dello Stritto
0003 
0004 /*  General PhotoMultiplier Digitization
0005  *
0006  *  Apply the given quantum efficiency for photon detection
0007  *  Converts the number of detected photons to signal amplitude
0008  *
0009  *  Author: Chao Peng (ANL)
0010  *  Date: 10/02/2020
0011  *
0012  *  Ported from Juggler by Thomas Britton (JLab)
0013  */
0014 
0015 #include "PhotoMultiplierHitDigi.h"
0016 
0017 #include <Evaluator/DD4hepUnits.h>
0018 #include <algorithm>
0019 #include <algorithms/logger.h>
0020 #include <cmath>
0021 #include <edm4hep/Vector3d.h>
0022 #include <fmt/core.h>
0023 #include <gsl/pointers>
0024 #include <iterator>
0025 #include <podio/ObjectID.h>
0026 
0027 #include "algorithms/digi/PhotoMultiplierHitDigiConfig.h"
0028 
0029 namespace eicrecon {
0030 
0031 //------------------------
0032 // init
0033 //------------------------
0034 void PhotoMultiplierHitDigi::init() {
0035   // print the configuration parameters
0036   debug() << m_cfg << endmsg;
0037 
0038   // initialize quantum efficiency table
0039   qe_init();
0040 }
0041 
0042 //------------------------
0043 // process
0044 //------------------------
0045 void PhotoMultiplierHitDigi::process(const PhotoMultiplierHitDigi::Input& input,
0046                                      const PhotoMultiplierHitDigi::Output& output) const {
0047   const auto [headers, sim_hits] = input;
0048   auto [raw_hits, hit_assocs]    = output;
0049 
0050   // local random generator
0051   auto seed = m_uid.getUniqueID(*headers, name());
0052   std::default_random_engine generator(seed);
0053   std::normal_distribution<double> gaussian;
0054   std::uniform_real_distribution<double> uniform;
0055 
0056   trace("{:=^70}", " call PhotoMultiplierHitDigi::process ");
0057   std::unordered_map<CellIDType, std::vector<HitData>> hit_groups;
0058   // collect the photon hit in the same cell
0059   // calculate signal
0060   trace("{:-<70}", "Loop over simulated hits ");
0061   for (std::size_t sim_hit_index = 0; sim_hit_index < sim_hits->size(); sim_hit_index++) {
0062     const auto& sim_hit = sim_hits->at(sim_hit_index);
0063     auto edep_eV        = sim_hit.getEDep() *
0064                    1e9; // [GeV] -> [eV] // FIXME: use common unit converters, when available
0065     auto id = sim_hit.getCellID();
0066     trace("hit: pixel id={:#018X}  edep = {} eV", id, edep_eV);
0067 
0068     // overall safety factor
0069     if (uniform(generator) > m_cfg.safetyFactor) {
0070       continue;
0071     }
0072 
0073     // quantum efficiency
0074     if (m_cfg.enableQuantumEfficiency and !qe_pass(edep_eV, uniform(generator))) {
0075       continue;
0076     }
0077 
0078     // pixel gap cuts
0079     if (m_cfg.enablePixelGaps) {
0080       auto pos = sim_hit.getPosition();
0081       if (!m_PixelGapMask(
0082               id, dd4hep::Position(pos.x * dd4hep::mm, pos.y * dd4hep::mm, pos.z * dd4hep::mm))) {
0083         continue;
0084       }
0085     }
0086 
0087     // cell time, signal amplitude, truth photon
0088     trace(" -> hit accepted");
0089     trace(" -> MC hit id={}", sim_hit.getObjectID().index);
0090     auto time  = sim_hit.getTime();
0091     double amp = m_cfg.speMean + gaussian(generator) * m_cfg.speError;
0092 
0093     // insert hit to `hit_groups`
0094     InsertHit(hit_groups, id, amp, time, sim_hit_index, generator, gaussian);
0095   }
0096 
0097   // print `hit_groups`
0098   if (level() <= algorithms::LogLevel::kTrace) {
0099     trace("{:-<70}", "Accepted hit groups ");
0100     for (auto& [id, hitVec] : hit_groups) {
0101       for (auto& hit : hitVec) {
0102         trace("hit_group: pixel id={:#018X} -> npe={} signal={} time={}", id, hit.npe, hit.signal,
0103               hit.time);
0104         for (auto i : hit.sim_hit_indices) {
0105           trace(" - MC hit: EDep={}, id={}", sim_hits->at(i).getEDep(),
0106                 sim_hits->at(i).getObjectID().index);
0107         }
0108       }
0109     }
0110   }
0111 
0112   //build noise raw hits
0113   if (m_cfg.enableNoise) {
0114     trace("{:=^70}", " BEGIN NOISE INJECTION ");
0115     float p            = m_cfg.noiseRate * m_cfg.noiseTimeWindow;
0116     auto cellID_action = [this, &gaussian, &generator, &hit_groups, &uniform](auto id) {
0117       // cell time, signal amplitude
0118       double amp    = m_cfg.speMean + gaussian(generator) * m_cfg.speError;
0119       TimeType time = m_cfg.noiseTimeWindow * uniform(generator) / dd4hep::ns;
0120 
0121       // insert in `hit_groups`, or if the pixel already has a hit, update `npe` and `signal`
0122       this->InsertHit(hit_groups, id, amp, time,
0123                       0, // not used
0124                       generator, gaussian, true);
0125     };
0126     m_VisitRngCellIDs(cellID_action, p);
0127   }
0128 
0129   // build output `RawTrackerHit` and `MCRecoTrackerHitAssociation` collections
0130   trace("{:-<70}", "Digitized raw hits ");
0131   for (auto& it : hit_groups) {
0132     for (auto& data : it.second) {
0133 
0134       // build `RawTrackerHit`
0135       auto raw_hit = raw_hits->create();
0136       raw_hit.setCellID(it.first);
0137       raw_hit.setCharge(static_cast<decltype(edm4eic::RawTrackerHitData::charge)>(data.signal));
0138       raw_hit.setTimeStamp(static_cast<decltype(edm4eic::RawTrackerHitData::timeStamp)>(
0139           data.time / m_cfg.timeResolution));
0140       trace("raw_hit: cellID={:#018X} -> charge={} timeStamp={}", raw_hit.getCellID(),
0141             raw_hit.getCharge(), raw_hit.getTimeStamp());
0142 
0143       // build `MCRecoTrackerHitAssociation` (for non-noise hits only)
0144       if (!data.sim_hit_indices.empty()) {
0145         for (auto i : data.sim_hit_indices) {
0146           auto hit_assoc = hit_assocs->create();
0147           hit_assoc.setWeight(1.0 / data.sim_hit_indices.size()); // not used
0148           hit_assoc.setRawHit(raw_hit);
0149           hit_assoc.setSimHit(sim_hits->at(i));
0150         }
0151       }
0152     }
0153   }
0154 }
0155 
0156 void PhotoMultiplierHitDigi::qe_init() {
0157   // get quantum efficiency table
0158   qeff.clear();
0159   auto hc = dd4hep::h_Planck * dd4hep::c_light / (dd4hep::eV * dd4hep::nm); // [eV*nm]
0160   for (const auto& [wl, qe] : m_cfg.quantumEfficiency) {
0161     qeff.emplace_back(hc / wl, qe); // convert wavelength [nm] -> energy [eV]
0162   }
0163 
0164   // sort quantum efficiency data first
0165   std::sort(qeff.begin(), qeff.end(),
0166             [](const std::pair<double, double>& v1, const std::pair<double, double>& v2) {
0167               return v1.first < v2.first;
0168             });
0169 
0170   // print the table
0171   debug("{:-^60}", " Quantum Efficiency vs. Energy ");
0172   for (auto& [en, qe] : qeff) {
0173     debug("  {:>10.4} {:<}", en, qe);
0174   }
0175   trace("{:=^60}", "");
0176 
0177   if (m_cfg.enableQuantumEfficiency) {
0178     // sanity checks
0179     if (qeff.empty()) {
0180       qeff = {{2.6, 0.3}, {7.0, 0.3}};
0181       warning("Invalid quantum efficiency data provided, using default values {} {:.2f} {} {:.2f} "
0182               "{} {:.2f} {} {:.2f} {}",
0183               "{{", qeff.front().first, ",", qeff.front().second, "},{", qeff.back().first, ",",
0184               qeff.back().second, "}}");
0185     }
0186     if (qeff.front().first > 3.0) {
0187       warning("Quantum efficiency data start from {:.2f} {}", qeff.front().first,
0188               " eV, maybe you are using wrong units?");
0189     }
0190     if (qeff.back().first < 3.0) {
0191       warning("Quantum efficiency data end at {:.2f} {}", qeff.back().first,
0192               " eV, maybe you are using wrong units?");
0193     }
0194   }
0195 }
0196 
0197 template <class RndmIter, typename T, class Compare>
0198 RndmIter PhotoMultiplierHitDigi::interval_search(RndmIter beg, RndmIter end, const T& val,
0199                                                  Compare comp) const {
0200   // special cases
0201   auto dist = std::distance(beg, end);
0202   if ((dist < 2) || (comp(*beg, val) > 0) || (comp(*std::prev(end), val) < 0)) {
0203     return end;
0204   }
0205   auto mid = std::next(beg, dist / 2);
0206 
0207   while (mid != end) {
0208     if (comp(*mid, val) == 0) {
0209       return mid;
0210     }
0211     if (comp(*mid, val) > 0) {
0212       end = mid;
0213     } else {
0214       beg = std::next(mid);
0215     }
0216     mid = std::next(beg, std::distance(beg, end) / 2);
0217   }
0218 
0219   if (mid == end || comp(*mid, val) > 0) {
0220     return std::prev(mid);
0221   }
0222   return mid;
0223 }
0224 
0225 bool PhotoMultiplierHitDigi::qe_pass(double ev, double rand) const {
0226   auto it = interval_search(
0227       qeff.begin(), qeff.end(), ev,
0228       [](const std::pair<double, double>& vals, double val) { return vals.first - val; });
0229 
0230   if (it == qeff.end()) {
0231     // warning("{} eV is out of QE data range, assuming 0\% efficiency",ev);
0232     return false;
0233   }
0234 
0235   double prob = it->second;
0236   auto itn    = std::next(it);
0237   if (itn != qeff.end() && (itn->first - it->first != 0)) {
0238     prob = (it->second * (itn->first - ev) + itn->second * (ev - it->first)) /
0239            (itn->first - it->first);
0240   }
0241 
0242   // trace("{} eV, QE: {}\%",ev,prob*100.);
0243   return rand <= prob;
0244 }
0245 
0246 // add a hit to local `hit_groups` data structure
0247 // NOLINTBEGIN(bugprone-easily-swappable-parameters)
0248 void PhotoMultiplierHitDigi::InsertHit(
0249     std::unordered_map<CellIDType, std::vector<HitData>>& hit_groups, CellIDType id, double amp,
0250     TimeType time, std::size_t sim_hit_index, std::default_random_engine& generator,
0251     std::normal_distribution<double>& gaussian,
0252     bool is_noise_hit) const // NOLINTEND(bugprone-easily-swappable-parameters)
0253 {
0254   auto it = hit_groups.find(id);
0255   if (it != hit_groups.end()) {
0256     std::size_t i = 0;
0257     for (auto ghit = it->second.begin(); ghit != it->second.end(); ++ghit, ++i) {
0258       if (std::abs(time - ghit->time) <= (m_cfg.hitTimeWindow)) {
0259         // hit group found, update npe, signal, and list of MC hits
0260         ghit->npe += 1;
0261         ghit->signal += amp;
0262         if (!is_noise_hit) {
0263           ghit->sim_hit_indices.push_back(sim_hit_index);
0264         }
0265         trace(" -> add to group @ {:#018X}: signal={}", id, ghit->signal);
0266         break;
0267       }
0268     }
0269     // no hits group found
0270     if (i >= it->second.size()) {
0271       auto sig = amp + m_cfg.pedMean + m_cfg.pedError * gaussian(generator);
0272       decltype(HitData::sim_hit_indices) indices;
0273       if (!is_noise_hit) {
0274         indices.push_back(sim_hit_index);
0275       }
0276       hit_groups.insert(
0277           {id, {HitData{.npe = 1, .signal = sig, .time = time, .sim_hit_indices = indices}}});
0278       trace(" -> no group found,");
0279       trace("    so new group @ {:#018X}: signal={}", id, sig);
0280     }
0281   } else {
0282     auto sig = amp + m_cfg.pedMean + m_cfg.pedError * gaussian(generator);
0283     decltype(HitData::sim_hit_indices) indices;
0284     if (!is_noise_hit) {
0285       indices.push_back(sim_hit_index);
0286     }
0287     hit_groups.insert({id, {HitData{1, sig, time, indices}}});
0288     trace(" -> new group @ {:#018X}: signal={}", id, sig);
0289   }
0290 }
0291 
0292 } // namespace eicrecon