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:
MyFunctor() {
status=false;
}
void setFunction(Function fun) {
f=fun;
status=true;
}
Bool_t getStatus(){return status;}
Double_t doEval(Double_t x) {
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;
}
Author
Omar Zapata

Definition in file Functor.C.

f
#define f(i)
Definition: RSha256.hxx:122
TMath::BesselY1
Double_t BesselY1(Double_t x)
Bessel function Y0(x) for positive x.
Definition: TMath.cxx:1714
r
ROOT::R::TRInterface & r
Definition: Object.C:4
ROOT::R::TRInterface
Definition: TRInterface.h:137
x
Double_t x[n]
Definition: legend1.C:17
TMath::Erf
Double_t Erf(Double_t x)
Computation of the error function erf(x).
Definition: TMath.cxx:184
Function
Double_t(* Function)(Double_t)
Definition: Functor.C:4
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
TRInterface.h
Functor
void Functor()
Definition: Functor.C:26
ROOT::R::class_
Definition: RExports.h:144
ROOTR_EXPOSED_CLASS
#define ROOTR_EXPOSED_CLASS
Definition: RExports.h:164
LOAD_ROOTR_MODULE
#define LOAD_ROOTR_MODULE(NAME)
Definition: RExports.h:177
Double_t
double Double_t
Definition: RtypesCore.h:59
ROOTR_MODULE
#define ROOTR_MODULE
Definition: RExports.h:163
ROOT::R::TRInterface::Instance
static TRInterface & Instance()
static method to get an TRInterface instance reference
Definition: TRInterface.cxx:185
TMath.h