Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
df023_aggregate.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_dataframe
3/// \notebook
4/// Use the Aggregate action to specify arbitrary data aggregations.
5///
6/// This tutorial shows how to use the Aggregate action to evaluate the product of all the elements of a column.
7/// This operation may be performed using a Reduce action, however aggregate is used for the sake of the tutorial
8///
9/// \macro_code
10/// \macro_output
11///
12/// \date July 2018
13/// \authors Enrico Guiraud, Danilo Piparo (CERN), Massimo Tumolo (Politecnico di Torino)
14
15void df023_aggregate()
16{
17
18 // Column to be aggregated
19 const std::string columnName = "x";
20
22 auto rdf = ROOT::RDataFrame(5);
23 auto d = rdf.Define(columnName, "rdfentry_ + 1.");
24
25 // Aggregator function. It receives an accumulator (acc) and a column value (x). The variable acc is shared among the
26 // calls, so the function has to specify how the value has to be aggregated in the accumulator.
27 auto aggregator = [](double acc, double x) { return acc * x; };
28
29 // If multithread is enabled, the aggregator function will be called by more threads and will produce a vector of
30 // partial accumulators. The merger function performs the final aggregation of these partial results.
31 auto merger = [](std::vector<double> &accumulators) {
32 auto size = accumulators.size();
33 for (int i = 1; i < size; ++i) {
34 accumulators[0] *= accumulators[i];
35 }
36 };
37
38 // The accumulator is initialized at this value by every thread.
39 double initValue = 1.;
40
41 // Multiplies all elements of the column "x"
42 auto result = d.Aggregate(aggregator, merger, columnName, initValue);
43
44 std::cout << *result << std::endl;
45}
#define d(i)
Definition RSha256.hxx:102
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
Double_t x[n]
Definition legend1.C:17
void EnableImplicitMT(UInt_t numthreads=0)
Enable ROOT's implicit multi-threading for all objects and methods that provide an internal paralleli...
Definition TROOT.cxx:539