Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-11 07:50:01

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 ///////////////////////////////////////////////////////////////////
0010 // BinnedArrayXD.h, Acts project
0011 ///////////////////////////////////////////////////////////////////
0012 
0013 #pragma once
0014 
0015 #include "Acts/Utilities/BinUtility.hpp"
0016 #include "Acts/Utilities/BinnedArray.hpp"
0017 #include "Acts/Utilities/Helpers.hpp"
0018 
0019 #include <array>
0020 #include <iostream>
0021 #include <vector>
0022 
0023 class MsgStream;
0024 
0025 namespace Acts {
0026 
0027 /// @class BinnedArrayXD
0028 ///
0029 /// Avoiding a map search, the templated BinnedArray class can help
0030 /// ordereing geometrical objects by providing a dedicated BinUtility.
0031 ///
0032 /// This can be 0D, 1D, 2D, and 3D in regular binning
0033 ///
0034 /// the type of Binning is given defined through the BinUtility
0035 template <class T>
0036 class BinnedArrayXD : public BinnedArray<T> {
0037   /// typedef the object and position for readability
0038   using TAP = std::pair<T, Vector3>;
0039 
0040  public:
0041   /// Constructor for single object
0042   ///
0043   /// @tparam object is the single object
0044   explicit BinnedArrayXD(T object)
0045       : BinnedArray<T>(),
0046         m_objectGrid(
0047             1, std::vector<std::vector<T>>(1, std::vector<T>(1, nullptr))),
0048         m_arrayObjects({object}),
0049         m_binUtility(nullptr) {
0050     /// fill the single object into the object grid
0051     m_objectGrid[0][0][0] = object;
0052   }
0053 
0054   /// Constructor with std::vector and a BinUtility
0055   /// - fills the internal data structure
0056   ///
0057   /// @param tapvector is a vector of object and binning position
0058   /// @param bu is the unique bin utility for this binned array
0059   BinnedArrayXD(const std::vector<TAP>& tapvector,
0060                 std::unique_ptr<const BinUtility> bu)
0061       : BinnedArray<T>(),
0062         m_objectGrid(bu->bins(2),
0063                      std::vector<std::vector<T>>(
0064                          bu->bins(1), std::vector<T>(bu->bins(0), nullptr))),
0065         m_arrayObjects(),
0066         m_binUtility(std::move(bu)) {
0067     /// reserve the right amount of data
0068     m_arrayObjects.reserve(tapvector.size());
0069     /// loop over the object & position for ordering
0070     for (auto& tap : tapvector) {
0071       /// check for inside
0072       if (m_binUtility->inside(tap.second)) {
0073         // butil to the array store - if the bingen
0074         // dimension is smaller 1,2 it will provide 0
0075         auto bins = m_binUtility->binTriple(tap.second);
0076         /// fill the data
0077         m_objectGrid[bins[2]][bins[1]][bins[0]] = tap.first;
0078         /// fill the unique m_arrayObjects
0079         if (!rangeContainsValue(m_arrayObjects, tap.first)) {
0080           m_arrayObjects.push_back(tap.first);
0081         }
0082       }
0083     }
0084   }
0085 
0086   /// Constructor with a grid and a BinUtility
0087   ///
0088   /// @param grid is the prepared object grid
0089   /// @param bu is the unique bin utility for this binned array
0090   BinnedArrayXD(const std::vector<std::vector<std::vector<T>>>& grid,
0091                 std::unique_ptr<const BinUtility> bu)
0092       : BinnedArray<T>(),
0093         m_objectGrid(grid),
0094         m_arrayObjects(),
0095         m_binUtility(std::move(bu)) {
0096     // get the total dimension
0097     std::size_t objects =
0098         m_binUtility->bins(0) * m_binUtility->bins(1) * m_binUtility->bins(2);
0099     /// reserve the right amount of data
0100     m_arrayObjects.reserve(objects);
0101     /// loop over the object & position for ordering
0102     for (auto& o2 : m_objectGrid) {
0103       for (auto& o1 : o2) {
0104         for (auto& o0 : o1) {
0105           /// fill the unique m_arrayObjects
0106           if (o0 && !rangeContainsValue(m_arrayObjects, o0)) {
0107             m_arrayObjects.push_back(o0);
0108           }
0109         }
0110       }
0111     }
0112   }
0113 
0114   /// Copy constructor
0115   /// - not allowed, use the same array
0116   BinnedArrayXD(const BinnedArrayXD<T>& barr) = delete;
0117 
0118   /// Assignment operator
0119   /// - not allowed, use the same array
0120   BinnedArrayXD& operator=(const BinnedArrayXD<T>& barr) = delete;
0121 
0122   /// Destructor
0123   ~BinnedArrayXD() override = default;
0124   /// Returns the object in the array from a local position
0125   ///
0126   /// @todo check if we can change to triple return at once
0127   ///
0128   /// @param lposition is the local position for the bin search
0129   /// @param bins is the bin triple filled during this access
0130   ///
0131   /// @return is the object in that bin
0132   T object(const Vector2& lposition,
0133            std::array<std::size_t, 3>& bins) const final {
0134     if (m_binUtility) {
0135       std::size_t bdim = m_binUtility->dimensions();
0136       bins[2] = bdim > 2 ? m_binUtility->bin(lposition, 2) : 0;
0137       bins[1] = bdim > 1 ? m_binUtility->bin(lposition, 1) : 0;
0138       bins[0] = m_binUtility->bin(lposition, 0);
0139       return m_objectGrid[bins[2]][bins[1]][bins[0]];
0140     }
0141     return m_objectGrid[0][0][0];
0142   }
0143 
0144   // satisfy overload / override
0145   T object(const Vector2& lposition) const override {
0146     std::array<std::size_t, 3> bins{};
0147     return object(lposition, bins);
0148   }
0149 
0150   /// Returns the object in the array from a global position
0151   ///
0152   /// @param position is the global position for the bin search
0153   /// @param bins is the bins triple filled during access
0154   ///
0155   /// @return is the object in that bin
0156   T object(const Vector3& position,
0157            std::array<std::size_t, 3>& bins) const final {
0158     if (m_binUtility) {
0159       std::size_t bdim = m_binUtility->dimensions();
0160       bins[2] = bdim > 2 ? m_binUtility->bin(position, 2) : 0;
0161       bins[1] = bdim > 1 ? m_binUtility->bin(position, 1) : 0;
0162       bins[0] = m_binUtility->bin(position, 0);
0163       return m_objectGrid[bins[2]][bins[1]][bins[0]];
0164     }
0165     return m_objectGrid[0][0][0];
0166   }
0167 
0168   // satisfy overload / override
0169   T object(const Vector3& position) const override {
0170     std::array<std::size_t, 3> bins{};
0171     return object(position, bins);
0172   }
0173 
0174   /// Return all unique object
0175   /// @return vector of unique array objects
0176   const std::vector<T>& arrayObjects() const final { return m_arrayObjects; }
0177 
0178   /// Return the object grid
0179   /// multiple entries are allowed and wanted
0180   /// @return internal object grid
0181   const std::vector<std::vector<std::vector<T>>>& objectGrid() const final {
0182     return m_objectGrid;
0183   }
0184 
0185   /// Return the BinUtility
0186   /// @return plain pointer to the bin utility of this array
0187   const BinUtility* binUtility() const final { return (m_binUtility.get()); }
0188 
0189  private:
0190   /// the data store - a 3D array at default
0191   std::vector<std::vector<std::vector<T>>> m_objectGrid;
0192   /// Vector of unique Array objects
0193   std::vector<T> m_arrayObjects;
0194   /// binUtility for retrieving and filling the Array
0195   std::unique_ptr<const BinUtility> m_binUtility;
0196 };
0197 }  // namespace Acts