{
"cells": [
{
"cell_type": "markdown",
"id": "b2a09c3b",
"metadata": {},
"source": [
"# df023_aggregate\n",
"Use the Aggregate action to specify arbitrary data aggregations.\n",
"\n",
"This tutorial shows how to use the Aggregate action to evaluate the product of all the elements of a column.\n",
"This operation may be performed using a Reduce action, however aggregate is used for the sake of the tutorial\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Enrico Guiraud, Danilo Piparo (CERN), Massimo Tumolo (Politecnico di Torino) \n",
"This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:09 PM."
]
},
{
"cell_type": "markdown",
"id": "80b741a0",
"metadata": {},
"source": [
"Column to be aggregated"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "34a747b4",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:09:58.546416Z",
"iopub.status.busy": "2026-05-19T20:09:58.546290Z",
"iopub.status.idle": "2026-05-19T20:09:58.932441Z",
"shell.execute_reply": "2026-05-19T20:09:58.931903Z"
}
},
"outputs": [],
"source": [
"const std::string columnName = \"x\";\n",
"\n",
"ROOT::EnableImplicitMT(2);\n",
"auto rdf = ROOT::RDataFrame(5);\n",
"auto d = rdf.Define(columnName, \"rdfentry_ + 1.\");"
]
},
{
"cell_type": "markdown",
"id": "91a29364",
"metadata": {},
"source": [
"Aggregator function. It receives an accumulator (acc) and a column value (x). The variable acc is shared among the\n",
"calls, so the function has to specify how the value has to be aggregated in the accumulator."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b32ccb7c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:09:58.935318Z",
"iopub.status.busy": "2026-05-19T20:09:58.935191Z",
"iopub.status.idle": "2026-05-19T20:09:59.140847Z",
"shell.execute_reply": "2026-05-19T20:09:59.139868Z"
}
},
"outputs": [],
"source": [
"auto aggregator = [](double acc, double x) { return acc * x; };"
]
},
{
"cell_type": "markdown",
"id": "eec9c519",
"metadata": {},
"source": [
"If multithread is enabled, the aggregator function will be called by more threads and will produce a vector of\n",
"partial accumulators. The merger function performs the final aggregation of these partial results."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "05004594",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:09:59.142378Z",
"iopub.status.busy": "2026-05-19T20:09:59.142250Z",
"iopub.status.idle": "2026-05-19T20:09:59.344887Z",
"shell.execute_reply": "2026-05-19T20:09:59.343868Z"
}
},
"outputs": [],
"source": [
"auto merger = [](std::vector &accumulators) {\n",
" auto size = accumulators.size();\n",
" for (int i = 1; i < size; ++i) {\n",
" accumulators[0] *= accumulators[i];\n",
" }\n",
"};"
]
},
{
"cell_type": "markdown",
"id": "bf10c613",
"metadata": {},
"source": [
"The accumulator is initialized at this value by every thread."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "19b8dc4b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:09:59.346388Z",
"iopub.status.busy": "2026-05-19T20:09:59.346257Z",
"iopub.status.idle": "2026-05-19T20:09:59.556119Z",
"shell.execute_reply": "2026-05-19T20:09:59.555446Z"
}
},
"outputs": [],
"source": [
"double initValue = 1.;"
]
},
{
"cell_type": "markdown",
"id": "e10a2e0d",
"metadata": {},
"source": [
"Multiplies all elements of the column \"x\""
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d8190367",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:09:59.558287Z",
"iopub.status.busy": "2026-05-19T20:09:59.558168Z",
"iopub.status.idle": "2026-05-19T20:10:00.774461Z",
"shell.execute_reply": "2026-05-19T20:10:00.773558Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"120\n"
]
}
],
"source": [
"auto result = d.Aggregate(aggregator, merger, columnName, initValue);\n",
"\n",
"std::cout << *result << std::endl;"
]
},
{
"cell_type": "markdown",
"id": "9401e5be",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f99ff641",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:10:00.775936Z",
"iopub.status.busy": "2026-05-19T20:10:00.775805Z",
"iopub.status.idle": "2026-05-19T20:10:00.987824Z",
"shell.execute_reply": "2026-05-19T20:10:00.987413Z"
}
},
"outputs": [],
"source": [
"gROOT->GetListOfCanvases()->Draw()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ROOT C++",
"language": "c++",
"name": "root"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".C",
"mimetype": " text/x-c++src",
"name": "c++"
}
},
"nbformat": 4,
"nbformat_minor": 5
}