|
| 1 | +#include "hist_test.hxx" |
| 2 | + |
| 3 | +#include <ROOT/RAxes.hxx> |
| 4 | + |
| 5 | +#include <stdexcept> |
| 6 | +#include <tuple> |
| 7 | +#include <variant> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +TEST(RAxes, Constructor) |
| 11 | +{ |
| 12 | + static constexpr std::size_t BinsX = 20; |
| 13 | + const RRegularAxis regularAxis(BinsX, 0, BinsX); |
| 14 | + static constexpr std::size_t BinsY = 30; |
| 15 | + std::vector<double> bins; |
| 16 | + for (std::size_t i = 0; i < BinsY; i++) { |
| 17 | + bins.push_back(i); |
| 18 | + } |
| 19 | + bins.push_back(BinsY); |
| 20 | + const RVariableBinAxis variableBinAxis(bins); |
| 21 | + |
| 22 | + RAxes axes({regularAxis, variableBinAxis}); |
| 23 | + EXPECT_EQ(axes.GetNumDimensions(), 2); |
| 24 | + const auto &v = axes.GetVector(); |
| 25 | + ASSERT_EQ(v.size(), 2); |
| 26 | + EXPECT_EQ(v[0].index(), 0); |
| 27 | + EXPECT_EQ(v[1].index(), 1); |
| 28 | + EXPECT_TRUE(std::get_if<RRegularAxis>(&v[0]) != nullptr); |
| 29 | + EXPECT_TRUE(std::get_if<RVariableBinAxis>(&v[1]) != nullptr); |
| 30 | + |
| 31 | + std::vector<RAxes::AxisVariant> newAxes{variableBinAxis, regularAxis}; |
| 32 | + axes = RAxes(newAxes); |
| 33 | + EXPECT_EQ(axes.GetNumDimensions(), 2); |
| 34 | + |
| 35 | + EXPECT_THROW(RAxes({}), std::invalid_argument); |
| 36 | +} |
| 37 | + |
| 38 | +TEST(RAxes, Equality) |
| 39 | +{ |
| 40 | + static constexpr std::size_t BinsX = 20; |
| 41 | + const RRegularAxis regularAxis(BinsX, 0, BinsX); |
| 42 | + static constexpr std::size_t BinsY = 30; |
| 43 | + std::vector<double> bins; |
| 44 | + for (std::size_t i = 0; i < BinsY; i++) { |
| 45 | + bins.push_back(i); |
| 46 | + } |
| 47 | + bins.push_back(BinsY); |
| 48 | + const RVariableBinAxis variableBinAxis(bins); |
| 49 | + |
| 50 | + const RAxes axesA({regularAxis, variableBinAxis}); |
| 51 | + const RAxes axesA2({regularAxis, variableBinAxis}); |
| 52 | + const RAxes axesB({variableBinAxis, regularAxis}); |
| 53 | + const RAxes axesC({regularAxis}); |
| 54 | + const RAxes axesD({variableBinAxis}); |
| 55 | + |
| 56 | + EXPECT_TRUE(axesA == axesA); |
| 57 | + EXPECT_TRUE(axesA == axesA2); |
| 58 | + EXPECT_TRUE(axesA2 == axesA); |
| 59 | + |
| 60 | + EXPECT_FALSE(axesA == axesB); |
| 61 | + EXPECT_FALSE(axesA == axesC); |
| 62 | + EXPECT_FALSE(axesA == axesD); |
| 63 | + |
| 64 | + EXPECT_FALSE(axesB == axesC); |
| 65 | + EXPECT_FALSE(axesB == axesD); |
| 66 | + |
| 67 | + EXPECT_FALSE(axesC == axesD); |
| 68 | +} |
| 69 | + |
| 70 | +TEST(RAxes, ComputeTotalNumBins) |
| 71 | +{ |
| 72 | + static constexpr std::size_t BinsX = 20; |
| 73 | + const RRegularAxis regularAxis(BinsX, 0, BinsX); |
| 74 | + static constexpr std::size_t BinsY = 30; |
| 75 | + std::vector<double> bins; |
| 76 | + for (std::size_t i = 0; i < BinsY; i++) { |
| 77 | + bins.push_back(i); |
| 78 | + } |
| 79 | + bins.push_back(BinsY); |
| 80 | + const RVariableBinAxis variableBinAxis(bins); |
| 81 | + const RAxes axes({regularAxis, variableBinAxis}); |
| 82 | + |
| 83 | + // Both axes include underflow and overflow bins. |
| 84 | + EXPECT_EQ(axes.ComputeTotalNumBins(), (BinsX + 2) * (BinsY + 2)); |
| 85 | +} |
| 86 | + |
| 87 | +TEST(RAxes, ComputeGlobalIndex) |
| 88 | +{ |
| 89 | + static constexpr std::size_t BinsX = 20; |
| 90 | + const RRegularAxis regularAxis(BinsX, 0, BinsX); |
| 91 | + static constexpr std::size_t BinsY = 30; |
| 92 | + std::vector<double> bins; |
| 93 | + for (std::size_t i = 0; i < BinsY; i++) { |
| 94 | + bins.push_back(i); |
| 95 | + } |
| 96 | + bins.push_back(BinsY); |
| 97 | + const RVariableBinAxis variableBinAxis(bins); |
| 98 | + const RAxes axes({regularAxis, variableBinAxis}); |
| 99 | + |
| 100 | + { |
| 101 | + const auto globalIndex = axes.ComputeGlobalIndex(std::make_tuple(1.5, 2.5)); |
| 102 | + EXPECT_EQ(globalIndex.fIndex, 1 * (BinsY + 2) + 2); |
| 103 | + EXPECT_TRUE(globalIndex.fValid); |
| 104 | + } |
| 105 | + |
| 106 | + { |
| 107 | + // Underflow bin of the first axis. |
| 108 | + const auto globalIndex = axes.ComputeGlobalIndex(std::make_tuple(-1, 2.5)); |
| 109 | + EXPECT_EQ(globalIndex.fIndex, BinsX * (BinsY + 2) + 2); |
| 110 | + EXPECT_TRUE(globalIndex.fValid); |
| 111 | + } |
| 112 | + |
| 113 | + { |
| 114 | + // Overflow bin of the second axis. |
| 115 | + const auto globalIndex = axes.ComputeGlobalIndex(std::make_tuple(1.5, 42)); |
| 116 | + EXPECT_EQ(globalIndex.fIndex, 1 * (BinsY + 2) + BinsY + 1); |
| 117 | + EXPECT_TRUE(globalIndex.fValid); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +TEST(RAxes, ComputeGlobalIndexNoFlowBins) |
| 122 | +{ |
| 123 | + static constexpr std::size_t BinsX = 20; |
| 124 | + const RRegularAxis regularAxis(BinsX, 0, BinsX, /*enableFlowBins=*/false); |
| 125 | + static constexpr std::size_t BinsY = 30; |
| 126 | + std::vector<double> bins; |
| 127 | + for (std::size_t i = 0; i < BinsY; i++) { |
| 128 | + bins.push_back(i); |
| 129 | + } |
| 130 | + bins.push_back(BinsY); |
| 131 | + const RVariableBinAxis variableBinAxis(bins, /*enableFlowBins=*/false); |
| 132 | + const RAxes axes({regularAxis, variableBinAxis}); |
| 133 | + ASSERT_EQ(axes.ComputeTotalNumBins(), BinsX * BinsY); |
| 134 | + |
| 135 | + { |
| 136 | + const auto globalIndex = axes.ComputeGlobalIndex(std::make_tuple(1.5, 2.5)); |
| 137 | + EXPECT_EQ(globalIndex.fIndex, 1 * BinsY + 2); |
| 138 | + EXPECT_TRUE(globalIndex.fValid); |
| 139 | + } |
| 140 | + |
| 141 | + { |
| 142 | + // Underflow bin of the first axis. |
| 143 | + const auto globalIndex = axes.ComputeGlobalIndex(std::make_tuple(-1, 2.5)); |
| 144 | + EXPECT_EQ(globalIndex.fIndex, 0); |
| 145 | + EXPECT_FALSE(globalIndex.fValid); |
| 146 | + } |
| 147 | + |
| 148 | + { |
| 149 | + // Overflow bin of the second axis. |
| 150 | + const auto globalIndex = axes.ComputeGlobalIndex(std::make_tuple(1.5, 42)); |
| 151 | + EXPECT_EQ(globalIndex.fIndex, 0); |
| 152 | + EXPECT_FALSE(globalIndex.fValid); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +TEST(RAxes, ComputeGlobalIndexInvalidNumberOfArguments) |
| 157 | +{ |
| 158 | + static constexpr std::size_t Bins = 20; |
| 159 | + const RRegularAxis axis(Bins, 0, Bins); |
| 160 | + const RAxes axes1({axis}); |
| 161 | + ASSERT_EQ(axes1.GetNumDimensions(), 1); |
| 162 | + const RAxes axes2({axis, axis}); |
| 163 | + ASSERT_EQ(axes2.GetNumDimensions(), 2); |
| 164 | + |
| 165 | + EXPECT_NO_THROW(axes1.ComputeGlobalIndex(std::make_tuple(1))); |
| 166 | + EXPECT_THROW(axes1.ComputeGlobalIndex(std::make_tuple(1, 2)), std::invalid_argument); |
| 167 | + |
| 168 | + EXPECT_THROW(axes2.ComputeGlobalIndex(std::make_tuple(1)), std::invalid_argument); |
| 169 | + EXPECT_NO_THROW(axes2.ComputeGlobalIndex(std::make_tuple(1, 2))); |
| 170 | + EXPECT_THROW(axes2.ComputeGlobalIndex(std::make_tuple(1, 2, 3)), std::invalid_argument); |
| 171 | +} |
0 commit comments