{
"cells": [
{
"cell_type": "markdown",
"id": "d5f5a78f",
"metadata": {},
"source": [
"# example\n",
"Simple example on how to use ROOT-R interface.\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": "79f542b2",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:28.968839Z",
"iopub.status.busy": "2026-05-19T20:27:28.968727Z",
"iopub.status.idle": "2026-05-19T20:27:29.291959Z",
"shell.execute_reply": "2026-05-19T20:27:29.291346Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_44:2:8: error: 'R' is not a class, namespace, or enumeration\n",
" ROOT::R::TRInterface &r = ROOT::R::TRInterface::Instance();\n",
" ^\n",
"input_line_44:2:8: note: 'R' declared here\n",
"input_line_44:2:34: 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": "95661799",
"metadata": {},
"source": [
"print R version"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d12ce77f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:29.293748Z",
"iopub.status.busy": "2026-05-19T20:27:29.293596Z",
"iopub.status.idle": "2026-05-19T20:27:29.499059Z",
"shell.execute_reply": "2026-05-19T20:27:29.498406Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_54:2:3: error: use of undeclared identifier 'r'\n",
" (r.Execute(\"print(version$version.string)\"))\n",
" ^\n",
"Error in : Error evaluating expression (r.Execute(\"print(version$version.string)\"))\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"r.Execute(\"print(version$version.string)\");"
]
},
{
"cell_type": "markdown",
"id": "4dd49252",
"metadata": {},
"source": [
"compute standard deviation of 1000 vector normal numbers"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "96b4e352",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:29.500792Z",
"iopub.status.busy": "2026-05-19T20:27:29.500676Z",
"iopub.status.idle": "2026-05-19T20:27:29.884120Z",
"shell.execute_reply": "2026-05-19T20:27:29.883514Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZN9__gnu_cxx17__normal_iteratorIPdSt6vectorIdSaIdEEEppEv, _ZN5TMath11AreEqualAbsEddd, _ZN12__cling_N52112std_dev_rootE, _ZNK5cling5Value6castAsIdEET_v, _ZNK9__gnu_cxx17__normal_iteratorIPdSt6vectorIdSaIdEEEdeEv, $.cling-module-288.__inits.0, _ZN5TMath4SqrtEd, _ZN5TMath6StdDevIN9__gnu_cxx17__normal_iteratorIPdSt6vectorIdSaIdEEEEEEdT_S8_, _ZN9__gnu_cxxneIPdSt6vectorIdSaIdEEEEbRKNS_17__normal_iteratorIT_T0_EESA_, cling_module_288_.13, _ZN5TMath6LimitsIdE3MinEv, _ZNK9__gnu_cxx17__normal_iteratorIPdSt6vectorIdSaIdEEE4baseEv, _ZNK5cling5Value9isInvalidEv, _ZNK5cling5Value6isVoidEv, _ZNK5cling5Value5getAsIdEET_v, _ZNSt6vectorIdSaIdEE5beginEv, _ZN5TMath3RMSIN9__gnu_cxx17__normal_iteratorIPdSt6vectorIdSaIdEEEEEEdT_S8_, _ZN5cling5Value7CastFwdIdE4castERKS0_, cling_module_288_.15, _ZN12__cling_N52124__dynamic__cling_Un1Qu30E, _ZN5TMath3AbsEd, _ZN12__cling_N5211vE, _ZN9__gnu_cxx17__normal_iteratorIPdSt6vectorIdSaIdEEEC1ERKS1_, _ZNSt14numeric_limitsIdE3minEv, _ZNK5cling5Value5getAsIPvEET_v, cling_module_288_, _ZN5cling7runtime8internal9EvaluateTIdEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE, _ZN12__cling_N52116__cling_Un1Qu321EPv, _ZNK5cling7runtime8internal15LifetimeHandler9getMemoryEv, _ZN5TMath4MeanIN9__gnu_cxx17__normal_iteratorIPdSt6vectorIdSaIdEEEEEEdT_S8_, _ZNK5cling5Value5getAsImEET_v, _ZSt3absd, __orc_init_func.cling-module-288, _GLOBAL__sub_I_cling_module_288, _ZN12__cling_N5219std_dev_rE, _ZNSt6vectorIdSaIdEE3endEv, _ZNK5cling5Value7isValidEv, cling_module_288_.14, _ZNK5cling5Value21isPointerOrObjectTypeEv, _ZN9__gnu_cxx17__normal_iteratorIPdSt6vectorIdSaIdEEEppEi, _ZN9__gnu_cxx17__normal_iteratorIPdSt6vectorIdSaIdEEEC2ERKS1_ }) }\n",
"IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerD1Ev' unresolved while linking [cling interface function]!\n",
"You are probably missing the definition of cling::runtime::internal::LifetimeHandler::~LifetimeHandler()\n",
"Maybe you need to load the corresponding shared library?\n",
"IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerC1EPNS1_15DynamicExprInfoEPN5clang11DeclContextEPKcPNS_11InterpreterE' unresolved while linking [cling interface function]!\n",
"You are probably missing the definition of cling::runtime::internal::LifetimeHandler::LifetimeHandler(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*, char const*, cling::Interpreter*)\n",
"Maybe you need to load the corresponding shared library?\n"
]
}
],
"source": [
"double std_dev_r = r.Eval(\"sd(rnorm(10000))\");\n",
"std::vector v = r.Eval(\"rnorm(10000)\");\n",
"double std_dev_root = TMath::StdDev(v.begin(),v.end());\n",
"std::cout << \"standard deviation from R = \" << std_dev_r << std::endl;\n",
"std::cout << \"standard deviation from ROOT = \" << std_dev_root << std::endl;\n",
"if (!TMath::AreEqualAbs(std_dev_r,std_dev_root,0.1))\n",
" Error(\"ROOT-R-Example\",\"Different std-dev found\");"
]
},
{
"cell_type": "markdown",
"id": "7817885e",
"metadata": {},
"source": [
"use << to execute the R command instead of Execute"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9a3968bf",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:29.885848Z",
"iopub.status.busy": "2026-05-19T20:27:29.885725Z",
"iopub.status.idle": "2026-05-19T20:27:30.089523Z",
"shell.execute_reply": "2026-05-19T20:27:30.088891Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"cling JIT session error: Failed to materialize symbols: { (main, { _ZNK5cling7runtime8internal15LifetimeHandler9getMemoryEv }) }\n",
"[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZN8TMatrixTIdED2Ev, cling_module_289_.36, __vd_init_order__cling_Un1Qu31, _ZN8TMatrixTIdED1Ev, cling_module_289_, _ZN8TMatrixTIdE5ClearEPKc, _ZN12__cling_N52224__dynamic__cling_Un1Qu32E, _GLOBAL__sub_I_cling_module_289, _ZN12__cling_N52224__dynamic__cling_Un1Qu31E, $.cling-module-289.__inits.0, _ZN12__cling_N5221mE, cling_module_289_.38, _ZN12__cling_N5221aE, _ZN12__cling_N52216__cling_Un1Qu322EPv, cling_module_289_.39, _Z30__fd_init_order__cling_Un1Qu30v, __orc_init_func.cling-module-289, _ZN12__cling_N5222m2E, cling_module_289_.40, cling_module_289_.37, _ZN12TMatrixTBaseIdED2Ev }) }\n"
]
}
],
"source": [
"r << \"mat<-matrix(c(1,2,3,4,5,6),2,3,byrow=TRUE)\";\n",
"TMatrixD m = r[\"mat\"];\n",
"std::array a = r.Eval(\"seq(1:6)\");\n",
"TMatrixD m2(2,3,a.data());\n",
"\n",
"if (!(m==m2)) {\n",
" Error(\"ROOT-R-Example\",\"Different matrix found\");\n",
" m.Print();\n",
" m2.Print();\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "78e4a5ef",
"metadata": {},
"source": [
"example on how to pass ROOT objects to R"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "71cee5d9",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:30.091244Z",
"iopub.status.busy": "2026-05-19T20:27:30.091125Z",
"iopub.status.idle": "2026-05-19T20:27:30.295214Z",
"shell.execute_reply": "2026-05-19T20:27:30.294569Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"cling JIT session error: Failed to materialize symbols: { (main, { _ZNK5cling5Value6isVoidEv }) }\n",
"[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZNSt15__new_allocatorIdED2Ev, _ZNSt20__uninitialized_copyILb1EE13__uninit_copyIPKdPdEET0_T_S6_S5_, _ZNSt12_Vector_baseIdSaIdEE13_M_deallocateEPdm, _ZNSt12_Vector_baseIdSaIdEE12_Vector_implD2Ev, _ZSt8_DestroyIPdEvT_S1_, _ZN12__cling_N52316__cling_Un1Qu323EPv, _ZNSt12_Vector_baseIdSaIdEE17_Vector_impl_dataC2Ev, _Z30__fd_init_order__cling_Un1Qu32v, cling_module_290_.13, _ZN5cling7runtime8internal9EvaluateTIbEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE, _ZSt22__uninitialized_copy_aIPKdPddET0_T_S4_S3_RSaIT1_E, $.cling-module-290.__inits.0, _ZNSt11__copy_moveILb0ELb1ESt26random_access_iterator_tagE8__copy_mIKddEEPT0_PT_S7_S5_, _ZNSt6vectorIdSaIdEE11_S_max_sizeERKS0_, _ZN12__cling_N5236v_rootE, _ZSt14__copy_move_a1ILb0EPKdPdET1_T0_S4_S3_, _ZNSt15__new_allocatorIdE10deallocateEPdm, _ZSt18uninitialized_copyIPKdPdET0_T_S4_S3_, _ZNSt6vectorIdSaIdEED1Ev, _ZSt12__miter_baseIPKdET_S2_, _ZSt14__copy_move_a2ILb0EPKdPdET1_T0_S4_S3_, _ZNK5cling5Value5getAsIbEET_v, _ZNSt12_Vector_baseIdSaIdEE12_Vector_implD1Ev, cling_module_290_.14, _ZNKSt16initializer_listIdE5beginEv, _ZSt4copyIPKdPdET0_T_S4_S3_, _ZNSt12_Vector_baseIdSaIdEE19_M_get_Tp_allocatorEv, _ZNSt11__copy_moveILb0ELb0ESt26random_access_iterator_tagE12__assign_oneIdKdEEvPT_PT0_, _ZSt12__niter_baseIPdET_S1_, __vd_init_order__cling_Un1Qu33, _ZNK5cling5Value6castAsIbEET_v, _ZN12__cling_N5237isEqualE, cling_module_290_, _ZNSt12_Vector_baseIdSaIdEEC2ERKS0_, _ZSt12__niter_wrapIPdET_RKS1_S1_, _ZNSt15__new_allocatorIdE8allocateEmPKv, _ZSt13__copy_move_aILb0EPKdPdET1_T0_S4_S3_, _GLOBAL__sub_I_cling_module_290, _ZNSt6vectorIdSaIdEE17_S_check_init_lenEmRKS0_, _ZNSt12_Destroy_auxILb1EE9__destroyIPdEEvT_S3_, _ZNKSt16initializer_listIdE4sizeEv, __orc_init_func.cling-module-290, _ZN5cling5Value7CastFwdIbE4castERKS0_, _ZNSt12_Vector_baseIdSaIdEE12_Vector_implC2ERKS0_, _ZNSt6vectorIdSaIdEEC1ESt16initializer_listIdERKS0_, _ZNSt6vectorIdSaIdEEC2ESt16initializer_listIdERKS0_, _ZNSt12_Vector_baseIdSaIdEE11_M_allocateEm, _ZNSt6vectorIdSaIdEED2Ev, _ZNSt12_Vector_baseIdSaIdEE12_Vector_implC1ERKS0_, _ZNKSt16initializer_listIdE3endEv, _ZSt12__niter_baseIPKdET_S2_, _ZNSt12_Vector_baseIdSaIdEED2Ev, _ZNSt6vectorIdSaIdEE21_M_range_initialize_nIPKdEEvT_S5_m }) }\n"
]
}
],
"source": [
"std::vector v_root{1,2,3,4,5,6,7,8};\n",
"r[\"v\"] = v_root;\n",
"r << \"v2<-seq(1:8)\";\n",
"bool isEqual = r.Eval(\"prod(v==v2)\");\n",
"if (!isEqual) {\n",
" Error(\"ROOT-R-Example\",\"Different vector created\");\n",
" r << \"print(v)\";\n",
" r << \"print(v2)\";\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "3383a557",
"metadata": {},
"source": [
"example on how to pass functions to R"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "3c4bd74a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:30.296803Z",
"iopub.status.busy": "2026-05-19T20:27:30.296675Z",
"iopub.status.idle": "2026-05-19T20:27:30.504293Z",
"shell.execute_reply": "2026-05-19T20:27:30.503504Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_58:2:18: error: cannot initialize an array element of type 'void *' with an rvalue of type 'double (*)(double, double, double)'\n",
" r[\"gaus_pdf\"] = ROOT::Math::normal_pdf;\n",
" ^~~~~~~~~~~~~~~~~~~~~~\n"
]
}
],
"source": [
"r[\"gaus_pdf\"] = ROOT::Math::normal_pdf;\n",
"\n",
"r << \"y<-gaus_pdf(0,1,1)\";\n",
"double value_r = r[\"y\"];\n",
"double value_root = ROOT::Math::normal_pdf(0,1,1);\n",
"std::cout << \"Function gaussian(0,1,1) evaluated in R = \" << value_r << std::endl;\n",
"std::cout << \"Function gaussian(0,1,1) evaluated in ROOT = \" << value_root << std::endl;\n",
"if (value_r != value_root)\n",
" Error(\"ROOT-R-Example\",\"Different function value found in r = %f and ROOT = %f\", value_r, value_root);"
]
}
],
"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
}