Logo ROOT  
Reference Guide
Functor.C File Reference

Detailed Description

View in nbviewer Open in SWAN Example to create class Functor

#include<TRInterface.h>
#include<TMath.h>
//Functor class with the function inside
class MyFunctor{
public:
status=false;
}
void setFunction(Function fun) {
f=fun;
status=true;
}
Bool_t getStatus(){return status;}
return f(x);
}
private:
Bool_t status;
};
// this macro exposes the class into R's enviornment
// and lets you pass objects directly.
// Macro to create a module
ROOTR_MODULE(MyFunctorModule) {
//creating a default constructor
.constructor()
//adding the method doEval to evaluate the internal function
.method( "doEval", &MyFunctor::doEval )
.method( "getStatus", &MyFunctor::getStatus)
;
}
void Functor()
{
// Creating functor with deafult function TMath::BesselY1
// and status false from R's environment
// Loading module into R's enviornment
r["MyFunctorModule"]<<LOAD_ROOTR_MODULE(MyFunctorModule);
//creating a class variable from module
r<<"MyFunctor <- MyFunctorModule$MyFunctor";
//creating a MyFunctor's object
r<<"u <- new(MyFunctor)";
//printing status
r<<"print(u$getStatus())";
//printing values from Functor and Function
r<<"print(sprintf('value in R = %f',u$doEval( 1 )))";
std::cout<<"value in ROOT = "<<TMath::BesselY1(1)<<std::endl;
// creating a MyFunctor's object and passing objects to R's
// enviornment, the status should be true because it is not
// using the default function
MyFunctor functor;
functor.setFunction(TMath::Erf);
r["functor"]<<functor;
//printing the status that should be true
r<<"print(functor$getStatus())";
r<<"print(sprintf('value in R = %f',functor$doEval( 1 )))";
std::cout<<"value in ROOT = "<<TMath::Erf(1)<<std::endl;
}
ROOT::R::TRInterface & r
Definition: Object.C:4
#define ROOTR_EXPOSED_CLASS
Definition: RExports.h:165
#define ROOTR_MODULE
Definition: RExports.h:164
#define LOAD_ROOTR_MODULE(NAME)
Definition: RExports.h:178
bool Bool_t
Definition: RtypesCore.h:63
double Double_t
Definition: RtypesCore.h:59
Double_t(* Function)(Double_t)
Definition: Functor.C:4
void Functor()
Definition: Functor.C:26
Double_t doEval(Double_t x)
Definition: Functor.C:11
MyFunctor()
Definition: Functor.C:8
Function f
Definition: Functor.C:15
ROOT R was implemented using the R Project library and the modules Rcpp and RInside
Definition: TRInterface.h:136
static TRInterface & Instance()
static method to get an TRInterface instance reference
Double_t x[n]
Definition: legend1.C:17
Double_t Erf(Double_t x)
Computation of the error function erf(x).
Definition: TMath.cxx:184
Double_t BesselY1(Double_t x)
Bessel function Y0(x) for positive x.
Definition: TMath.cxx:1714
Author
Omar Zapata

Definition in file Functor.C.