Skip to content

Commit b15d1a8

Browse files
committed
[hist] Test forwarding of Fill arguments
They should not be copied, which might be expensive.
1 parent caf49e8 commit b15d1a8

7 files changed

Lines changed: 148 additions & 0 deletions

File tree

hist/histv7/test/hist_axes.cxx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,20 @@ TEST(RAxes, ComputeGlobalIndexNoFlowBins)
251251
}
252252
}
253253

254+
TEST(RAxes, ComputeGlobalIndexForward)
255+
{
256+
static constexpr std::size_t Bins = 20;
257+
const RRegularAxis axis(Bins, {0, Bins});
258+
const RAxes axes({axis});
259+
260+
std::tuple<CopyArgument> args(1.5);
261+
auto globalIndex = axes.ComputeGlobalIndex(args);
262+
EXPECT_EQ(globalIndex.fIndex, 2);
263+
EXPECT_TRUE(globalIndex.fValid);
264+
265+
ASSERT_FALSE(CopyArgument::HasBeenCopied());
266+
}
267+
254268
TEST(RAxes, ComputeGlobalIndexInvalidNumberOfArguments)
255269
{
256270
static constexpr std::size_t Bins = 20;

hist/histv7/test/hist_concurrent.cxx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,36 @@ TEST(RHistFillContext, FillCategoricalWeight)
190190
EXPECT_FLOAT_EQ(hist->ComputeNEffectiveEntries(), 1.9931034);
191191
}
192192

193+
TEST(RHistFillContext, FillForward)
194+
{
195+
static constexpr std::size_t Bins = 20;
196+
auto hist = std::make_shared<RHist<float>>(Bins, std::make_pair(0, Bins));
197+
198+
{
199+
RHistConcurrentFiller filler(hist);
200+
auto context = filler.CreateFillContext();
201+
std::tuple<CopyArgument> args(1.5);
202+
context->Fill(args);
203+
context->Fill(args, RWeight(0.5));
204+
}
205+
EXPECT_EQ(hist->GetNEntries(), 2);
206+
EXPECT_EQ(hist->GetBinContent(1), 1.5);
207+
208+
ASSERT_FALSE(CopyArgument::HasBeenCopied());
209+
210+
{
211+
RHistConcurrentFiller filler(hist);
212+
auto context = filler.CreateFillContext();
213+
CopyArgument arg(2.5);
214+
context->Fill(arg);
215+
context->Fill(arg, RWeight(0.5));
216+
}
217+
EXPECT_EQ(hist->GetNEntries(), 4);
218+
EXPECT_EQ(hist->GetBinContent(2), 1.5);
219+
220+
ASSERT_FALSE(CopyArgument::HasBeenCopied());
221+
}
222+
193223
TEST(RHistFillContext, Flush)
194224
{
195225
static constexpr std::size_t Bins = 20;

hist/histv7/test/hist_engine.cxx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,26 @@ TEST(RHistEngine, FillWeightNegative)
492492
EXPECT_EQ(engine.GetBinContent(RBinIndex(1)), 0);
493493
}
494494

495+
TEST(RHistEngine, FillForward)
496+
{
497+
static constexpr std::size_t Bins = 20;
498+
RHistEngine<float> engine(Bins, {0, Bins});
499+
500+
std::tuple<CopyArgument> args(1.5);
501+
engine.Fill(args);
502+
engine.Fill(args, RWeight(0.5));
503+
EXPECT_EQ(engine.GetBinContent(1), 1.5);
504+
505+
ASSERT_FALSE(CopyArgument::HasBeenCopied());
506+
507+
CopyArgument arg(2.5);
508+
engine.Fill(arg);
509+
engine.Fill(arg, RWeight(0.5));
510+
EXPECT_EQ(engine.GetBinContent(2), 1.5);
511+
512+
ASSERT_FALSE(CopyArgument::HasBeenCopied());
513+
}
514+
495515
TEST(RHistEngine, Scale)
496516
{
497517
static constexpr std::size_t Bins = 20;

hist/histv7/test/hist_engine_atomic.cxx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,26 @@ TEST(RHistEngine, FillAtomicTupleWeightInvalidNumberOfArguments)
237237
EXPECT_THROW(engine2.FillAtomic(std::make_tuple(1, 2, 3), RWeight(1)), std::invalid_argument);
238238
}
239239

240+
TEST(RHistEngine, FillAtomicForward)
241+
{
242+
static constexpr std::size_t Bins = 20;
243+
RHistEngine<float> engine(Bins, {0, Bins});
244+
245+
std::tuple<CopyArgument> args(1.5);
246+
engine.FillAtomic(args);
247+
engine.FillAtomic(args, RWeight(0.5));
248+
EXPECT_EQ(engine.GetBinContent(1), 1.5);
249+
250+
ASSERT_FALSE(CopyArgument::HasBeenCopied());
251+
252+
CopyArgument arg(2.5);
253+
engine.FillAtomic(arg);
254+
engine.FillAtomic(arg, RWeight(0.5));
255+
EXPECT_EQ(engine.GetBinContent(2), 1.5);
256+
257+
ASSERT_FALSE(CopyArgument::HasBeenCopied());
258+
}
259+
240260
TEST(RHistEngine, StressFillAddAtomicWeight)
241261
{
242262
static constexpr std::size_t NThreads = 4;

hist/histv7/test/hist_hist.cxx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,28 @@ TEST(RHist, FillExceptionSafety)
350350
EXPECT_EQ(hist.GetStats().GetDimensionStats(1).fSumWX, 2.5);
351351
}
352352

353+
TEST(RHist, FillForward)
354+
{
355+
static constexpr std::size_t Bins = 20;
356+
RHist<float> hist(Bins, {0, Bins});
357+
358+
std::tuple<CopyArgument> args(1.5);
359+
hist.Fill(args);
360+
hist.Fill(args, RWeight(0.5));
361+
EXPECT_EQ(hist.GetNEntries(), 2);
362+
EXPECT_EQ(hist.GetBinContent(1), 1.5);
363+
364+
ASSERT_FALSE(CopyArgument::HasBeenCopied());
365+
366+
CopyArgument arg(2.5);
367+
hist.Fill(arg);
368+
hist.Fill(arg, RWeight(0.5));
369+
EXPECT_EQ(hist.GetNEntries(), 4);
370+
EXPECT_EQ(hist.GetBinContent(2), 1.5);
371+
372+
ASSERT_FALSE(CopyArgument::HasBeenCopied());
373+
}
374+
353375
TEST(RHist, Scale)
354376
{
355377
static constexpr std::size_t Bins = 20;

hist/histv7/test/hist_stats.cxx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,25 @@ TEST(RHistStats, FillExceptionSafety)
664664
EXPECT_EQ(stats.GetDimensionStats(1).fSumWX, 2);
665665
}
666666

667+
TEST(RHistEngine, FillForward)
668+
{
669+
RHistStats stats(1);
670+
671+
std::tuple<CopyArgument> args(1.5);
672+
stats.Fill(args);
673+
EXPECT_EQ(stats.GetNEntries(), 1);
674+
EXPECT_EQ(stats.GetDimensionStats(0).fSumWX, 1.5);
675+
676+
ASSERT_FALSE(CopyArgument::HasBeenCopied());
677+
678+
CopyArgument arg(2.5);
679+
stats.Fill(arg);
680+
EXPECT_EQ(stats.GetNEntries(), 2);
681+
EXPECT_EQ(stats.GetDimensionStats(0).fSumWX, 4);
682+
683+
ASSERT_FALSE(CopyArgument::HasBeenCopied());
684+
}
685+
667686
TEST(RHistStats, Scale)
668687
{
669688
RHistStats stats(3);

hist/histv7/test/hist_test.hxx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,29 @@ using ROOT::Experimental::Internal::RSliceBinIndexMapper;
4545
#include <thread>
4646
#include <vector>
4747

48+
class CopyArgument final {
49+
static int gCopies;
50+
51+
double fArgument;
52+
53+
public:
54+
CopyArgument(double argument) : fArgument(argument) {}
55+
CopyArgument(const CopyArgument &) { gCopies++; }
56+
CopyArgument(CopyArgument &&) = default;
57+
CopyArgument &operator=(const CopyArgument &)
58+
{
59+
gCopies++;
60+
return *this;
61+
}
62+
CopyArgument &operator=(CopyArgument &&) = default;
63+
64+
operator double() const { return fArgument; }
65+
66+
static bool HasBeenCopied() { return gCopies > 0; }
67+
};
68+
69+
int CopyArgument::gCopies = 0;
70+
4871
template <typename Work>
4972
void StressInParallel(std::size_t nThreads, Work &&w)
5073
{

0 commit comments

Comments
 (0)