ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BinnedArrayXD.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file BinnedArrayXD.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2016-2018 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 // BinnedArrayXD.h, Acts project
12 
13 #pragma once
14 #include <array>
15 #include <iostream>
16 #include <vector>
19 
20 class MsgStream;
21 
22 namespace Acts {
23 
32 template <class T>
33 class BinnedArrayXD : public BinnedArray<T> {
35  using TAP = std::pair<T, Vector3D>;
36 
37  public:
41  BinnedArrayXD(T object)
42  : BinnedArray<T>(),
44  1, std::vector<std::vector<T>>(1, std::vector<T>(1, nullptr))),
45  m_arrayObjects({object}),
46  m_binUtility(nullptr) {
48  m_objectGrid[0][0][0] = object;
49  }
50 
56  BinnedArrayXD(const std::vector<TAP>& tapvector,
57  std::unique_ptr<const BinUtility> bu)
58  : BinnedArray<T>(),
59  m_objectGrid(bu->bins(2),
60  std::vector<std::vector<T>>(
61  bu->bins(1), std::vector<T>(bu->bins(0), nullptr))),
63  m_binUtility(std::move(bu)) {
65  m_arrayObjects.reserve(tapvector.size());
67  for (auto& tap : tapvector) {
69  if (m_binUtility->inside(tap.second)) {
70  // butil to the array store - if the bingen
71  // dimension is smaller 1,2 it will provide 0
72  auto bins = m_binUtility->binTriple(tap.second);
74  m_objectGrid[bins[2]][bins[1]][bins[0]] = tap.first;
76  if (std::find(m_arrayObjects.begin(), m_arrayObjects.end(),
77  tap.first) == m_arrayObjects.end()) {
78  m_arrayObjects.push_back(tap.first);
79  }
80  }
81  }
82  }
83 
88  BinnedArrayXD(const std::vector<std::vector<std::vector<T>>>& grid,
89  std::unique_ptr<const BinUtility> bu)
90  : BinnedArray<T>(),
91  m_objectGrid(grid),
93  m_binUtility(std::move(bu)) {
94  // get the total dimension
95  size_t objects =
96  m_binUtility->bins(0) * m_binUtility->bins(1) * m_binUtility->bins(2);
98  m_arrayObjects.reserve(objects);
100  for (auto& o2 : m_objectGrid) {
101  for (auto& o1 : o2) {
102  for (auto& o0 : o1) {
103  if (o0) {
105  if (std::find(m_arrayObjects.begin(), m_arrayObjects.end(), o0) ==
106  m_arrayObjects.end()) {
107  m_arrayObjects.push_back(o0);
108  }
109  }
110  }
111  }
112  }
113  }
114 
117  BinnedArrayXD(const BinnedArrayXD<T>& barr) = delete;
118 
121  BinnedArrayXD& operator=(const BinnedArrayXD<T>& barr) = delete;
122 
124  ~BinnedArrayXD() override = default;
133  T object(const Vector2D& lposition, std::array<size_t, 3>& bins) const final {
134  if (m_binUtility) {
135  size_t bdim = m_binUtility->dimensions();
136  bins[2] = bdim > 2 ? m_binUtility->bin(lposition, 2) : 0;
137  bins[1] = bdim > 1 ? m_binUtility->bin(lposition, 1) : 0;
138  bins[0] = m_binUtility->bin(lposition, 0);
139  return m_objectGrid[bins[2]][bins[1]][bins[0]];
140  }
141  return m_objectGrid[0][0][0];
142  }
143 
144  // satisfy overload / override
145  T object(const Vector2D& lposition) const override {
146  std::array<size_t, 3> bins;
147  return object(lposition, bins);
148  }
149 
156  T object(const Vector3D& position, std::array<size_t, 3>& bins) const final {
157  if (m_binUtility) {
158  size_t bdim = m_binUtility->dimensions();
159  bins[2] = bdim > 2 ? m_binUtility->bin(position, 2) : 0;
160  bins[1] = bdim > 1 ? m_binUtility->bin(position, 1) : 0;
161  bins[0] = m_binUtility->bin(position, 0);
162  return m_objectGrid[bins[2]][bins[1]][bins[0]];
163  }
164  return m_objectGrid[0][0][0];
165  }
166 
167  // satisfy overload / override
168  T object(const Vector3D& position) const override {
169  std::array<size_t, 3> bins;
170  return object(position, bins);
171  }
172 
175  const std::vector<T>& arrayObjects() const final { return m_arrayObjects; }
176 
180  const std::vector<std::vector<std::vector<T>>>& objectGrid() const final {
181  return m_objectGrid;
182  }
183 
190  std::vector<T> objectCluster(
191  const std::array<size_t, 3>& binTriple) const override {
192  // prepare the return vector
193  std::vector<T> rvector;
194  // reference bin object to be excluded
195  T bObject = m_objectGrid[binTriple[2]][binTriple[1]][binTriple[0]];
196  // get the dimensions first
197  size_t bdim = m_binUtility->dimensions();
198  // avoiding code duplication
199  std::vector<size_t> zerorange = {0};
200  // 2D bin
201  std::vector<size_t> bin2values =
202  (bdim > 2) ? m_binUtility->binningData()[2].neighbourRange(binTriple[2])
203  : zerorange;
204  // 1D bin
205  std::vector<size_t> bin1values =
206  (bdim > 1) ? m_binUtility->binningData()[1].neighbourRange(binTriple[1])
207  : zerorange;
208  // 0D bin
209  std::vector<size_t> bin0values =
210  m_binUtility->binningData()[0].neighbourRange(binTriple[0]);
211 
212  // do the loop
213  for (auto b2 : bin2values) {
214  for (auto b1 : bin1values) {
215  for (auto b0 : bin0values) {
216  // get the object
217  T object = m_objectGrid[b2][b1][b0];
218  if (object && object != bObject &&
219  std::find(rvector.begin(), rvector.end(), object) ==
220  rvector.end()) {
221  rvector.push_back(object);
222  }
223  }
224  }
225  }
226  // return the ones you found
227  return rvector;
228  }
229 
232  const BinUtility* binUtility() const final { return (m_binUtility.get()); }
233 
234  private:
236  std::vector<std::vector<std::vector<T>>> m_objectGrid;
238  std::vector<T> m_arrayObjects;
240  std::unique_ptr<const BinUtility> m_binUtility;
241 };
242 } // end of namespace Acts