{ "cells": [ { "cell_type": "markdown", "id": "7106673d", "metadata": {}, "source": [ "# rf109_chi2residpull\n", "Basic functionality: Calculating chi^2 from histograms and curves in RooPlots, making\n", "histogram of residual and pull distributions\n", "\n", "\n", "\n", "\n", "**Author:** Wouter Verkerke \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:29 PM." ] }, { "cell_type": "code", "execution_count": null, "id": "dcf2f787", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TAxis.h\"\n", "#include \"RooPlot.h\"\n", "#include \"RooHist.h\"\n", "using namespace RooFit;" ] }, { "cell_type": "markdown", "id": "09f49d20", "metadata": {}, "source": [ "Setup model\n", "---------------------" ] }, { "cell_type": "markdown", "id": "7bc340ad", "metadata": {}, "source": [ "Create observables" ] }, { "cell_type": "code", "execution_count": null, "id": "74550fd8", "metadata": { "collapsed": false }, "outputs": [], "source": [ "RooRealVar x(\"x\", \"x\", -10, 10);" ] }, { "cell_type": "markdown", "id": "519b1ccb", "metadata": {}, "source": [ "Create Gaussian" ] }, { "cell_type": "code", "execution_count": null, "id": "9e2f285a", "metadata": { "collapsed": false }, "outputs": [], "source": [ "RooRealVar sigma(\"sigma\", \"sigma\", 3, 0.1, 10);\n", "RooRealVar mean(\"mean\", \"mean\", 0, -10, 10);\n", "RooGaussian gauss(\"gauss\", \"gauss\", x, 0.0, sigma);" ] }, { "cell_type": "markdown", "id": "58bcc19f", "metadata": {}, "source": [ "Generate a sample of 1000 events with sigma=3" ] }, { "cell_type": "code", "execution_count": null, "id": "fad5cb7b", "metadata": { "collapsed": false }, "outputs": [], "source": [ "std::unique_ptr data{gauss.generate(x, 10000)};" ] }, { "cell_type": "markdown", "id": "a044f1bc", "metadata": {}, "source": [ "Change sigma to 3.15" ] }, { "cell_type": "code", "execution_count": null, "id": "0aa081e9", "metadata": { "collapsed": false }, "outputs": [], "source": [ "sigma.setVal(3.15);" ] }, { "cell_type": "markdown", "id": "bf1d5634", "metadata": {}, "source": [ "Plot data and slightly distorted model\n", "---------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "fd4a6054", "metadata": {}, "source": [ "Overlay projection of gauss with sigma=3.15 on data with sigma=3.0" ] }, { "cell_type": "code", "execution_count": null, "id": "5d99d19f", "metadata": { "collapsed": false }, "outputs": [], "source": [ "RooPlot *frame1 = x.frame(Title(\"Data with distorted Gaussian pdf\"), Bins(40));\n", "data->plotOn(frame1, DataError(RooAbsData::SumW2));\n", "gauss.plotOn(frame1);" ] }, { "cell_type": "markdown", "id": "5152180e", "metadata": {}, "source": [ "Calculate chi^2\n", "------------------------------" ] }, { "cell_type": "markdown", "id": "9c464f33", "metadata": {}, "source": [ "Show the chi^2 of the curve w.r.t. the histogram\n", "If multiple curves or datasets live in the frame you can specify\n", "the name of the relevant curve and/or dataset in chiSquare()" ] }, { "cell_type": "code", "execution_count": null, "id": "93a8935d", "metadata": { "collapsed": false }, "outputs": [], "source": [ "cout << \"chi^2 = \" << frame1->chiSquare() << endl;" ] }, { "cell_type": "markdown", "id": "c1353ec6", "metadata": {}, "source": [ "Show residual and pull dists\n", "-------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "15658cb5", "metadata": {}, "source": [ "Construct a histogram with the residuals of the data w.r.t. the curve" ] }, { "cell_type": "code", "execution_count": null, "id": "ea9c75d9", "metadata": { "collapsed": false }, "outputs": [], "source": [ "RooHist *hresid = frame1->residHist();" ] }, { "cell_type": "markdown", "id": "ec543218", "metadata": {}, "source": [ "Construct a histogram with the pulls of the data w.r.t the curve" ] }, { "cell_type": "code", "execution_count": null, "id": "d6ca4c2a", "metadata": { "collapsed": false }, "outputs": [], "source": [ "RooHist *hpull = frame1->pullHist();" ] }, { "cell_type": "markdown", "id": "b60a1b61", "metadata": {}, "source": [ "Create a new frame to draw the residual distribution and add the distribution to the frame" ] }, { "cell_type": "code", "execution_count": null, "id": "98cca9a9", "metadata": { "collapsed": false }, "outputs": [], "source": [ "RooPlot *frame2 = x.frame(Title(\"Residual Distribution\"));\n", "frame2->addPlotable(hresid, \"P\");" ] }, { "cell_type": "markdown", "id": "f3271169", "metadata": {}, "source": [ "Create a new frame to draw the pull distribution and add the distribution to the frame" ] }, { "cell_type": "code", "execution_count": null, "id": "35998a3d", "metadata": { "collapsed": false }, "outputs": [], "source": [ "RooPlot *frame3 = x.frame(Title(\"Pull Distribution\"));\n", "frame3->addPlotable(hpull, \"P\");\n", "\n", "TCanvas *c = new TCanvas(\"rf109_chi2residpull\", \"rf109_chi2residpull\", 900, 300);\n", "c->Divide(3);\n", "c->cd(1);\n", "gPad->SetLeftMargin(0.15);\n", "frame1->GetYaxis()->SetTitleOffset(1.6);\n", "frame1->Draw();\n", "c->cd(2);\n", "gPad->SetLeftMargin(0.15);\n", "frame2->GetYaxis()->SetTitleOffset(1.6);\n", "frame2->Draw();\n", "c->cd(3);\n", "gPad->SetLeftMargin(0.15);\n", "frame3->GetYaxis()->SetTitleOffset(1.6);\n", "frame3->Draw();" ] }, { "cell_type": "markdown", "id": "0ab4c393", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": null, "id": "bdf96ba3", "metadata": { "collapsed": false }, "outputs": [], "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 }