{ "cells": [ { "cell_type": "markdown", "id": "e2f15bd9", "metadata": {}, "source": [ "# fithist\n", "Example of fit where the model is histogram + 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": "e8e135b8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:52.975783Z", "iopub.status.busy": "2026-05-19T20:24:52.975652Z", "iopub.status.idle": "2026-05-19T20:24:52.987201Z", "shell.execute_reply": "2026-05-19T20:24:52.986537Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \n", "#include \n", "#include \n", "\n", "TH1F *background;" ] }, { "cell_type": "markdown", "id": "3eb617b8", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 2, "id": "efebd9dc", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:52.988525Z", "iopub.status.busy": "2026-05-19T20:24:52.988409Z", "iopub.status.idle": "2026-05-19T20:24:53.006578Z", "shell.execute_reply": "2026-05-19T20:24:53.006156Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void histgen() {\n", " //generate the histogram background and save it to a file\n", " //background taken as linearly decreasing\n", "\n", " TF1 f1(\"f1\",\"pol1\",0,10);\n", " f1.SetParameters(5,-0.5);\n", " TH1F h(\"background\",\"linear background\",100,0,10);\n", " h.FillRandom(\"f1\",10000);\n", " TFile f(\"background.root\",\"recreate\");\n", " //save the background histogram\n", " h.Write();\n", " //superimpose a Gaussian signal to the background histogram\n", " TF1 f2(\"f2\",\"gaus\",0,10);\n", " f2.SetParameters(1,6,0.5);\n", " h.FillRandom(\"f2\",2000);\n", " h.SetName(\"result\");\n", " h.Write();\n", "}" ] }, { "cell_type": "markdown", "id": "8eafb989", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 3, "id": "23346293", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:53.008103Z", "iopub.status.busy": "2026-05-19T20:24:53.007984Z", "iopub.status.idle": "2026-05-19T20:24:53.012017Z", "shell.execute_reply": "2026-05-19T20:24:53.011641Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "double ftotal(double *x, double *par) {\n", " double xx = x[0];\n", " int bin = background->GetXaxis()->FindBin(xx);\n", " double br = par[3]*background->GetBinContent(bin);\n", " double arg = (xx-par[1])/par[2];\n", " double sr = par[0]*TMath::Exp(-0.5*arg*arg);\n", " return sr + br;\n", "}" ] }, { "cell_type": "markdown", "id": "294d8965", "metadata": {}, "source": [ "fit function ftotal to signal + background" ] }, { "cell_type": "code", "execution_count": 4, "id": "8a3e60d4", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:53.013546Z", "iopub.status.busy": "2026-05-19T20:24:53.013428Z", "iopub.status.idle": "2026-05-19T20:24:53.509400Z", "shell.execute_reply": "2026-05-19T20:24:53.508885Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "Chi2 = 7.84157\n", "NDf = 96\n", "Edm = 3.7399e-06\n", "NCalls = 174\n", "p0 = 156.638 +/- 5.91061 \t (limited)\n", "p1 = 5.98183 +/- 0.0189043 \n", "p2 = -0.506659 +/- 0.0180481 \n", "p3 = 1.0003 +/- 0.0112955 \n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "input_line_47:9:1: warning: 'norm' shadows a declaration with the same name in the 'std' namespace; use '::norm' to reference this declaration\n", "double norm = result->GetMaximum();\n", "^\n", "Info in : created default TCanvas with name c1\n" ] } ], "source": [ "histgen();\n", "\n", "TFile *f = new TFile(\"background.root\");\n", "background = (TH1F*)f->Get(\"background\"); //pointer used in ftotal\n", "TH1F *result = (TH1F*)f->Get(\"result\");\n", "\n", "TF1 *ftot = new TF1(\"ftot\",ftotal,0,10,4);\n", "double norm = result->GetMaximum();\n", "ftot->SetParameters(0.5*norm,5,.2,norm);\n", "ftot->SetParLimits(0,.3*norm,norm);\n", "\n", "result->Fit(\"ftot\",\"b\");" ] }, { "cell_type": "markdown", "id": "5519fb36", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 5, "id": "72fe7156", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:53.511251Z", "iopub.status.busy": "2026-05-19T20:24:53.511102Z", "iopub.status.idle": "2026-05-19T20:24:53.736985Z", "shell.execute_reply": "2026-05-19T20:24:53.736009Z" } }, "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 }