Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-17 07:58:49

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