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 Monday, August 15, 2022 at 09:40 AM.

In [1]:
ROOTR_EXPOSED_CLASS(MyFunctor)

R
input_line_42:2:32: error: expected ';' after expression
 ROOTR_EXPOSED_CLASS(MyFunctor)
                               ^
                               ;
input_line_42:4:1: error: use of undeclared identifier 'R'
R
^

Definition of a helper function:

In [2]:
%%cpp -d

#include<TRInterface.h>
#include<TMath.h>

typedef Double_t (*Function)(Double_t);

//Functor class with the function inside
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;
};
In file included from input_line_52:2:
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:64:9: fatal error: 'RcppCommon.h' file not found
#include<RcppCommon.h>
        ^~~~~~~~~~~~~~

this macro exposes the class into R's enviornment and lets you pass objects directly. Macro to create a module

In [3]:
%%cpp -d
OOTR_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_53:1:1: error: unknown type name 'OOTR_MODULE'
OOTR_MODULE(MyFunctorModule) {
^
input_line_53:2:13: error: no member named 'class_' in namespace 'ROOT::R'
   ROOT::R::class_<MyFunctor>( "MyFunctor" )
   ~~~~~~~~~^
input_line_53:2:20: error: use of undeclared identifier 'MyFunctor'
   ROOT::R::class_<MyFunctor>( "MyFunctor" )
                   ^
input_line_53:4:4: error: member reference base type 'const char [10]' is not a structure or union
   .constructor()
   ^~~~~~~~~~~~
input_line_53:6:24: error: use of undeclared identifier 'MyFunctor'
   .method( "doEval", &MyFunctor::doEval )
                       ^
input_line_53:7:27: error: use of undeclared identifier 'MyFunctor'
   .method( "getStatus", &MyFunctor::getStatus)
                          ^
input_line_53:9:2: error: expected ';' after top level declarator
}
 ^
 ;
In [4]:
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:64: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_54: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 [5]:
r["MyFunctorModule"]<<LOAD_ROOTR_MODULE(MyFunctorModule);
IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal9EvaluateTIvEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE' unresolved while linking [cling interface function]!
You are probably missing the definition of void cling::runtime::internal::EvaluateT<void>(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*)
Maybe you need to load the corresponding shared library?

creating a class variable from module

In [6]:
r<<"MyFunctor <- MyFunctorModule$MyFunctor";
IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal9EvaluateTIvEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE' unresolved while linking [cling interface function]!
You are probably missing the definition of void cling::runtime::internal::EvaluateT<void>(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*)
Maybe you need to load the corresponding shared library?

creating a MyFunctor's object

In [7]:
r<<"u <- new(MyFunctor)";
IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal9EvaluateTIvEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE' unresolved while linking [cling interface function]!
You are probably missing the definition of void cling::runtime::internal::EvaluateT<void>(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*)
Maybe you need to load the corresponding shared library?

printing status

In [8]:
r<<"print(u$getStatus())";
IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal9EvaluateTIvEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE' unresolved while linking [cling interface function]!
You are probably missing the definition of void cling::runtime::internal::EvaluateT<void>(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*)
Maybe you need to load the corresponding shared library?

printing values from Functor and Function

In [9]:
r<<"print(sprintf('value in R = %f',u$doEval( 1 )))";
std::cout<<"value in ROOT = "<<TMath::BesselY1(1)<<std::endl;
IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal9EvaluateTIvEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE' unresolved while linking [cling interface function]!
You are probably missing the definition of void cling::runtime::internal::EvaluateT<void>(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*)
Maybe you need to load the corresponding shared library?

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 [10]:
MyFunctor functor;
functor.setFunction(TMath::Erf);
r["functor"]<<functor;
input_line_62:2:2: error: unknown type name 'MyFunctor'
 MyFunctor functor;
 ^
input_line_62:4:1: error: use of undeclared identifier 'r'
r["functor"]<<functor;
^

printing the status that should be true

In [11]:
r<<"print(functor$getStatus())";
r<<"print(sprintf('value in R = %f',functor$doEval( 1 )))";
std::cout<<"value in ROOT = "<<TMath::Erf(1)<<std::endl;
IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal9EvaluateTIvEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE' unresolved while linking [cling interface function]!
You are probably missing the definition of void cling::runtime::internal::EvaluateT<void>(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*)
Maybe you need to load the corresponding shared library?