Logo ROOT   6.12/06
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Namespaces
tdf012_DefinesAndFiltersAsStrings.py File Reference

Namespaces

 tdf012_DefinesAndFiltersAsStrings
 

Detailed Description

This tutorial illustrates how to use jit-compiling features of TDataFrame to define data using C++ code in a Python script.

1 
2 import ROOT
3 
4 ## We will inefficiently calculate an approximation of pi by generating
5 ## some data and doing very simple filtering and analysis on it.
6 
7 ## We start by creating an empty dataframe where we will insert 10 million
8 ## random points in a square of side 2.0 (that is, with an inscribed unit
9 ## circle).
10 
11 npoints = 10000000
12 tdf = ROOT.ROOT.Experimental.TDataFrame(npoints)
13 
14 ## Define what data we want inside the dataframe. We do not need to define p
15 ## as an array, but we do it here to demonstrate how to use jitting with TDataFrame
16 
17 pidf = tdf.Define("x", "gRandom->Uniform(-1.0, 1.0)") \
18  .Define("y", "gRandom->Uniform(-1.0, 1.0)") \
19  .Define("p", "std::array<double, 2> v{x, y}; return v;") \
20  .Define("r", "double r2 = 0.0; for (auto&& w : p) r2 += w*w; return sqrt(r2);")
21 
22 ## Now we have a dataframe with columns x, y, p (which is a point based on x
23 ## and y), and the radius r = sqrt(x*x + y*y). In order to approximate pi, we
24 ## need to know how many of our data points fall inside the circle of radius
25 ## one compared with the total number of points. The ratio of the areas is
26 ##
27 ## A_circle / A_square = pi r*r / l * l, where r = 1.0, and l = 2.0
28 ##
29 ## Therefore, we can approximate pi with 4 times the number of points inside
30 ## the unit circle over the total number of points:
31 
32 incircle = pidf.Filter("r <= 1.0").Count().GetValue()
33 
34 pi_approx = 4.0 * incircle / npoints
35 
36 print("pi is approximately equal to %g" % (pi_approx))
Date
October 2017
Author
Guilherme Amadio

Definition in file tdf012_DefinesAndFiltersAsStrings.py.