{
"cells": [
{
"cell_type": "markdown",
"id": "4fc4d1ed",
"metadata": {},
"source": [
"# multifit\n",
" Fitting multiple functions to different ranges of a 1-D histogram\n",
" Example showing how to fit in a sub-range of an histogram\n",
" A histogram is created and filled with the bin contents and errors\n",
" defined in the table below.\n",
" Three Gaussians are fitted in sub-ranges of this histogram.\n",
" A new function (a sum of 3 Gaussians) is fitted on another subrange\n",
" Note that when fitting simple functions, such as Gaussians, the initial\n",
" values of parameters are automatically computed by ROOT.\n",
" In the more complicated case of the sum of 3 Gaussians, the initial values\n",
" of parameters must be given. In this particular case, the initial values\n",
" are taken from the result of the individual fits.\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Rene Brun \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:25 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f63e36ba",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:12.461269Z",
"iopub.status.busy": "2026-05-19T20:25:12.461157Z",
"iopub.status.idle": "2026-05-19T20:25:12.781745Z",
"shell.execute_reply": "2026-05-19T20:25:12.781058Z"
}
},
"outputs": [],
"source": [
"const int np = 49;\n",
"float x[np] = {1.913521, 1.953769, 2.347435, 2.883654, 3.493567, 4.047560, 4.337210, 4.364347, 4.563004,\n",
" 5.054247, 5.194183, 5.380521, 5.303213, 5.384578, 5.563983, 5.728500, 5.685752, 5.080029,\n",
" 4.251809, 3.372246, 2.207432, 1.227541, 0.8597788, 0.8220503, 0.8046592, 0.7684097, 0.7469761,\n",
" 0.8019787, 0.8362375, 0.8744895, 0.9143721, 0.9462768, 0.9285364, 0.8954604, 0.8410891, 0.7853871,\n",
" 0.7100883, 0.6938808, 0.7363682, 0.7032954, 0.6029015, 0.5600163, 0.7477068, 1.188785, 1.938228,\n",
" 2.602717, 3.472962, 4.465014, 5.177035};"
]
},
{
"cell_type": "markdown",
"id": "0751e5b5",
"metadata": {},
"source": [
"The histogram are filled with bins defined in the array x."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "87379101",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:12.783937Z",
"iopub.status.busy": "2026-05-19T20:25:12.783815Z",
"iopub.status.idle": "2026-05-19T20:25:12.986142Z",
"shell.execute_reply": "2026-05-19T20:25:12.985516Z"
}
},
"outputs": [],
"source": [
"TH1F *h = new TH1F(\"h\", \"Example of several fits in subranges\", np, 85, 134);\n",
"h->SetMaximum(7);\n",
"\n",
"for (int i = 0; i < np; i++) {\n",
" h->SetBinContent(i + 1, x[i]);\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "91b14195",
"metadata": {},
"source": [
"Define the parameter array for the total function."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b83d93a0",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:12.988297Z",
"iopub.status.busy": "2026-05-19T20:25:12.988183Z",
"iopub.status.idle": "2026-05-19T20:25:13.208246Z",
"shell.execute_reply": "2026-05-19T20:25:13.207546Z"
}
},
"outputs": [],
"source": [
"double par[9];"
]
},
{
"cell_type": "markdown",
"id": "045179a0",
"metadata": {},
"source": [
"Three TF1 objects are created, one for each subrange."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ecdd59f5",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:13.210244Z",
"iopub.status.busy": "2026-05-19T20:25:13.210126Z",
"iopub.status.idle": "2026-05-19T20:25:13.416693Z",
"shell.execute_reply": "2026-05-19T20:25:13.416025Z"
}
},
"outputs": [],
"source": [
"TF1 *g1 = new TF1(\"g1\", \"gaus\", 85, 95);\n",
"TF1 *g2 = new TF1(\"g2\", \"gaus\", 98, 108);\n",
"TF1 *g3 = new TF1(\"g3\", \"gaus\", 110, 121);"
]
},
{
"cell_type": "markdown",
"id": "2977f76f",
"metadata": {},
"source": [
"The total is the sum of the three, each has three parameters."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "51c942f9",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:13.418810Z",
"iopub.status.busy": "2026-05-19T20:25:13.418691Z",
"iopub.status.idle": "2026-05-19T20:25:13.625257Z",
"shell.execute_reply": "2026-05-19T20:25:13.624741Z"
}
},
"outputs": [],
"source": [
"TF1 *total = new TF1(\"total\", \"gaus(0)+gaus(3)+gaus(6)\", 85, 125);\n",
"total->SetLineColor(2);"
]
},
{
"cell_type": "markdown",
"id": "dee8775b",
"metadata": {},
"source": [
"Fit each function and add it to the list of functions. By default,\n",
"TH1::Fit() fits the function on the defined histogram range. You can\n",
"specify the \"R\" option in the second parameter of TH1::Fit() to restrict\n",
"the fit to the range specified in the TF1 constructor. Alternatively, you\n",
"can also specify the range in the call to TH1::Fit(), which we demonstrate\n",
"here with the 3rd Gaussian. The \"+\" option needs to be added to the later\n",
"fits to not replace existing fitted functions in the histogram."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f9a91508",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:13.626988Z",
"iopub.status.busy": "2026-05-19T20:25:13.626871Z",
"iopub.status.idle": "2026-05-19T20:25:13.834391Z",
"shell.execute_reply": "2026-05-19T20:25:13.833965Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"****************************************\n",
"Minimizer is Minuit2 / Migrad\n",
"Chi2 = 0.0848003\n",
"NDf = 7\n",
"Edm = 8.86911e-08\n",
"NCalls = 106\n",
"Constant = 4.96664 +/- 2.83221 \n",
"Mean = 95.4663 +/- 12.3905 \n",
"Sigma = 6.82779 +/- 7.49131 \t (limited)\n",
"****************************************\n",
"Minimizer is Minuit2 / Migrad\n",
"Chi2 = 0.0771026\n",
"NDf = 7\n",
"Edm = 1.00182e-07\n",
"NCalls = 73\n",
"Constant = 5.96312 +/- 1.14355 \n",
"Mean = 100.467 +/- 1.53372 \n",
"Sigma = 3.54806 +/- 1.16899 \t (limited)\n",
"****************************************\n",
"Minimizer is Minuit2 / Migrad\n",
"Chi2 = 0.00877492\n",
"NDf = 8\n",
"Edm = 4.98832e-06\n",
"NCalls = 87\n",
"Constant = 0.912053 +/- 0.435309 \n",
"Mean = 116.304 +/- 8.32344 \n",
"Sigma = 8.38103 +/- 18.5139 \t (limited)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Info in : created default TCanvas with name c1\n"
]
}
],
"source": [
"h->Fit(g1, \"R\");\n",
"h->Fit(g2, \"R+\");\n",
"h->Fit(g3, \"+\", \"\", 110, 121);"
]
},
{
"cell_type": "markdown",
"id": "9a89db1a",
"metadata": {},
"source": [
"Get the parameters from the fit."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7da45f86",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:13.835998Z",
"iopub.status.busy": "2026-05-19T20:25:13.835880Z",
"iopub.status.idle": "2026-05-19T20:25:14.042181Z",
"shell.execute_reply": "2026-05-19T20:25:14.041716Z"
}
},
"outputs": [],
"source": [
"g1->GetParameters(&par[0]);\n",
"g2->GetParameters(&par[3]);\n",
"g3->GetParameters(&par[6]);"
]
},
{
"cell_type": "markdown",
"id": "b15bfdba",
"metadata": {},
"source": [
"Use the parameters on the sum."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "2991de03",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:14.052102Z",
"iopub.status.busy": "2026-05-19T20:25:14.051965Z",
"iopub.status.idle": "2026-05-19T20:25:14.257043Z",
"shell.execute_reply": "2026-05-19T20:25:14.256649Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"****************************************\n",
"Minimizer is Minuit2 / Migrad\n",
"Chi2 = 0.31282\n",
"NDf = 31\n",
"Edm = 3.25007e-06\n",
"NCalls = 495\n",
"p0 = 4.91052 +/- 1.41324 \n",
"p1 = 94.4492 +/- 3.71244 \n",
"p2 = 5.9461 +/- 2.41662 \n",
"p3 = 3.22456 +/- 3.11384 \n",
"p4 = 101.662 +/- 1.67862 \n",
"p5 = 2.48631 +/- 1.91151 \n",
"p6 = 0.911626 +/- 0.368736 \n",
"p7 = 117.581 +/- 5.06092 \n",
"p8 = 7.59194 +/- 8.78217 \n"
]
}
],
"source": [
"total->SetParameters(par);\n",
"h->Fit(total, \"R+\");"
]
},
{
"cell_type": "markdown",
"id": "cf156c0e",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "2b4182e3",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:14.258778Z",
"iopub.status.busy": "2026-05-19T20:25:14.258655Z",
"iopub.status.idle": "2026-05-19T20:25:14.488114Z",
"shell.execute_reply": "2026-05-19T20:25:14.487679Z"
}
},
"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
}