Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-28 09:20:46

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2026 Wouter Deconinck
0003 
0004 #include <Math/GenVector/LorentzVector.h>
0005 #include <Math/GenVector/PxPyPzE4D.h>
0006 #include <Math/GenVector/VectorUtil.h>
0007 #include <Math/Vector4Dfwd.h>
0008 #include <edm4eic/InclusiveKinematicsCollection.h>
0009 #include <edm4eic/ReconstructedParticleCollection.h>
0010 #include <edm4hep/MCParticleCollection.h>
0011 #include <edm4hep/Vector3d.h>
0012 #include <edm4hep/Vector3f.h>
0013 #include <algorithm>
0014 #include <cmath>
0015 #include <cstdlib>
0016 #include <format>
0017 #include <optional>
0018 #include <stdexcept>
0019 #include <tuple>
0020 #include <vector>
0021 
0022 #include "InclusiveKinematicsTruthDressed.h"
0023 #include "TruthLeptonLine.h"
0024 #include "algorithms/reco/InclusiveKinematicsTruthDressedConfig.h"
0025 
0026 using ROOT::Math::PxPyPzEVector;
0027 
0028 namespace eicrecon {
0029 
0030 void InclusiveKinematicsTruthDressed::init() {
0031   if (m_cfg.coneDeltaR < 0) {
0032     error("Dressing cone size coneDeltaR = {} must not be negative", m_cfg.coneDeltaR);
0033     throw std::runtime_error(
0034         std::format("Dressing cone size coneDeltaR = {} must not be negative", m_cfg.coneDeltaR));
0035   }
0036 }
0037 
0038 void InclusiveKinematicsTruthDressed::process(
0039     const InclusiveKinematicsTruthDressed::Input& input,
0040     const InclusiveKinematicsTruthDressed::Output& output) const {
0041 
0042   const auto [mcparts]                            = input;
0043   auto [kinematics, scattered, scat_constituents] = output;
0044   scat_constituents->setSubsetCollection();
0045 
0046   // Get lepton line of the signal interaction
0047   const auto truth = find_lepton_line_truth(*mcparts);
0048   if (!truth.has_value()) {
0049     debug("No beam electron with a final-state scattered lepton found");
0050     return;
0051   }
0052 
0053   // Get incoming electron beam
0054   static const auto ei_mass = m_particleSvc.particle(11).mass;
0055   const PxPyPzEVector ei    = four_momentum(truth->beam_lepton(), ei_mass);
0056 
0057   // Get incoming hadron beam
0058   if (!truth->beam_hadron.has_value()) {
0059     debug("No beam hadron found");
0060     return;
0061   }
0062   const auto& pi_particle = *truth->beam_hadron;
0063   const auto pi_beam      = beam_per_nucleon(
0064       pi_particle,
0065       is_ion(pi_particle.getPDG()) ? 0. : m_particleSvc.particle(pi_particle.getPDG()).mass);
0066 
0067   // Get bare scattered lepton
0068   const auto& ef_particle     = truth->scattered_lepton();
0069   const auto ef_mass          = m_particleSvc.particle(std::abs(ef_particle.getPDG())).mass;
0070   const PxPyPzEVector ef_bare = four_momentum(ef_particle, ef_mass);
0071 
0072   // Select photons radiated from the scattered lepton (only charged leptons radiate): by
0073   // parentage, when the record has photons emitted after the hard scattering (e.g. DJANGOH
0074   // 4.6.21, Pythia8 with QED showers); otherwise inside the dressing cone around the bare lepton
0075   // (e.g. DJANGOH 4.6.10, which attaches all photons to the beam lepton)
0076   std::vector<edm4hep::MCParticle> fsr_photons;
0077   if (ef_particle.getCharge() != 0) {
0078     const bool use_parentage =
0079         std::any_of(truth->photons.begin(), truth->photons.end(),
0080                     [&truth](const auto& photon) { return truth->is_fsr(photon); });
0081     for (const auto& photon : truth->photons) {
0082       const bool selected = use_parentage
0083                                 ? truth->is_fsr(photon)
0084                                 : ROOT::Math::VectorUtil::DeltaR(four_momentum(photon.particle),
0085                                                                  ef_bare) < m_cfg.coneDeltaR;
0086       if (selected) {
0087         fsr_photons.push_back(photon.particle);
0088       }
0089     }
0090     trace("{} of {} lepton-line photons added by {}", fsr_photons.size(), truth->photons.size(),
0091           use_parentage ? "parentage" : "cone");
0092   }
0093 
0094   // Dressed scattered lepton
0095   PxPyPzEVector ef = ef_bare;
0096   scat_constituents->push_back(ef_particle);
0097   for (const auto& photon : fsr_photons) {
0098     ef += four_momentum(photon);
0099     scat_constituents->push_back(photon);
0100   }
0101 
0102   auto scat = scattered->create();
0103   scat.setType(ef_particle.getGeneratorStatus());
0104   scat.setEnergy(ef.E());
0105   scat.setMomentum(
0106       {static_cast<float>(ef.Px()), static_cast<float>(ef.Py()), static_cast<float>(ef.Pz())});
0107   const auto& vertex = ef_particle.getVertex();
0108   scat.setReferencePoint(
0109       {static_cast<float>(vertex.x), static_cast<float>(vertex.y), static_cast<float>(vertex.z)});
0110   scat.setCharge(ef_particle.getCharge());
0111   scat.setMass(ef.M());
0112   scat.setGoodnessOfPID(1);
0113   scat.setPDG(ef_particle.getPDG());
0114 
0115   // DIS kinematics calculations
0116   const auto q   = ei - ef;
0117   const auto dis = dis_variables(ei, pi_beam.momentum, pi_beam.mass, q);
0118   auto kin       = kinematics->create(dis.x, dis.Q2, dis.W, dis.y, dis.nu);
0119   kin.setScat(scat);
0120 
0121   debug("x,Q2,W,y,nu = {},{},{},{},{}", kin.getX(), kin.getQ2(), kin.getW(), kin.getY(),
0122         kin.getNu());
0123 }
0124 
0125 } // namespace eicrecon