Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(util): Grid type-erased output and comparisons #3469

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Core/include/Acts/Utilities/Axis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,17 @@ class Axis<AxisType::Equidistant, bdt> final : public IAxis {
return binEdges;
}

friend std::ostream& operator<<(std::ostream& os, const Axis& axis) {
os << "Axis<" << bdt << ", Equidistant>(";
os << axis.m_min << ", ";
os << axis.m_max << ", ";
os << axis.m_bins << ")";
return os;
}

protected:
void toStream(std::ostream& os) const override { os << *this; }

private:
/// minimum of binning range
ActsScalar m_min{};
Expand Down Expand Up @@ -695,6 +706,19 @@ class Axis<AxisType::Variable, bdt> final : public IAxis {
/// @return Vector which contains the bin edges
std::vector<ActsScalar> getBinEdges() const override { return m_binEdges; }

friend std::ostream& operator<<(std::ostream& os, const Axis& axis) {
os << "Axis<" << bdt << ", Variable>(";
os << axis.m_binEdges.front();
for (std::size_t i = 1; i < axis.m_binEdges.size(); i++) {
os << ", " << axis.m_binEdges[i];
}
os << ")";
return os;
}

protected:
void toStream(std::ostream& os) const override { os << *this; }

private:
/// vector of bin edges (sorted in ascending order)
std::vector<ActsScalar> m_binEdges;
Expand Down
4 changes: 3 additions & 1 deletion Core/include/Acts/Utilities/AxisFwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ enum class AxisBoundaryType {
/// Tag helper type for Axis constructors with class template deduction
/// @tparam bdt the boundary type
template <AxisBoundaryType bdt>
struct AxisBoundaryTypeTag {};
struct AxisBoundaryTypeTag {
static constexpr AxisBoundaryType value = bdt;
};

/// Convenience typedefs for AxisBoundaryTypeTag
constexpr auto AxisOpen = AxisBoundaryTypeTag<AxisBoundaryType::Open>{};
Expand Down
42 changes: 42 additions & 0 deletions Core/include/Acts/Utilities/Grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <set>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>

namespace Acts {
Expand All @@ -39,6 +40,30 @@ class IGrid {
/// Get a dynamically sized vector of axis objects for inspection
/// @return a vector of axis pointers
virtual boost::container::small_vector<const IAxis*, 3> axes() const = 0;

/// Helper to print out the grid
/// @param os the output stream
/// @param grid the grid to print
/// @return the output stream
friend std::ostream& operator<<(std::ostream& os, const IGrid& grid) {
grid.toStream(os);
return os;
}

friend bool operator==(const IGrid& lhs, const IGrid& rhs) {
auto lhsAxes = lhs.axes();
auto rhsAxes = rhs.axes();
return lhsAxes.size() == rhsAxes.size() &&
std::equal(lhsAxes.begin(), lhsAxes.end(), rhsAxes.begin(),
[](const IAxis* a, const IAxis* b) { return *a == *b; });
}

friend bool operator!=(const IGrid& lhs, const IGrid& rhs) {
return !(lhs == rhs);
andiwand marked this conversation as resolved.
Show resolved Hide resolved
}

protected:
virtual void toStream(std::ostream& os) const = 0;
};

/// @brief class for describing a regular multi-dimensional grid
Expand Down Expand Up @@ -583,6 +608,11 @@ class Grid final : public IGrid {
return local_iterator_t(*this, std::move(endline), navigator);
}

protected:
void toStream(std::ostream& os) const override {
printAxes(os, std::make_index_sequence<sizeof...(Axes)>());
}

private:
/// set of axis defining the multi-dimensional grid
std::tuple<Axes...> m_axes;
Expand All @@ -596,6 +626,18 @@ class Grid final : public IGrid {
const index_t& localBins) const {
return detail::grid_helper::closestPointsIndices(localBins, m_axes);
}

template <std::size_t... Is>
void printAxes(std::ostream& os, std::index_sequence<Is...> /*s*/) const {
auto printOne = [&os, this](auto I) {
constexpr std::size_t index = decltype(I)::value;
if constexpr (index > 0) {
os << ", ";
}
os << std::get<index>(m_axes);
};
(printOne(std::integral_constant<std::size_t, Is>()), ...);
}
};

template <typename T, class... Axes>
Expand Down
63 changes: 63 additions & 0 deletions Core/include/Acts/Utilities/IAxis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Utilities/AxisFwd.hpp"

#include <iosfwd>
#include <vector>

namespace Acts {
Expand Down Expand Up @@ -56,5 +57,67 @@ class IAxis {
///
/// @return total number of bins (excluding under-/overflow bins)
virtual std::size_t getNBins() const = 0;

/// Helper function that dispatches from the @c IAxis base class
/// to a concrete axis type. It will call the provided @p callable
/// with a const reference to the concrete axis type.
/// @tparam callable_t the callable type
/// @param callable the callable object
template <typename callable_t>
decltype(auto) visit(const callable_t& callable) const {
auto switchOnType = [this, &callable](auto bdtTag) {
constexpr auto bdt = decltype(bdtTag)::value;
switch (getType()) {
case AxisType::Equidistant:
return callable(
dynamic_cast<const Axis<AxisType::Equidistant, bdt>&>(*this));
case AxisType::Variable:
return callable(
dynamic_cast<const Axis<AxisType::Variable, bdt>&>(*this));
default:
throw std::logic_error{"Unknown type case"};
}
};

switch (getBoundaryType()) {
case AxisBoundaryType::Open:
return switchOnType(AxisOpen);
case AxisBoundaryType::Bound:
return switchOnType(AxisBound);
case AxisBoundaryType::Closed:
return switchOnType(AxisClosed);
default:
throw std::logic_error{"Unknown boundary type case"};
}
}

/// Check if two axes are equal
/// @param lhs first axis
/// @param rhs second axis
/// @return true if the axes are equal
friend bool operator==(const IAxis& lhs, const IAxis& rhs);
andiwand marked this conversation as resolved.
Show resolved Hide resolved

/// Check if two axes are not equal
/// @param lhs first axis
/// @param rhs second axis
/// @return true if the axes are not equal
friend bool operator!=(const IAxis& lhs, const IAxis& rhs) {
return !(lhs == rhs);
}

/// Output stream operator
/// @param os output stream
/// @param axis the axis to be printed
/// @return the output stream
friend std::ostream& operator<<(std::ostream& os, const IAxis& axis);

protected:
/// Dispatch to the correct stream operator
/// @param os output stream
virtual void toStream(std::ostream& os) const = 0;
};

template <typename T>
concept AxisConcept = std::derived_from<T, IAxis>;

} // namespace Acts
1 change: 1 addition & 0 deletions Core/src/Utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ target_sources(
TrackHelpers.cpp
BinningType.cpp
Intersection.cpp
IAxis.cpp
)
26 changes: 26 additions & 0 deletions Core/src/Utilities/IAxis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// This file is part of the Acts project.
//
// Copyright (C) 2024 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "Acts/Utilities/IAxis.hpp"

namespace Acts {

bool operator==(const IAxis& lhs, const IAxis& rhs) {
return lhs.getType() == rhs.getType() &&
lhs.getBoundaryType() == rhs.getBoundaryType() &&
lhs.getMin() == rhs.getMin() && lhs.getMax() == rhs.getMax() &&
lhs.getNBins() == rhs.getNBins() &&
lhs.getBinEdges() == rhs.getBinEdges();
}

std::ostream& operator<<(std::ostream& os, const IAxis& axis) {
axis.toStream(os);
return os;
}

} // namespace Acts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class TestAxis : public IAxis {
ActsScalar getMax() const final { return 1.; }

std::size_t getNBins() const final { return 1; };

void toStream(std::ostream& os) const final { os << "TextAxis"; }
};

class MultiGrid1D {
Expand Down
44 changes: 44 additions & 0 deletions Tests/UnitTests/Core/Utilities/AxesTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,48 @@ BOOST_AUTO_TEST_CASE(AxisTypeDeduction) {
Axis<AxisType::Variable, AxisBoundaryType::Closed>>);
}

BOOST_AUTO_TEST_CASE(AxisVisit) {
auto eqOpen = Axis{0.0, 10., 10};
eqOpen.visit([](const auto& axis) {
BOOST_CHECK(
(std::is_same_v<std::decay_t<decltype(axis)>,
Axis<AxisType::Equidistant, AxisBoundaryType::Open>>));
});

auto eqBound = Axis{AxisBound, 0.0, 10., 10};
eqBound.visit([](const auto& axis) {
BOOST_CHECK(
(std::is_same_v<std::decay_t<decltype(axis)>,
Axis<AxisType::Equidistant, AxisBoundaryType::Bound>>));
});

auto eqClosed = Axis{AxisClosed, 0.0, 10., 10};
eqClosed.visit([](const auto& axis) {
BOOST_CHECK((
std::is_same_v<std::decay_t<decltype(axis)>,
Axis<AxisType::Equidistant, AxisBoundaryType::Closed>>));
});

auto varOpen = Axis{{0, 1, 2., 3, 4}};
varOpen.visit([](const auto& axis) {
BOOST_CHECK(
(std::is_same_v<std::decay_t<decltype(axis)>,
Axis<AxisType::Variable, AxisBoundaryType::Open>>));
});

auto varBound = Axis{AxisBound, {0, 1, 2., 3, 4}};
varBound.visit([](const auto& axis) {
BOOST_CHECK(
(std::is_same_v<std::decay_t<decltype(axis)>,
Axis<AxisType::Variable, AxisBoundaryType::Bound>>));
});

auto varClosed = Axis{AxisClosed, {0, 1, 2., 3, 4}};
varClosed.visit([](const auto& axis) {
BOOST_CHECK(
(std::is_same_v<std::decay_t<decltype(axis)>,
Axis<AxisType::Variable, AxisBoundaryType::Closed>>));
});
}

} // namespace Acts::Test
Loading