Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:10:43

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   Alignment(fitter_t fitter,
0140             std::unique_ptr<const Acts::Logger> _logger =
0141                 Acts::getDefaultLogger("Alignment", Acts::Logging::INFO))
0142       : m_fitter(std::move(fitter)), m_logger{std::move(_logger)} {}
0143 
0144   /// @brief evaluate alignment state for a single track
0145   ///
0146   /// @tparam source_link_t Source link type identifying uncalibrated input
0147   /// measurements.
0148   /// @tparam start_parameters_t Type of the initial parameters
0149   /// @tparam fit_options_t The fit options type
0150   ///
0151   /// @param gctx The current geometry context object
0152   /// @param sourceLinks The fittable uncalibrated measurements
0153   /// @param sParameters The initial track parameters
0154   /// @param fitOptions The fit Options steering the fit
0155   /// @param idxedAlignSurfaces The idxed surfaces to be aligned
0156   /// @param alignMask The alignment mask (same for all detector element for the
0157   /// moment)
0158   ///
0159   /// @result The alignment state for a single track
0160   template <typename source_link_t, typename start_parameters_t,
0161             typename fit_options_t>
0162   Acts::Result<detail::TrackAlignmentState> evaluateTrackAlignmentState(
0163       const Acts::GeometryContext& gctx,
0164       const std::vector<source_link_t>& sourceLinks,
0165       const start_parameters_t& sParameters, const fit_options_t& fitOptions,
0166       const std::unordered_map<const Acts::Surface*, std::size_t>&
0167           idxedAlignSurfaces,
0168       const AlignmentMask& alignMask) const;
0169 
0170   /// @brief calculate the alignment parameters delta
0171   ///
0172   /// @tparam trajectory_container_t The trajectories container type
0173   /// @tparam start_parameters_t The initial parameters container type
0174   /// @tparam fit_options_t The fit options type
0175   ///
0176   /// @param trajectoryCollection The collection of trajectories as input of
0177   /// fitting
0178   /// @param startParametersCollection The collection of starting parameters as
0179   /// input of fitting
0180   /// @param fitOptions The fit Options steering the fit
0181   /// @param alignResult [in, out] The aligned result
0182   /// @param alignMask The alignment mask (same for all measurements now)
0183   template <typename trajectory_container_t,
0184             typename start_parameters_container_t, typename fit_options_t>
0185   void calculateAlignmentParameters(
0186       const trajectory_container_t& trajectoryCollection,
0187       const start_parameters_container_t& startParametersCollection,
0188       const fit_options_t& fitOptions, AlignmentResult& alignResult,
0189       const AlignmentMask& alignMask = AlignmentMask::All) const;
0190 
0191   /// @brief update the detector element alignment parameters
0192   ///
0193   /// @param gctx The geometry context
0194   /// @param alignedDetElements The detector elements to be aligned
0195   /// @param alignedTransformUpdater The updater for updating the aligned
0196   /// @param alignResult [in, out] The aligned result
0197   Acts::Result<void> updateAlignmentParameters(
0198       const Acts::GeometryContext& gctx,
0199       const std::vector<Acts::DetectorElementBase*>& alignedDetElements,
0200       const AlignedTransformUpdater& alignedTransformUpdater,
0201       AlignmentResult& alignResult) const;
0202 
0203   /// @brief Alignment implementation
0204   ///
0205   /// @tparam trajectory_container_t The trajectories container type
0206   /// @tparam start_parameters_t The initial parameters container type
0207   /// @tparam fit_options_t The fit options type
0208   ///
0209   /// @param trajectoryCollection The collection of trajectories as input of
0210   /// fitting
0211   /// @param startParametersCollection The collection of starting parameters as
0212   /// input of fitting
0213   /// @param alignOptions The alignment options
0214   ///
0215   /// @result The alignment result
0216   template <typename trajectory_container_t,
0217             typename start_parameters_container_t, typename fit_options_t>
0218   Acts::Result<AlignmentResult> align(
0219       const trajectory_container_t& trajectoryCollection,
0220       const start_parameters_container_t& startParametersCollection,
0221       const AlignmentOptions<fit_options_t>& alignOptions) const;
0222 
0223  private:
0224   // The fitter
0225   fitter_t m_fitter;
0226 
0227   std::unique_ptr<const Acts::Logger> m_logger;
0228 
0229   const Acts::Logger& logger() const { return *m_logger; }
0230 };
0231 }  // namespace ActsAlignment
0232 
0233 #include "ActsAlignment/Kernel/Alignment.ipp"