Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
df025_RNode.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_dataframe
3/// \notebook
4/// Manipulate RDF objects in functions, loops and conditional branches.
5///
6/// Each RDataFrame object has its own type. It helps with performance,
7/// but sometimes it gets in the way of writing simple code that manages RDF objects.
8/// Luckily, every RDF object can be converted to the generic RNode type.
9/// This tutorial shows how to take advantage of RNode to easily manipulate RDataFrames.
10///
11/// \macro_code
12/// \macro_output
13///
14/// \date June 2020
15/// \authors Danilo Piparo, Enrico Guiraud (CERN)
16
17/// A generic function that takes an RDF object and applies a string filter
18ROOT::RDF::RNode AddFilter(ROOT::RDF::RNode node, string_view filterStr)
19{
20 return node.Filter(filterStr);
21}
22
23void df025_RNode()
24{
25 ROOT::RDataFrame df(8);
26
27 // Using the generic AddFilter helper function defined above: RNode in, RNode out
28 auto f1 = AddFilter(df, "rdfentry_ > 0");
29 auto f2 = f1.Filter([](ULong64_t e) { return e > 1; }, {"rdfentry_"});
30
31 // Conditionally applying a filter is simple with ROOT::RDF::RNode
32 bool someCondition = true;
33 auto maybe_filtered = ROOT::RDF::RNode(f2);
34 if (someCondition)
35 maybe_filtered = maybe_filtered.Filter("rdfentry_ > 3");
36
37 // Adding new columns with Define in a loop is simple thanks to ROOT::RDF::RNode
38 auto with_columns = ROOT::RDF::RNode(maybe_filtered);
39 for (auto i = 0; i < 3; ++i)
40 with_columns = with_columns.Define("x" + std::to_string(i), "42");
41
42 // RNodes can be used exactly like any other RDF object
43 std::cout << "Entries passing the selection: " << with_columns.Count().GetValue() << std::endl;
44}
#define e(i)
Definition RSha256.hxx:103
unsigned long long ULong64_t
Definition RtypesCore.h:81
The public interface to the RDataFrame federation of classes.
RInterface< RDFDetail::RFilter< F, Proxied >, DS_t > Filter(F f, const ColumnNames_t &columns={}, std::string_view name="")
Append a filter to the call graph.
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
TF1 * f1
Definition legend1.C:11
RInterface<::ROOT::Detail::RDF::RNodeBase, void > RNode