Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
df006_ranges.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_dataframe
3 ## \notebook -nodraw
4 ## This tutorial shows how to express the concept of ranges when working with the RDataFrame.
5 ##
6 ## \macro_code
7 ## \macro_output
8 ##
9 ## \date March 2017
10 ## \author Danilo Piparo
11 
12 import ROOT
13 
14 def fill_tree(treeName, fileName):
15  tdf = ROOT.ROOT.RDataFrame(100)
16  tdf.Define("b1", "(int) tdfentry_")\
17  .Define("b2", "(float) tdfentry_ * tdfentry_").Snapshot(treeName, fileName)
18 
19 
20 # We prepare an input tree to run on
21 fileName = "df006_ranges_py.root"
22 treeName = "myTree"
23 
24 fill_tree(treeName, fileName)
25 
26 # We read the tree from the file and create a RDataFrame.
27 RDF = ROOT.ROOT.RDataFrame
28 d = RDF(treeName, fileName)
29 
30 # ## Usage of ranges
31 # Now we'll count some entries using ranges
32 c_all = d.Count()
33 
34 # This is how you can express a range of the first 30 entries
35 d_0_30 = d.Range(0, 30)
36 c_0_30 = d_0_30.Count()
37 
38 # This is how you pick all entries from 15 onwards
39 d_15_end = d.Range(15, 0)
40 c_15_end = d_15_end.Count()
41 
42 # We can use a stride too, in this case we pick an event every 3
43 d_15_end_3 = d.Range(15, 0, 3)
44 c_15_end_3 = d_15_end_3.Count()
45 
46 # The Range is a 1st class citizen in the RDataFrame graph:
47 # not only actions (like Count) but also filters and new columns can be added to it.
48 d_0_50 = d.Range(0, 50)
49 c_0_50_odd_b1 = d_0_50.Filter("1 == b1 % 2").Count()
50 
51 # An important thing to notice is that the counts of a filter are relative to the
52 # number of entries a filter "sees". Therefore, if a Range depends on a filter,
53 # the Range will act on the entries passing the filter only.
54 c_0_3_after_even_b1 = d.Filter("0 == b1 % 2").Range(0, 3).Count()
55 
56 # Ok, time to wrap up: let's print all counts!
57 print("Usage of ranges:")
58 print(" - All entries:", c_all.GetValue())
59 print(" - Entries from 0 to 30:", c_0_30.GetValue())
60 print(" - Entries from 15 onwards:", c_15_end.GetValue())
61 print(" - Entries from 15 onwards in steps of 3:", c_15_end_3.GetValue())
62 print(" - Entries from 0 to 50, odd only:", c_0_50_odd_b1.GetValue())
63 print(" - First three entries of all even entries:", c_0_3_after_even_b1.GetValue())