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