Logo ROOT  
Reference Guide
example.C File Reference

Detailed Description

View in nbviewer Open in SWAN Simple example on how to use ROOT-R interface.

#include "TMath.h"
#include "Math/PdfFunc.h"
#include "TMatrixD.h"
#include "TError.h"
#include <array>
#include <vector>
#include "TRInterface.h"
void example() {
// print R version
r.Execute("print(version$version.string)");
// compute standard deviation of 1000 vector normal numbers
double std_dev_r = r.Eval("sd(rnorm(10000))");
std::vector<double> v = r.Eval("rnorm(10000)");
double std_dev_root = TMath::StdDev(v.begin(),v.end());
std::cout << "standard deviation from R = " << std_dev_r << std::endl;
std::cout << "standard deviation from ROOT = " << std_dev_root << std::endl;
if (!TMath::AreEqualAbs(std_dev_r,std_dev_root,0.1))
Error("ROOT-R-Example","Different std-dev found");
// use << to execute the R command instead of Execute
r << "mat<-matrix(c(1,2,3,4,5,6),2,3,byrow=TRUE)";
TMatrixD m = r["mat"];
std::array<double,6> a = r.Eval("seq(1:6)");
TMatrixD m2(2,3,a.data());
if (!(m==m2)) {
Error("ROOT-R-Example","Different matrix found");
m.Print();
m2.Print();
}
// example on how to pass ROOT objects to R
std::vector<double> v_root{1,2,3,4,5,6,7,8};
r["v"] = v_root;
r << "v2<-seq(1:8)";
bool isEqual = r.Eval("prod(v==v2)");
if (!isEqual) {
Error("ROOT-R-Example","Different vector created");
r << "print(v)";
r << "print(v2)";
}
// example on how to pass functions to R
r["gaus_pdf"] = ROOT::Math::normal_pdf;
r << "y<-gaus_pdf(0,1,1)";
double value_r = r["y"];
double value_root = ROOT::Math::normal_pdf(0,1,1);
std::cout << "Function gaussian(0,1,1) evaluated in R = " << value_r << std::endl;
std::cout << "Function gaussian(0,1,1) evaluated in ROOT = " << value_root << std::endl;
if (value_r != value_root)
Error("ROOT-R-Example","Different function value found in r = %f and ROOT = %f", value_r, value_root);
}
Author

Definition in file example.C.

m
auto * m
Definition: textangle.C:8
TMath::StdDev
Double_t StdDev(Long64_t n, const T *a, const Double_t *w=0)
Definition: TMath.h:536
r
ROOT::R::TRInterface & r
Definition: Object.C:4
ROOT::R::TRInterface::Eval
Int_t Eval(const TString &code, TRObject &ans)
Method to eval R code and you get the result in a reference to TRObject.
Definition: TRInterface.cxx:78
ROOT::R::TRInterface
Definition: TRInterface.h:137
ROOT::R::TRInterface::Execute
void Execute(const TString &code)
Method to eval R code.
Definition: TRInterface.cxx:97
TMatrixT
Definition: TMatrixDfwd.h:22
v
@ v
Definition: rootcling_impl.cxx:3635
example
Definition: example.py:1
TRInterface.h
TGeant4Unit::m2
static constexpr double m2
Definition: TGeant4SystemOfUnits.h:129
ROOT::Math::normal_pdf
double normal_pdf(double x, double sigma=1, double x0=0)
Probability density function of the normal (Gaussian) distribution.
Definition: PdfFuncMathCore.h:501
PdfFunc.h
a
auto * a
Definition: textangle.C:12
TMath::AreEqualAbs
Bool_t AreEqualAbs(Double_t af, Double_t bf, Double_t epsilon)
Definition: TMath.h:430
ROOT::R::TRInterface::Instance
static TRInterface & Instance()
static method to get an TRInterface instance reference
Definition: TRInterface.cxx:185
TMatrixD.h
TMath.h
Error
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
TError.h