Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Grid.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Grid.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2023 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 
9 #pragma once
10 
12 #include "Acts/Utilities/IAxis.hpp"
16 
17 #include <array>
18 #include <numeric>
19 #include <set>
20 #include <tuple>
21 #include <type_traits>
22 #include <vector>
23 
24 namespace Acts {
25 
26 namespace detail {
27 
38 template <typename T, class... Axes>
39 class Grid final {
40  public:
42  static constexpr size_t DIM = sizeof...(Axes);
43 
45  using value_type = T;
49  using const_reference = const value_type&;
51  using point_t = std::array<ActsScalar, DIM>;
53  using index_t = std::array<size_t, DIM>;
54 
58  Grid(std::tuple<Axes...>& axes) = delete;
59  Grid(std::tuple<Axes...>&& axes) : m_axes(std::move(axes)) {
60  m_values.resize(size());
61  }
62 
76  //
77  template <class Point>
78  reference atPosition(const Point& point) {
79  return m_values.at(globalBinFromPosition(point));
80  }
81 
96  template <class Point>
97  const_reference atPosition(const Point& point) const {
98  return m_values.at(globalBinFromPosition(point));
99  }
100 
106  reference at(size_t bin) { return m_values.at(bin); }
107 
113  const_reference at(size_t bin) const { return m_values.at(bin); }
114 
123  reference atLocalBins(const index_t& localBins) {
124  return m_values.at(globalBinFromLocalBins(localBins));
125  }
126 
135  const_reference atLocalBins(const index_t& localBins) const {
136  return m_values.at(globalBinFromLocalBins(localBins));
137  }
138 
150  template <class Point>
152  const Point& position) const {
154  }
155 
159  static constexpr size_t dimensions() { return DIM; }
160 
168  std::array<ActsScalar, DIM> binCenter(const index_t& localBins) const {
169  return grid_helper::getBinCenter(localBins, m_axes);
170  }
171 
183  template <class Point>
184  size_t globalBinFromPosition(const Point& point) const {
186  }
187 
195  size_t globalBinFromLocalBins(const index_t& localBins) const {
196  return grid_helper::getGlobalBin(localBins, m_axes);
197  }
198 
211  template <class Point>
212  size_t globalBinFromFromLowerLeftEdge(const Point& point) const {
214  }
215 
228  template <class Point>
229  index_t localBinsFromPosition(const Point& point) const {
231  }
232 
241  index_t localBinsFromGlobalBin(size_t bin) const {
243  }
244 
258  template <class Point>
260  Point shiftedPoint;
262  for (size_t i = 0; i < DIM; i++) {
263  shiftedPoint[i] = point[i] + width[i] / 2;
264  }
265  return grid_helper::getLocalBinIndices(shiftedPoint, m_axes);
266  }
267 
275  point_t lowerLeftBinEdge(const index_t& localBins) const {
276  return grid_helper::getLowerLeftBinEdge(localBins, m_axes);
277  }
278 
286  point_t upperRightBinEdge(const index_t& localBins) const {
287  return grid_helper::getUpperRightBinEdge(localBins, m_axes);
288  }
289 
294 
301 
306 
311 
319  at(index) = value;
320  }
321  }
322 
348  template <class Point, typename U = T,
349  typename = std::enable_if_t<
351  std::array<ActsScalar, DIM>, U>::value>>
352  T interpolate(const Point& point) const {
353  // there are 2^DIM corner points used during the interpolation
354  constexpr size_t nCorners = 1 << DIM;
355 
356  // construct vector of pairs of adjacent bin centers and values
357  std::array<value_type, nCorners> neighbors{};
358 
359  // get local indices for current bin
360  // value of bin is interpreted as being the field value at its lower left
361  // corner
362  const auto& llIndices = localBinsFromPosition(point);
363 
364  // get global indices for all surrounding corner points
365  const auto& closestIndices = rawClosestPointsIndices(llIndices);
366 
367  // get values on grid points
368  size_t i = 0;
369  for (size_t index : closestIndices) {
370  neighbors.at(i++) = at(index);
371  }
372 
373  return Acts::interpolate(point, lowerLeftBinEdge(llIndices),
374  upperRightBinEdge(llIndices), neighbors);
375  }
376 
388  template <class Point>
389  bool isInside(const Point& position) const {
390  return grid_helper::isInside(position, m_axes);
391  }
392 
409  const index_t& localBins, size_t size = 1u) const {
410  return grid_helper::neighborHoodIndices(localBins, size, m_axes);
411  }
412 
430  const index_t& localBins,
431  std::array<std::pair<int, int>, DIM>& sizePerAxis) const {
432  return grid_helper::neighborHoodIndices(localBins, sizePerAxis, m_axes);
433  }
434 
440  size_t size(bool fullCounter = true) const {
441  index_t nBinsArray = numLocalBins();
442  std::size_t current_size = 1;
443  // add under-and overflow bins for each axis and multiply all bins
444  if (fullCounter) {
445  for (const auto& value : nBinsArray) {
446  current_size *= value + 2;
447  }
448  }
449  // ignore under-and overflow bins for each axis and multiply all bins
450  else {
451  for (const auto& value : nBinsArray) {
452  current_size *= value;
453  }
454  }
455  return current_size;
456  }
457 
458  std::array<const IAxis*, DIM> axes() const {
460  }
461 
462  private:
464  std::tuple<Axes...> m_axes;
466  std::vector<T> m_values;
467 
468  // Part of closestPointsIndices that goes after local bins resolution.
469  // Used as an interpolation performance optimization, but not exposed as it
470  // doesn't make that much sense from an API design standpoint.
472  const index_t& localBins) const {
473  return grid_helper::closestPointsIndices(localBins, m_axes);
474  }
475 };
476 } // namespace detail
477 
478 } // namespace Acts