File indexing completed on 2026-11-28 09:20:47
0001
0002
0003
0004 #include "Truthiness.h"
0005
0006 #include <edm4eic/Cov4f.h>
0007 #include <edm4eic/MCRecoParticleAssociationCollection.h>
0008 #include <edm4eic/ReconstructedParticleCollection.h>
0009 #include <edm4hep/MCParticleCollection.h>
0010 #include <edm4hep/Vector3d.h>
0011 #include <edm4hep/Vector3f.h>
0012 #include <edm4hep/utils/vector_utils.h>
0013 #include <cmath>
0014 #include <set>
0015 #include <tuple>
0016 #include <vector>
0017
0018 #include "algorithms/reco/TruthinessConfig.h"
0019
0020 #include <edm4eic/TruthinessCollection.h>
0021 #include <edm4eic/TruthinessContribution.h>
0022
0023 namespace eicrecon {
0024
0025 void Truthiness::process(const Truthiness::Input& input,
0026 [[maybe_unused]] const Truthiness::Output& output) const {
0027
0028 const auto [mc_particles, rc_particles, associations] = input;
0029
0030 double truthiness = 0.0;
0031 double total_pid_contribution = 0.0;
0032 double total_energy_contribution = 0.0;
0033 double total_momentum_contribution = 0.0;
0034
0035
0036 std::set<edm4hep::MCParticle> associated_mc_particles;
0037 std::set<edm4eic::ReconstructedParticle> associated_rc_particles;
0038
0039
0040 std::vector<edm4eic::TruthinessContribution> assoc_truthiness_vec;
0041 assoc_truthiness_vec.reserve(associations->size());
0042
0043
0044 for (const auto& assoc : *associations) {
0045 const auto& mc_part = assoc.getSim();
0046 const auto& rc_part = assoc.getRec();
0047
0048
0049 associated_mc_particles.insert(mc_part);
0050 associated_rc_particles.insert(rc_part);
0051
0052
0053 const auto mc_momentum = mc_part.getMomentum();
0054 const auto rc_momentum = rc_part.getMomentum();
0055
0056 const double mc_p = edm4hep::utils::magnitude(mc_momentum);
0057 const double mc_energy = std::sqrt(mc_p * mc_p + mc_part.getMass() * mc_part.getMass());
0058 const double rc_energy = rc_part.getEnergy();
0059
0060
0061 const auto rc_cov = rc_part.getCovMatrix();
0062
0063
0064 const bool energy_has_cov = (rc_cov.tt > 0.0);
0065 const double energy_error =
0066 energy_has_cov ? std::sqrt(rc_cov.tt) : m_cfg.defaultEnergyResolution;
0067 if (!energy_has_cov) {
0068 trace(" Energy uncertainty using default: {:.3f} GeV (covMatrix.tt = {})",
0069 m_cfg.defaultEnergyResolution, rc_cov.tt);
0070 }
0071
0072
0073 const bool px_has_cov = (rc_cov.xx > 0.0);
0074 const bool py_has_cov = (rc_cov.yy > 0.0);
0075 const bool pz_has_cov = (rc_cov.zz > 0.0);
0076 const double px_error = px_has_cov ? std::sqrt(rc_cov.xx) : m_cfg.defaultMomentumResolution;
0077 const double py_error = py_has_cov ? std::sqrt(rc_cov.yy) : m_cfg.defaultMomentumResolution;
0078 const double pz_error = pz_has_cov ? std::sqrt(rc_cov.zz) : m_cfg.defaultMomentumResolution;
0079 if (!px_has_cov || !py_has_cov || !pz_has_cov) {
0080 trace(" Momentum uncertainty using default for [{},{},{}]: {:.3f} GeV (covMatrix: xx={}, "
0081 "yy={}, zz={})",
0082 px_has_cov ? "cov" : "def", py_has_cov ? "cov" : "def", pz_has_cov ? "cov" : "def",
0083 m_cfg.defaultMomentumResolution, rc_cov.xx, rc_cov.yy, rc_cov.zz);
0084 }
0085
0086
0087 const double energy_diff = mc_energy - rc_energy;
0088 const double energy_chi2 = (energy_diff * energy_diff) / (energy_error * energy_error);
0089 const double energy_penalty = std::sqrt(energy_chi2);
0090
0091
0092 const double px_diff = mc_momentum.x - rc_momentum.x;
0093 const double py_diff = mc_momentum.y - rc_momentum.y;
0094 const double pz_diff = mc_momentum.z - rc_momentum.z;
0095 const double px_chi2 = (px_diff * px_diff) / (px_error * px_error);
0096 const double py_chi2 = (py_diff * py_diff) / (py_error * py_error);
0097 const double pz_chi2 = (pz_diff * pz_diff) / (pz_error * pz_error);
0098 const double momentum_chi2 = px_chi2 + py_chi2 + pz_chi2;
0099 const double momentum_penalty = std::sqrt(momentum_chi2);
0100
0101
0102 const double pdg_penalty_base = (mc_part.getPDG() != rc_part.getPDG()) ? 1.0 : 0.0;
0103 const double pdg_penalty = m_cfg.pidPenaltyWeight * pdg_penalty_base;
0104
0105 const double assoc_penalty = energy_penalty + momentum_penalty + pdg_penalty;
0106
0107 trace("Association: MC PDG={} (E={:.3f}, p=[{:.3f},{:.3f},{:.3f}]) <-> "
0108 "RC PDG={} (E={:.3f}±{:.3f}, p=[{:.3f}±{:.3f},{:.3f}±{:.3f},{:.3f}±{:.3f}])",
0109 mc_part.getPDG(), mc_energy, mc_momentum.x, mc_momentum.y, mc_momentum.z,
0110 rc_part.getPDG(), rc_energy, energy_error, rc_momentum.x, px_error, rc_momentum.y,
0111 py_error, rc_momentum.z, pz_error);
0112 trace(" Energy penalty: {:.6f} (χ²={:.3f}), Momentum penalty: {:.6f} (χ²={:.3f}), PDG "
0113 "penalty: {:.3f}",
0114 energy_penalty, energy_chi2, momentum_penalty, momentum_chi2, pdg_penalty);
0115
0116 truthiness += assoc_penalty;
0117 total_pid_contribution += pdg_penalty;
0118 total_energy_contribution += energy_penalty;
0119 total_momentum_contribution += momentum_penalty;
0120
0121 assoc_truthiness_vec.push_back({.pid = static_cast<float>(pdg_penalty),
0122 .energy = static_cast<float>(energy_penalty),
0123 .momentum = static_cast<float>(momentum_penalty)});
0124 }
0125
0126
0127 int unassociated_mc_count = 0;
0128 std::vector<edm4hep::MCParticle> unassociated_mc_vec;
0129 for (const auto& mc_part : *mc_particles) {
0130 if (mc_part.getGeneratorStatus() == 1 && mc_part.getCharge() != 0.0) {
0131 if (!associated_mc_particles.contains(mc_part)) {
0132 unassociated_mc_count++;
0133 unassociated_mc_vec.push_back(mc_part);
0134 trace("Unassociated MC particle: PDG={}, charge={:.1f}, status={}", mc_part.getPDG(),
0135 mc_part.getCharge(), mc_part.getGeneratorStatus());
0136 }
0137 }
0138 }
0139 const double mc_penalty =
0140 m_cfg.unassociatedMCPenaltyWeight * static_cast<double>(unassociated_mc_count);
0141 trace("Unassociated charged MC particles (status 1): {} (penalty: {:.2f}, weight: {:.2f})",
0142 unassociated_mc_count, mc_penalty, m_cfg.unassociatedMCPenaltyWeight);
0143 truthiness += mc_penalty;
0144
0145
0146 int unassociated_rc_count = 0;
0147 std::vector<edm4eic::ReconstructedParticle> unassociated_rc_vec;
0148 for (const auto& rc_part : *rc_particles) {
0149 if (!associated_rc_particles.contains(rc_part)) {
0150 unassociated_rc_count++;
0151 unassociated_rc_vec.push_back(rc_part);
0152 trace("Unassociated reconstructed particle: PDG={}, E={:.3f}, p=[{:.3f},{:.3f},{:.3f}]",
0153 rc_part.getPDG(), rc_part.getEnergy(), rc_part.getMomentum().x, rc_part.getMomentum().y,
0154 rc_part.getMomentum().z);
0155 }
0156 }
0157 const double rc_penalty =
0158 m_cfg.unassociatedRecoPenaltyWeight * static_cast<double>(unassociated_rc_count);
0159 trace("Unassociated reconstructed particles: {} (penalty: {:.2f}, weight: {:.2f})",
0160 unassociated_rc_count, rc_penalty, m_cfg.unassociatedRecoPenaltyWeight);
0161 truthiness += rc_penalty;
0162
0163
0164
0165 {
0166 std::lock_guard<std::mutex> lock(m_stats_mutex);
0167 const auto event_count = ++m_event_count;
0168 m_average_truthiness += (truthiness - m_average_truthiness) / event_count;
0169 }
0170
0171
0172 debug("Event truthiness: {:.6f} (from {} associations, {} unassociated MC, {} unassociated RC)",
0173 truthiness, associations->size(), unassociated_mc_count, unassociated_rc_count);
0174 trace(" Total PID contribution: {:.6f}", total_pid_contribution);
0175 trace(" Total energy contribution: {:.6f}", total_energy_contribution);
0176 trace(" Total momentum contribution: {:.6f}", total_momentum_contribution);
0177
0178
0179 const auto [truthiness_output] = output;
0180 auto truthiness_obj = truthiness_output->create();
0181
0182
0183 truthiness_obj.setTruthiness(static_cast<float>(truthiness));
0184 truthiness_obj.setAssociationContribution(
0185 {.pid = static_cast<float>(total_pid_contribution),
0186 .energy = static_cast<float>(total_energy_contribution),
0187 .momentum = static_cast<float>(total_momentum_contribution)});
0188 truthiness_obj.setUnassociatedMCParticlesContribution(static_cast<float>(mc_penalty));
0189 truthiness_obj.setUnassociatedRecoParticlesContribution(static_cast<float>(rc_penalty));
0190
0191
0192 for (const auto& assoc : *associations) {
0193 truthiness_obj.addToAssociations(assoc);
0194 }
0195 for (const auto& val : assoc_truthiness_vec) {
0196 truthiness_obj.addToAssociationContributions(val);
0197 }
0198
0199
0200 for (const auto& mc_part : unassociated_mc_vec) {
0201 truthiness_obj.addToUnassociatedMCParticles(mc_part);
0202 }
0203 for (const auto& rc_part : unassociated_rc_vec) {
0204 truthiness_obj.addToUnassociatedRecoParticles(rc_part);
0205 }
0206 }
0207
0208 }