Logo ROOT  
Reference Guide
df006_ranges.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_dataframe
3/// \notebook -draw
4/// This tutorial shows how to express the concept of ranges when working with the RDataFrame.
5///
6/// \macro_code
7/// \macro_output
8///
9/// \date March 2017
10/// \author Danilo Piparo
11
12// A simple helper function to fill a test tree: this makes the example
13// stand-alone.
14void fill_tree(const char *treeName, const char *fileName)
15{
17 int i(0);
18 d.Define("b1", [&i]() { return i; })
19 .Define("b2",
20 [&i]() {
21 float j = i * i;
22 ++i;
23 return j;
24 })
25 .Snapshot(treeName, fileName);
26}
27
28int df006_ranges()
29{
30
31 // We prepare an input tree to run on
32 auto fileName = "df006_ranges.root";
33 auto treeName = "myTree";
34 fill_tree(treeName, fileName);
35
36 // We read the tree from the file and create a RDataFrame.
37 ROOT::RDataFrame d(treeName, fileName);
38
39 // ## Usage of ranges
40 // Now we'll count some entries using ranges
41 auto c_all = d.Count();
42
43 // This is how you can express a range of the first 30 entries
44 auto d_0_30 = d.Range(0, 30);
45 auto c_0_30 = d_0_30.Count();
46
47 // This is how you pick all entries from 15 onwards
48 auto d_15_end = d.Range(15, 0);
49 auto c_15_end = d_15_end.Count();
50
51 // We can use a stride too, in this case we pick an event every 3
52 auto d_15_end_3 = d.Range(15, 0, 3);
53 auto c_15_end_3 = d_15_end_3.Count();
54
55 // The Range is a 1st class citizen in the RDataFrame graph:
56 // not only actions (like Count) but also filters and new columns can be added to it.
57 auto d_0_50 = d.Range(0, 50);
58 auto c_0_50_odd_b1 = d_0_50.Filter("1 == b1 % 2").Count();
59
60 // An important thing to notice is that the counts of a filter are relative to the
61 // number of entries a filter "sees". Therefore, if a Range depends on a filter,
62 // the Range will act on the entries passing the filter only.
63 auto c_0_3_after_even_b1 = d.Filter("0 == b1 % 2").Range(0, 3).Count();
64
65 // Ok, time to wrap up: let's print all counts!
66 cout << "Usage of ranges:\n"
67 << " - All entries: " << *c_all << endl
68 << " - Entries from 0 to 30: " << *c_0_30 << endl
69 << " - Entries from 15 onwards: " << *c_15_end << endl
70 << " - Entries from 15 onwards in steps of 3: " << *c_15_end_3 << endl
71 << " - Entries from 0 to 50, odd only: " << *c_0_50_odd_b1 << endl
72 << " - First three entries of all even entries: " << *c_0_3_after_even_b1 << endl;
73
74 return 0;
75}
#define d(i)
Definition: RSha256.hxx:102
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTrees,...
Definition: RDataFrame.hxx:42