Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-03 09:52:54

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