forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdf016_vecOps.py
More file actions
38 lines (33 loc) · 1.44 KB
/
df016_vecOps.py
File metadata and controls
38 lines (33 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
## \file
## \ingroup tutorial_dataframe
## \notebook -draw
## This tutorial shows the potential of the VecOps approach for treating collections
## stored in datasets, a situation very common in HEP data analysis.
##
## \macro_image
## \macro_code
##
## \date February 2018
## \author Danilo Piparo
import ROOT
tdf = ROOT.RDataFrame(1024)
coordDefineCode = '''ROOT::VecOps::RVec<double> {0}(len);
std::transform({0}.begin(), {0}.end(), {0}.begin(), [](double){{return gRandom->Uniform(-1.0, 1.0);}});
return {0};'''
d = tdf.Define("len", "gRandom->Uniform(0, 16)")\
.Define("x", coordDefineCode.format("x"))\
.Define("y", coordDefineCode.format("y"))
# Now we have in hands d, a RDataFrame with two columns, x and y, which
# hold collections of coordinates. The size of these collections vary.
# Let's now define radii out of x and y. We'll do it treating the collections
# stored in the columns without looping on the individual elements.
d1 = d.Define("r", "sqrt(x*x + y*y)")
# Now we want to plot 2 quarters of a ring with radii .5 and 1
# Note how the cuts are performed on RVecs, comparing them with integers and
# among themselves
ring_h = d1.Define("rInFig", "r > .4 && r < .8 && x*y < 0")\
.Define("yFig", "y[rInFig]")\
.Define("xFig", "x[rInFig]")\
.Histo2D(("fig", "Two quarters of a ring", 64, -1, 1, 64, -1, 1), "xFig", "yFig")
cring = ROOT.TCanvas()
ring_h.Draw("Colz")