Logo ROOT   6.12/06
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Namespaces
tdf001_introduction.py File Reference

Namespaces

 tdf001_introduction
 

Detailed Description

This tutorial illustrates the basic features of the TDataFrame class, a utility which allows to interact with data stored in TTrees following a functional-chain like approach.

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

Definition in file tdf001_introduction.py.