{ "cells": [ { "cell_type": "markdown", "id": "d5edd1d4", "metadata": {}, "source": [ "# minuit2FitBench2D\n", "Minuit2 fit 2D benchmark.\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": "91408864", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"TH1.h\"\n", "#include \"TF1.h\"\n", "#include \"TH2D.h\"\n", "#include \"TF2.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TStopwatch.h\"\n", "#include \"TSystem.h\"\n", "#include \"TRandom3.h\"\n", "#include \"TVirtualFitter.h\"\n", "#include \"TPaveLabel.h\"\n", "#include \"TStyle.h\"\n", "\n", "\n", "TF2 *fitFcn;\n", "TH2D *histo;" ] }, { "cell_type": "markdown", "id": "4117ad1f", "metadata": {}, "source": [ " Quadratic background function\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "6875ab93", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "double gaus2D(double *x, double *par) {\n", " double t1 = x[0] - par[1];\n", " double t2 = x[1] - par[2];\n", " return par[0]* exp( - 0.5 * ( t1*t1/( par[3]*par[3]) + t2*t2 /( par[4]*par[4] ) ) ) ;\n", "}" ] }, { "cell_type": "markdown", "id": "8b4fcc67", "metadata": {}, "source": [ " Sum of background and peak function\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "a2cd54be", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "double fitFunction(double *x, double *par) {\n", " return gaus2D(x,par);\n", "}" ] }, { "cell_type": "markdown", "id": "5b196ab9", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": null, "id": "58f2565d", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "void fillHisto(int n =10000) {\n", "\n", " gRandom = new TRandom3();\n", " for (int i = 0; i < n; ++i) {\n", " double x = gRandom->Gaus(2,3);\n", " double y = gRandom->Gaus(-1,4);\n", " histo->Fill(x,y,1.);\n", " }\n", "}" ] }, { "cell_type": "markdown", "id": "fd9ee752", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": null, "id": "08cfe659", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "void DoFit(const char* fitter, TVirtualPad *pad, int npass) {\n", " TStopwatch timer;\n", " TVirtualFitter::SetDefaultFitter(fitter);\n", " pad->SetGrid();\n", " fitFcn->SetParameters(100,0,0,2,7);\n", " fitFcn->Update();\n", "\n", " timer.Start();\n", " histo->Fit(\"fitFcn\",\"0\");\n", " timer.Stop();\n", "\n", " histo->Draw();\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.5,0.7,0.85,0.8,Form(\"%s CPU= %g s\",fitter,cputime),\"brNDC\");\n", " p->Draw();\n", " pad->Update();\n", "}" ] }, { "cell_type": "markdown", "id": "9c101f7b", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": null, "id": "8a2fe12c", "metadata": { "collapsed": false }, "outputs": [], "source": [ "int n = 100000;" ] }, { "cell_type": "code", "execution_count": null, "id": "dca48391", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TDirectory::TContext ctx{nullptr}; // Don't register histograms to the current directory\n", "TCanvas *c1 = new TCanvas(\"c1\",\"Fitting Demo\",10,10,900,900);\n", "c1->Divide(2,2);" ] }, { "cell_type": "markdown", "id": "5c6f66ca", "metadata": {}, "source": [ "create a TF1 with the range from 0 to 3 and 6 parameters" ] }, { "cell_type": "code", "execution_count": null, "id": "4ee02352", "metadata": { "collapsed": false }, "outputs": [], "source": [ "fitFcn = new TF2(\"fitFcn\",fitFunction,-10,10,-10,10,5);" ] }, { "cell_type": "markdown", "id": "ddbc3697", "metadata": {}, "source": [ "fitFcn->SetNpx(200);" ] }, { "cell_type": "code", "execution_count": null, "id": "6dd5d0d8", "metadata": { "collapsed": false }, "outputs": [], "source": [ "gStyle->SetOptFit();\n", "gStyle->SetStatY(0.6);\n", "\n", "\n", "histo = new TH2D(\"h2\",\"2D Gauss\",100,-10,10,100,-10,10);\n", "fillHisto(n);\n", "\n", "int npass=0;" ] }, { "cell_type": "markdown", "id": "225e6aad", "metadata": {}, "source": [ "with Minuit" ] }, { "cell_type": "code", "execution_count": null, "id": "26810744", "metadata": { "collapsed": false }, "outputs": [], "source": [ "c1->cd(1);\n", "DoFit(\"Minuit\",gPad,npass);" ] }, { "cell_type": "markdown", "id": "04cc9c14", "metadata": {}, "source": [ "with Fumili" ] }, { "cell_type": "code", "execution_count": null, "id": "97bacab8", "metadata": { "collapsed": false }, "outputs": [], "source": [ "c1->cd(2);\n", " DoFit(\"Fumili\",gPad,npass);" ] }, { "cell_type": "markdown", "id": "074d76ce", "metadata": {}, "source": [ "with Minuit2" ] }, { "cell_type": "code", "execution_count": null, "id": "779385ee", "metadata": { "collapsed": false }, "outputs": [], "source": [ "c1->cd(3);\n", "DoFit(\"Minuit2\",gPad,npass);" ] }, { "cell_type": "markdown", "id": "8d599f9e", "metadata": {}, "source": [ "with Fumili2" ] }, { "cell_type": "code", "execution_count": null, "id": "7fd0a1ac", "metadata": { "collapsed": false }, "outputs": [], "source": [ "c1->cd(4);\n", "DoFit(\"Fumili2\",gPad,npass);" ] }, { "cell_type": "markdown", "id": "d99eb2bd", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": null, "id": "7bb6382e", "metadata": { "collapsed": false }, "outputs": [], "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 }