ROOT   Reference Guide
df016_vecOps.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_dataframe
3 ## \notebook -draw
4 ## \brief 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
14
15 import ROOT
16
17 df = ROOT.RDataFrame(1024)
18 coordDefineCode = '''ROOT::VecOps::RVec<double> {0}(len);
19  std::transform({0}.begin(), {0}.end(), {0}.begin(), [](double){{return gRandom->Uniform(-1.0, 1.0);}});
20  return {0};'''
21 d = 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 hands d, a RDataFrame with two columns, x and y, which
26 # hold collections of coordinates. The size of these collections vary.
27 # Let's now define radii out of x and y. We'll do it treating the collections
28 # stored in the columns without looping on the individual elements.
29 d1 = 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
34 ring_h = d1.Define("rInFig", "r > .4 && r < .8 && x*y < 0")\
35  .Define("yFig", "y[rInFig]")\
36  .Define("xFig", "x[rInFig]")\
37  .Histo2D(("fig", "Two quarters of a ring", 64, -1, 1, 64, -1, 1), "xFig", "yFig")
38
39 cring = ROOT.TCanvas()
40 ring_h.Draw("Colz")
41 cring.SaveAs("df016_ring.png")
42
43 print("Saved figure to df016_ring.png")
ROOT::RDataFrame
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTrees,...
Definition: RDataFrame.hxx:42