Df 0 2 5_ R Node

Manipulating RDF objects in functions, loops and conditional branches

Each RDataFrame object has its own type. It helps with performance, but sometimes it gets in the way of writing simple code that manages RDF objects. Luckily, every RDF object can be converted to the generic RNode type. This tutorial shows how to take advantage of RNode to easily manipulate RDataFrames.

Author: Enrico Guiraud
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Monday, July 06, 2020 at 11:39 AM.

/ a generic function that takes an rdf object and applies a string filter

In [1]:
ROOT::RDF::RNode AddFilter(ROOT::RDF::RNode node, string_view filterStr)
{
   return node.Filter(filterStr);
}
In [2]:
ROOT::RDataFrame df(8);

Using the generic addfilter helper function defined above: rnode in, rnode out

In [3]:
auto f1 = AddFilter(df, "rdfentry_ > 0");
auto f2 = f1.Filter([](ULong64_t e) { return e > 1; }, {"rdfentry_"});

Conditionally applying a filter is simple with root::rdf::rnode

In [4]:
bool someCondition = true;
auto maybe_filtered = ROOT::RDF::RNode(f2);
if (someCondition)
   maybe_filtered = maybe_filtered.Filter("rdfentry_ > 3");

Adding new columns with define in a loop is simple thanks to root::rdf::rnode

In [5]:
auto with_columns = ROOT::RDF::RNode(maybe_filtered);
for (auto i = 0; i < 3; ++i)
   with_columns = with_columns.Define("x" + std::to_string(i), "42");

Rnodes can be used exactly like any other rdf object

In [6]:
std::cout << "Entries passing the selection: " << with_columns.Count().GetValue() << std::endl;
Entries passing the selection: 4

Draw all canvases

In [7]:
gROOT->GetListOfCanvases()->Draw()