18#ifndef ROOT_RDFOPERATIONS
19#define ROOT_RDFOPERATIONS
34#include "RConfigure.h"
80using Results = std::conditional_t<std::is_same<T, bool>::value, std::deque<T>, std::vector<T>>;
94 template <
typename... Args>
95 void Exec(
unsigned int slot, Args &&... args)
98 static_assert(std::is_same<TypeList<std::decay_t<Args>...>, ColumnTypes_t>
::value,
"");
106 std::string GetActionName() {
return "ForeachSlot"; }
126 return std::make_unique<RMergeableCount>(*
fResultCount);
131 std::string GetActionName() {
return "Count"; }
140template <
typename RNode_t>
142 std::shared_ptr<RCutFlowReport>
fReport;
152 ReportHelper(ReportHelper &&) =
default;
153 ReportHelper(
const ReportHelper &) =
delete;
155 void Exec(
unsigned int ) {}
169 std::string GetActionName() {
return "Report"; }
173 auto &&
result = *
static_cast<std::shared_ptr<RCutFlowReport> *
>(
newResult);
174 return ReportHelper{
result,
175 std::static_pointer_cast<RNode_t>(fNode->GetVariedFilter(std::string(
variation))).get(),
191 using Buf_t = std::vector<BufEl_t>;
193 std::vector<Buf_t> fBuffers;
196 unsigned int fNSlots;
197 unsigned int fBufSize;
211 void Exec(
unsigned int slot,
double v,
double w);
225 void Exec(
unsigned int slot,
const T &
vs,
const W &ws)
254 void Exec(
unsigned int slot,
const T
v,
const W &ws)
264 Hist_t &PartialUpdate(
unsigned int);
273 return std::make_unique<RMergeableFill<Hist_t>>(*fResultHist);
276 std::string GetActionName()
285 result->SetDirectory(
nullptr);
296 using iterator_category = std::forward_iterator_tag;
297 using difference_type = std::ptrdiff_t;
298 using value_type = T;
300 using reference = T &;
310 if constexpr (IsDataContainer<T>::value) {
311 return std::begin(val);
319std::size_t GetSize(
const T &val)
321 if constexpr (IsDataContainer<T>::value) {
322 return std::size(val);
343template <
typename HIST = Hist_t>
345 std::vector<HIST *> fObjects;
349 auto Merge(std::vector<H *> &
objs,
int )
359 template <
typename H>
360 auto Merge(std::vector<H *> &
objs,
double )
361 ->
decltype(
objs[0]->Merge(std::vector<HIST *>{}),
void())
367 template <
typename T>
370 static_assert(
sizeof(T) < 0,
371 "The type passed to Fill does not provide a Merge(TCollection*) or Merge(const std::vector&) method.");
374 template <std::size_t
ColIdx,
typename End_t,
typename...
Its>
388 fObjects[0] =
h.get();
390 for (
unsigned int i = 1; i <
nSlots; ++i) {
391 fObjects[i] =
new HIST(*fObjects[0]);
399 template <
typename...
ValTypes, std::enable_if_t<!Disjunction<IsDataContainer<ValTypes>...>
::value,
int> = 0>
402 fObjects[
slot]->Fill(
x...);
406 template <
typename...
Xs, std::enable_if_t<Disjunction<IsDataContainer<Xs>...>
::value,
int> = 0>
410 constexpr std::array<
bool,
sizeof...(Xs)>
isContainer{IsDataContainer<Xs>::value...};
415 static_assert(
colidx <
sizeof...(Xs),
"Error: index of collection-type argument not found.");
421 std::array<std::size_t,
sizeof...(xs)>
sizes = {{GetSize(
xs)...}};
423 for (std::size_t i = 0; i <
sizeof...(xs); ++i) {
425 throw std::runtime_error(
"Cannot fill histogram with values in containers of different sizes.");
432 template <
typename T = HIST>
435 static_assert(
sizeof(T) < 0,
436 "When filling an object with RDataFrame (e.g. via a Fill action) the number or types of the "
437 "columns passed did not match the signature of the object's `Fill` method.");
444 if (fObjects.size() == 1)
450 for (
auto it = ++fObjects.begin(); it != fObjects.end(); ++it)
454 HIST &PartialUpdate(
unsigned int slot) {
return *fObjects[
slot]; }
459 return std::make_unique<RMergeableFill<HIST>>(*fObjects[0]);
464 std::string GetActionName()
466 return std::string(fObjects[0]->IsA()->GetName()) +
"\\n" + std::string(fObjects[0]->GetName());
471 std::string GetActionName()
473 return "Fill custom object";
476 template <
typename H = HIST>
487template <
typename BinContentType,
bool WithWeight = false>
494 std::unique_ptr<ROOT::Experimental::RHistConcurrentFiller<BinContentType>>
fFiller;
495 std::vector<std::shared_ptr<ROOT::Experimental::RHistFillContext<BinContentType>>> fContexts;
501 for (
unsigned int i = 0; i <
nSlots; i++) {
502 fContexts[i] =
fFiller->CreateFillContext();
511 std::shared_ptr<Result_t> GetResultPtr()
const {
return fFiller.GetHist(); }
516 template <
typename...
ColumnTypes,
const std::size_t...
I>
523 fContexts[
slot]->Fill(args, weight);
539 for (
auto &&context : fContexts) {
544 std::string GetActionName() {
return "Hist"; }
547template <
typename BinContentType,
bool WithWeight = false>
554 std::shared_ptr<Result_t> fHist;
564 std::shared_ptr<Result_t> GetResultPtr()
const {
return fHist; }
569 template <
typename...
ColumnTypes,
const std::size_t...
I>
575 fHist->FillAtomic(args, weight);
591 std::string GetActionName() {
return "Hist"; }
600 std::vector<::TGraph *> fGraphs;
608 fGraphs[0] =
g.get();
610 for (
unsigned int i = 1; i <
nSlots; ++i) {
611 fGraphs[i] =
new TGraph(*fGraphs[0]);
619 template <
typename X0,
typename X1,
620 std::enable_if_t<IsDataContainer<X0>::value && IsDataContainer<X1>::value,
int> = 0>
623 if (
x0s.size() !=
x1s.size()) {
624 throw std::runtime_error(
"Cannot fill Graph with values in containers of different sizes.");
636 template <
typename X0,
typename X1,
637 std::enable_if_t<!IsDataContainer<X0>::value && !IsDataContainer<X1>::value,
int> = 0>
649 throw std::runtime_error(
"Graph was applied to a mix of scalar values and collections. This is not supported.");
654 const auto nSlots = fGraphs.size();
659 l.Add(fGraphs[
slot]);
667 return std::make_unique<RMergeableFill<Result_t>>(*fGraphs[0]);
670 std::string GetActionName() {
return "Graph"; }
672 Result_t &PartialUpdate(
unsigned int slot) {
return *fGraphs[
slot]; }
699 for (
unsigned int i = 1; i <
nSlots; ++i) {
709 typename X,
typename Y,
typename EXL,
typename EXH,
typename EYL,
typename EYH,
710 std::enable_if_t<IsDataContainer<X>::value && IsDataContainer<Y>::value && IsDataContainer<EXL>::value &&
711 IsDataContainer<EXH>::value && IsDataContainer<EYL>::value && IsDataContainer<EYH>::value,
716 if ((
xs.size() !=
ys.size()) || (
xs.size() !=
exls.size()) || (
xs.size() !=
exhs.size()) ||
717 (
xs.size() !=
eyls.size()) || (
xs.size() !=
eyhs.size())) {
718 throw std::runtime_error(
"Cannot fill GraphAsymmErrors with values in containers of different sizes.");
721 auto xsIt = std::begin(
xs);
722 auto ysIt = std::begin(
ys);
727 while (
xsIt != std::end(
xs)) {
736 typename X,
typename Y,
typename EXL,
typename EXH,
typename EYL,
typename EYH,
737 std::enable_if_t<!IsDataContainer<X>::value && !IsDataContainer<Y>::value && !IsDataContainer<EXL>::value &&
738 !IsDataContainer<EXH>::value && !IsDataContainer<EYL>::value && !IsDataContainer<EYH>::value,
750 template <
typename X,
typename Y,
typename EXL,
typename EXH,
typename EYL,
typename EYH,
754 throw std::runtime_error(
755 "GraphAsymmErrors was applied to a mix of scalar values and collections. This is not supported.");
776 std::string GetActionName() {
return "GraphAsymmErrors"; }
782 auto &
result = *
static_cast<std::shared_ptr<TGraphAsymmErrors> *
>(
newResult);
789template <
typename HIST>
791 std::vector<std::shared_ptr<HIST>> fObjects;
792 std::vector<std::unique_ptr<std::mutex>>
fMutexPtrs;
795 template <
typename T,
typename... Args>
802 template <
typename T,
typename... Args>
805 std::scoped_lock lock{mutex};
806 object.Fill(args...);
809 template <std::size_t
ColIdx,
typename End_t,
typename...
Its>
825 fObjects.front() =
h;
827 std::generate(fObjects.begin() + 1, fObjects.end(), [
h]() {
828 auto hist = std::make_shared<HIST>(*h);
829 UnsetDirectoryIfPossible(hist.get());
839 template <
typename...
ValTypes, std::enable_if_t<!Disjunction<IsDataContainer<ValTypes>...>
::value,
int> = 0>
847 template <
typename...
Xs, std::enable_if_t<Disjunction<IsDataContainer<Xs>...>
::value,
int> = 0>
851 constexpr std::array<
bool,
sizeof...(Xs)>
isContainer{IsDataContainer<Xs>::value...};
856 static_assert(
colidx <
sizeof...(Xs),
"Error: index of collection-type argument not found.");
862 std::array<std::size_t,
sizeof...(xs)>
sizes = {{GetSize(
xs)...}};
864 for (std::size_t i = 0; i <
sizeof...(xs); ++i) {
866 throw std::runtime_error(
"Cannot fill histogram with values in containers of different sizes.");
873 template <
typename T = HIST>
876 static_assert(
sizeof(T) < 0,
877 "When filling an object with RDataFrame (e.g. via a Fill action) the number or types of the "
878 "columns passed did not match the signature of the object's `FillThreadSafe` method.");
885 if (fObjects.size() > 1) {
887 for (
auto it = fObjects.cbegin() + 1; it != fObjects.end(); ++it) {
890 fObjects[0]->Merge(&list);
900 return std::make_unique<RMergeableFill<HIST>>(*fObjects[0]);
905 std::string GetActionName()
907 return std::string(fObjects[0]->IsA()->GetName()) +
"\\n" + std::string(fObjects[0]->GetName());
910 template <
typename H = HIST>
926template <
typename V,
typename COLL>
932template <
typename COLL>
939template <
typename RealT_t,
typename T,
typename COLL>
948 for (
unsigned int i = 1; i <
nSlots; ++i)
949 fColls.emplace_back(std::make_shared<COLL>());
963 for (
unsigned int i = 1; i <
fColls.size(); ++i) {
976 std::string GetActionName() {
return "Take"; }
988template <
typename RealT_t,
typename T>
990 :
public RActionImpl<TakeHelper<RealT_t, T, std::vector<T>>> {
998 for (
unsigned int i = 1; i <
nSlots; ++i) {
999 auto v = std::make_shared<std::vector<T>>();
1021 for (
unsigned int i = 1; i <
fColls.size(); ++i) {
1027 std::vector<T> &PartialUpdate(
unsigned int slot) {
return *
fColls[
slot]; }
1029 std::string GetActionName() {
return "Take"; }
1033 auto &
result = *
static_cast<std::shared_ptr<std::vector<T>
> *>(
newResult);
1041template <
typename RealT_t,
typename COLL>
1043 :
public RActionImpl<TakeHelper<RealT_t, RVec<RealT_t>, COLL>> {
1051 for (
unsigned int i = 1; i <
nSlots; ++i)
1052 fColls.emplace_back(std::make_shared<COLL>());
1066 for (
unsigned int i = 1; i <
fColls.size(); ++i) {
1068 for (
auto &
v : *
coll) {
1074 std::string GetActionName() {
return "Take"; }
1086template <
typename RealT_t>
1088 :
public RActionImpl<TakeHelper<RealT_t, RVec<RealT_t>, std::vector<RealT_t>>> {
1097 for (
unsigned int i = 1; i <
nSlots; ++i) {
1098 auto v = std::make_shared<std::vector<RealT_t>>();
1120 for (
unsigned int i = 1; i <
fColls.size(); ++i) {
1126 std::string GetActionName() {
return "Take"; }
1139template <
typename RealT_t,
typename T,
typename COLL>
1141template <
typename RealT_t,
typename T>
1143template <
typename RealT_t,
typename COLL>
1145template <
typename RealT_t>
1161template <
typename ResultType>
1188 *
fResultMin = std::numeric_limits<ResultType>::max();
1196 return std::make_unique<RMergeableMin<ResultType>>(*fResultMin);
1201 std::string GetActionName() {
return "Min"; }
1205 auto &
result = *
static_cast<std::shared_ptr<ResultType> *
>(
newResult);
1210template <
typename ResultType>
1237 *
fResultMax = std::numeric_limits<ResultType>::lowest();
1246 return std::make_unique<RMergeableMax<ResultType>>(*fResultMax);
1251 std::string GetActionName() {
return "Max"; }
1255 auto &
result = *
static_cast<std::shared_ptr<ResultType> *
>(
newResult);
1260template <
typename ResultType>
1269 template <
typename T = ResultType>
1275 template <
typename T = ResultType,
typename Dummy =
int>
1303 for (
auto &&
v :
vs) {
1329 return std::make_unique<RMergeableSum<ResultType>>(*fResultSum);
1334 std::string GetActionName() {
return "Sum"; }
1338 auto &
result = *
static_cast<std::shared_ptr<ResultType> *
>(
newResult);
1346 std::vector<ULong64_t> fCounts;
1347 std::vector<double>
fSums;
1361 for (
auto &&
v :
vs) {
1383 double &PartialUpdate(
unsigned int slot);
1385 std::string GetActionName() {
return "Mean"; }
1396 unsigned int fNSlots;
1399 std::vector<ULong64_t> fCounts;
1401 std::vector<double> fMeans;
1415 for (
auto &&
v :
vs) {
1429 std::inner_product(fMeans.begin(), fMeans.end(), fCounts.begin(), 0.) /
static_cast<Double_t>(
counts);
1433 std::string GetActionName() {
return "StdDev"; }
1442template <
typename PrevNodeType>
1447 std::shared_ptr<PrevNodeType> fPrevNode;
1448 size_t fEntriesToProcess;
1451 DisplayHelper(
size_t nRows,
const std::shared_ptr<Display_t> &
d,
const std::shared_ptr<PrevNodeType> &prevNode)
1455 DisplayHelper(DisplayHelper &&) =
default;
1456 DisplayHelper(
const DisplayHelper &) =
delete;
1459 template <
typename...
Columns>
1462 if (fEntriesToProcess == 0)
1466 --fEntriesToProcess;
1468 if (fEntriesToProcess == 0) {
1473 fPrevNode->StopProcessing();
1481 std::string GetActionName() {
return "Display"; }
1484template <
typename Acc,
typename Merge,
typename R,
typename T,
typename U,
1487 :
public RActionImpl<AggregateHelper<Acc, Merge, R, T, U, MustCopyAssign>> {
1490 std::shared_ptr<U> fResult;
1511 template <
bool MustCopyAssign_ = MustCopyAssign, std::enable_if_t<MustCopyAssign_,
int> = 0>
1517 template <
bool MustCopyAssign_ = MustCopyAssign, std::enable_if_t<!MustCopyAssign_,
int> = 0>
1526 bool MergeAll = std::is_same<void, MergeRet>::value>
1527 std::enable_if_t<MergeAll, void> Finalize()
1535 std::enable_if_t<MergeTwoByTwo, void> Finalize(...)
1538 *fResult = fMerge(*fResult,
acc);
1543 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))
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)