{ "cells": [ { "cell_type": "markdown", "id": "cd646c53", "metadata": {}, "source": [ "# fitMultiGraph\n", "fitting a parabola to a multigraph of 3 partly overlapping graphs\n", "with different errors\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": "markdown", "id": "b0f6fd05", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 1, "id": "55dac082", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:59.218224Z", "iopub.status.busy": "2026-05-19T20:24:59.218113Z", "iopub.status.idle": "2026-05-19T20:24:59.230688Z", "shell.execute_reply": "2026-05-19T20:24:59.229901Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void fitminuit()\n", "{\n", " int n = 30;\n", " double *xvalues1 = new double[n];\n", " double *xvalues2 = new double[n];\n", " double *xvalues3 = new double[n];\n", " double *yvalues1 = new double[n];\n", " double *yvalues2 = new double[n];\n", " double *yvalues3 = new double[n];\n", " double *evalues1 = new double[n];\n", " double *evalues2 = new double[n];\n", " double *evalues3 = new double[n];\n", " double *xtotal = new double[n*3];\n", " double *ytotal = new double[n*3];\n", " double *etotal = new double[n*3];\n", "\n", " TRandom r;\n", " int i;\n", " for (i=0; iAdd(gr1);\n", " mg->Add(gr2);\n", " mg->Add(gr3);\n", " //mg->Draw(\"ap\");\n", " //TF1 *ffit = new TF1(\"ffit\", \"TMath::Gaus(x, [0], [1], [2])\", -3, 3);\n", " //ffit->SetParameters(0, 1, 0);\n", " //mg->Fit(ffit);\n", "\n", " grtotal->Fit(\"gaus\");\n", " mg->Fit(\"gaus\");\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "id": "59292f8e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:59.232104Z", "iopub.status.busy": "2026-05-19T20:24:59.231978Z", "iopub.status.idle": "2026-05-19T20:24:59.554209Z", "shell.execute_reply": "2026-05-19T20:24:59.553760Z" } }, "outputs": [], "source": [ "int n = 30;\n", "double *xvalues1 = new double[n];\n", "double *xvalues2 = new double[n];\n", "double *xvalues3 = new double[n];\n", "double *yvalues1 = new double[n];\n", "double *yvalues2 = new double[n];\n", "double *yvalues3 = new double[n];\n", "double *evalues1 = new double[n];\n", "double *evalues2 = new double[n];\n", "double *evalues3 = new double[n];" ] }, { "cell_type": "markdown", "id": "33910cc8", "metadata": {}, "source": [ "generate the data for the graphs" ] }, { "cell_type": "code", "execution_count": 3, "id": "bec4ddbc", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:59.563521Z", "iopub.status.busy": "2026-05-19T20:24:59.563386Z", "iopub.status.idle": "2026-05-19T20:24:59.765803Z", "shell.execute_reply": "2026-05-19T20:24:59.765058Z" } }, "outputs": [], "source": [ "TRandom r;\n", "int i;\n", "for (i=0; iSetLineColor(kRed);\n", "gr2->SetLineColor(kBlue);\n", "gr2->SetMarkerStyle(24);\n", "gr2->SetMarkerSize(0.3);\n", "gr3->SetLineColor(kGreen);\n", "gr3->SetMarkerStyle(24);\n", "gr3->SetMarkerSize(0.3);" ] }, { "cell_type": "markdown", "id": "2f98d023", "metadata": {}, "source": [ "add the graphs to the multigraph" ] }, { "cell_type": "code", "execution_count": 5, "id": "44e13990", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:59.996413Z", "iopub.status.busy": "2026-05-19T20:24:59.996279Z", "iopub.status.idle": "2026-05-19T20:25:00.215167Z", "shell.execute_reply": "2026-05-19T20:25:00.211464Z" } }, "outputs": [], "source": [ "TMultiGraph *mg=new TMultiGraph(\"mg\",\n", " \"TMultiGraph of 3 TGraphErrors\");\n", "mg->Add(gr1);\n", "mg->Add(gr2);\n", "mg->Add(gr3);\n", "\n", "TCanvas *myc = new TCanvas(\"myc\",\n", " \"Fitting a MultiGraph of 3 TGraphErrors\");\n", "myc->SetGrid();\n", "\n", "mg->Draw(\"ap\");" ] }, { "cell_type": "markdown", "id": "6932caa5", "metadata": {}, "source": [ "fit" ] }, { "cell_type": "code", "execution_count": 6, "id": "7f0dc6c4", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:00.217281Z", "iopub.status.busy": "2026-05-19T20:25:00.217158Z", "iopub.status.idle": "2026-05-19T20:25:00.691675Z", "shell.execute_reply": "2026-05-19T20:25:00.691234Z" } }, "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", "Minimizer is Minuit2 / Migrad\n", "Chi2 = 95.1374\n", "NDf = 87\n", "Edm = 1.0713e-20\n", "NCalls = 55\n", "p0 = 3.21134 +/- 0.428055 \n", "p1 = 1.87952 +/- 0.248404 \n", "p2 = 1.00914 +/- 0.0349228 \n" ] } ], "source": [ "mg->Fit(\"pol2\", \"F\");" ] }, { "cell_type": "markdown", "id": "e98c4391", "metadata": {}, "source": [ "access to the fit function" ] }, { "cell_type": "code", "execution_count": 7, "id": "049ebd8c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:00.699181Z", "iopub.status.busy": "2026-05-19T20:25:00.699045Z", "iopub.status.idle": "2026-05-19T20:25:00.907096Z", "shell.execute_reply": "2026-05-19T20:25:00.906061Z" } }, "outputs": [], "source": [ "TF1 *fpol = mg->GetFunction(\"pol2\");\n", "fpol->SetLineWidth(1);" ] }, { "cell_type": "markdown", "id": "d9ccfd2e", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 8, "id": "5c1c09da", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:00.908569Z", "iopub.status.busy": "2026-05-19T20:25:00.908440Z", "iopub.status.idle": "2026-05-19T20:25:01.132991Z", "shell.execute_reply": "2026-05-19T20:25:01.128265Z" } }, "outputs": [ { "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 }