Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-19 07:57:19

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 <vector>
0012 
0013 namespace Acts {
0014 
0015 /// @brief A description of a triplet candidate.
0016 /// @tparam external_space_point_t  The external spacepoint type.
0017 template <typename external_space_point_t>
0018 struct TripletCandidate {
0019   /// @brief Default Constructor
0020   TripletCandidate() = default;
0021 
0022   /// @brief constructor
0023   /// @param b The bottom space point
0024   /// @param m The middle space point
0025   /// @param t The top space point
0026   /// @param w The quality of the candidate
0027   /// @param z The z coordinate of the origin
0028   /// @param q Whether the candidate is high or low quality
0029   TripletCandidate(external_space_point_t& b, external_space_point_t& m,
0030                    external_space_point_t& t, float w, float z, bool q)
0031       : bottom(&b), middle(&m), top(&t), weight(w), zOrigin(z), isQuality(q) {}
0032 
0033   /// Pointer to bottom space point of the triplet candidate
0034   external_space_point_t* bottom{nullptr};
0035   /// Pointer to middle space point of the triplet candidate
0036   external_space_point_t* middle{nullptr};
0037   /// Pointer to top space point of the triplet candidate
0038   external_space_point_t* top{nullptr};
0039   /// Quality weight of the triplet candidate
0040   float weight{0.};
0041   /// Z-coordinate of the estimated track origin
0042   float zOrigin{0.};
0043   /// Flag indicating whether this is a high-quality candidate
0044   bool isQuality{false};
0045 };
0046 
0047 /// @class CandidatesForMiddleSp
0048 /// The CandidatesForMiddleSp collects the triplet candidates given a
0049 /// fixed middle spacepoint. It internally stores the triplet candidates
0050 /// keeping only those with the higher quality.
0051 ///
0052 /// @tparam external_space_point_t The external spacepoint type.
0053 
0054 template <typename external_space_point_t>
0055 concept SatisfyCandidateConcept = requires(external_space_point_t spacePoint) {
0056   { spacePoint.x() } -> std::convertible_to<float>;
0057   { spacePoint.y() } -> std::convertible_to<float>;
0058   { spacePoint.z() } -> std::convertible_to<float>;
0059 };
0060 
0061 template <SatisfyCandidateConcept external_space_point_t>
0062 class CandidatesForMiddleSp {
0063  public:
0064   /// Type alias for triplet candidate type stored in this container
0065   using value_type = TripletCandidate<external_space_point_t>;
0066 
0067   /// @brief Setting maximum number of candidates to keep
0068   /// @param nLow Maximum number of candidates in the low-quality collection
0069   /// @param nHigh Maximum number of candidates in the high-quality collection
0070   void setMaxElements(std::size_t nLow, std::size_t nHigh);
0071 
0072   /// @brief Retrieve the triplet candidates, the resulting vector is already sorted,
0073   /// elements with higher quality first
0074   /// @returns Vector of triplet candidates
0075   std::vector<value_type> storage();
0076 
0077   /// @brief Adding a new triplet candidate to the collection, should it satisfy the
0078   /// selection criteria
0079   /// @param spB Bottom space point
0080   /// @param spM Medium space point
0081   /// @param spT Top space point
0082   /// @param weight The quality of the triplet candidate
0083   /// @param zOrigin The z-coordinate of the origin
0084   /// @param isQuality Whether the triplet candidate is high or low quality
0085   /// @returns whether the triplet candidate has been added or not to the collection
0086   bool push(external_space_point_t& spB, external_space_point_t& spM,
0087             external_space_point_t& spT, float weight, float zOrigin,
0088             bool isQuality);
0089 
0090   /// @brief Clear the internal storage
0091   void clear();
0092 
0093   /// @brief A function for sorting the triplet candidates from higher to lower quality
0094   /// @param i1 First triplet candidate
0095   /// @param i2 Second triplet candidate
0096   /// @returns The comparison result
0097   static bool descendingByQuality(const value_type& i1, const value_type& i2);
0098 
0099   /// @brief A function for sorting the triplet candidates from lower to higher quality
0100   /// @param i1 First triplet candidate
0101   /// @param i2 Second triplet candidate
0102   /// @returns The comparison result
0103   static bool ascendingByQuality(const value_type& i1, const value_type& i2);
0104 
0105   /// @brief Retrieve the number of Low quality candidates
0106   /// @returns The number of Low quality candidates
0107   std::size_t nLowQualityCandidates() const;
0108 
0109   /// @brief Retrieve the number of High quality candidates
0110   /// @returns The number of High quality candidates
0111   std::size_t nHighQualityCandidates() const;
0112 
0113  private:
0114   /// @brief Adding a new triplet candidate to the collection, should it satisfy the
0115   /// selection criteria
0116   /// @param indices The collection into which the candidate should be stored
0117   /// @param n The current number of stored elements in the container
0118   /// @param nMax The maximum number of elements that can be stored in the container
0119   /// @param spB The bottom space point
0120   /// @param spM The middle space point
0121   /// @param spT The top space point
0122   /// @param weight The quality of the triplet candidate
0123   /// @param zOrigin The z-coordinate of the origin
0124   /// @param isQuality Whether the triplet candidate is high or low quality
0125   /// @returns whether the triplet candidate has been added or not to the collection
0126   bool push(std::vector<std::size_t>& indices, std::size_t& n,
0127             const std::size_t nMax, external_space_point_t& spB,
0128             external_space_point_t& spM, external_space_point_t& spT,
0129             float weight, float zOrigin, bool isQuality);
0130 
0131   /// @brief Check if an element exists in the collection. The element to be checked
0132   /// is supposed to be in the n position of the collection.
0133   /// @param n Index of the requested element
0134   /// @param maxSize Number of elements currently stored in the collection
0135   /// @returns Whether the element exists
0136   bool exists(const std::size_t n, const std::size_t maxSize) const;
0137 
0138   /// @brief Pop an element from a collection. The removal of the element from the collection
0139   /// does not imply its destruction. In fact, the number of stored elements is
0140   /// simply diminished by 1. The popped element is technically still available
0141   /// at the end of the collection.
0142   /// @param indices The collection
0143   /// @param currentSize The current number of element stored in the collection. The function will
0144   /// diminish this value by 1
0145   void pop(std::vector<std::size_t>& indices, std::size_t& currentSize);
0146 
0147   /// @brief Return the weight for a candidate
0148   /// @param indices The collection in which the element is stored
0149   /// @param n Index of the element in the collection
0150   /// @returns The weight of the candidate
0151   float weight(const std::vector<std::size_t>& indices, std::size_t n) const;
0152 
0153   /// @brief Move an element up in the min heap tree. The function checks whether the element's
0154   /// weight is lower of its parent's weight. If so, it swaps them. Reiterate
0155   /// the process until the element is in the correct position on the tree
0156   /// @param indices The collection
0157   /// @param n The index of the element to place in the correct position
0158   void bubbleup(std::vector<std::size_t>& indices, std::size_t n);
0159 
0160   /// @brief Move an element down in the min heap tree. The function checks whether the elements's
0161   /// weight is lower of its child's weights. If so, it swaps the element with
0162   /// the child with the lowest weight. Reiterate the process until the element
0163   /// is in the correct position on the tree
0164   /// @param indices The collection
0165   /// @param n The index of the element to place in the correct position
0166   /// @param actualSize The current number of elements stored in the collection
0167   void bubbledw(std::vector<std::size_t>& indices, std::size_t n,
0168                 std::size_t actualSize);
0169 
0170   /// @brief Adding a new triplet candidate to the collection. The function is called after the candidate has satisfied
0171   /// all the selection criteria
0172   /// @param indices The collection
0173   /// @param n Current number of stored elements in the collection
0174   /// @param nMax The maximum number of elements that can be stored in the collection
0175   /// @param element The element that must be added to the collection
0176   void addToCollection(std::vector<std::size_t>& indices, std::size_t& n,
0177                        const std::size_t nMax, value_type&& element);
0178 
0179  private:
0180   // sizes
0181   // m_maxSize* is the maximum size of the indices collections. These values
0182   // are set by the user once
0183   std::size_t m_maxSizeHigh{0};
0184   std::size_t m_maxSizeLow{0};
0185   // m_n_* is the current size of the indices collections [0, m_maxSize*).
0186   // These values are set internally by the class
0187   std::size_t m_nHigh{0};
0188   std::size_t m_nLow{0};
0189 
0190   // storage contains the collection of the candidates
0191   std::vector<value_type> m_storage{};
0192 
0193   // The following vectors store indexes to elements in the storage
0194   // They are sorted as a min heap tree, in which
0195   // Each node is lower than its children
0196   // Thus, it is guaranteed that the lower elements is at the front
0197   // Sorting criteria is the seed quality
0198   //
0199   // This is in effect faster sorted container - implementation with std::set
0200   // and std::priority_queue were tried and were found to be slower.
0201 
0202   // list of indexes of candidates with high quality in the storage
0203   std::vector<std::size_t> m_indicesHigh{};
0204   // list of indexes of candidates with low quality in the storage
0205   std::vector<std::size_t> m_indicesLow{};
0206 };
0207 
0208 }  // namespace Acts
0209 
0210 #include "Acts/Seeding/CandidatesForMiddleSp.ipp"