{
"cells": [
{
"cell_type": "markdown",
"id": "52ac17ac",
"metadata": {},
"source": [
"# fitExclude\n",
"Illustrates how to fit excluding points in a given range.\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:24 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f712a2bc",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:50.968170Z",
"iopub.status.busy": "2026-05-19T20:24:50.968052Z",
"iopub.status.idle": "2026-05-19T20:24:50.979136Z",
"shell.execute_reply": "2026-05-19T20:24:50.978342Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \n",
"#include \n",
"#include \n",
"\n",
"bool reject;"
]
},
{
"cell_type": "markdown",
"id": "126da84b",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "740eb838",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:50.980555Z",
"iopub.status.busy": "2026-05-19T20:24:50.980436Z",
"iopub.status.idle": "2026-05-19T20:24:50.983704Z",
"shell.execute_reply": "2026-05-19T20:24:50.982935Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"double fline(double *x, double *par)\n",
"{\n",
" if (reject && x[0] > 2.5 && x[0] < 3.5) {\n",
" TF1::RejectPoint();\n",
" return 0;\n",
" }\n",
" return par[0] + par[1]*x[0];\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "3e3178ad",
"metadata": {},
"source": [
"Create a source function"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "3b5f0557",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:50.985044Z",
"iopub.status.busy": "2026-05-19T20:24:50.984930Z",
"iopub.status.idle": "2026-05-19T20:24:51.304249Z",
"shell.execute_reply": "2026-05-19T20:24:51.303087Z"
}
},
"outputs": [],
"source": [
"TF1 *f1 = new TF1(\"f1\",\"[0] +[1]*x +gaus(2)\",0,5);\n",
"f1->SetParameters(6,-1,5,3,0.2);"
]
},
{
"cell_type": "markdown",
"id": "e8263b2f",
"metadata": {},
"source": [
"create and fill histogram according to the source function"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f5a5c694",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:51.305993Z",
"iopub.status.busy": "2026-05-19T20:24:51.305871Z",
"iopub.status.idle": "2026-05-19T20:24:51.533834Z",
"shell.execute_reply": "2026-05-19T20:24:51.529430Z"
}
},
"outputs": [],
"source": [
"TH1F *h = new TH1F(\"h\",\"background + signal\",100,0,5);\n",
"h->FillRandom(\"f1\",2000);\n",
"TF1 *fl = new TF1(\"fl\",fline,0,5,2);\n",
"fl->SetParameters(2,-1);"
]
},
{
"cell_type": "markdown",
"id": "e0789eea",
"metadata": {},
"source": [
"fit only the linear background excluding the signal area"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3998aa5c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:51.541328Z",
"iopub.status.busy": "2026-05-19T20:24:51.541195Z",
"iopub.status.idle": "2026-05-19T20:24:51.747681Z",
"shell.execute_reply": "2026-05-19T20:24:51.747110Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"****************************************\n",
"Minimizer is Minuit2 / Migrad\n",
"Chi2 = 84.1529\n",
"NDf = 78\n",
"Edm = 7.20399e-21\n",
"NCalls = 32\n",
"p0 = 28.4731 +/- 0.946564 \n",
"p1 = -4.81581 +/- 0.26533 \n"
]
}
],
"source": [
"reject = true;\n",
"h->Fit(fl,\"0\");\n",
"reject = false;"
]
},
{
"cell_type": "markdown",
"id": "e89d2ee7",
"metadata": {},
"source": [
"store 2 separate functions for visualization"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "53172c82",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:51.749408Z",
"iopub.status.busy": "2026-05-19T20:24:51.749286Z",
"iopub.status.idle": "2026-05-19T20:24:51.963960Z",
"shell.execute_reply": "2026-05-19T20:24:51.958663Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Info in : created default TCanvas with name c1\n"
]
}
],
"source": [
"TF1 *fleft = new TF1(\"fleft\",fline,0,2.5,2);\n",
"fleft->SetParameters(fl->GetParameters());\n",
"h->GetListOfFunctions()->Add(fleft);\n",
"gROOT->GetListOfFunctions()->Remove(fleft);\n",
"TF1 *fright = new TF1(\"fright\",fline,3.5,5,2);\n",
"fright->SetParameters(fl->GetParameters());\n",
"h->GetListOfFunctions()->Add(fright);\n",
"gROOT->GetListOfFunctions()->Remove(fright);\n",
"h->Draw();"
]
},
{
"cell_type": "markdown",
"id": "64a79fb9",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "e50c9a6f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:51.971802Z",
"iopub.status.busy": "2026-05-19T20:24:51.971660Z",
"iopub.status.idle": "2026-05-19T20:24:52.203214Z",
"shell.execute_reply": "2026-05-19T20:24:52.202654Z"
}
},
"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
}