Skip to content

Commit 9823478

Browse files
committed
[hist] More constructors for RHistEngine and RHist
They simplify creation of multi-dimensional histograms in user code.
1 parent ef9e91f commit 9823478

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

hist/histv7/inc/ROOT/RHist.hxx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,35 @@ public:
7575
/// \param[in] axes the axis objects, must have size > 0
7676
explicit RHist(std::vector<RAxisVariant> axes) : fEngine(std::move(axes)), fStats(fEngine.GetNDimensions()) {}
7777

78-
/// Construct a one-dimensional histogram engine with a regular axis.
78+
/// Construct a histogram.
79+
///
80+
/// Note that there is no perfect forwarding of the axis objects. If that is needed, use the
81+
/// \ref RHist(std::vector<RAxisVariant> axes) "overload accepting a std::vector".
82+
///
83+
/// \param[in] axes the axis objects, must have size > 0
84+
explicit RHist(std::initializer_list<RAxisVariant> axes) : RHist(std::vector(axes)) {}
85+
86+
/// Construct a histogram.
87+
///
88+
/// Note that there is no perfect forwarding of the axis objects. If that is needed, use the
89+
/// \ref RHist(std::vector<RAxisVariant> axes) "overload accepting a std::vector".
90+
///
91+
/// \param[in] axis1 the first axis object
92+
/// \param[in] axes the remaining axis objects
93+
template <typename... Axes>
94+
explicit RHist(const RAxisVariant &axis1, const Axes &...axes) : RHist(std::vector<RAxisVariant>{axis1, axes...})
95+
{
96+
}
97+
98+
/// Construct a one-dimensional histogram with a regular axis.
7999
///
80100
/// \param[in] nNormalBins the number of normal bins, must be > 0
81101
/// \param[in] interval the axis interval (lower end inclusive, upper end exclusive)
82102
/// \par See also
83103
/// the \ref RRegularAxis::RRegularAxis(std::uint64_t nNormalBins, std::pair<double, double> interval, bool
84104
/// enableFlowBins) "constructor of RRegularAxis"
85-
RHist(std::uint64_t nNormalBins, std::pair<double, double> interval) : RHist({RRegularAxis(nNormalBins, interval)})
105+
RHist(std::uint64_t nNormalBins, std::pair<double, double> interval)
106+
: RHist(std::vector<RAxisVariant>{RRegularAxis(nNormalBins, interval)})
86107
{
87108
}
88109

hist/histv7/inc/ROOT/RHistEngine.hxx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,27 @@ public:
8484
fBinContents.resize(fAxes.ComputeTotalNBins());
8585
}
8686

87+
/// Construct a histogram engine.
88+
///
89+
/// Note that there is no perfect forwarding of the axis objects. If that is needed, use the
90+
/// \ref RHistEngine(std::vector<RAxisVariant> axes) "overload accepting a std::vector".
91+
///
92+
/// \param[in] axes the axis objects, must have size > 0
93+
explicit RHistEngine(std::initializer_list<RAxisVariant> axes) : RHistEngine(std::vector(axes)) {}
94+
95+
/// Construct a histogram engine.
96+
///
97+
/// Note that there is no perfect forwarding of the axis objects. If that is needed, use the
98+
/// \ref RHistEngine(std::vector<RAxisVariant> axes) "overload accepting a std::vector".
99+
///
100+
/// \param[in] axis1 the first axis object
101+
/// \param[in] axes the remaining axis objects
102+
template <typename... Axes>
103+
explicit RHistEngine(const RAxisVariant &axis1, const Axes &...axes)
104+
: RHistEngine(std::vector<RAxisVariant>{axis1, axes...})
105+
{
106+
}
107+
87108
/// Construct a one-dimensional histogram engine with a regular axis.
88109
///
89110
/// \param[in] nNormalBins the number of normal bins, must be > 0
@@ -92,7 +113,7 @@ public:
92113
/// the \ref RRegularAxis::RRegularAxis(std::uint64_t nNormalBins, std::pair<double, double> interval, bool
93114
/// enableFlowBins) "constructor of RRegularAxis"
94115
RHistEngine(std::uint64_t nNormalBins, std::pair<double, double> interval)
95-
: RHistEngine({RRegularAxis(nNormalBins, interval)})
116+
: RHistEngine(std::vector<RAxisVariant>{RRegularAxis(nNormalBins, interval)})
96117
{
97118
}
98119

hist/histv7/test/hist_engine.cxx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ TEST(RHistEngine, Constructor)
5959
EXPECT_EQ(engine.GetNDimensions(), 1);
6060
engine = RHistEngine<int>({variableBinAxis, categoricalAxis});
6161
EXPECT_EQ(engine.GetNDimensions(), 2);
62+
63+
// Templated constructors
64+
engine = RHistEngine<int>(variableBinAxis);
65+
EXPECT_EQ(engine.GetNDimensions(), 1);
66+
engine = RHistEngine<int>(variableBinAxis, categoricalAxis);
67+
EXPECT_EQ(engine.GetNDimensions(), 2);
68+
engine = RHistEngine<int>(variableBinAxis, categoricalAxis, regularAxis);
69+
EXPECT_EQ(engine.GetNDimensions(), 3);
6270
}
6371

6472
TEST(RHistEngine, GetBinContentInvalidNumberOfArguments)

hist/histv7/test/hist_hist.cxx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ TEST(RHist, Constructor)
4545
EXPECT_EQ(hist.GetNDimensions(), 1);
4646
hist = RHist<int>({regularAxis, regularAxis});
4747
EXPECT_EQ(hist.GetNDimensions(), 2);
48+
49+
// Templated constructors
50+
hist = RHist<int>(regularAxis);
51+
EXPECT_EQ(hist.GetNDimensions(), 1);
52+
hist = RHist<int>(regularAxis, regularAxis);
53+
EXPECT_EQ(hist.GetNDimensions(), 2);
54+
hist = RHist<int>(regularAxis, regularAxis, regularAxis);
55+
EXPECT_EQ(hist.GetNDimensions(), 3);
4856
}
4957

5058
TEST(RHist, Add)

0 commit comments

Comments
 (0)