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