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