Skip to content

Commit 96520e1

Browse files
author
Petr Jacka
committed
[RDF] Adding interfaces to HistoNSparseD
1 parent 6e0212d commit 96520e1

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

tree/dataframe/inc/ROOT/RDF/HistoModels.hxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class THnT;
2222
using THnD = THnT<double>;
2323
template <typename T>
2424
class THnSparseT;
25-
using THnSparseD = THnSparseT<double>;
25+
class TArrayD;
26+
using THnSparseD = THnSparseT<TArrayD>;
2627
class TProfile;
2728
class TProfile2D;
2829

tree/dataframe/inc/ROOT/RDF/InterfaceUtils.hxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ struct Histo1D{};
8989
struct Histo2D{};
9090
struct Histo3D{};
9191
struct HistoND{};
92+
struct HistoNSparseD{};
9293
struct Graph{};
9394
struct GraphAsymmErrors{};
9495
struct Profile1D{};
@@ -121,7 +122,7 @@ struct HistoUtils<T, false> {
121122
static bool HasAxisLimits(T &) { return true; }
122123
};
123124

124-
// Generic filling (covers Histo2D, HistoND, Profile1D and Profile2D actions, with and without weights)
125+
// Generic filling (covers Histo2D, HistoND, HistoNSparseD, Profile1D and Profile2D actions, with and without weights)
125126
template <typename... ColTypes, typename ActionTag, typename ActionResultType, typename PrevNodeType>
126127
std::unique_ptr<RActionBase>
127128
BuildAction(const ColumnNames_t &bl, const std::shared_ptr<ActionResultType> &h, const unsigned int nSlots,

tree/dataframe/inc/ROOT/RDF/RInterface.hxx

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "TH2.h" // For Histo actions
4141
#include "TH3.h" // For Histo actions
4242
#include "THn.h"
43+
#include "THnSparse.h"
4344
#include "TProfile.h"
4445
#include "TProfile2D.h"
4546
#include "TStatistic.h"
@@ -2286,6 +2287,82 @@ public:
22862287
columnList.size());
22872288
}
22882289

2290+
2291+
////////////////////////////////////////////////////////////////////////////
2292+
/// \brief Fill and return a sparse N-dimensional histogram (*lazy action*).
2293+
/// \tparam FirstColumn The first type of the column the values of which are used to fill the object. Inferred if not
2294+
/// present.
2295+
/// \tparam OtherColumns A list of the other types of the columns the values of which are used to fill the
2296+
/// object.
2297+
/// \param[in] model The returned histogram will be constructed using this as a model.
2298+
/// \param[in] columnList
2299+
/// A list containing the names of the columns that will be passed when calling `Fill`.
2300+
/// (N columns for unweighted filling, or N+1 columns for weighted filling)
2301+
/// \return the N-dimensional histogram wrapped in a RResultPtr.
2302+
///
2303+
/// This action is *lazy*: upon invocation of this method the calculation is
2304+
/// booked but not executed. See RResultPtr documentation.
2305+
///
2306+
/// ### Example usage:
2307+
/// ~~~{.cpp}
2308+
/// auto myFilledObj = myDf.HistoND<float, float, float, float>({"name","title", 4,
2309+
/// {40,40,40,40}, {20.,20.,20.,20.}, {60.,60.,60.,60.}},
2310+
/// {"col0", "col1", "col2", "col3"});
2311+
/// ~~~
2312+
///
2313+
template <typename FirstColumn, typename... OtherColumns> // need FirstColumn to disambiguate overloads
2314+
RResultPtr<::THnSparseD> HistoNSparseD(const THnSparseDModel &model, const ColumnNames_t &columnList)
2315+
{
2316+
std::shared_ptr<::THnSparseD> h(nullptr);
2317+
{
2318+
ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
2319+
h = model.GetHistogram();
2320+
2321+
if (int(columnList.size()) == (h->GetNdimensions() + 1)) {
2322+
h->Sumw2();
2323+
} else if (int(columnList.size()) != h->GetNdimensions()) {
2324+
throw std::runtime_error("Wrong number of columns for the specified number of histogram axes.");
2325+
}
2326+
}
2327+
return CreateAction<RDFInternal::ActionTags::HistoNSparseD, FirstColumn, OtherColumns...>(columnList, h, h,
2328+
fProxiedPtr);
2329+
}
2330+
2331+
////////////////////////////////////////////////////////////////////////////
2332+
/// \brief Fill and return a sparse N-dimensional histogram (*lazy action*).
2333+
/// \param[in] model The returned histogram will be constructed using this as a model.
2334+
/// \param[in] columnList A list containing the names of the columns that will be passed when calling `Fill`
2335+
/// (N columns for unweighted filling, or N+1 columns for weighted filling)
2336+
/// \return the N-dimensional histogram wrapped in a RResultPtr.
2337+
///
2338+
/// This action is *lazy*: upon invocation of this method the calculation is
2339+
/// booked but not executed. Also see RResultPtr.
2340+
///
2341+
/// ### Example usage:
2342+
/// ~~~{.cpp}
2343+
/// auto myFilledObj = myDf.HistoNSparseD({"name","title", 4,
2344+
/// {40,40,40,40}, {20.,20.,20.,20.}, {60.,60.,60.,60.}},
2345+
/// {"col0", "col1", "col2", "col3"});
2346+
/// ~~~
2347+
///
2348+
RResultPtr<::THnSparseD> HistoNSparseD(const THnSparseDModel &model, const ColumnNames_t &columnList)
2349+
{
2350+
std::shared_ptr<::THnSparseD> h(nullptr);
2351+
{
2352+
ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
2353+
h = model.GetHistogram();
2354+
2355+
if (int(columnList.size()) == (h->GetNdimensions() + 1)) {
2356+
h->Sumw2();
2357+
} else if (int(columnList.size()) != h->GetNdimensions()) {
2358+
throw std::runtime_error("Wrong number of columns for the specified number of histogram axes.");
2359+
}
2360+
}
2361+
return CreateAction<RDFInternal::ActionTags::HistoNSparseD, RDFDetail::RInferredType>(columnList, h, h, fProxiedPtr,
2362+
columnList.size());
2363+
}
2364+
2365+
22892366
////////////////////////////////////////////////////////////////////////////
22902367
/// \brief Fill and return a TGraph object (*lazy action*).
22912368
/// \tparam X The type of the column used to fill the x axis.

tree/dataframe/src/RDFHistoModels.cxx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "TH2.h"
2121
#include "TH3.h"
2222
#include "THn.h"
23+
#include "THnSparse.h"
2324

2425
/**
2526
* \class ROOT::RDF::TH1DModel
@@ -359,7 +360,12 @@ std::shared_ptr<::THnSparseD> THnSparseDModel::GetHistogram() const
359360
}
360361
std::shared_ptr<::THnSparseD> h;
361362
if (varbinning) {
362-
h = std::make_shared<::THnSparseD>(fName, fTitle, fDim, fNbins.data(), fBinEdges, fChunkSize);
363+
std::vector<TAxis> axes(fDim);
364+
for (int idim = 0; idim < fDim; ++idim) {
365+
axes[idim] = TAxis(fNbins[idim], fBinEdges[idim].data());
366+
}
367+
h = std::make_shared<::THnSparseD>(fName, fTitle, axes, fChunkSize);
368+
// h = std::make_shared<::THnSparseD>(fName, fTitle, fDim, fNbins.data(), fBinEdges, fChunkSize);
363369
} else {
364370
h = std::make_shared<::THnSparseD>(fName, fTitle, fDim, fNbins.data(), fXmin.data(), fXmax.data(), fChunkSize);
365371
}

0 commit comments

Comments
 (0)