Skip to content

Commit 6eadae9

Browse files
committed
[hist] Enable RHist with RCategoricalAxis
1 parent 2a93eae commit 6eadae9

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

hist/histv7/inc/ROOT/RHist.hxx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define ROOT_RHist
77

88
#include "RBinIndex.hxx"
9+
#include "RCategoricalAxis.hxx"
910
#include "RHistEngine.hxx"
1011
#include "RHistStats.hxx"
1112
#include "RWeight.hxx"
@@ -17,6 +18,7 @@
1718
#include <stdexcept>
1819
#include <tuple>
1920
#include <utility>
21+
#include <variant>
2022
#include <vector>
2123

2224
class TBuffer;
@@ -73,7 +75,16 @@ public:
7375
/// Construct a histogram.
7476
///
7577
/// \param[in] axes the axis objects, must have size > 0
76-
explicit RHist(std::vector<RAxisVariant> axes) : fEngine(std::move(axes)), fStats(fEngine.GetNDimensions()) {}
78+
explicit RHist(std::vector<RAxisVariant> axes) : fEngine(std::move(axes)), fStats(fEngine.GetNDimensions())
79+
{
80+
// The axes parameter was moved, use from the engine.
81+
const auto &engineAxes = fEngine.GetAxes();
82+
for (std::size_t i = 0; i < engineAxes.size(); i++) {
83+
if (std::get_if<RCategoricalAxis>(&engineAxes[i])) {
84+
fStats.DisableDimension(i);
85+
}
86+
}
87+
}
7788

7889
/// Construct a one-dimensional histogram engine with a regular axis.
7990
///

hist/histv7/test/hist_hist.cxx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,43 @@ TEST(RHist, FillWeight)
172172
EXPECT_FLOAT_EQ(hist.ComputeStdDev(), 0.49913420);
173173
}
174174

175+
TEST(RHist, FillCategorical)
176+
{
177+
const std::vector<std::string> categories = {"a", "b", "c"};
178+
const RCategoricalAxis axis(categories);
179+
RHist<int> hist({axis});
180+
181+
hist.Fill("b");
182+
hist.Fill(std::make_tuple("c"));
183+
184+
EXPECT_EQ(hist.GetBinContent(RBinIndex(1)), 1);
185+
std::array<RBinIndex, 1> indices = {2};
186+
EXPECT_EQ(hist.GetBinContent(indices), 1);
187+
188+
EXPECT_EQ(hist.GetNEntries(), 2);
189+
EXPECT_FLOAT_EQ(hist.ComputeNEffectiveEntries(), 2);
190+
}
191+
192+
TEST(RHist, FillCategoricalWeight)
193+
{
194+
const std::vector<std::string> categories = {"a", "b", "c"};
195+
const RCategoricalAxis axis(categories);
196+
RHist<float> hist({axis});
197+
198+
hist.Fill("b", RWeight(0.8));
199+
hist.Fill(std::make_tuple("c"), RWeight(0.9));
200+
201+
EXPECT_FLOAT_EQ(hist.GetBinContent(RBinIndex(1)), 0.8);
202+
std::array<RBinIndex, 1> indices = {2};
203+
EXPECT_FLOAT_EQ(hist.GetBinContent(indices), 0.9);
204+
205+
EXPECT_EQ(hist.GetNEntries(), 2);
206+
EXPECT_FLOAT_EQ(hist.GetStats().GetSumW(), 1.7);
207+
EXPECT_FLOAT_EQ(hist.GetStats().GetSumW2(), 1.45);
208+
// Cross-checked with TH1
209+
EXPECT_FLOAT_EQ(hist.ComputeNEffectiveEntries(), 1.9931034);
210+
}
211+
175212
TEST(RHist, Scale)
176213
{
177214
static constexpr std::size_t Bins = 20;

0 commit comments

Comments
 (0)