Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
df016_vecOps.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_dataframe
3## \notebook -draw
4## Process collections in RDataFrame with the help of RVec.
5##
6## This tutorial shows the potential of the VecOps approach for treating collections
7## stored in datasets, a situation very common in HEP data analysis.
8##
9## \macro_image
10## \macro_code
11##
12## \date February 2018
13## \author Danilo Piparo (CERN)
14
15import ROOT
16
17df = ROOT.RDataFrame(1024)
18coordDefineCode = '''ROOT::RVecD {0}(len);
19 std::transform({0}.begin(), {0}.end(), {0}.begin(), [](double){{return gRandom->Uniform(-1.0, 1.0);}});
20 return {0};'''
21d = df.Define("len", "gRandom->Uniform(0, 16)")\
22 .Define("x", coordDefineCode.format("x"))\
23 .Define("y", coordDefineCode.format("y"))
24
25# Now we have in our hands d, a RDataFrame with two columns, x and y, which
26# hold collections of coordinates. The sizes of these collections vary.
27# Let's now define radii radii from the x and y coordinates. We'll do it treating
28# the collections stored in the columns without looping on the individual elements.
29d1 = d.Define("r", "sqrt(x*x + y*y)")
30
31# Now we want to plot 2 quarters of a ring with radii .5 and 1.
32# Note how the cuts are performed on RVecs, comparing them with integers and
33# among themselves.
34ring_h = d1.Define("rInFig", "r > .5 && r < 1 && x*y < 0")\
35 .Define("yFig", "y[rInFig]")\
36 .Define("xFig", "x[rInFig]")\
37 .Histo2D(("fig", "Two quarters of a ring", 64, -1.1, 1.1, 64, -1.1, 1.1), "xFig", "yFig")
38
39cring = ROOT.TCanvas()
40ring_h.Draw("Colz")
41cring.SaveAs("df016_ring.png")
42
43print("Saved figure to df016_ring.png")
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...