Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ActionHelpers.hxx
Go to the documentation of this file.
1/**
2 \file ROOT/RDF/ActionHelpers.hxx
3 \ingroup dataframe
4 \author Enrico Guiraud, CERN
5 \author Danilo Piparo, CERN
6 \date 2016-12
7 \author Vincenzo Eduardo Padulano
8 \date 2020-06
9*/
10
11/*************************************************************************
12 * Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. *
13 * All rights reserved. *
14 * *
15 * For the licensing terms see $ROOTSYS/LICENSE. *
16 * For the list of contributors see $ROOTSYS/README/CREDITS. *
17 *************************************************************************/
18
19#ifndef ROOT_RDFOPERATIONS
20#define ROOT_RDFOPERATIONS
21
22#include "ROOT/RVec.hxx"
23#include "ROOT/RDF/Utils.hxx"
24#include "ROOT/TypeTraits.hxx"
25#include "ROOT/RDF/RDisplay.hxx"
26#include "RtypesCore.h"
27#include "TH1.h"
28#include "TH3.h"
29#include "TGraph.h"
30#include "TGraphAsymmErrors.h"
31#include "TObject.h"
34
35#include "RConfigure.h" // for R__HAS_ROOT7
36#ifdef R__HAS_ROOT7
37#include <ROOT/RHist.hxx>
39#include <ROOT/RHistEngine.hxx>
40#include <ROOT/RWeight.hxx>
41#endif
42
43#include <algorithm>
44#include <array>
45#include <limits>
46#include <memory>
47#include <mutex>
48#include <stdexcept>
49#include <string>
50#include <string_view>
51#include <tuple>
52#include <type_traits>
53#include <utility> // std::index_sequence
54#include <vector>
55#include <numeric> // std::accumulate in MeanHelper
56
57class TCollection;
58class TStatistic;
59class TTreeReader;
60namespace ROOT::RDF {
61class RCutFlowReport;
62} // namespace ROOT::RDF
63
64/// \cond HIDDEN_SYMBOLS
65
66namespace ROOT {
67namespace Internal {
68namespace RDF {
69using namespace ROOT::TypeTraits;
70using namespace ROOT::VecOps;
71using namespace ROOT::RDF;
72using namespace ROOT::Detail::RDF;
73
74using Hist_t = ::TH1D;
75
76/// The container type for each thread's partial result in an action helper
77// We have to avoid to instantiate std::vector<bool> as that makes it impossible to return a reference to one of
78// the thread-local results. In addition, a common definition for the type of the container makes it easy to swap
79// the type of the underlying container if e.g. we see problems with false sharing of the thread-local results..
80template <typename T>
81using Results = std::conditional_t<std::is_same<T, bool>::value, std::deque<T>, std::vector<T>>;
82
83template <typename F>
84class R__CLING_PTRCHECK(off) ForeachSlotHelper : public RActionImpl<ForeachSlotHelper<F>> {
85 F fCallable;
86
87public:
91 ForeachSlotHelper(const ForeachSlotHelper &) = delete;
92
93 void InitTask(TTreeReader *, unsigned int) {}
94
95 template <typename... Args>
96 void Exec(unsigned int slot, Args &&... args)
97 {
98 // check that the decayed types of Args are the same as the branch types
99 static_assert(std::is_same<TypeList<std::decay_t<Args>...>, ColumnTypes_t>::value, "");
100 fCallable(slot, std::forward<Args>(args)...);
101 }
102
103 void Initialize() { /* noop */}
104
105 void Finalize() { /* noop */}
106
107 std::string GetActionName() { return "ForeachSlot"; }
108};
109
110class R__CLING_PTRCHECK(off) CountHelper : public RActionImpl<CountHelper> {
111 std::shared_ptr<ULong64_t> fResultCount;
112 Results<ULong64_t> fCounts;
113
114public:
115 using ColumnTypes_t = TypeList<>;
116 CountHelper(const std::shared_ptr<ULong64_t> &resultCount, const unsigned int nSlots);
117 CountHelper(CountHelper &&) = default;
118 CountHelper(const CountHelper &) = delete;
119 void InitTask(TTreeReader *, unsigned int) {}
120 void Exec(unsigned int slot);
121 void Initialize() { /* noop */}
122 void Finalize();
123
124 // Helper functions for RMergeableValue
125 std::unique_ptr<RMergeableValueBase> GetMergeableValue() const final
126 {
127 return std::make_unique<RMergeableCount>(*fResultCount);
128 }
129
130 ULong64_t &PartialUpdate(unsigned int slot);
131
132 std::string GetActionName() { return "Count"; }
133
134 CountHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
135 {
136 auto &result = *static_cast<std::shared_ptr<ULong64_t> *>(newResult);
137 return CountHelper(result, fCounts.size());
138 }
139};
140
141template <typename RNode_t>
142class R__CLING_PTRCHECK(off) ReportHelper : public RActionImpl<ReportHelper<RNode_t>> {
143 std::shared_ptr<RCutFlowReport> fReport;
144 /// Non-owning pointer, never null. As usual, the node is owned by its children nodes (and therefore indirectly by
145 /// the RAction corresponding to this action helper).
146 RNode_t *fNode;
148
149public:
150 using ColumnTypes_t = TypeList<>;
151 ReportHelper(const std::shared_ptr<RCutFlowReport> &report, RNode_t *node, bool emptyRep)
152 : fReport(report), fNode(node), fReturnEmptyReport(emptyRep){};
153 ReportHelper(ReportHelper &&) = default;
154 ReportHelper(const ReportHelper &) = delete;
155 void InitTask(TTreeReader *, unsigned int) {}
156 void Exec(unsigned int /* slot */) {}
157 void Initialize() { /* noop */}
158 void Finalize()
159 {
161 fNode->Report(*fReport);
162 }
163
164 std::unique_ptr<RMergeableValueBase> GetMergeableValue() const final
165 {
166 auto cutinfo_vec = fReport->fCutInfos;
167 return std::make_unique<RMergeableReport>(*fReport, cutinfo_vec);
168 }
169
170 std::string GetActionName() { return "Report"; }
171
172 ReportHelper MakeNew(void *newResult, std::string_view variation = "nominal")
173 {
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(),
178 }
179};
180
181/// This helper fills TH1Ds for which no axes were specified by buffering the fill values to pick good axes limits.
182///
183/// TH1Ds have an automatic mechanism to pick good limits based on the first N entries they were filled with, but
184/// that does not work in multi-thread event loops as it might yield histograms with incompatible binning in each
185/// thread, making it impossible to merge the per-thread results.
186/// Instead, this helper delays the decision on the axes limits until all threads have done processing, synchronizing
187/// the decision on the limits as part of the merge operation.
188class R__CLING_PTRCHECK(off) BufferedFillHelper : public RActionImpl<BufferedFillHelper> {
189 // this sets a total initial size of 16 MB for the buffers (can increase)
190 static constexpr unsigned int fgTotalBufSize = 2097152;
191 using BufEl_t = double;
192 using Buf_t = std::vector<BufEl_t>;
193
194 std::vector<Buf_t> fBuffers;
195 std::vector<Buf_t> fWBuffers;
196 std::shared_ptr<Hist_t> fResultHist;
197 unsigned int fNSlots;
198 unsigned int fBufSize;
199 /// Histograms containing "snapshots" of partial results. Non-null only if a registered callback requires it.
201 Buf_t fMin;
202 Buf_t fMax;
203
204 void UpdateMinMax(unsigned int slot, double v);
205
206public:
207 BufferedFillHelper(const std::shared_ptr<Hist_t> &h, const unsigned int nSlots);
209 BufferedFillHelper(const BufferedFillHelper &) = delete;
210 void InitTask(TTreeReader *, unsigned int) {}
211 void Exec(unsigned int slot, double v);
212 void Exec(unsigned int slot, double v, double w);
213
215 void Exec(unsigned int slot, const T &vs)
216 {
217 auto &thisBuf = fBuffers[slot];
218 // range-based for results in warnings on some compilers due to vector<bool>'s custom reference type
219 for (auto v = vs.begin(); v != vs.end(); ++v) {
221 thisBuf.emplace_back(*v); // TODO: Can be optimised in case T == BufEl_t
222 }
223 }
224
226 void Exec(unsigned int slot, const T &vs, const W &ws)
227 {
228 auto &thisBuf = fBuffers[slot];
229
230 for (auto &v : vs) {
232 thisBuf.emplace_back(v);
233 }
234
235 auto &thisWBuf = fWBuffers[slot];
236 for (auto &w : ws) {
237 thisWBuf.emplace_back(w); // TODO: Can be optimised in case T == BufEl_t
238 }
239 }
240
242 void Exec(unsigned int slot, const T &vs, const W w)
243 {
244 auto &thisBuf = fBuffers[slot];
245 for (auto &v : vs) {
247 thisBuf.emplace_back(v); // TODO: Can be optimised in case T == BufEl_t
248 }
249
250 auto &thisWBuf = fWBuffers[slot];
251 thisWBuf.insert(thisWBuf.end(), vs.size(), w);
252 }
253
255 void Exec(unsigned int slot, const T v, const W &ws)
256 {
258 auto &thisBuf = fBuffers[slot];
259 thisBuf.insert(thisBuf.end(), ws.size(), v);
260
261 auto &thisWBuf = fWBuffers[slot];
262 thisWBuf.insert(thisWBuf.end(), ws.begin(), ws.end());
263 }
264
265 Hist_t &PartialUpdate(unsigned int);
266
267 void Initialize() { /* noop */}
268
269 void Finalize();
270
271 // Helper functions for RMergeableValue
272 std::unique_ptr<RMergeableValueBase> GetMergeableValue() const final
273 {
274 return std::make_unique<RMergeableFill<Hist_t>>(*fResultHist);
275 }
276
277 std::string GetActionName()
278 {
279 return std::string(fResultHist->IsA()->GetName()) + "\\n" + std::string(fResultHist->GetName());
280 }
281
282 BufferedFillHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
283 {
284 auto &result = *static_cast<std::shared_ptr<Hist_t> *>(newResult);
285 result->Reset();
286 result->SetDirectory(nullptr);
287 return BufferedFillHelper(result, fNSlots);
288 }
289};
290
291// class which wraps a pointer and implements a no-op increment operator
292template <typename T>
294 const T *obj_;
295
296public:
297 using iterator_category = std::forward_iterator_tag;
298 using difference_type = std::ptrdiff_t;
299 using value_type = T;
300 using pointer = T *;
301 using reference = T &;
302 ScalarConstIterator(const T *obj) : obj_(obj) {}
303 const T &operator*() const { return *obj_; }
304 ScalarConstIterator<T> &operator++() { return *this; }
305};
306
307// return unchanged value for scalar
308template <typename T>
309auto MakeBegin(const T &val)
310{
311 if constexpr (IsDataContainer<T>::value) {
312 return std::begin(val);
313 } else {
314 return ScalarConstIterator<T>(&val);
315 }
316}
317
318// return container size for containers, and 1 for scalars
319template <typename T>
320std::size_t GetSize(const T &val)
321{
322 if constexpr (IsDataContainer<T>::value) {
323 return std::size(val);
324 } else {
325 return 1;
326 }
327}
328
329// Helpers for dealing with histograms and similar:
331void ResetIfPossible(H *h)
332{
333 h->Reset();
334}
335
337void ResetIfPossible(...);
338
341
342/// The generic Fill helper: it calls Fill on per-thread objects and then Merge to produce a final result.
343/// For one-dimensional histograms, if no axes are specified, RDataFrame uses BufferedFillHelper instead.
344template <typename HIST = Hist_t>
345class R__CLING_PTRCHECK(off) FillHelper : public RActionImpl<FillHelper<HIST>> {
346 std::vector<HIST *> fObjects;
347
348 // Merge overload for types with Merge(TCollection*), like TH1s
350 auto Merge(std::vector<H *> &objs, int /*toincreaseoverloadpriority*/)
351 -> decltype(objs[0]->Merge((TCollection *)nullptr), void())
352 {
353 TList l;
354 for (auto it = ++objs.begin(); it != objs.end(); ++it)
355 l.Add(*it);
356 objs[0]->Merge(&l);
357 }
358
359 // Merge overload for types with Merge(const std::vector&)
360 template <typename H>
361 auto Merge(std::vector<H *> &objs, double /*toloweroverloadpriority*/)
362 -> decltype(objs[0]->Merge(std::vector<HIST *>{}), void())
363 {
364 objs[0]->Merge({++objs.begin(), objs.end()});
365 }
366
367 // Merge overload to error out in case no valid HIST::Merge method was detected
368 template <typename T>
369 void Merge(T, ...)
370 {
371 static_assert(sizeof(T) < 0,
372 "The type passed to Fill does not provide a Merge(TCollection*) or Merge(const std::vector&) method.");
373 }
374
375 template <std::size_t ColIdx, typename End_t, typename... Its>
376 void ExecLoop(unsigned int slot, End_t end, Its... its)
377 {
378 for (auto *thisSlotH = fObjects[slot]; GetNthElement<ColIdx>(its...) != end; (std::advance(its, 1), ...)) {
379 thisSlotH->Fill(*its...);
380 }
381 }
382
383public:
384 FillHelper(FillHelper &&) = default;
385 FillHelper(const FillHelper &) = delete;
386
387 FillHelper(const std::shared_ptr<HIST> &h, const unsigned int nSlots) : fObjects(nSlots, nullptr)
388 {
389 fObjects[0] = h.get();
390 // Initialize all other slots
391 for (unsigned int i = 1; i < nSlots; ++i) {
392 fObjects[i] = new HIST(*fObjects[0]);
393 UnsetDirectoryIfPossible(fObjects[i]);
394 }
395 }
396
397 void InitTask(TTreeReader *, unsigned int) {}
398
399 // no container arguments
400 template <typename... ValTypes, std::enable_if_t<!Disjunction<IsDataContainer<ValTypes>...>::value, int> = 0>
401 auto Exec(unsigned int slot, const ValTypes &...x) -> decltype(fObjects[slot]->Fill(x...), void())
402 {
403 fObjects[slot]->Fill(x...);
404 }
405
406 // at least one container argument
407 template <typename... Xs, std::enable_if_t<Disjunction<IsDataContainer<Xs>...>::value, int> = 0>
408 auto Exec(unsigned int slot, const Xs &...xs) -> decltype(fObjects[slot]->Fill(*MakeBegin(xs)...), void())
409 {
410 // array of bools keeping track of which inputs are containers
411 constexpr std::array<bool, sizeof...(Xs)> isContainer{IsDataContainer<Xs>::value...};
412
413 // index of the first container input
414 constexpr std::size_t colidx = FindIdxTrue(isContainer);
415 // if this happens, there is a bug in the implementation
416 static_assert(colidx < sizeof...(Xs), "Error: index of collection-type argument not found.");
417
418 // get the end iterator to the first container
419 auto const xrefend = std::end(GetNthElement<colidx>(xs...));
420
421 // array of container sizes (1 for scalars)
422 std::array<std::size_t, sizeof...(xs)> sizes = {{GetSize(xs)...}};
423
424 for (std::size_t i = 0; i < sizeof...(xs); ++i) {
425 if (isContainer[i] && sizes[i] != sizes[colidx]) {
426 throw std::runtime_error("Cannot fill histogram with values in containers of different sizes.");
427 }
428 }
429
431 }
432
433 template <typename T = HIST>
434 void Exec(...)
435 {
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.");
439 }
440
441 void Initialize() { /* noop */}
442
443 void Finalize()
444 {
445 if (fObjects.size() == 1)
446 return;
447
448 Merge(fObjects, /*toselectcorrectoverload=*/0);
449
450 // delete the copies we created for the slots other than the first
451 for (auto it = ++fObjects.begin(); it != fObjects.end(); ++it)
452 delete *it;
453 }
454
455 HIST &PartialUpdate(unsigned int slot) { return *fObjects[slot]; }
456
457 // Helper functions for RMergeableValue
458 std::unique_ptr<RMergeableValueBase> GetMergeableValue() const final
459 {
460 return std::make_unique<RMergeableFill<HIST>>(*fObjects[0]);
461 }
462
463 // if the fObjects vector type is derived from TObject, return the name of the object
465 std::string GetActionName()
466 {
467 return std::string(fObjects[0]->IsA()->GetName()) + "\\n" + std::string(fObjects[0]->GetName());
468 }
469
470 // if fObjects is not derived from TObject, indicate it is some other object
472 std::string GetActionName()
473 {
474 return "Fill custom object";
475 }
476
477 template <typename H = HIST>
478 FillHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
479 {
480 auto &result = *static_cast<std::shared_ptr<H> *>(newResult);
481 ResetIfPossible(result.get());
483 return FillHelper(result, fObjects.size());
484 }
485};
486
487#ifdef R__HAS_ROOT7
488template <typename BinContentType, bool WithWeight = false>
490 : public ROOT::Detail::RDF::RActionImpl<RHistFillHelper<BinContentType, WithWeight>> {
491public:
493
494private:
495 std::unique_ptr<ROOT::Experimental::RHistConcurrentFiller<BinContentType>> fFiller;
496 std::vector<std::shared_ptr<ROOT::Experimental::RHistFillContext<BinContentType>>> fContexts;
497
498public:
500 : fFiller(new ROOT::Experimental::RHistConcurrentFiller<BinContentType>(h)), fContexts(nSlots)
501 {
502 for (unsigned int i = 0; i < nSlots; i++) {
503 fContexts[i] = fFiller->CreateFillContext();
504 }
505 }
506 RHistFillHelper(const RHistFillHelper &) = delete;
507 RHistFillHelper(RHistFillHelper &&) = default;
508 RHistFillHelper &operator=(const RHistFillHelper &) = delete;
510 ~RHistFillHelper() = default;
511
512 std::shared_ptr<Result_t> GetResultPtr() const { return fFiller.GetHist(); }
513
514 void Initialize() {}
515 void InitTask(TTreeReader *, unsigned int) {}
516
517 template <typename... ColumnTypes, const std::size_t... I>
518 void
519 ExecWithWeight(unsigned int slot, const std::tuple<const ColumnTypes &...> &columnValues, std::index_sequence<I...>)
520 {
521 // Build a tuple of const references with the actual arguments, stripping the weight and avoiding copies.
522 std::tuple<const std::tuple_element_t<I, std::tuple<ColumnTypes...>> &...> args(std::get<I>(columnValues)...);
523 ROOT::Experimental::RWeight weight(std::get<sizeof...(ColumnTypes) - 1>(columnValues));
524 fContexts[slot]->Fill(args, weight);
525 }
526
527 template <typename... ColumnTypes>
528 void Exec(unsigned int slot, const ColumnTypes &...columnValues)
529 {
530 if constexpr (WithWeight) {
531 auto t = std::forward_as_tuple(columnValues...);
532 ExecWithWeight(slot, t, std::make_index_sequence<sizeof...(ColumnTypes) - 1>());
533 } else {
534 fContexts[slot]->Fill(columnValues...);
535 }
536 }
537
538 void Finalize()
539 {
540 for (auto &&context : fContexts) {
541 context->Flush();
542 }
543 }
544
545 std::string GetActionName() { return "Hist"; }
546};
547
548template <typename BinContentType, bool WithWeight = false>
550 : public ROOT::Detail::RDF::RActionImpl<RHistEngineFillHelper<BinContentType, WithWeight>> {
551public:
553
554private:
555 std::shared_ptr<Result_t> fHist;
556
557public:
563 ~RHistEngineFillHelper() = default;
564
565 std::shared_ptr<Result_t> GetResultPtr() const { return fHist; }
566
567 void Initialize() {}
568 void InitTask(TTreeReader *, unsigned int) {}
569
570 template <typename... ColumnTypes, const std::size_t... I>
571 void ExecWithWeight(const std::tuple<const ColumnTypes &...> &columnValues, std::index_sequence<I...>)
572 {
573 // Build a tuple of const references with the actual arguments, stripping the weight and avoiding copies.
574 std::tuple<const std::tuple_element_t<I, std::tuple<ColumnTypes...>> &...> args(std::get<I>(columnValues)...);
575 ROOT::Experimental::RWeight weight(std::get<sizeof...(ColumnTypes) - 1>(columnValues));
576 fHist->FillAtomic(args, weight);
577 }
578
579 template <typename... ColumnTypes>
580 void Exec(unsigned int, const ColumnTypes &...columnValues)
581 {
582 if constexpr (WithWeight) {
583 auto t = std::forward_as_tuple(columnValues...);
584 ExecWithWeight(t, std::make_index_sequence<sizeof...(ColumnTypes) - 1>());
585 } else {
586 fHist->FillAtomic(columnValues...);
587 }
588 }
589
590 void Finalize() {}
591
592 std::string GetActionName() { return "Hist"; }
593};
594#endif
595
597public:
598 using Result_t = ::TGraph;
599
600private:
601 std::vector<::TGraph *> fGraphs;
602
603public:
605 FillTGraphHelper(const FillTGraphHelper &) = delete;
606
607 FillTGraphHelper(const std::shared_ptr<::TGraph> &g, const unsigned int nSlots) : fGraphs(nSlots, nullptr)
608 {
609 fGraphs[0] = g.get();
610 // Initialize all other slots
611 for (unsigned int i = 1; i < nSlots; ++i) {
612 fGraphs[i] = new TGraph(*fGraphs[0]);
613 }
614 }
615
616 void Initialize() {}
617 void InitTask(TTreeReader *, unsigned int) {}
618
619 // case: both types are container types
620 template <typename X0, typename X1,
621 std::enable_if_t<IsDataContainer<X0>::value && IsDataContainer<X1>::value, int> = 0>
622 void Exec(unsigned int slot, const X0 &x0s, const X1 &x1s)
623 {
624 if (x0s.size() != x1s.size()) {
625 throw std::runtime_error("Cannot fill Graph with values in containers of different sizes.");
626 }
627 auto *thisSlotG = fGraphs[slot];
628 auto x0sIt = std::begin(x0s);
629 const auto x0sEnd = std::end(x0s);
630 auto x1sIt = std::begin(x1s);
631 for (; x0sIt != x0sEnd; x0sIt++, x1sIt++) {
632 thisSlotG->SetPoint(thisSlotG->GetN(), *x0sIt, *x1sIt);
633 }
634 }
635
636 // case: both types are non-container types, e.g. scalars
637 template <typename X0, typename X1,
638 std::enable_if_t<!IsDataContainer<X0>::value && !IsDataContainer<X1>::value, int> = 0>
639 void Exec(unsigned int slot, X0 x0, X1 x1)
640 {
641 auto thisSlotG = fGraphs[slot];
642 thisSlotG->SetPoint(thisSlotG->GetN(), x0, x1);
643 }
644
645 // case: types are combination of containers and non-containers
646 // this is not supported, error out
647 template <typename X0, typename X1, typename... ExtraArgsToLowerPriority>
648 void Exec(unsigned int, X0, X1, ExtraArgsToLowerPriority...)
649 {
650 throw std::runtime_error("Graph was applied to a mix of scalar values and collections. This is not supported.");
651 }
652
653 void Finalize()
654 {
655 const auto nSlots = fGraphs.size();
656 auto resGraph = fGraphs[0];
657 TList l;
658 l.SetOwner(); // The list will free the memory associated to its elements upon destruction
659 for (unsigned int slot = 1; slot < nSlots; ++slot) {
660 l.Add(fGraphs[slot]);
661 }
662 resGraph->Merge(&l);
663 }
664
665 // Helper functions for RMergeableValue
666 std::unique_ptr<RMergeableValueBase> GetMergeableValue() const final
667 {
668 return std::make_unique<RMergeableFill<Result_t>>(*fGraphs[0]);
669 }
670
671 std::string GetActionName() { return "Graph"; }
672
673 Result_t &PartialUpdate(unsigned int slot) { return *fGraphs[slot]; }
674
675 FillTGraphHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
676 {
677 auto &result = *static_cast<std::shared_ptr<TGraph> *>(newResult);
678 result->Set(0);
679 return FillTGraphHelper(result, fGraphs.size());
680 }
681};
682
684 : public ROOT::Detail::RDF::RActionImpl<FillTGraphAsymmErrorsHelper> {
685public:
686 using Result_t = ::TGraphAsymmErrors;
687
688private:
689 std::vector<::TGraphAsymmErrors *> fGraphAsymmErrors;
690
691public:
694
695 FillTGraphAsymmErrorsHelper(const std::shared_ptr<::TGraphAsymmErrors> &g, const unsigned int nSlots)
696 : fGraphAsymmErrors(nSlots, nullptr)
697 {
698 fGraphAsymmErrors[0] = g.get();
699 // Initialize all other slots
700 for (unsigned int i = 1; i < nSlots; ++i) {
702 }
703 }
704
705 void Initialize() {}
706 void InitTask(TTreeReader *, unsigned int) {}
707
708 // case: all types are container types
709 template <
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,
713 int> = 0>
714 void
715 Exec(unsigned int slot, const X &xs, const Y &ys, const EXL &exls, const EXH &exhs, const EYL &eyls, const EYH &eyhs)
716 {
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.");
720 }
722 auto xsIt = std::begin(xs);
723 auto ysIt = std::begin(ys);
724 auto exlsIt = std::begin(exls);
725 auto exhsIt = std::begin(exhs);
726 auto eylsIt = std::begin(eyls);
727 auto eyhsIt = std::begin(eyhs);
728 while (xsIt != std::end(xs)) {
729 const auto n = thisSlotG->GetN(); // must use the same `n` for SetPoint and SetPointError
730 thisSlotG->SetPoint(n, *xsIt++, *ysIt++);
731 thisSlotG->SetPointError(n, *exlsIt++, *exhsIt++, *eylsIt++, *eyhsIt++);
732 }
733 }
734
735 // case: all types are non-container types, e.g. scalars
736 template <
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,
740 int> = 0>
741 void Exec(unsigned int slot, X x, Y y, EXL exl, EXH exh, EYL eyl, EYH eyh)
742 {
744 const auto n = thisSlotG->GetN();
745 thisSlotG->SetPoint(n, x, y);
746 thisSlotG->SetPointError(n, exl, exh, eyl, eyh);
747 }
748
749 // case: types are combination of containers and non-containers
750 // this is not supported, error out
751 template <typename X, typename Y, typename EXL, typename EXH, typename EYL, typename EYH,
752 typename... ExtraArgsToLowerPriority>
753 void Exec(unsigned int, X, Y, EXL, EXH, EYL, EYH, ExtraArgsToLowerPriority...)
754 {
755 throw std::runtime_error(
756 "GraphAsymmErrors was applied to a mix of scalar values and collections. This is not supported.");
757 }
758
759 void Finalize()
760 {
761 const auto nSlots = fGraphAsymmErrors.size();
763 TList l;
764 l.SetOwner(); // The list will free the memory associated to its elements upon destruction
765 for (unsigned int slot = 1; slot < nSlots; ++slot) {
767 }
768 resGraphAsymmErrors->Merge(&l);
769 }
770
771 // Helper functions for RMergeableValue
772 std::unique_ptr<RMergeableValueBase> GetMergeableValue() const final
773 {
774 return std::make_unique<RMergeableFill<Result_t>>(*fGraphAsymmErrors[0]);
775 }
776
777 std::string GetActionName() { return "GraphAsymmErrors"; }
778
779 Result_t &PartialUpdate(unsigned int slot) { return *fGraphAsymmErrors[slot]; }
780
781 FillTGraphAsymmErrorsHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
782 {
783 auto &result = *static_cast<std::shared_ptr<TGraphAsymmErrors> *>(newResult);
784 result->Set(0);
786 }
787};
788
789/// A FillHelper for classes supporting the FillThreadSafe function.
790template <typename HIST>
791class R__CLING_PTRCHECK(off) ThreadSafeFillHelper : public RActionImpl<ThreadSafeFillHelper<HIST>> {
792 std::vector<std::shared_ptr<HIST>> fObjects;
793 std::vector<std::unique_ptr<std::mutex>> fMutexPtrs;
794
795 // This overload matches if the function exists:
796 template <typename T, typename... Args>
797 auto TryCallFillThreadSafe(T &object, std::mutex &, int /*dummy*/, Args... args)
798 -> decltype(ROOT::Internal::FillThreadSafe(object, args...), void())
799 {
800 ROOT::Internal::FillThreadSafe(object, args...);
801 }
802 // This one has lower precedence because of the dummy argument, and uses a lock
803 template <typename T, typename... Args>
804 auto TryCallFillThreadSafe(T &object, std::mutex &mutex, char /*dummy*/, Args... args)
805 {
806 std::scoped_lock lock{mutex};
807 object.Fill(args...);
808 }
809
810 template <std::size_t ColIdx, typename End_t, typename... Its>
811 void ExecLoop(unsigned int slot, End_t end, Its... its)
812 {
813 const auto localSlot = slot % fObjects.size();
814 for (; GetNthElement<ColIdx>(its...) != end; (std::advance(its, 1), ...)) {
816 }
817 }
818
819public:
822
823 ThreadSafeFillHelper(const std::shared_ptr<HIST> &h, const unsigned int nSlots)
824 {
825 fObjects.resize(nSlots);
826 fObjects.front() = h;
827
828 std::generate(fObjects.begin() + 1, fObjects.end(), [h]() {
829 auto hist = std::make_shared<HIST>(*h);
830 UnsetDirectoryIfPossible(hist.get());
831 return hist;
832 });
833 fMutexPtrs.resize(nSlots);
834 std::generate(fMutexPtrs.begin(), fMutexPtrs.end(), []() { return std::make_unique<std::mutex>(); });
835 }
836
837 void InitTask(TTreeReader *, unsigned int) {}
838
839 // no container arguments
840 template <typename... ValTypes, std::enable_if_t<!Disjunction<IsDataContainer<ValTypes>...>::value, int> = 0>
841 void Exec(unsigned int slot, const ValTypes &...x)
842 {
843 const auto localSlot = slot % fObjects.size();
845 }
846
847 // at least one container argument
848 template <typename... Xs, std::enable_if_t<Disjunction<IsDataContainer<Xs>...>::value, int> = 0>
849 void Exec(unsigned int slot, const Xs &...xs)
850 {
851 // array of bools keeping track of which inputs are containers
852 constexpr std::array<bool, sizeof...(Xs)> isContainer{IsDataContainer<Xs>::value...};
853
854 // index of the first container input
855 constexpr std::size_t colidx = FindIdxTrue(isContainer);
856 // if this happens, there is a bug in the implementation
857 static_assert(colidx < sizeof...(Xs), "Error: index of collection-type argument not found.");
858
859 // get the end iterator to the first container
860 auto const xrefend = std::end(GetNthElement<colidx>(xs...));
861
862 // array of container sizes (1 for scalars)
863 std::array<std::size_t, sizeof...(xs)> sizes = {{GetSize(xs)...}};
864
865 for (std::size_t i = 0; i < sizeof...(xs); ++i) {
866 if (isContainer[i] && sizes[i] != sizes[colidx]) {
867 throw std::runtime_error("Cannot fill histogram with values in containers of different sizes.");
868 }
869 }
870
872 }
873
874 template <typename T = HIST>
875 void Exec(...)
876 {
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.");
880 }
881
882 void Initialize() { /* noop */ }
883
884 void Finalize()
885 {
886 if (fObjects.size() > 1) {
887 TList list;
888 for (auto it = fObjects.cbegin() + 1; it != fObjects.end(); ++it) {
889 list.Add(it->get());
890 }
891 fObjects[0]->Merge(&list);
892 }
893
894 fObjects.resize(1);
895 fMutexPtrs.clear();
896 }
897
898 // Helper function for RMergeableValue
899 std::unique_ptr<RMergeableValueBase> GetMergeableValue() const final
900 {
901 return std::make_unique<RMergeableFill<HIST>>(*fObjects[0]);
902 }
903
904 // if the fObjects vector type is derived from TObject, return the name of the object
906 std::string GetActionName()
907 {
908 return std::string(fObjects[0]->IsA()->GetName()) + "\\n" + std::string(fObjects[0]->GetName());
909 }
910
911 template <typename H = HIST>
912 ThreadSafeFillHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
913 {
914 auto &result = *static_cast<std::shared_ptr<H> *>(newResult);
915 ResetIfPossible(result.get());
917 return ThreadSafeFillHelper(result, fObjects.size());
918 }
919};
920
921// In case of the take helper we have 4 cases:
922// 1. The column is not an RVec, the collection is not a vector
923// 2. The column is not an RVec, the collection is a vector
924// 3. The column is an RVec, the collection is not a vector
925// 4. The column is an RVec, the collection is a vector
926
927template <typename V, typename COLL>
928void FillColl(V&& v, COLL& c) {
929 c.emplace_back(v);
930}
931
932// Use push_back for bool since some compilers do not support emplace_back.
933template <typename COLL>
934void FillColl(bool v, COLL& c) {
935 c.push_back(v);
936}
937
938// Case 1.: The column is not an RVec, the collection is not a vector
939// No optimisations, no transformations: just copies.
940template <typename RealT_t, typename T, typename COLL>
941class R__CLING_PTRCHECK(off) TakeHelper : public RActionImpl<TakeHelper<RealT_t, T, COLL>> {
943
944public:
945 using ColumnTypes_t = TypeList<T>;
946 TakeHelper(const std::shared_ptr<COLL> &resultColl, const unsigned int nSlots)
947 {
948 fColls.emplace_back(resultColl);
949 for (unsigned int i = 1; i < nSlots; ++i)
950 fColls.emplace_back(std::make_shared<COLL>());
951 }
953 TakeHelper(const TakeHelper &) = delete;
954
955 void InitTask(TTreeReader *, unsigned int) {}
956
957 void Exec(unsigned int slot, T &v) { FillColl(v, *fColls[slot]); }
958
959 void Initialize() { /* noop */}
960
961 void Finalize()
962 {
963 auto rColl = fColls[0];
964 for (unsigned int i = 1; i < fColls.size(); ++i) {
965 const auto &coll = fColls[i];
966 const auto end = coll->end();
967 // Use an explicit loop here to prevent compiler warnings introduced by
968 // clang's range-based loop analysis and vector<bool> references.
969 for (auto j = coll->begin(); j != end; j++) {
970 FillColl(*j, *rColl);
971 }
972 }
973 }
974
975 COLL &PartialUpdate(unsigned int slot) { return *fColls[slot].get(); }
976
977 std::string GetActionName() { return "Take"; }
978
979 TakeHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
980 {
981 auto &result = *static_cast<std::shared_ptr<COLL> *>(newResult);
982 result->clear();
983 return TakeHelper(result, fColls.size());
984 }
985};
986
987// Case 2.: The column is not an RVec, the collection is a vector
988// Optimisations, no transformations: just copies.
989template <typename RealT_t, typename T>
990class R__CLING_PTRCHECK(off) TakeHelper<RealT_t, T, std::vector<T>>
991 : public RActionImpl<TakeHelper<RealT_t, T, std::vector<T>>> {
993
994public:
995 using ColumnTypes_t = TypeList<T>;
996 TakeHelper(const std::shared_ptr<std::vector<T>> &resultColl, const unsigned int nSlots)
997 {
998 fColls.emplace_back(resultColl);
999 for (unsigned int i = 1; i < nSlots; ++i) {
1000 auto v = std::make_shared<std::vector<T>>();
1001 v->reserve(1024);
1002 fColls.emplace_back(v);
1003 }
1004 }
1006 TakeHelper(const TakeHelper &) = delete;
1007
1008 void InitTask(TTreeReader *, unsigned int) {}
1009
1010 void Exec(unsigned int slot, T &v) { FillColl(v, *fColls[slot]); }
1011
1012 void Initialize() { /* noop */}
1013
1014 // This is optimised to treat vectors
1015 void Finalize()
1016 {
1017 ULong64_t totSize = 0;
1018 for (auto &coll : fColls)
1019 totSize += coll->size();
1020 auto rColl = fColls[0];
1021 rColl->reserve(totSize);
1022 for (unsigned int i = 1; i < fColls.size(); ++i) {
1023 auto &coll = fColls[i];
1024 rColl->insert(rColl->end(), coll->begin(), coll->end());
1025 }
1026 }
1027
1028 std::vector<T> &PartialUpdate(unsigned int slot) { return *fColls[slot]; }
1029
1030 std::string GetActionName() { return "Take"; }
1031
1032 TakeHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
1033 {
1034 auto &result = *static_cast<std::shared_ptr<std::vector<T>> *>(newResult);
1035 result->clear();
1036 return TakeHelper(result, fColls.size());
1037 }
1038};
1039
1040// Case 3.: The column is a RVec, the collection is not a vector
1041// No optimisations, transformations from RVecs to vectors
1042template <typename RealT_t, typename COLL>
1044 : public RActionImpl<TakeHelper<RealT_t, RVec<RealT_t>, COLL>> {
1046
1047public:
1048 using ColumnTypes_t = TypeList<RVec<RealT_t>>;
1049 TakeHelper(const std::shared_ptr<COLL> &resultColl, const unsigned int nSlots)
1050 {
1051 fColls.emplace_back(resultColl);
1052 for (unsigned int i = 1; i < nSlots; ++i)
1053 fColls.emplace_back(std::make_shared<COLL>());
1054 }
1056 TakeHelper(const TakeHelper &) = delete;
1057
1058 void InitTask(TTreeReader *, unsigned int) {}
1059
1060 void Exec(unsigned int slot, RVec<RealT_t> av) { fColls[slot]->emplace_back(av.begin(), av.end()); }
1061
1062 void Initialize() { /* noop */}
1063
1064 void Finalize()
1065 {
1066 auto rColl = fColls[0];
1067 for (unsigned int i = 1; i < fColls.size(); ++i) {
1068 auto &coll = fColls[i];
1069 for (auto &v : *coll) {
1070 rColl->emplace_back(v);
1071 }
1072 }
1073 }
1074
1075 std::string GetActionName() { return "Take"; }
1076
1077 TakeHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
1078 {
1079 auto &result = *static_cast<std::shared_ptr<COLL> *>(newResult);
1080 result->clear();
1081 return TakeHelper(result, fColls.size());
1082 }
1083};
1084
1085// Case 4.: The column is an RVec, the collection is a vector
1086// Optimisations, transformations from RVecs to vectors
1087template <typename RealT_t>
1088class R__CLING_PTRCHECK(off) TakeHelper<RealT_t, RVec<RealT_t>, std::vector<RealT_t>>
1089 : public RActionImpl<TakeHelper<RealT_t, RVec<RealT_t>, std::vector<RealT_t>>> {
1090
1092
1093public:
1094 using ColumnTypes_t = TypeList<RVec<RealT_t>>;
1095 TakeHelper(const std::shared_ptr<std::vector<std::vector<RealT_t>>> &resultColl, const unsigned int nSlots)
1096 {
1097 fColls.emplace_back(resultColl);
1098 for (unsigned int i = 1; i < nSlots; ++i) {
1099 auto v = std::make_shared<std::vector<RealT_t>>();
1100 v->reserve(1024);
1101 fColls.emplace_back(v);
1102 }
1103 }
1105 TakeHelper(const TakeHelper &) = delete;
1106
1107 void InitTask(TTreeReader *, unsigned int) {}
1108
1109 void Exec(unsigned int slot, RVec<RealT_t> av) { fColls[slot]->emplace_back(av.begin(), av.end()); }
1110
1111 void Initialize() { /* noop */}
1112
1113 // This is optimised to treat vectors
1114 void Finalize()
1115 {
1116 ULong64_t totSize = 0;
1117 for (auto &coll : fColls)
1118 totSize += coll->size();
1119 auto rColl = fColls[0];
1120 rColl->reserve(totSize);
1121 for (unsigned int i = 1; i < fColls.size(); ++i) {
1122 auto &coll = fColls[i];
1123 rColl->insert(rColl->end(), coll->begin(), coll->end());
1124 }
1125 }
1126
1127 std::string GetActionName() { return "Take"; }
1128
1129 TakeHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
1130 {
1131 auto &result = *static_cast<typename decltype(fColls)::value_type *>(newResult);
1132 result->clear();
1133 return TakeHelper(result, fColls.size());
1134 }
1135};
1136
1137// Extern templates for TakeHelper
1138// NOTE: The move-constructor of specializations declared as extern templates
1139// must be defined out of line, otherwise cling fails to find its symbol.
1140template <typename RealT_t, typename T, typename COLL>
1142template <typename RealT_t, typename T>
1144template <typename RealT_t, typename COLL>
1146template <typename RealT_t>
1147TakeHelper<RealT_t, RVec<RealT_t>, std::vector<RealT_t>>::TakeHelper(TakeHelper<RealT_t, RVec<RealT_t>, std::vector<RealT_t>> &&) = default;
1148
1149// External templates are disabled for gcc5 since this version wrongly omits the C++11 ABI attribute
1150#if __GNUC__ > 5
1151extern template class TakeHelper<bool, bool, std::vector<bool>>;
1155extern template class TakeHelper<int, int, std::vector<int>>;
1156extern template class TakeHelper<long, long, std::vector<long>>;
1158extern template class TakeHelper<float, float, std::vector<float>>;
1160#endif
1161
1162template <typename ResultType>
1163class R__CLING_PTRCHECK(off) MinHelper : public RActionImpl<MinHelper<ResultType>> {
1164 std::shared_ptr<ResultType> fResultMin;
1166
1167public:
1168 MinHelper(MinHelper &&) = default;
1169 MinHelper(const std::shared_ptr<ResultType> &minVPtr, const unsigned int nSlots)
1170 : fResultMin(minVPtr), fMins(nSlots, std::numeric_limits<ResultType>::max())
1171 {
1172 }
1173
1174 void Exec(unsigned int slot, ResultType v) { fMins[slot] = std::min(v, fMins[slot]); }
1175
1176 void InitTask(TTreeReader *, unsigned int) {}
1177
1179 void Exec(unsigned int slot, const T &vs)
1180 {
1181 for (auto &&v : vs)
1182 fMins[slot] = std::min(static_cast<ResultType>(v), fMins[slot]);
1183 }
1184
1185 void Initialize() { /* noop */}
1186
1187 void Finalize()
1188 {
1189 *fResultMin = std::numeric_limits<ResultType>::max();
1190 for (auto &m : fMins)
1191 *fResultMin = std::min(m, *fResultMin);
1192 }
1193
1194 // Helper functions for RMergeableValue
1195 std::unique_ptr<RMergeableValueBase> GetMergeableValue() const final
1196 {
1197 return std::make_unique<RMergeableMin<ResultType>>(*fResultMin);
1198 }
1199
1200 ResultType &PartialUpdate(unsigned int slot) { return fMins[slot]; }
1201
1202 std::string GetActionName() { return "Min"; }
1203
1204 MinHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
1205 {
1206 auto &result = *static_cast<std::shared_ptr<ResultType> *>(newResult);
1207 return MinHelper(result, fMins.size());
1208 }
1209};
1210
1211template <typename ResultType>
1212class R__CLING_PTRCHECK(off) MaxHelper : public RActionImpl<MaxHelper<ResultType>> {
1213 std::shared_ptr<ResultType> fResultMax;
1215
1216public:
1217 MaxHelper(MaxHelper &&) = default;
1218 MaxHelper(const MaxHelper &) = delete;
1219 MaxHelper(const std::shared_ptr<ResultType> &maxVPtr, const unsigned int nSlots)
1220 : fResultMax(maxVPtr), fMaxs(nSlots, std::numeric_limits<ResultType>::lowest())
1221 {
1222 }
1223
1224 void InitTask(TTreeReader *, unsigned int) {}
1225 void Exec(unsigned int slot, ResultType v) { fMaxs[slot] = std::max(v, fMaxs[slot]); }
1226
1228 void Exec(unsigned int slot, const T &vs)
1229 {
1230 for (auto &&v : vs)
1231 fMaxs[slot] = std::max(static_cast<ResultType>(v), fMaxs[slot]);
1232 }
1233
1234 void Initialize() { /* noop */}
1235
1236 void Finalize()
1237 {
1238 *fResultMax = std::numeric_limits<ResultType>::lowest();
1239 for (auto &m : fMaxs) {
1240 *fResultMax = std::max(m, *fResultMax);
1241 }
1242 }
1243
1244 // Helper functions for RMergeableValue
1245 std::unique_ptr<RMergeableValueBase> GetMergeableValue() const final
1246 {
1247 return std::make_unique<RMergeableMax<ResultType>>(*fResultMax);
1248 }
1249
1250 ResultType &PartialUpdate(unsigned int slot) { return fMaxs[slot]; }
1251
1252 std::string GetActionName() { return "Max"; }
1253
1254 MaxHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
1255 {
1256 auto &result = *static_cast<std::shared_ptr<ResultType> *>(newResult);
1257 return MaxHelper(result, fMaxs.size());
1258 }
1259};
1260
1261template <typename ResultType>
1262class R__CLING_PTRCHECK(off) SumHelper : public RActionImpl<SumHelper<ResultType>> {
1263 std::shared_ptr<ResultType> fResultSum;
1266
1267 /// Evaluate neutral element for this type and the sum operation.
1268 /// This is assumed to be any_value - any_value if operator- is defined
1269 /// for the type, otherwise a default-constructed ResultType{} is used.
1270 template <typename T = ResultType>
1271 auto NeutralElement(const T &v, int /*overloadresolver*/) -> decltype(v - v)
1272 {
1273 return v - v;
1274 }
1275
1276 template <typename T = ResultType, typename Dummy = int>
1277 ResultType NeutralElement(const T &, Dummy) // this overload has lower priority thanks to the template arg
1278 {
1279 return ResultType{};
1280 }
1281
1282public:
1283 SumHelper(SumHelper &&) = default;
1284 SumHelper(const SumHelper &) = delete;
1285 SumHelper(const std::shared_ptr<ResultType> &sumVPtr, const unsigned int nSlots)
1288 {
1289 }
1290 void InitTask(TTreeReader *, unsigned int) {}
1291
1292 void Exec(unsigned int slot, ResultType x)
1293 {
1294 // Kahan Sum:
1296 ResultType t = fSums[slot] + y;
1297 fCompensations[slot] = (t - fSums[slot]) - y;
1298 fSums[slot] = t;
1299 }
1300
1302 void Exec(unsigned int slot, const T &vs)
1303 {
1304 for (auto &&v : vs) {
1305 Exec(slot, v);
1306 }
1307 }
1308
1309 void Initialize() { /* noop */}
1310
1311 void Finalize()
1312 {
1317 for (auto &m : fSums) {
1318 // Kahan Sum:
1319 y = m - compensation;
1320 t = sum + y;
1321 compensation = (t - sum) - y;
1322 sum = t;
1323 }
1324 *fResultSum += sum;
1325 }
1326
1327 // Helper functions for RMergeableValue
1328 std::unique_ptr<RMergeableValueBase> GetMergeableValue() const final
1329 {
1330 return std::make_unique<RMergeableSum<ResultType>>(*fResultSum);
1331 }
1332
1333 ResultType &PartialUpdate(unsigned int slot) { return fSums[slot]; }
1334
1335 std::string GetActionName() { return "Sum"; }
1336
1337 SumHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
1338 {
1339 auto &result = *static_cast<std::shared_ptr<ResultType> *>(newResult);
1340 *result = NeutralElement(*result, -1);
1341 return SumHelper(result, fSums.size());
1342 }
1343};
1344
1345class R__CLING_PTRCHECK(off) MeanHelper : public RActionImpl<MeanHelper> {
1346 std::shared_ptr<double> fResultMean;
1347 std::vector<ULong64_t> fCounts;
1348 std::vector<double> fSums;
1349 std::vector<double> fPartialMeans;
1350 std::vector<double> fCompensations;
1351
1352public:
1353 MeanHelper(const std::shared_ptr<double> &meanVPtr, const unsigned int nSlots);
1354 MeanHelper(MeanHelper &&) = default;
1355 MeanHelper(const MeanHelper &) = delete;
1356 void InitTask(TTreeReader *, unsigned int) {}
1357 void Exec(unsigned int slot, double v);
1358
1360 void Exec(unsigned int slot, const T &vs)
1361 {
1362 for (auto &&v : vs) {
1363
1364 fCounts[slot]++;
1365 // Kahan Sum:
1366 double y = v - fCompensations[slot];
1367 double t = fSums[slot] + y;
1368 fCompensations[slot] = (t - fSums[slot]) - y;
1369 fSums[slot] = t;
1370 }
1371 }
1372
1373 void Initialize() { /* noop */}
1374
1375 void Finalize();
1376
1377 // Helper functions for RMergeableValue
1378 std::unique_ptr<RMergeableValueBase> GetMergeableValue() const final
1379 {
1380 const ULong64_t counts = std::accumulate(fCounts.begin(), fCounts.end(), 0ull);
1381 return std::make_unique<RMergeableMean>(*fResultMean, counts);
1382 }
1383
1384 double &PartialUpdate(unsigned int slot);
1385
1386 std::string GetActionName() { return "Mean"; }
1387
1388 MeanHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
1389 {
1390 auto &result = *static_cast<std::shared_ptr<double> *>(newResult);
1391 return MeanHelper(result, fSums.size());
1392 }
1393};
1394
1395class R__CLING_PTRCHECK(off) StdDevHelper : public RActionImpl<StdDevHelper> {
1396 // Number of subsets of data
1397 unsigned int fNSlots;
1398 std::shared_ptr<double> fResultStdDev;
1399 // Number of element for each slot
1400 std::vector<ULong64_t> fCounts;
1401 // Mean of each slot
1402 std::vector<double> fMeans;
1403 // Squared distance from the mean
1404 std::vector<double> fDistancesfromMean;
1405
1406public:
1407 StdDevHelper(const std::shared_ptr<double> &meanVPtr, const unsigned int nSlots);
1408 StdDevHelper(StdDevHelper &&) = default;
1409 StdDevHelper(const StdDevHelper &) = delete;
1410 void InitTask(TTreeReader *, unsigned int) {}
1411 void Exec(unsigned int slot, double v);
1412
1414 void Exec(unsigned int slot, const T &vs)
1415 {
1416 for (auto &&v : vs) {
1417 Exec(slot, v);
1418 }
1419 }
1420
1421 void Initialize() { /* noop */}
1422
1423 void Finalize();
1424
1425 // Helper functions for RMergeableValue
1426 std::unique_ptr<RMergeableValueBase> GetMergeableValue() const final
1427 {
1428 const ULong64_t counts = std::accumulate(fCounts.begin(), fCounts.end(), 0ull);
1429 const Double_t mean =
1430 std::inner_product(fMeans.begin(), fMeans.end(), fCounts.begin(), 0.) / static_cast<Double_t>(counts);
1431 return std::make_unique<RMergeableStdDev>(*fResultStdDev, counts, mean);
1432 }
1433
1434 std::string GetActionName() { return "StdDev"; }
1435
1436 StdDevHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
1437 {
1438 auto &result = *static_cast<std::shared_ptr<double> *>(newResult);
1439 return StdDevHelper(result, fCounts.size());
1440 }
1441};
1442
1443template <typename PrevNodeType>
1444class R__CLING_PTRCHECK(off) DisplayHelper : public RActionImpl<DisplayHelper<PrevNodeType>> {
1445private:
1447 std::shared_ptr<Display_t> fDisplayerHelper;
1448 std::shared_ptr<PrevNodeType> fPrevNode;
1449 size_t fEntriesToProcess;
1450
1451public:
1452 DisplayHelper(size_t nRows, const std::shared_ptr<Display_t> &d, const std::shared_ptr<PrevNodeType> &prevNode)
1453 : fDisplayerHelper(d), fPrevNode(prevNode), fEntriesToProcess(nRows)
1454 {
1455 }
1456 DisplayHelper(DisplayHelper &&) = default;
1457 DisplayHelper(const DisplayHelper &) = delete;
1458 void InitTask(TTreeReader *, unsigned int) {}
1459
1460 template <typename... Columns>
1461 void Exec(unsigned int, Columns &... columns)
1462 {
1463 if (fEntriesToProcess == 0)
1464 return;
1465
1466 fDisplayerHelper->AddRow(columns...);
1467 --fEntriesToProcess;
1468
1469 if (fEntriesToProcess == 0) {
1470 // No more entries to process. Send a one-time signal that this node
1471 // of the graph is done. It is important that the 'StopProcessing'
1472 // method is only called once from this helper, otherwise it would seem
1473 // like more than one operation has completed its work.
1474 fPrevNode->StopProcessing();
1475 }
1476 }
1477
1478 void Initialize() {}
1479
1480 void Finalize() {}
1481
1482 std::string GetActionName() { return "Display"; }
1483};
1484
1485template <typename Acc, typename Merge, typename R, typename T, typename U,
1486 bool MustCopyAssign = std::is_same<R, U>::value>
1488 : public RActionImpl<AggregateHelper<Acc, Merge, R, T, U, MustCopyAssign>> {
1490 Merge fMerge;
1491 std::shared_ptr<U> fResult;
1493
1494public:
1495 using ColumnTypes_t = TypeList<T>;
1496
1497 AggregateHelper(Acc &&f, Merge &&m, const std::shared_ptr<U> &result, const unsigned int nSlots)
1498 : fAggregate(std::move(f)), fMerge(std::move(m)), fResult(result), fAggregators(nSlots, *result)
1499 {
1500 }
1501
1502 AggregateHelper(Acc &f, Merge &m, const std::shared_ptr<U> &result, const unsigned int nSlots)
1503 : fAggregate(f), fMerge(m), fResult(result), fAggregators(nSlots, *result)
1504 {
1505 }
1506
1507 AggregateHelper(AggregateHelper &&) = default;
1508 AggregateHelper(const AggregateHelper &) = delete;
1509
1510 void InitTask(TTreeReader *, unsigned int) {}
1511
1512 template <bool MustCopyAssign_ = MustCopyAssign, std::enable_if_t<MustCopyAssign_, int> = 0>
1513 void Exec(unsigned int slot, const T &value)
1514 {
1516 }
1517
1518 template <bool MustCopyAssign_ = MustCopyAssign, std::enable_if_t<!MustCopyAssign_, int> = 0>
1519 void Exec(unsigned int slot, const T &value)
1520 {
1522 }
1523
1524 void Initialize() { /* noop */}
1525
1527 bool MergeAll = std::is_same<void, MergeRet>::value>
1528 std::enable_if_t<MergeAll, void> Finalize()
1529 {
1530 fMerge(fAggregators);
1531 *fResult = fAggregators[0];
1532 }
1533
1535 bool MergeTwoByTwo = std::is_same<U, MergeRet>::value>
1536 std::enable_if_t<MergeTwoByTwo, void> Finalize(...) // ... needed to let compiler distinguish overloads
1537 {
1538 for (const auto &acc : fAggregators)
1539 *fResult = fMerge(*fResult, acc);
1540 }
1541
1542 U &PartialUpdate(unsigned int slot) { return fAggregators[slot]; }
1543
1544 std::string GetActionName() { return "Aggregate"; }
1545
1546 AggregateHelper MakeNew(void *newResult, std::string_view /*variation*/ = "nominal")
1547 {
1548 auto &result = *static_cast<std::shared_ptr<U> *>(newResult);
1549 return AggregateHelper(fAggregate, fMerge, result, fAggregators.size());
1550 }
1551};
1552
1553} // end of NS RDF
1554} // end of NS Internal
1555} // end of NS ROOT
1556
1557/// \endcond
1558
1559#endif
PyObject * fCallable
Handle_t Display_t
Display handle.
Definition GuiTypes.h:27
#define d(i)
Definition RSha256.hxx:102
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define g(i)
Definition RSha256.hxx:105
#define h(i)
Definition RSha256.hxx:106
#define R(a, b, c, d, e, f, g, h, i)
Definition RSha256.hxx:110
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.
Definition RtypesCore.h:73
unsigned long long ULong64_t
Portable unsigned long integer 8 bytes.
Definition RtypesCore.h:84
#define X(type, name)
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
Definition TStringLong.h:20
TTime operator*(const TTime &t1, const TTime &t2)
Definition TTime.h:85
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.
Definition RHist.hxx:64
This class is the textual representation of the content of a columnar dataset.
Definition RDisplay.hxx:65
const_iterator begin() const
const_iterator end() const
A "std::vector"-like collection of values implementing handy operation to analyse them.
Definition RVec.hxx:1524
Collection abstract base class.
Definition TCollection.h:65
TGraph with asymmetric error bars.
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:109
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:81
Statistical variable, defined by its mean and variance (RMS).
Definition TStatistic.h:33
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition TTreeReader.h:46
RooCmdArg Columns(Int_t ncol)
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
#define I(x, y, z)
#define H(x, y, z)
CPYCPPYY_EXTERN bool Exec(const std::string &cmd)
Definition API.cxx:455
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)
Definition Utils.hxx:235
void UnsetDirectoryIfPossible(TH1 *h)
auto FillThreadSafe(T &histo, Args... args) -> decltype(histo.FillThreadSafe(args...), void())
Entrypoint for thread-safe filling from RDataFrame.
Definition TH3.h:39
ROOT type_traits extensions.
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition tmvaglob.cxx:176
A weight for filling histograms.
Definition RWeight.hxx:17
TMarker m
Definition textangle.C:8
TLine l
Definition textangle.C:4
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2338