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