Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
BinnedArray2D.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // BinnedArray2D.h, (c) ATLAS Detector software
8 
9 #ifndef TRKDETDESCRUTILS_BINNEDARRAY2D_H
10 #define TRKDETDESCRUTILS_BINNEDARRAY2D_H
11 
12 #include "GaudiKernel/GaudiException.h"
15 
16 // STL
17 #include <vector>
18 #include <memory>
19 
20 class MsgStream;
21 
22 namespace Trk {
23 
35 template<class T>
36 class BinnedArray2D final : public BinnedArray<T>
37 {
38 
39 public:
40  // defaults, copy assignment can not be defaulted (CachedPtr)
41  BinnedArray2D() = default;
44  ~BinnedArray2D() = default;
45 
48  const std::vector<std::pair<std::shared_ptr<T>, Amg::Vector3D>>& tclassvector,
49  const BinUtility& bingen)
50  : BinnedArray<T>(),
51  m_array{},
52  m_arrayObjects(nullptr),
53  m_binUtility(bingen) {
54  initialize(tclassvector);
55  }
57  const std::vector<std::pair<std::shared_ptr<T>, Amg::Vector3D>>& tclassvector,
58  BinUtility&& bingen)
59  : BinnedArray<T>(),
60  m_array{},
61  m_arrayObjects(nullptr),
62  m_binUtility(std::move(bingen)) {
63  initialize(tclassvector);
64  }
65 
68  : BinnedArray<T>(),
69  m_array{barr.m_array},
70  m_arrayObjects(nullptr),
71  m_binUtility(barr.m_binUtility) {}
72 
75  if (this != &barr) {
76  m_arrayObjects.release();
78  m_array = barr.m_array;
79  }
80  return *this;
81  }
82 
84  BinnedArray2D* clone() const { return new BinnedArray2D(*this); }
85 
89  T* object(const Amg::Vector2D& lp) const
90  {
91  if (m_binUtility.inside(lp)) {
92  return (m_array[m_binUtility.bin(lp, 1)][m_binUtility.bin(lp, 0)]).get();
93  }
94  return nullptr;
95  }
99  T* object(const Amg::Vector3D& gp) const
100  {
101  if (m_binUtility.inside(gp)) {
102  return (m_array[m_binUtility.bin(gp, 1)][m_binUtility.bin(gp, 0)]).get();
103  }
104  return nullptr;
105  }
106 
109  T* entryObject(const Amg::Vector3D& pos) const
110  {
111  return (m_array[m_binUtility.entry(pos, 1)][m_binUtility.entry(pos, 0)]).get();
112  }
113 
116  T* nextObject(const Amg::Vector3D& gp,
117  const Amg::Vector3D& mom,
118  bool associatedResult = true) const
119  {
120  // direct access to associated one
121  if (associatedResult){
122  return object(gp);
123  }
124  size_t nextFirst = m_binUtility.next(gp, mom, 0);
125  size_t nextSecond = m_binUtility.next(gp, mom, 1);
126 
127  return (nextFirst > 0 && nextSecond > 0)
128  ? (m_array[nextSecond][nextFirst]).get()
129  : nullptr;
130  }
131 
133  std::span<T* const> arrayObjects()
134  {
136  return std::span<T* const>(m_arrayObjects->begin(), m_arrayObjects->end());
137  }
138 
140  std::span<T const * const> arrayObjects() const
141  {
143  return std::span<T const * const>(m_arrayObjects->begin(), m_arrayObjects->end());
144  }
145 
147  unsigned int arrayObjectsNumber() const
148  {
149  return (m_array.size() * (m_array[0]).size());
150  }
151 
153  const BinUtility* binUtility() const { return &m_binUtility; }
154 
155 private:
156  void createArrayCache() const
157  {
158  if (!m_arrayObjects) {
159  std::unique_ptr<std::vector<T*>> arrayObjects = std::make_unique<std::vector<T*>>();
160  arrayObjects->reserve(arrayObjectsNumber());
161  for (size_t ihl = 0; ihl < (m_binUtility.bins(1)); ++ihl) {
162  for (size_t ill = 0; ill < (m_binUtility.bins(0)); ++ill) {
163  arrayObjects->push_back((m_array[ihl][ill]).get());
164  }
165  }
166  m_arrayObjects.set(std::move(arrayObjects));
167  }
168  }
169 
170  void initialize(const std::vector<std::pair<std::shared_ptr<T>, Amg::Vector3D>>& tclassvector){
171  m_array = std::vector<std::vector<std::shared_ptr<T>>>(m_binUtility.bins(1));
172  for (size_t i = 0; i < m_binUtility.bins(1); ++i) {
173  m_array[i] = std::vector<std::shared_ptr<T>>(m_binUtility.bins(0));
174  }
175  // fill the Volume vector into the array
176  size_t vecsize = tclassvector.size();
177  for (size_t ivec = 0; ivec < vecsize; ++ivec) {
178  const Amg::Vector3D currentGlobal(((tclassvector[ivec]).second));
179  if (m_binUtility.inside(currentGlobal)) {
180  std::vector<std::shared_ptr<T>>& curVec = m_array[m_binUtility.bin(currentGlobal, 1)];
181  curVec[m_binUtility.bin(currentGlobal, 0)] = ((tclassvector)[ivec]).first;
182  } else {
183  throw GaudiException("BinnedArray2D", "Object outside bounds",
184  StatusCode::FAILURE);
185  }
186  }
187  }
189  std::vector<std::vector<std::shared_ptr<T>>> m_array{};
194 };
195 
196 } // end of namespace Trk
197 
198 #endif // TRKSURFACES_BINNEDARRAY2D_H
Trk::BinUtility::entry
size_t entry(const Amg::Vector3D &position, size_t ba=0) const
Bin from a 3D vector (already in binning frame)
Definition: BinUtility.h:136
Trk::BinnedArray2D::operator=
BinnedArray2D & operator=(const BinnedArray2D &barr)
Assignment operator.
Definition: BinnedArray2D.h:74
Trk::BinnedArray2D::arrayObjectsNumber
unsigned int arrayObjectsNumber() const
Number of Entries in the Array.
Definition: BinnedArray2D.h:147
Trk::BinnedArray2D::arrayObjects
std::span< T const *const > arrayObjects() const
Return all objects of the Array const T.
Definition: BinnedArray2D.h:140
Trk::BinnedArray2D::object
T * object(const Amg::Vector2D &lp) const
Returns the pointer to the templated class object from the BinnedArray, it returns nullptr if not def...
Definition: BinnedArray2D.h:89
Trk::BinnedArray2D::initialize
void initialize(const std::vector< std::pair< std::shared_ptr< T >, Amg::Vector3D >> &tclassvector)
vector of pointers to the class T
Definition: BinnedArray2D.h:170
Trk::BinnedArray2D::operator=
BinnedArray2D & operator=(BinnedArray2D &&)=default
Trk::BinnedArray2D::~BinnedArray2D
~BinnedArray2D()=default
Amg::Vector2D
Eigen::Matrix< double, 2, 1 > Vector2D
Definition: GeoPrimitives.h:48
Trk::BinnedArray2D::binUtility
const BinUtility * binUtility() const
Return the BinUtility.
Definition: BinnedArray2D.h:153
Trk::BinnedArray2D::arrayObjects
std::span< T *const > arrayObjects()
Return all objects of the Array non-const T.
Definition: BinnedArray2D.h:133
BinnedArray.h
Trk::BinnedArray2D::entryObject
T * entryObject(const Amg::Vector3D &pos) const
Returns the pointer to the templated class object from the BinnedArray -entry point.
Definition: BinnedArray2D.h:109
CxxUtils::CachedUniquePtrT
Cached pointer with atomic update.
Definition: CachedUniquePtr.h:54
Trk::BinnedArray2D::nextObject
T * nextObject(const Amg::Vector3D &gp, const Amg::Vector3D &mom, bool associatedResult=true) const
Returns the pointer to the templated class object from the BinnedArray.
Definition: BinnedArray2D.h:116
Trk::BinnedArray2D::BinnedArray2D
BinnedArray2D(const BinnedArray2D &barr)
Copy Constructor !
Definition: BinnedArray2D.h:67
Trk::BinnedArray2D::BinnedArray2D
BinnedArray2D(const std::vector< std::pair< std::shared_ptr< T >, Amg::Vector3D >> &tclassvector, BinUtility &&bingen)
Definition: BinnedArray2D.h:56
BinUtility.h
python.SystemOfUnits.second
float second
Definition: SystemOfUnits.py:134
Trk::BinUtility::bins
size_t bins(size_t ba=0) const
Number of bins.
Definition: BinUtility.h:221
ParticleGun_EoverP_Config.mom
mom
Definition: ParticleGun_EoverP_Config.py:63
lumiFormat.i
int i
Definition: lumiFormat.py:85
Trk::BinnedArray2D::BinnedArray2D
BinnedArray2D()=default
Trk::BinnedArray2D::m_arrayObjects
CxxUtils::CachedUniquePtr< std::vector< T * > > m_arrayObjects
binUtility for retrieving and filling the Array
Definition: BinnedArray2D.h:191
columnar::final
CM final
Definition: ColumnAccessor.h:106
Trk::BinnedArray2D::m_binUtility
BinUtility m_binUtility
Definition: BinnedArray2D.h:193
Trk::BinnedArray2D
Definition: BinnedArray2D.h:37
Trk::BinnedArray2D::m_array
std::vector< std::vector< std::shared_ptr< T > > > m_array
1D vector of cached not owning pointers to class T
Definition: BinnedArray2D.h:189
Trk::BinUtility
Definition: BinUtility.h:39
Trk
Ensure that the ATLAS eigen extensions are properly loaded.
Definition: FakeTrackBuilder.h:9
Trk::BinUtility::inside
bool inside(const Amg::Vector3D &position) const
Check if bin is inside from Vector3D.
Definition: BinUtility.h:186
Trk::BinnedArray2D::createArrayCache
void createArrayCache() const
Definition: BinnedArray2D.h:156
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
Trk::BinnedArray2D::BinnedArray2D
BinnedArray2D(BinnedArray2D &&)=default
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
Trk::BinnedArray2D::object
T * object(const Amg::Vector3D &gp) const
Returns the pointer to the templated class object from the BinnedArray it returns nullptr if not defi...
Definition: BinnedArray2D.h:99
Trk::BinnedArray2D::clone
BinnedArray2D * clone() const
Implizit Constructor.
Definition: BinnedArray2D.h:84
Trk::BinUtility::bin
size_t bin(const Amg::Vector3D &position, size_t ba=0) const
Bin from a 3D vector (already in binning frame)
Definition: BinUtility.h:126
Trk::BinnedArray
Definition: BinnedArray.h:36
CxxUtils::ivec
vec_fb< typename boost::int_t< sizeof(T) *8 >::exact, N > ivec
Definition: vec_fb.h:53
Trk::BinnedArray2D::BinnedArray2D
BinnedArray2D(const std::vector< std::pair< std::shared_ptr< T >, Amg::Vector3D >> &tclassvector, const BinUtility &bingen)
Constructors with arguments.
Definition: BinnedArray2D.h:47
Trk::BinUtility::next
size_t next(const Amg::Vector3D &position, const Amg::Vector3D &direction, size_t ba=0) const
Bin from a 3D vector (already in binning frame)
Definition: BinUtility.h:145