ATLAS Offline Software
BinnedArray2D.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 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"
16 
17 // STL
18 #include <vector>
19 
20 class MsgStream;
21 
22 namespace Trk {
23 
35 template<class T>
36 class BinnedArray2D final : public BinnedArray<T>
37 {
38 
39 public:
42  : BinnedArray<T>()
43  , m_array{}
44  , m_arrayObjects(nullptr)
45  , m_binUtility(nullptr)
46  {}
47 
50  const std::vector<std::pair<SharedObject<T>, Amg::Vector3D>>& tclassvector,
51  BinUtility* bingen)
52  : BinnedArray<T>()
53  , m_array{}
54  , m_arrayObjects(nullptr)
55  , m_binUtility(bingen)
56  {
57 
58  if (bingen) {
59  m_array = std::vector<std::vector<SharedObject<T>>>(bingen->bins(1));
60  for (size_t i = 0; i < bingen->bins(1); ++i) {
61  m_array[i] = std::vector<SharedObject<T>>(bingen->bins(0));
62  }
63  // fill the Volume vector into the array
64  size_t vecsize = tclassvector.size();
65  for (size_t ivec = 0; ivec < vecsize; ++ivec) {
66  const Amg::Vector3D currentGlobal(((tclassvector[ivec]).second));
67  if (bingen->inside(currentGlobal)) {
68  std::vector<SharedObject<T>>& curVec =
69  m_array[bingen->bin(currentGlobal, 1)];
70 
71  curVec[bingen->bin(currentGlobal, 0)] = ((tclassvector)[ivec]).first;
72  } else {
73  throw GaudiException(
74  "BinnedArray2D", "Object outside bounds", StatusCode::FAILURE);
75  }
76  }
77  }
78  }
79 
82  : BinnedArray<T>()
83  , m_array{}
84  , m_arrayObjects(nullptr)
85  , m_binUtility(nullptr)
86  {
87  m_binUtility = (barr.m_binUtility) ? barr.m_binUtility->clone() : nullptr;
88  // copy over
89  m_array = std::vector<std::vector<SharedObject<T>>>(barr.m_array.size());
90  for (size_t ihl = 0; ihl < barr.m_array.size(); ++ihl) {
91  m_array[ihl] = std::vector<SharedObject<T>>((barr.m_array[0]).size());
92  for (size_t ill = 0; ill < (barr.m_array[0]).size(); ++ill) {
93  m_array[ihl][ill] = (barr.m_array)[ihl][ill];
94  }
95  }
96  }
99  {
100  if (this != &barr) {
101  m_arrayObjects.release();
102  delete m_binUtility;
103  // now refill
104  m_binUtility = (barr.m_binUtility) ? barr.m_binUtility->clone() : nullptr;
105  // assign over
106  m_array = std::vector<std::vector<SharedObject<T>>>(barr.m_array.size());
107  for (size_t ihl = 0; ihl < barr.m_array.size(); ++ihl) {
108  m_array[ihl] = std::vector<SharedObject<T>>((barr.m_array[0]).size());
109  for (size_t ill = 0; ill < ((barr.m_array)[0]).size(); ++ill) {
110  m_array[ihl][ill] = (barr.m_array)[ihl][ill];
111  }
112  }
113  }
114  return *this;
115  }
117  BinnedArray2D* clone() const { return new BinnedArray2D(*this); }
118 
121 
125  T* object(const Amg::Vector2D& lp) const
126  {
127  if (m_binUtility->inside(lp)) {
128  return (m_array[m_binUtility->bin(lp, 1)][m_binUtility->bin(lp, 0)])
129  .get();
130  }
131  return nullptr;
132  }
133 
137  T* object(const Amg::Vector3D& gp) const
138  {
139  if (m_binUtility->inside(gp)) {
140  return (m_array[m_binUtility->bin(gp, 1)][m_binUtility->bin(gp, 0)])
141  .get();
142  }
143  return nullptr;
144  }
145 
148  T* entryObject(const Amg::Vector3D& pos) const
149  {
150  return (m_array[m_binUtility->entry(pos, 1)][m_binUtility->entry(pos, 0)])
151  .get();
152  }
153 
156  T* nextObject(const Amg::Vector3D& gp,
157  const Amg::Vector3D& mom,
158  bool associatedResult = true) const
159  {
160  // direct access to associated one
161  if (associatedResult)
162  return object(gp);
163  size_t nextFirst = m_binUtility->next(gp, mom, 0);
164  size_t nextSecond = m_binUtility->next(gp, mom, 1);
165 
166  return (nextFirst > 0 && nextSecond > 0)
167  ? (m_array[nextSecond][nextFirst]).get()
168  : nullptr;
169  }
170 
173  {
175  return BinnedArraySpan<T* const>(&*(m_arrayObjects->begin()), &*(m_arrayObjects->end()));
176  }
177 
180  {
182  return BinnedArraySpan<T const * const>(&*(m_arrayObjects->begin()), &*(m_arrayObjects->end()));
183  }
184 
186  unsigned int arrayObjectsNumber() const
187  {
188  return (m_array.size() * (m_array[0]).size());
189  }
190 
192  const BinUtility* binUtility() const { return (m_binUtility); }
193 
194 private:
195  void createArrayCache() const
196  {
197  if (!m_arrayObjects) {
198 
199  std::unique_ptr<std::vector<T*>> arrayObjects =
200  std::make_unique<std::vector<T*>>();
201 
202  arrayObjects->reserve(arrayObjectsNumber());
203 
204  for (size_t ihl = 0; ihl < (m_binUtility->bins(1)); ++ihl) {
205  for (size_t ill = 0; ill < (m_binUtility->bins(0)); ++ill) {
206  arrayObjects->push_back((m_array[ihl][ill]).get());
207  }
208  }
209  m_arrayObjects.set(std::move(arrayObjects));
210  }
211  }
212 
214  std::vector<std::vector<SharedObject<T>>> m_array;
219 };
220 
221 } // end of namespace Trk
222 
223 #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:145
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
Trk::BinnedArray2D::operator=
BinnedArray2D & operator=(const BinnedArray2D &barr)
Assignment operator.
Definition: BinnedArray2D.h:98
Trk::BinnedArray2D::arrayObjectsNumber
unsigned int arrayObjectsNumber() const
Number of Entries in the Array.
Definition: BinnedArray2D.h:186
Trk::BinnedArray2D::object
T * object(const Amg::Vector2D &lp) const
Returns the pointer to the templated class object from the BinnedArray, it returns 0 if not defined;.
Definition: BinnedArray2D.h:125
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:192
Trk::BinnedArray2D::arrayObjects
BinnedArraySpan< T const *const > arrayObjects() const
Return all objects of the Array const T.
Definition: BinnedArray2D.h:179
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:148
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:156
Trk::BinnedArray2D::BinnedArray2D
BinnedArray2D(const BinnedArray2D &barr)
Copy Constructor - copies only pointers !
Definition: BinnedArray2D.h:81
Trk::BinnedArray2D::arrayObjects
BinnedArraySpan< T *const > arrayObjects()
Return all objects of the Array non-const T.
Definition: BinnedArray2D.h:172
BinUtility.h
Trk::BinnedArray2D::BinnedArray2D
BinnedArray2D(const std::vector< std::pair< SharedObject< T >, Amg::Vector3D >> &tclassvector, BinUtility *bingen)
Constructor with std::vector and a BinUtility.
Definition: BinnedArray2D.h:49
Trk::BinUtility::bins
size_t bins(size_t ba=0) const
Number of bins.
Definition: BinUtility.h:223
Trk::BinnedArray2D::BinnedArray2D
BinnedArray2D()
Default Constructor - needed for inherited classes.
Definition: BinnedArray2D.h:41
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
ParticleGun_EoverP_Config.mom
mom
Definition: ParticleGun_EoverP_Config.py:63
lumiFormat.i
int i
Definition: lumiFormat.py:85
Trk::BinnedArray2D::m_arrayObjects
CxxUtils::CachedUniquePtr< std::vector< T * > > m_arrayObjects
binUtility for retrieving and filling the Array
Definition: BinnedArray2D.h:216
SharedObject.h
Trk::BinnedArray2D::m_binUtility
BinUtility * m_binUtility
Definition: BinnedArray2D.h:218
Trk::BinnedArray2D
Definition: BinnedArray2D.h:37
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:190
Trk::BinnedArray2D::createArrayCache
void createArrayCache() const
vector of pointers to the class T
Definition: BinnedArray2D.h:195
Trk::BinnedArray2D::~BinnedArray2D
~BinnedArray2D()
Virtual Destructor.
Definition: BinnedArray2D.h:120
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
Trk::SharedObject
std::shared_ptr< T > SharedObject
Definition: SharedObject.h:24
Trk::BinUtility::clone
BinUtility * clone() const
Implizit Constructor.
Definition: BinUtility.h:130
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 0 if not defined;.
Definition: BinnedArray2D.h:137
Trk::BinnedArray2D::clone
BinnedArray2D * clone() const
Implizit Constructor.
Definition: BinnedArray2D.h:117
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:136
Trk::BinnedArray
Definition: BinnedArray.h:38
CxxUtils::ivec
vec_fb< typename boost::int_t< sizeof(T) *8 >::exact, N > ivec
Definition: vec_fb.h:53
Trk::BinnedArraySpan
std::span< T > BinnedArraySpan
Definition: BinnedArray.h:34
Trk::BinnedArray2D::m_array
std::vector< std::vector< SharedObject< T > > > m_array
forced 1D vector of pointers to class T
Definition: BinnedArray2D.h:214
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:153