{ "cells": [ { "cell_type": "markdown", "id": "a642bc02", "metadata": {}, "source": [ "# minuit2FitBench\n", "Demonstrate performance and usage of Minuit2 and Fumili2 for monodimensional fits.\n", "\n", "\n", "\n", "\n", "**Author:** Lorenzo Moneta \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": null, "id": "9f76926f", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"TH1.h\"\n", "#include \"TF1.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TStopwatch.h\"\n", "#include \"TSystem.h\"\n", "#include \"TRandom3.h\"\n", "#include \"Math/MinimizerOptions.h\"\n", "#include \"TPaveLabel.h\"\n", "#include \"TStyle.h\"\n", "#include \"TMath.h\"\n", "#include \"TROOT.h\"\n", "#include \"TFrame.h\"\n", "/*#include \"Fit/FitConfig.h\"*/\n", "\n", "\n", "TF1 *fitFcn;\n", "TH1 *histo;" ] }, { "cell_type": "markdown", "id": "a1a9dfab", "metadata": {}, "source": [ " Quadratic background function\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "f2cafaf8", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "double background(double *x, double *par) {\n", " return par[0] + par[1]*x[0] + par[2]*x[0]*x[0];\n", "}" ] }, { "cell_type": "markdown", "id": "86341f6a", "metadata": {}, "source": [ " Lorenzian Peak function\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "829e98ee", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "double lorentzianPeak(double *x, double *par) {\n", " return (0.5*par[0]*par[1]/TMath::Pi()) /\n", " TMath::Max( 1.e-10,(x[0]-par[2])*(x[0]-par[2]) + .25*par[1]*par[1]);\n", "}" ] }, { "cell_type": "markdown", "id": "4c08674f", "metadata": {}, "source": [ " Sum of background and peak function\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "3fb4daf4", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "double fitFunction(double *x, double *par) {\n", " return background(x,par) + lorentzianPeak(x,&par[3]);\n", "}" ] }, { "cell_type": "markdown", "id": "5857f1c6", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": null, "id": "b21fa550", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "bool DoFit(const char* fitter, TVirtualPad *pad, int npass) {\n", " printf(\"\\n*********************************************************************************\\n\");\n", " printf(\"\\t %s \\n\",fitter);\n", " printf(\"*********************************************************************************\\n\");\n", "\n", " gRandom = new TRandom3();\n", " TStopwatch timer;\n", " // timer.Start();\n", " ROOT::Math::MinimizerOptions::SetDefaultMinimizer(fitter);\n", " pad->SetGrid();\n", " pad->SetLogy();\n", " fitFcn->SetParameters(1,1,1,6,.03,1);\n", " fitFcn->Update();\n", " std::string title = std::string(fitter) + \" fit bench\";\n", " histo = new TH1D(fitter,title.c_str(),200,0,3);\n", "\n", " TString fitterType(fitter);\n", "\n", " timer.Start();\n", " bool ok = true;\n", " // fill histogram many times\n", " // every time increase its statistics and re-use previous fitted\n", " // parameter values as starting point\n", " for (int pass=0;passSetParameters(1,1,1,6,.03,1);\n", " for (int i=0;i<5000;i++) {\n", " histo->Fill(fitFcn->GetRandom());\n", " }\n", " int iret = histo->Fit(fitFcn,\"Q0\");\n", " ok &= (iret == 0);\n", " if (iret!=0) Error(\"DoFit\",\"Fit pass %d failed !\",pass);\n", " }\n", " // do last fit computing Minos Errors (except for Fumili)\n", " if (!fitterType.Contains(\"Fumili\")) // Fumili does not implement Error options (MINOS)\n", " histo->Fit(fitFcn,\"E\");\n", " else\n", " histo->Fit(fitFcn,\"\");\n", " timer.Stop();\n", "\n", " (histo->GetFunction(\"fitFcn\"))->SetLineColor(kRed+3);\n", " gPad->SetFillColor(kYellow-10);\n", "\n", "\n", " double cputime = timer.CpuTime();\n", " printf(\"%s, npass=%d : RT=%7.3f s, Cpu=%7.3f s\\n\",fitter,npass,timer.RealTime(),cputime);\n", " TPaveLabel *p = new TPaveLabel(0.45,0.7,0.88,0.8,Form(\"%s CPU= %g s\",fitter,cputime),\"brNDC\");\n", " p->Draw();\n", " p->SetTextColor(kRed+3);\n", " p->SetFillColor(kYellow-8);\n", " pad->Update();\n", " return ok;\n", "}" ] }, { "cell_type": "markdown", "id": "7d0be831", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": null, "id": "2b314603", "metadata": { "collapsed": false }, "outputs": [], "source": [ "int npass=20;" ] }, { "cell_type": "code", "execution_count": null, "id": "56a50adf", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TDirectory::TContext ctx{nullptr}; // Don't register histograms to the current directory\n", "TCanvas *c1 = new TCanvas(\"FitBench\",\"Fitting Demo\",10,10,900,900);\n", "c1->Divide(2,2);\n", "c1->SetFillColor(kYellow-9);" ] }, { "cell_type": "markdown", "id": "7a3bc695", "metadata": {}, "source": [ "create a TF1 with the range from 0 to 3 and 6 parameters" ] }, { "cell_type": "code", "execution_count": null, "id": "a3f35bc2", "metadata": { "collapsed": false }, "outputs": [], "source": [ "fitFcn = new TF1(\"fitFcn\",fitFunction,0,3,6);\n", "fitFcn->SetNpx(200);\n", "gStyle->SetOptFit();\n", "gStyle->SetStatY(0.6);\n", "\n", "bool ok = true;" ] }, { "cell_type": "markdown", "id": "e590d1e1", "metadata": {}, "source": [ "with Minuit" ] }, { "cell_type": "code", "execution_count": null, "id": "e93ba478", "metadata": { "collapsed": false }, "outputs": [], "source": [ "c1->cd(1);\n", "ok &= DoFit(\"Minuit\",gPad,npass);" ] }, { "cell_type": "markdown", "id": "d5caf339", "metadata": {}, "source": [ "with Fumili" ] }, { "cell_type": "code", "execution_count": null, "id": "c2fe0127", "metadata": { "collapsed": false }, "outputs": [], "source": [ "c1->cd(2);\n", "ok &= DoFit(\"Fumili\",gPad,npass);" ] }, { "cell_type": "markdown", "id": "e397a8c2", "metadata": {}, "source": [ "with Minuit2" ] }, { "cell_type": "code", "execution_count": null, "id": "0bf53a77", "metadata": { "collapsed": false }, "outputs": [], "source": [ "c1->cd(3);\n", "ok &= DoFit(\"Minuit2\",gPad,npass);" ] }, { "cell_type": "markdown", "id": "dae024d9", "metadata": {}, "source": [ "with Fumili2" ] }, { "cell_type": "code", "execution_count": null, "id": "945e3716", "metadata": { "collapsed": false }, "outputs": [], "source": [ "c1->cd(4);\n", "ok &= DoFit(\"Fumili2\",gPad,npass);\n", "\n", "c1->SaveAs(\"FitBench.root\");\n", "return (ok) ? 0 : 1;" ] }, { "cell_type": "markdown", "id": "32431348", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": null, "id": "f964d3a0", "metadata": { "collapsed": false }, "outputs": [], "source": [ "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 }