{ "cells": [ { "cell_type": "markdown", "id": "86450154", "metadata": {}, "source": [ "# ConfidenceIntervals\n", "Illustrates TVirtualFitter::GetConfidenceIntervals\n", "This method computes confidence intervals for the fitted function\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:24 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "6ae4a437", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:33.741255Z", "iopub.status.busy": "2026-05-19T20:24:33.741139Z", "iopub.status.idle": "2026-05-19T20:24:34.086853Z", "shell.execute_reply": "2026-05-19T20:24:34.086255Z" } }, "outputs": [], "source": [ "TCanvas *myc = new TCanvas(\"myc\",\n", " \"Confidence intervals on the fitted function\",1000, 500);\n", "myc->Divide(3,1);" ] }, { "cell_type": "markdown", "id": "9748fedc", "metadata": {}, "source": [ "### 1. A graph\n", "Create and fill a graph" ] }, { "cell_type": "code", "execution_count": 2, "id": "c3264d5b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:34.088828Z", "iopub.status.busy": "2026-05-19T20:24:34.088703Z", "iopub.status.idle": "2026-05-19T20:24:34.290855Z", "shell.execute_reply": "2026-05-19T20:24:34.290298Z" } }, "outputs": [], "source": [ "int ngr = 100;\n", "TGraph *gr = new TGraph(ngr);\n", "gr->SetName(\"GraphNoError\");\n", "double x, y;\n", "int i;\n", "for (i=0; iUniform(-1, 1);\n", " y = -1 + 2*x + gRandom->Gaus(0, 1);\n", " gr->SetPoint(i, x, y);\n", "}" ] }, { "cell_type": "markdown", "id": "14770aed", "metadata": {}, "source": [ "Create the fitting function" ] }, { "cell_type": "code", "execution_count": 3, "id": "f63d09c8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:34.292537Z", "iopub.status.busy": "2026-05-19T20:24:34.292417Z", "iopub.status.idle": "2026-05-19T20:24:34.496905Z", "shell.execute_reply": "2026-05-19T20:24:34.496364Z" } }, "outputs": [], "source": [ "TF1 *fpol = new TF1(\"fpol\", \"pol1\", -1, 1);\n", "fpol->SetLineWidth(2);\n", "gr->Fit(fpol, \"Q\");\n", "\n", "/*Create a TGraphErrors to hold the confidence intervals*/\n", "TGraphErrors *grint = new TGraphErrors(ngr);\n", "grint->SetTitle(\"Fitted line with .95 conf. band\");\n", "for (i=0; iSetPoint(i, gr->GetX()[i], 0);\n", "/*Compute the confidence intervals at the x points of the created graph*/\n", "(TVirtualFitter::GetFitter())->GetConfidenceIntervals(grint);" ] }, { "cell_type": "markdown", "id": "9cb88af7", "metadata": {}, "source": [ "Now the \"grint\" graph contains function values as its y-coordinates\n", "and confidence intervals as the errors on these coordinates\n", "Draw the graph, the function and the confidence intervals" ] }, { "cell_type": "code", "execution_count": 4, "id": "4167360e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:34.502537Z", "iopub.status.busy": "2026-05-19T20:24:34.502409Z", "iopub.status.idle": "2026-05-19T20:24:34.716167Z", "shell.execute_reply": "2026-05-19T20:24:34.715706Z" } }, "outputs": [], "source": [ "myc->cd(1);\n", "grint->SetLineColor(kRed);\n", "grint->Draw(\"ap\");\n", "gr->SetMarkerStyle(5);\n", "gr->SetMarkerSize(0.7);\n", "gr->Draw(\"psame\");" ] }, { "cell_type": "markdown", "id": "a7f63030", "metadata": {}, "source": [ "### 2. A histogram" ] }, { "cell_type": "code", "execution_count": 5, "id": "4c376502", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:34.719232Z", "iopub.status.busy": "2026-05-19T20:24:34.719121Z", "iopub.status.idle": "2026-05-19T20:24:34.925136Z", "shell.execute_reply": "2026-05-19T20:24:34.924691Z" } }, "outputs": [], "source": [ "myc->cd(2);" ] }, { "cell_type": "markdown", "id": "87315b7f", "metadata": {}, "source": [ "Create, fill and fit a histogram" ] }, { "cell_type": "code", "execution_count": 6, "id": "50b09ef4", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:34.926619Z", "iopub.status.busy": "2026-05-19T20:24:34.926491Z", "iopub.status.idle": "2026-05-19T20:24:35.246179Z", "shell.execute_reply": "2026-05-19T20:24:35.245591Z" } }, "outputs": [], "source": [ "int nh=5000;\n", "TH1D *h = new TH1D(\"h\",\n", " \"Fitted Gaussian with .95 conf.band\", 100, -3, 3);\n", "h->FillRandom(\"gaus\", nh);\n", "TF1 *f = new TF1(\"fgaus\", \"gaus\", -3, 3);\n", "f->SetLineWidth(2);\n", "h->Fit(f, \"Q\");\n", "h->Draw();\n", "\n", "/*Create a histogram to hold the confidence intervals*/\n", "TH1D *hint = new TH1D(\"hint\",\n", " \"Fitted Gaussian with .95 conf.band\", 100, -3, 3);\n", "(TVirtualFitter::GetFitter())->GetConfidenceIntervals(hint);" ] }, { "cell_type": "markdown", "id": "0411695d", "metadata": {}, "source": [ "Now the \"hint\" histogram has the fitted function values as the\n", "bin contents and the confidence intervals as bin errors" ] }, { "cell_type": "code", "execution_count": 7, "id": "b0823e1c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:35.249078Z", "iopub.status.busy": "2026-05-19T20:24:35.248955Z", "iopub.status.idle": "2026-05-19T20:24:35.454855Z", "shell.execute_reply": "2026-05-19T20:24:35.454398Z" } }, "outputs": [], "source": [ "hint->SetStats(false);\n", "hint->SetFillColor(2);\n", "hint->Draw(\"e3 same\");" ] }, { "cell_type": "markdown", "id": "55400c13", "metadata": {}, "source": [ "### 3. A 2d graph\n", "Create and fill the graph" ] }, { "cell_type": "code", "execution_count": 8, "id": "94cee42b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:35.464496Z", "iopub.status.busy": "2026-05-19T20:24:35.464365Z", "iopub.status.idle": "2026-05-19T20:24:35.692581Z", "shell.execute_reply": "2026-05-19T20:24:35.692065Z" } }, "outputs": [], "source": [ "int ngr2 = 100;\n", "double z, rnd, e=0.3;\n", "TGraph2D *gr2 = new TGraph2D(ngr2);\n", "gr2->SetName(\"Graph2DNoError\");\n", "TF2 *f2 = new TF2(\"f2\",\n", " \"1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+250\",-6,6,-6,6);\n", "f2->SetParameters(1,1);\n", "for (i=0; iGetRandom2(x,y);\n", " // Generate a random number in [-e,e]\n", " rnd = 2*gRandom->Rndm()*e-e;\n", " z = f2->Eval(x,y)*(1+rnd);\n", " gr2->SetPoint(i,x,y,z);\n", "}" ] }, { "cell_type": "markdown", "id": "979d7beb", "metadata": {}, "source": [ "Create a graph with errors to store the intervals" ] }, { "cell_type": "code", "execution_count": 9, "id": "a98f10f2", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:35.708369Z", "iopub.status.busy": "2026-05-19T20:24:35.708235Z", "iopub.status.idle": "2026-05-19T20:24:35.914512Z", "shell.execute_reply": "2026-05-19T20:24:35.914067Z" } }, "outputs": [], "source": [ "TGraph2DErrors *grint2 = new TGraph2DErrors(ngr2);\n", "for (i=0; iSetPoint(i, gr2->GetX()[i], gr2->GetY()[i], 0);" ] }, { "cell_type": "markdown", "id": "94628e4e", "metadata": {}, "source": [ "Fit the graph" ] }, { "cell_type": "code", "execution_count": 10, "id": "ca71a266", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:35.920160Z", "iopub.status.busy": "2026-05-19T20:24:35.920027Z", "iopub.status.idle": "2026-05-19T20:24:36.127566Z", "shell.execute_reply": "2026-05-19T20:24:36.127032Z" } }, "outputs": [], "source": [ "f2->SetParameters(0.5,1.5);\n", "gr2->Fit(f2, \"Q\");\n", "/*Compute the confidence intervals*/\n", "(TVirtualFitter::GetFitter())->GetConfidenceIntervals(grint2);" ] }, { "cell_type": "markdown", "id": "8733f268", "metadata": {}, "source": [ "Now the \"grint2\" graph contains function values as z-coordinates\n", "and confidence intervals as their errors\n", "draw" ] }, { "cell_type": "code", "execution_count": 11, "id": "12908e2a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:36.129220Z", "iopub.status.busy": "2026-05-19T20:24:36.129103Z", "iopub.status.idle": "2026-05-19T20:24:36.335338Z", "shell.execute_reply": "2026-05-19T20:24:36.334828Z" } }, "outputs": [], "source": [ "myc->cd(3);\n", "f2->SetNpx(30);\n", "f2->SetNpy(30);\n", "f2->SetFillColor(kBlue);\n", "f2->Draw(\"surf4\");\n", "grint2->SetNpx(20);\n", "grint2->SetNpy(20);\n", "grint2->SetMarkerStyle(24);\n", "grint2->SetMarkerSize(0.7);\n", "grint2->SetMarkerColor(kRed);\n", "grint2->SetLineColor(kRed);\n", "grint2->Draw(\"E0 same\");\n", "grint2->SetTitle(\"Fitted 2d function with .95 error bars\");\n", "\n", "myc->cd();" ] }, { "cell_type": "markdown", "id": "0a67a4d8", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 12, "id": "e67dcad1", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:36.337278Z", "iopub.status.busy": "2026-05-19T20:24:36.337157Z", "iopub.status.idle": "2026-05-19T20:24:36.661947Z", "shell.execute_reply": "2026-05-19T20:24:36.661037Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "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 }