Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-05 07:48:16

0001 // Created by Joe Osborn
0002 // Subject to the terms in the LICENSE file found in the top-level directory.
0003 //
0004 
0005 #include "IterativeVertexFinder.h"
0006 
0007 #include <Acts/Definitions/TrackParametrization.hpp>
0008 #include <Acts/Definitions/Units.hpp>
0009 #include <Acts/EventData/TrackParameters.hpp>
0010 #include <Acts/EventData/TrackProxy.hpp>
0011 #include <Acts/Propagator/EigenStepper.hpp>
0012 #include <Acts/Propagator/Propagator.hpp>
0013 #include <Acts/Propagator/VoidNavigator.hpp>
0014 #include <Acts/Surfaces/Surface.hpp>
0015 #include <Acts/Utilities/Logger.hpp>
0016 #include <Acts/Utilities/Result.hpp>
0017 #include <Acts/Vertexing/FullBilloirVertexFitter.hpp>
0018 #include <Acts/Vertexing/HelicalTrackLinearizer.hpp>
0019 #include <Acts/Vertexing/IVertexFinder.hpp>
0020 #include <Acts/Vertexing/ImpactPointEstimator.hpp>
0021 #include <Acts/Vertexing/IterativeVertexFinder.hpp>
0022 #include <Acts/Vertexing/LinearizedTrack.hpp>
0023 #include <Acts/Vertexing/TrackAtVertex.hpp>
0024 #include <Acts/Vertexing/Vertex.hpp>
0025 #include <Acts/Vertexing/VertexingOptions.hpp>
0026 #include <Acts/Vertexing/ZScanVertexFinder.hpp>
0027 #include <ActsExamples/EventData/Track.hpp>
0028 #include <edm4eic/Cov4f.h>
0029 #include <edm4eic/ReconstructedParticleCollection.h>
0030 #include <edm4eic/Track.h>
0031 #include <edm4eic/TrackParameters.h>
0032 #include <edm4eic/Trajectory.h>
0033 #include <edm4eic/unit_system.h>
0034 #include <edm4hep/Vector2f.h>
0035 #include <edm4hep/Vector4f.h>
0036 #include <podio/RelationRange.h>
0037 #include <spdlog/common.h>
0038 #include <cmath>
0039 #include <string>
0040 #include <tuple>
0041 #include <utility>
0042 #include <vector>
0043 
0044 #include "ActsGeometryProvider.h"
0045 #include "algorithms/tracking/IterativeVertexFinderConfig.h"
0046 #include "extensions/spdlog/SpdlogToActs.h"
0047 
0048 void eicrecon::IterativeVertexFinder::process(const Input& input, const Output& output) const {
0049   const auto [trackStates, tracks, reconParticles] = input;
0050   auto [outputVertices]                            = output;
0051 
0052   // Construct ConstTrackContainer from underlying containers
0053   auto trackStateContainer = std::make_shared<Acts::ConstVectorMultiTrajectory>(*trackStates);
0054   auto trackContainer      = std::make_shared<Acts::ConstVectorTrackContainer>(*tracks);
0055   ActsExamples::ConstTrackContainer constTracks(trackContainer, trackStateContainer);
0056 
0057   using Propagator           = Acts::Propagator<Acts::EigenStepper<>>;
0058   using Linearizer           = Acts::HelicalTrackLinearizer;
0059   using VertexFitter         = Acts::FullBilloirVertexFitter;
0060   using ImpactPointEstimator = Acts::ImpactPointEstimator;
0061   using VertexSeeder         = Acts::ZScanVertexFinder;
0062   using VertexFinder         = Acts::IterativeVertexFinder;
0063   using VertexFinderOptions  = Acts::VertexingOptions;
0064 
0065   // Convert algorithm log level to Acts log level
0066   const auto spdlog_level = static_cast<spdlog::level::level_enum>(this->level());
0067   const auto acts_level   = eicrecon::SpdlogToActsLevel(spdlog_level);
0068   ACTS_LOCAL_LOGGER(Acts::getDefaultLogger("IVF", acts_level));
0069 
0070   Acts::EigenStepper<> stepper(m_BField);
0071 
0072   // Set up propagator with void navigator
0073   auto propagator = std::make_shared<Propagator>(stepper, Acts::VoidNavigator{},
0074                                                  logger().cloneWithSuffix("Prop"));
0075 
0076   // Setup the track linearizer
0077   Linearizer::Config linearizerCfg;
0078   linearizerCfg.bField     = m_BField;
0079   linearizerCfg.propagator = propagator;
0080   Linearizer linearizer(linearizerCfg, logger().cloneWithSuffix("HelLin"));
0081 
0082   // Setup the vertex fitter
0083   VertexFitter::Config vertexFitterCfg;
0084   vertexFitterCfg.extractParameters.connect<&Acts::InputTrack::extractParameters>();
0085   vertexFitterCfg.trackLinearizer.connect<&Linearizer::linearizeTrack>(&linearizer);
0086   VertexFitter vertexFitter(vertexFitterCfg);
0087 
0088   // Setup the seed finder
0089   ImpactPointEstimator::Config ipEstCfg(m_BField, propagator);
0090   ImpactPointEstimator ipEst(ipEstCfg);
0091   VertexSeeder::Config seederCfg(ipEst);
0092   seederCfg.extractParameters.connect<&Acts::InputTrack::extractParameters>();
0093   auto seeder = std::make_shared<VertexSeeder>(seederCfg);
0094 
0095   // Set up the actual vertex finder
0096   VertexFinder::Config finderCfg(std::move(vertexFitter), std::move(seeder), std::move(ipEst));
0097   finderCfg.maxVertices                 = m_cfg.maxVertices;
0098   finderCfg.reassignTracksAfterFirstFit = m_cfg.reassignTracksAfterFirstFit;
0099   finderCfg.extractParameters.connect<&Acts::InputTrack::extractParameters>();
0100   finderCfg.trackLinearizer.connect<&Linearizer::linearizeTrack>(&linearizer);
0101   finderCfg.field = m_BField;
0102   VertexFinder finder(std::move(finderCfg));
0103 
0104   // Get run-scoped contexts from service
0105   const auto& gctx = m_geoSvc->getActsGeometryContext();
0106   const auto& mctx = m_geoSvc->getActsMagneticFieldContext();
0107 
0108   Acts::IVertexFinder::State state(std::in_place_type<VertexFinder::State>, *m_BField, mctx);
0109 
0110   VertexFinderOptions finderOpts(gctx, mctx);
0111 
0112   std::vector<Acts::InputTrack> inputTracks;
0113   std::vector<Acts::BoundTrackParameters> trackParameters;
0114   trackParameters.reserve(constTracks.size());
0115 
0116   for (const auto& track : constTracks) {
0117     // Filter tracks based on minimum number of measurements (hits)
0118     if (track.nMeasurements() < m_cfg.minTrackHits) {
0119       trace("Track rejected: {} measurements < {} minimum required measurements",
0120             track.nMeasurements(), m_cfg.minTrackHits);
0121       continue;
0122     }
0123 
0124     // Create BoundTrackParameters and store it
0125     trackParameters.emplace_back(track.referenceSurface().getSharedPtr(), track.parameters(),
0126                                  track.covariance(), track.particleHypothesis());
0127     // Create InputTrack from stored parameters
0128     inputTracks.emplace_back(&trackParameters.back());
0129     trace("Track local position at input = {} mm, {} mm",
0130           track.parameters()[Acts::eBoundLoc0] / Acts::UnitConstants::mm,
0131           track.parameters()[Acts::eBoundLoc1] / Acts::UnitConstants::mm);
0132   }
0133 
0134   std::vector<Acts::Vertex> vertices;
0135   auto result = finder.find(inputTracks, finderOpts, state);
0136   if (result.ok()) {
0137     vertices = std::move(result.value());
0138   }
0139 
0140   for (const auto& vtx : vertices) {
0141     edm4eic::Cov4f cov(vtx.fullCovariance()(0, 0), vtx.fullCovariance()(1, 1),
0142                        vtx.fullCovariance()(2, 2), vtx.fullCovariance()(3, 3),
0143                        vtx.fullCovariance()(0, 1), vtx.fullCovariance()(0, 2),
0144                        vtx.fullCovariance()(0, 3), vtx.fullCovariance()(1, 2),
0145                        vtx.fullCovariance()(1, 3), vtx.fullCovariance()(2, 3));
0146     auto eicvertex = outputVertices->create();
0147     eicvertex.setType(1); // boolean flag if vertex is primary vertex of event
0148     eicvertex.setChi2((float)vtx.fitQuality().first); // chi2
0149     eicvertex.setNdf((float)vtx.fitQuality().second); // ndf
0150     eicvertex.setPosition({
0151         (float)vtx.position().x(),
0152         (float)vtx.position().y(),
0153         (float)vtx.position().z(),
0154         (float)vtx.time(),
0155     });                              // vtxposition
0156     eicvertex.setPositionError(cov); // covariance
0157 
0158     for (const auto& t : vtx.tracks()) {
0159       const auto& par = Acts::InputTrack::extractParameters(t.originalParams);
0160       trace("Track local position from vertex = {} mm, {} mm",
0161             par.localPosition().x() / Acts::UnitConstants::mm,
0162             par.localPosition().y() / Acts::UnitConstants::mm);
0163       float loc_a = par.localPosition().x();
0164       float loc_b = par.localPosition().y();
0165 
0166       for (const auto& part : *reconParticles) {
0167         const auto& tracks = part.getTracks();
0168         for (const auto& trk : tracks) {
0169           const auto& traj    = trk.getTrajectory();
0170           const auto& trkPars = traj.getTrackParameters();
0171           for (const auto& par : trkPars) {
0172             const double EPSILON = 1.0e-4; // mm
0173             if (std::abs((par.getLoc().a / edm4eic::unit::mm) - (loc_a / Acts::UnitConstants::mm)) <
0174                     EPSILON &&
0175                 std::abs((par.getLoc().b / edm4eic::unit::mm) - (loc_b / Acts::UnitConstants::mm)) <
0176                     EPSILON) {
0177               trace("From ReconParticles, track local position [Loc a, Loc b] = {} mm, {} mm",
0178                     par.getLoc().a / edm4eic::unit::mm, par.getLoc().b / edm4eic::unit::mm);
0179               eicvertex.addToAssociatedParticles(part);
0180             } // endif
0181           } // end for par
0182         } // end for trk
0183       } // end for part
0184     } // end for t
0185     debug("One vertex found at (x,y,z) = ({}, {}, {}) mm.",
0186           vtx.position().x() / Acts::UnitConstants::mm,
0187           vtx.position().y() / Acts::UnitConstants::mm,
0188           vtx.position().z() / Acts::UnitConstants::mm);
0189 
0190   } // end for vtx
0191 }