File indexing completed on 2026-11-28 09:20:47
0001
0002
0003
0004 #include <edm4eic/unit_system.h>
0005 #include <edm4hep/Vector3d.h>
0006 #include <edm4hep/utils/vector_utils.h>
0007 #include <podio/RelationRange.h>
0008 #include <algorithm>
0009 #include <cmath>
0010 #include <cstdlib>
0011
0012 #include "TruthLeptonLine.h"
0013
0014 namespace eicrecon {
0015
0016 namespace {
0017
0018
0019
0020 constexpr std::size_t max_line_length = 100;
0021
0022
0023
0024 constexpr double max_mass_per_nucleon = 1.5 * edm4eic::unit::GeV;
0025
0026 }
0027
0028 std::optional<edm4hep::MCParticle>
0029 find_signal_beam_lepton(const edm4hep::MCParticleCollection& mcparts) {
0030 std::optional<edm4hep::MCParticle> beam;
0031 for (const auto& p : mcparts) {
0032 const auto status = p.getGeneratorStatus();
0033 if (status <= 0 || status % 1000 != 4 || std::abs(p.getPDG()) != 11) {
0034 continue;
0035 }
0036 if (!beam.has_value() || status_base(status) < status_base(beam->getGeneratorStatus())) {
0037 beam = p;
0038 }
0039 }
0040 return beam;
0041 }
0042
0043 std::optional<edm4hep::MCParticle> find_beam_hadron(const edm4hep::MCParticleCollection& mcparts,
0044 int32_t base) {
0045 for (const auto& p : mcparts) {
0046 const auto pdg = p.getPDG();
0047 if (p.getGeneratorStatus() == base + 4 && (pdg == 2212 || pdg == 2112 || is_ion(pdg))) {
0048 return p;
0049 }
0050 }
0051 return std::nullopt;
0052 }
0053
0054 std::optional<LeptonLineTruth>
0055 find_lepton_line_truth(const edm4hep::MCParticleCollection& mcparts) {
0056 const auto beam = find_signal_beam_lepton(mcparts);
0057 if (!beam.has_value()) {
0058 return std::nullopt;
0059 }
0060
0061 LeptonLineTruth truth;
0062 const int32_t base = status_base(beam->getGeneratorStatus());
0063 truth.status_base = base;
0064
0065
0066 const auto in_interaction = [base](const edm4hep::MCParticle& p) {
0067 return p.getGeneratorStatus() != 0 && status_base(p.getGeneratorStatus()) == base;
0068 };
0069
0070
0071 const int32_t pdg_lepton = beam->getPDG();
0072 const int32_t pdg_neutrino = pdg_lepton + (pdg_lepton > 0 ? 1 : -1);
0073 truth.line.push_back(*beam);
0074 while (truth.line.size() < max_line_length) {
0075 std::optional<edm4hep::MCParticle> next;
0076 for (const auto& d : truth.line.back().getDaughters()) {
0077 if (!in_interaction(d) || (d.getPDG() != pdg_lepton && d.getPDG() != pdg_neutrino)) {
0078 continue;
0079 }
0080 if (!next.has_value() || d.getEnergy() > next->getEnergy()) {
0081 next = d;
0082 }
0083 }
0084 if (!next.has_value()) {
0085 break;
0086 }
0087 truth.line.push_back(*next);
0088 }
0089 if (truth.line.size() < 2 || truth.line.back().getGeneratorStatus() != base + 1) {
0090 return std::nullopt;
0091 }
0092
0093 truth.beam_hadron = find_beam_hadron(mcparts, base);
0094
0095
0096 std::optional<std::size_t> boson_parent;
0097 for (std::size_t i = 0; i < truth.line.size(); ++i) {
0098 const auto status = truth.line[i].getGeneratorStatus() - base;
0099 if (status == 21 && !truth.hard_in.has_value()) {
0100 truth.hard_in = i;
0101 }
0102 if (status == 23 && truth.hard_in.has_value() && !truth.hard_out.has_value()) {
0103 truth.hard_out = i;
0104 }
0105 for (const auto& d : truth.line[i].getDaughters()) {
0106 if (!in_interaction(d)) {
0107 continue;
0108 }
0109 const auto pdg = std::abs(d.getPDG());
0110
0111 if ((pdg == 22 || pdg == 23 || pdg == 24) && d.getMass() < 0 &&
0112 d.getGeneratorStatus() != base + 1) {
0113 if (!truth.boson.has_value()) {
0114 truth.boson = d;
0115 boson_parent = i;
0116 }
0117 continue;
0118 }
0119 if (pdg == 22 && std::ranges::none_of(truth.photons, [&d](const auto& photon) {
0120 return photon.particle.getObjectID() == d.getObjectID();
0121 })) {
0122 truth.photons.push_back({d, i});
0123 }
0124 }
0125 }
0126
0127
0128
0129 if (truth.hard_in.has_value() && !truth.hard_out.has_value() &&
0130 *truth.hard_in + 1 < truth.line.size()) {
0131 truth.hard_out = *truth.hard_in + 1;
0132 }
0133
0134
0135
0136 if (boson_parent.has_value()) {
0137 truth.first_outgoing = *boson_parent + 1;
0138 } else if (truth.hard_out.has_value()) {
0139 truth.first_outgoing = truth.hard_out;
0140 }
0141
0142 return truth;
0143 }
0144
0145 ROOT::Math::PxPyPzEVector four_momentum(const edm4hep::MCParticle& particle, double mass) {
0146 const auto& p = particle.getMomentum();
0147 return {p.x, p.y, p.z, std::hypot(edm4hep::utils::magnitude(p), mass)};
0148 }
0149
0150 ROOT::Math::PxPyPzEVector four_momentum(const edm4hep::MCParticle& particle) {
0151 const auto& p = particle.getMomentum();
0152 return {p.x, p.y, p.z, particle.getEnergy()};
0153 }
0154
0155 BeamPerNucleon beam_per_nucleon(const edm4hep::MCParticle& hadron, double nucleon_mass) {
0156 if (!is_ion(hadron.getPDG())) {
0157 return {.momentum = four_momentum(hadron, nucleon_mass), .mass = nucleon_mass};
0158 }
0159 const auto& p = hadron.getMomentum();
0160 double mass = hadron.getMass();
0161 double scale = 1.0;
0162 if (mass > max_mass_per_nucleon) {
0163 scale = 1.0 / ion_mass_number(hadron.getPDG());
0164 }
0165 const double px = scale * p.x;
0166 const double py = scale * p.y;
0167 const double pz = scale * p.z;
0168 mass *= scale;
0169 return {.momentum = {px, py, pz, std::sqrt(px * px + py * py + pz * pz + mass * mass)},
0170 .mass = mass};
0171 }
0172
0173 }