Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:09

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   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           if (o0) {
0106             /// fill the unique m_arrayObjects
0107             if (!rangeContainsValue(m_arrayObjects, o0)) {
0108               m_arrayObjects.push_back(o0);
0109             }
0110           }
0111         }
0112       }
0113     }
0114   }
0115 
0116   /// Copy constructor
0117   /// - not allowed, use the same array
0118   BinnedArrayXD(const BinnedArrayXD<T>& barr) = delete;
0119 
0120   /// Assignment operator
0121   /// - not allowed, use the same array
0122   BinnedArrayXD& operator=(const BinnedArrayXD<T>& barr) = delete;
0123 
0124   /// Destructor
0125   ~BinnedArrayXD() override = default;
0126   /// Returns the object in the array from a local position
0127   ///
0128   /// @todo check if we can change to triple return at once
0129   ///
0130   /// @param lposition is the local position for the bin search
0131   /// @param bins is the bin triple filled during this access
0132   ///
0133   /// @return is the object in that bin
0134   T object(const Vector2& lposition,
0135            std::array<std::size_t, 3>& bins) const final {
0136     if (m_binUtility) {
0137       std::size_t bdim = m_binUtility->dimensions();
0138       bins[2] = bdim > 2 ? m_binUtility->bin(lposition, 2) : 0;
0139       bins[1] = bdim > 1 ? m_binUtility->bin(lposition, 1) : 0;
0140       bins[0] = m_binUtility->bin(lposition, 0);
0141       return m_objectGrid[bins[2]][bins[1]][bins[0]];
0142     }
0143     return m_objectGrid[0][0][0];
0144   }
0145 
0146   // satisfy overload / override
0147   T object(const Vector2& lposition) const override {
0148     std::array<std::size_t, 3> bins{};
0149     return object(lposition, bins);
0150   }
0151 
0152   /// Returns the object in the array from a global position
0153   ///
0154   /// @param position is the global position for the bin search
0155   /// @param bins is the bins triple filled during access
0156   ///
0157   /// @return is the object in that bin
0158   T object(const Vector3& position,
0159            std::array<std::size_t, 3>& bins) const final {
0160     if (m_binUtility) {
0161       std::size_t bdim = m_binUtility->dimensions();
0162       bins[2] = bdim > 2 ? m_binUtility->bin(position, 2) : 0;
0163       bins[1] = bdim > 1 ? m_binUtility->bin(position, 1) : 0;
0164       bins[0] = m_binUtility->bin(position, 0);
0165       return m_objectGrid[bins[2]][bins[1]][bins[0]];
0166     }
0167     return m_objectGrid[0][0][0];
0168   }
0169 
0170   // satisfy overload / override
0171   T object(const Vector3& position) const override {
0172     std::array<std::size_t, 3> bins{};
0173     return object(position, bins);
0174   }
0175 
0176   /// Return all unique object
0177   /// @return vector of unique array objects
0178   const std::vector<T>& arrayObjects() const final { return m_arrayObjects; }
0179 
0180   /// Return the object grid
0181   /// multiple entries are allowed and wanted
0182   /// @return internal object grid
0183   const std::vector<std::vector<std::vector<T>>>& objectGrid() const final {
0184     return m_objectGrid;
0185   }
0186 
0187   /// Return the BinUtility
0188   /// @return plain pointer to the bin utility of this array
0189   const BinUtility* binUtility() const final { return (m_binUtility.get()); }
0190 
0191  private:
0192   /// the data store - a 3D array at default
0193   std::vector<std::vector<std::vector<T>>> m_objectGrid;
0194   /// Vector of unique Array objects
0195   std::vector<T> m_arrayObjects;
0196   /// binUtility for retrieving and filling the Array
0197   std::unique_ptr<const BinUtility> m_binUtility;
0198 };
0199 }  // namespace Acts