Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:11:35

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/Algebra.hpp"
0012 #include "Acts/EventData/CompositeSpacePoint.hpp"
0013 #include "Acts/Seeding/detail/CompSpacePointAuxiliaries.hpp"
0014 #include "Acts/Utilities/CalibrationContext.hpp"
0015 #include "Acts/Utilities/Logger.hpp"
0016 
0017 #include <memory>
0018 #include <vector>
0019 
0020 namespace Acts::Experimental::detail {
0021 
0022 ///  @brief The FastStrawLineFitter fits a straight line to a set of straw measurements
0023 ///         The space points  passed to the fitter need to be all straw space
0024 ///         points and their straw wires need to be approximately parallel to
0025 ///         each other. Under this assumption, each straw measurements can be
0026 ///         projected onto the plane which is perpendicular to its straw wire
0027 ///         and the residual to the k-th straw is written as
0028 ///
0029 ///                 R_{k} = T_{z,k} * sin \theta - (T_{y,k} -  y_{0})* cos \theta - sign_{k} * r_{k}
0030 ///
0031 ///         where, \theta and y_{0} are the two line parameters to fit, T_{z,k} and T_{y,k} are
0032 ///         the coordinates of the straw-tube in the plane, sign_{k} fixes the
0033 ///         left-right ambiguity, and r_{k} is the drift radius of the
0034 ///         measurement. The z & y coordinates are given by the dot product of
0035 ///         the straw's local position with its planeNormal & toNextSensor
0036 ///         vector, respectively. Assuming that the drift radius does not need
0037 ///         to be calibrated during the fit, the entire problem reduces to the
0038 ///         minimization of a polynomial having the form
0039 ///
0040 ///                   A * cos(\theta)  + B * sin(\theta)  + C * cos(2\theta) + D * sin(2\theta)
0041 ///         where A, B, C, D are fit constants depending on the configuration of
0042 ///         the tubes to fit. They are documented in
0043 ///         https://gitlab.cern.ch/atlas-nextgen/work-package-2.5/analyticalsegment
0044 ///
0045 class FastStrawLineFitter {
0046  public:
0047   /// @brief abrivation of the residual indices to fetch the proper covariance
0048   using ResidualIdx = CompSpacePointAuxiliaries::ResidualIdx;
0049   /// @brief Vector type
0050   using Vector = CompSpacePointAuxiliaries::Vector;
0051   /// @brief Configuration object
0052   struct Config {
0053     /// @brief Number of maximum iterations
0054     std::size_t maxIter{10};
0055     /// @brief Cutoff to def
0056     double precCutOff{1.e-9};
0057   };
0058   /// @brief Constructor of the fast straw line fitter
0059   /// @param cfg: Reference to the fitter's configuration object
0060   /// @param logger: Optional overwrite of the logging object
0061   explicit FastStrawLineFitter(const Config& cfg,
0062                                std::unique_ptr<const Logger> logger =
0063                                    getDefaultLogger("FastStrawLineFitter",
0064                                                     Logging::Level::INFO));
0065 
0066   /// @brief Helper struct to pack the result of the straw line fit
0067   struct FitResult {
0068     virtual ~FitResult() = default;
0069     /// @brief Printer method
0070     virtual void print(std::ostream& ostr) const;
0071     /// @brief Ostream operator
0072     friend std::ostream& operator<<(std::ostream& ostr, const FitResult& x) {
0073       x.print(ostr);
0074       return ostr;
0075     }
0076     /// @brief Fitted inclination angle
0077     double theta{0.};
0078     /// @brief Uncertainty on the fitted angle
0079     double dTheta{0.};
0080     /// @brief Fitted line intercept
0081     double y0{0.};
0082     /// @brief Uncertainty on the intercept
0083     double dY0{0.};
0084     /// @brief Evaluated chi2 postfit
0085     double chi2{0.};
0086     /// @brief Number of degrees of freedom
0087     std::size_t nDoF{0};
0088     /// @brief Number of iterations to converge
0089     std::size_t nIter{0};
0090   };
0091 
0092   /// @brief Brief fit a straight line to a set of straw measurements and return
0093   ///        the theta angle & the intercept as well as the associated
0094   ///        uncertainties.
0095   /// @param measuremments: Collection of straw measurements to fit
0096   /// @param signs: Convention of whether the straw-line shall be left or right
0097   ///               of a particular straw measurements. Needs to have the same
0098   ///               length as the list of measurements.
0099   template <CompositeSpacePointContainer StrawCont_t>
0100   std::optional<FitResult> fit(const StrawCont_t& measurements,
0101                                const std::vector<int>& signs) const;
0102 
0103  private:
0104   /// @brief Index of the drift circle covariance inside the straw
0105   ///        space-point's covariance array
0106   static constexpr auto s_covIdx = toUnderlying(ResidualIdx::bending);
0107 
0108   ///@brief Auxiliary struct to calculate the fast-fit constants
0109   struct FitAuxiliaries {
0110     /// @brief Default destructor
0111     virtual ~FitAuxiliaries() = default;
0112     /// @brief default constructor
0113     FitAuxiliaries() = default;
0114     /// @brief move constructor
0115     FitAuxiliaries(FitAuxiliaries&& other) = default;
0116     /// @brief Printer method
0117     virtual void print(std::ostream& ostr) const;
0118     /// @brief Ostream operator
0119     friend std::ostream& operator<<(std::ostream& ostr,
0120                                     const FitAuxiliaries& x) {
0121       x.print(ostr);
0122       return ostr;
0123     }
0124     ///@brief Tube position center - y weighted with inverse covariances
0125     double centerY{0.};
0126     ///@brief Tube position center - z weighted with inverse covariances
0127     double centerZ{0.};
0128     /// @brief Inverse covariances per straw measurement
0129     std::vector<double> invCovs{};
0130     ///@brief One over the sum of the inverse straw measurement covariances
0131     double covNorm{0.};
0132     ///@brief Expectation value of T_{z}^{2} - T_{y}^{2}
0133     double T_zzyy{0.};
0134     ///@brief Expectation value of T_{y} * T_{z}
0135     double T_yz{0.};
0136     ///@brief Expectation value of T_{z} * r
0137     double T_rz{0.};
0138     ///@brief Expectation value of T_{y} * r
0139     double T_ry{0.};
0140     ///@brief Prediced y0 given as the expectation value of the radii
0141     ///         divided by the inverse covariance sum.
0142     double fitY0{0.};
0143     /// @brief number of degrees of freedom
0144     std::size_t nDoF{0};
0145   };
0146   /// @brief Small Helper struct to calculate sin & cos of theta & 2 theta
0147   struct TrigonomHelper {
0148     /// @brief Constructor passing the theta angle
0149     explicit TrigonomHelper(const double theta)
0150         : cosTheta{std::cos(theta)},
0151           sinTheta{std::sin(theta)},
0152           cosTwoTheta{std::cos(2. * theta)},
0153           sinTwoTheta{std::sin(2. * theta)} {}
0154     double cosTheta{0.};
0155     double sinTheta{0.};
0156     double cosTwoTheta{0.};
0157     double sinTwoTheta{0.};
0158   };
0159   /// @brief Calculate the pure angular derivatives of the chi2 w.r.t theta
0160   /// @param angles: Wrapper object to pass sin & cos of theta
0161   /// @param fitPars: Constants of the current fit configuration
0162   /// @param thetaPrime: Mutable reference to which the first derivative is written
0163   /// @param thetaTwoPrime: Mutable reference to which the second derivative is written
0164   void calcAngularDerivatives(const TrigonomHelper& angles,
0165                               const FitAuxiliaries& fitPars, double& thetaPrime,
0166                               double& thetaTwoPrime) const;
0167   /// @brief Calculate the fit constants for a set of straw circles
0168   /// @param measurements: List of straw measurements
0169   /// @param signs: List of left/right signs to be annotated with each straw circle
0170   template <CompositeSpacePointContainer StrawCont_t>
0171   FitAuxiliaries fillAuxiliaries(const StrawCont_t& measurements,
0172                                  const std::vector<int>& signs) const;
0173   /// @brief Evaluate the chi2 after the fit has converged.
0174   /// @param measurements: List of straw measurements that have been used in the fit
0175   /// @param result: Mutable reference to the FitResult object. The object is used
0176   ///                to fetch the fit parameters and to store the chi2 result
0177   template <CompositeSpacePointContainer StrawCont_t>
0178   void calcPostFitChi2(const StrawCont_t& measurements,
0179                        FitResult& result) const;
0180   /// @brief Calculate the chi2 term from a straw measurement given the known theta, y0
0181   /// @param angle: Helper struct caching the cosTheta & sinTheta
0182   /// @param y0: Intercept of the fitted line
0183   /// @param strawMeas: Reference to the straw measurement of interest
0184   /// @param r: Optional updated calibrated straw radius
0185   template <CompositeSpacePoint Point_t>
0186   double chi2Term(const TrigonomHelper& angle, const double y0,
0187                   const Point_t& strawMeas,
0188                   std::optional<double> r = std::nullopt) const;
0189   /// @brief Fit the track inclination angle and calculate the intercept
0190   ///        afterwards
0191   /// @param fitPars: Constants of the current measurement configuration
0192   std::optional<FitResult> fit(const FitAuxiliaries& fitPars) const;
0193   /// @brief Calculate the starting parameters on theta from the fit constants
0194   static double startTheta(const FitAuxiliaries& fitPars);
0195   /// @brief Fill the y0 and the uncertainties on theta and y0 in the result
0196   /// @param fitPars: Fit constants from the straw measurements
0197   /// @param thetaTwoPrime: Second derivative of the chi2 w.r.t theta
0198   /// @param result: Mutable reference to the FitResult object. The updated parameter are written
0199   ///                to this object
0200   void completeResult(const FitAuxiliaries& fitPars, const double thetaTwoPrime,
0201                       FitResult& result) const;
0202 
0203   const Config m_cfg{};
0204   std::unique_ptr<const Acts::Logger> m_logger{};
0205 
0206   const Acts::Logger& logger() const;
0207 };
0208 
0209 }  // namespace Acts::Experimental::detail
0210 #include "Acts/Seeding/detail/FastStrawLineFitter.ipp"