ATLAS Offline Software
Loading...
Searching...
No Matches
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
20class MsgStream;
21
22namespace Trk {
23
34
35template<class T>
36class BinnedArray2D final : public BinnedArray<T>
37{
38
39public:
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),
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 }
96
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
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
155private:
156 void createArrayCache() const
157 {
158 if (!m_arrayObjects) {
159 std::unique_ptr<std::vector<T*>> arrayObjects = std::make_unique<std::vector<T*>>();
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
A generic symmetric BinUtility, for fully symmetric binning in terms of binning grid and binning type...
Definition BinUtility.h:39
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.
BinnedArray2D(BinnedArray2D &&)=default
void createArrayCache() const
T * entryObject(const Amg::Vector3D &pos) const
Returns the pointer to the templated class object from the BinnedArray -entry point.
BinnedArray2D(const std::vector< std::pair< std::shared_ptr< T >, Amg::Vector3D > > &tclassvector, const BinUtility &bingen)
Constructors with arguments.
BinnedArray2D * clone() const
Implizit Constructor.
BinnedArray2D(const std::vector< std::pair< std::shared_ptr< T >, Amg::Vector3D > > &tclassvector, BinUtility &&bingen)
BinnedArray2D()=default
BinnedArray2D & operator=(const BinnedArray2D &barr)
Assignment operator.
void initialize(const std::vector< std::pair< std::shared_ptr< T >, Amg::Vector3D > > &tclassvector)
vector of pointers to the class T
BinUtility m_binUtility
CxxUtils::CachedUniquePtr< std::vector< T * > > m_arrayObjects
binUtility for retrieving and filling the Array
std::span< T const *const > arrayObjects() const
Return all objects of the Array const T.
const BinUtility * binUtility() const
Return the BinUtility.
std::vector< std::vector< std::shared_ptr< T > > > m_array
1D vector of cached not owning pointers to class T
BinnedArray2D & operator=(BinnedArray2D &&)=default
unsigned int arrayObjectsNumber() const
Number of Entries in the Array.
~BinnedArray2D()=default
T * object(const Amg::Vector2D &lp) const
Returns the pointer to the templated class object from the BinnedArray, it returns nullptr if not def...
BinnedArray2D(const BinnedArray2D &barr)
Copy Constructor !
std::span< T *const > arrayObjects()
Return all objects of the Array non-const T.
T * object(const Amg::Vector3D &gp) const
Returns the pointer to the templated class object from the BinnedArray it returns nullptr if not defi...
BinnedArray()=default
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:130
Eigen::Matrix< double, 2, 1 > Vector2D
Eigen::Matrix< double, 3, 1 > Vector3D
CachedUniquePtrT< const T > CachedUniquePtr
Ensure that the ATLAS eigen extensions are properly loaded.
STL namespace.
void initialize()