{ "cells": [ { "cell_type": "markdown", "id": "86caba91", "metadata": {}, "source": [ "# Function\n", "\n", "\n", "\n", "\n", "**Author:** \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": "df7f1e98", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:30.390403Z", "iopub.status.busy": "2026-05-19T20:27:30.390290Z", "iopub.status.idle": "2026-05-19T20:27:30.708811Z", "shell.execute_reply": "2026-05-19T20:27:30.708330Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_44:1:23: error: expected namespace name\n", "using namespace ROOT::R;\n", " ~~~~~~^\n" ] } ], "source": [ "using namespace ROOT::R;" ] }, { "cell_type": "code", "execution_count": 2, "id": "745b706c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:30.710207Z", "iopub.status.busy": "2026-05-19T20:27:30.710090Z", "iopub.status.idle": "2026-05-19T20:27:30.917314Z", "shell.execute_reply": "2026-05-19T20:27:30.916827Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_53:2:18: error: 'R' is not a class, namespace, or enumeration\n", " auto &r = ROOT::R::TRInterface::Instance();\n", " ^\n", "input_line_53:2:18: note: 'R' declared here\n" ] } ], "source": [ "auto &r = ROOT::R::TRInterface::Instance();\n", "r.SetVerbose(1);" ] }, { "cell_type": "markdown", "id": "b6286f2f", "metadata": {}, "source": [ "Defining functions to be used from R" ] }, { "cell_type": "code", "execution_count": 3, "id": "e0628c11", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:30.919010Z", "iopub.status.busy": "2026-05-19T20:27:30.918897Z", "iopub.status.idle": "2026-05-19T20:27:31.125214Z", "shell.execute_reply": "2026-05-19T20:27:31.124457Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_54:2:2: error: unknown type name 'TRFunctionImport'\n", " TRFunctionImport c(\"c\");\n", " ^\n", "input_line_54:3:1: error: unknown type name 'TRFunctionImport'\n", "TRFunctionImport rlist(\"list\");\n", "^\n", "input_line_54:4:1: error: unknown type name 'TRFunctionImport'\n", "TRFunctionImport asformula(\"as.formula\");\n", "^\n", "input_line_54:5:1: error: unknown type name 'TRFunctionImport'\n", "TRFunctionImport nls(\"nls\");\n", "^\n", "input_line_54:6:1: error: unknown type name 'TRFunctionImport'\n", "TRFunctionImport confint(\"confint\");\n", "^\n", "input_line_54:7:1: error: unknown type name 'TRFunctionImport'\n", "TRFunctionImport summary(\"summary\");\n", "^\n", "input_line_54:8:1: error: unknown type name 'TRFunctionImport'\n", "TRFunctionImport print(\"print\");\n", "^\n", "input_line_54:9:1: error: unknown type name 'TRFunctionImport'\n", "TRFunctionImport plot(\"plot\");\n", "^\n", "input_line_54:10:1: error: unknown type name 'TRFunctionImport'\n", "TRFunctionImport lines(\"lines\");\n", "^\n", "input_line_54:11:1: error: unknown type name 'TRFunctionImport'\n", "TRFunctionImport devnew(\"dev.new\");\n", "^\n", "input_line_54:12:1: error: unknown type name 'TRFunctionImport'\n", "TRFunctionImport devoff(\"dev.off\");\n", "^\n", "input_line_54:13:1: error: unknown type name 'TRFunctionImport'\n", "TRFunctionImport devcur(\"dev.cur\");\n", "^\n", "input_line_54:14:1: error: unknown type name 'TRFunctionImport'\n", "TRFunctionImport rmin(\"min\");\n", "^\n", "input_line_54:15:1: error: unknown type name 'TRFunctionImport'\n", "TRFunctionImport rmax(\"max\");\n", "^\n", "input_line_54:16:1: error: unknown type name 'TRFunctionImport'\n", "TRFunctionImport seq(\"seq\");\n", "^\n", "input_line_54:17:1: error: unknown type name 'TRFunctionImport'\n", "TRFunctionImport predict(\"predict\");\n", "^\n", "input_line_54:19:1: error: use of undeclared identifier 'r'\n", "r<<\"options(device='pdf')\";\n", "^\n" ] } ], "source": [ "TRFunctionImport c(\"c\");\n", "TRFunctionImport rlist(\"list\");\n", "TRFunctionImport asformula(\"as.formula\");\n", "TRFunctionImport nls(\"nls\");\n", "TRFunctionImport confint(\"confint\");\n", "TRFunctionImport summary(\"summary\");\n", "TRFunctionImport print(\"print\");\n", "TRFunctionImport plot(\"plot\");\n", "TRFunctionImport lines(\"lines\");\n", "TRFunctionImport devnew(\"dev.new\");\n", "TRFunctionImport devoff(\"dev.off\");\n", "TRFunctionImport devcur(\"dev.cur\");\n", "TRFunctionImport rmin(\"min\");\n", "TRFunctionImport rmax(\"max\");\n", "TRFunctionImport seq(\"seq\");\n", "TRFunctionImport predict(\"predict\");\n", "\n", "r<<\"options(device='pdf')\";" ] }, { "cell_type": "markdown", "id": "36c621a8", "metadata": {}, "source": [ "doing the procedure" ] }, { "cell_type": "code", "execution_count": 4, "id": "fcf39077", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:31.126672Z", "iopub.status.busy": "2026-05-19T20:27:31.126537Z", "iopub.status.idle": "2026-05-19T20:27:31.332326Z", "shell.execute_reply": "2026-05-19T20:27:31.331553Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_55:2:2: error: unknown type name 'TRObject'\n", " TRObject xdata = c(-2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9);\n", " ^\n", "input_line_55:2:19: error: use of undeclared identifier 'c'\n", " TRObject xdata = c(-2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9);\n", " ^\n", "input_line_55:3:1: error: unknown type name 'TRObject'\n", "TRObject ydata = c(0.699369,0.700462,0.695354,1.03905,1.97389,2.41143,1.91091,0.919576,-0.730975,-1.42001);\n", "^\n", "input_line_55:3:18: error: use of undeclared identifier 'c'\n", "TRObject ydata = c(0.699369,0.700462,0.695354,1.03905,1.97389,2.41143,1.91091,0.919576,-0.730975,-1.42001);\n", " ^\n", "input_line_55:5:1: error: unknown type name 'TRDataFrame'\n", "TRDataFrame data;\n", "^\n", "input_line_55:5:1: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration\n" ] } ], "source": [ "TRObject xdata = c(-2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9);\n", "TRObject ydata = c(0.699369,0.700462,0.695354,1.03905,1.97389,2.41143,1.91091,0.919576,-0.730975,-1.42001);\n", "\n", "TRDataFrame data;\n", "data[\"xdata\"]=xdata;\n", "data[\"ydata\"]=ydata;" ] }, { "cell_type": "markdown", "id": "631ba06d", "metadata": {}, "source": [ "fit = nls(ydata ~ p1*cos(p2*xdata) + p2*sin(p1*xdata), start=list(p1=1,p2=0.2))" ] }, { "cell_type": "code", "execution_count": 5, "id": "8fa29a88", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:31.333814Z", "iopub.status.busy": "2026-05-19T20:27:31.333699Z", "iopub.status.idle": "2026-05-19T20:27:31.538359Z", "shell.execute_reply": "2026-05-19T20:27:31.537564Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_56:2:2: error: unknown type name 'TRObject'\n", " TRObject fit = nls(asformula(\"ydata ~ p1*cos(p2*xdata) + p2*sin(p1*xdata)\"),Label[\"data\"]=data, Label[\"start\"]=rlist(Label[\"p1\"]=1,Label[\"p2\"]=0.2));\n", " ^\n", "input_line_56:2:21: error: use of undeclared identifier 'asformula'\n", " TRObject fit = nls(asformula(\"ydata ~ p1*cos(p2*xdata) + p2*sin(p1*xdata)\"),Label[\"data\"]=data, Label[\"start\"]=rlist(Label[\"p1\"]=1,Label[\"p2\"]=0.2));\n", " ^\n", "input_line_56:2:78: error: use of undeclared identifier 'Label'\n", " TRObject fit = nls(asformula(\"ydata ~ p1*cos(p2*xdata) + p2*sin(p1*xdata)\"),Label[\"data\"]=data, Label[\"start\"]=rlist(Label[\"p1\"]=1,Label[\"p2\"]=0.2));\n", " ^\n", "input_line_56:2:98: error: use of undeclared identifier 'Label'\n", " TRObject fit = nls(asformula(\"ydata ~ p1*cos(p2*xdata) + p2*sin(p1*xdata)\"),Label[\"data\"]=data, Label[\"start\"]=rlist(Label[\"p1\"]=1,Label[\"p2\"]=0.2));\n", " ^\n", "input_line_56:2:119: error: use of undeclared identifier 'Label'\n", " TRObject fit = nls(asformula(\"ydata ~ p1*cos(p2*xdata) + p2*sin(p1*xdata)\"),Label[\"data\"]=data, Label[\"start\"]=rlist(Label[\"p1\"]=1,Label[\"p2\"]=0.2));\n", " ^\n", "input_line_56:2:133: error: use of undeclared identifier 'Label'\n", " TRObject fit = nls(asformula(\"ydata ~ p1*cos(p2*xdata) + p2*sin(p1*xdata)\"),Label[\"data\"]=data, Label[\"start\"]=rlist(Label[\"p1\"]=1,Label[\"p2\"]=0.2));\n", " ^\n", "input_line_56:8:4: error: use of undeclared identifier 'devnew'\n", " devnew(\"Fitting Regression\");\n", " ^\n", "input_line_56:9:9: error: use of undeclared identifier 'xdata'\n", " plot(xdata,ydata);\n", " ^\n", "input_line_56:9:15: error: use of undeclared identifier 'ydata'\n", " plot(xdata,ydata);\n", " ^\n", "input_line_56:11:4: error: unknown type name 'TRObject'\n", " TRObject xgrid=seq(rmin(xdata),rmax(xdata),Label[\"len\"]=10);\n", " ^\n", "input_line_56:11:28: error: use of undeclared identifier 'xdata'\n", " TRObject xgrid=seq(rmin(xdata),rmax(xdata),Label[\"len\"]=10);\n", " ^\n", "input_line_56:11:40: error: use of undeclared identifier 'xdata'\n", " TRObject xgrid=seq(rmin(xdata),rmax(xdata),Label[\"len\"]=10);\n", " ^\n", "input_line_56:11:47: error: use of undeclared identifier 'Label'\n", " TRObject xgrid=seq(rmin(xdata),rmax(xdata),Label[\"len\"]=10);\n", " ^\n", "input_line_56:12:35: error: use of undeclared identifier 'Label'\n", " lines(xgrid,predict(fit,xgrid),Label[\"col\"] = \"green\");\n", " ^\n", "input_line_56:13:11: error: use of undeclared identifier 'Label'\n", " devoff(Label[\"which\"] = devcur() );\n", " ^\n", "input_line_56:13:28: error: use of undeclared identifier 'devcur'\n", " devoff(Label[\"which\"] = devcur() );\n", " ^\n" ] } ], "source": [ "TRObject fit = nls(asformula(\"ydata ~ p1*cos(p2*xdata) + p2*sin(p1*xdata)\"),Label[\"data\"]=data, Label[\"start\"]=rlist(Label[\"p1\"]=1,Label[\"p2\"]=0.2));\n", "print(summary(fit));\n", "\n", "print(confint(fit));\n", "\n", "if (!gROOT->IsBatch()) {\n", " devnew(\"Fitting Regression\");\n", " plot(xdata,ydata);\n", "\n", " TRObject xgrid=seq(rmin(xdata),rmax(xdata),Label[\"len\"]=10);\n", " lines(xgrid,predict(fit,xgrid),Label[\"col\"] = \"green\");\n", " devoff(Label[\"which\"] = devcur() );\n", "}" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }