ROOT
6.14/05
Reference Guide
tutorials
dataframe
df012_DefinesAndFiltersAsStrings.C
Go to the documentation of this file.
1
/// \file
2
/// \ingroup tutorial_dataframe
3
/// \notebook -nodraw
4
///
5
/// This tutorial illustrates how to save some typing when using RDataFrame
6
/// by invoking functions that perform jit-compiling at runtime.
7
///
8
/// \macro_code
9
///
10
/// \date October 2017
11
/// \author Guilherme Amadio
12
13
void
df012_DefinesAndFiltersAsStrings
()
14
{
15
// We will inefficiently calculate an approximation of pi by generating
16
// some data and doing very simple filtering and analysis on it
17
18
// We start by creating an empty dataframe where we will insert 10 million
19
// random points in a square of side 2.0 (that is, with an inscribed circle
20
// of radius 1.0)
21
22
size_t
npoints = 10000000;
23
ROOT::RDataFrame
tdf(npoints);
24
25
// Define what we want inside the dataframe. We do not need to define p as an array,
26
// but we do it here to demonstrate how to use jitting with RDataFrame
27
28
// NOTE: Although it's possible to use "for (auto&& x : p)" below, it will
29
// shadow the name of the data column "x", and may cause compilation failures
30
// if the local variable and the data column are of different types or the
31
// local x variable is declared in the global scope of the lambda function
32
33
auto
pidf = tdf.Define(
"x"
,
"gRandom->Uniform(-1.0, 1.0)"
)
34
.Define(
"y"
,
"gRandom->Uniform(-1.0, 1.0)"
)
35
.Define(
"p"
,
"std::array<double, 2> v{x, y}; return v;"
)
36
.Define(
"r"
,
"double r2 = 0.0; for (auto&& x : p) r2 += x*x; return sqrt(r2);"
);
37
38
// Now we have a dataframe with columns x, y, p (which is a point based on x
39
// and y), and the radius r = sqrt(x*x + y*y). In order to approximate pi, we
40
// need to know how many of our data points fall inside the unit circle compared
41
// with the total number of points. The ratio of the areas is
42
//
43
// A_circle / A_square = pi r*r / l * l, where r = 1.0, and l = 2.0
44
//
45
// Therefore, we can approximate pi with 4 times the number of points inside the
46
// unit circle over the total number of points in our dataframe:
47
48
auto
incircle
= *(pidf.Filter(
"r <= 1.0"
).Count());
49
50
double
pi_approx = 4.0 *
incircle
/ npoints;
51
52
std::cout <<
"pi is approximately equal to "
<< pi_approx << std::endl;
53
}
incircle
REAL incircle(struct mesh *m, struct behavior *b, vertex pa, vertex pb, vertex pc, vertex pd)
Definition:
triangle.c:5863
ROOT::RDataFrame
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTrees, CSV's and other data formats.
Definition:
RDataFrame.hxx:42
df012_DefinesAndFiltersAsStrings
Definition:
df012_DefinesAndFiltersAsStrings.py:1