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