{ "cells": [ { "cell_type": "markdown", "id": "5ee23030", "metadata": {}, "source": [ "# testUnfold7b\n", "Test program for the classes TUnfoldDensity and TUnfoldBinning\n", "\n", "A toy test of the TUnfold package\n", "\n", "This example is documented in conference proceedings:\n", "\n", " arXiv:1611.01927\n", "12th Conference on Quark Confinement and the Hadron Spectrum (Confinement XII)\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", " - testUnfold7a.C create root files with TTree objects for\n", " signal, background and data\n", " - write files testUnfold7_signal.root\n", " testUnfold7_background.root\n", " testUnfold7_data.root\n", "\n", " - testUnfold7b.C loop over trees and fill histograms based on the\n", " TUnfoldBinning objects\n", " - read testUnfold7binning.xml\n", " testUnfold7_signal.root\n", " testUnfold7_background.root\n", " testUnfold7_data.root\n", "\n", " - write testUnfold7_histograms.root\n", "\n", " - testUnfold7c.C run the unfolding\n", " - read testUnfold7_histograms.root\n", " - write testUnfold7_result.root\n", " testUnfold7_result.ps\n", "\n", "\n", " **Version 17.6, in parallel to changes in TUnfold**\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": "0b5f2a95", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:39.685253Z", "iopub.status.busy": "2026-05-19T20:11:39.685138Z", "iopub.status.idle": "2026-05-19T20:11:40.030194Z", "shell.execute_reply": "2026-05-19T20:11:40.029413Z" } }, "outputs": [], "source": [ "/* below is the content of the file testUnfold7binning.xml,\n", " which is required as input to run this example.\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", "\n", " */\n", "\n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \"TUnfoldBinningXML.h\"\n", "\n", "using std::cout;" ] }, { "cell_type": "markdown", "id": "a0273ba6", "metadata": {}, "source": [ "switch on histogram errors" ] }, { "cell_type": "code", "execution_count": 2, "id": "6452b935", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:40.032464Z", "iopub.status.busy": "2026-05-19T20:11:40.032335Z", "iopub.status.idle": "2026-05-19T20:11:40.237059Z", "shell.execute_reply": "2026-05-19T20:11:40.236461Z" } }, "outputs": [], "source": [ "TH1::SetDefaultSumw2();" ] }, { "cell_type": "markdown", "id": "b30fc731", "metadata": {}, "source": [ "=======================================================\n", "Step 1: open file to save histograms and binning schemes" ] }, { "cell_type": "code", "execution_count": 3, "id": "079d9c36", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:40.239408Z", "iopub.status.busy": "2026-05-19T20:11:40.239293Z", "iopub.status.idle": "2026-05-19T20:11:40.441961Z", "shell.execute_reply": "2026-05-19T20:11:40.441585Z" } }, "outputs": [], "source": [ "TFile *outputFile=new TFile(\"testUnfold7_histograms.root\",\"recreate\");" ] }, { "cell_type": "markdown", "id": "048ec7e6", "metadata": {}, "source": [ "=======================================================\n", "Step 2: read binning from XML\n", "and save them to output file" ] }, { "cell_type": "code", "execution_count": 4, "id": "a3d4964d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:40.444516Z", "iopub.status.busy": "2026-05-19T20:11:40.444398Z", "iopub.status.idle": "2026-05-19T20:11:40.648271Z", "shell.execute_reply": "2026-05-19T20:11:40.647500Z" } }, "outputs": [], "source": [ "TUnfoldBinning *fineBinningRoot,*coarseBinningRoot;" ] }, { "cell_type": "markdown", "id": "dc1cd85c", "metadata": {}, "source": [ "read binning schemes in XML format" ] }, { "cell_type": "code", "execution_count": 5, "id": "193be3af", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:40.650024Z", "iopub.status.busy": "2026-05-19T20:11:40.649903Z", "iopub.status.idle": "2026-05-19T20:11:40.852987Z", "shell.execute_reply": "2026-05-19T20:11:40.852358Z" } }, "outputs": [], "source": [ "TDOMParser parser;\n", "TString dir = gSystem->UnixPathName(gSystem->GetDirName(__FILE__));\n", "Int_t error = parser.ParseFile(dir+\"/testUnfold7binning.xml\");\n", "if(error) {\n", " cout<<\"error=\"<Write();\n", "coarseBinningRoot->Write();\n", "\n", "if(fineBinningRoot) {\n", " fineBinningRoot->PrintStream(cout);\n", "} else {\n", " cout<<\"could not read 'detector' binning\\n\";\n", "}\n", "if(coarseBinningRoot) {\n", " coarseBinningRoot->PrintStream(cout);\n", "} else {\n", " cout<<\"could not read 'generator' binning\\n\";\n", "}\n", "\n", "TUnfoldBinning const *fineBinning=fineBinningRoot;//->FindNode(\"ptfine\");\n", "TUnfoldBinning const *coarseBinning=coarseBinningRoot;//->FindNode(\"ptcoarse\");" ] }, { "cell_type": "markdown", "id": "bc71a16b", "metadata": {}, "source": [ "=======================================================\n", "Step 3: book and fill data histograms" ] }, { "cell_type": "code", "execution_count": 7, "id": "5228e5fa", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:41.059364Z", "iopub.status.busy": "2026-05-19T20:11:41.059254Z", "iopub.status.idle": "2026-05-19T20:11:41.262002Z", "shell.execute_reply": "2026-05-19T20:11:41.261596Z" } }, "outputs": [], "source": [ "Float_t ptRec[3],ptGen[3],weight;\n", "Int_t isTriggered,isSignal;\n", "\n", "outputFile->cd();\n", "\n", "TH1 *histDataRecF=fineBinning->CreateHistogram(\"histDataRecF\");\n", "TH1 *histDataRecC=coarseBinning->CreateHistogram(\"histDataRecC\");\n", "TH1 *histDataBgrF=fineBinning->CreateHistogram(\"histDataBgrF\");\n", "TH1 *histDataBgrC=coarseBinning->CreateHistogram(\"histDataBgrC\");\n", "TH1 *histDataGen=coarseBinning->CreateHistogram(\"histDataGen\");\n", "\n", "for (auto histo : {histDataRecF, histDataRecC, histDataBgrF, histDataBgrC, histDataGen}) {\n", " histo->SetDirectory(outputFile);\n", "}\n", "\n", "TFile *dataFile=new TFile(\"testUnfold7_data.root\");\n", "TTree *dataTree=(TTree *) dataFile->Get(\"data\");\n", "\n", "if(!dataTree) {\n", " cout<<\"could not read 'data' tree\\n\";\n", "}\n", "\n", "dataTree->ResetBranchAddresses();\n", "dataTree->SetBranchAddress(\"ptrec\",ptRec);" ] }, { "cell_type": "markdown", "id": "5e4ecfb8", "metadata": {}, "source": [ "dataTree->SetBranchAddress(\"discr\",&discr);\n", "for real data, only the triggered events are available" ] }, { "cell_type": "code", "execution_count": 8, "id": "b5cd996e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:41.268296Z", "iopub.status.busy": "2026-05-19T20:11:41.268175Z", "iopub.status.idle": "2026-05-19T20:11:41.471220Z", "shell.execute_reply": "2026-05-19T20:11:41.470611Z" } }, "outputs": [], "source": [ "dataTree->SetBranchAddress(\"istriggered\",&isTriggered);" ] }, { "cell_type": "markdown", "id": "15f52f2c", "metadata": {}, "source": [ "data truth parameters" ] }, { "cell_type": "code", "execution_count": 9, "id": "343421d2", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:41.473185Z", "iopub.status.busy": "2026-05-19T20:11:41.473070Z", "iopub.status.idle": "2026-05-19T20:11:41.676784Z", "shell.execute_reply": "2026-05-19T20:11:41.676310Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "loop over data events\n" ] } ], "source": [ "dataTree->SetBranchAddress(\"ptgen\",ptGen);\n", "dataTree->SetBranchAddress(\"issignal\",&isSignal);\n", "dataTree->SetBranchStatus(\"*\",1);\n", "\n", "cout<<\"loop over data events\\n\";\n", "\n", "#define VAR_REC (ptRec[2])\n", "#define VAR_GEN (ptGen[2])\n", "\n", "for(Int_t ievent=0;ieventGetEntriesFast();ievent++) {\n", " if(dataTree->GetEntry(ievent)<=0) break;\n", " // fill histogram with reconstructed quantities\n", " if(isTriggered) {\n", " int binF=fineBinning->GetGlobalBinNumber(VAR_REC);\n", " int binC=coarseBinning->GetGlobalBinNumber(VAR_REC);\n", " histDataRecF->Fill(binF);\n", " histDataRecC->Fill(binC);\n", " if(!isSignal) {\n", " histDataBgrF->Fill(binF);\n", " histDataBgrC->Fill(binC);\n", " }\n", " }\n", " // fill histogram with data truth parameters\n", " if(isSignal) {\n", " int binGen=coarseBinning->GetGlobalBinNumber(VAR_GEN);\n", " histDataGen->Fill(binGen);\n", " }\n", "}\n", "\n", "delete dataTree;\n", "delete dataFile;" ] }, { "cell_type": "markdown", "id": "76e18eef", "metadata": {}, "source": [ "=======================================================\n", "Step 4: book and fill histogram of migrations\n", "it receives events from both signal MC and background MC" ] }, { "cell_type": "code", "execution_count": 10, "id": "d20d19ba", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:41.678351Z", "iopub.status.busy": "2026-05-19T20:11:41.678243Z", "iopub.status.idle": "2026-05-19T20:11:41.881115Z", "shell.execute_reply": "2026-05-19T20:11:41.880624Z" } }, "outputs": [], "source": [ "TH2 *histMcsigGenRecF=TUnfoldBinning::CreateHistogramOfMigrations\n", " (coarseBinning,fineBinning,\"histMcsigGenRecF\");\n", "TH2 *histMcsigGenRecC=TUnfoldBinning::CreateHistogramOfMigrations\n", " (coarseBinning,coarseBinning,\"histMcsigGenRecC\");\n", "TH1 *histMcsigRecF=fineBinning->CreateHistogram(\"histMcsigRecF\");\n", "TH1 *histMcsigRecC=coarseBinning->CreateHistogram(\"histMcsigRecC\");\n", "TH1 *histMcsigGen=coarseBinning->CreateHistogram(\"histMcsigGen\");\n", "for (auto histo :\n", " std::initializer_list{histMcsigGenRecF, histMcsigGenRecC, histMcsigRecF, histMcsigRecC, histMcsigGen}) {\n", " histo->SetDirectory(outputFile);\n", "}\n", "\n", "TFile *signalFile=new TFile(\"testUnfold7_signal.root\");\n", "TTree *signalTree=(TTree *) signalFile->Get(\"signal\");\n", "\n", "if(!signalTree) {\n", " cout<<\"could not read 'signal' tree\\n\";\n", "}\n", "\n", "signalTree->ResetBranchAddresses();\n", "signalTree->SetBranchAddress(\"ptrec\",&ptRec);" ] }, { "cell_type": "markdown", "id": "9bc25186", "metadata": {}, "source": [ "signalTree->SetBranchAddress(\"discr\",&discr);" ] }, { "cell_type": "code", "execution_count": 11, "id": "55f904ab", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:41.882935Z", "iopub.status.busy": "2026-05-19T20:11:41.882825Z", "iopub.status.idle": "2026-05-19T20:11:42.302903Z", "shell.execute_reply": "2026-05-19T20:11:42.291651Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "loop over MC signal events\n" ] } ], "source": [ "signalTree->SetBranchAddress(\"istriggered\",&isTriggered);\n", "signalTree->SetBranchAddress(\"ptgen\",&ptGen);\n", "signalTree->SetBranchAddress(\"weight\",&weight);\n", "signalTree->SetBranchStatus(\"*\",1);\n", "\n", "cout<<\"loop over MC signal events\\n\";\n", "\n", "for(Int_t ievent=0;ieventGetEntriesFast();ievent++) {\n", " if(signalTree->GetEntry(ievent)<=0) break;\n", "\n", " int binC=0,binF=0;\n", " if(isTriggered) {\n", " binF=fineBinning->GetGlobalBinNumber(VAR_REC);\n", " binC=coarseBinning->GetGlobalBinNumber(VAR_REC);\n", " }\n", " int binGen=coarseBinning->GetGlobalBinNumber(VAR_GEN);\n", " histMcsigGenRecF->Fill(binGen,binF,weight);\n", " histMcsigGenRecC->Fill(binGen,binC,weight);\n", " histMcsigRecF->Fill(binF,weight);\n", " histMcsigRecC->Fill(binC,weight);\n", " histMcsigGen->Fill(binGen,weight);\n", "}\n", "\n", "delete signalTree;\n", "delete signalFile;\n", "\n", "TH1 *histMcbgrRecF=fineBinning->CreateHistogram(\"histMcbgrRecF\");\n", "TH1 *histMcbgrRecC=coarseBinning->CreateHistogram(\"histMcbgrRecC\");\n", "histMcbgrRecF->SetDirectory(outputFile);\n", "histMcbgrRecC->SetDirectory(outputFile);\n", "\n", "TFile *bgrFile=new TFile(\"testUnfold7_background.root\");\n", "TTree *bgrTree=(TTree *) bgrFile->Get(\"background\");\n", "\n", "if(!bgrTree) {\n", " cout<<\"could not read 'background' tree\\n\";\n", "}\n", "\n", "bgrTree->ResetBranchAddresses();\n", "bgrTree->SetBranchAddress(\"ptrec\",&ptRec);" ] }, { "cell_type": "markdown", "id": "9ef63fcf", "metadata": {}, "source": [ "bgrTree->SetBranchAddress(\"discr\",&discr);" ] }, { "cell_type": "code", "execution_count": 12, "id": "dceb5ff3", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:42.322437Z", "iopub.status.busy": "2026-05-19T20:11:42.322266Z", "iopub.status.idle": "2026-05-19T20:11:42.526203Z", "shell.execute_reply": "2026-05-19T20:11:42.525719Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "loop over MC background events\n" ] } ], "source": [ "bgrTree->SetBranchAddress(\"istriggered\",&isTriggered);\n", "bgrTree->SetBranchAddress(\"weight\",&weight);\n", "bgrTree->SetBranchStatus(\"*\",1);\n", "\n", "cout<<\"loop over MC background events\\n\";\n", "\n", "for(Int_t ievent=0;ieventGetEntriesFast();ievent++) {\n", " if(bgrTree->GetEntry(ievent)<=0) break;\n", "\n", " // here, for background only reconstructed quantities are known\n", " // and only the reconstructed events are relevant\n", " if(isTriggered) {\n", " int binF=fineBinning->GetGlobalBinNumber(VAR_REC);\n", " int binC=coarseBinning->GetGlobalBinNumber(VAR_REC);\n", " histMcbgrRecF->Fill(binF,weight);\n", " histMcbgrRecC->Fill(binC,weight);\n", " }\n", "}\n", "\n", "delete bgrTree;\n", "delete bgrFile;\n", "\n", "outputFile->Write();\n", "delete outputFile;" ] }, { "cell_type": "markdown", "id": "1731c2a3", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 13, "id": "fa72c0db", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:42.530587Z", "iopub.status.busy": "2026-05-19T20:11:42.530456Z", "iopub.status.idle": "2026-05-19T20:11:42.736838Z", "shell.execute_reply": "2026-05-19T20:11:42.736466Z" } }, "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 }