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, April 17, 2024 at 11:16 AM.
ROOTR_EXPOSED_CLASS(MyFunctor)
R
input_line_43:2:32: error: expected ';' after expression ROOTR_EXPOSED_CLASS(MyFunctor) ^ ; input_line_43:4:1: error: use of undeclared identifier 'R' R ^
Definition of a helper function:
%%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
%%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:13: error: use of undeclared identifier 'MyFunctorModule' OOTR_MODULE(MyFunctorModule) { ^
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
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
r<<"MyFunctor <- MyFunctorModule$MyFunctor";
cling JIT session error: Failed to materialize symbols: { (main, { _ZN5cling7runtime8internal15DynamicExprInfoC1EPKcPPvb }) }
creating a MyFunctor's object
r<<"u <- new(MyFunctor)";
cling JIT session error: Failed to materialize symbols: { (main, { _ZN5cling7runtime8internal15DynamicExprInfoC1EPKcPPvb }) }
printing status
r<<"print(u$getStatus())";
cling JIT session error: Failed to materialize symbols: { (main, { _ZN5cling7runtime8internal15DynamicExprInfoC1EPKcPPvb }) }
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;
cling JIT session error: Failed to materialize symbols: { (main, { _ZN5cling7runtime8internal15DynamicExprInfoC1EPKcPPvb }) }
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;
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
r<<"print(functor$getStatus())";
r<<"print(sprintf('value in R = %f',functor$doEval( 1 )))";
std::cout<<"value in ROOT = "<<TMath::Erf(1)<<std::endl;
cling JIT session error: Failed to materialize symbols: { (main, { _ZN5cling7runtime8internal15DynamicExprInfoC1EPKcPPvb }) }