Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2026 Dmitry Romanov
0003 
0004 #include <TVector3.h>
0005 #include <edm4eic/Vertex.h>
0006 #include <edm4hep/Vector3f.h>
0007 #include <algorithm>
0008 #include <cmath>
0009 #include <initializer_list>
0010 #include <limits>
0011 #include <tuple>
0012 #include <vector>
0013 
0014 #include "ChargedLambdaReconstruction.h"
0015 
0016 namespace {
0017 
0018 /** Closest approach of two straight lines r + t*d (d normalized).
0019  *
0020  * Each daughter enters as the straight line through its reference point along its
0021  * momentum. The result is exact for these lines and is used only as a cut variable and a
0022  * decay-vertex proxy; how far it represents the real decay vertex is described under
0023  * "Approximations and domain of applicability" in ChargedLambdaReconstruction.h. To be
0024  * revisited once far-forward candidates carry trajectories and the common vertexing tools
0025  * can be applied to them.
0026  *
0027  * Returns the distance and the midpoint between the two closest points. For
0028  * near-parallel lines (a degenerate pairing, not a physical V0) the closest points
0029  * are ill-defined; fall back to the perpendicular distance between the lines and the
0030  * midpoint of the reference points. */
0031 struct LineApproach {
0032   double distance;
0033   TVector3 midpoint;
0034 };
0035 
0036 LineApproach closestApproach(const TVector3& r1, const TVector3& d1, const TVector3& r2,
0037                              const TVector3& d2) {
0038   const TVector3 cross = d1.Cross(d2);
0039   const double denom   = cross.Mag2();
0040   const TVector3 dr    = r2 - r1;
0041   if (denom < std::numeric_limits<float>::epsilon()) {
0042     const TVector3 perp = dr - dr.Dot(d1) * d1;
0043     return {.distance = perp.Mag(), .midpoint = 0.5 * (r1 + r2)};
0044   }
0045   const double t1       = dr.Cross(d2).Dot(cross) / denom;
0046   const double t2       = dr.Cross(d1).Dot(cross) / denom;
0047   const TVector3 point1 = r1 + t1 * d1;
0048   const TVector3 point2 = r2 + t2 * d2;
0049   return {.distance = (point2 - point1).Mag(), .midpoint = 0.5 * (point1 + point2)};
0050 }
0051 
0052 } // namespace
0053 
0054 namespace eicrecon {
0055 
0056 void ChargedLambdaReconstruction::init() {
0057   const double mass_min = std::max(0.0, m_lambda_mass - m_cfg.massWindow);
0058   const double mass_max = m_lambda_mass + m_cfg.massWindow;
0059   m_mass2_min           = mass_min * mass_min;
0060   m_mass2_max           = mass_max * mass_max;
0061 }
0062 
0063 void ChargedLambdaReconstruction::process(const ChargedLambdaReconstruction::Input& input,
0064                                           const ChargedLambdaReconstruction::Output& output) const {
0065   const auto [charged_particles, roman_pots, off_momentum] = input;
0066   auto [out_lambdas]                                       = output;
0067 
0068   using ParticleT = edm4eic::ReconstructedParticle;
0069 
0070   // --------------------------------------------------------------------------
0071   // Collect the daughter pools. Roman-Pot and off-momentum candidates are
0072   // transfer-matrix reconstructions under the proton hypothesis, so all of them
0073   // enter the proton pool; tracking candidates are routed by charge.
0074   // --------------------------------------------------------------------------
0075 
0076   std::vector<ParticleT> protons;
0077   std::vector<ParticleT> pions;
0078 
0079   for (const auto& particle : *charged_particles) {
0080     if (particle.getCharge() > 0) {
0081       protons.push_back(particle);
0082     } else if (particle.getCharge() < 0) {
0083       pions.push_back(particle);
0084     }
0085   }
0086   for (const edm4eic::ReconstructedParticleCollection* far_forward_coll :
0087        {roman_pots.get(), off_momentum.get()}) {
0088     for (const auto& particle : *far_forward_coll) {
0089       protons.push_back(particle);
0090     }
0091   }
0092 
0093   if (protons.empty() || pions.empty()) {
0094     debug("no pairs to build: {} proton candidates, {} pion candidates", protons.size(),
0095           pions.size());
0096     return;
0097   }
0098 
0099   // --------------------------------------------------------------------------
0100   // Evaluate every (p, pi-) pairing with the V0 cut chain.
0101   // --------------------------------------------------------------------------
0102 
0103   for (const auto& proton : protons) {
0104     const auto proton_mom = proton.getMomentum();
0105     const auto proton_ref = proton.getReferencePoint();
0106     const TVector3 p_mom(proton_mom.x, proton_mom.y, proton_mom.z);
0107     const TVector3 p_ref(proton_ref.x, proton_ref.y, proton_ref.z);
0108     const double p_mag = p_mom.Mag();
0109     if (p_mag < std::numeric_limits<float>::epsilon()) {
0110       continue;
0111     }
0112 
0113     for (const auto& pion : pions) {
0114       const auto pion_mom = pion.getMomentum();
0115       const auto pion_ref = pion.getReferencePoint();
0116       const TVector3 pi_mom(pion_mom.x, pion_mom.y, pion_mom.z);
0117       const TVector3 pi_ref(pion_ref.x, pion_ref.y, pion_ref.z);
0118       const double pi_mag = pi_mom.Mag();
0119       if (pi_mag < std::numeric_limits<float>::epsilon()) {
0120         continue;
0121       }
0122 
0123       const double opening = p_mom.Angle(pi_mom);
0124       if (opening > m_cfg.openingAngleMax) {
0125         continue;
0126       }
0127 
0128       const TVector3 pair_mom = p_mom + pi_mom;
0129       if (pair_mom.Theta() > m_cfg.pairThetaMax) {
0130         continue;
0131       }
0132 
0133       const auto approach =
0134           closestApproach(p_ref, p_mom.Unit(), pi_ref, pi_mom.Unit()); // [mm], EDM units
0135       if (approach.distance > m_cfg.dcaMax) {
0136         continue;
0137       }
0138       if (approach.midpoint.Z() < m_cfg.vertexZMin || approach.midpoint.Z() > m_cfg.vertexZMax) {
0139         continue;
0140       }
0141 
0142       if (approach.midpoint.Mag() > std::numeric_limits<float>::epsilon() &&
0143           pair_mom.Angle(approach.midpoint) > m_cfg.pointingMax) {
0144         continue;
0145       }
0146 
0147       const double mom_asym = (p_mag - pi_mag) / (p_mag + pi_mag);
0148       if (mom_asym < m_cfg.momAsymMin) {
0149         continue;
0150       }
0151 
0152       // invariant mass under the (m_p, m_pi) hypothesis; input energies are not used
0153       // because tracking-based candidates carry no mass assignment
0154       const double energy = std::hypot(p_mag, m_proton_mass) + std::hypot(pi_mag, m_pion_mass);
0155       const double mass2  = energy * energy - pair_mom.Mag2();
0156       if (mass2 < m_mass2_min || mass2 > m_mass2_max) {
0157         continue;
0158       }
0159       const double mass = std::sqrt(mass2);
0160 
0161       auto lambda = out_lambdas->create();
0162       lambda.setPDG(3122);
0163       lambda.setCharge(0);
0164       lambda.setMass(mass);
0165       lambda.setEnergy(energy);
0166       lambda.setMomentum({static_cast<float>(pair_mom.X()), static_cast<float>(pair_mom.Y()),
0167                           static_cast<float>(pair_mom.Z())});
0168       lambda.setReferencePoint({static_cast<float>(approach.midpoint.X()),
0169                                 static_cast<float>(approach.midpoint.Y()),
0170                                 static_cast<float>(approach.midpoint.Z())});
0171       lambda.addToParticles(proton);
0172       lambda.addToParticles(pion);
0173 
0174       trace("Lambda candidate: m = {:.4f} GeV, p = {:.2f} GeV, decay-z proxy = {:.0f} mm, dca = "
0175             "{:.1f} mm, opening = {:.1f} mrad",
0176             mass, pair_mom.Mag(), approach.midpoint.Z(), approach.distance, opening * 1e3);
0177     }
0178   }
0179 
0180   debug("built {} Lambda candidates from {} proton and {} pion candidates", out_lambdas->size(),
0181         protons.size(), pions.size());
0182 }
0183 
0184 } // namespace eicrecon