Logo ROOT   6.10/00
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Namespaces
tdf006_ranges.py File Reference

Namespaces

 tdf006_ranges
 

Detailed Description

This tutorial shows how to express the concept of ranges when working with the TDataFrame.

1 
2 import ROOT
3 
4 fill_tree_code = '''
5 void fill_tree(const char *filename, const char *treeName)
6 {
7  TFile f(filename, "RECREATE");
8  TTree t(treeName, treeName);
9  int b1;
10  float b2;
11  t.Branch("b1", &b1);
12  t.Branch("b2", &b2);
13  for (int i = 0; i < 100; ++i) {
14  b1 = i;
15  b2 = i * i;
16  t.Fill();
17  }
18  t.Write();
19  f.Close();
20  return;
21 }
22 '''
23 
24 # We prepare an input tree to run on
25 fileName = "tdf006_ranges_py.root"
26 treeName = "myTree"
27 ROOT.gInterpreter.Declare(fill_tree_code)
28 ROOT.fill_tree(fileName, treeName)
29 
30 # We read the tree from the file and create a TDataFrame.
31 TDF = ROOT.ROOT.Experimental.TDataFrame
32 d = TDF(treeName, fileName)
33 
34 # ## Usage of ranges
35 # Now we'll count some entries using ranges
36 c_all = d.Count()
37 
38 # This is how you can express a range of the first 30 entries
39 d_0_30 = d.Range(0, 30)
40 c_0_30 = d_0_30.Count()
41 
42 # This is how you pick all entries from 15 onwards
43 d_15_end = d.Range(15, 0)
44 c_15_end = d_15_end.Count()
45 
46 # We can use a stride too, in this case we pick an event every 3
47 d_15_end_3 = d.Range(15, 0, 3)
48 c_15_end_3 = d_15_end_3.Count()
49 
50 # The Range is a 1st class citizen in the TDataFrame graph:
51 # not only actions (like Count) but also filters and new columns can be added to it.
52 d_0_50 = d.Range(0, 50)
53 c_0_50_odd_b1 = d_0_50.Filter("1 == b1 % 2").Count()
54 
55 # An important thing to notice is that the counts of a filter are relative to the
56 # number of entries a filter "sees". Therefore, if a Range depends on a filter,
57 # the Range will act on the entries passing the filter only.
58 c_0_3_after_even_b1 = d.Filter("0 == b1 % 2").Range(0, 3).Count()
59 
60 # Ok, time to wrap up: let's print all counts!
61 print("Usage of ranges:")
62 print(" - All entries:", c_all.GetValue())
63 print(" - Entries from 0 to 30:", c_0_30.GetValue())
64 print(" - Entries from 15 onwards:", c_15_end.GetValue())
65 print(" - Entries from 15 onwards in steps of 3:", c_15_end_3.GetValue())
66 print(" - Entries from 0 to 50, odd only:", c_0_50_odd_b1.GetValue())
67 print(" - First three entries of all even entries:", c_0_3_after_even_b1.GetValue())
Date
March 2017
Author
Danilo Piparo

Definition in file tdf006_ranges.py.