{ "cells": [ { "cell_type": "markdown", "id": "4e29b2f7", "metadata": {}, "source": [ "# exampleFit3D\n", "example of fitting a 3D function\n", "Typical multidimensional parametric regression where the predictor\n", "depends on 3 variables\n", "\n", "In the case of 1 or 2D one can use the TGraph classes\n", "but since no TGraph3D class exists this tutorial provide\n", "an example of fitting 3D points\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:24 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "fd94a60d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:39.018554Z", "iopub.status.busy": "2026-05-19T20:24:39.018329Z", "iopub.status.idle": "2026-05-19T20:24:39.445621Z", "shell.execute_reply": "2026-05-19T20:24:39.444529Z" } }, "outputs": [], "source": [ "const int n = 1000;\n", "double x[n], y[n], z[n], v[n];\n", "double ev = 0.1;" ] }, { "cell_type": "markdown", "id": "31aff649", "metadata": {}, "source": [ "generate the data" ] }, { "cell_type": "code", "execution_count": 2, "id": "fe32aca1", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:39.447296Z", "iopub.status.busy": "2026-05-19T20:24:39.447168Z", "iopub.status.idle": "2026-05-19T20:24:39.650482Z", "shell.execute_reply": "2026-05-19T20:24:39.648850Z" } }, "outputs": [], "source": [ "TRandom2 r;\n", "for (int i = 0; i < n; ++i) {\n", " x[i] = r.Uniform(0,10);\n", " y[i] = r.Uniform(0,10);\n", " z[i] = r.Uniform(0,10);\n", " v[i] = sin(x[i] ) + cos(y[i]) + z[i] + r.Gaus(0,ev);\n", "}" ] }, { "cell_type": "markdown", "id": "1b981346", "metadata": {}, "source": [ "create a 3d binned data structure" ] }, { "cell_type": "code", "execution_count": 3, "id": "a1127f26", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:39.652095Z", "iopub.status.busy": "2026-05-19T20:24:39.651977Z", "iopub.status.idle": "2026-05-19T20:24:39.861347Z", "shell.execute_reply": "2026-05-19T20:24:39.859459Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_54:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration\n", " ROOT::Fit::BinData data(n,3);\n", " ^\n" ] } ], "source": [ "ROOT::Fit::BinData data(n,3);\n", "double xx[3];\n", "for(int i = 0; i < n; ++i) {\n", " xx[0] = x[i];\n", " xx[1] = y[i];\n", " xx[2] = z[i];\n", " // add the 3d-data coordinate, the predictor value (v[i]) and its errors\n", " data.Add(xx, v[i], ev);\n", "}\n", "\n", "TF3 * f3 = new TF3(\"f3\",\"[0] * sin(x) + [1] * cos(y) + [2] * z\",0,10,0,10,0,10);\n", "f3->SetParameters(2,2,2);\n", "ROOT::Fit::Fitter fitter;" ] }, { "cell_type": "markdown", "id": "d81320aa", "metadata": {}, "source": [ "wrapped the TF1 in a IParamMultiFunction interface for the Fitter class" ] }, { "cell_type": "code", "execution_count": 4, "id": "78a9c636", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:39.873314Z", "iopub.status.busy": "2026-05-19T20:24:39.873181Z", "iopub.status.idle": "2026-05-19T20:24:40.087559Z", "shell.execute_reply": "2026-05-19T20:24:40.086439Z" } }, "outputs": [], "source": [ "ROOT::Math::WrappedMultiTF1 wf(*f3,3);\n", "fitter.SetFunction(wf);" ] }, { "cell_type": "code", "execution_count": 5, "id": "f29115e8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:40.089163Z", "iopub.status.busy": "2026-05-19T20:24:40.089039Z", "iopub.status.idle": "2026-05-19T20:24:40.302059Z", "shell.execute_reply": "2026-05-19T20:24:40.295507Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_59:2:24: error: reference to 'data' is ambiguous\n", " bool ret = fitter.Fit(data);\n", " ^\n", "input_line_54:2:21: note: candidate found by name lookup is 'data'\n", " ROOT::Fit::BinData data(n,3);\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:344:5: note: candidate found by name lookup is 'std::data'\n", " data(initializer_list<_Tp> __il) noexcept\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:312:5: note: candidate found by name lookup is 'std::data'\n", " data(_Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:323:5: note: candidate found by name lookup is 'std::data'\n", " data(const _Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:334:5: note: candidate found by name lookup is 'std::data'\n", " data(_Tp (&__array)[_Nm]) noexcept\n", " ^\n" ] } ], "source": [ "bool ret = fitter.Fit(data);\n", "if (ret) {\n", " const ROOT::Fit::FitResult & res = fitter.Result();\n", " // print result (should be around 1)\n", " res.Print(std::cout);\n", " // copy all fit result info (values, chi2, etc..) in TF3\n", " f3->SetFitResult(res);\n", " // test fit p-value (chi2 probability)\n", " double prob = res.Prob();\n", " if (prob < 1.E-2)\n", " Error(\"exampleFit3D\",\"Bad data fit - fit p-value is %f\",prob);\n", " else\n", " std::cout << \"Good fit : p-value = \" << prob << std::endl;\n", "\n", "}\n", "else\n", " Error(\"exampleFit3D\",\"3D fit failed\");" ] } ], "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 }