Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RDataFrame.cxx
Go to the documentation of this file.
1// Author: Enrico Guiraud, Danilo Piparo CERN 12/2016
2
3/*************************************************************************
4 * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
12#include "ROOT/RDataFrame.hxx"
13#include "ROOT/RDataSource.hxx"
17#include "ROOT/RDF/Utils.hxx"
18#include <string_view>
19#include "TChain.h"
20#include "TDirectory.h"
21#include "RtypesCore.h" // for ULong64_t
22#include "TTree.h"
23
24#include <fstream> // std::ifstream
25#include <nlohmann/json.hpp> // nlohmann::json::parse
26#include <memory> // for make_shared, allocator, shared_ptr
27#include <ostream> // ostringstream
28#include <stdexcept>
29#include <string>
30#include <vector>
31
32// clang-format off
33/**
34* \class ROOT::RDataFrame
35* \ingroup dataframe
36* \brief ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree , CSV and other data formats, in C++ or Python.
37
38In addition, multi-threading and other low-level optimisations allow users to exploit all the resources available
39on their machines completely transparently.<br>
40Skip to the [class reference](#reference) or keep reading for the user guide.
41
42In a nutshell:
43~~~{.cpp}
44ROOT::EnableImplicitMT(); // Tell ROOT you want to go parallel
45ROOT::RDataFrame d("myTree", "file_*.root"); // Interface to TTree and TChain
46auto myHisto = d.Histo1D("Branch_A"); // This books the (lazy) filling of a histogram
47myHisto->Draw(); // Event loop is run here, upon first access to a result
48~~~
49
50Calculations are expressed in terms of a type-safe *functional chain of actions and transformations*, RDataFrame takes
51care of their execution. The implementation automatically puts in place several low level optimisations such as
52multi-thread parallelization and caching.
53
54\htmlonly
55<a href="https://doi.org/10.5281/zenodo.260230"><img src="https://zenodo.org/badge/DOI/10.5281/zenodo.260230.svg"
56alt="DOI"></a>
57\endhtmlonly
58
59## For the impatient user
60You can directly see RDataFrame in action in our [tutorials](https://root.cern/doc/master/group__tutorial__dataframe.html), in C++ or Python.
61
62## Table of Contents
63- [Cheat sheet](\ref cheatsheet)
64- [Introduction](\ref introduction)
65- [Crash course](\ref crash-course)
66- [Working with collections](\ref collections)
67- [Transformations: manipulating data](\ref transformations)
68- [Actions: getting results](\ref actions)
69- [Distributed execution in Python](\ref distrdf)
70- [Performance tips and parallel execution](\ref parallel-execution)
71- [More features](\ref more-features)
72 - [Systematic variations](\ref systematics)
73 - [RDataFrame objects as function arguments and return values](\ref rnode)
74 - [Storing RDataFrame objects in collections](\ref RDFCollections)
75 - [Executing callbacks every N events](\ref callbacks)
76 - [Default column lists](\ref default-branches)
77 - [Special helper columns: `rdfentry_` and `rdfslot_`](\ref helper-cols)
78 - [Just-in-time compilation: column type inference and explicit declaration of column types](\ref jitting)
79 - [User-defined custom actions](\ref generic-actions)
80 - [Dataset joins with friend trees](\ref friends)
81 - [Reading data formats other than ROOT trees](\ref other-file-formats)
82 - [Computation graphs (storing and reusing sets of transformations)](\ref callgraphs)
83 - [Visualizing the computation graph](\ref representgraph)
84 - [Activating RDataFrame execution logs](\ref rdf-logging)
85 - [Creating an RDataFrame from a dataset specification file](\ref rdf-from-spec)
86 - [Adding a progress bar](\ref progressbar)
87 - [Working with missing values in the dataset](\ref missing-values)
88- [Efficient analysis in Python](\ref python)
89- <a class="el" href="classROOT_1_1RDataFrame.html#reference" onclick="javascript:toggleInherit('pub_methods_classROOT_1_1RDF_1_1RInterface')">Class reference</a>
90
91\anchor cheatsheet
92## Cheat sheet
93These are the operations which can be performed with RDataFrame.
94
95### Transformations
96Transformations are a way to manipulate the data.
97
98| **Transformation** | **Description** |
99|------------------|--------------------|
100| Alias() | Introduce an alias for a particular column name. |
101| DefaultValueFor() | If the value of the input column is missing, provide a default value instead. |
102| Define() | Create a new column in the dataset. Example usages include adding a column that contains the invariant mass of a particle, or a selection of elements of an array (e.g. only the `pt`s of "good" muons). |
103| DefinePerSample() | Define a new column that is updated when the input sample changes, e.g. when switching tree being processed in a chain. |
104| DefineSlot() | Same as Define(), but the user-defined function must take an extra `unsigned int slot` as its first parameter. `slot` will take a different value, `0` to `nThreads - 1`, for each thread of execution. This is meant as a helper in writing thread-safe Define() transformation when using RDataFrame after ROOT::EnableImplicitMT(). DefineSlot() works just as well with single-thread execution: in that case `slot` will always be `0`. |
105| DefineSlotEntry() | Same as DefineSlot(), but the entry number is passed in addition to the slot number. This is meant as a helper in case the expression depends on the entry number. For details about entry numbers in multi-threaded runs, see [here](\ref helper-cols). |
106| Filter() | Filter rows based on user-defined conditions. |
107| FilterAvailable() | Specialized Filter. If the value of the input column is available, keep the entry, otherwise discard it. |
108| FilterMissing() | Specialized Filter. If the value of the input column is missing, keep the entry, otherwise discard it. |
109| Range() | Filter rows based on entry number (single-thread only). |
110| Redefine() | Overwrite the value and/or type of an existing column. See Define() for more information. |
111| RedefineSlot() | Overwrite the value and/or type of an existing column. See DefineSlot() for more information. |
112| RedefineSlotEntry() | Overwrite the value and/or type of an existing column. See DefineSlotEntry() for more information. |
113| Vary() | Register systematic variations for an existing column. Varied results are then extracted via VariationsFor(). |
114
115
116### Actions
117Actions aggregate data into a result. Each one is described in more detail in the reference guide.
118
119In the following, whenever we say an action "returns" something, we always mean it returns a smart pointer to it. Actions only act on events that pass all preceding filters.
120
121Lazy actions only trigger the event loop when one of the results is accessed for the first time, making it easy to
122produce many different results in one event loop. Instant actions trigger the event loop instantly.
123
124
125| **Lazy action** | **Description** |
126|------------------|-----------------|
127| Aggregate() | Execute a user-defined accumulation operation on the processed column values. |
128| Book() | Book execution of a custom action using a user-defined helper object. |
129| Cache() | Cache column values in memory. Custom columns can be cached as well, filtered entries are not cached. Users can specify which columns to save (default is all). |
130| Count() | Return the number of events processed. Useful e.g. to get a quick count of the number of events passing a Filter. |
131| Display() | Provides a printable representation of the dataset contents. The method returns a ROOT::RDF::RDisplay() instance which can print a tabular representation of the data or return it as a string. |
132| Fill() | Fill a user-defined object with the values of the specified columns, as if by calling `Obj.Fill(col1, col2, ...)`. |
133| Graph() | Fills a TGraph with the two columns provided. If multi-threading is enabled, the order of the points may not be the one expected, it is therefore suggested to sort if before drawing. |
134| GraphAsymmErrors() | Fills a TGraphAsymmErrors. If multi-threading is enabled, the order of the points may not be the one expected, it is therefore suggested to sort if before drawing. |
135| Histo1D(), Histo2D(), Histo3D() | Fill a one-, two-, three-dimensional histogram with the processed column values. |
136| HistoND() | Fill an N-dimensional histogram with the processed column values. |
137| Max() | Return the maximum of processed column values. If the type of the column is inferred, the return type is `double`, the type of the column otherwise.|
138| Mean() | Return the mean of processed column values.|
139| Min() | Return the minimum of processed column values. If the type of the column is inferred, the return type is `double`, the type of the column otherwise.|
140| Profile1D(), Profile2D() | Fill a one- or two-dimensional profile with the column values that passed all filters. |
141| Reduce() | Reduce (e.g. sum, merge) entries using the function (lambda, functor...) passed as argument. The function must have signature `T(T,T)` where `T` is the type of the column. Return the final result of the reduction operation. An optional parameter allows initialization of the result object to non-default values. |
142| Report() | Obtain statistics on how many entries have been accepted and rejected by the filters. See the section on [named filters](#named-filters-and-cutflow-reports) for a more detailed explanation. The method returns a ROOT::RDF::RCutFlowReport instance which can be queried programmatically to get information about the effects of the individual cuts. |
143| Stats() | Return a TStatistic object filled with the input columns. |
144| StdDev() | Return the unbiased standard deviation of the processed column values. |
145| Sum() | Return the sum of the values in the column. If the type of the column is inferred, the return type is `double`, the type of the column otherwise. |
146| Take() | Extract a column from the dataset as a collection of values, e.g. a `std::vector<float>` for a column of type `float`. |
147
148| **Instant action** | **Description** |
149|---------------------|-----------------|
150| Foreach() | Execute a user-defined function on each entry. Users are responsible for the thread-safety of this callable when executing with implicit multi-threading enabled. |
151| ForeachSlot() | Same as Foreach(), but the user-defined function must take an extra `unsigned int slot` as its first parameter. `slot` will take a different value, `0` to `nThreads - 1`, for each thread of execution. This is meant as a helper in writing thread-safe Foreach() actions when using RDataFrame after ROOT::EnableImplicitMT(). ForeachSlot() works just as well with single-thread execution: in that case `slot` will always be `0`. |
152| Snapshot() | Write the processed dataset to disk, in a new TTree and TFile. Custom columns can be saved as well, filtered entries are not saved. Users can specify which columns to save (default is all). Snapshot, by default, overwrites the output file if it already exists. Snapshot() can be made *lazy* setting the appropriate flag in the snapshot options.|
153
154
155### Queries
156
157These operations do not modify the dataframe or book computations but simply return information on the RDataFrame object.
158
159| **Operation** | **Description** |
160|---------------------|-----------------|
161| Describe() | Get useful information describing the dataframe, e.g. columns and their types. |
162| GetColumnNames() | Get the names of all the available columns of the dataset. |
163| GetColumnType() | Return the type of a given column as a string. |
164| GetColumnTypeNamesList() | Return the list of type names of columns in the dataset. |
165| GetDefinedColumnNames() | Get the names of all the defined columns. |
166| GetFilterNames() | Return the names of all filters in the computation graph. |
167| GetNRuns() | Return the number of event loops run by this RDataFrame instance so far. |
168| GetNSlots() | Return the number of processing slots that RDataFrame will use during the event loop (i.e. the concurrency level). |
169| SaveGraph() | Store the computation graph of an RDataFrame in [DOT format (graphviz)](https://en.wikipedia.org/wiki/DOT_(graph_description_language)) for easy inspection. See the [relevant section](\ref representgraph) for details. |
170
171\anchor introduction
172## Introduction
173Users define their analysis as a sequence of operations to be performed on the dataframe object; the framework
174takes care of the management of the loop over entries as well as low-level details such as I/O and parallelization.
175RDataFrame provides methods to perform most common operations required by ROOT analyses;
176at the same time, users can just as easily specify custom code that will be executed in the event loop.
177
178RDataFrame is built with a *modular* and *flexible* workflow in mind, summarised as follows:
179
1801. Construct a dataframe object by specifying a dataset. RDataFrame supports TTree as well as TChain, [CSV files](https://root.cern/doc/master/df014__CSVDataSource_8C.html), [SQLite files](https://root.cern/doc/master/df027__SQliteDependencyOverVersion_8C.html), [RNTuples](https://root.cern/doc/master/structROOT_1_1Experimental_1_1RNTuple.html), and it can be extended to custom data formats. From Python, [NumPy arrays can be imported into RDataFrame](https://root.cern/doc/master/df032__MakeNumpyDataFrame_8py.html) as well.
181
1822. Transform the dataframe by:
183
184 - [Applying filters](https://root.cern/doc/master/classROOT_1_1RDataFrame.html#transformations). This selects only specific rows of the dataset.
185
186 - [Creating custom columns](https://root.cern/doc/master/classROOT_1_1RDataFrame.html#transformations). Custom columns can, for example, contain the results of a computation that must be performed for every row of the dataset.
187
1883. [Produce results](https://root.cern/doc/master/classROOT_1_1RDataFrame.html#actions). *Actions* are used to aggregate data into results. Most actions are *lazy*, i.e. they are not executed on the spot, but registered with RDataFrame and executed only when a result is accessed for the first time.
189
190Make sure to book all transformations and actions before you access the contents of any of the results. This lets RDataFrame accumulate work and then produce all results at the same time, upon first access to any of them.
191
192The following table shows how analyses based on TTreeReader and TTree::Draw() translate to RDataFrame. Follow the
193[crash course](#crash-course) to discover more idiomatic and flexible ways to express analyses with RDataFrame.
194<table>
195<tr>
196 <td>
197 <b>TTreeReader</b>
198 </td>
199 <td>
200 <b>ROOT::RDataFrame</b>
201 </td>
202</tr>
203<tr>
204 <td>
205~~~{.cpp}
206TTreeReader reader("myTree", file);
207TTreeReaderValue<A_t> a(reader, "A");
208TTreeReaderValue<B_t> b(reader, "B");
209TTreeReaderValue<C_t> c(reader, "C");
210while(reader.Next()) {
211 if(IsGoodEvent(*a, *b, *c))
212 DoStuff(*a, *b, *c);
213}
214~~~
215 </td>
216 <td>
217~~~{.cpp}
218ROOT::RDataFrame d("myTree", file, {"A", "B", "C"});
219d.Filter(IsGoodEvent).Foreach(DoStuff);
220~~~
221 </td>
222</tr>
223<tr>
224 <td>
225 <b>TTree::Draw</b>
226 </td>
227 <td>
228 <b>ROOT::RDataFrame</b>
229 </td>
230</tr>
231<tr>
232 <td>
233~~~{.cpp}
234auto *tree = file->Get<TTree>("myTree");
235tree->Draw("x", "y > 2");
236~~~
237 </td>
238 <td>
239~~~{.cpp}
240ROOT::RDataFrame df("myTree", file);
241auto h = df.Filter("y > 2").Histo1D("x");
242h->Draw()
243~~~
244 </td>
245</tr>
246<tr>
247 <td>
248~~~{.cpp}
249tree->Draw("jet_eta", "weight*(event == 1)");
250~~~
251 </td>
252 <td>
253~~~{.cpp}
254df.Filter("event == 1").Histo1D("jet_eta", "weight");
255// or the fully compiled version:
256df.Filter([] (ULong64_t e) { return e == 1; }, {"event"}).Histo1D<RVec<float>>("jet_eta", "weight");
257~~~
258 </td>
259</tr>
260<tr>
261 <td>
262~~~{cpp}
263// object selection: for each event, fill histogram with array of selected pts
264tree->Draw('Muon_pt', 'Muon_pt > 100')
265~~~
266 </td>
267 <td>
268~~~{cpp}
269// with RDF, arrays are read as ROOT::VecOps::RVec objects
270df.Define("good_pt", "Muon_pt[Muon_pt > 100]").Histo1D("good_pt")
271~~~
272 </td>
273</tr>
274</table>
275
276\anchor crash-course
277## Crash course
278All snippets of code presented in the crash course can be executed in the ROOT interpreter. Simply precede them with
279~~~{.cpp}
280using namespace ROOT; // RDataFrame's namespace
281~~~
282which is omitted for brevity. The terms "column" and "branch" are used interchangeably.
283
284### Creating an RDataFrame
285RDataFrame's constructor is where the user specifies the dataset and, optionally, a default set of columns that
286operations should work with. Here are the most common methods to construct an RDataFrame object:
287~~~{.cpp}
288// single file -- all constructors are equivalent
289TFile *f = TFile::Open("file.root");
290TTree *t = f.Get<TTree>("treeName");
291
292RDataFrame d1("treeName", "file.root");
293RDataFrame d2("treeName", f); // same as TTreeReader
294RDataFrame d3(*t);
295
296// multiple files -- all constructors are equivalent
297TChain chain("myTree");
298chain.Add("file1.root");
299chain.Add("file2.root");
300
301RDataFrame d4("myTree", {"file1.root", "file2.root"});
302std::vector<std::string> files = {"file1.root", "file2.root"};
303RDataFrame d5("myTree", files);
304RDataFrame d6("myTree", "file*.root"); // the glob is passed as-is to TChain's constructor
305RDataFrame d7(chain);
306~~~
307Additionally, users can construct an RDataFrame with no data source by passing an integer number. This is the number of rows that
308will be generated by this RDataFrame.
309~~~{.cpp}
310RDataFrame d(10); // a RDF with 10 entries (and no columns/branches, for now)
311d.Foreach([] { static int i = 0; std::cout << i++ << std::endl; }); // silly example usage: count to ten
312~~~
313This is useful to generate simple datasets on the fly: the contents of each event can be specified with Define() (explained below). For example, we have used this method to generate [Pythia](https://pythia.org/) events and write them to disk in parallel (with the Snapshot action).
314
315For data sources other than TTrees and TChains, RDataFrame objects are constructed using ad-hoc factory functions (see e.g. FromCSV(), FromSqlite(), FromArrow()):
316
317~~~{.cpp}
318auto df = ROOT::RDF::FromCSV("input.csv");
319// use df as usual
320~~~
321
322### Filling a histogram
323Let's now tackle a very common task, filling a histogram:
324~~~{.cpp}
325// Fill a TH1D with the "MET" branch
326RDataFrame d("myTree", "file.root");
327auto h = d.Histo1D("MET");
328h->Draw();
329~~~
330The first line creates an RDataFrame associated to the TTree "myTree". This tree has a branch named "MET".
331
332Histo1D() is an *action*; it returns a smart pointer (a ROOT::RDF::RResultPtr, to be precise) to a TH1D histogram filled
333with the `MET` of all events. If the quantity stored in the column is a collection (e.g. a vector or an array), the
334histogram is filled with all vector elements for each event.
335
336You can use the objects returned by actions as if they were pointers to the desired results. There are many other
337possible [actions](\ref cheatsheet), and all their results are wrapped in smart pointers; we'll see why in a minute.
338
339### Applying a filter
340Let's say we want to cut over the value of branch "MET" and count how many events pass this cut. This is one way to do it:
341~~~{.cpp}
342RDataFrame d("myTree", "file.root");
343auto c = d.Filter("MET > 4.").Count(); // computations booked, not run
344std::cout << *c << std::endl; // computations run here, upon first access to the result
345~~~
346The filter string (which must contain a valid C++ expression) is applied to the specified columns for each event;
347the name and types of the columns are inferred automatically. The string expression is required to return a `bool`
348which signals whether the event passes the filter (`true`) or not (`false`).
349
350You can think of your data as "flowing" through the chain of calls, being transformed, filtered and finally used to
351perform actions. Multiple Filter() calls can be chained one after another.
352
353Using string filters is nice for simple things, but they are limited to specifying the equivalent of a single return
354statement or the body of a lambda, so it's cumbersome to use strings with more complex filters. They also add a small
355runtime overhead, as ROOT needs to just-in-time compile the string into C++ code. When more freedom is required or
356runtime performance is very important, a C++ callable can be specified instead (a lambda in the following snippet,
357but it can be any kind of function or even a functor class), together with a list of column names.
358This snippet is analogous to the one above:
359~~~{.cpp}
360RDataFrame d("myTree", "file.root");
361auto metCut = [](double x) { return x > 4.; }; // a C++11 lambda function checking "x > 4"
362auto c = d.Filter(metCut, {"MET"}).Count();
363std::cout << *c << std::endl;
364~~~
365
366An example of a more complex filter expressed as a string containing C++ code is shown below
367
368~~~{.cpp}
369RDataFrame d("myTree", "file.root");
370auto df = d.Define("p", "std::array<double, 4> p{px, py, pz}; return p;")
371 .Filter("double p2 = 0.0; for (auto&& x : p) p2 += x*x; return sqrt(p2) < 10.0;");
372~~~
373
374The code snippet above defines a column `p` that is a fixed-size array using the component column names and then
375filters on its magnitude by looping over its elements. It must be noted that the usage of strings to define columns
376like the one above is currently the only possibility when using PyROOT. When writing expressions as such, only constants
377and data coming from other columns in the dataset can be involved in the code passed as a string. Local variables and
378functions cannot be used, since the interpreter will not know how to find them. When capturing local state is necessary,
379it must first be declared to the ROOT C++ interpreter.
380
381More information on filters and how to use them to automatically generate cutflow reports can be found [below](#Filters).
382
383### Defining custom columns
384Let's now consider the case in which "myTree" contains two quantities "x" and "y", but our analysis relies on a derived
385quantity `z = sqrt(x*x + y*y)`. Using the Define() transformation, we can create a new column in the dataset containing
386the variable "z":
387~~~{.cpp}
388RDataFrame d("myTree", "file.root");
389auto sqrtSum = [](double x, double y) { return sqrt(x*x + y*y); };
390auto zMean = d.Define("z", sqrtSum, {"x","y"}).Mean("z");
391std::cout << *zMean << std::endl;
392~~~
393Define() creates the variable "z" by applying `sqrtSum` to "x" and "y". Later in the chain of calls we refer to
394variables created with Define() as if they were actual tree branches/columns, but they are evaluated on demand, at most
395once per event. As with filters, Define() calls can be chained with other transformations to create multiple custom
396columns. Define() and Filter() transformations can be concatenated and intermixed at will.
397
398As with filters, it is possible to specify new columns as string expressions. This snippet is analogous to the one above:
399~~~{.cpp}
400RDataFrame d("myTree", "file.root");
401auto zMean = d.Define("z", "sqrt(x*x + y*y)").Mean("z");
402std::cout << *zMean << std::endl;
403~~~
404
405Again the names of the columns used in the expression and their types are inferred automatically. The string must be
406valid C++ and it is just-in-time compiled. The process has a small runtime overhead and like with filters it is currently the only possible approach when using PyROOT.
407
408Previously, when showing the different ways an RDataFrame can be created, we showed a constructor that takes a
409number of entries as a parameter. In the following example we show how to combine such an "empty" RDataFrame with Define()
410transformations to create a dataset on the fly. We then save the generated data on disk using the Snapshot() action.
411~~~{.cpp}
412RDataFrame d(100); // an RDF that will generate 100 entries (currently empty)
413int x = -1;
414auto d_with_columns = d.Define("x", [&x] { return ++x; })
415 .Define("xx", [&x] { return x*x; });
416d_with_columns.Snapshot("myNewTree", "newfile.root");
417~~~
418This example is slightly more advanced than what we have seen so far. First, it makes use of lambda captures (a
419simple way to make external variables available inside the body of C++ lambdas) to act on the same variable `x` from
420both Define() transformations. Second, we have *stored* the transformed dataframe in a variable. This is always
421possible, since at each point of the transformation chain users can store the status of the dataframe for further use (more
422on this [below](#callgraphs)).
423
424You can read more about defining new columns [here](#custom-columns).
425
426\image html RDF_Graph.png "A graph composed of two branches, one starting with a filter and one with a define. The end point of a branch is always an action."
427
428
429### Running on a range of entries
430It is sometimes necessary to limit the processing of the dataset to a range of entries. For this reason, the RDataFrame
431offers the concept of ranges as a node of the RDataFrame chain of transformations; this means that filters, columns and
432actions can be concatenated to and intermixed with Range()s. If a range is specified after a filter, the range will act
433exclusively on the entries passing the filter -- it will not even count the other entries! The same goes for a Range()
434hanging from another Range(). Here are some commented examples:
435~~~{.cpp}
436RDataFrame d("myTree", "file.root");
437// Here we store a dataframe that loops over only the first 30 entries in a variable
438auto d30 = d.Range(30);
439// This is how you pick all entries from 15 onwards
440auto d15on = d.Range(15, 0);
441// We can specify a stride too, in this case we pick an event every 3
442auto d15each3 = d.Range(0, 15, 3);
443~~~
444Note that ranges are not available when multi-threading is enabled. More information on ranges is available
445[here](#ranges).
446
447### Executing multiple actions in the same event loop
448As a final example let us apply two different cuts on branch "MET" and fill two different histograms with the "pt_v" of
449the filtered events.
450By now, you should be able to easily understand what is happening:
451~~~{.cpp}
452RDataFrame d("treeName", "file.root");
453auto h1 = d.Filter("MET > 10").Histo1D("pt_v");
454auto h2 = d.Histo1D("pt_v");
455h1->Draw(); // event loop is run once here
456h2->Draw("SAME"); // no need to run the event loop again
457~~~
458RDataFrame executes all above actions by **running the event-loop only once**. The trick is that actions are not
459executed at the moment they are called, but they are **lazy**, i.e. delayed until the moment one of their results is
460accessed through the smart pointer. At that time, the event loop is triggered and *all* results are produced
461simultaneously.
462
463### Properly exploiting RDataFrame laziness
464
465For yet another example of the difference between the correct and incorrect running of the event-loop, see the following
466two code snippets. We assume our ROOT file has branches a, b and c.
467
468The correct way - the dataset is only processed once.
469~~~{.py}
470df_correct = ROOT.RDataFrame(treename, filename);
471
472h_a = df_correct.Histo1D("a")
473h_b = df_correct.Histo1D("b")
474h_c = df_correct.Histo1D("c")
475
476h_a_val = h_a.GetValue()
477h_b_val = h_b.GetValue()
478h_c_val = h_c.GetValue()
479
480print(f"How many times was the data set processed? {df_wrong.GetNRuns()} time.") # The answer will be 1 time.
481~~~
482
483An incorrect way - the dataset is processed three times.
484~~~{.py}
485df_incorrect = ROOT.RDataFrame(treename, filename);
486
487h_a = df_incorrect.Histo1D("a")
488h_a_val = h_a.GetValue()
489
490h_b = df_incorrect.Histo1D("b")
491h_b_val = h_b.GetValue()
492
493h_c = df_incorrect.Histo1D("c")
494h_c_val = h_c.GetValue()
495
496print(f"How many times was the data set processed? {df_wrong.GetNRuns()} times.") # The answer will be 3 times.
497~~~
498
499It is therefore good practice to declare all your transformations and actions *before* accessing their results, allowing
500RDataFrame to run the loop once and produce all results in one go.
501
502### Going parallel
503Let's say we would like to run the previous examples in parallel on several cores, dividing events fairly between cores.
504The only modification required to the snippets would be the addition of this line *before* constructing the main
505dataframe object:
506~~~{.cpp}
507ROOT::EnableImplicitMT();
508~~~
509Simple as that. More details are given [below](#parallel-execution).
510
511\anchor collections
512## Working with collections and object selections
513
514RDataFrame reads collections as the special type [ROOT::RVec](https://root.cern/doc/master/classROOT_1_1VecOps_1_1RVec.html): for example, a column containing an array of floating point numbers can be read as a ROOT::RVecF. C-style arrays (with variable or static size), STL vectors and most other collection types can be read this way.
515
516RVec is a container similar to std::vector (and can be used just like a std::vector) but it also offers a rich interface to operate on the array elements in a vectorised fashion, similarly to Python's NumPy arrays.
517
518For example, to fill a histogram with the "pt" of selected particles for each event, Define() can be used to create a column that contains the desired array elements as follows:
519
520~~~{.cpp}
521// h is filled with all the elements of `good_pts`, for each event
522auto h = df.Define("good_pts", [](const ROOT::RVecF &pt) { return pt[pt > 0]; })
523 .Histo1D("good_pts");
524~~~
525
526And in Python:
527
528~~~{.py}
529h = df.Define("good_pts", "pt[pt > 0]").Histo1D("good_pts")
530~~~
531
532Learn more at ROOT::VecOps::RVec.
533
534\anchor transformations
535## Transformations: manipulating data
536\anchor Filters
537### Filters
538A filter is created through a call to `Filter(f, columnList)` or `Filter(filterString)`. In the first overload, `f` can
539be a function, a lambda expression, a functor class, or any other callable object. It must return a `bool` signalling
540whether the event has passed the selection (`true`) or not (`false`). It should perform "read-only" operations on the
541columns, and should not have side-effects (e.g. modification of an external or static variable) to ensure correctness
542when implicit multi-threading is active. The second overload takes a string with a valid C++ expression in which column
543names are used as variable names (e.g. `Filter("x[0] + x[1] > 0")`). This is a convenience feature that comes with a
544certain runtime overhead: C++ code has to be generated on the fly from this expression before using it in the event
545loop. See the paragraph about "Just-in-time compilation" below for more information.
546
547RDataFrame only evaluates filters when necessary: if multiple filters are chained one after another, they are executed
548in order and the first one returning `false` causes the event to be discarded and triggers the processing of the next
549entry. If multiple actions or transformations depend on the same filter, that filter is not executed multiple times for
550each entry: after the first access it simply serves a cached result.
551
552\anchor named-filters-and-cutflow-reports
553#### Named filters and cutflow reports
554An optional string parameter `name` can be passed to the Filter() method to create a **named filter**. Named filters
555work as usual, but also keep track of how many entries they accept and reject.
556
557Statistics are retrieved through a call to the Report() method:
558
559- when Report() is called on the main RDataFrame object, it returns a ROOT::RDF::RResultPtr<RCutFlowReport> relative to all
560named filters declared up to that point
561- when called on a specific node (e.g. the result of a Define() or Filter()), it returns a ROOT::RDF::RResultPtr<RCutFlowReport>
562relative all named filters in the section of the chain between the main RDataFrame and that node (included).
563
564Stats are stored in the same order as named filters have been added to the graph, and *refer to the latest event-loop*
565that has been run using the relevant RDataFrame.
566
567\anchor ranges
568### Ranges
569When RDataFrame is not being used in a multi-thread environment (i.e. no call to EnableImplicitMT() was made),
570Range() transformations are available. These act very much like filters but instead of basing their decision on
571a filter expression, they rely on `begin`,`end` and `stride` parameters.
572
573- `begin`: initial entry number considered for this range.
574- `end`: final entry number (excluded) considered for this range. 0 means that the range goes until the end of the dataset.
575- `stride`: process one entry of the [begin, end) range every `stride` entries. Must be strictly greater than 0.
576
577The actual number of entries processed downstream of a Range() node will be `(end - begin)/stride` (or less if less
578entries than that are available).
579
580Note that ranges act "locally", not based on the global entry count: `Range(10,50)` means "skip the first 10 entries
581*that reach this node*, let the next 40 entries pass, then stop processing". If a range node hangs from a filter node,
582and the range has a `begin` parameter of 10, that means the range will skip the first 10 entries *that pass the
583preceding filter*.
584
585Ranges allow "early quitting": if all branches of execution of a functional graph reached their `end` value of
586processed entries, the event-loop is immediately interrupted. This is useful for debugging and quick data explorations.
587
588\anchor custom-columns
589### Custom columns
590Custom columns are created by invoking `Define(name, f, columnList)`. As usual, `f` can be any callable object
591(function, lambda expression, functor class...); it takes the values of the columns listed in `columnList` (a list of
592strings) as parameters, in the same order as they are listed in `columnList`. `f` must return the value that will be
593assigned to the temporary column.
594
595A new variable is created called `name`, accessible as if it was contained in the dataset from subsequent
596transformations/actions.
597
598Use cases include:
599- caching the results of complex calculations for easy and efficient multiple access
600- extraction of quantities of interest from complex objects
601- branch aliasing, i.e. changing the name of a branch
602
603An exception is thrown if the `name` of the new column/branch is already in use for another branch in the TTree.
604
605It is also possible to specify the quantity to be stored in the new temporary column as a C++ expression with the method
606`Define(name, expression)`. For example this invocation
607
608~~~{.cpp}
609df.Define("pt", "sqrt(px*px + py*py)");
610~~~
611
612will create a new column called "pt" the value of which is calculated starting from the columns px and py. The system
613builds a just-in-time compiled function starting from the expression after having deduced the list of necessary branches
614from the names of the variables specified by the user.
615
616#### Custom columns as function of slot and entry number
617
618It is possible to create custom columns also as a function of the processing slot and entry numbers. The methods that can
619be invoked are:
620- `DefineSlot(name, f, columnList)`. In this case the callable f has this signature `R(unsigned int, T1, T2, ...)`: the
621first parameter is the slot number which ranges from 0 to ROOT::GetThreadPoolSize() - 1.
622- `DefineSlotEntry(name, f, columnList)`. In this case the callable f has this signature `R(unsigned int, ULong64_t,
623T1, T2, ...)`: the first parameter is the slot number while the second one the number of the entry being processed.
624
625\anchor actions
626## Actions: getting results
627### Instant and lazy actions
628Actions can be **instant** or **lazy**. Instant actions are executed as soon as they are called, while lazy actions are
629executed whenever the object they return is accessed for the first time. As a rule of thumb, actions with a return value
630are lazy, the others are instant.
631
632### Return type of a lazy action
633
634When a lazy action is called, it returns a \link ROOT::RDF::RResultPtr ROOT::RDF::RResultPtr<T>\endlink, where T is the
635type of the result of the action. The final result will be stored in the `RResultPtr` and can be retrieved by
636dereferencing it or via its `GetValue` method.
637
638### Actions that return collections
639
640If the type of the return value of an action is a collection, e.g. `std::vector<int>`, you can iterate its elements
641directly through the wrapping `RResultPtr`:
642
643~~~{.cpp}
644ROOT::RDataFrame df{5};
645auto df1 = df.Define("x", []{ return 42; });
646for (const auto &el: df1.Take<int>("x")){
647 std::cout << "Element: " << el << "\n";
648}
649~~~
650
651~~~{.py}
652df = ROOT.RDataFrame(5).Define("x", "42")
653for el in df.Take[int]("x"):
654 print(f"Element: {el}")
655~~~
656
657### Actions and readers
658
659An action that needs values for its computations will request it from a reader, e.g. a column created via `Define` or
660available from the input dataset. The action will request values from each column of the list of input columns (either
661inferred or specified by the user), in order. For example:
662
663~~~{.cpp}
664ROOT::RDataFrame df{1};
665auto df1 = df.Define("x", []{ return 11; });
666auto df2 = df1.Define("y", []{ return 22; });
667auto graph = df2.Graph<int, int>("x","y");
668~~~
669
670The `Graph` action is going to request first the value from column "x", then that of column "y". Specifically, the order
671of execution of the operations of nodes in this branch of the computation graph is guaranteed to be top to bottom.
672
673\anchor distrdf
674## Distributed execution
675
676RDataFrame applications can be executed in parallel through distributed computing frameworks on a set of remote machines
677thanks to the Python package `ROOT.RDF.Experimental.Distributed`. This experimental, **Python-only** package allows to scale the
678optimized performance RDataFrame can achieve on a single machine to multiple nodes at the same time. It is designed so
679that different backends can be easily plugged in, currently supporting [Apache Spark](http://spark.apache.org/) and
680[Dask](https://dask.org/). To make use of distributed RDataFrame, you only need to switch `ROOT.RDataFrame` with
681the backend-specific `RDataFrame` of your choice, for example:
682
683~~~{.py}
684import ROOT
685
686# Point RDataFrame calls to the Spark specific RDataFrame
687RDataFrame = ROOT.RDF.Experimental.Distributed.Spark.RDataFrame
688
689# It still accepts the same constructor arguments as traditional RDataFrame
690df = RDataFrame("mytree", "myfile.root")
691
692# Continue the application with the traditional RDataFrame API
693sum = df.Filter("x > 10").Sum("y")
694h = df.Histo1D(("name", "title", 10, 0, 10), "x")
695
696print(sum.GetValue())
697h.Draw()
698~~~
699
700The main goal of this package is to support running any RDataFrame application distributedly. Nonetheless, not all
701parts of the RDataFrame API currently work with this package. The subset that is currently available is:
702- Alias
703- AsNumpy
704- Count
705- Define
706- DefinePerSample
707- Filter
708- Graph
709- Histo[1,2,3]D
710- HistoND
711- Max
712- Mean
713- Min
714- Profile[1,2,3]D
715- Redefine
716- Snapshot
717- Stats
718- StdDev
719- Sum
720- Systematic variations: Vary and [VariationsFor](\ref ROOT::RDF::Experimental::VariationsFor).
721- Parallel submission of distributed graphs: [RunGraphs](\ref ROOT::RDF::RunGraphs).
722- Information about the dataframe: GetColumnNames.
723
724with support for more operations coming in the future. Currently, to the supported data sources belong TTree, TChain, RNTuple and RDatasetSpec.
725
726\note The distributed RDataFrame module requires at least Python version 3.8.
727
728### Connecting to a Spark cluster
729
730In order to distribute the RDataFrame workload, you can connect to a Spark cluster you have access to through the
731official [Spark API](https://spark.apache.org/docs/latest/rdd-programming-guide.html#initializing-spark), then hook the
732connection instance to the distributed `RDataFrame` object like so:
733
734~~~{.py}
735import pyspark
736import ROOT
737
738# Create a SparkContext object with the right configuration for your Spark cluster
739conf = SparkConf().setAppName(appName).setMaster(master)
740sc = SparkContext(conf=conf)
741
742# Point RDataFrame calls to the Spark specific RDataFrame
743RDataFrame = ROOT.RDF.Experimental.Distributed.Spark.RDataFrame
744
745# The Spark RDataFrame constructor accepts an optional "sparkcontext" parameter
746# and it will distribute the application to the connected cluster
747df = RDataFrame("mytree", "myfile.root", sparkcontext = sc)
748~~~
749
750If an instance of [SparkContext](https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.SparkContext.html)
751is not provided, the default behaviour is to create one in the background for you.
752
753### Connecting to a Dask cluster
754
755Similarly, you can connect to a Dask cluster by creating your own connection object which internally operates with one
756of the cluster schedulers supported by Dask (more information in the
757[Dask distributed docs](http://distributed.dask.org/en/stable/)):
758
759~~~{.py}
760import ROOT
761from dask.distributed import Client
762
763# Point RDataFrame calls to the Dask specific RDataFrame
764RDataFrame = ROOT.RDF.Experimental.Distributed.Dask.RDataFrame
765
766# In a Python script the Dask client needs to be initalized in a context
767# Jupyter notebooks / Python session don't need this
768if __name__ == "__main__":
769 # With an already setup cluster that exposes a Dask scheduler endpoint
770 client = Client("dask_scheduler.domain.com:8786")
771
772 # The Dask RDataFrame constructor accepts the Dask Client object as an optional argument
773 df = RDataFrame("mytree","myfile.root", daskclient=client)
774 # Proceed as usual
775 df.Define("x","someoperation").Histo1D(("name", "title", 10, 0, 10), "x")
776~~~
777
778If an instance of [distributed.Client](http://distributed.dask.org/en/stable/api.html#distributed.Client) is not
779provided to the RDataFrame object, it will be created for you and it will run the computations in the local machine
780using all cores available.
781
782### Choosing the number of distributed tasks
783
784A distributed RDataFrame has internal logic to define in how many chunks the input dataset will be split before sending
785tasks to the distributed backend. Each task reads and processes one of said chunks. The logic is backend-dependent, but
786generically tries to infer how many cores are available in the cluster through the connection object. The number of
787tasks will be equal to the inferred number of cores. There are cases where the connection object of the chosen backend
788doesn't have information about the actual resources of the cluster. An example of this is when using Dask to connect to
789a batch system. The client object created at the beginning of the application does not automatically know how many cores
790will be available during distributed execution, since the jobs are submitted to the batch system after the creation of
791the connection. In such cases, the logic is to default to process the whole dataset in 2 tasks.
792
793The number of tasks submitted for distributed execution can be also set programmatically, by providing the optional
794keyword argument `npartitions` when creating the RDataFrame object. This parameter is accepted irrespectively of the
795backend used:
796
797~~~{.py}
798import ROOT
799
800# Define correct imports and access the distributed RDataFrame appropriate for the
801# backend used in the analysis
802RDataFrame = ROOT.RDF.Experimental.Distributed.[BACKEND].RDataFrame
803
804if __name__ == "__main__":
805 # The `npartitions` optional argument tells the RDataFrame how many tasks are desired
806 df = RDataFrame("mytree","myfile.root", npartitions=NPARTITIONS)
807 # Proceed as usual
808 df.Define("x","someoperation").Histo1D(("name", "title", 10, 0, 10), "x")
809~~~
810
811Note that when processing a TTree or TChain dataset, the `npartitions` value should not exceed the number of clusters in
812the dataset. The number of clusters in a TTree can be retrieved by typing `rootls -lt myfile.root` at a command line.
813
814### Distributed Snapshot
815
816The Snapshot operation behaves slightly differently when executed distributedly. First off, it requires the path
817supplied to the Snapshot call to be accessible from any worker of the cluster and from the client machine (in general
818it should be provided as an absolute path). Another important difference is that `n` separate files will be produced,
819where `n` is the number of dataset partitions. As with local RDataFrame, the result of a Snapshot on a distributed
820RDataFrame is another distributed RDataFrame on which we can define a new computation graph and run more distributed
821computations.
822
823### Distributed RunGraphs
824
825Submitting multiple distributed RDataFrame executions is supported through the RunGraphs function. Similarly to its
826local counterpart, the function expects an iterable of objects representing an RDataFrame action. Each action will be
827triggered concurrently to send multiple computation graphs to a distributed cluster at the same time:
828
829~~~{.py}
830import ROOT
831RDataFrame = ROOT.RDF.Experimental.Distributed.Dask.RDataFrame
832RunGraphs = ROOT.RDF.Experimental.Distributed.RunGraphs
833
834# Create 3 different dataframes and book an histogram on each one
835histoproxies = [
836 RDataFrame(100)
837 .Define("x", "rdfentry_")
838 .Histo1D(("name", "title", 10, 0, 100), "x")
839 for _ in range(4)
840]
841
842# Execute the 3 computation graphs
843RunGraphs(histoproxies)
844# Retrieve all the histograms in one go
845histos = [histoproxy.GetValue() for histoproxy in histoproxies]
846~~~
847
848Every distributed backend supports this feature and graphs belonging to different backends can be still triggered with
849a single call to RunGraphs (e.g. it is possible to send a Spark job and a Dask job at the same time).
850
851### Histogram models in distributed mode
852
853When calling a Histo*D operation in distributed mode, remember to pass to the function the model of the histogram to be
854computed, e.g. the axis range and the number of bins:
855
856~~~{.py}
857import ROOT
858RDataFrame = ROOT.RDF.Experimental.Distributed.[BACKEND].RDataFrame
859
860if __name__ == "__main__":
861 df = RDataFrame("mytree","myfile.root").Define("x","someoperation")
862 # The model can be passed either as a tuple with the arguments in the correct order
863 df.Histo1D(("name", "title", 10, 0, 10), "x")
864 # Or by creating the specific struct
865 model = ROOT.RDF.TH1DModel("name", "title", 10, 0, 10)
866 df.Histo1D(model, "x")
867~~~
868
869Without this, two partial histograms resulting from two distributed tasks would have incompatible binning, thus leading
870to errors when merging them. Failing to pass a histogram model will raise an error on the client side, before starting
871the distributed execution.
872
873### Live visualization in distributed mode with dask
874
875The live visualization feature allows real-time data representation of plots generated during the execution
876of a distributed RDataFrame application.
877It enables visualizing intermediate results as they are computed across multiple nodes of a Dask cluster
878by creating a canvas and continuously updating it as partial results become available.
879
880The LiveVisualize() function can be imported from the Python package **ROOT.RDF.Experimental.Distributed**:
881
882~~~{.py}
883import ROOT
884
885LiveVisualize = ROOT.RDF.Experimental.Distributed.LiveVisualize
886~~~
887
888The function takes drawable objects (e.g. histograms) and optional callback functions as argument, it accepts 4 different input formats:
889
890- Passing a list or tuple of drawables:
891You can pass a list or tuple containing the plots you want to visualize. For example:
892
893~~~{.py}
894LiveVisualize([h_gaus, h_exp, h_random])
895~~~
896
897- Passing a list or tuple of drawables with a global callback function:
898You can also include a global callback function that will be applied to all plots. For example:
899
900~~~{.py}
901def set_fill_color(hist):
902 hist.SetFillColor(ROOT.kBlue)
903
904LiveVisualize([h_gaus, h_exp, h_random], set_fill_color)
905~~~
906
907- Passing a Dictionary of drawables and callback functions:
908For more control, you can create a dictionary where keys are plots and values are corresponding (optional) callback functions. For example:
909
910~~~{.py}
911plot_callback_dict = {
912 graph: set_marker,
913 h_exp: fit_exp,
914 tprofile_2d: None
915}
916
917LiveVisualize(plot_callback_dict)
918~~~
919
920- Passing a Dictionary of drawables and callback functions with a global callback function:
921You can also combine a dictionary of plots and callbacks with a global callback function:
922
923~~~{.py}
924LiveVisualize(plot_callback_dict, write_to_tfile)
925~~~
926
927\note The allowed operations to pass to LiveVisualize are:
928 - Histo1D(), Histo2D(), Histo3D()
929 - Graph()
930 - Profile1D(), Profile2D()
931
932\warning The Live Visualization feature is only supported for the Dask backend.
933
934### Injecting C++ code and using external files into distributed RDF script
935
936Distributed RDF provides an interface for the users who want to inject the C++ code (via header files, shared libraries or declare the code directly)
937into their distributed RDF application, or their application needs to use information from external files which should be distributed
938to the workers (for example, a JSON or a txt file with necessary parameters information).
939
940The examples below show the usage of these interface functions: firstly, how this is done in a local Python
941RDF application and secondly, how it is done distributedly.
942
943#### Include and distribute header files.
944
945~~~{.py}
946# Local RDataFrame script
947ROOT.gInterpreter.AddIncludePath("myheader.hxx")
948df.Define(...)
949
950# Distributed RDF script
951ROOT.RDF.Experimental.Distributed.DistributeHeaders("myheader.hxx")
952df.Define(...)
953~~~
954
955#### Load and distribute shared libraries
956
957~~~{.py}
958# Local RDataFrame script
959ROOT.gSystem.Load("my_library.so")
960df.Define(...)
961
962# Distributed RDF script
963ROOT.RDF.Experimental.Distributed.DistributeSharedLibs("my_library.so")
964df.Define(...)
965~~~
966
967#### Declare and distribute the cpp code
968
969The cpp code is always available to all dataframes.
970
971~~~{.py}
972# Local RDataFrame script
973ROOT.gInterpreter.Declare("my_code")
974df.Define(...)
975
976# Distributed RDF script
977ROOT.RDF.Experimental.Distributed.DistributeCppCode("my_code")
978df.Define(...)
979~~~
980
981#### Distribute additional files (other than headers or shared libraries).
982
983~~~{.py}
984# Local RDataFrame script is not applicable here as local RDF application can simply access the external files it needs.
985
986# Distributed RDF script
987ROOT.RDF.Experimental.Distributed.DistributeFiles("my_file")
988df.Define(...)
989~~~
990
991\anchor parallel-execution
992## Performance tips and parallel execution
993As pointed out before in this document, RDataFrame can transparently perform multi-threaded event loops to speed up
994the execution of its actions. Users have to call ROOT::EnableImplicitMT() *before* constructing the RDataFrame
995object to indicate that it should take advantage of a pool of worker threads. **Each worker thread processes a distinct
996subset of entries**, and their partial results are merged before returning the final values to the user.
997There are no guarantees on the order in which threads will process the batches of entries.
998In particular, note that this means that, for multi-thread event loops, there is no
999guarantee on the order in which Snapshot() will _write_ entries: they could be scrambled with respect to the input dataset. The values of the special `rdfentry_` column will also not correspond to the entry numbers in the input dataset (e.g. TChain) in multi-thread runs.
1000
1001\warning By default, RDataFrame will use as many threads as the hardware supports, using up **all** the resources on
1002a machine. This might be undesirable on shared computing resources such as a batch cluster. Therefore, when running on shared computing resources, use
1003~~~{.cpp}
1004ROOT::EnableImplicitMT(i)
1005~~~
1006replacing `i` with the number of CPUs/slots that were allocated for this job.
1007
1008### Thread-safety of user-defined expressions
1009RDataFrame operations such as Histo1D() or Snapshot() are guaranteed to work correctly in multi-thread event loops.
1010User-defined expressions, such as strings or lambdas passed to Filter(), Define(), Foreach(), Reduce() or Aggregate()
1011will have to be thread-safe, i.e. it should be possible to call them concurrently from different threads.
1012
1013Note that simple Filter() and Define() transformations will inherently satisfy this requirement: Filter() / Define()
1014expressions will often be *pure* in the functional programming sense (no side-effects, no dependency on external state),
1015which eliminates all risks of race conditions.
1016
1017In order to facilitate writing of thread-safe operations, some RDataFrame features such as Foreach(), Define() or \link ROOT::RDF::RResultPtr::OnPartialResult OnPartialResult()\endlink
1018offer thread-aware counterparts (ForeachSlot(), DefineSlot(), \link ROOT::RDF::RResultPtr::OnPartialResultSlot OnPartialResultSlot()\endlink): their only difference is that they
1019will pass an extra `slot` argument (an unsigned integer) to the user-defined expression. When calling user-defined code
1020concurrently, RDataFrame guarantees that different threads will employ different values of the `slot` parameter,
1021where `slot` will be a number between 0 and `GetNSlots() - 1`.
1022In other words, within a slot, computation runs sequentially and events are processed sequentially.
1023Note that the same slot might be associated to different threads over the course of a single event loop, but two threads
1024will never receive the same slot at the same time.
1025This extra parameter might facilitate writing safe parallel code by having each thread write/modify a different
1026processing slot, e.g. a different element of a list. See [here](#generic-actions) for an example usage of ForeachSlot().
1027
1028### Parallel execution of multiple RDataFrame event loops
1029A complex analysis may require multiple separate RDataFrame computation graphs to produce all desired results. This poses the challenge that the
1030event loops of each computation graph can be parallelized, but the different loops run sequentially, one after the other.
1031On many-core architectures it might be desirable to run different event loops concurrently to improve resource usage.
1032ROOT::RDF::RunGraphs() allows running multiple RDataFrame event loops concurrently:
1033~~~{.cpp}
1034ROOT::EnableImplicitMT();
1035ROOT::RDataFrame df1("tree1", "f1.root");
1036ROOT::RDataFrame df2("tree2", "f2.root");
1037auto histo1 = df1.Histo1D("x");
1038auto histo2 = df2.Histo1D("y");
1039
1040// just accessing result pointers, the event loops of separate RDataFrames run one after the other
1041histo1->Draw(); // runs first multi-thread event loop
1042histo2->Draw(); // runs second multi-thread event loop
1043
1044// alternatively, with ROOT::RDF::RunGraphs, event loops for separate computation graphs can run concurrently
1045ROOT::RDF::RunGraphs({histo1, histo2});
1046histo1->Draw(); // results can then be used as usual
1047~~~
1048
1049### Performance considerations
1050
1051To obtain the maximum performance out of RDataFrame, make sure to avoid just-in-time compiled versions of transformations and actions if at all possible.
1052For instance, `Filter("x > 0")` requires just-in-time compilation of the corresponding C++ logic, while the equivalent `Filter([](float x) { return x > 0.; }, {"x"})` does not.
1053Similarly, `Histo1D("x")` requires just-in-time compilation after the type of `x` is retrieved from the dataset, while `Histo1D<float>("x")` does not; the latter spelling
1054should be preferred for performance-critical applications.
1055
1056Python applications cannot easily specify template parameters or pass C++ callables to RDataFrame.
1057See [Efficient analysis in Python](#python) for possible ways to speed up hot paths in this case.
1058
1059Just-in-time compilation happens once, right before starting an event loop. To reduce the runtime cost of this step, make sure to book all operations *for all RDataFrame computation graphs*
1060before the first event loop is triggered: just-in-time compilation will happen once for all code required to be generated up to that point, also across different computation graphs.
1061
1062Also make sure not to count the just-in-time compilation time (which happens once before the event loop and does not depend on the size of the dataset) as part of the event loop runtime (which scales with the size of the dataset). RDataFrame has an experimental logging feature that simplifies measuring the time spent in just-in-time compilation and in the event loop (as well as providing some more interesting information). See [Activating RDataFrame execution logs](\ref rdf-logging).
1063
1064### Memory usage
1065
1066There are two reasons why RDataFrame may consume more memory than expected. Firstly, each result is duplicated for each worker thread, which e.g. in case of many (possibly multi-dimensional) histograms with fine binning can result in visible memory consumption during the event loop. The thread-local copies of the results are destroyed when the final result is produced. Reducing the number of threads or using coarser binning will reduce the memory usage.
1067
1068Secondly, just-in-time compilation of string expressions or non-templated actions (see the previous paragraph) causes Cling, ROOT's C++ interpreter, to allocate some memory for the generated code that is only released at the end of the application. This commonly results in memory usage creep in long-running applications that create many RDataFrames one after the other. Possible mitigations include creating and running each RDataFrame event loop in a sub-process, or booking all operations for all different RDataFrame computation graphs before the first event loop is triggered, so that the interpreter is invoked only once for all computation graphs:
1069
1070~~~{.cpp}
1071// assuming df1 and df2 are separate computation graphs, do:
1072auto h1 = df1.Histo1D("x");
1073auto h2 = df2.Histo1D("y");
1074h1->Draw(); // we just-in-time compile everything needed by df1 and df2 here
1075h2->Draw("SAME");
1076
1077// do not:
1078auto h1 = df1.Histo1D("x");
1079h1->Draw(); // we just-in-time compile here
1080auto h2 = df2.Histo1D("y");
1081h2->Draw("SAME"); // we just-in-time compile again here, as the second Histo1D call is new
1082~~~
1083
1084\anchor more-features
1085## More features
1086Here is a list of the most important features that have been omitted in the "Crash course" for brevity.
1087You don't need to read all these to start using RDataFrame, but they are useful to save typing time and runtime.
1088
1089\anchor systematics
1090### Systematic variations
1091
1092Starting from ROOT v6.26, RDataFrame provides a flexible syntax to define systematic variations.
1093This is done in two steps: a) register variations for one or more existing columns using Vary() and b) extract variations
1094of normal RDataFrame results using \ref ROOT::RDF::Experimental::VariationsFor "VariationsFor()". In between these steps, no other change
1095to the analysis code is required: the presence of systematic variations for certain columns is automatically propagated
1096through filters, defines and actions, and RDataFrame will take these dependencies into account when producing varied
1097results. \ref ROOT::RDF::Experimental::VariationsFor "VariationsFor()" is included in header `ROOT/RDFHelpers.hxx`. The compiled C++ programs must include this header
1098explicitly, this is not required for ROOT macros.
1099
1100An example usage of Vary() and \ref ROOT::RDF::Experimental::VariationsFor "VariationsFor()" in C++:
1101
1102~~~{.cpp}
1103auto nominal_hx =
1104 df.Vary("pt", "ROOT::RVecD{pt*0.9f, pt*1.1f}", {"down", "up"})
1105 .Filter("pt > pt_cut")
1106 .Define("x", someFunc, {"pt"})
1107 .Histo1D<float>("x");
1108
1109// request the generation of varied results from the nominal_hx
1110ROOT::RDF::Experimental::RResultMap<TH1D> hx = ROOT::RDF::Experimental::VariationsFor(nominal_hx);
1111
1112// the event loop runs here, upon first access to any of the results or varied results:
1113hx["nominal"].Draw(); // same effect as nominal_hx->Draw()
1114hx["pt:down"].Draw("SAME");
1115hx["pt:up"].Draw("SAME");
1116~~~
1117
1118A list of variation "tags" is passed as the last argument to Vary(). The tags give names to the varied values that are returned
1119as elements of an RVec of the appropriate C++ type. The number of variation tags must correspond to the number of elements of
1120this RVec (2 in the example above: the first element will correspond to the tag "down", the second
1121to the tag "up"). The _full_ variation name will be composed of the varied column name and the variation tags (e.g.
1122"pt:down", "pt:up" in this example). Python usage looks similar.
1123
1124Note how we use the "pt" column as usual in the Filter() and Define() calls and we simply use "x" as the value to fill
1125the resulting histogram. To produce the varied results, RDataFrame will automatically execute the Filter and Define
1126calls for each variation and fill the histogram with values and cuts that depend on the variation.
1127
1128There is no limitation to the complexity of a Vary() expression. Just like for the Define() and Filter() calls, users are
1129not limited to string expressions but they can also pass any valid C++ callable, including lambda functions and
1130complex functors. The callable can be applied to zero or more existing columns and it will always receive their
1131_nominal_ value in input.
1132
1133#### Varying multiple columns in lockstep
1134
1135In the following Python snippet we use the Vary() signature that allows varying multiple columns simultaneously or
1136"in lockstep":
1137
1138~~~{.python}
1139df.Vary(["pt", "eta"],
1140 "RVec<RVecF>{{pt*0.9, pt*1.1}, {eta*0.9, eta*1.1}}",
1141 variationTags=["down", "up"],
1142 variationName="ptAndEta")
1143~~~
1144
1145The expression returns an RVec of two RVecs: each inner vector contains the varied values for one column. The
1146inner vectors follow the same ordering as the column names that are passed as the first argument. Besides the variation tags, in
1147this case we also have to explicitly pass the variation name (here: "ptAndEta") as the default column name does not exist.
1148
1149The above call will produce variations "ptAndEta:down" and "ptAndEta:up".
1150
1151#### Combining multiple variations
1152
1153Even if a result depends on multiple variations, only one variation is applied at a time, i.e. there will be no result produced
1154by applying multiple systematic variations at the same time.
1155For example, in the following example snippet, the RResultMap instance `all_h` will contain keys "nominal", "pt:down",
1156"pt:up", "eta:0", "eta:1", but no "pt:up&&eta:0" or similar:
1157
1158~~~{.cpp}
1159auto df = _df.Vary("pt",
1160 "ROOT::RVecD{pt*0.9, pt*1.1}",
1161 {"down", "up"})
1162 .Vary("eta",
1163 [](float eta) { return RVecF{eta*0.9f, eta*1.1f}; },
1164 {"eta"},
1165 2);
1166
1167auto nom_h = df.Histo2D(histoModel, "pt", "eta");
1168auto all_hs = VariationsFor(nom_h);
1169all_hs.GetKeys(); // returns {"nominal", "pt:down", "pt:up", "eta:0", "eta:1"}
1170~~~
1171
1172Note how we passed the integer `2` instead of a list of variation tags to the second Vary() invocation: this is a
1173shorthand that automatically generates tags 0 to N-1 (in this case 0 and 1).
1174
1175\note Currently, VariationsFor() and RResultMap are in the `ROOT::RDF::Experimental` namespace, to indicate that these
1176 interfaces might still evolve and improve based on user feedback. We expect that some aspects of the related
1177 programming model will be streamlined in future versions.
1178
1179\note Currently, the results of a Snapshot(), Report() or Display() call cannot be varied (i.e. it is not possible to
1180 call \ref ROOT::RDF::Experimental::VariationsFor "VariationsFor()" on them. These limitations will be lifted in future releases.
1181
1182See the Vary() method for more information and [this tutorial](https://root.cern/doc/master/df106__HiggsToFourLeptons_8C.html)
1183for an example usage of Vary and \ref ROOT::RDF::Experimental::VariationsFor "VariationsFor()" in the analysis.
1184
1185\anchor rnode
1186### RDataFrame objects as function arguments and return values
1187RDataFrame variables/nodes are relatively cheap to copy and it's possible to both pass them to (or move them into)
1188functions and to return them from functions. However, in general each dataframe node will have a different C++ type,
1189which includes all available compile-time information about what that node does. One way to cope with this complication
1190is to use template functions and/or C++14 auto return types:
1191~~~{.cpp}
1192template <typename RDF>
1193auto ApplySomeFilters(RDF df)
1194{
1195 return df.Filter("x > 0").Filter([](int y) { return y < 0; }, {"y"});
1196}
1197~~~
1198
1199A possibly simpler, C++11-compatible alternative is to take advantage of the fact that any dataframe node can be
1200converted (implicitly or via an explicit cast) to the common type ROOT::RDF::RNode:
1201~~~{.cpp}
1202// a function that conditionally adds a Range to an RDataFrame node.
1203RNode MaybeAddRange(RNode df, bool mustAddRange)
1204{
1205 return mustAddRange ? df.Range(1) : df;
1206}
1207// use as :
1208ROOT::RDataFrame df(10);
1209auto maybeRangedDF = MaybeAddRange(df, true);
1210~~~
1211
1212The conversion to ROOT::RDF::RNode is cheap, but it will introduce an extra virtual call during the RDataFrame event
1213loop (in most cases, the resulting performance impact should be negligible). Python users can perform the conversion with the helper function `ROOT.RDF.AsRNode`.
1214
1215\anchor RDFCollections
1216### Storing RDataFrame objects in collections
1217
1218ROOT::RDF::RNode also makes it simple to store RDataFrame nodes in collections, e.g. a `std::vector<RNode>` or a `std::map<std::string, RNode>`:
1219
1220~~~{.cpp}
1221std::vector<ROOT::RDF::RNode> dfs;
1222dfs.emplace_back(ROOT::RDataFrame(10));
1223dfs.emplace_back(dfs[0].Define("x", "42.f"));
1224~~~
1225
1226\anchor callbacks
1227### Executing callbacks every N events
1228It's possible to schedule execution of arbitrary functions (callbacks) during the event loop.
1229Callbacks can be used e.g. to inspect partial results of the analysis while the event loop is running,
1230drawing a partially-filled histogram every time a certain number of new entries is processed, or
1231displaying a progress bar while the event loop runs.
1232
1233For example one can draw an up-to-date version of a result histogram every 100 entries like this:
1234~~~{.cpp}
1235auto h = df.Histo1D("x");
1236TCanvas c("c","x hist");
1237h.OnPartialResult(100, [&c](TH1D &h_) { c.cd(); h_.Draw(); c.Update(); });
1238// event loop runs here, this final `Draw` is executed after the event loop is finished
1239h->Draw();
1240~~~
1241
1242Callbacks are registered to a ROOT::RDF::RResultPtr and must be callables that takes a reference to the result type as argument
1243and return nothing. RDataFrame will invoke registered callbacks passing partial action results as arguments to them
1244(e.g. a histogram filled with a part of the selected events).
1245
1246Read more on ROOT::RDF::RResultPtr::OnPartialResult() and ROOT::RDF::RResultPtr::OnPartialResultSlot().
1247
1248\anchor default-branches
1249### Default column lists
1250When constructing an RDataFrame object, it is possible to specify a **default column list** for your analysis, in the
1251usual form of a list of strings representing branch/column names. The default column list will be used as a fallback
1252whenever a list specific to the transformation/action is not present. RDataFrame will take as many of these columns as
1253needed, ignoring trailing extra names if present.
1254~~~{.cpp}
1255// use "b1" and "b2" as default columns
1256RDataFrame d1("myTree", "file.root", {"b1","b2"});
1257auto h = d1.Filter([](int b1, int b2) { return b1 > b2; }) // will act on "b1" and "b2"
1258 .Histo1D(); // will act on "b1"
1259
1260// just one default column this time
1261RDataFrame d2("myTree", "file.root", {"b1"});
1262auto d2f = d2.Filter([](double b2) { return b2 > 0; }, {"b2"}) // we can still specify non-default column lists
1263auto min = d2f.Min(); // returns the minimum value of "b1" for the filtered entries
1264auto vals = d2f.Take<double>(); // return the values for all entries passing the selection as a vector
1265~~~
1266
1267\anchor helper-cols
1268### Special helper columns: rdfentry_ and rdfslot_
1269Every instance of RDataFrame is created with two special columns called `rdfentry_` and `rdfslot_`. The `rdfentry_`
1270column is of type `ULong64_t` and it holds the current entry number while `rdfslot_` is an `unsigned int`
1271holding the index of the current data processing slot.
1272For backwards compatibility reasons, the names `tdfentry_` and `tdfslot_` are also accepted.
1273These columns are ignored by operations such as [Cache](classROOT_1_1RDF_1_1RInterface.html#aaaa0a7bb8eb21315d8daa08c3e25f6c9)
1274or [Snapshot](classROOT_1_1RDF_1_1RInterface.html#a233b7723e498967f4340705d2c4db7f8).
1275
1276\warning Note that in multi-thread event loops the values of `rdfentry_` _do not_ correspond to what would be the entry numbers
1277of a TChain constructed over the same set of ROOT files, as the entries are processed in an unspecified order.
1278
1279\anchor jitting
1280### Just-in-time compilation: column type inference and explicit declaration of column types
1281C++ is a statically typed language: all types must be known at compile-time. This includes the types of the TTree
1282branches we want to work on. For filters, defined columns and some of the actions, **column types are deduced from the
1283signature** of the relevant filter function/temporary column expression/action function:
1284~~~{.cpp}
1285// here b1 is deduced to be `int` and b2 to be `double`
1286df.Filter([](int x, double y) { return x > 0 && y < 0.; }, {"b1", "b2"});
1287~~~
1288If we specify an incorrect type for one of the columns, an exception with an informative message will be thrown at
1289runtime, when the column value is actually read from the dataset: RDataFrame detects type mismatches. The same would
1290happen if we swapped the order of "b1" and "b2" in the column list passed to Filter().
1291
1292Certain actions, on the other hand, do not take a function as argument (e.g. Histo1D()), so we cannot deduce the type of
1293the column at compile-time. In this case **RDataFrame infers the type of the column** from the TTree itself. This
1294is why we never needed to specify the column types for all actions in the above snippets.
1295
1296When the column type is not a common one such as `int`, `double`, `char` or `float` it is nonetheless good practice to
1297specify it as a template parameter to the action itself, like this:
1298~~~{.cpp}
1299df.Histo1D("b1"); // OK, the type of "b1" is deduced at runtime
1300df.Min<MyNumber_t>("myObject"); // OK, "myObject" is deduced to be of type `MyNumber_t`
1301~~~
1302
1303Deducing types at runtime requires the just-in-time compilation of the relevant actions, which has a small runtime
1304overhead, so specifying the type of the columns as template parameters to the action is good practice when performance is a goal.
1305
1306When strings are passed as expressions to Filter() or Define(), fundamental types are passed as constants. This avoids certaincommon mistakes such as typing `x = 0` rather than `x == 0`:
1307
1308~~~{.cpp}
1309// this throws an error (note the typo)
1310df.Define("x", "0").Filter("x = 0");
1311~~~
1312
1313\anchor generic-actions
1314### User-defined custom actions
1315RDataFrame strives to offer a comprehensive set of standard actions that can be performed on each event. At the same
1316time, it allows users to inject their own action code to perform arbitrarily complex data reductions.
1317
1318#### Implementing custom actions with Book()
1319
1320Through the Book() method, users can implement a custom action and have access to the same features
1321that built-in RDataFrame actions have, e.g. hooks to events related to the start, end and execution of the
1322event loop, or the possibility to return a lazy RResultPtr to an arbitrary type of result:
1323
1324~~~{.cpp}
1325#include <ROOT/RDataFrame.hxx>
1326#include <memory>
1327
1328class MyCounter : public ROOT::Detail::RDF::RActionImpl<MyCounter> {
1329 std::shared_ptr<int> fFinalResult = std::make_shared<int>(0);
1330 std::vector<int> fPerThreadResults;
1331
1332public:
1333 // We use a public type alias to advertise the type of the result of this action
1334 using Result_t = int;
1335
1336 MyCounter(unsigned int nSlots) : fPerThreadResults(nSlots) {}
1337
1338 // Called before the event loop to retrieve the address of the result that will be filled/generated.
1339 std::shared_ptr<int> GetResultPtr() const { return fFinalResult; }
1340
1341 // Called at the beginning of the event loop.
1342 void Initialize() {}
1343
1344 // Called at the beginning of each processing task.
1345 void InitTask(TTreeReader *, int) {}
1346
1347 /// Called at every entry.
1348 void Exec(unsigned int slot)
1349 {
1350 fPerThreadResults[slot]++;
1351 }
1352
1353 // Called at the end of the event loop.
1354 void Finalize()
1355 {
1356 *fFinalResult = std::accumulate(fPerThreadResults.begin(), fPerThreadResults.end(), 0);
1357 }
1358
1359 // Called by RDataFrame to retrieve the name of this action.
1360 std::string GetActionName() const { return "MyCounter"; }
1361};
1362
1363int main() {
1364 ROOT::RDataFrame df(10);
1365 ROOT::RDF::RResultPtr<int> resultPtr = df.Book<>(MyCounter{df.GetNSlots()}, {});
1366 // The GetValue call triggers the event loop
1367 std::cout << "Number of processed entries: " << resultPtr.GetValue() << std::endl;
1368}
1369~~~
1370
1371See the Book() method for more information and [this tutorial](https://root.cern/doc/master/df018__customActions_8C.html)
1372for a more complete example.
1373
1374#### Injecting arbitrary code in the event loop with Foreach() and ForeachSlot()
1375
1376Foreach() takes a callable (lambda expression, free function, functor...) and a list of columns and
1377executes the callable on the values of those columns for each event that passes all upstream selections.
1378It can be used to perform actions that are not already available in the interface. For example, the following snippet
1379evaluates the root mean square of column "x":
1380~~~{.cpp}
1381// Single-thread evaluation of RMS of column "x" using Foreach
1382double sumSq = 0.;
1383unsigned int n = 0;
1384df.Foreach([&sumSq, &n](double x) { ++n; sumSq += x*x; }, {"x"});
1385std::cout << "rms of x: " << std::sqrt(sumSq / n) << std::endl;
1386~~~
1387In multi-thread runs, users are responsible for the thread-safety of the expression passed to Foreach():
1388thread will execute the expression concurrently.
1389The code above would need to employ some resource protection mechanism to ensure non-concurrent writing of `rms`; but
1390this is probably too much head-scratch for such a simple operation.
1391
1392ForeachSlot() can help in this situation. It is an alternative version of Foreach() for which the function takes an
1393additional "processing slot" parameter besides the columns it should be applied to. RDataFrame
1394guarantees that ForeachSlot() will invoke the user expression with different `slot` parameters for different concurrent
1395executions (see [Special helper columns: rdfentry_ and rdfslot_](\ref helper-cols) for more information on the slot parameter).
1396We can take advantage of ForeachSlot() to evaluate a thread-safe root mean square of column "x":
1397~~~{.cpp}
1398// Thread-safe evaluation of RMS of column "x" using ForeachSlot
1399ROOT::EnableImplicitMT();
1400const unsigned int nSlots = df.GetNSlots();
1401std::vector<double> sumSqs(nSlots, 0.);
1402std::vector<unsigned int> ns(nSlots, 0);
1403
1404df.ForeachSlot([&sumSqs, &ns](unsigned int slot, double x) { sumSqs[slot] += x*x; ns[slot] += 1; }, {"x"});
1405double sumSq = std::accumulate(sumSqs.begin(), sumSqs.end(), 0.); // sum all squares
1406unsigned int n = std::accumulate(ns.begin(), ns.end(), 0); // sum all counts
1407std::cout << "rms of x: " << std::sqrt(sumSq / n) << std::endl;
1408~~~
1409Notice how we created one `double` variable for each processing slot and later merged their results via `std::accumulate`.
1410
1411
1412\anchor friends
1413### Dataset joins with friend trees
1414
1415Vertically concatenating multiple trees that have the same columns (creating a logical dataset with the same columns and
1416more rows) is trivial in RDataFrame: just pass the tree name and a list of file names to RDataFrame's constructor, or create a TChain
1417out of the desired trees and pass that to RDataFrame.
1418
1419Horizontal concatenations of trees or chains (creating a logical dataset with the same number of rows and the union of the
1420columns of multiple trees) leverages TTree's "friend" mechanism.
1421
1422Simple joins of trees that do not have the same number of rows are also possible with indexed friend trees (see below).
1423
1424To use friend trees in RDataFrame, set up trees with the appropriate relationships and then instantiate an RDataFrame
1425with the main tree:
1426
1427~~~{.cpp}
1428TTree main([...]);
1429TTree friend([...]);
1430main.AddFriend(&friend, "myFriend");
1431
1432RDataFrame df(main);
1433auto df2 = df.Filter("myFriend.MyCol == 42");
1434~~~
1435
1436The same applies for TChains. Columns coming from the friend trees can be referred to by their full name, like in the example above,
1437or the friend tree name can be omitted in case the column name is not ambiguous (e.g. "MyCol" could be used instead of
1438"myFriend.MyCol" in the example above if there is no column "MyCol" in the main tree).
1439
1440\note A common source of confusion is that trees that are written out from a multi-thread Snapshot() call will have their
1441 entries (block-wise) shuffled with respect to the original tree. Such trees cannot be used as friends of the original
1442 one: rows will be mismatched.
1443
1444Indexed friend trees provide a way to perform simple joins of multiple trees over a common column.
1445When a certain entry in the main tree (or chain) is loaded, the friend trees (or chains) will then load an entry where the
1446"index" columns have a value identical to the one in the main one. For example, in Python:
1447
1448~~~{.py}
1449main_tree = ...
1450aux_tree = ...
1451
1452# If a friend tree has an index on `commonColumn`, when the main tree loads
1453# a given row, it also loads the row of the friend tree that has the same
1454# value of `commonColumn`
1455aux_tree.BuildIndex("commonColumn")
1456
1457mainTree.AddFriend(aux_tree)
1458
1459df = ROOT.RDataFrame(mainTree)
1460~~~
1461
1462RDataFrame supports indexed friend TTrees from ROOT v6.24 in single-thread mode and from v6.28/02 in multi-thread mode.
1463
1464\anchor other-file-formats
1465### Reading data formats other than ROOT trees
1466RDataFrame can be interfaced with RDataSources. The ROOT::RDF::RDataSource interface defines an API that RDataFrame can use to read arbitrary columnar data formats.
1467
1468RDataFrame calls into concrete RDataSource implementations to retrieve information about the data, retrieve (thread-local) readers or "cursors" for selected columns
1469and to advance the readers to the desired data entry.
1470Some predefined RDataSources are natively provided by ROOT such as the ROOT::RDF::RCsvDS which allows to read comma separated files:
1471~~~{.cpp}
1472auto tdf = ROOT::RDF::FromCSV("MuRun2010B.csv");
1473auto filteredEvents =
1474 tdf.Filter("Q1 * Q2 == -1")
1475 .Define("m", "sqrt(pow(E1 + E2, 2) - (pow(px1 + px2, 2) + pow(py1 + py2, 2) + pow(pz1 + pz2, 2)))");
1476auto h = filteredEvents.Histo1D("m");
1477h->Draw();
1478~~~
1479
1480See also FromNumpy (Python-only), FromRNTuple(), FromArrow(), FromSqlite().
1481
1482\anchor callgraphs
1483### Computation graphs (storing and reusing sets of transformations)
1484
1485As we saw, transformed dataframes can be stored as variables and reused multiple times to create modified versions of the dataset. This implicitly defines a **computation graph** in which
1486several paths of filtering/creation of columns are executed simultaneously, and finally aggregated results are produced.
1487
1488RDataFrame detects when several actions use the same filter or the same defined column, and **only evaluates each
1489filter or defined column once per event**, regardless of how many times that result is used down the computation graph.
1490Objects read from each column are **built once and never copied**, for maximum efficiency.
1491When "upstream" filters are not passed, subsequent filters, temporary column expressions and actions are not evaluated,
1492so it might be advisable to put the strictest filters first in the graph.
1493
1494\anchor representgraph
1495### Visualizing the computation graph
1496It is possible to print the computation graph from any node to obtain a [DOT (graphviz)](https://en.wikipedia.org/wiki/DOT_(graph_description_language)) representation either on the standard output
1497or in a file.
1498
1499Invoking the function ROOT::RDF::SaveGraph() on any node that is not the head node, the computation graph of the branch
1500the node belongs to is printed. By using the head node, the entire computation graph is printed.
1501
1502Following there is an example of usage:
1503~~~{.cpp}
1504// First, a sample computational graph is built
1505ROOT::RDataFrame df("tree", "f.root");
1506
1507auto df2 = df.Define("x", []() { return 1; })
1508 .Filter("col0 % 1 == col0")
1509 .Filter([](int b1) { return b1 <2; }, {"cut1"})
1510 .Define("y", []() { return 1; });
1511
1512auto count = df2.Count();
1513
1514// Prints the graph to the rd1.dot file in the current directory
1515ROOT::RDF::SaveGraph(df, "./mydot.dot");
1516// Prints the graph to standard output
1517ROOT::RDF::SaveGraph(df);
1518~~~
1519
1520The generated graph can be rendered using one of the graphviz filters, e.g. `dot`. For instance, the image below can be generated with the following command:
1521~~~{.sh}
1522$ dot -Tpng computation_graph.dot -ocomputation_graph.png
1523~~~
1524
1525\image html RDF_Graph2.png
1526
1527\anchor rdf-logging
1528### Activating RDataFrame execution logs
1529
1530RDataFrame has experimental support for verbose logging of the event loop runtimes and other interesting related information. It is activated as follows:
1531~~~{.cpp}
1532#include <ROOT/RLogger.hxx>
1533
1534// this increases RDF's verbosity level as long as the `verbosity` variable is in scope
1535auto verbosity = ROOT::Experimental::RLogScopedVerbosity(ROOT::Detail::RDF::RDFLogChannel(), ROOT::Experimental::ELogLevel::kInfo);
1536~~~
1537
1538or in Python:
1539~~~{.python}
1540import ROOT
1541
1542verbosity = ROOT.Experimental.RLogScopedVerbosity(ROOT.Detail.RDF.RDFLogChannel(), ROOT.Experimental.ELogLevel.kInfo)
1543~~~
1544
1545More information (e.g. start and end of each multi-thread task) is printed using `ELogLevel.kDebug` and even more
1546(e.g. a full dump of the generated code that RDataFrame just-in-time-compiles) using `ELogLevel.kDebug+10`.
1547
1548\anchor rdf-from-spec
1549### Creating an RDataFrame from a dataset specification file
1550
1551RDataFrame can be created using a dataset specification JSON file:
1552
1553~~~{.python}
1554import ROOT
1555
1556df = ROOT.RDF.Experimental.FromSpec("spec.json")
1557~~~
1558
1559The input dataset specification JSON file needs to be provided by the user and it describes all necessary samples and
1560their associated metadata information. The main required key is the "samples" (at least one sample is needed) and the
1561required sub-keys for each sample are "trees" and "files". Additionally, one can specify a metadata dictionary for each
1562sample in the "metadata" key.
1563
1564A simple example for the formatting of the specification in the JSON file is the following:
1565
1566~~~{.cpp}
1567{
1568 "samples": {
1569 "sampleA": {
1570 "trees": ["tree1", "tree2"],
1571 "files": ["file1.root", "file2.root"],
1572 "metadata": {
1573 "lumi": 10000.0,
1574 "xsec": 1.0,
1575 "sample_category" = "data"
1576 }
1577 },
1578 "sampleB": {
1579 "trees": ["tree3", "tree4"],
1580 "files": ["file3.root", "file4.root"],
1581 "metadata": {
1582 "lumi": 0.5,
1583 "xsec": 1.5,
1584 "sample_category" = "MC_background"
1585 }
1586 }
1587 }
1588}
1589~~~
1590
1591The metadata information from the specification file can be then accessed using the DefinePerSample function.
1592For example, to access luminosity information (stored as a double):
1593
1594~~~{.python}
1595df.DefinePerSample("lumi", 'rdfsampleinfo_.GetD("lumi")')
1596~~~
1597
1598or sample_category information (stored as a string):
1599
1600~~~{.python}
1601df.DefinePerSample("sample_category", 'rdfsampleinfo_.GetS("sample_category")')
1602~~~
1603
1604or directly the filename:
1605
1606~~~{.python}
1607df.DefinePerSample("name", "rdfsampleinfo_.GetSampleName()")
1608~~~
1609
1610An example implementation of the "FromSpec" method is available in tutorial: df106_HiggstoFourLeptons.py, which also
1611provides a corresponding exemplary JSON file for the dataset specification.
1612
1613\anchor progressbar
1614### Adding a progress bar
1615
1616A progress bar showing the processed event statistics can be added to any RDataFrame program.
1617The event statistics include elapsed time, currently processed file, currently processed events, the rate of event processing
1618and an estimated remaining time (per file being processed). It is recorded and printed in the terminal every m events and every
1619n seconds (by default m = 1000 and n = 1). The ProgressBar can be also added when the multithread (MT) mode is enabled.
1620
1621ProgressBar is added after creating the dataframe object (df):
1622~~~{.cpp}
1623ROOT::RDataFrame df("tree", "file.root");
1624ROOT::RDF::Experimental::AddProgressBar(df);
1625~~~
1626
1627Alternatively, RDataFrame can be cast to an RNode first, giving the user more flexibility
1628For example, it can be called at any computational node, such as Filter or Define, not only the head node,
1629with no change to the ProgressBar function itself (please see the [Efficient analysis in Python](#python)
1630section for appropriate usage in Python):
1631~~~{.cpp}
1632ROOT::RDataFrame df("tree", "file.root");
1633auto df_1 = ROOT::RDF::RNode(df.Filter("x>1"));
1634ROOT::RDF::Experimental::AddProgressBar(df_1);
1635~~~
1636Examples of implemented progress bars can be seen by running [Higgs to Four Lepton tutorial](https://root.cern/doc/master/df106__HiggsToFourLeptons_8py_source.html) and [Dimuon tutorial](https://root.cern/doc/master/df102__NanoAODDimuonAnalysis_8C.html).
1637
1638\anchor missing-values
1639### Working with missing values in the dataset
1640
1641In certain situations a dataset might be missing one or more values at one or
1642more of its entries. For example:
1643
1644- If the dataset is composed of multiple files and one or more files is
1645 missing one or more columns required by the analysis.
1646- When joining different datasets horizontally according to some index value
1647 (e.g. the event number), if the index does not find a match in one or more
1648 other datasets for a certain entry.
1649
1650For example, suppose that column "y" does not have a value for entry 42:
1651
1652\code{.unparsed}
1653+-------+---+---+
1654| Entry | x | y |
1655+-------+---+---+
1656| 42 | 1 | |
1657+-------+---+---+
1658\endcode
1659
1660If the RDataFrame application reads that column, for example if a Take() action
1661was requested, the default behaviour is to throw an exception indicating
1662that that column is missing an entry.
1663
1664The following paragraphs discuss the functionalities provided by RDataFrame to
1665work with missing values in the dataset.
1666
1667#### FilterAvailable and FilterMissing
1668
1669FilterAvailable and FilterMissing are specialized RDataFrame Filter operations.
1670They take as input argument the name of a column of the dataset to watch for
1671missing values. Like Filter, they will either keep or discard an entire entry
1672based on whether a condition returns true or false. Specifically:
1673
1674- FilterAvailable: the condition is whether the value of the column is present.
1675 If so, the entry is kept. Otherwise if the value is missing the entry is
1676 discarded.
1677- FilterMissing: the condition is whether the value of the column is missing. If
1678 so, the entry is kept. Otherwise if the value is present the entry is
1679 discarded.
1680
1681\code{.py}
1682df = ROOT.RDataFrame(dataset)
1683
1684# Anytime an entry from "col" is missing, the entire entry will be filtered out
1685df_available = df.FilterAvailable("col")
1686df_available = df_available.Define("twice", "col * 2")
1687
1688# Conversely, if we want to select the entries for which the column has missing
1689# values, we do the following
1690df_missingcol = df.FilterMissing("col")
1691# Following operations in the same branch of the computation graph clearly
1692# cannot access that same column, since there would be no value to read
1693df_missingcol = df_missingcol.Define("observable", "othercolumn * 2")
1694\endcode
1695
1696\code{.cpp}
1697ROOT::RDataFrame df{dataset};
1698
1699// Anytime an entry from "col" is missing, the entire entry will be filtered out
1700auto df_available = df.FilterAvailable("col");
1701auto df_twicecol = df_available.Define("twice", "col * 2");
1702
1703// Conversely, if we want to select the entries for which the column has missing
1704// values, we do the following
1705auto df_missingcol = df.FilterMissing("col");
1706// Following operations in the same branch of the computation graph clearly
1707// cannot access that same column, since there would be no value to read
1708auto df_observable = df_missingcol.Define("observable", "othercolumn * 2");
1709\endcode
1710
1711#### DefaultValueFor
1712
1713DefaultValueFor creates a node of the computation graph which just forwards the
1714values of the columns necessary for other downstream nodes, when they are
1715available. In case a value of the input column passed to this function is not
1716available, the node will provide the default value passed to this function call
1717instead. Example:
1718
1719\code{.py}
1720df = ROOT.RDataFrame(dataset)
1721# Anytime an entry from "col" is missing, the value will be the default one
1722default_value = ... # Some sensible default value here
1723df = df.DefaultValueFor("col", default_value)
1724df = df.Define("twice", "col * 2")
1725\endcode
1726
1727\code{.cpp}
1728ROOT::RDataFrame df{dataset};
1729// Anytime an entry from "col" is missing, the value will be the default one
1730constexpr auto default_value = ... // Some sensible default value here
1731auto df_default = df.DefaultValueFor("col", default_value);
1732auto df_col = df_default.Define("twice", "col * 2");
1733\endcode
1734
1735#### Mixing different strategies to work with missing values in the same RDataFrame
1736
1737All the operations presented above only act on the particular branch of the
1738computation graph where they are called, so that different results can be
1739obtained by mixing and matching the filtering or providing a default value
1740strategies:
1741
1742\code{.py}
1743df = ROOT.RDataFrame(dataset)
1744# Anytime an entry from "col" is missing, the value will be the default one
1745default_value = ... # Some sensible default value here
1746df_default = df.DefaultValueFor("col", default_value).Define("twice", "col * 2")
1747df_filtered = df.FilterAvailable("col").Define("twice", "col * 2")
1748
1749# Same number of total entries as the input dataset, with defaulted values
1750df_default.Display(["twice"]).Print()
1751# Only keep the entries where "col" has values
1752df_filtered.Display(["twice"]).Print()
1753\endcode
1754
1755\code{.cpp}
1756ROOT::RDataFrame df{dataset};
1757
1758// Anytime an entry from "col" is missing, the value will be the default one
1759constexpr auto default_value = ... // Some sensible default value here
1760auto df_default = df.DefaultValueFor("col", default_value).Define("twice", "col * 2");
1761auto df_filtered = df.FilterAvailable("col").Define("twice", "col * 2");
1762
1763// Same number of total entries as the input dataset, with defaulted values
1764df_default.Display({"twice"})->Print();
1765// Only keep the entries where "col" has values
1766df_filtered.Display({"twice"})->Print();
1767\endcode
1768
1769#### Further considerations
1770
1771Note that working with missing values is currently supported with a TTree-based
1772data source. Support of this functionality for other data sources may come in
1773the future.
1774
1775*/
1776// clang-format on
1777
1778namespace ROOT {
1779
1781using ColumnNamesPtr_t = std::shared_ptr<const ColumnNames_t>;
1782
1783////////////////////////////////////////////////////////////////////////////
1784/// \brief Build the dataframe.
1785/// \param[in] treeName Name of the tree contained in the directory
1786/// \param[in] dirPtr TDirectory where the tree is stored, e.g. a TFile.
1787/// \param[in] defaultColumns Collection of default columns.
1788///
1789/// The default columns are looked at in case no column is specified in the
1790/// booking of actions or transformations.
1791/// \see ROOT::RDF::RInterface for the documentation of the methods available.
1792RDataFrame::RDataFrame(std::string_view treeName, TDirectory *dirPtr, const ColumnNames_t &defaultColumns)
1793 : RInterface(std::make_shared<RDFDetail::RLoopManager>(nullptr, defaultColumns))
1794{
1795 if (!dirPtr) {
1796 auto msg = "Invalid TDirectory!";
1797 throw std::runtime_error(msg);
1798 }
1799 const std::string treeNameInt(treeName);
1800 auto tree = static_cast<TTree *>(dirPtr->Get(treeNameInt.c_str()));
1801 if (!tree) {
1802 auto msg = "Tree \"" + treeNameInt + "\" cannot be found!";
1803 throw std::runtime_error(msg);
1804 }
1805 GetProxiedPtr()->SetTree(std::shared_ptr<TTree>(tree, [](TTree *) {}));
1806}
1807
1808////////////////////////////////////////////////////////////////////////////
1809/// \brief Build the dataframe.
1810/// \param[in] treeName Name of the tree contained in the directory
1811/// \param[in] filenameglob TDirectory where the tree is stored, e.g. a TFile.
1812/// \param[in] defaultColumns Collection of default columns.
1813///
1814/// The filename glob supports the same type of expressions as TChain::Add(), and it is passed as-is to TChain's
1815/// constructor.
1816///
1817/// The default columns are looked at in case no column is specified in the
1818/// booking of actions or transformations.
1819/// \see ROOT::RDF::RInterface for the documentation of the methods available.
1820#ifdef R__HAS_ROOT7
1821RDataFrame::RDataFrame(std::string_view treeName, std::string_view fileNameGlob, const ColumnNames_t &defaultColumns)
1822 : RInterface(ROOT::Detail::RDF::CreateLMFromFile(treeName, fileNameGlob, defaultColumns))
1823{
1824}
1825#else
1826RDataFrame::RDataFrame(std::string_view treeName, std::string_view fileNameGlob, const ColumnNames_t &defaultColumns)
1827 : RInterface(ROOT::Detail::RDF::CreateLMFromTTree(treeName, fileNameGlob, defaultColumns))
1828{
1829}
1830#endif
1831
1832////////////////////////////////////////////////////////////////////////////
1833/// \brief Build the dataframe.
1834/// \param[in] treeName Name of the tree contained in the directory
1835/// \param[in] fileglobs Collection of file names of filename globs
1836/// \param[in] defaultColumns Collection of default columns.
1837///
1838/// The filename globs support the same type of expressions as TChain::Add(), and each glob is passed as-is
1839/// to TChain's constructor.
1840///
1841/// The default columns are looked at in case no column is specified in the booking of actions or transformations.
1842/// \see ROOT::RDF::RInterface for the documentation of the methods available.
1843#ifdef R__HAS_ROOT7
1844RDataFrame::RDataFrame(std::string_view datasetName, const std::vector<std::string> &fileNameGlobs,
1845 const ColumnNames_t &defaultColumns)
1846 : RInterface(ROOT::Detail::RDF::CreateLMFromFile(datasetName, fileNameGlobs, defaultColumns))
1847{
1848}
1849#else
1850RDataFrame::RDataFrame(std::string_view datasetName, const std::vector<std::string> &fileNameGlobs,
1851 const ColumnNames_t &defaultColumns)
1852 : RInterface(ROOT::Detail::RDF::CreateLMFromTTree(datasetName, fileNameGlobs, defaultColumns))
1853{
1854}
1855#endif
1856
1857////////////////////////////////////////////////////////////////////////////
1858/// \brief Build the dataframe.
1859/// \param[in] tree The tree or chain to be studied.
1860/// \param[in] defaultColumns Collection of default column names to fall back to when none is specified.
1861///
1862/// The default columns are looked at in case no column is specified in the
1863/// booking of actions or transformations.
1864/// \see ROOT::RDF::RInterface for the documentation of the methods available.
1865RDataFrame::RDataFrame(TTree &tree, const ColumnNames_t &defaultColumns)
1866 : RInterface(std::make_shared<RDFDetail::RLoopManager>(&tree, defaultColumns))
1867{
1868}
1869
1870//////////////////////////////////////////////////////////////////////////
1871/// \brief Build a dataframe that generates numEntries entries.
1872/// \param[in] numEntries The number of entries to generate.
1873///
1874/// An empty-source dataframe constructed with a number of entries will
1875/// generate those entries on the fly when some action is triggered,
1876/// and it will do so for all the previously-defined columns.
1877/// \see ROOT::RDF::RInterface for the documentation of the methods available.
1879 : RInterface(std::make_shared<RDFDetail::RLoopManager>(numEntries))
1880
1881{
1882}
1883
1884//////////////////////////////////////////////////////////////////////////
1885/// \brief Build dataframe associated to data source.
1886/// \param[in] ds The data source object.
1887/// \param[in] defaultColumns Collection of default column names to fall back to when none is specified.
1888///
1889/// A dataframe associated to a data source will query it to access column values.
1890/// \see ROOT::RDF::RInterface for the documentation of the methods available.
1891RDataFrame::RDataFrame(std::unique_ptr<ROOT::RDF::RDataSource> ds, const ColumnNames_t &defaultColumns)
1892 : RInterface(std::make_shared<RDFDetail::RLoopManager>(std::move(ds), defaultColumns))
1893{
1894}
1895
1896//////////////////////////////////////////////////////////////////////////
1897/// \brief Build dataframe from an RDatasetSpec object.
1898/// \param[in] spec The dataset specification object.
1899///
1900/// A dataset specification includes trees and file names,
1901/// as well as an optional friend list and/or entry range.
1902///
1903/// ### Example usage from Python:
1904/// ~~~{.py}
1905/// spec = (
1906/// ROOT.RDF.Experimental.RDatasetSpec()
1907/// .AddSample(("data", "tree", "file.root"))
1908/// .WithGlobalFriends("friendTree", "friend.root", "alias")
1909/// .WithGlobalRange((100, 200))
1910/// )
1911/// df = ROOT.RDataFrame(spec)
1912/// ~~~
1913///
1914/// See also ROOT::RDataFrame::FromSpec().
1916 : RInterface(std::make_shared<RDFDetail::RLoopManager>(std::move(spec)))
1917{
1918}
1919
1921{
1922 // If any node of the computation graph associated with this RDataFrame
1923 // declared code to jit, we need to make sure the compilation actually
1924 // happens. For example, a jitted Define could have been booked but
1925 // if the computation graph is not actually run then the code of the
1926 // Define node is not jitted. This in turn would cause memory leaks.
1927 // See https://github.com/root-project/root/issues/15399
1928 fLoopManager->Jit();
1929}
1930
1931namespace RDF {
1932namespace Experimental {
1933
1934////////////////////////////////////////////////////////////////////////////
1935/// \brief Create the RDataFrame from the dataset specification file.
1936/// \param[in] jsonFile Path to the dataset specification JSON file.
1937///
1938/// The input dataset specification JSON file must include a number of keys that
1939/// describe all the necessary samples and their associated metadata information.
1940/// The main key, "samples", is required and at least one sample is needed. Each
1941/// sample must have at least one key "trees" and at least one key "files" from
1942/// which the data is read. Optionally, one or more metadata information can be
1943/// added, as well as the friend list information.
1944///
1945/// ### Example specification file JSON:
1946/// The following is an example of the dataset specification JSON file formatting:
1947///~~~{.cpp}
1948/// {
1949/// "samples": {
1950/// "sampleA": {
1951/// "trees": ["tree1", "tree2"],
1952/// "files": ["file1.root", "file2.root"],
1953/// "metadata": {"lumi": 1.0, }
1954/// },
1955/// "sampleB": {
1956/// "trees": ["tree3", "tree4"],
1957/// "files": ["file3.root", "file4.root"],
1958/// "metadata": {"lumi": 0.5, }
1959/// },
1960/// ...
1961/// },
1962/// }
1963///~~~
1964ROOT::RDataFrame FromSpec(const std::string &jsonFile)
1965{
1966 const nlohmann::ordered_json fullData = nlohmann::ordered_json::parse(std::ifstream(jsonFile));
1967 if (!fullData.contains("samples") || fullData["samples"].empty()) {
1968 throw std::runtime_error(
1969 R"(The input specification does not contain any samples. Please provide the samples in the specification like:
1970{
1971 "samples": {
1972 "sampleA": {
1973 "trees": ["tree1", "tree2"],
1974 "files": ["file1.root", "file2.root"],
1975 "metadata": {"lumi": 1.0, }
1976 },
1977 "sampleB": {
1978 "trees": ["tree3", "tree4"],
1979 "files": ["file3.root", "file4.root"],
1980 "metadata": {"lumi": 0.5, }
1981 },
1982 ...
1983 },
1984})");
1985 }
1986
1987 RDatasetSpec spec;
1988 for (const auto &keyValue : fullData["samples"].items()) {
1989 const std::string &sampleName = keyValue.key();
1990 const auto &sample = keyValue.value();
1991 // TODO: if requested in https://github.com/root-project/root/issues/11624
1992 // allow union-like types for trees and files, see: https://github.com/nlohmann/json/discussions/3815
1993 if (!sample.contains("trees")) {
1994 throw std::runtime_error("A list of tree names must be provided for sample " + sampleName + ".");
1995 }
1996 std::vector<std::string> trees = sample["trees"];
1997 if (!sample.contains("files")) {
1998 throw std::runtime_error("A list of files must be provided for sample " + sampleName + ".");
1999 }
2000 std::vector<std::string> files = sample["files"];
2001 if (!sample.contains("metadata")) {
2002 spec.AddSample(RSample{sampleName, trees, files});
2003 } else {
2004 RMetaData m;
2005 for (const auto &metadata : sample["metadata"].items()) {
2006 const auto &val = metadata.value();
2007 if (val.is_string())
2008 m.Add(metadata.key(), val.get<std::string>());
2009 else if (val.is_number_integer())
2010 m.Add(metadata.key(), val.get<int>());
2011 else if (val.is_number_float())
2012 m.Add(metadata.key(), val.get<double>());
2013 else
2014 throw std::logic_error("The metadata keys can only be of type [string|int|double].");
2015 }
2016 spec.AddSample(RSample{sampleName, trees, files, m});
2017 }
2018 }
2019 if (fullData.contains("friends")) {
2020 for (const auto &friends : fullData["friends"].items()) {
2021 std::string alias = friends.key();
2022 std::vector<std::string> trees = friends.value()["trees"];
2023 std::vector<std::string> files = friends.value()["files"];
2024 if (files.size() != trees.size() && trees.size() > 1)
2025 throw std::runtime_error("Mismatch between trees and files in a friend.");
2026 spec.WithGlobalFriends(trees, files, alias);
2027 }
2028 }
2029
2030 if (fullData.contains("range")) {
2031 std::vector<int> range = fullData["range"];
2032
2033 if (range.size() == 1)
2034 spec.WithGlobalRange({range[0]});
2035 else if (range.size() == 2)
2036 spec.WithGlobalRange({range[0], range[1]});
2037 }
2038 return ROOT::RDataFrame(spec);
2039}
2040
2041} // namespace Experimental
2042} // namespace RDF
2043
2044} // namespace ROOT
2045
2046namespace cling {
2047//////////////////////////////////////////////////////////////////////////
2048/// Print an RDataFrame at the prompt
2049std::string printValue(ROOT::RDataFrame *df)
2050{
2051 // The loop manager is never null, except when its construction failed.
2052 // This can happen e.g. if the constructor of RLoopManager that expects
2053 // a file name is used and that file doesn't exist. This point is usually
2054 // not even reached in that situation, since the exception thrown by the
2055 // constructor will also stop execution of the program. But it can still
2056 // be reached at the prompt, if the user tries to print the RDataFrame
2057 // variable after an incomplete initialization.
2058 auto *lm = df->GetLoopManager();
2059 if (!lm) {
2060 throw std::runtime_error("Cannot print information about this RDataFrame, "
2061 "it was not properly created. It must be discarded.");
2062 }
2063 auto *tree = lm->GetTree();
2064 auto defCols = lm->GetDefaultColumnNames();
2065
2066 std::ostringstream ret;
2067 if (tree) {
2068 ret << "A data frame built on top of the " << tree->GetName() << " dataset.";
2069 if (!defCols.empty()) {
2070 if (defCols.size() == 1)
2071 ret << "\nDefault column: " << defCols[0];
2072 else {
2073 ret << "\nDefault columns:\n";
2074 for (auto &&col : defCols) {
2075 ret << " - " << col << "\n";
2076 }
2077 }
2078 }
2079 } else if (auto ds = df->fDataSource) {
2080 ret << "A data frame associated to the data source \"" << cling::printValue(ds) << "\"";
2081 } else {
2082 ret << "An empty data frame that will create " << lm->GetNEmptyEntries() << " entries\n";
2083 }
2084
2085 return ret.str();
2086}
2087} // namespace cling
unsigned long long ULong64_t
Definition RtypesCore.h:70
The head node of a RDF computation graph.
The dataset specification for RDataFrame.
RDatasetSpec & WithGlobalFriends(const std::string &treeName, const std::string &fileNameGlob, const std::string &alias="")
Add friend tree to RDatasetSpec object.
RDatasetSpec & AddSample(RSample sample)
Add sample (RSample class object) to the RDatasetSpec object.
RDatasetSpec & WithGlobalRange(const RDatasetSpec::REntryRange &entryRange={})
Create an RDatasetSpec object for a given range of entries.
Class behaving as a heterogenuous dictionary to store the metadata of a dataset.
Definition RMetaData.hxx:50
Class representing a sample which is a grouping of trees and their fileglobs, and,...
Definition RSample.hxx:39
std::shared_ptr< ROOT::Detail::RDF::RLoopManager > fLoopManager
< The RLoopManager at the root of this computation graph. Never null.
RDataSource * fDataSource
Non-owning pointer to a data-source object. Null if no data-source. RLoopManager has ownership of the...
RDFDetail::RLoopManager * GetLoopManager() const
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
RDataFrame(std::string_view treeName, std::string_view filenameglob, const ColumnNames_t &defaultColumns={})
Build the dataframe.
ROOT::RDF::ColumnNames_t ColumnNames_t
Describe directory structure in memory.
Definition TDirectory.h:45
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
A TTree represents a columnar dataset.
Definition TTree.h:79
ROOT::RDataFrame FromSpec(const std::string &jsonFile)
Factory method to create an RDataFrame from a JSON specification file.
std::vector< std::string > ColumnNames_t
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
std::shared_ptr< const ColumnNames_t > ColumnNamesPtr_t
TMarker m
Definition textangle.C:8