18#ifndef ROOT_RDFOPERATIONS
19#define ROOT_RDFOPERATIONS
34#include "RConfigure.h"
81using Results = std::conditional_t<std::is_same<T, bool>::value, std::deque<T>, std::vector<T>>;
95 template <
typename... Args>
96 void Exec(
unsigned int slot, Args &&... args)
99 static_assert(std::is_same<TypeList<std::decay_t<Args>...>, ColumnTypes_t>
::value,
"");
107 std::string GetActionName() {
return "ForeachSlot"; }
127 return std::make_unique<RMergeableCount>(*
fResultCount);
132 std::string GetActionName() {
return "Count"; }
141template <
typename RNode_t>
143 std::shared_ptr<RCutFlowReport>
fReport;
153 ReportHelper(ReportHelper &&) =
default;
154 ReportHelper(
const ReportHelper &) =
delete;
156 void Exec(
unsigned int ) {}
170 std::string GetActionName() {
return "Report"; }
174 auto &&
result = *
static_cast<std::shared_ptr<RCutFlowReport> *
>(
newResult);
175 return ReportHelper{
result,
176 std::static_pointer_cast<RNode_t>(fNode->GetVariedFilter(std::string(
variation))).get(),
192 using Buf_t = std::vector<BufEl_t>;
194 std::vector<Buf_t> fBuffers;
197 unsigned int fNSlots;
198 unsigned int fBufSize;
212 void Exec(
unsigned int slot,
double v,
double w);
226 void Exec(
unsigned int slot,
const T &
vs,
const W &ws)
255 void Exec(
unsigned int slot,
const T
v,
const W &ws)
265 Hist_t &PartialUpdate(
unsigned int);
274 return std::make_unique<RMergeableFill<Hist_t>>(*fResultHist);
277 std::string GetActionName()
286 result->SetDirectory(
nullptr);
297 using iterator_category = std::forward_iterator_tag;
298 using difference_type = std::ptrdiff_t;
299 using value_type = T;
301 using reference = T &;
311 if constexpr (IsDataContainer<T>::value) {
312 return std::begin(val);
320std::size_t GetSize(
const T &val)
322 if constexpr (IsDataContainer<T>::value) {
323 return std::size(val);
330template <
typename Helper>
333 template <
typename... Iterators>
334 void ExecLoop(
unsigned int slot, std::size_t elements, Iterators...
its)
336 for (std::size_t i = 0; i < elements; i++) {
338 (std::advance(
its, 1), ...);
346 if constexpr (std::disjunction_v<IsDataContainer<ColumnTypes>...>) {
347 constexpr std::array<
bool,
sizeof...(ColumnTypes)>
isContainer{IsDataContainer<ColumnTypes>::value...};
349 std::array<std::size_t,
sizeof...(columnValues)>
sizes = {{GetSize(
columnValues)...}};
350 std::size_t elements = 0;
351 for (std::size_t i = 0; i <
isContainer.size(); i++) {
355 }
else if (elements !=
sizes[i]) {
356 throw std::runtime_error(
"Cannot fill values in containers of different sizes.");
382template <
typename HIST = Hist_t>
384 std::vector<HIST *> fObjects;
388 auto Merge(std::vector<H *> &
objs,
int )
398 template <
typename H>
399 auto Merge(std::vector<H *> &
objs,
double )
400 ->
decltype(
objs[0]->Merge(std::vector<HIST *>{}),
void())
406 template <
typename T>
409 static_assert(
sizeof(T) < 0,
410 "The type passed to Fill does not provide a Merge(TCollection*) or Merge(const std::vector&) method.");
413 template <std::size_t
ColIdx,
typename End_t,
typename...
Its>
427 fObjects[0] =
h.get();
429 for (
unsigned int i = 1; i <
nSlots; ++i) {
430 fObjects[i] =
new HIST(*fObjects[0]);
438 template <
typename...
ValTypes, std::enable_if_t<!std::disjunction<IsDataContainer<ValTypes>...>
::value,
int> = 0>
441 fObjects[
slot]->Fill(
x...);
445 template <
typename...
Xs, std::enable_if_t<std::disjunction<IsDataContainer<Xs>...>
::value,
int> = 0>
449 constexpr std::array<
bool,
sizeof...(Xs)>
isContainer{IsDataContainer<Xs>::value...};
454 static_assert(
colidx <
sizeof...(Xs),
"Error: index of collection-type argument not found.");
460 std::array<std::size_t,
sizeof...(xs)>
sizes = {{GetSize(
xs)...}};
462 for (std::size_t i = 0; i <
sizeof...(xs); ++i) {
464 throw std::runtime_error(
"Cannot fill histogram with values in containers of different sizes.");
471 template <
typename T = HIST>
474 static_assert(
sizeof(T) < 0,
475 "When filling an object with RDataFrame (e.g. via a Fill action) the number or types of the "
476 "columns passed did not match the signature of the object's `Fill` method.");
483 if (fObjects.size() == 1)
489 for (
auto it = ++fObjects.begin(); it != fObjects.end(); ++it)
493 HIST &PartialUpdate(
unsigned int slot) {
return *fObjects[
slot]; }
498 return std::make_unique<RMergeableFill<HIST>>(*fObjects[0]);
503 std::string GetActionName()
505 return std::string(fObjects[0]->IsA()->GetName()) +
"\\n" + std::string(fObjects[0]->GetName());
510 std::string GetActionName()
512 return "Fill custom object";
515 template <
typename H = HIST>
526template <
typename BinContentType,
bool WithWeight = false>
528 public ExecLoopTrait<RHistFillHelper<BinContentType, WithWeight>> {
533 std::unique_ptr<ROOT::Experimental::RHistConcurrentFiller<BinContentType>>
fFiller;
534 std::vector<std::shared_ptr<ROOT::Experimental::RHistFillContext<BinContentType>>> fContexts;
540 for (
unsigned int i = 0; i <
nSlots; i++) {
541 fContexts[i] =
fFiller->CreateFillContext();
550 std::shared_ptr<Result_t> GetResultPtr()
const {
return fFiller.GetHist(); }
555 template <
typename...
ColumnTypes,
const std::size_t... I>
562 fContexts[
slot]->Fill(args, weight);
578 for (
auto &&context : fContexts) {
590 std::string GetActionName() {
return "Hist"; }
593template <
typename BinContentType,
bool WithWeight = false>
595 :
public RActionImpl<RHistEngineFillHelper<BinContentType, WithWeight>>,
596 public ExecLoopTrait<RHistEngineFillHelper<BinContentType, WithWeight>> {
601 std::shared_ptr<Result_t> fHist;
611 std::shared_ptr<Result_t> GetResultPtr()
const {
return fHist; }
616 template <
typename...
ColumnTypes,
const std::size_t... I>
622 fHist->FillAtomic(args, weight);
645 std::string GetActionName() {
return "Hist"; }
654 std::vector<::TGraph *> fGraphs;
662 fGraphs[0] =
g.get();
664 for (
unsigned int i = 1; i <
nSlots; ++i) {
665 fGraphs[i] =
new TGraph(*fGraphs[0]);
673 template <
typename X0,
typename X1,
674 std::enable_if_t<IsDataContainer<X0>::value && IsDataContainer<X1>::value,
int> = 0>
677 if (
x0s.size() !=
x1s.size()) {
678 throw std::runtime_error(
"Cannot fill Graph with values in containers of different sizes.");
690 template <
typename X0,
typename X1,
691 std::enable_if_t<!IsDataContainer<X0>::value && !IsDataContainer<X1>::value,
int> = 0>
703 throw std::runtime_error(
"Graph was applied to a mix of scalar values and collections. This is not supported.");
708 const auto nSlots = fGraphs.size();
713 l.Add(fGraphs[
slot]);
721 return std::make_unique<RMergeableFill<Result_t>>(*fGraphs[0]);
724 std::string GetActionName() {
return "Graph"; }
726 Result_t &PartialUpdate(
unsigned int slot) {
return *fGraphs[
slot]; }
753 for (
unsigned int i = 1; i <
nSlots; ++i) {
763 typename X,
typename Y,
typename EXL,
typename EXH,
typename EYL,
typename EYH,
764 std::enable_if_t<IsDataContainer<X>::value && IsDataContainer<Y>::value && IsDataContainer<EXL>::value &&
765 IsDataContainer<EXH>::value && IsDataContainer<EYL>::value && IsDataContainer<EYH>::value,
770 if ((
xs.size() !=
ys.size()) || (
xs.size() !=
exls.size()) || (
xs.size() !=
exhs.size()) ||
771 (
xs.size() !=
eyls.size()) || (
xs.size() !=
eyhs.size())) {
772 throw std::runtime_error(
"Cannot fill GraphAsymmErrors with values in containers of different sizes.");
775 auto xsIt = std::begin(
xs);
776 auto ysIt = std::begin(
ys);
781 while (
xsIt != std::end(
xs)) {
790 typename X,
typename Y,
typename EXL,
typename EXH,
typename EYL,
typename EYH,
791 std::enable_if_t<!IsDataContainer<X>::value && !IsDataContainer<Y>::value && !IsDataContainer<EXL>::value &&
792 !IsDataContainer<EXH>::value && !IsDataContainer<EYL>::value && !IsDataContainer<EYH>::value,
804 template <
typename X,
typename Y,
typename EXL,
typename EXH,
typename EYL,
typename EYH,
808 throw std::runtime_error(
809 "GraphAsymmErrors was applied to a mix of scalar values and collections. This is not supported.");
830 std::string GetActionName() {
return "GraphAsymmErrors"; }
836 auto &
result = *
static_cast<std::shared_ptr<TGraphAsymmErrors> *
>(
newResult);
843template <
typename HIST>
845 std::vector<std::shared_ptr<HIST>> fObjects;
846 std::vector<std::unique_ptr<std::mutex>>
fMutexPtrs;
849 template <
typename T,
typename... Args>
856 template <
typename T,
typename... Args>
859 std::scoped_lock lock{mutex};
860 object.Fill(args...);
863 template <std::size_t
ColIdx,
typename End_t,
typename...
Its>
879 fObjects.front() =
h;
881 std::generate(fObjects.begin() + 1, fObjects.end(), [
h]() {
882 auto hist = std::make_shared<HIST>(*h);
883 UnsetDirectoryIfPossible(hist.get());
893 template <
typename...
ValTypes, std::enable_if_t<!std::disjunction<IsDataContainer<ValTypes>...>
::value,
int> = 0>
901 template <
typename...
Xs, std::enable_if_t<std::disjunction<IsDataContainer<Xs>...>
::value,
int> = 0>
905 constexpr std::array<
bool,
sizeof...(Xs)>
isContainer{IsDataContainer<Xs>::value...};
910 static_assert(
colidx <
sizeof...(Xs),
"Error: index of collection-type argument not found.");
916 std::array<std::size_t,
sizeof...(xs)>
sizes = {{GetSize(
xs)...}};
918 for (std::size_t i = 0; i <
sizeof...(xs); ++i) {
920 throw std::runtime_error(
"Cannot fill histogram with values in containers of different sizes.");
927 template <
typename T = HIST>
930 static_assert(
sizeof(T) < 0,
931 "When filling an object with RDataFrame (e.g. via a Fill action) the number or types of the "
932 "columns passed did not match the signature of the object's `FillThreadSafe` method.");
939 if (fObjects.size() > 1) {
941 for (
auto it = fObjects.cbegin() + 1; it != fObjects.end(); ++it) {
944 fObjects[0]->Merge(&list);
954 return std::make_unique<RMergeableFill<HIST>>(*fObjects[0]);
959 std::string GetActionName()
961 return std::string(fObjects[0]->IsA()->GetName()) +
"\\n" + std::string(fObjects[0]->GetName());
964 template <
typename H = HIST>
980template <
typename V,
typename COLL>
986template <
typename COLL>
993template <
typename RealT_t,
typename T,
typename COLL>
1002 for (
unsigned int i = 1; i <
nSlots; ++i)
1003 fColls.emplace_back(std::make_shared<COLL>());
1017 for (
unsigned int i = 1; i <
fColls.size(); ++i) {
1030 std::string GetActionName() {
return "Take"; }
1042template <
typename RealT_t,
typename T>
1044 :
public RActionImpl<TakeHelper<RealT_t, T, std::vector<T>>> {
1052 for (
unsigned int i = 1; i <
nSlots; ++i) {
1053 auto v = std::make_shared<std::vector<T>>();
1075 for (
unsigned int i = 1; i <
fColls.size(); ++i) {
1081 std::vector<T> &PartialUpdate(
unsigned int slot) {
return *
fColls[
slot]; }
1083 std::string GetActionName() {
return "Take"; }
1087 auto &
result = *
static_cast<std::shared_ptr<std::vector<T>
> *>(
newResult);
1095template <
typename RealT_t,
typename COLL>
1097 :
public RActionImpl<TakeHelper<RealT_t, RVec<RealT_t>, COLL>> {
1105 for (
unsigned int i = 1; i <
nSlots; ++i)
1106 fColls.emplace_back(std::make_shared<COLL>());
1120 for (
unsigned int i = 1; i <
fColls.size(); ++i) {
1122 for (
auto &
v : *
coll) {
1128 std::string GetActionName() {
return "Take"; }
1140template <
typename RealT_t>
1142 :
public RActionImpl<TakeHelper<RealT_t, RVec<RealT_t>, std::vector<RealT_t>>> {
1151 for (
unsigned int i = 1; i <
nSlots; ++i) {
1152 auto v = std::make_shared<std::vector<RealT_t>>();
1174 for (
unsigned int i = 1; i <
fColls.size(); ++i) {
1180 std::string GetActionName() {
return "Take"; }
1193template <
typename RealT_t,
typename T,
typename COLL>
1195template <
typename RealT_t,
typename T>
1197template <
typename RealT_t,
typename COLL>
1199template <
typename RealT_t>
1215template <
typename ResultType>
1242 *
fResultMin = std::numeric_limits<ResultType>::max();
1250 return std::make_unique<RMergeableMin<ResultType>>(*fResultMin);
1255 std::string GetActionName() {
return "Min"; }
1259 auto &
result = *
static_cast<std::shared_ptr<ResultType> *
>(
newResult);
1264template <
typename ResultType>
1291 *
fResultMax = std::numeric_limits<ResultType>::lowest();
1300 return std::make_unique<RMergeableMax<ResultType>>(*fResultMax);
1305 std::string GetActionName() {
return "Max"; }
1309 auto &
result = *
static_cast<std::shared_ptr<ResultType> *
>(
newResult);
1314template <
typename ResultType>
1323 template <
typename T = ResultType>
1329 template <
typename T = ResultType,
typename Dummy =
int>
1357 for (
auto &&
v :
vs) {
1383 return std::make_unique<RMergeableSum<ResultType>>(*fResultSum);
1388 std::string GetActionName() {
return "Sum"; }
1392 auto &
result = *
static_cast<std::shared_ptr<ResultType> *
>(
newResult);
1400 std::vector<ULong64_t> fCounts;
1401 std::vector<double>
fSums;
1415 for (
auto &&
v :
vs) {
1437 double &PartialUpdate(
unsigned int slot);
1439 std::string GetActionName() {
return "Mean"; }
1450 unsigned int fNSlots;
1453 std::vector<ULong64_t> fCounts;
1455 std::vector<double> fMeans;
1469 for (
auto &&
v :
vs) {
1483 std::inner_product(fMeans.begin(), fMeans.end(), fCounts.begin(), 0.) /
static_cast<Double_t>(
counts);
1487 std::string GetActionName() {
return "StdDev"; }
1496template <
typename PrevNodeType>
1501 std::shared_ptr<PrevNodeType> fPrevNode;
1502 size_t fEntriesToProcess;
1505 DisplayHelper(
size_t nRows,
const std::shared_ptr<Display_t> &
d,
const std::shared_ptr<PrevNodeType> &prevNode)
1509 DisplayHelper(DisplayHelper &&) =
default;
1510 DisplayHelper(
const DisplayHelper &) =
delete;
1513 template <
typename...
Columns>
1516 if (fEntriesToProcess == 0)
1520 --fEntriesToProcess;
1522 if (fEntriesToProcess == 0) {
1527 fPrevNode->StopProcessing();
1535 std::string GetActionName() {
return "Display"; }
1538template <
typename Acc,
typename Merge,
typename R,
typename T,
typename U,
1541 :
public RActionImpl<AggregateHelper<Acc, Merge, R, T, U, MustCopyAssign>> {
1544 std::shared_ptr<U> fResult;
1565 template <
bool MustCopyAssign_ = MustCopyAssign, std::enable_if_t<MustCopyAssign_,
int> = 0>
1571 template <
bool MustCopyAssign_ = MustCopyAssign, std::enable_if_t<!MustCopyAssign_,
int> = 0>
1580 bool MergeAll = std::is_same<void, MergeRet>::value>
1581 std::enable_if_t<MergeAll, void> Finalize()
1589 std::enable_if_t<MergeTwoByTwo, void> Finalize(...)
1592 *fResult = fMerge(*fResult,
acc);
1597 std::string GetActionName() {
return "Aggregate"; }
Handle_t Display_t
Display handle.
#define R(a, b, c, d, e, f, g, h, i)
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
Basic types used by ROOT and required by TInterpreter.
double Double_t
Double 8 bytes.
unsigned long long ULong64_t
Portable unsigned long integer 8 bytes.
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char x1
TTime operator*(const TTime &t1, const TTime &t2)
Base class for action helpers, see RInterface::Book() for more information.
A histogram data structure to bin data along multiple dimensions.
A histogram for aggregation of data along multiple dimensions.
This class is the textual representation of the content of a columnar dataset.
const_iterator begin() const
const_iterator end() const
A "std::vector"-like collection of values implementing handy operation to analyse them.
Collection abstract base class.
TGraph with asymmetric error bars.
A TGraph is an object made of two arrays X and Y with npoints each.
1-D histogram with a double per channel (see TH1 documentation)
TH1 is the base class of all histogram classes in ROOT.
void Add(TObject *obj) override
Statistical variable, defined by its mean and variance (RMS).
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
RooCmdArg Columns(Int_t ncol)
CPYCPPYY_EXTERN bool Exec(const std::string &cmd)
std::unique_ptr< RMergeableVariations< T > > GetMergeableValue(ROOT::RDF::Experimental::RResultMap< T > &rmap)
Retrieve mergeable values after calling ROOT::RDF::VariationsFor .
void ResetIfPossible(TStatistic *h)
constexpr std::size_t FindIdxTrue(const T &arr)
void UnsetDirectoryIfPossible(TH1 *h)
auto FillThreadSafe(T &histo, Args... args) -> decltype(histo.FillThreadSafe(args...), void())
Entrypoint for thread-safe filling from RDataFrame.
ROOT type_traits extensions.
void Initialize(Bool_t useTMVAStyle=kTRUE)
A weight for filling histograms.
static uint64_t sum(uint64_t i)