ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BinUtility.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file BinUtility.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 // BinUtility.h, Acts project
12 
13 #pragma once
14 #include <array>
15 #include <iostream>
16 #include <memory>
17 #include <vector>
21 
22 namespace Acts {
23 
34 class BinUtility {
35  public:
37  BinUtility() : m_binningData(), m_transform(nullptr), m_itransform(nullptr) {
38  m_binningData.reserve(3);
39  }
40 
45  BinUtility(const BinningData& bData,
46  const std::shared_ptr<const Transform3D>& tForm = nullptr)
47  : m_binningData(),
48  m_transform(tForm),
49  m_itransform(tForm ? new Transform3D(tForm->inverse()) : nullptr) {
50  m_binningData.reserve(3);
51  m_binningData.push_back(bData);
52  }
53 
62  BinUtility(size_t bins, float min, float max, BinningOption opt = open,
64  const std::shared_ptr<const Transform3D>& tForm = nullptr)
65  : m_binningData(),
66  m_transform(tForm),
67  m_itransform(tForm ? new Transform3D(tForm->inverse()) : nullptr) {
68  m_binningData.reserve(3);
69  m_binningData.push_back(BinningData(opt, value, bins, min, max));
70  }
71 
78  BinUtility(std::vector<float>& bValues, BinningOption opt = open,
80  const std::shared_ptr<const Transform3D>& tForm = nullptr)
81  : m_binningData(),
82  m_transform(tForm),
83  m_itransform(tForm ? new Transform3D(tForm->inverse()) : nullptr) {
84  m_binningData.reserve(3);
85  m_binningData.push_back(BinningData(opt, value, bValues));
86  }
87 
91  BinUtility(const BinUtility& sbu)
95  ? new Transform3D(sbu.m_transform->inverse())
96  : nullptr) {}
97 
102  if (this != &sbu) {
104  m_transform = sbu.m_transform;
106  ? std::unique_ptr<const Transform3D>(
107  new Transform3D(sbu.m_transform->inverse()))
108  : nullptr;
109  }
110  return (*this);
111  }
112 
117  const std::vector<BinningData>& bData = gbu.binningData();
118 
119  if (m_transform == nullptr && gbu.transform() != nullptr) {
120  // use other transform
121  m_transform = gbu.transform();
122  m_itransform =
123  std::make_unique<const Transform3D>(m_transform->inverse());
124  } else if (m_transform != nullptr && gbu.transform() != nullptr) {
125  // combine two existing transform
126  // note that this might lead to undesired behaviour of the combined
127  // BinUtility
128  m_transform = std::make_shared<const Transform3D>((*m_transform) *
129  (*gbu.transform()));
130  m_itransform =
131  std::make_unique<const Transform3D>(m_transform->inverse());
132  } // else {
133  // only this BU has transform, just keep it.
134  //}
135 
136  if (m_binningData.size() + bData.size() > 3) {
137  throw "BinUtility does not support dim > 3";
138  }
139  m_binningData.insert(m_binningData.end(), bData.begin(), bData.end());
140  return (*this);
141  }
142 
144  ~BinUtility() = default;
145 
147  const std::vector<BinningData>& binningData() const { return m_binningData; }
148 
150  size_t bins() const { return bins(0) * bins(1) * bins(2); }
151 
159  std::array<size_t, 3> binTriple(const Vector3D& position) const {
161  const Vector3D& bPosition =
162  m_itransform ? Vector3D((*m_itransform) * position) : position;
163  // get the dimension
164  size_t mdim = m_binningData.size();
166  size_t bin0 = m_binningData[0].searchGlobal(bPosition);
167  size_t bin1 = mdim > 1 ? m_binningData[1].searchGlobal(bPosition) : 0;
168  size_t bin2 = mdim > 2 ? m_binningData[2].searchGlobal(bPosition) : 0;
170  return {{bin0, bin1, bin2}};
171  }
172 
180  size_t bin(const Vector3D& position, size_t ba = 0) const {
181  if (ba >= m_binningData.size()) {
182  return 0;
183  }
184  size_t bEval =
186  ? m_binningData[ba].searchGlobal((*m_itransform) * position)
187  : m_binningData[ba].searchGlobal(position);
188  return bEval;
189  }
190 
201  std::vector<size_t> neighbourRange(const Vector3D& position,
202  size_t ba = 0) const {
203  if (ba >= m_binningData.size()) {
204  return {0};
205  }
206  std::vector<size_t> neighbourRange;
207  size_t cbin = bin(position, ba);
208  size_t pbin = cbin;
209  size_t nbin = cbin;
210  if (m_binningData[ba].decrement(pbin)) {
211  neighbourRange.push_back(pbin);
212  }
213  neighbourRange.push_back(cbin);
214  if (m_binningData[ba].increment(nbin) && nbin != pbin) {
215  neighbourRange.push_back(nbin);
216  }
217  return neighbourRange;
218  }
219 
229  int nextDirection(const Vector3D& position, const Vector3D& direction,
230  size_t ba = 0) const {
231  if (ba >= m_binningData.size()) {
232  return 0;
233  }
234  return m_binningData[ba].nextDirection(position, direction);
235  }
236 
248  size_t bin(const Vector2D& lposition, size_t ba = 0) const {
249  if (ba >= m_binningData.size()) {
250  return 0;
251  }
252  return m_binningData[ba].searchLocal(lposition);
253  }
258  bool inside(const Vector3D& position) const {
260  const Vector3D& bPosition =
261  m_itransform ? Vector3D((*m_itransform) * position) : position;
262  // loop and break
263  for (auto& bData : m_binningData) {
264  if (!(bData.inside(bPosition))) {
265  return false;
266  }
267  }
268  // survived all the checks
269  return true;
270  }
271 
276  bool inside(const Vector2D& lposition) const {
277  return true;
278  std::vector<BinningData>::const_iterator bdIter = m_binningData.begin();
279  for (; bdIter != m_binningData.end(); ++bdIter) {
280  if (!(*bdIter).inside(lposition)) {
281  return false;
282  }
283  }
284  return true;
285  }
286 
289  size_t dimensions() const { return m_binningData.size(); }
290 
296  size_t max(size_t ba = 0) const {
297  if (ba >= m_binningData.size()) {
298  return 0;
299  }
300  return (m_binningData[ba].bins() - 1);
301  }
302 
308  size_t bins(size_t ba) const {
309  if (ba >= m_binningData.size()) {
310  return 1;
311  }
312  return (m_binningData[ba].bins());
313  }
314 
318  std::shared_ptr<const Transform3D> transform() const { return m_transform; }
319 
325  BinningValue binningValue(size_t ba = 0) const {
326  if (ba >= m_binningData.size()) {
327  throw "dimension out of bounds";
328  }
329  return (m_binningData[ba].binvalue);
330  }
331 
336  size_t serialize(const std::array<size_t, 3>& bin) const {
337  size_t serializedBin = bin[0];
338  if (m_binningData.size() == 2) {
339  serializedBin += bin[1] * m_binningData[0].bins();
340  } else if (m_binningData.size() == 3) {
341  serializedBin +=
342  (bin[1] * m_binningData[0].bins() * bin[2] * m_binningData[1].bins());
343  }
344  return serializedBin;
345  }
346 
350  std::ostream& toStream(std::ostream& sl) const {
351  sl << "BinUtility for " << m_binningData.size()
352  << "- dimensional array:" << std::endl;
353  std::vector<BinningData>::const_iterator bdIter = m_binningData.begin();
354  for (size_t ibd = 0; bdIter != m_binningData.end(); ++bdIter, ++ibd) {
355  sl << "dimension : " << ibd << std::endl;
356  sl << " - type : " << size_t((*bdIter).type) << std::endl;
357  sl << " - option : " << size_t((*bdIter).option) << std::endl;
358  sl << " - value : " << size_t((*bdIter).binvalue) << std::endl;
359  sl << " - bins : " << (*bdIter).bins() << std::endl;
360  sl << " - min/max : " << (*bdIter).min << " / " << (*bdIter).max
361  << std::endl;
362  if ((*bdIter).type == equidistant) {
363  sl << " - step : " << (*bdIter).step << std::endl;
364  }
365  sl << " - boundaries : | ";
366  std::vector<float>::const_iterator bIter = (*bdIter).boundaries().begin();
367  for (; bIter != (*bdIter).boundaries().end(); ++bIter) {
368  sl << (*bIter) << " | ";
369  }
370  sl << std::endl;
371  }
372  return sl;
373  }
374 
375  private:
376  std::vector<BinningData> m_binningData;
377  std::shared_ptr<const Transform3D> m_transform;
378  std::unique_ptr<const Transform3D> m_itransform;
379 };
380 
382 std::ostream& operator<<(std::ostream& sl, const BinUtility& bgen);
383 
384 } // namespace Acts