{
"cells": [
{
"cell_type": "markdown",
"id": "578fa0d9",
"metadata": {},
"source": [
"# df040_RResultPtr_lifetimeManagement\n",
"Usage of RResultPtr: Lifetime management.\n",
"\n",
"This tutorial illustrates how to manage the lifetime of RDataFrame results.\n",
"When RDataFrame results are declared in functions (or scopes in general), \n",
"they are destroyed at the end of the scope.\n",
"To prevent this, one needs to copy the RResultPtr or obtain a copy of its\n",
"underlying shared_ptr.\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": null,
"id": "0e8f9c6c",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \n",
"#include \n",
"#include \n",
"#include \n",
"#include \n",
"#include \n",
"\n",
"TCanvas *c1, *c2;"
]
},
{
"cell_type": "markdown",
"id": "ad12e520",
"metadata": {},
"source": [
"Create a simple dataframe that fills random numbers into histograms."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "daf6257c",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"ROOT::RDataFrame bare_rdf(10);\n",
"std::mt19937 generator{1};\n",
"std::normal_distribution gaus{5., 1.};\n",
"auto rdf = bare_rdf.Define(\"x\", [&]() -> double {\n",
" return gaus(generator);\n",
" }, {});\n",
"\n",
"ROOT::RDF::TH1DModel histoModel{\"Histo\", \"Histo;x\", 10, 0, 10};"
]
},
{
"cell_type": "markdown",
"id": "ce5dcc04",
"metadata": {},
"source": [
"Keeping the results alive is vital when they are passed to other entities or when they are drawn.\n",
"Compare the following situations:"
]
},
{
"cell_type": "markdown",
"id": "64fb8506",
"metadata": {},
"source": [
"1. The wrong way (the result ht is destroyed at the end of the loop body):"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5a9da97",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"THStack histStack1(\"histStack1\", \"Stacking result histograms (wrong way)\");\n",
"for(int i=0; i<2; i++) {\n",
" auto ht = rdf.Histo1D(histoModel, {\"x\"});\n",
" ht->SetFillColor(kBlue+i);\n",
" histStack1.Add(ht.GetPtr()); // Wrong, this histogram will not survive\n",
"}\n",
"c1 = new TCanvas(\"c1\", \"THStack without obtaining a shared_ptr (wrong)\");\n",
"histStack1.DrawClone();\n",
"c1->Draw();"
]
},
{
"cell_type": "markdown",
"id": "e0fc1236",
"metadata": {},
"source": [
"2. The right way: Results survive because we copy the shared_ptr:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1c6d82e",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"THStack histStack2(\"histStack2\", \"THStack with shared_ptr (correct way)\");\n",
"std::vector> results;\n",
"for(int i=0; i<2; i++) {\n",
" auto ht = rdf.Histo1D(histoModel, {\"x\"});\n",
" ht->SetFillColor(kBlue+2*i);\n",
" histStack2.Add(ht.GetPtr());\n",
" results.push_back(ht.GetSharedPtr()); // Makes the histogram survive\n",
"}\n",
"c2 = new TCanvas(\"c2\", \"Drawing with obtaining a shared_ptr (right)\");\n",
"histStack2.DrawClone();\n",
"c2->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
}