Logo ROOT  
Reference Guide
Functor.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_r
3/// \notebook -nodraw
4/// Example to create class Functor
5///
6/// \macro_code
7///
8/// \author Omar Zapata
9
10#include<TRInterface.h>
11#include<TMath.h>
12
13typedef Double_t (*Function)(Double_t);
14
15//Functor class with the function inside
16class MyFunctor{
17public:
18 MyFunctor() {
19 status=false;
21 }
22
23 void setFunction(Function fun) {
24 f=fun;
25 status=true;
26 }
27
28 Bool_t getStatus(){return status;}
29
31 return f(x);
32 }
33
34private:
35 Function f;
36 Bool_t status;
37};
38
39// this macro exposes the class into R's enviornment
40// and lets you pass objects directly.
42
43// Macro to create a module
44ROOTR_MODULE(MyFunctorModule) {
45 ROOT::R::class_<MyFunctor>( "MyFunctor" )
46 //creating a default constructor
47 .constructor()
48 //adding the method doEval to evaluate the internal function
49 .method( "doEval", &MyFunctor::doEval )
50 .method( "getStatus", &MyFunctor::getStatus)
51 ;
52}
53
54void Functor()
55{
57
58 // Creating functor with deafult function TMath::BesselY1
59 // and status false from R's environment
60 // Loading module into R's enviornment
61 r["MyFunctorModule"]<<LOAD_ROOTR_MODULE(MyFunctorModule);
62
63 //creating a class variable from module
64 r<<"MyFunctor <- MyFunctorModule$MyFunctor";
65 //creating a MyFunctor's object
66 r<<"u <- new(MyFunctor)";
67
68 //printing status
69 r<<"print(u$getStatus())";
70
71 //printing values from Functor and Function
72 r<<"print(sprintf('value in R = %f',u$doEval( 1 )))";
73 std::cout<<"value in ROOT = "<<TMath::BesselY1(1)<<std::endl;
74
75 // creating a MyFunctor's object and passing objects to R's
76 // enviornment, the status should be true because it is not
77 // using the default function
78 MyFunctor functor;
79 functor.setFunction(TMath::Erf);
80 r["functor"]<<functor;
81
82 //printing the status that should be true
83 r<<"print(functor$getStatus())";
84 r<<"print(sprintf('value in R = %f',functor$doEval( 1 )))";
85 std::cout<<"value in ROOT = "<<TMath::Erf(1)<<std::endl;
86}
ROOT::R::TRInterface & r
Definition: Object.C:4
#define ROOTR_EXPOSED_CLASS
Definition: RExports.h:164
#define LOAD_ROOTR_MODULE(NAME)
Definition: RExports.h:177
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
ROOTR_MODULE(MyFunctorModule)
Definition: Functor.C:18
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