-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathdf023_aggregate.C
45 lines (37 loc) · 1.65 KB
/
df023_aggregate.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/// \file
/// \ingroup tutorial_dataframe
/// \notebook
/// Use the Aggregate action to specify arbitrary data aggregations.
///
/// This tutorial shows how to use the Aggregate action to evaluate the product of all the elements of a column.
/// This operation may be performed using a Reduce action, however aggregate is used for the sake of the tutorial
///
/// \macro_code
/// \macro_output
///
/// \date July 2018
/// \authors Enrico Guiraud, Danilo Piparo (CERN), Massimo Tumolo (Politecnico di Torino)
void df023_aggregate()
{
// Column to be aggregated
const std::string columnName = "x";
ROOT::EnableImplicitMT(2);
auto rdf = ROOT::RDataFrame(5);
auto d = rdf.Define(columnName, "rdfentry_ + 1.");
// Aggregator function. It receives an accumulator (acc) and a column value (x). The variable acc is shared among the
// calls, so the function has to specify how the value has to be aggregated in the accumulator.
auto aggregator = [](double acc, double x) { return acc * x; };
// If multithread is enabled, the aggregator function will be called by more threads and will produce a vector of
// partial accumulators. The merger function performs the final aggregation of these partial results.
auto merger = [](std::vector<double> &accumulators) {
auto size = accumulators.size();
for (int i = 1; i < size; ++i) {
accumulators[0] *= accumulators[i];
}
};
// The accumulator is initialized at this value by every thread.
double initValue = 1.;
// Multiplies all elements of the column "x"
auto result = d.Aggregate(aggregator, merger, columnName, initValue);
std::cout << *result << std::endl;
}