Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2026 Dmitry Kalinkin
0003 
0004 #include "TwoTrackVertexSeeder.h"
0005 
0006 #include <Acts/Definitions/Common.hpp>
0007 #include <Acts/Utilities/Result.hpp>
0008 #include <Acts/Vertexing/TrackAtVertex.hpp>
0009 #include <Acts/Vertexing/Vertex.hpp>
0010 #include <Acts/Vertexing/VertexingOptions.hpp>
0011 #include <Eigen/Core>
0012 #include <cmath>
0013 #include <functional>
0014 #include <system_error>
0015 #include <vector>
0016 
0017 namespace eicrecon {
0018 
0019 namespace {
0020 
0021   /// Helix through a track's perigee position, parametrised by the transverse
0022   /// path length s
0023   struct Helix {
0024     Acts::Vector3 origin;
0025     double phi0;
0026     double cotTheta;
0027     /// Signed curvature dphi/ds, zero for a straight line
0028     double h;
0029 
0030     Acts::Vector3 position(double s) const {
0031       if (std::abs(h * s) < 1e-9) {
0032         return origin + s * Acts::Vector3(std::cos(phi0), std::sin(phi0), cotTheta);
0033       }
0034       const double phi = phi0 + h * s;
0035       return origin + Acts::Vector3((std::sin(phi) - std::sin(phi0)) / h,
0036                                     (std::cos(phi0) - std::cos(phi)) / h, s * cotTheta);
0037     }
0038 
0039     Acts::Vector3 tangent(double s) const {
0040       const double phi = phi0 + h * s;
0041       return {std::cos(phi), std::sin(phi), cotTheta};
0042     }
0043   };
0044 
0045   Helix makeHelix(const Acts::BoundTrackParameters& track, const Acts::GeometryContext& gctx,
0046                   double bz) {
0047     const double pt = track.transverseMomentum();
0048     // A positive charge in a field along +z bends clockwise
0049     const double h = (pt > 0) ? -track.charge() * bz / pt : 0.;
0050     return {.origin   = track.position(gctx),
0051             .phi0     = track.phi(),
0052             .cotTheta = 1. / std::tan(track.theta()),
0053             .h        = h};
0054   }
0055 
0056 } // namespace
0057 
0058 Acts::Vector3 TwoTrackVertexSeeder::closestApproach(const Acts::BoundTrackParameters& track1,
0059                                                     const Acts::BoundTrackParameters& track2,
0060                                                     const Acts::GeometryContext& gctx,
0061                                                     double bz) const {
0062   const Helix h1 = makeHelix(track1, gctx, bz);
0063   const Helix h2 = makeHelix(track2, gctx, bz);
0064 
0065   // Minimise |h1(s1) - h2(s2)|^2 over the transverse path lengths s1, s2 by
0066   // Gauss-Newton, starting from the perigees (s1 = s2 = 0). Each iteration:
0067   //  1. takes the current points h1(s1), h2(s2), their separation w and the
0068   //     helix tangents t1, t2 (dh/ds, not normalised);
0069   //  2. replaces each helix by its tangent line and finds the steps ds1, ds2
0070   //     minimising |w + ds1 t1 - ds2 t2|^2;
0071   //  3. setting the derivatives to zero gives the linear system
0072   //       d + a ds1 - b ds2 = 0,  e + b ds1 - c ds2 = 0
0073   //     with a = t1.t1, b = t1.t2, c = t2.t2, d = t1.w, e = t2.w;
0074   //  4. moves both points by its solution, s1 += ds1, s2 += ds2.
0075   // At convergence w is perpendicular to both tangents. For straight lines
0076   // step 2 is exact, so nearly straight tracks converge in one step.
0077   double s1 = 0.;
0078   double s2 = 0.;
0079   for (unsigned int i = 0; i < m_cfg.maxIterations; ++i) {
0080     const Acts::Vector3 w  = h1.position(s1) - h2.position(s2);
0081     const Acts::Vector3 t1 = h1.tangent(s1);
0082     const Acts::Vector3 t2 = h2.tangent(s2);
0083     const double a         = t1.dot(t1);
0084     const double b         = t1.dot(t2);
0085     const double c         = t2.dot(t2);
0086     const double d         = t1.dot(w);
0087     const double e         = t2.dot(w);
0088     const double det       = a * c - b * b;
0089     if (det < 1e-12 * a * c) {
0090       // (Nearly) parallel tangents: keep the current points
0091       break;
0092     }
0093     const double ds1 = (b * e - c * d) / det;
0094     const double ds2 = (a * e - b * d) / det;
0095     if (!std::isfinite(ds1) || !std::isfinite(ds2)) {
0096       break;
0097     }
0098     s1 += ds1;
0099     s2 += ds2;
0100     if (std::abs(ds1) < m_cfg.tolerance && std::abs(ds2) < m_cfg.tolerance) {
0101       break;
0102     }
0103   }
0104 
0105   Acts::Vector3 seed = 0.5 * (h1.position(s1) + h2.position(s2));
0106   if (!seed.allFinite()) {
0107     seed = 0.5 * (h1.origin + h2.origin);
0108   }
0109   return seed;
0110 }
0111 
0112 Acts::Result<std::vector<Acts::Vertex>>
0113 TwoTrackVertexSeeder::find(const std::vector<Acts::InputTrack>& trackVector,
0114                            const Acts::VertexingOptions& vertexingOptions,
0115                            Acts::IVertexFinder::State& /*anyState*/) const {
0116   if (trackVector.size() != 2) {
0117     return std::vector<Acts::Vertex>{};
0118   }
0119 
0120   const auto& track1 = Acts::InputTrack::extractParameters(trackVector[0]);
0121   const auto& track2 = Acts::InputTrack::extractParameters(trackVector[1]);
0122   const auto& gctx   = vertexingOptions.geoContext.get();
0123 
0124   // Field at the midpoint of the perigees, assumed uniform along z
0125   auto cache              = m_cfg.bField->makeCache(vertexingOptions.magFieldContext);
0126   const Acts::Vector3 mid = 0.5 * (track1.position(gctx) + track2.position(gctx));
0127   auto field              = m_cfg.bField->getField(mid, cache);
0128   if (!field.ok()) {
0129     return field.error();
0130   }
0131 
0132   Acts::Vertex seed(closestApproach(track1, track2, gctx, (*field)[Acts::eZ]));
0133   seed.setFullCovariance(vertexingOptions.constraint.fullCovariance());
0134   return std::vector<Acts::Vertex>{seed};
0135 }
0136 
0137 Acts::IVertexFinder::State
0138 TwoTrackVertexSeeder::makeState(const Acts::MagneticFieldContext& /*mctx*/) const {
0139   return Acts::IVertexFinder::State{State{}};
0140 }
0141 
0142 void TwoTrackVertexSeeder::setTracksToRemove(
0143     Acts::IVertexFinder::State& /*anyState*/,
0144     const std::vector<Acts::InputTrack>& /*removedTracks*/) const {
0145   // Nothing to do: the seed only depends on the tracks passed to find()
0146 }
0147 
0148 } // namespace eicrecon