{ "cells": [ { "cell_type": "markdown", "id": "81c5df3d", "metadata": {}, "source": [ "# fit1\n", "Simple fitting example (1-d histogram with an interpreted function)\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": "5dac17d6", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:39.141336Z", "iopub.status.busy": "2026-05-19T20:24:39.141224Z", "iopub.status.idle": "2026-05-19T20:24:39.487352Z", "shell.execute_reply": "2026-05-19T20:24:39.486782Z" } }, "outputs": [], "source": [ "TCanvas *c1 = new TCanvas(\"c1_fit1\",\"The Fit Canvas\",200,10,700,500);\n", "c1->SetGridx();\n", "c1->SetGridy();\n", "c1->GetFrame()->SetFillColor(21);\n", "c1->GetFrame()->SetBorderMode(-1);\n", "c1->GetFrame()->SetBorderSize(5);" ] }, { "cell_type": "markdown", "id": "c8ffc1a9", "metadata": {}, "source": [ "(for more details, see\n", "filling histograms with random numbers from a function)" ] }, { "cell_type": "code", "execution_count": 2, "id": "ec703a29", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:39.489517Z", "iopub.status.busy": "2026-05-19T20:24:39.489397Z", "iopub.status.idle": "2026-05-19T20:24:39.695623Z", "shell.execute_reply": "2026-05-19T20:24:39.694990Z" } }, "outputs": [], "source": [ "TFormula *form1 = new TFormula(\"form1\", \"abs(sin(x)/x)\");\n", "TF1 *sqroot = new TF1(\"sqroot\", \"x*gaus(0) + [3]*form1\", 0.0, 10.0);\n", "sqroot->SetLineColor(4);\n", "sqroot->SetLineWidth(6);" ] }, { "cell_type": "markdown", "id": "776814f1", "metadata": {}, "source": [ "Set parameters to the functions \"gaus\" and \"form1\"." ] }, { "cell_type": "code", "execution_count": 3, "id": "0baa8f88", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:39.697695Z", "iopub.status.busy": "2026-05-19T20:24:39.697552Z", "iopub.status.idle": "2026-05-19T20:24:39.903932Z", "shell.execute_reply": "2026-05-19T20:24:39.903280Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Formula based function: sqroot \n", " sqroot : x*gaus(0) + [3]*form1 Ndim= 1, Npar= 4, Number= 0 \n", " Formula expression: \n", "\tx*[p0]*exp(-0.5*((x-[p1])/[p2])*((x-[p1])/[p2]))+[p3]*(abs(sin(x)/x)) \n" ] } ], "source": [ "sqroot->SetParameters(10.0, 4.0, 1.0, 20.0);\n", "sqroot->Print();\n", "\n", "TH1D *h1d = new TH1D(\"h1d\", \"Test random numbers\", 200, 0.0, 10.0);\n", "h1d->FillRandom(\"sqroot\", 10000);" ] }, { "cell_type": "markdown", "id": "befb66f6", "metadata": {}, "source": [ "Now fit histogram h1d with the function sqroot" ] }, { "cell_type": "code", "execution_count": 4, "id": "e852f3be", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:39.915454Z", "iopub.status.busy": "2026-05-19T20:24:39.915325Z", "iopub.status.idle": "2026-05-19T20:24:40.121918Z", "shell.execute_reply": "2026-05-19T20:24:40.121295Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "Chi2 = 198.935\n", "NDf = 190\n", "Edm = 1.49283e-07\n", "NCalls = 149\n", "p0 = 33.1658 +/- 0.545703 \n", "p1 = 4.00667 +/- 0.0165304 \n", "p2 = 0.984663 +/- 0.0128238 \n", "p3 = 63.4464 +/- 1.33233 \n" ] } ], "source": [ "h1d->SetFillColor(45);\n", "h1d->Fit(\"sqroot\");" ] }, { "cell_type": "markdown", "id": "8bfbf428", "metadata": {}, "source": [ "We now annotate the picture by creating a PaveText object\n", "and displaying the list of commands in this macro" ] }, { "cell_type": "code", "execution_count": 5, "id": "d1d4ace0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:40.124354Z", "iopub.status.busy": "2026-05-19T20:24:40.124230Z", "iopub.status.idle": "2026-05-19T20:24:40.353142Z", "shell.execute_reply": "2026-05-19T20:24:40.352720Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "