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