Functor

Example to create class Functor

Author: Omar Zapata
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, June 23, 2021 at 11:36 AM.

In [1]:
%%cpp -d
#include<TRInterface.h>
#include<TMath.h>

typedef Double_t (*Function)(Double_t);
In file included from input_line_41:1:
In file included from /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/TRInterface.h:15:
In file included from /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/TRObject.h:14:
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/RExports.h:54:9: fatal error: 'RcppCommon.h' file not found
#include<RcppCommon.h>
        ^~~~~~~~~~~~~~

Functor class with the function inside

In [2]:
class MyFunctor{
public:
   MyFunctor() {
      status=false;
      f=TMath::BesselY1;
   }

   void setFunction(Function fun) {
    f=fun;
    status=true;
   }

   Bool_t getStatus(){return status;}

   Double_t doEval(Double_t x) {
      return f(x);
   }

private:
   Function f;
   Bool_t status;
};
input_line_42:8:21: error: unknown type name 'Function'
   void setFunction(Function fun) {
                    ^
input_line_42:20:4: error: unknown type name 'Function'
   Function f;
   ^

This macro exposes the class into r's enviornment and lets you pass objects directly.

In [3]:
ROOTR_EXPOSED_CLASS(MyFunctor)
input_line_49:2:23: error: use of undeclared identifier 'MyFunctor'
 (ROOTR_EXPOSED_CLASS(MyFunctor))
                      ^
Error in <HandleInterpreterException>: Error evaluating expression (ROOTR_EXPOSED_CLASS(MyFunctor)).
Execution of your code was aborted.

Macro to create a module

In [4]:
ROOTR_MODULE(MyFunctorModule) {
   ROOT::R::class_<MyFunctor>( "MyFunctor" )
   //creating a default constructor
   .constructor()
   //adding the method doEval to evaluate the internal function
   .method( "doEval", &MyFunctor::doEval )
   .method( "getStatus", &MyFunctor::getStatus)
   ;
}
input_line_50:2:31: error: expected ';' after expression
 ROOTR_MODULE(MyFunctorModule) {
                              ^
                              ;
input_line_50:3:13: error: no member named 'class_' in namespace 'ROOT::R'
   ROOT::R::class_<MyFunctor>( "MyFunctor" )
   ~~~~~~~~~^
input_line_50:3:20: error: use of undeclared identifier 'MyFunctor'
   ROOT::R::class_<MyFunctor>( "MyFunctor" )
                   ^
input_line_50:5:4: error: member reference base type 'const char [10]' is not a structure or union
   .constructor()
   ^~~~~~~~~~~~
input_line_50:7:24: error: use of undeclared identifier 'MyFunctor'
   .method( "doEval", &MyFunctor::doEval )
                       ^
input_line_50:8:27: error: use of undeclared identifier 'MyFunctor'
   .method( "getStatus", &MyFunctor::getStatus)
                          ^
In [5]:
ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
In file included from libRInterface dictionary payload:5:
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/RExports.h:54:9: fatal error: 'RcppCommon.h' file not found
#include<RcppCommon.h>
        ^~~~~~~~~~~~~~
Error in <TInterpreter::AutoParse>: Error parsing payload code for class ROOT::R::TRInterface with content:

#line 1 "libRInterface dictionary payload"


#define _BACKWARD_BACKWARD_WARNING_H
// Inline headers
#include "RExports.h"
#include "TRDataFrame.h"
#include "TRFunctionExport.h"
#include "TRFunctionImport.h"
#include "TRInterface.h"
#include "TRInternalFunction.h"
#include "TRObject.h"

#undef  _BACKWARD_BACKWARD_WARNING_H

input_line_51:2:35: error: incomplete type 'ROOT::R::TRInterface' named in nested name specifier
 ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
                         ~~~~~~~~~^~~~~~~~~~~~~
libRInterface dictionary forward declarations' payload:9:93: note: forward declaration of 'ROOT::R::TRInterface'
namespace ROOT{namespace R{class __attribute__((annotate("$clingAutoload$TRInterface.h")))  TRInterface;}}
                                                                                            ^

Creating functor with deafult function tmath::bessely1 and status false from R's environment Loading module into R's enviornment

In [6]:
r["MyFunctorModule"]<<LOAD_ROOTR_MODULE(MyFunctorModule);
input_line_55:2:3: error: use of undeclared identifier 'r'
 (r["MyFunctorModule"] << LOAD_ROOTR_MODULE(MyFunctorModule))
  ^
input_line_55:2:45: error: use of undeclared identifier 'MyFunctorModule'
 (r["MyFunctorModule"] << LOAD_ROOTR_MODULE(MyFunctorModule))
                                            ^
Error in <HandleInterpreterException>: Error evaluating expression (r["MyFunctorModule"] << LOAD_ROOTR_MODULE(MyFunctorModule)).
Execution of your code was aborted.

Creating a class variable from module

In [7]:
r<<"MyFunctor <- MyFunctorModule$MyFunctor";
input_line_57:2:3: error: use of undeclared identifier 'r'
 (r << "MyFunctor <- MyFunctorModule$MyFunctor")
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r << "MyFunctor <- MyFunctorModule$MyFunctor").
Execution of your code was aborted.

Creating a myfunctor's object

In [8]:
r<<"u <- new(MyFunctor)";
input_line_59:2:3: error: use of undeclared identifier 'r'
 (r << "u <- new(MyFunctor)")
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r << "u <- new(MyFunctor)").
Execution of your code was aborted.

Printing status

In [9]:
r<<"print(u$getStatus())";
input_line_61:2:3: error: use of undeclared identifier 'r'
 (r << "print(u$getStatus())")
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r << "print(u$getStatus())").
Execution of your code was aborted.

Printing values from functor and function

In [10]:
r<<"print(sprintf('value in R = %f',u$doEval( 1 )))";
std::cout<<"value in ROOT = "<<TMath::BesselY1(1)<<std::endl;
input_line_63:2:3: error: use of undeclared identifier 'r'
 (r << "print(sprintf('value in R = %f',u$doEval( 1 )))")
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r << "print(sprintf('value in R = %f',u$doEval( 1 )))").
Execution of your code was aborted.

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

In [11]:
MyFunctor functor;
functor.setFunction(TMath::Erf);
r["functor"]<<functor;
input_line_64:2:11: error: expected ';' after expression
 MyFunctor functor;
          ^
          ;
input_line_64:2:12: error: use of undeclared identifier 'functor'
 MyFunctor functor;
           ^
input_line_64:3:1: error: use of undeclared identifier 'functor'
functor.setFunction(TMath::Erf);
^
input_line_64:4:1: error: use of undeclared identifier 'r'
r["functor"]<<functor;
^
input_line_64:4:15: error: use of undeclared identifier 'functor'
r["functor"]<<functor;
              ^

Printing the status that should be true

In [12]:
r<<"print(functor$getStatus())";
r<<"print(sprintf('value in R = %f',functor$doEval( 1 )))";
std::cout<<"value in ROOT = "<<TMath::Erf(1)<<std::endl;
input_line_66:2:3: error: use of undeclared identifier 'r'
 (r << "print(functor$getStatus())")
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r << "print(functor$getStatus())").
Execution of your code was aborted.