Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-05 08:11:47

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/TrackFittingAlgorithm.hpp"
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/EventData/GenericBoundTrackParameters.hpp"
0013 #include "Acts/EventData/SourceLink.hpp"
0014 #include "Acts/EventData/TrackProxy.hpp"
0015 #include "Acts/EventData/VectorMultiTrajectory.hpp"
0016 #include "Acts/EventData/VectorTrackContainer.hpp"
0017 #include "Acts/Propagator/Propagator.hpp"
0018 #include "Acts/Surfaces/PerigeeSurface.hpp"
0019 #include "Acts/Surfaces/Surface.hpp"
0020 #include "Acts/Utilities/Result.hpp"
0021 #include "ActsExamples/EventData/IndexSourceLink.hpp"
0022 #include "ActsExamples/EventData/Measurement.hpp"
0023 #include "ActsExamples/EventData/MeasurementCalibration.hpp"
0024 #include "ActsExamples/EventData/ProtoTrack.hpp"
0025 #include "ActsExamples/Framework/AlgorithmContext.hpp"
0026 #include "ActsExamples/TrackFitting/TrackFitterFunction.hpp"
0027 
0028 #include <cstddef>
0029 #include <functional>
0030 #include <ostream>
0031 #include <stdexcept>
0032 #include <system_error>
0033 #include <utility>
0034 #include <vector>
0035 
0036 ActsExamples::TrackFittingAlgorithm::TrackFittingAlgorithm(
0037     Config config, Acts::Logging::Level level)
0038     : ActsExamples::IAlgorithm("TrackFittingAlgorithm", level),
0039       m_cfg(std::move(config)) {
0040   if (m_cfg.inputMeasurements.empty()) {
0041     throw std::invalid_argument("Missing input measurement collection");
0042   }
0043   if (m_cfg.inputProtoTracks.empty()) {
0044     throw std::invalid_argument("Missing input proto tracks collection");
0045   }
0046   if (m_cfg.inputInitialTrackParameters.empty()) {
0047     throw std::invalid_argument(
0048         "Missing input initial track parameters collection");
0049   }
0050   if (m_cfg.outputTracks.empty()) {
0051     throw std::invalid_argument("Missing output tracks collection");
0052   }
0053   if (!m_cfg.calibrator) {
0054     throw std::invalid_argument("Missing calibrator");
0055   }
0056   if (m_cfg.inputClusters.empty() && m_cfg.calibrator->needsClusters()) {
0057     throw std::invalid_argument("The configured calibrator needs clusters");
0058   }
0059 
0060   m_inputMeasurements.initialize(m_cfg.inputMeasurements);
0061   m_inputProtoTracks.initialize(m_cfg.inputProtoTracks);
0062   m_inputInitialTrackParameters.initialize(m_cfg.inputInitialTrackParameters);
0063   m_inputClusters.maybeInitialize(m_cfg.inputClusters);
0064   m_outputTracks.initialize(m_cfg.outputTracks);
0065 }
0066 
0067 ActsExamples::ProcessCode ActsExamples::TrackFittingAlgorithm::execute(
0068     const ActsExamples::AlgorithmContext& ctx) const {
0069   // Read input data
0070   const auto& measurements = m_inputMeasurements(ctx);
0071   const auto& protoTracks = m_inputProtoTracks(ctx);
0072   const auto& initialParameters = m_inputInitialTrackParameters(ctx);
0073 
0074   const ClusterContainer* clusters =
0075       m_inputClusters.isInitialized() ? &m_inputClusters(ctx) : nullptr;
0076 
0077   // Consistency cross checks
0078   if (protoTracks.size() != initialParameters.size()) {
0079     ACTS_FATAL("Inconsistent number of proto tracks and parameters "
0080                << protoTracks.size() << " vs " << initialParameters.size());
0081     return ProcessCode::ABORT;
0082   }
0083 
0084   // Construct a perigee surface as the target surface
0085   auto pSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(
0086       Acts::Vector3{0., 0., 0.});
0087 
0088   // Measurement calibrator must be instantiated here, because we need the
0089   // measurements to construct it. The other extensions are hold by the
0090   // fit-function-object
0091   ActsExamples::MeasurementCalibratorAdapter calibrator(*(m_cfg.calibrator),
0092                                                         measurements, clusters);
0093 
0094   TrackFitterFunction::GeneralFitterOptions options{
0095       ctx.geoContext, ctx.magFieldContext, ctx.calibContext, pSurface.get(),
0096       Acts::PropagatorPlainOptions(ctx.geoContext, ctx.magFieldContext)};
0097 
0098   auto trackContainer = std::make_shared<Acts::VectorTrackContainer>();
0099   auto trackStateContainer = std::make_shared<Acts::VectorMultiTrajectory>();
0100   TrackContainer tracks(trackContainer, trackStateContainer);
0101 
0102   // Perform the fit for each input track
0103   std::vector<Acts::SourceLink> trackSourceLinks;
0104   for (std::size_t itrack = 0; itrack < protoTracks.size(); ++itrack) {
0105     // Check if you are not in picking mode
0106     if (m_cfg.pickTrack > -1 &&
0107         static_cast<std::size_t>(m_cfg.pickTrack) != itrack) {
0108       continue;
0109     }
0110 
0111     // The list of hits and the initial start parameters
0112     const auto& protoTrack = protoTracks[itrack];
0113     const auto& initialParams = initialParameters[itrack];
0114 
0115     // We can have empty tracks which must give empty fit results so the number
0116     // of entries in input and output containers matches.
0117     if (protoTrack.empty()) {
0118       ACTS_WARNING("Empty track " << itrack << " found.");
0119       continue;
0120     }
0121 
0122     ACTS_VERBOSE("Initial 4 position: "
0123                  << initialParams.fourPosition(ctx.geoContext).transpose());
0124     ACTS_VERBOSE(
0125         "Initial direction: " << initialParams.direction().transpose());
0126     ACTS_VERBOSE("Initial momentum: " << initialParams.absoluteMomentum());
0127 
0128     // Clear & reserve the right size
0129     trackSourceLinks.clear();
0130     trackSourceLinks.reserve(protoTrack.size());
0131 
0132     // Fill the source links via their indices from the container
0133     for (auto measIndex : protoTrack) {
0134       ConstVariableBoundMeasurementProxy measurement =
0135           measurements.getMeasurement(measIndex);
0136       IndexSourceLink sourceLink(measurement.geometryId(), measIndex);
0137       trackSourceLinks.push_back(Acts::SourceLink(sourceLink));
0138     }
0139 
0140     ACTS_VERBOSE("Invoke fitter for track " << itrack);
0141     auto result = (*m_cfg.fit)(trackSourceLinks, initialParams, options,
0142                                calibrator, tracks);
0143 
0144     if (result.ok()) {
0145       // Get the fit output object
0146       const auto& track = result.value();
0147       if (track.hasReferenceSurface()) {
0148         ACTS_VERBOSE("Fitted parameters for track " << itrack);
0149         ACTS_VERBOSE("  " << track.parameters().transpose());
0150         ACTS_VERBOSE("Measurements: (prototrack->track): "
0151                      << protoTrack.size() << " -> " << track.nMeasurements());
0152       } else {
0153         ACTS_VERBOSE("No fitted parameters for track " << itrack);
0154       }
0155     } else {
0156       ACTS_WARNING("Fit failed for track "
0157                    << itrack << " with error: " << result.error() << ", "
0158                    << result.error().message());
0159     }
0160   }
0161 
0162   std::stringstream ss;
0163   trackStateContainer->statistics().toStream(ss);
0164   ACTS_DEBUG(ss.str());
0165 
0166   ConstTrackContainer constTracks{
0167       std::make_shared<Acts::ConstVectorTrackContainer>(
0168           std::move(*trackContainer)),
0169       std::make_shared<Acts::ConstVectorMultiTrajectory>(
0170           std::move(*trackStateContainer))};
0171 
0172   m_outputTracks(ctx, std::move(constTracks));
0173   return ActsExamples::ProcessCode::SUCCESS;
0174 }