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
16 
17 #include <array>
18 #include <iostream>
19 #include <vector>
20 
21 class MsgStream;
22 
23 namespace Acts {
24 
33 template <class T>
34 class BinnedArrayXD : public BinnedArray<T> {
36  using TAP = std::pair<T, Vector3D>;
37 
38  public:
42  BinnedArrayXD(T object)
43  : BinnedArray<T>(),
45  1, std::vector<std::vector<T>>(1, std::vector<T>(1, nullptr))),
46  m_arrayObjects({object}),
47  m_binUtility(nullptr) {
49  m_objectGrid[0][0][0] = object;
50  }
51 
57  BinnedArrayXD(const std::vector<TAP>& tapvector,
58  std::unique_ptr<const BinUtility> bu)
59  : BinnedArray<T>(),
60  m_objectGrid(bu->bins(2),
61  std::vector<std::vector<T>>(
62  bu->bins(1), std::vector<T>(bu->bins(0), nullptr))),
64  m_binUtility(std::move(bu)) {
66  m_arrayObjects.reserve(tapvector.size());
68  for (auto& tap : tapvector) {
70  if (m_binUtility->inside(tap.second)) {
71  // butil to the array store - if the bingen
72  // dimension is smaller 1,2 it will provide 0
73  auto bins = m_binUtility->binTriple(tap.second);
75  m_objectGrid[bins[2]][bins[1]][bins[0]] = tap.first;
77  if (std::find(m_arrayObjects.begin(), m_arrayObjects.end(),
78  tap.first) == m_arrayObjects.end()) {
79  m_arrayObjects.push_back(tap.first);
80  }
81  }
82  }
83  }
84 
89  BinnedArrayXD(const std::vector<std::vector<std::vector<T>>>& grid,
90  std::unique_ptr<const BinUtility> bu)
91  : BinnedArray<T>(),
92  m_objectGrid(grid),
94  m_binUtility(std::move(bu)) {
95  // get the total dimension
96  size_t objects =
97  m_binUtility->bins(0) * m_binUtility->bins(1) * m_binUtility->bins(2);
99  m_arrayObjects.reserve(objects);
101  for (auto& o2 : m_objectGrid) {
102  for (auto& o1 : o2) {
103  for (auto& o0 : o1) {
104  if (o0) {
106  if (std::find(m_arrayObjects.begin(), m_arrayObjects.end(), o0) ==
107  m_arrayObjects.end()) {
108  m_arrayObjects.push_back(o0);
109  }
110  }
111  }
112  }
113  }
114  }
115 
118  BinnedArrayXD(const BinnedArrayXD<T>& barr) = delete;
119 
122  BinnedArrayXD& operator=(const BinnedArrayXD<T>& barr) = delete;
123 
125  ~BinnedArrayXD() override = default;
134  T object(const Vector2D& lposition, std::array<size_t, 3>& bins) const final {
135  if (m_binUtility) {
136  size_t bdim = m_binUtility->dimensions();
137  bins[2] = bdim > 2 ? m_binUtility->bin(lposition, 2) : 0;
138  bins[1] = bdim > 1 ? m_binUtility->bin(lposition, 1) : 0;
139  bins[0] = m_binUtility->bin(lposition, 0);
140  return m_objectGrid[bins[2]][bins[1]][bins[0]];
141  }
142  return m_objectGrid[0][0][0];
143  }
144 
145  // satisfy overload / override
146  T object(const Vector2D& lposition) const override {
147  std::array<size_t, 3> bins;
148  return object(lposition, bins);
149  }
150 
157  T object(const Vector3D& position, std::array<size_t, 3>& bins) const final {
158  if (m_binUtility) {
159  size_t bdim = m_binUtility->dimensions();
160  bins[2] = bdim > 2 ? m_binUtility->bin(position, 2) : 0;
161  bins[1] = bdim > 1 ? m_binUtility->bin(position, 1) : 0;
162  bins[0] = m_binUtility->bin(position, 0);
163  return m_objectGrid[bins[2]][bins[1]][bins[0]];
164  }
165  return m_objectGrid[0][0][0];
166  }
167 
168  // satisfy overload / override
169  T object(const Vector3D& position) const override {
170  std::array<size_t, 3> bins;
171  return object(position, bins);
172  }
173 
176  const std::vector<T>& arrayObjects() const final { return m_arrayObjects; }
177 
181  const std::vector<std::vector<std::vector<T>>>& objectGrid() const final {
182  return m_objectGrid;
183  }
184 
191  std::vector<T> objectCluster(
192  const std::array<size_t, 3>& binTriple) const override {
193  // prepare the return vector
194  std::vector<T> rvector;
195  // reference bin object to be excluded
196  T bObject = m_objectGrid[binTriple[2]][binTriple[1]][binTriple[0]];
197  // get the dimensions first
198  size_t bdim = m_binUtility->dimensions();
199  // avoiding code duplication
200  std::vector<size_t> zerorange = {0};
201  // 2D bin
202  std::vector<size_t> bin2values =
203  (bdim > 2) ? m_binUtility->binningData()[2].neighbourRange(binTriple[2])
204  : zerorange;
205  // 1D bin
206  std::vector<size_t> bin1values =
207  (bdim > 1) ? m_binUtility->binningData()[1].neighbourRange(binTriple[1])
208  : zerorange;
209  // 0D bin
210  std::vector<size_t> bin0values =
211  m_binUtility->binningData()[0].neighbourRange(binTriple[0]);
212 
213  // do the loop
214  for (auto b2 : bin2values) {
215  for (auto b1 : bin1values) {
216  for (auto b0 : bin0values) {
217  // get the object
218  T object = m_objectGrid[b2][b1][b0];
219  if (object && object != bObject &&
220  std::find(rvector.begin(), rvector.end(), object) ==
221  rvector.end()) {
222  rvector.push_back(object);
223  }
224  }
225  }
226  }
227  // return the ones you found
228  return rvector;
229  }
230 
233  const BinUtility* binUtility() const final { return (m_binUtility.get()); }
234 
235  private:
237  std::vector<std::vector<std::vector<T>>> m_objectGrid;
239  std::vector<T> m_arrayObjects;
241  std::unique_ptr<const BinUtility> m_binUtility;
242 };
243 } // end of namespace Acts