Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-16 09:43:28

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Utilities/Helpers.hpp"
0013 #include "Acts/Utilities/IMultiAxis.hpp"
0014 
0015 #include <algorithm>
0016 
0017 namespace Acts {
0018 
0019 /// @brief Multi-dimensional binning defined by a product of one-dimensional
0020 /// axes
0021 ///
0022 /// This class stores a fixed set of concrete @c Axis objects in a tuple and
0023 /// implements the @c IMultiAxisXD interface for the resulting grid. The grid
0024 /// dimension and the concrete axis types (binning and boundary types) are
0025 /// fixed at compile time, while the @c IMultiAxis base allows handling
0026 /// different multi-axes through a common pointer. The grid index conventions
0027 /// (per-axis local bin indices starting at @c 1, under-/overflow bins at @c 0
0028 /// and <tt>nBins + 1</tt>, and flattened global bin indices including those
0029 /// under-/overflow bins) are described on @c IMultiAxis.
0030 ///
0031 /// @tparam Axes parameter pack of concrete @c Axis types spanning the grid
0032 template <AxisConcept... Axes>
0033 class MultiAxis : public IMultiAxisXD<sizeof...(Axes)> {
0034  public:
0035   /// Base interface for this multi-axis' dimension
0036   using Base = IMultiAxisXD<sizeof...(Axes)>;
0037 
0038   /// Dimension of the grid (number of axes)
0039   static constexpr std::size_t DIM = Base::DIM;
0040   /// Flattened global bin index across all axes
0041   using GlobalBin = typename Base::GlobalBin;
0042   /// Statically sized multi-index holding one local bin index per axis
0043   using LocalBins = typename Base::LocalBins;
0044   /// Statically sized point holding one coordinate per axis
0045   using Point = typename Base::Point;
0046   /// Point holding one coordinate per axis as an Eigen (algebra) vector
0047   using VectorPoint = Vector<DIM>;
0048 
0049   /// Tuple type holding the concrete axes
0050   using AxesTuple = std::tuple<Axes...>;
0051 
0052   /// Construct from a tuple of axes (copy)
0053   /// @param axes tuple of axes spanning the grid
0054   explicit MultiAxis(const std::tuple<Axes...>& axes) : m_axes(axes) {}
0055 
0056   /// Construct from a tuple of axes (move)
0057   /// @param axes tuple of axes spanning the grid
0058   explicit MultiAxis(std::tuple<Axes...>&& axes) : m_axes(std::move(axes)) {}
0059 
0060   /// Construct from individual axes (forwarding)
0061   /// @param axes axes spanning the grid
0062   explicit MultiAxis(Axes&&... axes) : m_axes(std::forward_as_tuple(axes...)) {}
0063 
0064   /// Construct from individual axes (copy)
0065   /// @param axes axes spanning the grid
0066   explicit MultiAxis(const Axes&... axes) : m_axes(std::tuple(axes...)) {}
0067 
0068   /// @copydoc IMultiAxisXD::getAxis(std::size_t) const
0069   const IAxis& getAxis(std::size_t i) const final {
0070     return template_switch_lambda<0, DIM - 1>(
0071         i, [this]<typename T>(T) -> const IAxis& {
0072           constexpr std::size_t iValue = T::value;
0073           return std::get<iValue>(m_axes);
0074         });
0075   }
0076 
0077   /// Get the tuple of concrete axes
0078   /// @return const reference to the stored axes tuple
0079   const AxesTuple& getAxesTuple() const { return m_axes; }
0080 
0081   /// @copydoc IMultiAxisXD::getNBins() const
0082   LocalBins getNBins() const final {
0083     return detail::MultiAxisHelper::getNBins(m_axes);
0084   }
0085 
0086   /// @copydoc IMultiAxisXD::getMinPoint() const
0087   Point getMinPoint() const final {
0088     return detail::MultiAxisHelper::getMin(m_axes);
0089   }
0090 
0091   /// @copydoc IMultiAxisXD::getMaxPoint() const
0092   Point getMaxPoint() const final {
0093     return detail::MultiAxisHelper::getMax(m_axes);
0094   }
0095 
0096   /// @copydoc IMultiAxisXD::isInside(const Point&) const
0097   bool isInside(const Point& point) const final {
0098     return detail::MultiAxisHelper::isInside(point, m_axes);
0099   }
0100 
0101   /// Check whether a point lies inside the grid limits
0102   /// @tparam Derived Eigen expression type of the point (e.g. @c VectorPoint)
0103   /// @param point coordinates to check, one per axis
0104   /// @return @c true if the point is within range along every axis
0105   template <typename Derived>
0106   bool isInside(const Eigen::MatrixBase<Derived>& point) const {
0107     return detail::MultiAxisHelper::isInside(point.derived(), m_axes);
0108   }
0109 
0110   /// @copydoc IMultiAxisXD::getLowerLeftBinEdge(const LocalBins&) const
0111   Point getLowerLeftBinEdge(const LocalBins& localBins) const final {
0112     return detail::MultiAxisHelper::getLowerLeftBinEdge(localBins, m_axes);
0113   }
0114 
0115   /// @copydoc IMultiAxisXD::getUpperRightBinEdge(const LocalBins&) const
0116   Point getUpperRightBinEdge(const LocalBins& localBins) const final {
0117     return detail::MultiAxisHelper::getUpperRightBinEdge(localBins, m_axes);
0118   }
0119 
0120   /// @copydoc IMultiAxisXD::getBinCenter(const LocalBins&) const
0121   Point getBinCenter(const LocalBins& localBins) const final {
0122     return detail::MultiAxisHelper::getBinCenter(localBins, m_axes);
0123   }
0124 
0125   /// @copydoc IMultiAxisXD::getBinWidth(const LocalBins&) const
0126   Point getBinWidth(const LocalBins& localBins) const final {
0127     return detail::MultiAxisHelper::getBinWidth(localBins, m_axes);
0128   }
0129 
0130   /// @copydoc IMultiAxisXD::getGlobalBinFromPoint(const Point&) const
0131   GlobalBin getGlobalBinFromPoint(const Point& point) const final {
0132     return getGlobalBinFromLocalBins(getLocalBinsFromPoint(point));
0133   }
0134 
0135   /// Determine the flattened global bin index for a given point
0136   /// @tparam Derived Eigen expression type of the point (e.g. @c VectorPoint)
0137   /// @param point coordinates to look up, one per axis
0138   /// @return global bin index of the bin containing the point
0139   template <typename Derived>
0140   GlobalBin getGlobalBinFromPoint(
0141       const Eigen::MatrixBase<Derived>& point) const {
0142     return getGlobalBinFromLocalBins(getLocalBinsFromPoint(point));
0143   }
0144 
0145   /// Determine the flattened global bin index of the bin with the lower left
0146   /// edge closest to the given point for each axis
0147   /// @param point coordinates to look up, one per axis
0148   /// @return global bin index of the bin
0149   GlobalBin getGlobalBinFromLowerLeftEdge(const Point& point) const {
0150     return getGlobalBinFromLocalBins(getLocalBinsFromLowerLeftEdge(point));
0151   }
0152 
0153   /// @copydoc getGlobalBinFromLowerLeftEdge(const Point&) const
0154   /// @tparam Derived Eigen expression type of the point (e.g. @c VectorPoint)
0155   template <typename Derived>
0156   GlobalBin getGlobalBinFromLowerLeftEdge(
0157       const Eigen::MatrixBase<Derived>& point) const {
0158     return getGlobalBinFromLocalBins(getLocalBinsFromLowerLeftEdge(point));
0159   }
0160 
0161   /// @copydoc IMultiAxisXD::getGlobalBinFromLocalBins(const LocalBins&) const
0162   GlobalBin getGlobalBinFromLocalBins(const LocalBins& localBins) const final {
0163     return detail::MultiAxisHelper::getGlobalBinFromLocalBins(localBins,
0164                                                               m_axes);
0165   }
0166 
0167   /// @copydoc IMultiAxisXD::getLocalBinsFromPoint(const Point&) const
0168   LocalBins getLocalBinsFromPoint(const Point& point) const final {
0169     return detail::MultiAxisHelper::getLocalBinsFromPoint(point, m_axes);
0170   }
0171 
0172   /// Determine the multi-index of local bin indices for a given point
0173   /// @tparam Derived Eigen expression type of the point (e.g. @c VectorPoint)
0174   /// @param point coordinates to look up, one per axis
0175   /// @return local bin indices along each axis (may be under-/overflow bins)
0176   template <typename Derived>
0177   LocalBins getLocalBinsFromPoint(
0178       const Eigen::MatrixBase<Derived>& point) const {
0179     return detail::MultiAxisHelper::getLocalBinsFromPoint(point.derived(),
0180                                                           m_axes);
0181   }
0182 
0183   /// Determine the multi-index of local bin indices for a given point, where
0184   /// the point is interpreted as being shifted by half a bin width along each
0185   /// axis.
0186   /// @param point coordinates to look up, one per axis
0187   /// @return local bin indices along each axis (may be under-/overflow bins)
0188   LocalBins getLocalBinsFromLowerLeftEdge(const Point& point) const {
0189     return detail::MultiAxisHelper::getLocalBinsFromLowerLeftEdge(point,
0190                                                                   m_axes);
0191   }
0192 
0193   /// @copydoc getLocalBinsFromLowerLeftEdge(const Point&) const
0194   /// @tparam Derived Eigen expression type of the point (e.g. @c VectorPoint)
0195   template <typename Derived>
0196   LocalBins getLocalBinsFromLowerLeftEdge(
0197       const Eigen::MatrixBase<Derived>& point) const {
0198     return detail::MultiAxisHelper::getLocalBinsFromLowerLeftEdge(
0199         point.derived(), m_axes);
0200   }
0201 
0202   /// @copydoc IMultiAxisXD::getLocalBinsFromGlobalBin(GlobalBin) const
0203   LocalBins getLocalBinsFromGlobalBin(GlobalBin globalBin) const final {
0204     return detail::MultiAxisHelper::getLocalBinsFromGlobalBin(globalBin,
0205                                                               m_axes);
0206   }
0207 
0208   /// @copydoc IMultiAxisXD::getNeighborHoodIndices(const LocalBins&, std::size_t) const
0209   detail::FlatNeighborHoodIndices<DIM> getNeighborHoodIndices(
0210       const LocalBins& localBins, std::size_t size = 1u) const final {
0211     return detail::MultiAxisHelper::neighborHoodIndices(localBins, size,
0212                                                         m_axes);
0213   }
0214 
0215   /// @copydoc IMultiAxisXD::getNeighborHoodIndices(const LocalBins&, const std::pair<int, int>&) const
0216   detail::FlatNeighborHoodIndices<DIM> getNeighborHoodIndices(
0217       const LocalBins& localBins, const std::pair<int, int>& size) const final {
0218     return detail::MultiAxisHelper::neighborHoodIndices(localBins, size,
0219                                                         m_axes);
0220   }
0221 
0222   /// @copydoc IMultiAxisXD::getNeighborHoodIndices(const LocalBins&, const std::array<std::pair<int, int>, DIM>&) const
0223   detail::FlatNeighborHoodIndices<DIM> getNeighborHoodIndices(
0224       const LocalBins& localBins,
0225       const std::array<std::pair<int, int>, DIM>& sizePerAxis) const final {
0226     return detail::MultiAxisHelper::neighborHoodIndices(localBins, sizePerAxis,
0227                                                         m_axes);
0228   }
0229 
0230   /// @copydoc IMultiAxisXD::getClosestPointsIndices(const LocalBins&) const
0231   detail::FlatNeighborHoodIndices<DIM> getClosestPointsIndices(
0232       const LocalBins& localBins) const override {
0233     return getNeighborHoodIndices(localBins, {0, 1});
0234   }
0235 
0236   /// @copydoc IMultiAxisXD::getClosestPointsIndices(const Point&) const
0237   detail::FlatNeighborHoodIndices<DIM> getClosestPointsIndices(
0238       const Point& point) const override {
0239     return getNeighborHoodIndices(getLocalBinsFromPoint(point), {0, 1});
0240   }
0241 
0242   /// Get the global bin indices of the grid points closest to the given point
0243   /// @tparam Derived Eigen expression type of the point (e.g. @c VectorPoint)
0244   /// @param point coordinates to look up, one per axis
0245   /// @return sorted collection of global bin indices of the closest grid points
0246   template <typename Derived>
0247   detail::FlatNeighborHoodIndices<DIM> getClosestPointsIndices(
0248       const Eigen::MatrixBase<Derived>& point) const {
0249     return getNeighborHoodIndices(getLocalBinsFromPoint(point), {0, 1});
0250   }
0251 
0252  private:
0253   /// tuple of concrete axes spanning the grid
0254   std::tuple<Axes...> m_axes;
0255 };
0256 
0257 }  // namespace Acts