Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:23:37

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/TrackFitting/RefittingAlgorithm.hpp"
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/EventData/GenericBoundTrackParameters.hpp"
0013 #include "Acts/EventData/MultiTrajectory.hpp"
0014 #include "Acts/EventData/SourceLink.hpp"
0015 #include "Acts/EventData/TrackParameters.hpp"
0016 #include "Acts/EventData/TrackProxy.hpp"
0017 #include "Acts/EventData/VectorMultiTrajectory.hpp"
0018 #include "Acts/EventData/VectorTrackContainer.hpp"
0019 #include "Acts/Surfaces/Surface.hpp"
0020 #include "Acts/Utilities/Result.hpp"
0021 #include "ActsExamples/Framework/AlgorithmContext.hpp"
0022 #include "ActsExamples/TrackFitting/RefittingCalibrator.hpp"
0023 #include "ActsExamples/TrackFitting/TrackFitterFunction.hpp"
0024 
0025 #include <algorithm>
0026 #include <optional>
0027 #include <ostream>
0028 #include <stdexcept>
0029 #include <system_error>
0030 #include <utility>
0031 #include <vector>
0032 
0033 namespace ActsExamples {
0034 
0035 RefittingAlgorithm::RefittingAlgorithm(Config config,
0036                                        Acts::Logging::Level level)
0037     : IAlgorithm("RefittingAlgorithm", level), m_cfg(std::move(config)) {
0038   if (m_cfg.inputTracks.empty()) {
0039     throw std::invalid_argument("Missing input tracks collection");
0040   }
0041   if (m_cfg.outputTracks.empty()) {
0042     throw std::invalid_argument("Missing output tracks collection");
0043   }
0044 
0045   m_inputTracks.initialize(m_cfg.inputTracks);
0046   m_outputTracks.initialize(m_cfg.outputTracks);
0047 }
0048 
0049 ProcessCode RefittingAlgorithm::execute(const AlgorithmContext& ctx) const {
0050   const auto& inputTracks = m_inputTracks(ctx);
0051 
0052   auto trackContainer = std::make_shared<Acts::VectorTrackContainer>();
0053   auto trackStateContainer = std::make_shared<Acts::VectorMultiTrajectory>();
0054   TrackContainer tracks(trackContainer, trackStateContainer);
0055 
0056   // Perform the fit for each input track
0057   std::vector<Acts::SourceLink> trackSourceLinks;
0058   std::vector<const Acts::Surface*> surfSequence;
0059   RefittingCalibrator calibrator;
0060 
0061   auto itrack = 0ul;
0062   for (const auto& track : inputTracks) {
0063     // Check if you are not in picking mode
0064     if (m_cfg.pickTrack > -1 &&
0065         static_cast<std::size_t>(m_cfg.pickTrack) != itrack++) {
0066       continue;
0067     }
0068 
0069     if (!track.hasReferenceSurface()) {
0070       ACTS_VERBOSE("Skip track " << itrack << ": missing ref surface");
0071       continue;
0072     }
0073 
0074     TrackFitterFunction::GeneralFitterOptions options{
0075         ctx.geoContext,
0076         ctx.magFieldContext,
0077         ctx.calibContext,
0078         &track.referenceSurface(),
0079         Acts::PropagatorPlainOptions(ctx.geoContext, ctx.magFieldContext),
0080         true};
0081 
0082     Acts::BoundTrackParameters initialParams(
0083         track.referenceSurface().getSharedPtr(), track.parameters(),
0084         track.covariance(), track.particleHypothesis());
0085 
0086     if (initialParams.covariance()) {
0087       for (auto i = 0ul; i < m_cfg.initialVarInflation.size(); ++i) {
0088         (*initialParams.covariance())(i, i) *= m_cfg.initialVarInflation.at(i);
0089       }
0090     }
0091 
0092     trackSourceLinks.clear();
0093     surfSequence.clear();
0094 
0095     for (auto state : track.trackStatesReversed()) {
0096       surfSequence.push_back(&state.referenceSurface());
0097 
0098       if (!state.hasCalibrated()) {
0099         continue;
0100       }
0101 
0102       auto sl = RefittingCalibrator::RefittingSourceLink{state};
0103       trackSourceLinks.push_back(Acts::SourceLink{sl});
0104     }
0105 
0106     if (surfSequence.empty()) {
0107       ACTS_WARNING("Empty track " << itrack << " found.");
0108       continue;
0109     }
0110 
0111     std::ranges::reverse(surfSequence);
0112 
0113     ACTS_VERBOSE("Initial parameters: "
0114                  << initialParams.fourPosition(ctx.geoContext).transpose()
0115                  << " -> " << initialParams.direction().transpose());
0116 
0117     ACTS_DEBUG("Invoke direct fitter for track " << itrack);
0118     auto result = (*m_cfg.fit)(trackSourceLinks, initialParams, options,
0119                                calibrator, surfSequence, tracks);
0120 
0121     if (result.ok()) {
0122       // Get the fit output object
0123       const auto& refittedTrack = result.value();
0124       if (refittedTrack.hasReferenceSurface()) {
0125         ACTS_VERBOSE("Refitted parameters for track " << itrack);
0126         ACTS_VERBOSE("  " << track.parameters().transpose());
0127         ACTS_VERBOSE("Measurements: " << refittedTrack.nMeasurements());
0128         ACTS_VERBOSE("Outliers: " << refittedTrack.nOutliers());
0129       } else {
0130         ACTS_DEBUG("No refitted parameters for track " << itrack);
0131       }
0132     } else {
0133       ACTS_WARNING("Fit failed for track "
0134                    << itrack << " with error: " << result.error() << ", "
0135                    << result.error().message());
0136     }
0137     ++itrack;
0138   }
0139 
0140   ACTS_DEBUG("Fitted tracks: " << trackContainer->size());
0141 
0142   if (logger().doPrint(Acts::Logging::DEBUG)) {
0143     std::stringstream ss;
0144     trackStateContainer->statistics().toStream(ss);
0145     ACTS_DEBUG(ss.str());
0146   }
0147 
0148   ConstTrackContainer constTracks{
0149       std::make_shared<Acts::ConstVectorTrackContainer>(
0150           std::move(*trackContainer)),
0151       std::make_shared<Acts::ConstVectorMultiTrajectory>(
0152           std::move(*trackStateContainer))};
0153 
0154   m_outputTracks(ctx, std::move(constTracks));
0155   return ProcessCode::SUCCESS;
0156 }
0157 
0158 }  // namespace ActsExamples