Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:34

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/Alignment/AlignmentAlgorithm.hpp"
0010 
0011 #include "Acts/Surfaces/PerigeeSurface.hpp"
0012 #include "Acts/TrackFitting/GainMatrixSmoother.hpp"
0013 #include "Acts/TrackFitting/GainMatrixUpdater.hpp"
0014 #include "ActsExamples/EventData/IndexSourceLink.hpp"
0015 #include "ActsExamples/EventData/MeasurementCalibration.hpp"
0016 #include "ActsExamples/EventData/ProtoTrack.hpp"
0017 #include "ActsExamples/EventData/Trajectories.hpp"
0018 #include "ActsExamples/Framework/WhiteBoard.hpp"
0019 
0020 ActsExamples::AlignmentAlgorithm::AlignmentAlgorithm(Config cfg,
0021                                                      Acts::Logging::Level lvl)
0022     : ActsExamples::IAlgorithm("AlignmentAlgorithm", lvl),
0023       m_cfg(std::move(cfg)) {
0024   if (m_cfg.inputMeasurements.empty()) {
0025     throw std::invalid_argument("Missing input measurement collection");
0026   }
0027   if (m_cfg.inputProtoTracks.empty()) {
0028     throw std::invalid_argument("Missing input proto tracks collection");
0029   }
0030   if (m_cfg.inputInitialTrackParameters.empty()) {
0031     throw std::invalid_argument(
0032         "Missing input initial track parameters collection");
0033   }
0034   if (m_cfg.outputAlignmentParameters.empty()) {
0035     throw std::invalid_argument(
0036         "Missing output alignment parameters collection");
0037   }
0038 
0039   m_inputMeasurements.initialize(m_cfg.inputMeasurements);
0040   m_inputProtoTracks.initialize(m_cfg.inputProtoTracks);
0041   m_inputInitialTrackParameters.initialize(m_cfg.inputInitialTrackParameters);
0042   m_outputAlignmentParameters.initialize(m_cfg.outputAlignmentParameters);
0043 }
0044 
0045 ActsExamples::ProcessCode ActsExamples::AlignmentAlgorithm::execute(
0046     const ActsExamples::AlgorithmContext& ctx) const {
0047   // Read input data
0048   const auto& measurements = m_inputMeasurements(ctx);
0049   const auto& protoTracks = m_inputProtoTracks(ctx);
0050   const auto& initialParameters = m_inputInitialTrackParameters(ctx);
0051 
0052   // Consistency cross checks
0053   if (protoTracks.size() != initialParameters.size()) {
0054     ACTS_FATAL("Inconsistent number of proto tracks and parameters "
0055                << protoTracks.size() << " vs " << initialParameters.size());
0056     return ProcessCode::ABORT;
0057   }
0058 
0059   std::size_t numTracksUsed = protoTracks.size();
0060   if (m_cfg.maxNumTracks > 0 &&
0061       m_cfg.maxNumTracks < static_cast<int>(protoTracks.size())) {
0062     numTracksUsed = m_cfg.maxNumTracks;
0063   }
0064 
0065   // Prepare the input track collection
0066   std::vector<std::vector<IndexSourceLink>> sourceLinkTrackContainer;
0067   sourceLinkTrackContainer.reserve(numTracksUsed);
0068   std::vector<IndexSourceLink> trackSourceLinks;
0069   for (std::size_t itrack = 0; itrack < numTracksUsed; ++itrack) {
0070     // The list of hits and the initial start parameters
0071     const auto& protoTrack = protoTracks[itrack];
0072 
0073     // Clear & reserve the right size
0074     trackSourceLinks.clear();
0075     trackSourceLinks.reserve(protoTrack.size());
0076 
0077     // Fill the source links via their indices from the container
0078     for (auto measIndex : protoTrack) {
0079       const ConstVariableBoundMeasurementProxy measurement =
0080           measurements.getMeasurement(measIndex);
0081       IndexSourceLink sourceLink(measurement.geometryId(), measIndex);
0082       trackSourceLinks.push_back(sourceLink);
0083     }
0084     sourceLinkTrackContainer.push_back(trackSourceLinks);
0085   }
0086 
0087   // Prepare the output for alignment parameters
0088   AlignmentParameters alignedParameters;
0089 
0090   // Construct a perigee surface as the target surface for the fitter
0091   auto pSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(
0092       Acts::Vector3{0., 0., 0.});
0093 
0094   Acts::KalmanFitterExtensions<Acts::VectorMultiTrajectory> extensions;
0095   PassThroughCalibrator pcalibrator;
0096   MeasurementCalibratorAdapter calibrator(pcalibrator, measurements);
0097   extensions.calibrator.connect<&MeasurementCalibratorAdapter::calibrate>(
0098       &calibrator);
0099   Acts::GainMatrixUpdater kfUpdater;
0100   Acts::GainMatrixSmoother kfSmoother;
0101   extensions.updater.connect<
0102       &Acts::GainMatrixUpdater::operator()<Acts::VectorMultiTrajectory>>(
0103       &kfUpdater);
0104   extensions.smoother.connect<
0105       &Acts::GainMatrixSmoother::operator()<Acts::VectorMultiTrajectory>>(
0106       &kfSmoother);
0107 
0108   // Set the KalmanFitter options
0109   TrackFitterOptions kfOptions(
0110       ctx.geoContext, ctx.magFieldContext, ctx.calibContext, extensions,
0111       Acts::PropagatorPlainOptions(ctx.geoContext, ctx.magFieldContext),
0112       &(*pSurface));
0113 
0114   // Set the alignment options
0115   ActsAlignment::AlignmentOptions<TrackFitterOptions> alignOptions(
0116       kfOptions, m_cfg.alignedTransformUpdater, m_cfg.alignedDetElements,
0117       m_cfg.chi2ONdfCutOff, m_cfg.deltaChi2ONdfCutOff, m_cfg.maxNumIterations);
0118 
0119   ACTS_DEBUG("Invoke track-based alignment with " << numTracksUsed
0120                                                   << " input tracks");
0121   auto result =
0122       (*m_cfg.align)(sourceLinkTrackContainer, initialParameters, alignOptions);
0123   if (result.ok()) {
0124     const auto& alignOutput = result.value();
0125     alignedParameters = alignOutput.alignedParameters;
0126     ACTS_VERBOSE(
0127         "Alignment finished with deltaChi2 = " << result.value().deltaChi2);
0128   } else {
0129     ACTS_WARNING("Alignment failed with " << result.error());
0130   }
0131 
0132   // add alignment parameters to event store
0133   m_outputAlignmentParameters(ctx, std::move(alignedParameters));
0134   return ActsExamples::ProcessCode::SUCCESS;
0135 }