Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:01:53

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2016-2018 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 http://mozilla.org/MPL/2.0/.
0008 
0009 ///////////////////////////////////////////////////////////////////
0010 // BinnedArrayXD.h, Acts project
0011 ///////////////////////////////////////////////////////////////////
0012 
0013 #pragma once
0014 #include "Acts/Utilities/BinUtility.hpp"
0015 #include "Acts/Utilities/BinnedArray.hpp"
0016 
0017 #include <array>
0018 #include <iostream>
0019 #include <vector>
0020 
0021 class MsgStream;
0022 
0023 namespace Acts {
0024 
0025 /// @class BinnedArrayXD
0026 ///
0027 /// Avoiding a map search, the templated BinnedArray class can help
0028 /// ordereing geometrical objects by providing a dedicated BinUtility.
0029 ///
0030 /// This can be 0D, 1D, 2D, and 3D in regular binning
0031 ///
0032 /// the type of Binning is given defined through the BinUtility
0033 template <class T>
0034 class BinnedArrayXD : public BinnedArray<T> {
0035   /// typedef the object and position for readability
0036   using TAP = std::pair<T, Vector3>;
0037 
0038  public:
0039   /// Constructor for single object
0040   ///
0041   /// @tparam object is the single object
0042   BinnedArrayXD(T object)
0043       : BinnedArray<T>(),
0044         m_objectGrid(
0045             1, std::vector<std::vector<T>>(1, std::vector<T>(1, nullptr))),
0046         m_arrayObjects({object}),
0047         m_binUtility(nullptr) {
0048     /// fill the single object into the object grid
0049     m_objectGrid[0][0][0] = object;
0050   }
0051 
0052   /// Constructor with std::vector and a BinUtility
0053   /// - fills the internal data structure
0054   ///
0055   /// @param tapvector is a vector of object and binning position
0056   /// @param bu is the unique bin utility for this binned array
0057   BinnedArrayXD(const std::vector<TAP>& tapvector,
0058                 std::unique_ptr<const BinUtility> bu)
0059       : BinnedArray<T>(),
0060         m_objectGrid(bu->bins(2),
0061                      std::vector<std::vector<T>>(
0062                          bu->bins(1), std::vector<T>(bu->bins(0), nullptr))),
0063         m_arrayObjects(),
0064         m_binUtility(std::move(bu)) {
0065     /// reserve the right amount of data
0066     m_arrayObjects.reserve(tapvector.size());
0067     /// loop over the object & position for ordering
0068     for (auto& tap : tapvector) {
0069       /// check for inside
0070       if (m_binUtility->inside(tap.second)) {
0071         // butil to the array store - if the bingen
0072         // dimension is smaller 1,2 it will provide 0
0073         auto bins = m_binUtility->binTriple(tap.second);
0074         /// fill the data
0075         m_objectGrid[bins[2]][bins[1]][bins[0]] = tap.first;
0076         /// fill the unique m_arrayObjects
0077         if (std::find(m_arrayObjects.begin(), m_arrayObjects.end(),
0078                       tap.first) == m_arrayObjects.end()) {
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           if (o0) {
0105             /// fill the unique m_arrayObjects
0106             if (std::find(m_arrayObjects.begin(), m_arrayObjects.end(), o0) ==
0107                 m_arrayObjects.end()) {
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 }  // end of namespace Acts