|
2 | 2 |
|
3 | 3 | #include <memory> |
4 | 4 | #include <stdexcept> |
| 5 | +#include <string> |
5 | 6 | #include <utility> |
| 7 | +#include <vector> |
6 | 8 |
|
7 | 9 | TEST(RHistConcurrentFiller, Constructor) |
8 | 10 | { |
@@ -141,6 +143,53 @@ TEST(RHistFillContext, StressFillWeight) |
141 | 143 | EXPECT_FLOAT_EQ(hist->ComputeMean(), 0.5); |
142 | 144 | } |
143 | 145 |
|
| 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 | + |
144 | 193 | TEST(RHistFillContext, Flush) |
145 | 194 | { |
146 | 195 | static constexpr std::size_t Bins = 20; |
|
0 commit comments