{
"cells": [
{
"cell_type": "markdown",
"id": "0965224c",
"metadata": {},
"source": [
"# df039_RResultPtr_basics\n",
"Usage of RResultPtr.\n",
"\n",
"This tutorial illustrates what is the difference between lazy and immediate results and how to use either of them in RDataFrame.\n",
"\"Lazy\" or deferred results are only produced once they are accessed. This allows for declaring multiple desired results, and producing\n",
"them in a single run of the event loop.\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Stephan Hageboeck (CERN) \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:10 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "c07d5dbd",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:10:23.261134Z",
"iopub.status.busy": "2026-05-19T20:10:23.261033Z",
"iopub.status.idle": "2026-05-19T20:10:23.294776Z",
"shell.execute_reply": "2026-05-19T20:10:23.273357Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \n",
"#include \n",
"\n",
"#include "
]
},
{
"cell_type": "markdown",
"id": "01e51cd9",
"metadata": {},
"source": [
" A function that adds a \"lazy\" histogram to a computation graph.\n",
"The event loop will not run if only the RResultPtr is declared.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7288fecc",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:10:23.309004Z",
"iopub.status.busy": "2026-05-19T20:10:23.308868Z",
"iopub.status.idle": "2026-05-19T20:10:23.418973Z",
"shell.execute_reply": "2026-05-19T20:10:23.418460Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"ROOT::RDF::RResultPtr histoLater(ROOT::RDF::RNode & rdf) {\n",
" return rdf.Histo1D({\"Histo2\", \"Histogram running later\", 10, 0, 20}, {\"x\"});\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "90a4c3e3",
"metadata": {},
"source": [
" A function that immediately produces a result.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "db0bb87f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:10:23.426103Z",
"iopub.status.busy": "2026-05-19T20:10:23.425982Z",
"iopub.status.idle": "2026-05-19T20:10:23.464094Z",
"shell.execute_reply": "2026-05-19T20:10:23.463634Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"std::shared_ptr histoNow(ROOT::RDF::RNode & rdf) {\n",
" auto histo = rdf.Histo1D({\"Histo2\", \"Histogram running later\", 10, 0, 20}, {\"x\"});\n",
" return histo.GetSharedPtr();\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "8d4b22e2",
"metadata": {},
"source": [
"Create a simple dataframe that fills event numbers into a histogram."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "67053024",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:10:23.486185Z",
"iopub.status.busy": "2026-05-19T20:10:23.486053Z",
"iopub.status.idle": "2026-05-19T20:10:24.392670Z",
"shell.execute_reply": "2026-05-19T20:10:24.379251Z"
}
},
"outputs": [],
"source": [
"ROOT::RDataFrame bare_rdf(10);\n",
"auto rdf = bare_rdf.Define(\"x\", [&](unsigned long long entry) -> unsigned int {\n",
" if (entry == 0) std::cout << \"Event loop is running\\n\";\n",
" return entry;\n",
" }, {\"rdfentry_\"});"
]
},
{
"cell_type": "markdown",
"id": "f36a34d2",
"metadata": {},
"source": [
"Book a histogram action. This will be stored as RResultPtr.\n",
"The action won't run yet."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "22c3ccb2",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:10:24.421282Z",
"iopub.status.busy": "2026-05-19T20:10:24.421102Z",
"iopub.status.idle": "2026-05-19T20:10:24.638813Z",
"shell.execute_reply": "2026-05-19T20:10:24.638258Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Declared histo1\n"
]
}
],
"source": [
"ROOT::RDF::RResultPtr histo1 = rdf.Histo1D({\"Histo1\", \"Histogram\", 10, 0, 10}, {\"x\"});\n",
"std::cout << \"Declared histo1\\n\";"
]
},
{
"cell_type": "markdown",
"id": "63977290",
"metadata": {},
"source": [
"When RDF results are declared in functions, one has to choose if one wants run it to immediately or lazily.\n",
"To run the event loop in a lazy fashion, return RResultPtr. This is equivalent to histo1 above, but happens\n",
"inside a function."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f5c8adc7",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:10:24.654282Z",
"iopub.status.busy": "2026-05-19T20:10:24.654138Z",
"iopub.status.idle": "2026-05-19T20:10:24.863441Z",
"shell.execute_reply": "2026-05-19T20:10:24.863080Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Declared histo2\n"
]
}
],
"source": [
"auto rNode = ROOT::RDF::AsRNode(rdf);\n",
"ROOT::RDF::RResultPtr histo2 = histoLater(rNode);\n",
"std::cout << \"Declared histo2\\n\";"
]
},
{
"cell_type": "markdown",
"id": "ac66ff8f",
"metadata": {},
"source": [
"If the function should produce the result immediately, a shared_ptr to the underlying result should be returned."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "906ab3bd",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:10:24.866129Z",
"iopub.status.busy": "2026-05-19T20:10:24.865991Z",
"iopub.status.idle": "2026-05-19T20:10:25.835789Z",
"shell.execute_reply": "2026-05-19T20:10:25.835190Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Event loop is running\n",
"Declared histo3\n"
]
}
],
"source": [
"std::shared_ptr histo3 = histoNow(rNode);\n",
"std::cout << \"Declared histo3\\n\";"
]
}
],
"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
}