19#ifndef ROOT_RDFOPERATIONS
20#define ROOT_RDFOPERATIONS
35#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);
344template <
typename HIST = Hist_t>
346 std::vector<HIST *> fObjects;
350 auto Merge(std::vector<H *> &
objs,
int )
360 template <
typename H>
361 auto Merge(std::vector<H *> &
objs,
double )
362 ->
decltype(
objs[0]->Merge(std::vector<HIST *>{}),
void())
368 template <
typename T>
371 static_assert(
sizeof(T) < 0,
372 "The type passed to Fill does not provide a Merge(TCollection*) or Merge(const std::vector&) method.");
375 template <std::size_t
ColIdx,
typename End_t,
typename...
Its>
389 fObjects[0] =
h.get();
391 for (
unsigned int i = 1; i <
nSlots; ++i) {
392 fObjects[i] =
new HIST(*fObjects[0]);
400 template <
typename...
ValTypes, std::enable_if_t<!Disjunction<IsDataContainer<ValTypes>...>
::value,
int> = 0>
403 fObjects[
slot]->Fill(
x...);
407 template <
typename...
Xs, std::enable_if_t<Disjunction<IsDataContainer<Xs>...>
::value,
int> = 0>
411 constexpr std::array<
bool,
sizeof...(Xs)>
isContainer{IsDataContainer<Xs>::value...};
416 static_assert(
colidx <
sizeof...(Xs),
"Error: index of collection-type argument not found.");
422 std::array<std::size_t,
sizeof...(xs)>
sizes = {{GetSize(
xs)...}};
424 for (std::size_t i = 0; i <
sizeof...(xs); ++i) {
426 throw std::runtime_error(
"Cannot fill histogram with values in containers of different sizes.");
433 template <
typename T = HIST>
436 static_assert(
sizeof(T) < 0,
437 "When filling an object with RDataFrame (e.g. via a Fill action) the number or types of the "
438 "columns passed did not match the signature of the object's `Fill` method.");
445 if (fObjects.size() == 1)
451 for (
auto it = ++fObjects.begin(); it != fObjects.end(); ++it)
455 HIST &PartialUpdate(
unsigned int slot) {
return *fObjects[
slot]; }
460 return std::make_unique<RMergeableFill<HIST>>(*fObjects[0]);
465 std::string GetActionName()
467 return std::string(fObjects[0]->
IsA()->GetName()) +
"\\n" + std::string(fObjects[0]->GetName());
472 std::string GetActionName()
474 return "Fill custom object";
477 template <
typename H = HIST>
488template <
typename BinContentType,
bool WithWeight = false>
495 std::unique_ptr<ROOT::Experimental::RHistConcurrentFiller<BinContentType>>
fFiller;
496 std::vector<std::shared_ptr<ROOT::Experimental::RHistFillContext<BinContentType>>> fContexts;
502 for (
unsigned int i = 0; i <
nSlots; i++) {
503 fContexts[i] =
fFiller->CreateFillContext();
512 std::shared_ptr<Result_t> GetResultPtr()
const {
return fFiller.GetHist(); }
517 template <
typename...
ColumnTypes,
const std::size_t...
I>
524 fContexts[
slot]->Fill(args, weight);
540 for (
auto &&context : fContexts) {
545 std::string GetActionName() {
return "Hist"; }
548template <
typename BinContentType,
bool WithWeight = false>
555 std::shared_ptr<Result_t> fHist;
565 std::shared_ptr<Result_t> GetResultPtr()
const {
return fHist; }
570 template <
typename...
ColumnTypes,
const std::size_t...
I>
576 fHist->FillAtomic(args, weight);
592 std::string GetActionName() {
return "Hist"; }
601 std::vector<::TGraph *> fGraphs;
609 fGraphs[0] =
g.get();
611 for (
unsigned int i = 1; i <
nSlots; ++i) {
612 fGraphs[i] =
new TGraph(*fGraphs[0]);
620 template <
typename X0,
typename X1,
621 std::enable_if_t<IsDataContainer<X0>::value && IsDataContainer<X1>::value,
int> = 0>
624 if (
x0s.size() !=
x1s.size()) {
625 throw std::runtime_error(
"Cannot fill Graph with values in containers of different sizes.");
637 template <
typename X0,
typename X1,
638 std::enable_if_t<!IsDataContainer<X0>::value && !IsDataContainer<X1>::value,
int> = 0>
650 throw std::runtime_error(
"Graph was applied to a mix of scalar values and collections. This is not supported.");
655 const auto nSlots = fGraphs.size();
660 l.Add(fGraphs[
slot]);
668 return std::make_unique<RMergeableFill<Result_t>>(*fGraphs[0]);
671 std::string GetActionName() {
return "Graph"; }
673 Result_t &PartialUpdate(
unsigned int slot) {
return *fGraphs[
slot]; }
700 for (
unsigned int i = 1; i <
nSlots; ++i) {
710 typename X,
typename Y,
typename EXL,
typename EXH,
typename EYL,
typename EYH,
711 std::enable_if_t<IsDataContainer<X>::value && IsDataContainer<Y>::value && IsDataContainer<EXL>::value &&
712 IsDataContainer<EXH>::value && IsDataContainer<EYL>::value && IsDataContainer<EYH>::value,
717 if ((
xs.size() !=
ys.size()) || (
xs.size() !=
exls.size()) || (
xs.size() !=
exhs.size()) ||
718 (
xs.size() !=
eyls.size()) || (
xs.size() !=
eyhs.size())) {
719 throw std::runtime_error(
"Cannot fill GraphAsymmErrors with values in containers of different sizes.");
722 auto xsIt = std::begin(
xs);
723 auto ysIt = std::begin(
ys);
728 while (
xsIt != std::end(
xs)) {
737 typename X,
typename Y,
typename EXL,
typename EXH,
typename EYL,
typename EYH,
738 std::enable_if_t<!IsDataContainer<X>::value && !IsDataContainer<Y>::value && !IsDataContainer<EXL>::value &&
739 !IsDataContainer<EXH>::value && !IsDataContainer<EYL>::value && !IsDataContainer<EYH>::value,
751 template <
typename X,
typename Y,
typename EXL,
typename EXH,
typename EYL,
typename EYH,
755 throw std::runtime_error(
756 "GraphAsymmErrors was applied to a mix of scalar values and collections. This is not supported.");
777 std::string GetActionName() {
return "GraphAsymmErrors"; }
783 auto &
result = *
static_cast<std::shared_ptr<TGraphAsymmErrors> *
>(
newResult);
790template <
typename HIST>
792 std::vector<std::shared_ptr<HIST>> fObjects;
793 std::vector<std::unique_ptr<std::mutex>>
fMutexPtrs;
796 template <
typename T,
typename... Args>
803 template <
typename T,
typename... Args>
806 std::scoped_lock lock{mutex};
807 object.Fill(args...);
810 template <std::size_t
ColIdx,
typename End_t,
typename...
Its>
826 fObjects.front() =
h;
828 std::generate(fObjects.begin() + 1, fObjects.end(), [
h]() {
829 auto hist = std::make_shared<HIST>(*h);
830 UnsetDirectoryIfPossible(hist.get());
840 template <
typename...
ValTypes, std::enable_if_t<!Disjunction<IsDataContainer<ValTypes>...>
::value,
int> = 0>
848 template <
typename...
Xs, std::enable_if_t<Disjunction<IsDataContainer<Xs>...>
::value,
int> = 0>
852 constexpr std::array<
bool,
sizeof...(Xs)>
isContainer{IsDataContainer<Xs>::value...};
857 static_assert(
colidx <
sizeof...(Xs),
"Error: index of collection-type argument not found.");
863 std::array<std::size_t,
sizeof...(xs)>
sizes = {{GetSize(
xs)...}};
865 for (std::size_t i = 0; i <
sizeof...(xs); ++i) {
867 throw std::runtime_error(
"Cannot fill histogram with values in containers of different sizes.");
874 template <
typename T = HIST>
877 static_assert(
sizeof(T) < 0,
878 "When filling an object with RDataFrame (e.g. via a Fill action) the number or types of the "
879 "columns passed did not match the signature of the object's `FillThreadSafe` method.");
886 if (fObjects.size() > 1) {
888 for (
auto it = fObjects.cbegin() + 1; it != fObjects.end(); ++it) {
891 fObjects[0]->Merge(&list);
901 return std::make_unique<RMergeableFill<HIST>>(*fObjects[0]);
906 std::string GetActionName()
908 return std::string(fObjects[0]->
IsA()->GetName()) +
"\\n" + std::string(fObjects[0]->GetName());
911 template <
typename H = HIST>
927template <
typename V,
typename COLL>
933template <
typename COLL>
940template <
typename RealT_t,
typename T,
typename COLL>
949 for (
unsigned int i = 1; i <
nSlots; ++i)
950 fColls.emplace_back(std::make_shared<COLL>());
964 for (
unsigned int i = 1; i <
fColls.size(); ++i) {
977 std::string GetActionName() {
return "Take"; }
989template <
typename RealT_t,
typename T>
991 :
public RActionImpl<TakeHelper<RealT_t, T, std::vector<T>>> {
999 for (
unsigned int i = 1; i <
nSlots; ++i) {
1000 auto v = std::make_shared<std::vector<T>>();
1022 for (
unsigned int i = 1; i <
fColls.size(); ++i) {
1028 std::vector<T> &PartialUpdate(
unsigned int slot) {
return *
fColls[
slot]; }
1030 std::string GetActionName() {
return "Take"; }
1034 auto &
result = *
static_cast<std::shared_ptr<std::vector<T>
> *>(
newResult);
1042template <
typename RealT_t,
typename COLL>
1044 :
public RActionImpl<TakeHelper<RealT_t, RVec<RealT_t>, COLL>> {
1052 for (
unsigned int i = 1; i <
nSlots; ++i)
1053 fColls.emplace_back(std::make_shared<COLL>());
1067 for (
unsigned int i = 1; i <
fColls.size(); ++i) {
1069 for (
auto &
v : *
coll) {
1075 std::string GetActionName() {
return "Take"; }
1087template <
typename RealT_t>
1089 :
public RActionImpl<TakeHelper<RealT_t, RVec<RealT_t>, std::vector<RealT_t>>> {
1098 for (
unsigned int i = 1; i <
nSlots; ++i) {
1099 auto v = std::make_shared<std::vector<RealT_t>>();
1121 for (
unsigned int i = 1; i <
fColls.size(); ++i) {
1127 std::string GetActionName() {
return "Take"; }
1140template <
typename RealT_t,
typename T,
typename COLL>
1142template <
typename RealT_t,
typename T>
1144template <
typename RealT_t,
typename COLL>
1146template <
typename RealT_t>
1162template <
typename ResultType>
1189 *
fResultMin = std::numeric_limits<ResultType>::max();
1197 return std::make_unique<RMergeableMin<ResultType>>(*fResultMin);
1202 std::string GetActionName() {
return "Min"; }
1206 auto &
result = *
static_cast<std::shared_ptr<ResultType> *
>(
newResult);
1211template <
typename ResultType>
1238 *
fResultMax = std::numeric_limits<ResultType>::lowest();
1247 return std::make_unique<RMergeableMax<ResultType>>(*fResultMax);
1252 std::string GetActionName() {
return "Max"; }
1256 auto &
result = *
static_cast<std::shared_ptr<ResultType> *
>(
newResult);
1261template <
typename ResultType>
1270 template <
typename T = ResultType>
1276 template <
typename T = ResultType,
typename Dummy =
int>
1304 for (
auto &&
v :
vs) {
1330 return std::make_unique<RMergeableSum<ResultType>>(*fResultSum);
1335 std::string GetActionName() {
return "Sum"; }
1339 auto &
result = *
static_cast<std::shared_ptr<ResultType> *
>(
newResult);
1347 std::vector<ULong64_t> fCounts;
1348 std::vector<double>
fSums;
1362 for (
auto &&
v :
vs) {
1384 double &PartialUpdate(
unsigned int slot);
1386 std::string GetActionName() {
return "Mean"; }
1397 unsigned int fNSlots;
1400 std::vector<ULong64_t> fCounts;
1402 std::vector<double> fMeans;
1416 for (
auto &&
v :
vs) {
1430 std::inner_product(fMeans.begin(), fMeans.end(), fCounts.begin(), 0.) /
static_cast<Double_t>(
counts);
1434 std::string GetActionName() {
return "StdDev"; }
1443template <
typename PrevNodeType>
1448 std::shared_ptr<PrevNodeType> fPrevNode;
1449 size_t fEntriesToProcess;
1452 DisplayHelper(
size_t nRows,
const std::shared_ptr<Display_t> &
d,
const std::shared_ptr<PrevNodeType> &prevNode)
1456 DisplayHelper(DisplayHelper &&) =
default;
1457 DisplayHelper(
const DisplayHelper &) =
delete;
1460 template <
typename...
Columns>
1463 if (fEntriesToProcess == 0)
1467 --fEntriesToProcess;
1469 if (fEntriesToProcess == 0) {
1474 fPrevNode->StopProcessing();
1482 std::string GetActionName() {
return "Display"; }
1485template <
typename Acc,
typename Merge,
typename R,
typename T,
typename U,
1488 :
public RActionImpl<AggregateHelper<Acc, Merge, R, T, U, MustCopyAssign>> {
1491 std::shared_ptr<U> fResult;
1512 template <
bool MustCopyAssign_ = MustCopyAssign, std::enable_if_t<MustCopyAssign_,
int> = 0>
1518 template <
bool MustCopyAssign_ = MustCopyAssign, std::enable_if_t<!MustCopyAssign_,
int> = 0>
1527 bool MergeAll = std::is_same<void, MergeRet>::value>
1528 std::enable_if_t<MergeAll, void> Finalize()
1536 std::enable_if_t<MergeTwoByTwo, void> Finalize(...)
1539 *fResult = fMerge(*fResult,
acc);
1544 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
Binding & operator=(OUT(*fun)(void))
TClass * IsA() const override
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)