{
"cells": [
{
"cell_type": "markdown",
"id": "e03643ac",
"metadata": {},
"source": [
"# fitLinear\n",
"Example of fitting with a linear function, using TLinearFitter\n",
"This example is for a TGraphErrors, but it can also be used\n",
"when fitting a histogram, a TGraph2D or a TMultiGraph\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Anna Kreshuk \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": "f83ebfd0",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:54.236106Z",
"iopub.status.busy": "2026-05-19T20:24:54.235956Z",
"iopub.status.idle": "2026-05-19T20:24:54.251767Z",
"shell.execute_reply": "2026-05-19T20:24:54.251075Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"TGraphErrors.h\"\n",
"#include \"TF1.h\"\n",
"#include \"TRandom.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TLegend.h\"\n",
"#include \"TMath.h\"\n",
"\n",
"\n",
"void makePoints(int n, double *x, double *y, double *e, int p);"
]
},
{
"cell_type": "markdown",
"id": "fc4ffa76",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d5f4c450",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:54.253186Z",
"iopub.status.busy": "2026-05-19T20:24:54.253071Z",
"iopub.status.idle": "2026-05-19T20:24:54.258959Z",
"shell.execute_reply": "2026-05-19T20:24:54.258371Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void makePoints(int n, double *x, double *y, double *e, int p)\n",
"{\n",
" int i;\n",
" TRandom r;\n",
"\n",
" if (p==2) {\n",
" for (i=0; iSetGrid();"
]
},
{
"cell_type": "markdown",
"id": "1cb5d680",
"metadata": {},
"source": [
"Generate points along a 3rd degree polynomial:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9d1d34ed",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:54.614187Z",
"iopub.status.busy": "2026-05-19T20:24:54.614065Z",
"iopub.status.idle": "2026-05-19T20:24:54.818795Z",
"shell.execute_reply": "2026-05-19T20:24:54.818027Z"
}
},
"outputs": [],
"source": [
"makePoints(n, x, y, e, 3);\n",
"TGraphErrors *gre3 = new TGraphErrors(n, x, y, nullptr, e);\n",
"gre3->Draw(\"a*\");"
]
},
{
"cell_type": "markdown",
"id": "b20b140b",
"metadata": {},
"source": [
"Fit the graph with the predefined \"pol3\" function"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "e33785c8",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:54.820515Z",
"iopub.status.busy": "2026-05-19T20:24:54.820399Z",
"iopub.status.idle": "2026-05-19T20:24:55.147120Z",
"shell.execute_reply": "2026-05-19T20:24:55.146424Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"****************************************\n",
"Minimizer is Linear / Migrad\n",
"Chi2 = 36.5406\n",
"NDf = 36\n",
"p0 = -7.07142 +/- 0.0233493 \n",
"p1 = -0.0194368 +/- 0.0354128 \n",
"p2 = 2.03968 +/- 0.0136149 \n",
"p3 = 1.00594 +/- 0.0139068 \n"
]
}
],
"source": [
"gre3->Fit(\"pol3\");"
]
},
{
"cell_type": "markdown",
"id": "fbecd9c7",
"metadata": {},
"source": [
"Access the fit results"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "70c1f832",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:55.148668Z",
"iopub.status.busy": "2026-05-19T20:24:55.148524Z",
"iopub.status.idle": "2026-05-19T20:24:55.354906Z",
"shell.execute_reply": "2026-05-19T20:24:55.354176Z"
}
},
"outputs": [],
"source": [
"TF1 *f3 = gre3->GetFunction(\"pol3\");\n",
"f3->SetLineWidth(1);"
]
},
{
"cell_type": "markdown",
"id": "804c5332",
"metadata": {},
"source": [
"Generate points along a sin(x)+sin(2x) function"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "af2e204e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:55.356897Z",
"iopub.status.busy": "2026-05-19T20:24:55.356770Z",
"iopub.status.idle": "2026-05-19T20:24:55.591807Z",
"shell.execute_reply": "2026-05-19T20:24:55.575278Z"
}
},
"outputs": [],
"source": [
"makePoints(n, x, y, e, 2);\n",
"TGraphErrors *gre2=new TGraphErrors(n, x, y, nullptr, e);\n",
"gre2->Draw(\"*same\");\n",
"gre2->SetMarkerColor(kBlue);\n",
"gre2->SetLineColor(kBlue);"
]
},
{
"cell_type": "markdown",
"id": "2b5ebcef",
"metadata": {},
"source": [
"The fitting function can be predefined and passed to the Fit function\n",
"The \"++\" mean that the linear fitter should be used, and the following\n",
"formula is equivalent to \"[0]*sin(x) + [1]*sin(2*x)\"\n",
"A function, defined this way, is in no way different from any other TF1,\n",
"it can be evaluated, drawn, you can get its parameters, etc.\n",
"The fit result (parameter values, parameter errors, chisquare, etc) are\n",
"written into the fitting function."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "f24ce665",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:55.599407Z",
"iopub.status.busy": "2026-05-19T20:24:55.599269Z",
"iopub.status.idle": "2026-05-19T20:24:55.812039Z",
"shell.execute_reply": "2026-05-19T20:24:55.811501Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"****************************************\n",
"Minimizer is Linear / Migrad\n",
"Chi2 = 46.7362\n",
"NDf = 38\n",
"p0 = 1.0005 +/- 0.0242765 \n",
"p1 = 0.985942 +/- 0.0279149 \n"
]
}
],
"source": [
"TF1 *f2 = new TF1(\"f2\", \"sin(x) ++ sin(2*x)\", -2, 2);\n",
"gre2->Fit(f2);\n",
"f2 = gre2->GetFunction(\"f2\");\n",
"f2->SetLineColor(kBlue);\n",
"f2->SetLineWidth(1);"
]
},
{
"cell_type": "markdown",
"id": "e22ab827",
"metadata": {},
"source": [
"Generate points along a -2+exp(-x) function"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "1ee77466",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:55.828377Z",
"iopub.status.busy": "2026-05-19T20:24:55.828240Z",
"iopub.status.idle": "2026-05-19T20:24:56.035370Z",
"shell.execute_reply": "2026-05-19T20:24:56.034859Z"
}
},
"outputs": [],
"source": [
"makePoints(n, x, y, e, 4);\n",
"TGraphErrors *gre4=new TGraphErrors(n, x, y, nullptr, e);\n",
"gre4->Draw(\"*same\");\n",
"gre4->SetMarkerColor(kRed);\n",
"gre4->SetLineColor(kRed);"
]
},
{
"cell_type": "markdown",
"id": "7fb97211",
"metadata": {},
"source": [
"If you don't want to define the function, you can just pass the string\n",
"with the formula:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b6633328",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:56.038310Z",
"iopub.status.busy": "2026-05-19T20:24:56.038184Z",
"iopub.status.idle": "2026-05-19T20:24:56.266144Z",
"shell.execute_reply": "2026-05-19T20:24:56.265285Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"****************************************\n",
"Minimizer is Linear / Migrad\n",
"Chi2 = 43.6161\n",
"NDf = 38\n",
"p0 = -2.04095 +/- 0.0220454 \n",
"p1 = 1.01171 +/- 0.00904363 \n"
]
}
],
"source": [
"gre4->Fit(\"1 ++ exp(-x)\");"
]
},
{
"cell_type": "markdown",
"id": "b7650833",
"metadata": {},
"source": [
"Access the fit results:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "43880dcb",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:56.267731Z",
"iopub.status.busy": "2026-05-19T20:24:56.267581Z",
"iopub.status.idle": "2026-05-19T20:24:56.474484Z",
"shell.execute_reply": "2026-05-19T20:24:56.473547Z"
}
},
"outputs": [],
"source": [
"TF1 *f4 = gre4->GetFunction(\"1 ++ exp(-x)\");\n",
"f4->SetName(\"f4\");\n",
"f4->SetLineColor(kRed);\n",
"f4->SetLineWidth(1);\n",
"\n",
"TLegend *leg = new TLegend(0.3, 0.7, 0.65, 0.9);\n",
"leg->AddEntry(gre3, \" -7 + 2*x*x + x*x*x\", \"p\");\n",
"leg->AddEntry(gre2, \"sin(x) + sin(2*x)\", \"p\");\n",
"leg->AddEntry(gre4, \"-2 + exp(-x)\", \"p\");\n",
"leg->Draw();"
]
},
{
"cell_type": "markdown",
"id": "c599f3ee",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "3fa4a6da",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:56.476105Z",
"iopub.status.busy": "2026-05-19T20:24:56.475980Z",
"iopub.status.idle": "2026-05-19T20:24:56.703379Z",
"shell.execute_reply": "2026-05-19T20:24:56.702788Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%jsroot on\n",
"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
}