Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-21 09:24:06

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "ActsExamples/Vertexing/AdaptiveMultiVertexFinderAlgorithm.hpp"
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/EventData/BoundTrackParameters.hpp"
0013 #include "Acts/Propagator/SympyStepper.hpp"
0014 #include "Acts/Utilities/AnnealingUtility.hpp"
0015 #include "Acts/Utilities/Logger.hpp"
0016 #include "Acts/Utilities/Result.hpp"
0017 #include "Acts/Vertexing/AdaptiveGridDensityVertexFinder.hpp"
0018 #include "Acts/Vertexing/AdaptiveGridTrackDensity.hpp"
0019 #include "Acts/Vertexing/AdaptiveMultiVertexFinder.hpp"
0020 #include "Acts/Vertexing/AdaptiveMultiVertexFitter.hpp"
0021 #include "Acts/Vertexing/GaussianTrackDensity.hpp"
0022 #include "Acts/Vertexing/ImpactPointEstimator.hpp"
0023 #include "Acts/Vertexing/TrackAtVertex.hpp"
0024 #include "Acts/Vertexing/TrackDensityVertexFinder.hpp"
0025 #include "Acts/Vertexing/Vertex.hpp"
0026 #include "ActsExamples/EventData/SimParticle.hpp"
0027 #include "ActsExamples/EventData/SimVertex.hpp"
0028 #include "ActsExamples/Framework/AlgorithmContext.hpp"
0029 #include "ActsExamples/Framework/ProcessCode.hpp"
0030 
0031 #include <algorithm>
0032 #include <memory>
0033 #include <optional>
0034 #include <ostream>
0035 #include <stdexcept>
0036 #include <system_error>
0037 #include <utility>
0038 
0039 #include "TruthVertexSeeder.hpp"
0040 #include "VertexingHelpers.hpp"
0041 
0042 namespace ActsExamples {
0043 
0044 AdaptiveMultiVertexFinderAlgorithm::AdaptiveMultiVertexFinderAlgorithm(
0045     const Config& config, std::unique_ptr<const Acts::Logger> logger)
0046     : IAlgorithm("AdaptiveMultiVertexFinder", std::move(logger)),
0047       m_cfg(config),
0048       m_propagator{[&]() {
0049         // Set up SympyStepper
0050         Acts::SympyStepper stepper(m_cfg.bField);
0051 
0052         // Set up the propagator
0053         return std::make_shared<Propagator>(stepper);
0054       }()},
0055       m_ipEstimator{[&]() {
0056         // Set up ImpactPointEstimator
0057         Acts::ImpactPointEstimator::Config ipEstimatorCfg(m_cfg.bField,
0058                                                           m_propagator);
0059         return Acts::ImpactPointEstimator(
0060             ipEstimatorCfg,
0061             this->logger().cloneWithSuffix("ImpactPointEstimator"));
0062       }()},
0063       m_linearizer{[&] {
0064         // Set up the helical track linearizer
0065         Linearizer::Config ltConfig;
0066 
0067         ltConfig.propagator = m_propagator;
0068         return Linearizer(
0069             ltConfig, this->logger().cloneWithSuffix("HelicalTrackLinearizer"));
0070       }()},
0071       m_vertexSeeder{makeVertexSeeder()},
0072       m_vertexFinder{makeVertexFinder(m_vertexSeeder)} {
0073   if (m_cfg.inputTrackParameters.empty()) {
0074     throw std::invalid_argument("Missing input track parameter collection");
0075   }
0076   if (m_cfg.outputProtoVertices.empty()) {
0077     throw std::invalid_argument("Missing output proto vertices collection");
0078   }
0079   if (m_cfg.outputVertices.empty()) {
0080     throw std::invalid_argument("Missing output vertices collection");
0081   }
0082   if (m_cfg.seedFinder == SeedFinder::TruthSeeder &&
0083       m_cfg.inputTruthVertices.empty()) {
0084     throw std::invalid_argument("Missing input truth vertex collection");
0085   }
0086 
0087   // Sanitize the configuration
0088   if (m_cfg.seedFinder != SeedFinder::TruthSeeder &&
0089       (!m_cfg.inputTruthParticles.empty() ||
0090        !m_cfg.inputTruthVertices.empty())) {
0091     ACTS_LOG_WITH_LOGGER(
0092         this->logger(), Acts::Logging::INFO,
0093         "Ignoring truth input as seed finder is not TruthSeeder");
0094     m_cfg.inputTruthVertices.clear();
0095     m_cfg.inputTruthVertices.clear();
0096   }
0097 
0098   m_inputTrackParameters.initialize(m_cfg.inputTrackParameters);
0099   m_inputTruthParticles.maybeInitialize(m_cfg.inputTruthParticles);
0100   m_inputTruthVertices.maybeInitialize(m_cfg.inputTruthVertices);
0101   m_outputProtoVertices.initialize(m_cfg.outputProtoVertices);
0102   m_outputVertices.initialize(m_cfg.outputVertices);
0103 }
0104 
0105 std::unique_ptr<Acts::IVertexFinder>
0106 AdaptiveMultiVertexFinderAlgorithm::makeVertexSeeder() const {
0107   if (m_cfg.seedFinder == SeedFinder::TruthSeeder) {
0108     using Seeder = TruthVertexSeeder;
0109     Seeder::Config seederConfig;
0110     seederConfig.useXY = false;
0111     seederConfig.useTime = m_cfg.useTime;
0112     seederConfig.simultaneousSeeds = m_cfg.simultaneousSeeds;
0113     return std::make_unique<Seeder>(seederConfig);
0114   }
0115 
0116   if (m_cfg.seedFinder == SeedFinder::GaussianSeeder) {
0117     using Seeder = Acts::TrackDensityVertexFinder;
0118     Acts::GaussianTrackDensity::Config trkDensityCfg;
0119     trkDensityCfg.extractParameters
0120         .connect<&Acts::InputTrack::extractParameters>();
0121     return std::make_unique<Seeder>(
0122         Seeder::Config{Acts::GaussianTrackDensity(trkDensityCfg)});
0123   }
0124 
0125   if (m_cfg.seedFinder == SeedFinder::AdaptiveGridSeeder) {
0126     // Set up track density used during vertex seeding
0127     Acts::AdaptiveGridTrackDensity::Config trkDensityCfg;
0128     // Bin extent in z-direction
0129     trkDensityCfg.spatialBinExtent = m_cfg.spatialBinExtent;
0130     // Bin extent in t-direction
0131     trkDensityCfg.temporalBinExtent = m_cfg.temporalBinExtent;
0132     // (min,max) z and t windows for filling the density map
0133     trkDensityCfg.spatialWindow = m_cfg.spatialWindow;
0134     trkDensityCfg.temporalWindow = m_cfg.temporalWindow;
0135     trkDensityCfg.useTime = m_cfg.useTime;
0136     Acts::AdaptiveGridTrackDensity trkDensity(trkDensityCfg);
0137 
0138     // Set up vertex seeder and finder
0139     using Seeder = Acts::AdaptiveGridDensityVertexFinder;
0140     Seeder::Config seederConfig(trkDensity);
0141     seederConfig.extractParameters
0142         .connect<&Acts::InputTrack::extractParameters>();
0143     return std::make_unique<Seeder>(seederConfig);
0144   }
0145 
0146   throw std::invalid_argument("Unknown seed finder");
0147 }
0148 
0149 Acts::AdaptiveMultiVertexFinder
0150 AdaptiveMultiVertexFinderAlgorithm::makeVertexFinder(
0151     std::shared_ptr<const Acts::IVertexFinder> seedFinder) const {
0152   // Set up deterministic annealing with user-defined temperatures
0153   Acts::AnnealingUtility annealingUtility(m_cfg.annealingConfig);
0154 
0155   // Set up the vertex fitter with user-defined annealing
0156   Fitter::Config fitterCfg(m_ipEstimator);
0157   fitterCfg.annealingTool = annealingUtility;
0158   fitterCfg.minWeight = m_cfg.minWeight;
0159   fitterCfg.doSmoothing = m_cfg.doSmoothing;
0160   fitterCfg.useTime = m_cfg.useTime;
0161   fitterCfg.extractParameters.connect<&Acts::InputTrack::extractParameters>();
0162   fitterCfg.trackLinearizer
0163       .connect<&Acts::HelicalTrackLinearizer::linearizeTrack>(&m_linearizer);
0164   Fitter fitter(std::move(fitterCfg),
0165                 logger().cloneWithSuffix("AdaptiveMultiVertexFitter"));
0166 
0167   Acts::AdaptiveMultiVertexFinder::Config finderConfig(
0168       std::move(fitter), std::move(seedFinder), m_ipEstimator, m_cfg.bField);
0169   // Set the initial variance of the 4D vertex position. Since time is on a
0170   // numerical scale, we have to provide a greater value in the corresponding
0171   // dimension.
0172   finderConfig.initialVariances = m_cfg.initialVariances;
0173   finderConfig.tracksMaxZinterval = m_cfg.tracksMaxZinterval;
0174   finderConfig.maxIterations = m_cfg.maxIterations;
0175   finderConfig.useTime = m_cfg.useTime;
0176   // 5 corresponds to a p-value of ~0.92 using `chi2(x=5,ndf=2)`
0177   finderConfig.tracksMaxSignificance = 5;
0178   // This should be used consistently with and without time
0179   finderConfig.doFullSplitting = m_cfg.doFullSplitting;
0180   // 3 corresponds to a p-value of ~0.92 using `chi2(x=3,ndf=1)`
0181   finderConfig.maxMergeVertexSignificance = 3;
0182   if (m_cfg.useTime) {
0183     // When using time, we have an extra contribution to the chi2 by the time
0184     // coordinate. We thus need to increase tracksMaxSignificance (i.e., the
0185     // maximum chi2 that a track can have to be associated with a vertex).
0186     // Using the same p-value for 3 dof instead of 2.
0187     // 6.7 corresponds to a p-value of ~0.92 using `chi2(x=6.7,ndf=3)`
0188     finderConfig.tracksMaxSignificance = 6.7;
0189     // Using the same p-value for 2 dof instead of 1.
0190     // 5 corresponds to a p-value of ~0.92 using `chi2(x=5,ndf=2)`
0191     finderConfig.maxMergeVertexSignificance = 5;
0192   }
0193 
0194   finderConfig.extractParameters
0195       .template connect<&Acts::InputTrack::extractParameters>();
0196 
0197   if (m_cfg.seedFinder == SeedFinder::TruthSeeder) {
0198     finderConfig.doNotBreakWhileSeeding = true;
0199   }
0200 
0201   finderConfig.tracksMaxSignificance =
0202       m_cfg.tracksMaxSignificance.value_or(finderConfig.tracksMaxSignificance);
0203   finderConfig.maxMergeVertexSignificance =
0204       m_cfg.maxMergeVertexSignificance.value_or(
0205           finderConfig.maxMergeVertexSignificance);
0206 
0207   // Instantiate the finder
0208   return Acts::AdaptiveMultiVertexFinder(std::move(finderConfig),
0209                                          logger().clone());
0210 }
0211 
0212 ProcessCode AdaptiveMultiVertexFinderAlgorithm::execute(
0213     const AlgorithmContext& ctx) const {
0214   const auto& inputTrackParameters = m_inputTrackParameters(ctx);
0215 
0216   auto inputTracks = makeInputTracks(inputTrackParameters);
0217 
0218   if (inputTrackParameters.size() != inputTracks.size()) {
0219     ACTS_ERROR("Input track containers do not align: "
0220                << inputTrackParameters.size() << " != " << inputTracks.size());
0221   }
0222 
0223   for (const auto& trk : inputTrackParameters) {
0224     if (trk.covariance() && trk.covariance()->determinant() <= 0) {
0225       // actually we should consider this as an error but I do not want the CI
0226       // to fail
0227       ACTS_WARNING("input track " << trk << " has det(cov) = "
0228                                   << trk.covariance()->determinant());
0229     }
0230   }
0231 
0232   // The vertex finder state
0233   auto state = m_vertexFinder.makeState(ctx.magFieldContext);
0234 
0235   // In case of the truth seeder, we need to wire the truth vertices into the
0236   // vertex finder
0237   if (m_cfg.seedFinder == SeedFinder::TruthSeeder) {
0238     const auto& truthParticles = m_inputTruthParticles(ctx);
0239     const auto& truthVertices = m_inputTruthVertices(ctx);
0240 
0241     auto& vertexSeederState =
0242         state.as<Acts::AdaptiveMultiVertexFinder::State>()
0243             .seedFinderState.as<TruthVertexSeeder::State>();
0244 
0245     std::map<SimVertexBarcode, std::size_t> vertexParticleCount;
0246 
0247     for (const auto& truthVertex : truthVertices) {
0248       // Skip secondary vertices
0249       if (truthVertex.vertexId().vertexSecondary() != 0) {
0250         continue;
0251       }
0252       vertexSeederState.truthVertices.push_back(truthVertex);
0253 
0254       // Count the number of particles associated with each vertex
0255       std::size_t particleCount = 0;
0256       for (const auto& particle : truthParticles) {
0257         if (static_cast<SimVertexBarcode>(particle.particleId().vertexId()) ==
0258             truthVertex.vertexId()) {
0259           ++particleCount;
0260         }
0261       }
0262       vertexParticleCount[truthVertex.vertexId()] = particleCount;
0263     }
0264 
0265     // sort by number of particles
0266     std::ranges::sort(vertexSeederState.truthVertices, {},
0267                       [&vertexParticleCount](const auto& v) {
0268                         return vertexParticleCount[v.vertexId()];
0269                       });
0270 
0271     ACTS_INFO("Got " << truthVertices.size() << " truth vertices and selected "
0272                      << vertexSeederState.truthVertices.size() << " in event");
0273   }
0274 
0275   // Default vertexing options, this is where e.g. a constraint could be set
0276   Options finderOpts(ctx.recoGeoContext, ctx.magFieldContext);
0277 
0278   VertexContainer vertices;
0279 
0280   if (inputTrackParameters.empty()) {
0281     ACTS_DEBUG("Empty track parameter collection found, skipping vertexing");
0282   } else {
0283     ACTS_DEBUG("Have " << inputTrackParameters.size()
0284                        << " input track parameters, running vertexing");
0285     // find vertices
0286     auto result = m_vertexFinder.find(inputTracks, finderOpts, state);
0287 
0288     if (result.ok()) {
0289       vertices = std::move(result.value());
0290     } else {
0291       ACTS_ERROR("Error in vertex finder: " << result.error().message());
0292     }
0293   }
0294 
0295   // show some debug output
0296   ACTS_DEBUG("Found " << vertices.size() << " vertices in event");
0297   for (const auto& vtx : vertices) {
0298     ACTS_DEBUG("Found vertex at " << vtx.fullPosition().transpose() << " with "
0299                                   << vtx.tracks().size() << " tracks.");
0300   }
0301 
0302   // store proto vertices extracted from the found vertices
0303   m_outputProtoVertices(ctx, makeProtoVertices(inputTracks, vertices));
0304 
0305   // store found vertices
0306   m_outputVertices(ctx, std::move(vertices));
0307 
0308   return ProcessCode::SUCCESS;
0309 }
0310 
0311 }  // namespace ActsExamples