{ "cells": [ { "cell_type": "markdown", "id": "4dc97edf", "metadata": {}, "source": [ "# chi2test\n", "Example to use chi2 test for comparing two histograms.\n", "\n", "One unweighted histogram is compared with a weighted histogram.\n", "The normalized residuals are retrieved and plotted in a simple graph.\n", "The QQ plot of the normalized residual using the\n", "normal distribution is also plotted.\n", "\n", "\n", "\n", "\n", "**Author:** Nikolai Gagunashvili, Daniel Haertl, 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": "markdown", "id": "e3f6a1b3", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": 1, "id": "ebd24aab", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:23.159853Z", "iopub.status.busy": "2026-05-19T20:24:23.159728Z", "iopub.status.idle": "2026-05-19T20:24:23.479153Z", "shell.execute_reply": "2026-05-19T20:24:23.478658Z" } }, "outputs": [], "source": [ "Float_t w=0;" ] }, { "cell_type": "markdown", "id": "f87191af", "metadata": {}, "source": [ "Note: The parameter w is used to produce the 2 pictures in\n", "the TH1::Chi2Test method. The 1st picture is produced with\n", "w=0 and the 2nd with w=17 (see TH1::Chi2Test() help)." ] }, { "cell_type": "markdown", "id": "73feabe7", "metadata": {}, "source": [ "Define Histograms." ] }, { "cell_type": "code", "execution_count": 2, "id": "6fa0115e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:23.481187Z", "iopub.status.busy": "2026-05-19T20:24:23.481075Z", "iopub.status.idle": "2026-05-19T20:24:23.688279Z", "shell.execute_reply": "2026-05-19T20:24:23.687638Z" } }, "outputs": [], "source": [ "const Int_t n = 20;\n", "\n", "TH1D *h1 = new TH1D(\"h1\", \"h1\", n, 4, 16);\n", "TH1D *h2 = new TH1D(\"h2\", \"h2\", n, 4, 16);\n", "\n", "h1->SetTitle(\"Unweighted Histogram\");\n", "h2->SetTitle(\"Weighted Histogram\");\n", "\n", "h1->SetBinContent(1, 0);\n", "h1->SetBinContent(2, 1);\n", "h1->SetBinContent(3, 0);\n", "h1->SetBinContent(4, 1);\n", "h1->SetBinContent(5, 1);\n", "h1->SetBinContent(6, 6);\n", "h1->SetBinContent(7, 7);\n", "h1->SetBinContent(8, 2);\n", "h1->SetBinContent(9, 22);\n", "h1->SetBinContent(10, 30);\n", "h1->SetBinContent(11, 27);\n", "h1->SetBinContent(12, 20);\n", "h1->SetBinContent(13, 13);\n", "h1->SetBinContent(14, 9);\n", "h1->SetBinContent(15, 9 + w);\n", "h1->SetBinContent(16, 13);\n", "h1->SetBinContent(17, 19);\n", "h1->SetBinContent(18, 11);\n", "h1->SetBinContent(19, 9);\n", "h1->SetBinContent(20, 0);\n", "\n", "h2->SetBinContent(1, 2.20173025 );\n", "h2->SetBinContent(2, 3.30143857);\n", "h2->SetBinContent(3, 2.5892849);\n", "h2->SetBinContent(4, 2.99990201);\n", "h2->SetBinContent(5, 4.92877054);\n", "h2->SetBinContent(6, 8.33036995);\n", "h2->SetBinContent(7, 6.95084763);\n", "h2->SetBinContent(8, 15.206357);\n", "h2->SetBinContent(9, 23.9236012);\n", "h2->SetBinContent(10, 44.3848114);\n", "h2->SetBinContent(11, 49.4465599);\n", "h2->SetBinContent(12, 25.1868858);\n", "h2->SetBinContent(13, 16.3129692);\n", "h2->SetBinContent(14, 13.0289612);\n", "h2->SetBinContent(15, 16.7857609);\n", "h2->SetBinContent(16, 22.9914703);\n", "h2->SetBinContent(17, 30.5279255);\n", "h2->SetBinContent(18, 12.5252123);\n", "h2->SetBinContent(19, 16.4104557);\n", "h2->SetBinContent(20, 7.86067867);\n", "h2->SetBinError(1, 0.38974303 );\n", "h2->SetBinError(2, 0.536510944);\n", "h2->SetBinError(3, 0.529702604);\n", "h2->SetBinError(4, 0.642001867);\n", "h2->SetBinError(5, 0.969341516);\n", "h2->SetBinError(6, 1.47611344);\n", "h2->SetBinError(7, 1.69797957);\n", "h2->SetBinError(8, 3.28577447);\n", "h2->SetBinError(9, 5.40784931);\n", "h2->SetBinError(10, 9.10106468);\n", "h2->SetBinError(11, 9.73541737);\n", "h2->SetBinError(12, 5.55019951);\n", "h2->SetBinError(13, 3.57914758);\n", "h2->SetBinError(14, 2.77877331);\n", "h2->SetBinError(15, 3.23697519);\n", "h2->SetBinError(16, 4.3608489);\n", "h2->SetBinError(17, 5.77172089);\n", "h2->SetBinError(18, 3.38666105);\n", "h2->SetBinError(19, 2.98861837);\n", "h2->SetBinError(20, 1.58402085);\n", "\n", "h1->SetEntries(217);\n", "h2->SetEntries(500);" ] }, { "cell_type": "markdown", "id": "ebfc4ec3", "metadata": {}, "source": [ "apply the chi2 test and retrieve the residuals" ] }, { "cell_type": "code", "execution_count": 3, "id": "8661f853", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:23.690070Z", "iopub.status.busy": "2026-05-19T20:24:23.689943Z", "iopub.status.idle": "2026-05-19T20:24:23.894451Z", "shell.execute_reply": "2026-05-19T20:24:23.894157Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Chi2 = 21.085124, Prob = 0.332116, NDF = 19, igood = 1\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Info in : There is a bin in h1 with less than 1 event.\n", "\n" ] } ], "source": [ "Double_t res[n], x[20];\n", "h1->Chi2Test(h2,\"UW P\",res);" ] }, { "cell_type": "markdown", "id": "45fdb8bf", "metadata": {}, "source": [ "Graph for Residuals" ] }, { "cell_type": "code", "execution_count": 4, "id": "1de448e4", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:23.896419Z", "iopub.status.busy": "2026-05-19T20:24:23.896302Z", "iopub.status.idle": "2026-05-19T20:24:24.112844Z", "shell.execute_reply": "2026-05-19T20:24:24.112413Z" } }, "outputs": [], "source": [ "for (Int_t i=0; iGetXaxis()->SetRangeUser(4,16);\n", "resgr->GetYaxis()->SetRangeUser(-3.5,3.5);\n", "resgr->GetYaxis()->SetTitle(\"Normalized Residuals\");\n", "resgr->SetMarkerStyle(21);\n", "resgr->SetMarkerColor(2);\n", "resgr->SetMarkerSize(.9);\n", "resgr->SetTitle(\"Normalized Residuals\");" ] }, { "cell_type": "markdown", "id": "e383e6a7", "metadata": {}, "source": [ "Quantile-Quantile plot" ] }, { "cell_type": "code", "execution_count": 5, "id": "95db7a39", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:24.121723Z", "iopub.status.busy": "2026-05-19T20:24:24.121577Z", "iopub.status.idle": "2026-05-19T20:24:24.383649Z", "shell.execute_reply": "2026-05-19T20:24:24.351716Z" } }, "outputs": [], "source": [ "TF1 *f = new TF1(\"f\",\"TMath::Gaus(x,0,1)\",-10,10);\n", "TGraphQQ *qqplot = new TGraphQQ(n,res,f);\n", "qqplot->SetMarkerStyle(20);\n", "qqplot->SetMarkerColor(2);\n", "qqplot->SetMarkerSize(.9);\n", "qqplot->SetTitle(\"Q-Q plot of Normalized Residuals\");" ] }, { "cell_type": "markdown", "id": "6057ea2d", "metadata": {}, "source": [ "create Canvas" ] }, { "cell_type": "code", "execution_count": 6, "id": "665a63b2", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:24.391083Z", "iopub.status.busy": "2026-05-19T20:24:24.390942Z", "iopub.status.idle": "2026-05-19T20:24:24.612872Z", "shell.execute_reply": "2026-05-19T20:24:24.597349Z" } }, "outputs": [], "source": [ "TCanvas *c1 = new TCanvas(\"c1\",\"Chistat Plot\",10,10,700,600);\n", "c1->Divide(2,2);" ] }, { "cell_type": "markdown", "id": "d6851503", "metadata": {}, "source": [ "Draw Histogramms and Graphs" ] }, { "cell_type": "code", "execution_count": 7, "id": "c6c0cbcb", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:24.614582Z", "iopub.status.busy": "2026-05-19T20:24:24.614455Z", "iopub.status.idle": "2026-05-19T20:24:24.936369Z", "shell.execute_reply": "2026-05-19T20:24:24.935940Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "c1->cd(1);\n", "h1->SetMarkerColor(4);\n", "h1->SetMarkerStyle(20);\n", "\n", "h1->Draw(\"E\");\n", "\n", "c1->cd(2);\n", "h2->Draw(\"\");\n", "h2->SetMarkerColor(4);\n", "h2->SetMarkerStyle(20);\n", "\n", "c1->cd(3);\n", "gPad->SetGridy();\n", "resgr->Draw(\"APL\");\n", "\n", "c1->cd(4);\n", "qqplot->Draw(\"AP\");\n", "\n", "c1->cd(0);\n", "\n", "c1->Update();\n", "return c1;" ] }, { "cell_type": "markdown", "id": "74100d3c", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 8, "id": "15811999", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:24.948620Z", "iopub.status.busy": "2026-05-19T20:24:24.948472Z", "iopub.status.idle": "2026-05-19T20:24:25.169928Z", "shell.execute_reply": "2026-05-19T20:24:25.169253Z" } }, "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 }