Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-12 07:51:39

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