ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Functor.C
Go to the documentation of this file.
1 //Example to create class Functor
2 //Author: Omar Zapata
3 #include<TRInterface.h>
4 #include<TMath.h>
5 
7 
8 //Functor class with the function inside
9 class MyFunctor{
10 public:
11  MyFunctor(){
12  status=false;
14  }
15  void setFunction(Function fun)
16  {
17  f=fun;
18  status=true;
19  }
20  Bool_t getStatus(){return status;}
21  Double_t doEval(Double_t x) {
22  return f(x);
23  }
24 private:
25  Function f;
26  Bool_t status;
27 };
28 //this macro exposes the class into R's enviornment
29 // and lets you pass objects directly.
30 ROOTR_EXPOSED_CLASS(MyFunctor)
31 
32 //Macro to create a module
33 ROOTR_MODULE(MyFunctorModule) {
34  ROOT::R::class_<MyFunctor>( "MyFunctor" )
35  //creating a default constructor
36  .constructor()
37  //adding the method doEval to evaluate the internal function
38  .method( "doEval", &MyFunctor::doEval )
39  .method( "getStatus", &MyFunctor::getStatus)
40  ;
41 }
42 
43 void Functor()
44 {
46  //////////////////////////////////////////////////////////
47  //Creating functor with deafult function TMath::BesselY1//
48  // and status false from R's environment //
49  //////////////////////////////////////////////////////////
50  //Loading module into R's enviornment
51  r["MyFunctorModule"]<<LOAD_ROOTR_MODULE(MyFunctorModule);
52 
53  //creating a class variable from module
54  r<<"MyFunctor <- MyFunctorModule$MyFunctor";
55  //creating a MyFunctor's object
56  r<<"u <- new(MyFunctor)";
57 
58  //printing status
59  r<<"print(u$getStatus())";
60 
61  //printing values from Functor and Function
62  r<<"print(sprintf('value in R = %f',u$doEval( 1 )))";
63  std::cout<<"value in ROOT = "<<TMath::BesselY1(1)<<std::endl;
64 
65  ////////////////////////////////////////////////////////////
66  //creating a MyFunctor's object and passing objects to R's //
67  //enviornment, the status should be true because it is not //
68  //using the default function //
69  ////////////////////////////////////////////////////////////
70  MyFunctor functor;
71  functor.setFunction(TMath::Erf);
72  r["functor"]<<functor;
73  //printing the status that should be true
74  r<<"print(functor$getStatus())";
75  r<<"print(sprintf('value in R = %f',functor$doEval( 1 )))";
76  std::cout<<"value in ROOT = "<<TMath::Erf(1)<<std::endl;
77 }
void Functor()
Definition: Functor.C:26
TAlienJobStatus * status
Definition: TAlienJob.cxx:51
bool Bool_t
Definition: RtypesCore.h:59
ROOTR_MODULE(MyFunctorModule)
Definition: Functor.C:18
TFile * f
Double_t BesselY1(Double_t x)
Returns the Bessel function Y1(x) for positive x.
Definition: TMath.cxx:1702
Double_t x[n]
Definition: legend1.C:17
#define ROOTR_EXPOSED_CLASS
Definition: RExports.h:172
ROOT::R::TRInterface & r
Definition: Object.C:4
Double_t Erf(Double_t x)
Computation of the error function erf(x).
Definition: TMath.cxx:187
Function
Definition: math.cpp:59
double Double_t
Definition: RtypesCore.h:55
static TRInterface & Instance()
static method to get an TRInterface instance reference
#define LOAD_ROOTR_MODULE(NAME)
Definition: RExports.h:185