Usage of RResultPtr: Lifetime management.
This tutorial illustrates how to manage the lifetime of RDataFrame results. When RDataFrame results are declared in functions (or scopes in general), they are destroyed at the end of the scope. To prevent this, one needs to copy the RResultPtr or obtain a copy of its underlying shared_ptr.
#include <random>
#include <vector>
void df040_RResultPtr_lifetimeManagement()
{
std::mt19937 generator{1};
std::normal_distribution gaus{5., 1.};
auto rdf = bare_rdf.Define("x", [&]() -> double {
return gaus(generator);
}, {});
THStack histStack1(
"histStack1",
"Stacking result histograms (wrong way)");
for(int i=0; i<2; i++) {
auto ht = rdf.Histo1D(histoModel, {"x"});
ht->SetFillColor(
kBlue+i);
histStack1.Add(ht.GetPtr());
}
c1 =
new TCanvas(
"c1",
"THStack without obtaining a shared_ptr (wrong)");
histStack1.DrawClone();
THStack histStack2(
"histStack2",
"THStack with shared_ptr (correct way)");
std::vector<std::shared_ptr<TH1D>> results;
for(int i=0; i<2; i++) {
auto ht = rdf.Histo1D(histoModel, {"x"});
ht->SetFillColor(
kBlue+2*i);
histStack2.Add(ht.GetPtr());
results.push_back(ht.GetSharedPtr());
}
c2 =
new TCanvas(
"c2",
"Drawing with obtaining a shared_ptr (right)");
histStack2.DrawClone();
}
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
A struct which stores some basic parameters of a TH1D.
- Date
- 2025
- Author
- Stephan Hageboeck (CERN)
Definition in file df040_RResultPtr_lifetimeManagement.C.