Df 0 2 3_Aggregate

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

Author: Enrico Guiraud, Danilo Piparo (CERN), Massimo Tumolo (Politecnico di Torino)
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, June 15, 2021 at 07:20 AM.

Column to be aggregated

In [1]:
const std::string columnName = "x";

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.

In [2]:
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.

In [3]:
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.

In [4]:
double initValue = 1.;

Multiplies all elements of the column "x"

In [5]:
auto result = d.Aggregate(aggregator, merger, columnName, initValue);

std::cout << *result << std::endl;

Draw all canvases

In [6]: