Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:00

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2023 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Units.hpp"
0013 #include "Acts/Utilities/Logger.hpp"
0014 #include "Acts/Utilities/Ray.hpp"
0015 #include "Acts/Utilities/Result.hpp"
0016 #include "Acts/Utilities/detail/periodic.hpp"
0017 
0018 #include <algorithm>
0019 #include <string>
0020 #include <utility>
0021 #include <vector>
0022 
0023 namespace Acts {
0024 
0025 /// @class SingleSeedVertexFinder
0026 ///
0027 /// @brief Implements the vertex finder based on the track seeds
0028 /// 0. Assumes there is only 1 vertex and that it has a high multiplicity
0029 /// 1. Sorts out all the input spacepoints based on their distance to the z-axis
0030 /// 2. Create seeds from 3 spacepoints with a small deviation from a straigh
0031 /// line
0032 /// 3. Find a point with a minimal distance from either planes
0033 /// (minimalizeWRT="planes") or rays (minimalizeWRT="rays") defined by the seeds
0034 /// 4. Returns the point position as the vertex
0035 template <typename spacepoint_t>
0036 class SingleSeedVertexFinder {
0037  public:
0038   /// Configuration struct
0039   struct Config {
0040     /// maximum deviation in phi between the near and middle spacepoints or
0041     /// middle and far spacepoints
0042     Acts::ActsScalar maxPhideviation = 0.08;
0043     /// maximum deviation in X-Y between the first 2 spacepoints and the last 2
0044     /// spacepoints
0045     Acts::ActsScalar maxXYdeviation = 0.08;
0046     /// maximum deviation in 3D between the first 2 spacepoints and the last 2
0047     /// spacepoints
0048     Acts::ActsScalar maxXYZdeviation = 0.08;
0049 
0050     /// minimum angle between Z axis and a triplet, effectively removing
0051     /// triplets with large |eta|
0052     Acts::ActsScalar minTheta = 1.;
0053 
0054     /// thresholds for near, middle, and far spacepoints
0055     Acts::ActsScalar rMinNear = 20.f * Acts::UnitConstants::mm;
0056     Acts::ActsScalar rMaxNear = 60.f * Acts::UnitConstants::mm;
0057     Acts::ActsScalar rMinMiddle = 150.f * Acts::UnitConstants::mm;
0058     Acts::ActsScalar rMaxMiddle = 190.f * Acts::UnitConstants::mm;
0059     Acts::ActsScalar rMinFar = 280.f * Acts::UnitConstants::mm;
0060     Acts::ActsScalar rMaxFar = 320.f * Acts::UnitConstants::mm;
0061 
0062     /// number of phi slices, at least 3 to avoid duplicities
0063     /// it should be less than 2*pi/maxPhideviation in order not to loop over
0064     /// triplets that will be rejected by maxPhideviation anyway
0065     std::uint32_t numPhiSlices = 60;
0066     /// use only a fraction of available phi slices to speed up calculations;
0067     Acts::ActsScalar useFracPhiSlices = 0.5;
0068 
0069     /// number of z slices
0070     std::uint32_t numZSlices = 150;
0071     /// use only a fraction of available z slices to speed up calculations;
0072     Acts::ActsScalar useFracZSlices = 0.5;
0073     /// maximum |z| to consider, z slices will be done within the range
0074     /// (-maxAbsZ,maxAbsZ)
0075     /// values of maxAbsZ, maxZPosition, rMaxFar, and minTheta should be
0076     /// set reasonably with respect to each other
0077     Acts::ActsScalar maxAbsZ = 450. * Acts::UnitConstants::mm;
0078 
0079     /// maximum Z position of the vertex at the point closest to the Z axis
0080     Acts::ActsScalar maxZPosition = 200.f * Acts::UnitConstants::mm;
0081     /// maximum R position of the vertex at the point closest to the Z axis
0082     Acts::ActsScalar maxRPosition = 10.f * Acts::UnitConstants::mm;
0083 
0084     /// chi^2 minimalization will happen with respect to "planes" or "rays"
0085     std::string minimalizeWRT = "planes";
0086 
0087     /// maximum number of iterations when discarding triplets with the largest
0088     /// chi^2
0089     std::uint32_t maxIterations = 20;
0090     /// each iteration, discard this fraction of triplets with the largest chi^2
0091     Acts::ActsScalar removeFraction = 0.10;
0092     /// if the vertex estimation moves less than this, stop iterations
0093     Acts::ActsScalar minVtxShift = 0.3f * Acts::UnitConstants::mm;
0094   };
0095 
0096   /// Const access to the config
0097   const Config& config() const { return m_cfg; }
0098 
0099   /// @brief Constructor
0100   /// @param cfg Configuration object
0101   /// @param lgr Logging instance
0102   SingleSeedVertexFinder(const Config& cfg,
0103                          std::unique_ptr<const Logger> lgr = getDefaultLogger(
0104                              "SingleSeedVertexFinder", Logging::INFO));
0105 
0106   /// @brief Destructor
0107   ~SingleSeedVertexFinder() = default;
0108 
0109   /// @brief Finds the vertex based on the provided spacepoints
0110   /// @param spacepoints Vector of the input spacepoints; they do not need to be sorted anyhow
0111   /// @return Position of the vertex
0112   Acts::Result<Acts::Vector3> findVertex(
0113       const std::vector<spacepoint_t>& spacepoints) const;
0114 
0115  private:
0116   /// @brief Struct to store spacepoint combinations from near, middle, and far parts of the detector. Also stores straight line fit through the spacepoints in case minimalizeWRT=="rays", so it's not fitted twice
0117   struct Triplet {
0118     Triplet(const spacepoint_t& aa, const spacepoint_t& bb,
0119             const spacepoint_t& cc)
0120         : a(aa), b(bb), c(cc), ray(Acts::Vector3::Zero(), {1., 1., 1.}) {}
0121 
0122     const spacepoint_t &a, &b, &c;
0123     Acts::Ray3D ray;
0124   };
0125 
0126   /// @brief Struct to store sorted spacepoints for each layer (near, middle, and far), for each slice of phi, and for each slice of z
0127   struct SortedSpacepoints {
0128     SortedSpacepoints(const int phiSize, const int zSize) {
0129       std::vector<std::pair<spacepoint_t const*, Acts::ActsScalar>> helper = {};
0130       std::vector<std::vector<std::pair<spacepoint_t const*, Acts::ActsScalar>>>
0131           helperZ(zSize, helper);
0132       std::vector<std::vector<
0133           std::vector<std::pair<spacepoint_t const*, Acts::ActsScalar>>>>
0134           helperPhi(phiSize, helperZ);
0135       sortedSP.fill(helperPhi);
0136     }
0137 
0138     /// @brief Provides non-const vector of spacepoints for a given layer, phi slice, and z slice
0139     /// @param layer Index of the layer (near=0, middle=1, far=2)
0140     /// @param phi Index of the phi slice
0141     /// @param z Index of the z slice
0142     /// @return Non-const vector of spacepoints
0143     inline std::vector<std::pair<spacepoint_t const*, Acts::ActsScalar>>& addSP(
0144         int layer, int phi, int z) {
0145       return sortedSP[layer][phi][z];
0146     }
0147 
0148     /// @brief Provides const vector of spacepoints for a given layer, phi slice, and z slice
0149     /// @param layer Index of the layer (near=0, middle=1, far=2)
0150     /// @param phi Index of the phi slice
0151     /// @param z Index of the z slice
0152     /// @return Const vector of spacepoints
0153     inline const std::vector<std::pair<spacepoint_t const*, Acts::ActsScalar>>&
0154     getSP(int layer, int phi, int z) const {
0155       return sortedSP[layer][phi][z];
0156     }
0157 
0158     std::array<std::vector<std::vector<std::vector<
0159                    std::pair<spacepoint_t const*, Acts::ActsScalar>>>>,
0160                3>
0161         sortedSP;
0162   };
0163 
0164   /// Configuration instance
0165   Config m_cfg;
0166 
0167   /// @brief Sorts spacepoints into a separate vectors for near, middle, and far spacepoints; for each slice of phi; and for each slice of z
0168   /// @param spacepoints Vector of the input spacepoints;
0169   /// @return Struct of the sorted spacepoints
0170   Acts::SingleSeedVertexFinder<spacepoint_t>::SortedSpacepoints sortSpacepoints(
0171       const std::vector<spacepoint_t>& spacepoints) const;
0172 
0173   /// @brief Makes triplets from the provided vectors of near, middle, and far spacepoints; for each slice of phi; and for each slice of z
0174   /// @param sortedSpacepoints Struct of the sorted spacepointss
0175   /// @return Vector of valid triplets
0176   std::vector<Triplet> findTriplets(
0177       const Acts::SingleSeedVertexFinder<spacepoint_t>::SortedSpacepoints&
0178           sortedSpacepoints) const;
0179 
0180   /// @brief Validate the triplet based on "maxXYdeviation", "maxXYZdeviation", "maxZPosition", and "maxRPosition"
0181   /// @param triplet A single triplet to be validated
0182   /// @return True if the deviations and fitted ray are within the configured ranges
0183   ///         If "minimalizeWRT"=="rays", then the fitted ray is also saved to
0184   ///         the triplet for later
0185   bool tripletValidationAndUpdate(Triplet& triplet) const;
0186 
0187   /// @brief Calculates equation of the plane (alpha*x + beta*y + gamma*z + delta = 0), given the three points
0188   /// @param triplet A single triplet (with 3 spacepoints)
0189   /// @return A pair of {{alpha,beta,gamma},delta}
0190   static std::pair<Acts::Vector3, Acts::ActsScalar> makePlaneFromTriplet(
0191       const Triplet& triplet);
0192 
0193   /// @brief Find a point (=the vertex) that has minimum chi^2 with respect to all planes defined by the triplets
0194   /// @param triplets Vector of all valid triplets
0195   /// @return Position {x,y,z} of the vertex
0196   Acts::Vector3 findClosestPointFromPlanes(
0197       const std::vector<Triplet>& triplets) const;
0198 
0199   /// @brief Calculates parameters of the ray (starting point + direction), given the three points
0200   /// @param triplet A single triplet (with 3 spacepoints)
0201   /// @return A ray of {starting_point, direction}
0202   static Acts::Ray3D makeRayFromTriplet(const Triplet& triplet);
0203 
0204   /// @brief Find a point (=the vertex) that has minimum chi^2 with respect to all rays fitted through the triplets
0205   /// @param triplets Vector of all valid triplets
0206   /// @return Position {x,y,z} of the vertex
0207   Acts::Vector3 findClosestPointFromRays(
0208       const std::vector<Triplet>& triplets) const;
0209 
0210   /// Logging instance
0211   std::unique_ptr<const Logger> m_logger;
0212 
0213   /// Private access to logging instance
0214   const Logger& logger() const { return *m_logger; }
0215 };
0216 
0217 }  // namespace Acts
0218 
0219 #include "Acts/Vertexing/SingleSeedVertexFinder.ipp"