Back to home page

EIC code displayed by LXR

 
 

    


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

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/Geometry/GeometryIdentifier.hpp"
0012 
0013 #include <algorithm>
0014 #include <cassert>
0015 #include <initializer_list>
0016 #include <iterator>
0017 #include <stdexcept>
0018 #include <utility>
0019 #include <vector>
0020 
0021 namespace Acts {
0022 
0023 /// Store values mapped into the geometry hierarchy.
0024 ///
0025 /// @tparam value_t stored value type
0026 ///
0027 /// The core functionality is to find an equivalent element, i.e. an
0028 /// identifier-value pair, for a given geometry identifier via
0029 ///
0030 ///     auto it = container.find(GeometryIdentifier(...));
0031 ///     if (it != container.end()) {
0032 ///         ...
0033 ///     }
0034 ///
0035 /// Trailing zero levels of stored geometry identifiers are used as
0036 /// broadcast values to refer to higher-level objects within the geometry, e.g.
0037 /// a geometry identifier with vanishing approach and sensitive index identifies
0038 /// a layer. An entry will all geometry identifier levels set to zero acts as
0039 /// the global default value.
0040 ///
0041 /// The container also supports range-based iteration over all stored elements
0042 ///
0043 ///     for (const auto& element : container) {
0044 ///         ...
0045 ///     }
0046 ///
0047 /// and index-based access to stored elements and associated geometry
0048 /// identifiers
0049 ///
0050 ///     GeometryIdentifier id3 = container.idAt(3);
0051 ///     const auto& element4 = container.valueAt(4);
0052 ///
0053 /// @note No guarantees are given for the element order when using range-based
0054 ///   or index-based access. Any apparent ordering must be considered an
0055 ///   implementation detail and might change.
0056 ///
0057 /// Adding elements is potentially expensive as the internal lookup structure
0058 /// must be updated. In addition, modifying an element in-place could change its
0059 /// identifier which would also break the lookup. Thus, the container can not be
0060 /// modified after construction to prevent misuse.
0061 template <typename value_t>
0062 class GeometryHierarchyMap {
0063  public:
0064   /// Combined geometry identifier and value element. Only used for input.
0065   using InputElement = typename std::pair<GeometryIdentifier, value_t>;
0066   using Iterator = typename std::vector<value_t>::const_iterator;
0067   using Value = value_t;
0068 
0069   /// Construct the container from the given elements.
0070   ///
0071   /// @param elements input elements (must be unique with respect to identifier)
0072   GeometryHierarchyMap(std::vector<InputElement> elements);
0073 
0074   /// Construct the container from an initializer list.
0075   ///
0076   /// @param elements input initializer list
0077   GeometryHierarchyMap(std::initializer_list<InputElement> elements);
0078 
0079   // defaulted constructors and assignment operators
0080   GeometryHierarchyMap() = default;
0081   GeometryHierarchyMap(const GeometryHierarchyMap&) = default;
0082   GeometryHierarchyMap(GeometryHierarchyMap&&) noexcept = default;
0083   ~GeometryHierarchyMap() = default;
0084   GeometryHierarchyMap& operator=(const GeometryHierarchyMap&) = default;
0085   GeometryHierarchyMap& operator=(GeometryHierarchyMap&&) noexcept = default;
0086 
0087   /// Return an iterator pointing to the beginning of the stored values.
0088   Iterator begin() const { return m_values.begin(); }
0089 
0090   /// Return an iterator pointing to the end of the stored values.
0091   Iterator end() const { return m_values.end(); }
0092 
0093   /// Check if any elements are stored.
0094   bool empty() const { return m_values.empty(); }
0095 
0096   /// Return the number of stored elements.
0097   std::size_t size() const { return m_values.size(); }
0098 
0099   /// Access the geometry identifier for the i-th element with bounds check.
0100   ///
0101   /// @throws std::out_of_range for invalid indices
0102   GeometryIdentifier idAt(std::size_t index) const { return m_ids.at(index); }
0103 
0104   /// Access the value of the i-th element in the container with bounds check.
0105   ///
0106   /// @throws std::out_of_range for invalid indices
0107   const Value& valueAt(std::size_t index) const { return m_values.at(index); }
0108 
0109   /// Find the most specific value for a given geometry identifier.
0110   ///
0111   /// This can be either from the element matching exactly to the given geometry
0112   /// id, if it exists, or from the element for the next available higher level
0113   /// within the geometry hierarchy.
0114   ///
0115   /// @param id geometry identifier for which information is requested
0116   /// @retval iterator to an existing value
0117   /// @retval `.end()` iterator if no matching element exists
0118   Iterator find(const GeometryIdentifier& id) const;
0119 
0120   /// Check if the most specific value exists for a given geometry identifier.
0121   ///
0122   /// This function checks if there is an element matching exactly the given
0123   /// geometry id, or from the element for the next available higher level
0124   /// within the geometry hierarchy.
0125   ///
0126   /// @param id geometry identifier for which existence is being checked
0127   /// @retval `true` if a matching element exists
0128   /// @retval `false` if no matching element exists
0129   bool contains(const GeometryIdentifier& id) const;
0130 
0131  private:
0132   // NOTE this class assumes that it knows the ordering of the levels within
0133   //      the geometry id. if the geometry id changes, this code has to be
0134   //      adapted too. the asserts ensure that such a change is caught.
0135   static_assert(GeometryIdentifier().setVolume(1).value() <
0136                     GeometryIdentifier().setVolume(1).setBoundary(1).value(),
0137                 "Incompatible GeometryIdentifier hierarchy");
0138   static_assert(GeometryIdentifier().setBoundary(1).value() <
0139                     GeometryIdentifier().setBoundary(1).setLayer(1).value(),
0140                 "Incompatible GeometryIdentifier hierarchy");
0141   static_assert(GeometryIdentifier().setLayer(1).value() <
0142                     GeometryIdentifier().setLayer(1).setApproach(1).value(),
0143                 "Incompatible GeometryIdentifier hierarchy");
0144   static_assert(GeometryIdentifier().setApproach(1).value() <
0145                     GeometryIdentifier().setApproach(1).setSensitive(1).value(),
0146                 "Incompatible GeometryIdentifier hierarchy");
0147 
0148   using Identifier = GeometryIdentifier::Value;
0149 
0150   // encoded ids for all elements for faster lookup.
0151   std::vector<Identifier> m_ids;
0152   // validity bit masks for the ids: which parts to use for comparison
0153   std::vector<Identifier> m_masks;
0154   std::vector<Value> m_values;
0155 
0156   /// Construct a mask where all leading non-zero levels are set.
0157   static constexpr Identifier makeLeadingLevelsMask(GeometryIdentifier id) {
0158     // construct id from encoded value with all bits set
0159     auto allSet = GeometryIdentifier(~GeometryIdentifier::Value{0u});
0160     // manually iterate over identifier levels starting from the lowest
0161     if (id.sensitive() != 0u) {
0162       // all levels are valid; keep all bits set.
0163       return allSet.setExtra(0u).value();
0164     }
0165     if (id.approach() != 0u) {
0166       return allSet.setExtra(0u).setSensitive(0u).value();
0167     }
0168     if (id.layer() != 0u) {
0169       return allSet.setExtra(0u).setSensitive(0u).setApproach(0u).value();
0170     }
0171     if (id.boundary() != 0u) {
0172       return allSet.setExtra(0u)
0173           .setSensitive(0u)
0174           .setApproach(0u)
0175           .setLayer(0u)
0176           .value();
0177     }
0178     if (id.volume() != 0u) {
0179       return allSet.setExtra(0u)
0180           .setSensitive(0u)
0181           .setApproach(0u)
0182           .setLayer(0u)
0183           .setBoundary(0u)
0184           .value();
0185     }
0186     // no valid levels; all bits are zero.
0187     return Identifier{0u};
0188   }
0189 
0190   /// Construct a mask where only the highest level is set.
0191   static constexpr Identifier makeHighestLevelMask() {
0192     return makeLeadingLevelsMask(GeometryIdentifier(0u).setVolume(1u));
0193   }
0194 
0195   /// Compare the two identifiers only within the masked bits.
0196   static constexpr bool equalWithinMask(Identifier lhs, Identifier rhs,
0197                                         Identifier mask) {
0198     return (lhs & mask) == (rhs & mask);
0199   }
0200 
0201   /// Ensure identifier ordering and uniqueness.
0202   static void sortAndCheckDuplicates(std::vector<InputElement>& elements);
0203 
0204   /// Fill the container from the input elements.
0205   ///
0206   /// This assumes that the elements are ordered and unique with respect to
0207   /// their identifiers.
0208   void fill(const std::vector<InputElement>& elements);
0209 };
0210 
0211 // implementations
0212 
0213 template <typename value_t>
0214 inline GeometryHierarchyMap<value_t>::GeometryHierarchyMap(
0215     std::vector<InputElement> elements) {
0216   sortAndCheckDuplicates(elements);
0217   fill(elements);
0218 }
0219 
0220 template <typename value_t>
0221 inline GeometryHierarchyMap<value_t>::GeometryHierarchyMap(
0222     std::initializer_list<InputElement> elements)
0223     : GeometryHierarchyMap(
0224           std::vector<InputElement>(elements.begin(), elements.end())) {}
0225 
0226 template <typename value_t>
0227 inline void GeometryHierarchyMap<value_t>::sortAndCheckDuplicates(
0228     std::vector<InputElement>& elements) {
0229   // ensure elements are sorted by identifier
0230   std::ranges::sort(elements, [=](const auto& lhs, const auto& rhs) {
0231     return lhs.first < rhs.first;
0232   });
0233 
0234   // Check that all elements have unique identifier
0235   auto dup = std::ranges::adjacent_find(
0236       elements,
0237       [](const auto& lhs, const auto& rhs) { return lhs.first == rhs.first; });
0238 
0239   if (dup != elements.end()) {
0240     throw std::invalid_argument("Input elements contain duplicates");
0241   }
0242 }
0243 
0244 template <typename value_t>
0245 inline void GeometryHierarchyMap<value_t>::fill(
0246     const std::vector<InputElement>& elements) {
0247   m_ids.clear();
0248   m_masks.clear();
0249   m_values.clear();
0250 
0251   m_ids.reserve(elements.size());
0252   m_masks.reserve(elements.size());
0253   m_values.reserve(elements.size());
0254 
0255   for (const auto& element : elements) {
0256     m_ids.push_back(element.first.value());
0257     m_masks.push_back(makeLeadingLevelsMask(element.first.value()));
0258     m_values.push_back(std::move(element.second));
0259   }
0260 }
0261 
0262 template <typename value_t>
0263 inline auto GeometryHierarchyMap<value_t>::find(
0264     const GeometryIdentifier& id) const -> Iterator {
0265   assert((m_ids.size() == m_values.size()) &&
0266          "Inconsistent container state: #ids != # values");
0267   assert((m_masks.size() == m_values.size()) &&
0268          "Inconsistent container state: #masks != #values");
0269 
0270   // we can not search for the element directly since the relevant one
0271   // might be stored at a higher level. ids for higher levels would always
0272   // be sorted before the requested id. searching for the first element
0273   // after the requested ensures that we include the full hierarchy.
0274   const auto it = std::upper_bound(m_ids.begin(), m_ids.end(), id.value());
0275   auto i = std::distance(m_ids.begin(), it);
0276 
0277   // now go up the hierarchy to find the first matching element.
0278   // example: the container stores four identifiers
0279   //
0280   //     2|x|x (volume-only)
0281   //     2|2|1 (volume, layer, and sensitive)
0282   //     2|3|x (volume and layer)
0283   //     2|3|4 (volume, layer, and sensitive)
0284   //
0285   // where | marks level boundaries. searching for either 2|3|4, 2|3|7, or
0286   // 2|4|x would first point to 2|3|4 and thus needs to go up the hierarchy.
0287   while (0 < i) {
0288     // index always starts after item of interest due to upper bound search
0289     --i;
0290 
0291     // if the input id does not even match at the highest hierarchy level
0292     // with the current comparison id, then have reached the end of this
0293     // hierarchy. having a special check for the highest level avoids an
0294     // unbounded search window all the way to the beginning of the container for
0295     // the global default entry.
0296     if (!equalWithinMask(id.value(), m_ids[i], makeHighestLevelMask())) {
0297       // check if a global default entry exists
0298       if (m_ids.front() == Identifier{0u}) {
0299         return begin();
0300       } else {
0301         return end();
0302       }
0303     }
0304 
0305     // since the search is going backwards in the sorted container, it
0306     // progresses from more specific to less specific elements. the first
0307     // match is automatically the appropriate one.
0308     if (equalWithinMask(id.value(), m_ids[i], m_masks[i])) {
0309       return std::next(begin(), i);
0310     }
0311   }
0312 
0313   // all options are exhausted and no matching element was found.
0314   return end();
0315 }
0316 
0317 template <typename value_t>
0318 inline auto GeometryHierarchyMap<value_t>::contains(
0319     const GeometryIdentifier& id) const -> bool {
0320   return this->find(id) != this->end();
0321 }
0322 
0323 }  // namespace Acts