Skip to content

Commit 0dda19a

Browse files
authored
chore: Elevate axis construction exceptions (#5594)
We only ever validated the correctness of the axis construction in the `IAxis` helpers. This just fell on my head in #5498 This PR elevates the validation to the concrete `Axis` constructors.
1 parent 9564107 commit 0dda19a

2 files changed

Lines changed: 24 additions & 25 deletions

File tree

Core/include/Acts/Utilities/Axis.hpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <algorithm>
1616
#include <cmath>
1717
#include <iostream>
18+
#include <stdexcept>
1819
#include <vector>
1920

2021
namespace Acts {
@@ -42,7 +43,19 @@ class Axis<AxisType::Equidistant, bdt> : public IAxis {
4243
m_min(xmin),
4344
m_max(xmax),
4445
m_width((xmax - xmin) / nBins),
45-
m_bins(nBins) {}
46+
m_bins(nBins) {
47+
if (m_min >= m_max) {
48+
std::string msg = "Axis: Invalid axis range'";
49+
msg += "', min edge (" + std::to_string(m_min) + ") ";
50+
msg += " needs to be smaller than max edge (";
51+
msg += std::to_string(m_max) + ").";
52+
throw std::invalid_argument(msg);
53+
}
54+
if (m_bins < 1u) {
55+
throw std::invalid_argument(
56+
"Axis: Invalid binning, at least one bin is needed.");
57+
}
58+
}
4659

4760
/// Divide the range \f$[\text{xmin},\text{xmax})\f$ into \f$\text{nBins}\f$
4861
/// equidistant bins.
@@ -325,7 +338,16 @@ class Axis<AxisType::Variable, bdt> : public IAxis {
325338
/// @pre @c binEdges must contain at least two entries.
326339
explicit Axis(std::vector<double> binEdges,
327340
std::optional<AxisDirection> direction = std::nullopt)
328-
: IAxis(direction), m_binEdges(std::move(binEdges)) {}
341+
: IAxis(direction), m_binEdges(std::move(binEdges)) {
342+
if (m_binEdges.size() < 2) {
343+
throw std::invalid_argument(
344+
"Axis: Invalid binning, at least two bin edges are needed.");
345+
}
346+
if (!std::ranges::is_sorted(m_binEdges)) {
347+
throw std::invalid_argument(
348+
"Axis: Invalid binning, bin edges are not sorted.");
349+
}
350+
}
329351

330352
/// Create a binning structure with @c nBins variable-sized bins from the
331353
/// given bin boundaries. @c nBins is given by the number of bin edges

Core/src/Utilities/IAxis.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@ std::unique_ptr<IAxis> IAxis::createEquidistant(
2121
using enum AxisType;
2222
using enum AxisBoundaryType;
2323

24-
if (min >= max) {
25-
std::string msg = "IAxis: Invalid axis range'";
26-
msg += "', min edge (" + std::to_string(min) + ") ";
27-
msg += " needs to be smaller than max edge (";
28-
msg += std::to_string(max) + ").";
29-
throw std::invalid_argument(msg);
30-
}
31-
if (nbins < 1u) {
32-
throw std::invalid_argument(
33-
"IAxis: Invalid binning, at least one bin is needed.");
34-
}
35-
3624
switch (aBoundaryType) {
3725
case Open:
3826
return std::make_unique<Axis<Equidistant, Open>>(min, max, nbins,
@@ -54,17 +42,6 @@ std::unique_ptr<IAxis> IAxis::createVariable(
5442
using enum AxisType;
5543
using enum AxisBoundaryType;
5644

57-
// Not enough edges
58-
if (edges.size() < 2) {
59-
throw std::invalid_argument(
60-
"IAxis: Invalid binning, at least two bin edges are needed.");
61-
}
62-
63-
// Not sorted
64-
if (!std::ranges::is_sorted(edges)) {
65-
throw std::invalid_argument(
66-
"IAxis: Invalid binning, bin edges are not sorted.");
67-
}
6845
switch (aBoundaryType) {
6946
case Open:
7047
return std::make_unique<Axis<Variable, Open>>(edges, direction);

0 commit comments

Comments
 (0)