Skip to content

Commit b6707b1

Browse files
committed
[hist] Propagate disabled dimensions in RHistFillContext
This enables RHist with RCategoricalAxis for RHistConcurrentFiller.
1 parent 6eadae9 commit b6707b1

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

hist/histv7/inc/ROOT/RHistFillContext.hxx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@ private:
3636
RHistStats fStats;
3737

3838
/// \sa RHistConcurrentFiller::CreateFillContent()
39-
explicit RHistFillContext(RHist<BinContentType> &hist) : fHist(&hist), fStats(hist.GetNDimensions()) {}
39+
explicit RHistFillContext(RHist<BinContentType> &hist) : fHist(&hist), fStats(hist.GetNDimensions())
40+
{
41+
// Propagate disabled dimensions to the local histogram statistics object.
42+
const auto &histStats = hist.GetStats();
43+
for (std::size_t i = 0; i < histStats.GetNDimensions(); i++) {
44+
if (!histStats.IsEnabled(i)) {
45+
fStats.DisableDimension(i);
46+
}
47+
}
48+
}
4049
RHistFillContext(const RHistFillContext<BinContentType> &) = delete;
4150
RHistFillContext(RHistFillContext<BinContentType> &&) = default;
4251
RHistFillContext<BinContentType> &operator=(const RHistFillContext<BinContentType> &) = delete;

hist/histv7/test/hist_concurrent.cxx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
#include <memory>
44
#include <stdexcept>
5+
#include <string>
56
#include <utility>
7+
#include <vector>
68

79
TEST(RHistConcurrentFiller, Constructor)
810
{
@@ -141,6 +143,53 @@ TEST(RHistFillContext, StressFillWeight)
141143
EXPECT_FLOAT_EQ(hist->ComputeMean(), 0.5);
142144
}
143145

146+
TEST(RHistFillContext, FillCategorical)
147+
{
148+
const std::vector<std::string> categories = {"a", "b", "c"};
149+
const RCategoricalAxis axis(categories);
150+
const std::vector<RAxisVariant> axes = {axis};
151+
auto hist = std::make_shared<RHist<int>>(axes);
152+
153+
{
154+
RHistConcurrentFiller filler(hist);
155+
auto context = filler.CreateFillContext();
156+
context->Fill("b");
157+
context->Fill(std::make_tuple("c"));
158+
}
159+
160+
EXPECT_EQ(hist->GetBinContent(RBinIndex(1)), 1);
161+
std::array<RBinIndex, 1> indices = {2};
162+
EXPECT_EQ(hist->GetBinContent(indices), 1);
163+
164+
EXPECT_EQ(hist->GetNEntries(), 2);
165+
EXPECT_FLOAT_EQ(hist->ComputeNEffectiveEntries(), 2);
166+
}
167+
168+
TEST(RHistFillContext, FillCategoricalWeight)
169+
{
170+
const std::vector<std::string> categories = {"a", "b", "c"};
171+
const RCategoricalAxis axis(categories);
172+
const std::vector<RAxisVariant> axes = {axis};
173+
auto hist = std::make_shared<RHist<float>>(axes);
174+
175+
{
176+
RHistConcurrentFiller filler(hist);
177+
auto context = filler.CreateFillContext();
178+
context->Fill("b", RWeight(0.8));
179+
context->Fill(std::make_tuple("c"), RWeight(0.9));
180+
}
181+
182+
EXPECT_FLOAT_EQ(hist->GetBinContent(RBinIndex(1)), 0.8);
183+
std::array<RBinIndex, 1> indices = {2};
184+
EXPECT_FLOAT_EQ(hist->GetBinContent(indices), 0.9);
185+
186+
EXPECT_EQ(hist->GetNEntries(), 2);
187+
EXPECT_FLOAT_EQ(hist->GetStats().GetSumW(), 1.7);
188+
EXPECT_FLOAT_EQ(hist->GetStats().GetSumW2(), 1.45);
189+
// Cross-checked with TH1
190+
EXPECT_FLOAT_EQ(hist->ComputeNEffectiveEntries(), 1.9931034);
191+
}
192+
144193
TEST(RHistFillContext, Flush)
145194
{
146195
static constexpr std::size_t Bins = 20;

0 commit comments

Comments
 (0)