Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2026 Wouter Deconinck
0003 
0004 #pragma once
0005 
0006 #include <Math/GenVector/LorentzVector.h>
0007 #include <Math/GenVector/PxPyPzE4D.h>
0008 #include <Math/Vector4Dfwd.h>
0009 #include <edm4hep/MCParticle.h>
0010 #include <edm4hep/MCParticleCollection.h>
0011 #include <cmath>
0012 #include <cstdint>
0013 #include <cstdlib>
0014 #include <optional>
0015 #include <vector>
0016 
0017 namespace eicrecon {
0018 
0019 /// Generator status offset of the interaction a particle belongs to. When several interactions
0020 /// are merged into one frame, background interactions have their generator status shifted by
0021 /// multiples of 1000.
0022 inline int32_t status_base(int32_t status) { return status - status % 1000; }
0023 
0024 /// Whether a PDG code denotes a nucleus or anti-nucleus (+/-10LZZZAAAI)
0025 inline bool is_ion(int32_t pdg) { return std::abs(pdg) >= 1000000000; }
0026 
0027 /// Mass number A of a nucleus or anti-nucleus PDG code (+/-10LZZZAAAI)
0028 inline int32_t ion_mass_number(int32_t pdg) { return (std::abs(pdg) / 10) % 1000; }
0029 
0030 /// A real photon radiated directly from the lepton line
0031 struct LeptonLinePhoton {
0032   edm4hep::MCParticle particle;
0033   /// Index in LeptonLineTruth::line of the lepton the photon was emitted from
0034   std::size_t parent_index;
0035 };
0036 
0037 /// Lepton side of a single DIS interaction, as recorded by the event generator in MCParticles.
0038 ///
0039 /// The lepton line is found by descending from the beam lepton through generator daughters with
0040 /// the same lepton flavor (charged lepton or its neutrino, for charged-current events), so the
0041 /// scattered lepton is the final-state end of the line rather than the first final-state lepton
0042 /// in the record. Supported topologies:
0043 ///  - DJANGOH, BeAGLE: beam lepton -> exchanged spacelike boson + scattered lepton (+ photons),
0044 ///    with FSR photons as daughters of the scattered lepton (DJANGOH 4.6.21) or all photons as
0045 ///    daughters of the beam lepton (DJANGOH 4.6.10);
0046 ///  - Pythia8: beam lepton (4) -> 42 -> 21 (hard incoming) -> 23 (hard outgoing) -> 44 -> 1.
0047 struct LeptonLineTruth {
0048   /// Lepton line, beam lepton first, final-state scattered lepton last
0049   std::vector<edm4hep::MCParticle> line;
0050   /// Beam hadron (nucleon or nucleus) of the same interaction, if found
0051   std::optional<edm4hep::MCParticle> beam_hadron;
0052   /// Exchanged spacelike boson (gamma*, Z, W) emitted from the lepton line, if stored
0053   std::optional<edm4hep::MCParticle> boson;
0054   /// Indices in `line` of the hard-scattering incoming (status 21) and outgoing (status 23)
0055   /// leptons, if stored (Pythia8)
0056   std::optional<std::size_t> hard_in;
0057   std::optional<std::size_t> hard_out;
0058   /// Index in `line` of the first lepton after the hard scattering, if the record identifies it
0059   std::optional<std::size_t> first_outgoing;
0060   /// Real photons radiated directly from the lepton line
0061   std::vector<LeptonLinePhoton> photons;
0062   /// Generator status offset of this interaction
0063   int32_t status_base{0};
0064 
0065   const edm4hep::MCParticle& beam_lepton() const { return line.front(); }
0066   const edm4hep::MCParticle& scattered_lepton() const { return line.back(); }
0067 
0068   /// Whether a photon was emitted after the hard scattering, according to the record
0069   bool is_fsr(const LeptonLinePhoton& photon) const {
0070     return first_outgoing.has_value() && photon.parent_index >= *first_outgoing;
0071   }
0072 };
0073 
0074 /// Find the beam lepton (status 4 electron or positron) of the signal interaction: the one with
0075 /// the lowest generator status offset, and the first in the record among those.
0076 std::optional<edm4hep::MCParticle>
0077 find_signal_beam_lepton(const edm4hep::MCParticleCollection& mcparts);
0078 
0079 /// Find the beam hadron (status 4 proton, neutron or nucleus) with the given generator status
0080 /// offset, first in the record.
0081 std::optional<edm4hep::MCParticle> find_beam_hadron(const edm4hep::MCParticleCollection& mcparts,
0082                                                     int32_t status_base);
0083 
0084 /// Find the lepton line of the signal interaction. Returns std::nullopt when there is no beam
0085 /// lepton, or the lepton line does not end in a final-state lepton.
0086 std::optional<LeptonLineTruth> find_lepton_line_truth(const edm4hep::MCParticleCollection& mcparts);
0087 
0088 /// Four-momentum of a generator particle, using the given mass
0089 ROOT::Math::PxPyPzEVector four_momentum(const edm4hep::MCParticle& particle, double mass);
0090 
0091 /// Four-momentum of a generator particle, using its stored mass
0092 ROOT::Math::PxPyPzEVector four_momentum(const edm4hep::MCParticle& particle);
0093 
0094 /// Beam hadron four-momentum and mass per nucleon
0095 struct BeamPerNucleon {
0096   ROOT::Math::PxPyPzEVector momentum;
0097   double mass;
0098 };
0099 
0100 /// Four-momentum and mass per nucleon of a beam hadron. Nucleons use `nucleon_mass` (ignored for
0101 /// nuclei). Nuclei use their stored mass, and are divided by the mass number A unless already
0102 /// stored per nucleon (stored mass below that of the deuteron, as written by e.g. Sartre).
0103 BeamPerNucleon beam_per_nucleon(const edm4hep::MCParticle& hadron, double nucleon_mass);
0104 
0105 /// Inclusive DIS variables
0106 struct DISVariables {
0107   double x;
0108   double Q2;
0109   double W;
0110   double y;
0111   double nu;
0112 };
0113 
0114 /// Inclusive DIS variables from the beam lepton k, the beam hadron P with mass M (per nucleon),
0115 /// and the four-momentum transfer q
0116 inline DISVariables dis_variables(const ROOT::Math::PxPyPzEVector& k,
0117                                   const ROOT::Math::PxPyPzEVector& P, double M,
0118                                   const ROOT::Math::PxPyPzEVector& q) {
0119   const auto q_dot_P = q.Dot(P);
0120   const auto Q2      = -q.Dot(q);
0121   const auto y       = q_dot_P / k.Dot(P);
0122   const auto nu      = q_dot_P / M;
0123   const auto x       = Q2 / (2. * q_dot_P);
0124   const auto W       = std::sqrt(M * M + 2. * q_dot_P - Q2);
0125   return {x, Q2, W, y, nu};
0126 }
0127 
0128 } // namespace eicrecon