{ "cells": [ { "cell_type": "markdown", "id": "70b3c948", "metadata": {}, "source": [ "# Functor\n", "Example to create class Functor\n", "\n", "\n", "\n", "\n", "**Author:** Omar Zapata \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:27 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "28444b3d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:37.903275Z", "iopub.status.busy": "2026-05-19T20:27:37.903143Z", "iopub.status.idle": "2026-05-19T20:27:38.236234Z", "shell.execute_reply": "2026-05-19T20:27:38.235486Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_44:2:32: error: expected ';' after expression\n", " ROOTR_EXPOSED_CLASS(MyFunctor)\n", " ^\n", " ;\n", "input_line_44:4:1: error: use of undeclared identifier 'R'\n", "R\n", "^\n" ] } ], "source": [ "ROOTR_EXPOSED_CLASS(MyFunctor)\n", "\n", "R" ] }, { "cell_type": "markdown", "id": "e0fea372", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 2, "id": "fe9417a8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:38.238151Z", "iopub.status.busy": "2026-05-19T20:27:38.238019Z", "iopub.status.idle": "2026-05-19T20:27:38.243195Z", "shell.execute_reply": "2026-05-19T20:27:38.242715Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_53:2:9: fatal error: 'TRInterface.h' file not found\n", "#include\n", " ^~~~~~~~~~~~~~~\n" ] } ], "source": [ "%%cpp -d\n", "\n", "#include\n", "#include\n", "\n", "typedef Double_t (*Function)(Double_t);\n", "\n", "//Functor class with the function inside\n", "class MyFunctor{\n", "public:\n", " MyFunctor() {\n", " status=false;\n", " f=TMath::BesselY1;\n", " }\n", "\n", " void setFunction(Function fun) {\n", " f=fun;\n", " status=true;\n", " }\n", "\n", " Bool_t getStatus(){return status;}\n", "\n", " Double_t doEval(Double_t x) {\n", " return f(x);\n", " }\n", "\n", "private:\n", " Function f;\n", " Bool_t status;\n", "};" ] }, { "cell_type": "markdown", "id": "970fb85a", "metadata": {}, "source": [ " this macro exposes the class into R's enviornment\n", "and lets you pass objects directly.\n", "Macro to create a module\n", " " ] }, { "cell_type": "code", "execution_count": 3, "id": "4d0b4f93", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:38.244635Z", "iopub.status.busy": "2026-05-19T20:27:38.244493Z", "iopub.status.idle": "2026-05-19T20:27:38.247332Z", "shell.execute_reply": "2026-05-19T20:27:38.246885Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_54:1:13: error: use of undeclared identifier 'MyFunctorModule'\n", "OOTR_MODULE(MyFunctorModule) {\n", " ^\n" ] } ], "source": [ "%%cpp -d\n", "OOTR_MODULE(MyFunctorModule) {\n", " ROOT::R::class_( \"MyFunctor\" )\n", " //creating a default constructor\n", " .constructor()\n", " //adding the method doEval to evaluate the internal function\n", " .method( \"doEval\", &MyFunctor::doEval )\n", " .method( \"getStatus\", &MyFunctor::getStatus)\n", " ;\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "id": "4feead38", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:38.248689Z", "iopub.status.busy": "2026-05-19T20:27:38.248550Z", "iopub.status.idle": "2026-05-19T20:27:38.457252Z", "shell.execute_reply": "2026-05-19T20:27:38.456598Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_55:2:8: error: 'R' is not a class, namespace, or enumeration\n", " ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();\n", " ^\n", "input_line_55:2:8: note: 'R' declared here\n", "input_line_55:2:32: error: no member named 'R' in namespace 'ROOT'\n", " ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();\n", " ~~~~~~^\n" ] } ], "source": [ "ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();" ] }, { "cell_type": "markdown", "id": "94ee2925", "metadata": {}, "source": [ "Creating functor with default function TMath::BesselY1\n", "and status false from R's environment\n", "Loading module into R's enviornment" ] }, { "cell_type": "code", "execution_count": 5, "id": "8613d8c9", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:38.458837Z", "iopub.status.busy": "2026-05-19T20:27:38.458710Z", "iopub.status.idle": "2026-05-19T20:27:38.785663Z", "shell.execute_reply": "2026-05-19T20:27:38.785006Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal9EvaluateTIvEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE' unresolved while linking [cling interface function]!\n", "You are probably missing the definition of void cling::runtime::internal::EvaluateT(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*)\n", "Maybe you need to load the corresponding shared library?\n" ] } ], "source": [ "r[\"MyFunctorModule\"]<