![]() |
ROOT
6.06/09
Reference Guide
|
ROOT R is an interface in ROOT to call R functions using an R C++ interface (Rcpp, see http://dirk.eddelbuettel.com/code/rcpp.html). This interface opens the possibility in ROOT to use the very large set of mathematical and statistical tools provided by R. With ROOTR you can perform a conversion from ROOT's C++ objects to R's objects, transform the returned R objects into ROOT's C++ objects, then the R functionality can be used directly for statistical studies in ROOT.
ROOTR creates a working environment to execute R coding called from C++
. It allows to translate some datatypes from C++
to R inside the R environment and vice versa in an easy way to get the most from both R and ROOT. To ease the sending and receiving of data in both environments, I overloaded the operators <<
,>>
and []
which make look the job as a flow of data between environments, we will see more of that later. With this tool you ca use any library or R package wich allows you to access a big ammount of benefits to make statistical analysis. ROOTR also has a R events processing system, which allows to use the R graphical system from C++
.
To install ROOTR please read first.
NOTE: Mac OSX Yosemite last xcode and without macports
Prerequisities
To compile with cmake added into ~/.profile
and
Install needed R packages, open R and in the prompt type
select a mirror and install.
Download code from git repo
To compile ROOTR lets to create a compilation directory and to activate it use cmake -Dr=ON ..
This is a basic video using ROOTR on
](http://www.youtube.com/watch?v=tvhuEen8t7c)
NOTE: Tested on Gnu/Linux Debian Jessie with gcc 4.9
Prerequisities install (For debian-based distros)
Install needed R packages, open R and in the prompt type
select a mirror and install. Download code from git repo
To compile ROOTR lets to create a compilation directory and to activate it use cmake -Dr=ON ..
This is a basic video using ROOTR on
](http://www.youtube.com/watch?v=FkrmM2xCPoM)
There is a class called TRInterface which is located at the header TRInterface.h and uses the namespace ROOT::R
, it is in charge of making calls to R to give and obtein data. This class has a series of overcharged operators which ease the passing and obtaining of data and code from R to C++ and vice versa. To create an object of this class the user must use the static methods ROOT::R::TRInterface::Instance
and ROOT::R::TRInterface::InstancePtr
which return a reference object and a pointer object respectively.
We have different ways to run R code and pass/obtain data to/from R environment: using the methods Execute(code) and Eval(code).
So, working with ROOTR is like working with flows of data to pass, obtain and process data.
You can pass functions from ROOT to R using the opetrators <<
and =
or using the class TRFunction, but the arguments and datatypes of the return value cannot be pointers. They must be ROOTR supported datatypes. So instead of using *Double_t
you must use std::vector
and instead of *Char_t
use TString or std::string
.
For this example we need to create a macro, so save it as fun.C
IMPORTANT
You can wrap a class and expose it in R environment using only a pair of macrodefinitions and the template class ROOT::R::class_<>
The ROOTR_EXPOSED_CLASS(Class)
macro allows you to expose the class as a new datatype of R, but it has to be alongside the ROOTR_MODULE(Module)
macro which allows you to create an internal R module and make the class wrapping To do this you must use inside the ROOTR_MODULE
braces the class ROOT::R::class_<>
and specify each constructor, attribute or method that the class to export has. Then the macrodefinition LOAD_ROOTR_MODULE(Module)
can load the module and the class in R's environment. You can find a more clear instruction by looking at a example below in Functor section.
DataFrame? is a very important datatype in R and in ROOTR we have a class to manipulate dataframes called TRDataFrame, with a lot of very useful operators overloaded to work with TRDataFrame's objects in a similar way that in the R environment but from c++ in ROOT. Example:
Lets to create need data to play with dataframe features
In R the dataframe have associate to every column a label, in ROOTR you can have the same label using the class ROOT::R::Label to create a TRDataFrame where you data have a label associate.
Output
Manipulating data between dataframes
Output
Getting data frames from R's environment
Output
Output
Output
ROOTR supports an eventloop for R's graphical system which allows plotting using the R functions to the graphical system or generating images(ps, pdf png, etc). You can find a demo in Interpolation below in examples section.
The interactive mode lets you get the R's command line within ROOT's command line to run R code with tab completion support. The variables created in the interactive mode can be passed to ROOT with TRObjectProxy and the method ParseEval?. To initialize the interactive mode just call Interactive() method and type ".q" to exit from R's prompt and to go to the ROOT's prompt again.
The examples can also be found in $ROOTSYS/tutorials/r
A functor is a class which wraps a function, very useful when states and propierties associated to that function are needed. In this example I show how to give support to a custom class to be used in R's environment, which at the same time is a functor.
The next example creates an exponential fit. The idea is to create a set of numbers x,y with noise from ROOT, pass them to R and fit the data to x^3
, get the fitted coefficient(power) and plot the data, the known function and the fitted function using ROOT's classes.
In the first image you can see the blue dots wichi are the function x^3
with gaussian noise, the red dots correspond to the original function and the green ones correspond to the fitted function.
DEoptim is a R package for Differential Evolution Minimization that lets you do global Minimization. To install this package you just need to run:
Then create a macro named GlobalMinimization.C with the next code.
In the image you can see the convergence plots of the functions and their minimum. For RosenBrock is (1,1,1) and for Rastrigin is (0,0,0).
This example shows an interpolation using the function aproxfun and how to make a plot with R's graphical functions.
More Information on R interpolation at http://stat.ethz.ch/R-manual/R-patched/library/stats/html/approxfun.html
The image shows the interpolated function plotted within R
Numerical integration using R passing the function from ROOT