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