Logo ROOT   6.18/05
Reference Guide
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
14import ROOT
15
16# A simple helper function to fill a test tree: this makes the example stand-alone.
17def 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
23fileName = "df001_introduction_py.root"
24treeName = "myTree"
25fill_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.
30RDF = ROOT.ROOT.RDataFrame
31d = 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.
42cutb1 = 'b1 < 5.'
43cutb1b2 = '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.
49entries1 = d.Filter(cutb1) \
50 .Filter(cutb1b2) \
51 .Count();
52
53print("%s entries passed all filters" %entries1.GetValue())
54
55entries2 = d.Filter("b1 < 5.").Count();
56print("%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.
61b1b2_cut = d.Filter(cutb1b2)
62minVal = b1b2_cut.Min('b1')
63maxVal = b1b2_cut.Max('b1')
64meanVal = b1b2_cut.Mean('b1')
65nonDefmeanVal = b1b2_cut.Mean("b2")
66print("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.
73hist = d.Filter(cutb1).Histo1D('b1')
74print("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:
83cutb1_result = d.Filter(cutb1);
84cutb1b2_result = d.Filter(cutb1b2);
85cutb1_cutb1b2_result = cutb1_result.Filter(cutb1b2)
86
87# Now we want to count:
88evts_cutb1_result = cutb1_result.Count()
89evts_cutb1b2_result = cutb1b2_result.Count()
90evts_cutb1_cutb1b2_result = cutb1_cutb1b2_result.Count()
91
92print("Events passing cutb1: %s" %evts_cutb1_result.GetValue())
93print("Events passing cutb1b2: %s" %evts_cutb1b2_result.GetValue())
94print("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:
109entries_sum = d.Define('sum', 'b2 + b1') \
110 .Filter('sum > 4.2') \
111 .Count()
112print(entries_sum.GetValue())
RVec< T > Filter(const RVec< T > &v, F &&f)
Create a new collection with the elements passing the filter expressed by the predicate.
Definition: RVec.hxx:936