{ "cells": [ { "cell_type": "markdown", "id": "8f6d1e13", "metadata": {}, "source": [ "# Ifit\n", " Example of a program to fit non-equidistant data points\n", "\n", " The fitting function fcn is a simple chisquare function\n", " The data consists of 5 data points (arrays x,y,z) + the errors in errorsz\n", " More details on the various functions or parameters for these functions\n", " can be obtained in an interactive ROOT session with:\n", "\n", "```cpp\n", " Root > TMinuit *minuit = new TMinuit(10);\n", "```\n", "\n", "```cpp\n", " Root > minuit->mnhelp(\"*\") to see the list of possible keywords\n", " Root > minuit->mnhelp(\"SET\") explains most parameters\n", "```\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:25 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "454535ce", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:04.368352Z", "iopub.status.busy": "2026-05-19T20:25:04.368220Z", "iopub.status.idle": "2026-05-19T20:25:04.380496Z", "shell.execute_reply": "2026-05-19T20:25:04.379866Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"TMinuit.h\"\n", "\n", "float z[5],x[5],y[5],errorz[5];" ] }, { "cell_type": "markdown", "id": "8bd67175", "metadata": {}, "source": [ " ______________________________________________________________________________\n", " " ] }, { "cell_type": "code", "execution_count": 2, "id": "fa70f0d0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:04.381763Z", "iopub.status.busy": "2026-05-19T20:25:04.381643Z", "iopub.status.idle": "2026-05-19T20:25:04.384779Z", "shell.execute_reply": "2026-05-19T20:25:04.384131Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "double func(float x,float y,double *par)\n", "{\n", " double value=( (par[0]*par[0])/(x*x)-1)/ ( par[1]+par[2]*y-par[3]*y*y);\n", " return value;\n", "}" ] }, { "cell_type": "markdown", "id": "def06f0c", "metadata": {}, "source": [ " ______________________________________________________________________________\n", " " ] }, { "cell_type": "code", "execution_count": 3, "id": "9f5eb273", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:04.386009Z", "iopub.status.busy": "2026-05-19T20:25:04.385895Z", "iopub.status.idle": "2026-05-19T20:25:04.389514Z", "shell.execute_reply": "2026-05-19T20:25:04.388889Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void fcn(int &npar, double *gin, double &f, double *par, int iflag)\n", "{\n", " const int nbins = 5;\n", " int i;\n", "\n", "//calculate chisquare\n", " double chisq = 0;\n", " double delta;\n", " for (i=0;iSetFCN(fcn);\n", "\n", "double arglist[10];\n", "int ierflg = 0;\n", "\n", "arglist[0] = 1;\n", "gMinuit->mnexcm(\"SET ERR\", arglist ,1,ierflg);" ] }, { "cell_type": "markdown", "id": "d62475da", "metadata": {}, "source": [ "Set starting values and step sizes for parameters" ] }, { "cell_type": "code", "execution_count": 8, "id": "58c839df", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:05.398571Z", "iopub.status.busy": "2026-05-19T20:25:05.398456Z", "iopub.status.idle": "2026-05-19T20:25:05.607190Z", "shell.execute_reply": "2026-05-19T20:25:05.606443Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_59:4:1: error: reference to 'gMinuit' is ambiguous\n", "gMinuit->mnparm(0, \"a1\", vstart[0], step[0], 0,0,ierflg);\n", "^\n", "input_line_58:8:10: note: candidate found by name lookup is '__cling_N522::gMinuit'\n", "TMinuit *gMinuit = new TMinuit(5); //initialize TMinuit with a maximum of 5 params\n", " ^\n", "/github/home/ROOT-CI/build/include/TMinuit.h:271:21: note: candidate found by name lookup is 'gMinuit'\n", "R__EXTERN TMinuit *gMinuit;\n", " ^\n", "input_line_59:5:1: error: reference to 'gMinuit' is ambiguous\n", "gMinuit->mnparm(1, \"a2\", vstart[1], step[1], 0,0,ierflg);\n", "^\n", "input_line_58:8:10: note: candidate found by name lookup is '__cling_N522::gMinuit'\n", "TMinuit *gMinuit = new TMinuit(5); //initialize TMinuit with a maximum of 5 params\n", " ^\n", "/github/home/ROOT-CI/build/include/TMinuit.h:271:21: note: candidate found by name lookup is 'gMinuit'\n", "R__EXTERN TMinuit *gMinuit;\n", " ^\n", "input_line_59:6:1: error: reference to 'gMinuit' is ambiguous\n", "gMinuit->mnparm(2, \"a3\", vstart[2], step[2], 0,0,ierflg);\n", "^\n", "input_line_58:8:10: note: candidate found by name lookup is '__cling_N522::gMinuit'\n", "TMinuit *gMinuit = new TMinuit(5); //initialize TMinuit with a maximum of 5 params\n", " ^\n", "/github/home/ROOT-CI/build/include/TMinuit.h:271:21: note: candidate found by name lookup is 'gMinuit'\n", "R__EXTERN TMinuit *gMinuit;\n", " ^\n", "input_line_59:7:1: error: reference to 'gMinuit' is ambiguous\n", "gMinuit->mnparm(3, \"a4\", vstart[3], step[3], 0,0,ierflg);\n", "^\n", "input_line_58:8:10: note: candidate found by name lookup is '__cling_N522::gMinuit'\n", "TMinuit *gMinuit = new TMinuit(5); //initialize TMinuit with a maximum of 5 params\n", " ^\n", "/github/home/ROOT-CI/build/include/TMinuit.h:271:21: note: candidate found by name lookup is 'gMinuit'\n", "R__EXTERN TMinuit *gMinuit;\n", " ^\n" ] } ], "source": [ "static double vstart[4] = {3, 1 , 0.1 , 0.01};\n", "static double step[4] = {0.1 , 0.1 , 0.01 , 0.001};\n", "gMinuit->mnparm(0, \"a1\", vstart[0], step[0], 0,0,ierflg);\n", "gMinuit->mnparm(1, \"a2\", vstart[1], step[1], 0,0,ierflg);\n", "gMinuit->mnparm(2, \"a3\", vstart[2], step[2], 0,0,ierflg);\n", "gMinuit->mnparm(3, \"a4\", vstart[3], step[3], 0,0,ierflg);" ] }, { "cell_type": "markdown", "id": "7920f21e", "metadata": {}, "source": [ "Now ready for minimization step" ] }, { "cell_type": "code", "execution_count": 9, "id": "ac7c7979", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:05.608516Z", "iopub.status.busy": "2026-05-19T20:25:05.608397Z", "iopub.status.idle": "2026-05-19T20:25:05.816271Z", "shell.execute_reply": "2026-05-19T20:25:05.815473Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_60:4:1: error: reference to 'gMinuit' is ambiguous\n", "gMinuit->mnexcm(\"MIGRAD\", arglist ,2,ierflg);\n", "^\n", "input_line_58:8:10: note: candidate found by name lookup is '__cling_N522::gMinuit'\n", "TMinuit *gMinuit = new TMinuit(5); //initialize TMinuit with a maximum of 5 params\n", " ^\n", "/github/home/ROOT-CI/build/include/TMinuit.h:271:21: note: candidate found by name lookup is 'gMinuit'\n", "R__EXTERN TMinuit *gMinuit;\n", " ^\n" ] } ], "source": [ "arglist[0] = 500;\n", "arglist[1] = 1.;\n", "gMinuit->mnexcm(\"MIGRAD\", arglist ,2,ierflg);" ] }, { "cell_type": "markdown", "id": "83e131db", "metadata": {}, "source": [ "Print results" ] }, { "cell_type": "code", "execution_count": 10, "id": "cda17669", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:05.817566Z", "iopub.status.busy": "2026-05-19T20:25:05.817452Z", "iopub.status.idle": "2026-05-19T20:25:06.025203Z", "shell.execute_reply": "2026-05-19T20:25:06.024423Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_61:4:1: error: reference to 'gMinuit' is ambiguous\n", "gMinuit->mnstat(amin,edm,errdef,nvpar,nparx,icstat);\n", "^\n", "input_line_58:8:10: note: candidate found by name lookup is '__cling_N522::gMinuit'\n", "TMinuit *gMinuit = new TMinuit(5); //initialize TMinuit with a maximum of 5 params\n", " ^\n", "/github/home/ROOT-CI/build/include/TMinuit.h:271:21: note: candidate found by name lookup is 'gMinuit'\n", "R__EXTERN TMinuit *gMinuit;\n", " ^\n" ] } ], "source": [ "double amin,edm,errdef;\n", "int nvpar,nparx,icstat;\n", "gMinuit->mnstat(amin,edm,errdef,nvpar,nparx,icstat);" ] }, { "cell_type": "markdown", "id": "9a6300df", "metadata": {}, "source": [ "gMinuit->mnprin(3,amin);" ] } ], "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 }