Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:22:11

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 #pragma once
0010 
0011 #include "Acts/Definitions/Alignment.hpp"
0012 #include "Acts/Definitions/TrackParametrization.hpp"
0013 #include "Acts/Geometry/GeometryContext.hpp"
0014 #include "Acts/MagneticField/MagneticFieldContext.hpp"
0015 #include "Acts/Surfaces/Surface.hpp"
0016 #include "Acts/TrackFitting/KalmanFitter.hpp"
0017 #include "Acts/TrackFitting/detail/KalmanGlobalCovariance.hpp"
0018 #include "Acts/Utilities/CalibrationContext.hpp"
0019 #include "Acts/Utilities/Logger.hpp"
0020 #include "Acts/Utilities/Result.hpp"
0021 #include "ActsAlignment/Kernel/AlignmentError.hpp"
0022 #include "ActsAlignment/Kernel/detail/AlignmentEngine.hpp"
0023 
0024 #include <limits>
0025 #include <map>
0026 #include <queue>
0027 #include <vector>
0028 
0029 namespace ActsAlignment {
0030 using AlignedTransformUpdater =
0031     std::function<bool(Acts::DetectorElementBase*, const Acts::GeometryContext&,
0032                        const Acts::Transform3&)>;
0033 ///
0034 /// @brief Options for align() call
0035 ///
0036 /// @tparam fit_options_t The fit options type
0037 ///
0038 template <typename fit_options_t>
0039 struct AlignmentOptions {
0040   /// Deleted default constructor
0041   AlignmentOptions() = delete;
0042 
0043   /// AlignmentOptions
0044   ///
0045   /// @param fOptions The fit options
0046   /// @param aTransformUpdater The updater to update aligned transform
0047   /// @param aDetElements The alignable detector elements
0048   /// @param chi2CufOff The alignment chi2 tolerance
0049   /// @param deltaChi2CutOff The change of chi2 within a few iterations
0050   /// @param maxIters The alignment maximum iterations
0051 
0052   AlignmentOptions(
0053       const fit_options_t& fOptions,
0054       const AlignedTransformUpdater& aTransformUpdater,
0055       const std::vector<Acts::DetectorElementBase*>& aDetElements = {},
0056       double chi2CutOff = 0.5,
0057       const std::pair<std::size_t, double>& deltaChi2CutOff = {5, 0.01},
0058       std::size_t maxIters = 5,
0059       const std::map<unsigned int, AlignmentMask>& iterState = {})
0060       : fitOptions(fOptions),
0061         alignedTransformUpdater(aTransformUpdater),
0062         alignedDetElements(aDetElements),
0063         averageChi2ONdfCutOff(chi2CutOff),
0064         deltaAverageChi2ONdfCutOff(deltaChi2CutOff),
0065         maxIterations(maxIters),
0066         iterationState(iterState) {}
0067 
0068   // The fit options
0069   fit_options_t fitOptions;
0070 
0071   /// The updater to the aligned transform
0072   AlignedTransformUpdater alignedTransformUpdater = nullptr;
0073 
0074   // The detector elements to be aligned
0075   std::vector<Acts::DetectorElementBase*> alignedDetElements;
0076 
0077   // The alignment tolerance to determine if the alignment is covered
0078   double averageChi2ONdfCutOff = 0.5;
0079 
0080   // The delta of average chi2/ndf within a couple of iterations to determine if
0081   // alignment is converged
0082   std::pair<std::size_t, double> deltaAverageChi2ONdfCutOff = {5, 0.01};
0083 
0084   // The maximum number of iterations to run alignment
0085   std::size_t maxIterations = 5;
0086 
0087   // The alignment mask for different iterations
0088   std::map<unsigned int, AlignmentMask> iterationState;
0089 };
0090 
0091 /// @brief Alignment result struct
0092 ///
0093 struct AlignmentResult {
0094   // The change of alignment parameters
0095   Acts::ActsDynamicVector deltaAlignmentParameters;
0096 
0097   // The aligned parameters for detector elements
0098   std::unordered_map<Acts::DetectorElementBase*, Acts::Transform3>
0099       alignedParameters;
0100 
0101   // The covariance of alignment parameters
0102   Acts::ActsDynamicMatrix alignmentCovariance;
0103 
0104   // The average chi2/ndf (ndf is the measurement dim)
0105   double averageChi2ONdf = std::numeric_limits<double>::max();
0106 
0107   // The delta chi2
0108   double deltaChi2 = std::numeric_limits<double>::max();
0109 
0110   // The chi2
0111   double chi2 = 0;
0112 
0113   // The measurement dimension from all tracks
0114   std::size_t measurementDim = 0;
0115 
0116   // The alignment degree of freedom
0117   std::size_t alignmentDof = 0;
0118 
0119   // The number of tracks used for alignment
0120   std::size_t numTracks = 0;
0121 
0122   // The indexed alignable surfaces
0123   std::unordered_map<const Acts::Surface*, std::size_t> idxedAlignSurfaces;
0124 
0125   Acts::Result<void> result{Acts::Result<void>::success()};
0126 };
0127 
0128 /// @brief KalmanFitter-based alignment implementation
0129 ///
0130 /// @tparam fitter_t Type of the fitter class
0131 template <typename fitter_t>
0132 struct Alignment {
0133   // @TODO: Redefine in terms of Track object
0134 
0135   /// Default constructor is deleted
0136   Alignment() = delete;
0137 
0138   /// Constructor from arguments
0139   explicit Alignment(fitter_t fitter,
0140                      std::unique_ptr<const Acts::Logger> _logger =
0141                          Acts::getDefaultLogger("Alignment",
0142                                                 Acts::Logging::INFO))
0143       : m_fitter(std::move(fitter)), m_logger{std::move(_logger)} {}
0144 
0145   /// @brief evaluate alignment state for a single track
0146   ///
0147   /// @tparam source_link_t Source link type identifying uncalibrated input
0148   /// measurements.
0149   /// @tparam start_parameters_t Type of the initial parameters
0150   /// @tparam fit_options_t The fit options type
0151   ///
0152   /// @param gctx The current geometry context object
0153   /// @param sourceLinks The fittable uncalibrated measurements
0154   /// @param sParameters The initial track parameters
0155   /// @param fitOptions The fit Options steering the fit
0156   /// @param idxedAlignSurfaces The idxed surfaces to be aligned
0157   /// @param alignMask The alignment mask (same for all detector element for the
0158   /// moment)
0159   ///
0160   /// @result The alignment state for a single track
0161   template <typename source_link_t, typename start_parameters_t,
0162             typename fit_options_t>
0163   Acts::Result<detail::TrackAlignmentState> evaluateTrackAlignmentState(
0164       const Acts::GeometryContext& gctx,
0165       const std::vector<source_link_t>& sourceLinks,
0166       const start_parameters_t& sParameters, const fit_options_t& fitOptions,
0167       const std::unordered_map<const Acts::Surface*, std::size_t>&
0168           idxedAlignSurfaces,
0169       const AlignmentMask& alignMask) const;
0170 
0171   /// @brief calculate the alignment parameters delta
0172   ///
0173   /// @tparam trajectory_container_t The trajectories container type
0174   /// @tparam start_parameters_t The initial parameters container type
0175   /// @tparam fit_options_t The fit options type
0176   ///
0177   /// @param trajectoryCollection The collection of trajectories as input of
0178   /// fitting
0179   /// @param startParametersCollection The collection of starting parameters as
0180   /// input of fitting
0181   /// @param fitOptions The fit Options steering the fit
0182   /// @param alignResult [in, out] The aligned result
0183   /// @param alignMask The alignment mask (same for all measurements now)
0184   template <typename trajectory_container_t,
0185             typename start_parameters_container_t, typename fit_options_t>
0186   void calculateAlignmentParameters(
0187       const trajectory_container_t& trajectoryCollection,
0188       const start_parameters_container_t& startParametersCollection,
0189       const fit_options_t& fitOptions, AlignmentResult& alignResult,
0190       const AlignmentMask& alignMask = AlignmentMask::All) const;
0191 
0192   /// @brief update the detector element alignment parameters
0193   ///
0194   /// @param gctx The geometry context
0195   /// @param alignedDetElements The detector elements to be aligned
0196   /// @param alignedTransformUpdater The updater for updating the aligned
0197   /// @param alignResult [in, out] The aligned result
0198   Acts::Result<void> updateAlignmentParameters(
0199       const Acts::GeometryContext& gctx,
0200       const std::vector<Acts::DetectorElementBase*>& alignedDetElements,
0201       const AlignedTransformUpdater& alignedTransformUpdater,
0202       AlignmentResult& alignResult) const;
0203 
0204   /// @brief Alignment implementation
0205   ///
0206   /// @tparam trajectory_container_t The trajectories container type
0207   /// @tparam start_parameters_t The initial parameters container type
0208   /// @tparam fit_options_t The fit options type
0209   ///
0210   /// @param trajectoryCollection The collection of trajectories as input of
0211   /// fitting
0212   /// @param startParametersCollection The collection of starting parameters as
0213   /// input of fitting
0214   /// @param alignOptions The alignment options
0215   ///
0216   /// @result The alignment result
0217   template <typename trajectory_container_t,
0218             typename start_parameters_container_t, typename fit_options_t>
0219   Acts::Result<AlignmentResult> align(
0220       const trajectory_container_t& trajectoryCollection,
0221       const start_parameters_container_t& startParametersCollection,
0222       const AlignmentOptions<fit_options_t>& alignOptions) const;
0223 
0224  private:
0225   // The fitter
0226   fitter_t m_fitter;
0227 
0228   std::unique_ptr<const Acts::Logger> m_logger;
0229 
0230   const Acts::Logger& logger() const { return *m_logger; }
0231 };
0232 }  // namespace ActsAlignment
0233 
0234 #include "ActsAlignment/Kernel/Alignment.ipp"