{
"cells": [
{
"cell_type": "markdown",
"id": "e505797a",
"metadata": {},
"source": [
"# exampleFunctor\n",
"Tutorial illustrating how to create a TF1 class using C++ functors or class member functions.\n",
"\n",
"Can be run with:\n",
"\n",
"```cpp\n",
" root > .x exampleFunctor.C\n",
" root > .x exampleFunctor.C+ with ACLIC\n",
"```\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Lorenzo Moneta \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:24 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "49cec8e8",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:24.803481Z",
"iopub.status.busy": "2026-05-19T20:24:24.803358Z",
"iopub.status.idle": "2026-05-19T20:24:24.809719Z",
"shell.execute_reply": "2026-05-19T20:24:24.809196Z"
}
},
"outputs": [],
"source": [
"%%cpp -d"
]
},
{
"cell_type": "markdown",
"id": "82ff02dc",
"metadata": {},
"source": [
"function class with a member function"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2c9d5f62",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:24.811280Z",
"iopub.status.busy": "2026-05-19T20:24:24.811158Z",
"iopub.status.idle": "2026-05-19T20:24:25.131115Z",
"shell.execute_reply": "2026-05-19T20:24:25.130345Z"
}
},
"outputs": [],
"source": [
"struct MyIntegFunc {\n",
" MyIntegFunc(TF1 * f): fFunc(f) {}\n",
" double Integral (double *x, double * ) const {\n",
" double a = fFunc->GetXmin();\n",
" return fFunc->Integral(a, *x);\n",
" }\n",
" TF1 * fFunc;\n",
"};"
]
},
{
"cell_type": "markdown",
"id": "28c74b54",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "ccdfc43e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:25.132914Z",
"iopub.status.busy": "2026-05-19T20:24:25.132788Z",
"iopub.status.idle": "2026-05-19T20:24:25.136208Z",
"shell.execute_reply": "2026-05-19T20:24:25.135629Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"\n",
"#include \"TF1.h\"\n",
"#include \"TMath.h\"\n",
"#include \"TLegend.h\"\n",
"\n",
"\n",
"double MyFunc (double *x, double *p ) {\n",
" return TMath::Gaus(x[0],p[0],p[1] );\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "e395b7f4",
"metadata": {},
"source": [
" function o\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c6255bb1",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:25.137496Z",
"iopub.status.busy": "2026-05-19T20:24:25.137381Z",
"iopub.status.idle": "2026-05-19T20:24:25.139671Z",
"shell.execute_reply": "2026-05-19T20:24:25.139198Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_54:1:8: error: use of undeclared identifier 'functor'\n",
"bject (functor)\n",
" ^\n"
]
}
],
"source": [
"%%cpp -d\n",
"bject (functor)\n",
"struct MyDerivFunc {\n",
" MyDerivFunc(TF1 * f): fFunc(f) {}\n",
" double operator() (double *x, double * ) const {\n",
" return fFunc->Derivative(*x);\n",
" }\n",
" TF1 * fFunc;\n",
"};"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "e872b505",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:25.141097Z",
"iopub.status.busy": "2026-05-19T20:24:25.140986Z",
"iopub.status.idle": "2026-05-19T20:24:25.347339Z",
"shell.execute_reply": "2026-05-19T20:24:25.346647Z"
}
},
"outputs": [],
"source": [
"double xmin = -10; double xmax = 10;"
]
},
{
"cell_type": "markdown",
"id": "c50127de",
"metadata": {},
"source": [
"create TF1 using a free C function"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c952d70b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:25.349382Z",
"iopub.status.busy": "2026-05-19T20:24:25.349259Z",
"iopub.status.idle": "2026-05-19T20:24:25.555947Z",
"shell.execute_reply": "2026-05-19T20:24:25.555255Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Info in : created default TCanvas with name c1\n"
]
}
],
"source": [
"TF1 * f1 = new TF1(\"f1\",MyFunc,xmin,xmax,2);\n",
"f1->SetParameters(0.,1.);\n",
"f1->SetMaximum(3); f1->SetMinimum(-1);\n",
"f1->Draw();"
]
},
{
"cell_type": "markdown",
"id": "a1b60b3d",
"metadata": {},
"source": [
"Derivative function\n",
"example to create TF1 using a functor"
]
},
{
"cell_type": "markdown",
"id": "de24fca6",
"metadata": {},
"source": [
"in order to work with interpreter the function object must be created and lived all time for all time\n",
"of the TF1. In compiled mode, the function object can be passed by value (recommended) and there\n",
"is also no need to specify the type of the function class. Example is as follows:\n",
"\n",
"`TF1 * f2 = new TF1(\"f2\",MyDerivFunc(f1), xmin, xmax,0); // only for C++ compiled mode`"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "0e62bc42",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:25.557467Z",
"iopub.status.busy": "2026-05-19T20:24:25.557331Z",
"iopub.status.idle": "2026-05-19T20:24:25.762294Z",
"shell.execute_reply": "2026-05-19T20:24:25.761566Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_60:2:28: error: unknown type name 'MyDerivFunc'\n",
" MyDerivFunc * deriv = new MyDerivFunc(f1);\n",
" ^\n",
"input_line_60:3:25: error: use of undeclared identifier 'deriv'\n",
"TF1 * f2 = new TF1(\"f2\",deriv, xmin, xmax, 0);\n",
" ^\n"
]
}
],
"source": [
"MyDerivFunc * deriv = new MyDerivFunc(f1);\n",
"TF1 * f2 = new TF1(\"f2\",deriv, xmin, xmax, 0);\n",
"\n",
"f2->SetLineColor(kBlue);\n",
"f2->Draw(\"same\");"
]
},
{
"cell_type": "markdown",
"id": "d800908b",
"metadata": {},
"source": [
"Integral function\n",
"example to create a TF1 using a member function of a user class"
]
},
{
"cell_type": "markdown",
"id": "6efb543e",
"metadata": {},
"source": [
"in order to work with interpreter the function object must be created and lived all time for all time\n",
"of the TF1. In compiled mode there is no need to specify the type of the function class and the name\n",
"of the member function\n",
"\n",
"`TF1 * f3 = new TF1(\"f3\",intg,&MyIntegFunc::Integral,xmin,xmax, 0); // only for C++ compiled mode`"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "868d9c37",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:25.763824Z",
"iopub.status.busy": "2026-05-19T20:24:25.763700Z",
"iopub.status.idle": "2026-05-19T20:24:25.970265Z",
"shell.execute_reply": "2026-05-19T20:24:25.969744Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_62:2:45: error: use of undeclared identifier 'f2'\n",
" (((*(TLegend **)0x7fdb982010c8))->AddEntry(f2, \"Deriv.\"))\n",
" ^\n",
"Error in : Error evaluating expression (((*(TLegend **)0x7fdb982010c8))->AddEntry(f2, \"Deriv.\"))\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"MyIntegFunc * intg = new MyIntegFunc(f1);\n",
"TF1 * f3 = new TF1(\"f3\",intg,&MyIntegFunc::Integral, xmin, xmax, 0);\n",
"\n",
"f3->SetLineColor(kRed);\n",
"f3->Draw(\"same\");\n",
"\n",
"TLegend * l = new TLegend(0.78, 0.25, 0.97 ,0.45);\n",
"l->AddEntry(f1, \"Func\");\n",
"l->AddEntry(f2, \"Deriv.\");\n",
"l->AddEntry(f3, \"Integral\");\n",
"l->Draw();"
]
},
{
"cell_type": "markdown",
"id": "e67265f3",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "cd6f7960",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:25.979870Z",
"iopub.status.busy": "2026-05-19T20:24:25.979683Z",
"iopub.status.idle": "2026-05-19T20:24:26.187129Z",
"shell.execute_reply": "2026-05-19T20:24:26.186205Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"gROOT->GetListOfCanvases()->Draw()"
]
}
],
"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
}