File indexing completed on 2026-01-07 09:24:58
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include "MPGDTrackerDigi.h"
0023
0024 #include <DD4hep/Alignments.h>
0025 #include <DD4hep/DetElement.h>
0026 #include <DD4hep/Handle.h>
0027 #include <DD4hep/IDDescriptor.h>
0028 #include <DD4hep/Objects.h>
0029 #include <DD4hep/Readout.h>
0030 #include <DD4hep/VolumeManager.h>
0031 #include <DD4hep/detail/SegmentationsInterna.h>
0032 #include <DDSegmentation/BitFieldCoder.h>
0033 #include <Evaluator/DD4hepUnits.h>
0034 #include <JANA/JException.h>
0035 #include <Math/GenVector/Cartesian3D.h>
0036 #include <Math/GenVector/DisplacementVector3D.h>
0037 #include <Parsers/Primitives.h>
0038
0039 #include <algorithms/geo.h>
0040 #include <algorithms/logger.h>
0041 #include <edm4hep/MCParticleCollection.h>
0042 #include <edm4hep/Vector3d.h>
0043 #include <edm4hep/Vector3f.h>
0044 #include <fmt/core.h>
0045 #include <algorithm>
0046 #include <cmath>
0047 #include <cstdint>
0048 #include <gsl/pointers>
0049 #include <initializer_list>
0050 #include <iterator>
0051 #include <random>
0052 #include <unordered_map>
0053 #include <utility>
0054 #include <vector>
0055
0056 #include "algorithms/digi/MPGDTrackerDigiConfig.h"
0057
0058 using namespace dd4hep;
0059
0060 namespace eicrecon {
0061
0062 void MPGDTrackerDigi::init() {
0063
0064 m_detector = algorithms::GeoSvc::instance().detector();
0065 const dd4hep::BitFieldCoder* m_id_dec = nullptr;
0066 if (m_cfg.readout.empty()) {
0067 throw JException("Readout is empty");
0068 }
0069 try {
0070 m_seg = m_detector->readout(m_cfg.readout).segmentation();
0071 m_id_dec = m_detector->readout(m_cfg.readout).idSpec().decoder();
0072 } catch (...) {
0073 critical("Failed to load ID decoder for \"{}\" readout.", m_cfg.readout);
0074 throw JException("Failed to load ID decoder");
0075 }
0076
0077
0078
0079
0080 if (m_id_dec->get(((CellID)0x3) << 30, "strip") != 0x3) {
0081 critical(R"(Missing or invalid "strip" field in IDDescriptor for "{}"
0082 readout.)",
0083 m_cfg.readout);
0084 throw JException("Invalid IDDescriptor");
0085 }
0086 debug(R"(Find valid "strip" field in IDDescriptor for "{}" readout.)", m_cfg.readout);
0087 }
0088
0089 void MPGDTrackerDigi::process(const MPGDTrackerDigi::Input& input,
0090 const MPGDTrackerDigi::Output& output) const {
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103 const auto [headers, sim_hits] = input;
0104 auto [raw_hits, associations] = output;
0105
0106
0107 auto seed = m_uid.getUniqueID(*headers, name());
0108 std::default_random_engine generator(seed);
0109 std::normal_distribution<double> gaussian;
0110
0111
0112 std::unordered_map<std::uint64_t, edm4eic::MutableRawTrackerHit> cell_hit_map;
0113
0114 const Position dummy(0, 0, 0);
0115 const VolumeManager& volman = m_detector->volumeManager();
0116
0117 using CellIDs = std::pair<CellID, CellID>;
0118 using Sim2IDs = std::vector<CellIDs>;
0119 Sim2IDs sim2IDs;
0120 for (const edm4hep::SimTrackerHit& sim_hit : *sim_hits) {
0121
0122
0123
0124
0125
0126
0127
0128 double time_smearing = gaussian(generator) * m_cfg.timeResolution;
0129 double result_time = sim_hit.getTime() + time_smearing;
0130 auto hit_time_stamp = (std::int32_t)(result_time * 1e3);
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143 const edm4hep::Vector3d& pos = sim_hit.getPosition();
0144 using dd4hep::mm;
0145 Position gpos(pos.x * mm, pos.y * mm, pos.z * mm);
0146 CellID vID =
0147
0148
0149 sim_hit.getCellID();
0150 DetElement local = volman.lookupDetElement(vID);
0151 const auto lpos = local.nominal().worldToLocal(gpos);
0152
0153 CellID stripBitp = ((CellID)0x1) << 30;
0154 CellID vIDp = vID | stripBitp;
0155 CellID cIDp = m_seg->cellID(lpos, dummy, vIDp);
0156
0157 CellID stripBitn = ((CellID)0x2) << 30;
0158 CellID vIDn = vID | stripBitn;
0159 CellID cIDn = m_seg->cellID(lpos, dummy, vIDn);
0160
0161 sim2IDs.emplace_back(cIDp, cIDn);
0162
0163 if (level() >= algorithms::LogLevel::kDebug) {
0164 CellID hIDp = cIDp >> 32;
0165 CellID sIDp = cIDp >> 30 & 0x3;
0166 debug("--------------------");
0167 debug("Hit cellIDp = 0x{:08x}, 0x{:08x} 0x{:02x}", hIDp, vIDp, sIDp);
0168 CellID hIDn = cIDn >> 32;
0169 CellID sIDn = cIDn >> 30 & 0x3;
0170 debug("Hit cellIDn = 0x{:08x}, 0x{:08x} 0x{:02x}", hIDn, vIDn, sIDn);
0171 debug(" position = ({:.2f}, {:.2f}, {:.2f})", sim_hit.getPosition().x,
0172 sim_hit.getPosition().y, sim_hit.getPosition().z);
0173 debug(" xy_radius = {:.2f}", std::hypot(sim_hit.getPosition().x, sim_hit.getPosition().y));
0174 debug(" momentum = ({:.2f}, {:.2f}, {:.2f})", sim_hit.getMomentum().x,
0175 sim_hit.getMomentum().y, sim_hit.getMomentum().z);
0176 debug(" edep = {:.2f}", sim_hit.getEDep());
0177 debug(" time = {:.4f}[ns]", sim_hit.getTime());
0178 debug(" particle time = {}[ns]", sim_hit.getParticle().getTime());
0179 debug(" time smearing: {:.4f}, resulting time = {:.4f} [ns]", time_smearing, result_time);
0180 debug(" hit_time_stamp: {} [~ps]", hit_time_stamp);
0181 }
0182
0183
0184 if (sim_hit.getEDep() < m_cfg.threshold) {
0185 debug(" edep is below threshold of {:.2f} [keV]", m_cfg.threshold / keV);
0186 continue;
0187 }
0188
0189
0190 for (CellID cID : {cIDp, cIDn}) {
0191 if (!cell_hit_map.contains(cID)) {
0192
0193 cell_hit_map[cID] = {
0194 cID, (std::int32_t)std::llround(sim_hit.getEDep() * 1e6),
0195 hit_time_stamp
0196 };
0197 } else {
0198
0199 auto& hit = cell_hit_map[cID];
0200 debug(" Hit already exists in cell ID={}, prev. hit time: {}", cID, hit.getTimeStamp());
0201
0202
0203 hit.setTimeStamp(std::min(hit_time_stamp, hit.getTimeStamp()));
0204
0205
0206 auto charge = hit.getCharge();
0207 hit.setCharge(charge + (std::int32_t)std::llround(sim_hit.getEDep() * 1e6));
0208 }
0209 }
0210 }
0211
0212
0213 for (auto item : cell_hit_map) {
0214 raw_hits->push_back(item.second);
0215 auto sim_it = sim2IDs.cbegin();
0216 for (const auto& sim_hit : *sim_hits) {
0217 CellIDs cIDs = *sim_it++;
0218 for (CellID cID : {cIDs.first, cIDs.second}) {
0219 if (item.first == cID) {
0220
0221 auto hitassoc = associations->create();
0222 hitassoc.setWeight(1.0);
0223 hitassoc.setRawHit(item.second);
0224 hitassoc.setSimHit(sim_hit);
0225 }
0226 }
0227 }
0228 }
0229 }
0230
0231 }