Logo ROOT   6.10/09
Reference Guide
tdf001_introduction.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_tdataframe
3 ## \notebook -nodraw
4 ## This tutorial illustrates the basic features of the TDataFrame class,
5 ## a utility which allows to interact with data stored in TTrees following
6 ## a functional-chain like approach.
7 ##
8 ## \macro_code
9 ##
10 ## \date May 2017
11 ## \author Danilo Piparo
12 
13 import ROOT
14 
15 # A simple helper function to fill a test tree: this makes the example stand-alone.
16 fill_tree_code = '''
17 void fill_tree(const char *filename, const char *treeName)
18 {
19  TFile f(filename, "RECREATE");
20  TTree t(treeName, treeName);
21  double b1;
22  int b2;
23  t.Branch("b1", &b1);
24  t.Branch("b2", &b2);
25  for (int i = 0; i < 10; ++i) {
26  b1 = i;
27  b2 = i * i;
28  t.Fill();
29  }
30  t.Write();
31  f.Close();
32  return;
33 }
34 '''
35 # We prepare an input tree to run on
36 fileName = "tdf001_introduction_py.root"
37 treeName = "myTree"
38 ROOT.gInterpreter.Declare(fill_tree_code)
39 ROOT.fill_tree(fileName, treeName)
40 
41 
42 # We read the tree from the file and create a TDataFrame, a class that
43 # allows us to interact with the data contained in the tree.
44 TDF = ROOT.ROOT.Experimental.TDataFrame
45 d = TDF(treeName, fileName)
46 
47 # Operations on the dataframe
48 # We now review some *actions* which can be performed on the data frame.
49 # All actions but ForEach return a TActionResultPtr<T>. The series of
50 # operations on the data frame is not executed until one of those pointers
51 # is accessed.
52 # But first of all, let us we define now our cut-flow with two strings.
53 # Filters can be expressed as strings. The content must be C++ code. The
54 # name of the variables must be the name of the branches. The code is
55 # just in time compiled.
56 cutb1 = 'b1 < 5.'
57 cutb1b2 = 'b2 % 2 && b1 < 4.'
58 
59 # `Count` action
60 # The `Count` allows to retrieve the number of the entries that passed the
61 # filters. Here we show how the automatic selection of the column kicks
62 # in in case the user specifies none.
63 entries1 = d.Filter(cutb1) \
64  .Filter(cutb1b2) \
65  .Count();
66 
67 print("%s entries passed all filters" %entries1.GetValue())
68 
69 entries2 = d.Filter("b1 < 5.").Count();
70 print("%s entries passed all filters" %entries2.GetValue())
71 
72 # `Min`, `Max` and `Mean` actions
73 # These actions allow to retrieve statistical information about the entries
74 # passing the cuts, if any.
75 b1b2_cut = d.Filter(cutb1b2)
76 minVal = b1b2_cut.Min('b1')
77 maxVal = b1b2_cut.Max('b1')
78 meanVal = b1b2_cut.Mean('b1')
79 nonDefmeanVal = b1b2_cut.Mean("b2")
80 print("The mean is always included between the min and the max: %s <= %s <= %s" %(minVal.GetValue(), meanVal.GetValue(), maxVal.GetValue()))
81 
82 # `Histo1D` action
83 # The `Histo1D` action allows to fill an histogram. It returns a TH1F filled
84 # with values of the column that passed the filters. For the most common
85 # types, the type of the values stored in the column is automatically
86 # guessed.
87 hist = d.Filter(cutb1).Histo1D('b1')
88 print("Filled h %s times, mean: %s" %(hist.GetEntries(), hist.GetMean()))
89 
90 # Express your chain of operations with clarity!
91 # We are discussing an example here but it is not hard to imagine much more
92 # complex pipelines of actions acting on data. Those might require code
93 # which is well organised, for example allowing to conditionally add filters
94 # or again to clearly separate filters and actions without the need of
95 # writing the entire pipeline on one line. This can be easily achieved.
96 # We'll show this re-working the `Count` example:
97 cutb1_result = d.Filter(cutb1);
98 cutb1b2_result = d.Filter(cutb1b2);
99 cutb1_cutb1b2_result = cutb1_result.Filter(cutb1b2)
100 
101 # Now we want to count:
102 evts_cutb1_result = cutb1_result.Count()
103 evts_cutb1b2_result = cutb1b2_result.Count()
104 evts_cutb1_cutb1b2_result = cutb1_cutb1b2_result.Count()
105 
106 print("Events passing cutb1: %s" %evts_cutb1_result.GetValue())
107 print("Events passing cutb1b2: %s" %evts_cutb1b2_result.GetValue())
108 print("Events passing both: %s" %evts_cutb1_cutb1b2_result.GetValue())
109 
110 # Calculating quantities starting from existing columns
111 # Often, operations need to be carried out on quantities calculated starting
112 # from the ones present in the columns. We'll create in this example a third
113 # column the values of which are the sum of the *b1* and *b2* ones, entry by
114 # entry. The way in which the new quantity is defined is via a runable.
115 # It is important to note two aspects at this point:
116 # - The value is created on the fly only if the entry passed the existing
117 # filters.
118 # - The newly created column behaves as the one present on the file on disk.
119 # - The operation creates a new value, without modifying anything. De facto,
120 # this is like having a general container at disposal able to accommodate
121 # any value of any type.
122 # Let's dive in an example:
123 entries_sum = d.Define('sum', 'b2 + b1') \
124  .Filter('sum > 4.2') \
125  .Count()
126 print(entries_sum.GetValue())