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