{ "cells": [ { "cell_type": "markdown", "id": "b75b05ea", "metadata": {}, "source": [ "# testUnfold5b\n", "Test program for the classes TUnfoldDensity and TUnfoldBinning.\n", "\n", "A toy test of the TUnfold package\n", "\n", "This is an example of unfolding a two-dimensional distribution\n", "also using an auxiliary measurement to constrain some background\n", "\n", "The example comprises several macros\n", " - testUnfold5a.C create root files with TTree objects for\n", " signal, background and data\n", " - write files testUnfold5_signal.root\n", " testUnfold5_background.root\n", " testUnfold5_data.root\n", "\n", " - testUnfold5b.C create a root file with the TUnfoldBinning objects\n", " - write file testUnfold5_binning.root\n", "\n", " - testUnfold5c.C loop over trees and fill histograms based on the\n", " TUnfoldBinning objects\n", " - read testUnfold5_binning.root\n", " testUnfold5_signal.root\n", " testUnfold5_background.root\n", " testUnfold5_data.root\n", "\n", " - write testUnfold5_histograms.root\n", "\n", " - testUnfold5d.C run the unfolding\n", " - read testUnfold5_histograms.root\n", " - write testUnfold5_result.root\n", " testUnfold5_result.ps\n", "\n", "\n", " **Version 17.6, in parallel to changes in TUnfold**\n", "\n", "#### History:\n", " - Version 17.5, updated for writing out XML code\n", " - Version 17.4, updated for writing out XML code\n", " - Version 17.3, updated for writing out XML code\n", " - Version 17.2, updated for writing out XML code\n", " - Version 17.1, in parallel to changes in TUnfold\n", " - Version 17.0 example for multi-dimensional unfolding\n", "\n", " This file is part of TUnfold.\n", "\n", " TUnfold is free software: you can redistribute it and/or modify\n", " it under the terms of the GNU General Public License as published by\n", " the Free Software Foundation, either version 3 of the License, or\n", " (at your option) any later version.\n", "\n", " TUnfold is distributed in the hope that it will be useful,\n", " but WITHOUT ANY WARRANTY; without even the implied warranty of\n", " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n", " GNU General Public License for more details.\n", "\n", " You should have received a copy of the GNU General Public License\n", " along with TUnfold. If not, see .\n", "\n", "\n", "\n", "**Author:** Stefan Schmitt DESY, 14.10.2008 \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:11 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "44129312", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:23.699900Z", "iopub.status.busy": "2026-05-19T20:11:23.699760Z", "iopub.status.idle": "2026-05-19T20:11:23.706829Z", "shell.execute_reply": "2026-05-19T20:11:23.706147Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \n", "#include \n", "#include \n", "#include \"TUnfoldBinningXML.h\"\n", "#include \n", "\n", "using std::ofstream;" ] }, { "cell_type": "markdown", "id": "c09ace32", "metadata": {}, "source": [ "write binning schemes to root file" ] }, { "cell_type": "code", "execution_count": 2, "id": "f8f0c388", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:23.708403Z", "iopub.status.busy": "2026-05-19T20:11:23.708238Z", "iopub.status.idle": "2026-05-19T20:11:24.044368Z", "shell.execute_reply": "2026-05-19T20:11:24.043510Z" } }, "outputs": [], "source": [ "TFile *binningSchemes=new TFile(\"testUnfold5_binning.root\",\"recreate\");" ] }, { "cell_type": "markdown", "id": "de973686", "metadata": {}, "source": [ "reconstructed pt, eta, discriminator" ] }, { "cell_type": "code", "execution_count": 3, "id": "0d1463c0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:24.046005Z", "iopub.status.busy": "2026-05-19T20:11:24.045878Z", "iopub.status.idle": "2026-05-19T20:11:24.249725Z", "shell.execute_reply": "2026-05-19T20:11:24.249089Z" } }, "outputs": [], "source": [ "#define NBIN_PT_FINE 8\n", "#define NBIN_ETA_FINE 10\n", "#define NBIN_DISCR 4" ] }, { "cell_type": "markdown", "id": "8c68ead4", "metadata": {}, "source": [ "generated pt, eta" ] }, { "cell_type": "code", "execution_count": 4, "id": "cb41f2c8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:24.251648Z", "iopub.status.busy": "2026-05-19T20:11:24.251512Z", "iopub.status.idle": "2026-05-19T20:11:24.455291Z", "shell.execute_reply": "2026-05-19T20:11:24.454627Z" } }, "outputs": [], "source": [ "#define NBIN_PT_COARSE 3\n", "#define NBIN_ETA_COARSE 3" ] }, { "cell_type": "markdown", "id": "a2d6c72f", "metadata": {}, "source": [ "pt binning" ] }, { "cell_type": "code", "execution_count": 5, "id": "f5eac1f4", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:24.457075Z", "iopub.status.busy": "2026-05-19T20:11:24.456956Z", "iopub.status.idle": "2026-05-19T20:11:24.660720Z", "shell.execute_reply": "2026-05-19T20:11:24.660059Z" } }, "outputs": [], "source": [ "Double_t ptBinsFine[NBIN_PT_FINE+1]=\n", " {3.5,4.0,4.5,5.0,6.0,7.0,8.0,10.0,13.0};\n", "Double_t ptBinsCoarse[NBIN_PT_COARSE+1]=\n", " { 4.0, 5.0, 7.0, 10.0};" ] }, { "cell_type": "markdown", "id": "7d36081b", "metadata": {}, "source": [ "eta binning" ] }, { "cell_type": "code", "execution_count": 6, "id": "8201cf00", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:24.662179Z", "iopub.status.busy": "2026-05-19T20:11:24.662062Z", "iopub.status.idle": "2026-05-19T20:11:24.864936Z", "shell.execute_reply": "2026-05-19T20:11:24.864496Z" } }, "outputs": [], "source": [ "Double_t etaBinsFine[NBIN_ETA_FINE+1]=\n", " {-3.,-2.5,-2.0,-1.,-0.5,0.0,0.5,1.,2.,2.5,3.};\n", "Double_t etaBinsCoarse[NBIN_ETA_COARSE+1]=\n", " { -2.0, -0.5, 0.5, 2. };" ] }, { "cell_type": "markdown", "id": "5438d1e4", "metadata": {}, "source": [ "discriminator bins" ] }, { "cell_type": "code", "execution_count": 7, "id": "c7597199", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:24.866978Z", "iopub.status.busy": "2026-05-19T20:11:24.866845Z", "iopub.status.idle": "2026-05-19T20:11:25.094486Z", "shell.execute_reply": "2026-05-19T20:11:25.093726Z" } }, "outputs": [], "source": [ "Double_t discrBins[NBIN_DISCR+1]={0.,0.15,0.5,0.85,1.0};" ] }, { "cell_type": "markdown", "id": "708b6fea", "metadata": {}, "source": [ "=======================================================================\n", "detector level binning scheme" ] }, { "cell_type": "code", "execution_count": 8, "id": "4bb41f09", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:25.106859Z", "iopub.status.busy": "2026-05-19T20:11:25.106717Z", "iopub.status.idle": "2026-05-19T20:11:25.310514Z", "shell.execute_reply": "2026-05-19T20:11:25.309561Z" } }, "outputs": [], "source": [ "TUnfoldBinning *detectorBinning=new TUnfoldBinning(\"detector\");" ] }, { "cell_type": "markdown", "id": "6545bfe8", "metadata": {}, "source": [ "highest discriminator bin has fine binning" ] }, { "cell_type": "code", "execution_count": 9, "id": "250b892f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:25.312307Z", "iopub.status.busy": "2026-05-19T20:11:25.312184Z", "iopub.status.idle": "2026-05-19T20:11:25.516028Z", "shell.execute_reply": "2026-05-19T20:11:25.515296Z" } }, "outputs": [], "source": [ "TUnfoldBinning *detectorDistribution=\n", " detectorBinning->AddBinning(\"detectordistribution\");\n", "detectorDistribution->AddAxis(\"pt\",NBIN_PT_FINE,ptBinsFine,\n", " false, // no underflow bin (not reconstructed)\n", " true // overflow bin\n", " );\n", "detectorDistribution->AddAxis(\"eta\",NBIN_ETA_FINE,etaBinsFine,\n", " false, // no underflow bin (not reconstructed)\n", " false // no overflow bin (not reconstructed)\n", " );\n", "detectorDistribution->AddAxis(\"discriminator\",NBIN_DISCR,discrBins,\n", " false, // no underflow bin (empty)\n", " false // no overflow bin (empty)\n", " );\n", "/* TUnfoldBinning *detectorExtra=\n", " detectorBinning->AddBinning(\"detectorextra\",7,\"one;zwei;three\");\n", " detectorBinning->PrintStream(cout); */" ] }, { "cell_type": "markdown", "id": "855ac9c3", "metadata": {}, "source": [ "=======================================================================\n", "generator level binning" ] }, { "cell_type": "code", "execution_count": 10, "id": "5fb7c03e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:25.523268Z", "iopub.status.busy": "2026-05-19T20:11:25.523102Z", "iopub.status.idle": "2026-05-19T20:11:25.732255Z", "shell.execute_reply": "2026-05-19T20:11:25.731697Z" } }, "outputs": [], "source": [ "TUnfoldBinning *generatorBinning=new TUnfoldBinning(\"generator\");" ] }, { "cell_type": "markdown", "id": "5e1ffe31", "metadata": {}, "source": [ "signal distribution is measured with coarse binning\n", "underflow and overflow bins are needed ot take care of\n", "what happens outside the phase-space" ] }, { "cell_type": "code", "execution_count": 11, "id": "1b733dd7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:25.733859Z", "iopub.status.busy": "2026-05-19T20:11:25.733717Z", "iopub.status.idle": "2026-05-19T20:11:25.935991Z", "shell.execute_reply": "2026-05-19T20:11:25.935358Z" } }, "outputs": [], "source": [ "TUnfoldBinning *signalBinning = generatorBinning->AddBinning(\"signal\");\n", "signalBinning->AddAxis(\"ptgen\",NBIN_PT_COARSE,ptBinsCoarse,\n", " true, // underflow bin\n", " true // overflow bin\n", " );\n", "signalBinning->AddAxis(\"etagen\",NBIN_ETA_COARSE,etaBinsCoarse,\n", " true, // underflow bin\n", " true // overflow bin\n", " );" ] }, { "cell_type": "markdown", "id": "5ed781f6", "metadata": {}, "source": [ "this is just an example how to set bin-dependent factors\n", "for the regularisation" ] }, { "cell_type": "code", "execution_count": 12, "id": "639747ea", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:25.937579Z", "iopub.status.busy": "2026-05-19T20:11:25.937455Z", "iopub.status.idle": "2026-05-19T20:11:26.141320Z", "shell.execute_reply": "2026-05-19T20:11:26.140640Z" } }, "outputs": [], "source": [ "TF2 *userFunc=new TF2(\"userfunc\",\"1./x+0.2*y^2\",ptBinsCoarse[0],\n", " ptBinsCoarse[NBIN_PT_COARSE],\n", " etaBinsCoarse[0],etaBinsCoarse[NBIN_ETA_COARSE]);\n", "signalBinning->SetBinFactorFunction(1.0,userFunc);" ] }, { "cell_type": "markdown", "id": "b8c18a86", "metadata": {}, "source": [ "background distribution is unfolded with fine binning\n", "!!! in the reconstructed variable !!!\n", "\n", "This has the effect of \"normalizing\" the background in each\n", "pt,eta bin to the low discriminator values\n", "Only the shape of the discriminator in each (pt,eta) bin\n", "is taken from Monte Carlo\n", "\n", "This method has been applied e.g. in\n", "H1 Collaboration, \"Prompt photons in Photoproduction\"\n", "Eur.Phys.J. C66 (2010) 17" ] }, { "cell_type": "code", "execution_count": 13, "id": "15217f85", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:26.147874Z", "iopub.status.busy": "2026-05-19T20:11:26.147742Z", "iopub.status.idle": "2026-05-19T20:11:26.352286Z", "shell.execute_reply": "2026-05-19T20:11:26.351552Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TUnfoldBinning \"generator\" has 115 bins [1,116] nTH1x=115\n", " TUnfoldBinning \"signal\" has 25 bins [1,26] nTH1x=25\n", " distribution: 25 bins\n", " \"ptgen\" nbin=3 plus underflow plus overflow\n", " \"etagen\" nbin=3 plus underflow plus overflow\n", " TUnfoldBinning \"background\" has 90 bins [26,116] nTH1x=90\n", " distribution: 90 bins\n", " \"ptrec\" nbin=8 plus overflow\n", " \"etarec\" nbin=10\n" ] } ], "source": [ "TUnfoldBinning *bgrBinning = generatorBinning->AddBinning(\"background\");\n", "bgrBinning->AddAxis(\"ptrec\",NBIN_PT_FINE,ptBinsFine,\n", " false, // no underflow bin (not reconstructed)\n", " true // overflow bin\n", " );\n", "bgrBinning->AddAxis(\"etarec\",NBIN_ETA_FINE,etaBinsFine,\n", " false, // no underflow bin (not reconstructed)\n", " false // no overflow bin (not reconstructed)\n", " );\n", "generatorBinning->PrintStream(cout);\n", "\n", "detectorBinning->Write();\n", "generatorBinning->Write();\n", "\n", "ofstream xmlOut(\"testUnfold5binning.xml\");\n", "TUnfoldBinningXML::ExportXML(*detectorBinning,xmlOut,kTRUE,kFALSE);\n", "TUnfoldBinningXML::ExportXML(*generatorBinning,xmlOut,kFALSE,kTRUE);\n", "TUnfoldBinningXML::WriteDTD();\n", "xmlOut.close();\n", "\n", "delete binningSchemes;" ] }, { "cell_type": "markdown", "id": "88e5e0cb", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 14, "id": "636f242b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:26.354004Z", "iopub.status.busy": "2026-05-19T20:11:26.353879Z", "iopub.status.idle": "2026-05-19T20:11:26.557506Z", "shell.execute_reply": "2026-05-19T20:11:26.556941Z" } }, "outputs": [], "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 }