{ "cells": [ { "cell_type": "markdown", "id": "9fe29f7d", "metadata": {}, "source": [ "# fitLinear2\n", "Fit a 5d hyperplane by n points, using the linear fitter directly\n", "\n", "This macro shows some features of the TLinearFitter class\n", "A 5-d hyperplane is fit, a constant term is assumed in the hyperplane\n", "equation `(y = a0 + a1*x0 + a2*x1 + a3*x2 + a4*x3 + a5*x4)`\n", "\n", "\n", "\n", "\n", "**Author:** Anna Kreshuk \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": "662af6c8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:54.246279Z", "iopub.status.busy": "2026-05-19T20:24:54.246158Z", "iopub.status.idle": "2026-05-19T20:24:54.559434Z", "shell.execute_reply": "2026-05-19T20:24:54.559009Z" } }, "outputs": [], "source": [ "int n=100;\n", "int i;\n", "TRandom randNum;\n", "TLinearFitter *lf=new TLinearFitter(5);" ] }, { "cell_type": "markdown", "id": "070c661b", "metadata": {}, "source": [ "The predefined \"hypN\" functions are the fastest to fit" ] }, { "cell_type": "code", "execution_count": 2, "id": "6794545c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:54.565281Z", "iopub.status.busy": "2026-05-19T20:24:54.565140Z", "iopub.status.idle": "2026-05-19T20:24:54.772026Z", "shell.execute_reply": "2026-05-19T20:24:54.771051Z" } }, "outputs": [], "source": [ "lf->SetFormula(\"hyp5\");\n", "\n", "double *x=new double[n*10*5];\n", "double *y=new double[n*10];\n", "double *e=new double[n*10];" ] }, { "cell_type": "markdown", "id": "f7d2b4c4", "metadata": {}, "source": [ "Create the points and put them into the fitter" ] }, { "cell_type": "code", "execution_count": 3, "id": "0ffb2426", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:54.773496Z", "iopub.status.busy": "2026-05-19T20:24:54.773373Z", "iopub.status.idle": "2026-05-19T20:24:54.976668Z", "shell.execute_reply": "2026-05-19T20:24:54.975890Z" } }, "outputs": [], "source": [ "for (i=0; iAssignData(n, 5, x, y, e);" ] }, { "cell_type": "markdown", "id": "482fe36e", "metadata": {}, "source": [ "A different way to put the points into the fitter would be to use\n", "the AddPoint function for each point. This way the points are copied and stored\n", "inside the fitter" ] }, { "cell_type": "markdown", "id": "646bfb98", "metadata": {}, "source": [ "Perform the fitting and look at the results" ] }, { "cell_type": "code", "execution_count": 5, "id": "e5a7a250", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:55.189314Z", "iopub.status.busy": "2026-05-19T20:24:55.189195Z", "iopub.status.idle": "2026-05-19T20:24:55.395522Z", "shell.execute_reply": "2026-05-19T20:24:55.394967Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "par[0]=0.000069+-0.001011\n", "par[1]=3.999934+-0.000164\n", "par[2]=0.999835+-0.000172\n", "par[3]=1.999892+-0.000178\n", "par[4]=2.999967+-0.000185\n", "par[5]=0.199823+-0.000174\n", "chisquare=70.148012\n" ] } ], "source": [ "lf->Eval();\n", "TVectorD params;\n", "TVectorD errors;\n", "lf->GetParameters(params);\n", "lf->GetErrors(errors);\n", "for (int i=0; i<6; i++)\n", " printf(\"par[%d]=%f+-%f\\n\", i, params(i), errors(i));\n", "double chisquare=lf->GetChisquare();\n", "printf(\"chisquare=%f\\n\", chisquare);" ] }, { "cell_type": "markdown", "id": "0dacf1bb", "metadata": {}, "source": [ "Now suppose you want to add some more points and see if the parameters will change" ] }, { "cell_type": "code", "execution_count": 6, "id": "167a375c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:55.397336Z", "iopub.status.busy": "2026-05-19T20:24:55.397202Z", "iopub.status.idle": "2026-05-19T20:24:55.616939Z", "shell.execute_reply": "2026-05-19T20:24:55.601779Z" } }, "outputs": [], "source": [ "for (i=n; iAssignData(n*2, 5, x, y, e);\n", "lf->Eval();\n", "lf->GetParameters(params);\n", "lf->GetErrors(errors);\n", "printf(\"\\nMore Points:\\n\");\n", "for (int i=0; i<6; i++)\n", " printf(\"par[%d]=%f+-%f\\n\", i, params(i), errors(i));\n", "chisquare=lf->GetChisquare();\n", "printf(\"chisquare=%.15f\\n\", chisquare);" ] }, { "cell_type": "markdown", "id": "0bd82ef7", "metadata": {}, "source": [ "Suppose, you are not satisfied with the result and want to try a different formula\n", "Without a constant:\n", "Since the AssignData() function was used, you don't have to add all points to the fitter again" ] }, { "cell_type": "code", "execution_count": 8, "id": "2de62377", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:55.850923Z", "iopub.status.busy": "2026-05-19T20:24:55.850801Z", "iopub.status.idle": "2026-05-19T20:24:56.077910Z", "shell.execute_reply": "2026-05-19T20:24:56.077227Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Without Constant\n", "par[0]=3.999913+-0.000121\n", "par[1]=0.999890+-0.000125\n", "par[2]=2.000057+-0.000123\n", "par[3]=2.999919+-0.000127\n", "par[4]=0.199918+-0.000130\n", "chisquare=145.649621\n" ] } ], "source": [ "lf->SetFormula(\"x0++x1++x2++x3++x4\");\n", "\n", "lf->Eval();\n", "lf->GetParameters(params);\n", "lf->GetErrors(errors);\n", "printf(\"\\nWithout Constant\\n\");\n", "for (int i=0; i<5; i++)\n", " printf(\"par[%d]=%f+-%f\\n\", i, params(i), errors(i));\n", "chisquare=lf->GetChisquare();\n", "printf(\"chisquare=%f\\n\", chisquare);" ] }, { "cell_type": "markdown", "id": "5ddfe2c7", "metadata": {}, "source": [ "Now suppose that you want to fix the value of one of the parameters\n", "Let's fix the first parameter at 4:" ] }, { "cell_type": "code", "execution_count": 9, "id": "ffd3317c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:56.079731Z", "iopub.status.busy": "2026-05-19T20:24:56.079580Z", "iopub.status.idle": "2026-05-19T20:24:56.286083Z", "shell.execute_reply": "2026-05-19T20:24:56.285530Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Fixed Constant:\n", "par[0]=0.000536+-0.000712\n", "par[1]=4.000000+-1.000000\n", "par[2]=0.999884+-0.000125\n", "par[3]=2.000070+-0.000123\n", "par[4]=2.999910+-0.000127\n", "par[5]=0.199920+-0.000130\n", "chisquare=145.602523231222932\n" ] } ], "source": [ "lf->SetFormula(\"hyp5\");\n", "lf->FixParameter(1, 4);\n", "lf->Eval();\n", "lf->GetParameters(params);\n", "lf->GetErrors(errors);\n", "printf(\"\\nFixed Constant:\\n\");\n", "for (i=0; i<6; i++)\n", " printf(\"par[%d]=%f+-%f\\n\", i, params(i), errors(i));\n", "chisquare=lf->GetChisquare();\n", "printf(\"chisquare=%.15f\\n\", chisquare);" ] }, { "cell_type": "markdown", "id": "4b2b04ed", "metadata": {}, "source": [ "The fixed parameters can then be released by the ReleaseParameter method" ] }, { "cell_type": "code", "execution_count": 10, "id": "4720faa8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:56.287805Z", "iopub.status.busy": "2026-05-19T20:24:56.287686Z", "iopub.status.idle": "2026-05-19T20:24:56.494011Z", "shell.execute_reply": "2026-05-19T20:24:56.493376Z" } }, "outputs": [], "source": [ "delete lf;" ] } ], "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 }