{ "cells": [ { "cell_type": "markdown", "id": "47f029e7", "metadata": {}, "source": [ "# minuit2GausFit\n", "Perform fits with different configurations using Minuit2\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": "markdown", "id": "60fcb5b1", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 1, "id": "d65d46fa", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:09.414475Z", "iopub.status.busy": "2026-05-19T20:25:09.414352Z", "iopub.status.idle": "2026-05-19T20:25:09.444767Z", "shell.execute_reply": "2026-05-19T20:25:09.443978Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "\n", "#include \"TH1.h\"\n", "#include \"TF1.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TRandom3.h\"\n", "#include \"TVirtualFitter.h\"\n", "#include \"TPaveLabel.h\"\n", "#include \"TStyle.h\"\n", "\n", "#include \n", "#include \n", "\n", "\n", "void testGausFit( std::string type = \"Minuit2\", int n = 1000) {\n", "\n", " gRandom = new TRandom3();\n", "\n", " TVirtualFitter::SetDefaultFitter(type.c_str() );\n", "\n", " std::string name;\n", " name = \"h1_\" + type;\n", " TH1D * h1 = new TH1D(name.c_str(),\"Chi2 Fit\",100, -5, 5. );\n", " name = \"h2_\" + type;\n", " TH1D * h2 = new TH1D(name.c_str(),\"Chi2 Fit with Minos Error\",100, -5, 5. );\n", " name = \"h3_\" + type;\n", " TH1D * h3 = new TH1D(name.c_str(),\"Chi2 Fit with Integral and Minos\",100, -5, 5. );\n", " name = \"h4_\" + type;\n", " TH1D * h4 = new TH1D(name.c_str(),\"Likelihood Fit with Minos Error\",100, -5, 5. );\n", "\n", " gStyle->SetOptStat(1111111);\n", " gStyle->SetOptFit(1111111);\n", "\n", " for (int i = 0; i < n; ++i) {\n", " double x = gRandom->Gaus(0,1);\n", " h1->Fill( x );\n", " h2->Fill( x );\n", " h3->Fill( x );\n", " h4->Fill( x );\n", " }\n", "\n", " std::string cname = type + \"Canvas\" ;\n", " std::string ctitle = type + \" Gaussian Fit\" ;\n", " TCanvas *c1 = new TCanvas(cname.c_str(),cname.c_str(),10,10,900,900);\n", " c1->Divide(2,2);\n", "\n", " c1->cd(1);\n", " std::cout << \"\\nDo Fit 1\\n\";\n", " h1->Fit(\"gaus\",\"Q\");\n", " h1->Draw();\n", " c1->cd(2);\n", " std::cout << \"\\nDo Fit 2\\n\";\n", " h2->Fit(\"gaus\",\"E\");\n", " h2->Draw();\n", " c1->cd(3);\n", " std::cout << \"\\nDo Fit 3\\n\";\n", " h3->Fit(\"gaus\",\"IGE\");\n", " h3->Draw();\n", " c1->cd(4);\n", " std::cout << \"\\nDo Fit 4\\n\";\n", " h4->Fit(\"gaus\",\"LE\");\n", " h4->Draw();\n", "\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "id": "258d02ba", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:09.445982Z", "iopub.status.busy": "2026-05-19T20:25:09.445868Z", "iopub.status.idle": "2026-05-19T20:25:10.447935Z", "shell.execute_reply": "2026-05-19T20:25:10.447258Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Do Fit 1\n", "\n", "Do Fit 2\n", "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "Chi2 = 65.1586\n", "NDf = 56\n", "Edm = 1.93774e-09\n", "NCalls = 69\n", "Constant = 36.3132 +/- 1.52625 -1.51651 +1.53547 (Minos) \n", "Mean = 0.013082 +/- 0.0347499 -0.0347674 +0.0347613 (Minos) \n", "Sigma = 1.03413 +/- 0.0288039 -0.0286274 +0.0290102 (Minos) \t (limited)\n", "\n", "Do Fit 3\n", "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "Chi2 = 65.1586\n", "NDf = 56\n", "Edm = 6.86315e-08\n", "NCalls = 57\n", "Constant = 36.327 +/- 2 -1.51685 +1.53726 (Minos) \n", "Mean = 0.0130817 +/- 2 \n", "Sigma = 1.03373 +/- 6.72116 \t (limited)\n", "\n", "Do Fit 4\n", "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "MinFCN = 43.3935\n", "Chi2 = 86.7869\n", "NDf = 97\n", "Edm = 9.97216e-08\n", "NCalls = 62\n", "Constant = 38.427 +/- 1.48837 -1.46667 +1.51031 (Minos) \n", "Mean = 0.027601 +/- 0.032831 -0.0328395 +0.0328395 (Minos) \n", "Sigma = 1.03819 +/- 0.0232194 -0.0227841 +0.0236699 (Minos) \t (limited)\n", "\n", "Do Fit 1\n", "\n", "Do Fit 2\n", "****************************************\n", "Minimizer is Minuit2 / Fumili\n", "Chi2 = 65.1586\n", "NDf = 56\n", "Edm = 4.17059e-08\n", "NCalls = 32\n", "Constant = 36.313 +/- 1.52625 -1.51631 +1.53567 (Minos) \n", "Mean = 0.0130818 +/- 0.0347499 -0.0347672 +0.0347615 (Minos) \n", "Sigma = 1.03413 +/- 0.0288039 -0.0286324 +0.0290052 (Minos) \t (limited)\n", "\n", "Do Fit 3\n", "****************************************\n", "Minimizer is Minuit2 / Fumili\n", "Chi2 = 65.1586\n", "NDf = 56\n", "Edm = 9.83435e-08\n", "NCalls = 32\n", "Constant = 36.327 +/- 1.52734 -1.51727 +1.5369 (Minos) \n", "Mean = 0.0130818 +/- 0.0347498 -0.0347672 +0.0347615 (Minos) \n", "Sigma = 1.03373 +/- 0.0288152 -0.028647 +0.0290131 (Minos) \t (limited)\n", "\n", "Do Fit 4\n", "****************************************\n", "Minimizer is Minuit2 / Fumili\n", "MinFCN = 43.3935\n", "Chi2 = 86.7869\n", "NDf = 97\n", "Edm = 1.39892e-09\n", "NCalls = 32\n", "Constant = 38.4264 +/- 1.48835 -1.46601 +1.51097 (Minos) \n", "Mean = 0.027601 +/- 0.0328313 -0.0328395 +0.0328395 (Minos) \n", "Sigma = 1.0382 +/- 0.0232198 -0.0227933 +0.0236607 (Minos) \t (limited)\n" ] } ], "source": [ "int n = 1000;\n", "testGausFit(\"Minuit2\",n);\n", "testGausFit(\"Fumili2\",n);" ] }, { "cell_type": "markdown", "id": "615eef1f", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 3, "id": "cb66425c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:10.449342Z", "iopub.status.busy": "2026-05-19T20:25:10.449224Z", "iopub.status.idle": "2026-05-19T20:25:10.670804Z", "shell.execute_reply": "2026-05-19T20:25:10.670283Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "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 }