Skip to content

Commit 0eae724

Browse files
committed
[hist] Implement RHistEngine::Clone
1 parent 00e66e1 commit 0eae724

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

hist/histv7/inc/ROOT/RHistEngine.hxx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ public:
176176
}
177177
}
178178

179+
/// Clone this histogram engine.
180+
///
181+
/// \return the clone object
182+
RHistEngine<BinContentType> Clone() const
183+
{
184+
RHistEngine<BinContentType> h(fAxes.Get());
185+
for (std::size_t i = 0; i < fBinContents.size(); i++) {
186+
h.fBinContents[i] = fBinContents[i];
187+
}
188+
return h;
189+
}
190+
179191
/// Whether this histogram engine type supported weighted filling.
180192
static constexpr bool SupportsWeightedFilling = std::is_floating_point_v<BinContentType>;
181193

hist/histv7/test/hist_engine.cxx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,39 @@ TEST(RHistEngine, Clear)
128128
EXPECT_EQ(engine.GetBinContent(RBinIndex::Overflow()), 0);
129129
}
130130

131+
TEST(RHistEngine, Clone)
132+
{
133+
static constexpr std::size_t Bins = 20;
134+
const RRegularAxis axis(Bins, 0, Bins);
135+
RHistEngine<int> engineA({axis});
136+
137+
engineA.Fill(-100);
138+
for (std::size_t i = 0; i < Bins; i++) {
139+
engineA.Fill(i);
140+
}
141+
engineA.Fill(100);
142+
143+
RHistEngine<int> engineB = engineA.Clone();
144+
ASSERT_EQ(engineB.GetNDimensions(), 1);
145+
ASSERT_EQ(engineB.GetTotalNBins(), Bins + 2);
146+
147+
EXPECT_EQ(engineB.GetBinContent(RBinIndex::Underflow()), 1);
148+
for (auto index : axis.GetNormalRange()) {
149+
EXPECT_EQ(engineB.GetBinContent(index), 1);
150+
}
151+
EXPECT_EQ(engineB.GetBinContent(RBinIndex::Overflow()), 1);
152+
153+
// Check that we can continue filling the clone.
154+
for (std::size_t i = 0; i < Bins; i++) {
155+
engineB.Fill(i);
156+
}
157+
158+
for (auto index : axis.GetNormalRange()) {
159+
EXPECT_EQ(engineA.GetBinContent(index), 1);
160+
EXPECT_EQ(engineB.GetBinContent(index), 2);
161+
}
162+
}
163+
131164
TEST(RHistEngine, Fill)
132165
{
133166
static constexpr std::size_t Bins = 20;

0 commit comments

Comments
 (0)