Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
testUnfold7b.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_unfold
3/// \notebook
4/// Test program for the classes TUnfoldDensity and TUnfoldBinning
5///
6/// A toy test of the TUnfold package
7///
8/// This example is documented in conference proceedings:
9///
10/// arXiv:1611.01927
11/// 12th Conference on Quark Confinement and the Hadron Spectrum (Confinement XII)
12///
13/// This is an example of unfolding a two-dimensional distribution
14/// also using an auxiliary measurement to constrain some background
15///
16/// The example comprises several macros
17/// - testUnfold7a.C create root files with TTree objects for
18/// signal, background and data
19/// - write files testUnfold7_signal.root
20/// testUnfold7_background.root
21/// testUnfold7_data.root
22///
23/// - testUnfold7b.C loop over trees and fill histograms based on the
24/// TUnfoldBinning objects
25/// - read testUnfold7binning.xml
26/// testUnfold7_signal.root
27/// testUnfold7_background.root
28/// testUnfold7_data.root
29///
30/// - write testUnfold7_histograms.root
31///
32/// - testUnfold7c.C run the unfolding
33/// - read testUnfold7_histograms.root
34/// - write testUnfold7_result.root
35/// testUnfold7_result.ps
36///
37/// \macro_output
38/// \macro_code
39///
40/// **Version 17.6, in parallel to changes in TUnfold**
41///
42/// This file is part of TUnfold.
43///
44/// TUnfold is free software: you can redistribute it and/or modify
45/// it under the terms of the GNU General Public License as published by
46/// the Free Software Foundation, either version 3 of the License, or
47/// (at your option) any later version.
48///
49/// TUnfold is distributed in the hope that it will be useful,
50/// but WITHOUT ANY WARRANTY; without even the implied warranty of
51/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52/// GNU General Public License for more details.
53///
54/// You should have received a copy of the GNU General Public License
55/// along with TUnfold. If not, see <http://www.gnu.org/licenses/>.
56///
57/// \author Stefan Schmitt DESY, 14.10.2008
58
59
60/* below is the content of the file testUnfold7binning.xml,
61 which is required as input to run this example.
62
63<?xml version="1.0" encoding="UTF-8" standalone="no"?>
64<!DOCTYPE TUnfoldBinning SYSTEM "tunfoldbinning.dtd">
65<TUnfoldBinning>
66<BinningNode name="fine" firstbin="1" factor="1">
67 <Axis name="pt" lowEdge="0.">
68 <Bin repeat="20" width="1."/>
69 <Bin repeat="12" width="2.5"/>
70 <Bin location="overflow" width="10"/>
71 </Axis>
72</BinningNode>
73<BinningNode name="coarse" firstbin="1" factor="1">
74 <Axis name="pt" lowEdge="0.">
75 <Bin repeat="10" width="2"/>
76 <Bin repeat="6" width="5"/>
77 <Bin location="overflow" width="10"/>
78 </Axis>
79</BinningNode>
80</TUnfoldBinning>
81
82 */
83
84#include <iostream>
85#include <cmath>
86#include <TMath.h>
87#include <TFile.h>
88#include <TTree.h>
89#include <TH1.h>
90#include <TDOMParser.h>
91#include <TXMLDocument.h>
92#include "TUnfoldBinningXML.h"
93
94using std::cout;
95
96
97void testUnfold7b()
98{
99 // switch on histogram errors
101
102 //=======================================================
103 // Step 1: open file to save histograms and binning schemes
104
105 TFile *outputFile=new TFile("testUnfold7_histograms.root","recreate");
106
107 //=======================================================
108 // Step 2: read binning from XML
109 // and save them to output file
110
111 TUnfoldBinning *fineBinningRoot,*coarseBinningRoot;
112
113 // read binning schemes in XML format
114
115 TDOMParser parser;
116 TString dir = gSystem->UnixPathName(gSystem->GetDirName(__FILE__));
117 Int_t error = parser.ParseFile(dir+"/testUnfold7binning.xml");
118 if(error) {
119 cout<<"error="<<error<<" from TDOMParser\n";
120 cout<<"==============================================================\n";
121 cout<<"Maybe the file testUnfold7binning.xml is missing?\n";
122 cout<<"The content of the file is included in the comments section\n";
123 cout<<"of this macro \"testUnfold7b.C\"\n";
124 cout<<"==============================================================\n";
125 }
126 TXMLDocument const *XMLdocument=parser.GetXMLDocument();
127 fineBinningRoot=
128 TUnfoldBinningXML::ImportXML(XMLdocument,"fine");
129 coarseBinningRoot=
130 TUnfoldBinningXML::ImportXML(XMLdocument,"coarse");
131
132 // write binning schemes to output file
133 fineBinningRoot->Write();
134 coarseBinningRoot->Write();
135
136 if(fineBinningRoot) {
137 fineBinningRoot->PrintStream(cout);
138 } else {
139 cout<<"could not read 'detector' binning\n";
140 }
141 if(coarseBinningRoot) {
142 coarseBinningRoot->PrintStream(cout);
143 } else {
144 cout<<"could not read 'generator' binning\n";
145 }
146
147 TUnfoldBinning const *fineBinning=fineBinningRoot;//->FindNode("ptfine");
148 TUnfoldBinning const *coarseBinning=coarseBinningRoot;//->FindNode("ptcoarse");
149
150
151 //=======================================================
152 // Step 3: book and fill data histograms
153
154 Float_t ptRec[3],ptGen[3],weight;
155 Int_t isTriggered,isSignal;
156
157 outputFile->cd();
158
159 TH1 *histDataRecF=fineBinning->CreateHistogram("histDataRecF");
160 TH1 *histDataRecC=coarseBinning->CreateHistogram("histDataRecC");
161 TH1 *histDataBgrF=fineBinning->CreateHistogram("histDataBgrF");
162 TH1 *histDataBgrC=coarseBinning->CreateHistogram("histDataBgrC");
163 TH1 *histDataGen=coarseBinning->CreateHistogram("histDataGen");
164
165 for (auto histo : {histDataRecF, histDataRecC, histDataBgrF, histDataBgrC, histDataGen}) {
166 histo->SetDirectory(outputFile);
167 }
168
169 TFile *dataFile=new TFile("testUnfold7_data.root");
170 TTree *dataTree=(TTree *) dataFile->Get("data");
171
172 if(!dataTree) {
173 cout<<"could not read 'data' tree\n";
174 }
175
176 dataTree->ResetBranchAddresses();
177 dataTree->SetBranchAddress("ptrec",ptRec);
178 //dataTree->SetBranchAddress("discr",&discr);
179 // for real data, only the triggered events are available
180 dataTree->SetBranchAddress("istriggered",&isTriggered);
181 // data truth parameters
182 dataTree->SetBranchAddress("ptgen",ptGen);
183 dataTree->SetBranchAddress("issignal",&isSignal);
184 dataTree->SetBranchStatus("*",1);
185
186 cout<<"loop over data events\n";
187
188#define VAR_REC (ptRec[2])
189#define VAR_GEN (ptGen[2])
190
191 for(Int_t ievent=0;ievent<dataTree->GetEntriesFast();ievent++) {
192 if(dataTree->GetEntry(ievent)<=0) break;
193 // fill histogram with reconstructed quantities
194 if(isTriggered) {
195 int binF=fineBinning->GetGlobalBinNumber(VAR_REC);
196 int binC=coarseBinning->GetGlobalBinNumber(VAR_REC);
197 histDataRecF->Fill(binF);
198 histDataRecC->Fill(binC);
199 if(!isSignal) {
200 histDataBgrF->Fill(binF);
201 histDataBgrC->Fill(binC);
202 }
203 }
204 // fill histogram with data truth parameters
205 if(isSignal) {
206 int binGen=coarseBinning->GetGlobalBinNumber(VAR_GEN);
207 histDataGen->Fill(binGen);
208 }
209 }
210
211 delete dataTree;
212 delete dataFile;
213
214 //=======================================================
215 // Step 4: book and fill histogram of migrations
216 // it receives events from both signal MC and background MC
217
219 (coarseBinning,fineBinning,"histMcsigGenRecF");
221 (coarseBinning,coarseBinning,"histMcsigGenRecC");
222 TH1 *histMcsigRecF=fineBinning->CreateHistogram("histMcsigRecF");
223 TH1 *histMcsigRecC=coarseBinning->CreateHistogram("histMcsigRecC");
224 TH1 *histMcsigGen=coarseBinning->CreateHistogram("histMcsigGen");
225 for (auto histo :
226 std::initializer_list<TH1 *>{histMcsigGenRecF, histMcsigGenRecC, histMcsigRecF, histMcsigRecC, histMcsigGen}) {
227 histo->SetDirectory(outputFile);
228 }
229
230 TFile *signalFile=new TFile("testUnfold7_signal.root");
231 TTree *signalTree=(TTree *) signalFile->Get("signal");
232
233 if(!signalTree) {
234 cout<<"could not read 'signal' tree\n";
235 }
236
237 signalTree->ResetBranchAddresses();
238 signalTree->SetBranchAddress("ptrec",&ptRec);
239 //signalTree->SetBranchAddress("discr",&discr);
240 signalTree->SetBranchAddress("istriggered",&isTriggered);
241 signalTree->SetBranchAddress("ptgen",&ptGen);
242 signalTree->SetBranchAddress("weight",&weight);
243 signalTree->SetBranchStatus("*",1);
244
245 cout<<"loop over MC signal events\n";
246
247 for(Int_t ievent=0;ievent<signalTree->GetEntriesFast();ievent++) {
248 if(signalTree->GetEntry(ievent)<=0) break;
249
250 int binC=0,binF=0;
251 if(isTriggered) {
252 binF=fineBinning->GetGlobalBinNumber(VAR_REC);
253 binC=coarseBinning->GetGlobalBinNumber(VAR_REC);
254 }
255 int binGen=coarseBinning->GetGlobalBinNumber(VAR_GEN);
256 histMcsigGenRecF->Fill(binGen,binF,weight);
257 histMcsigGenRecC->Fill(binGen,binC,weight);
258 histMcsigRecF->Fill(binF,weight);
259 histMcsigRecC->Fill(binC,weight);
260 histMcsigGen->Fill(binGen,weight);
261 }
262
263 delete signalTree;
264 delete signalFile;
265
266 TH1 *histMcbgrRecF=fineBinning->CreateHistogram("histMcbgrRecF");
267 TH1 *histMcbgrRecC=coarseBinning->CreateHistogram("histMcbgrRecC");
268 histMcbgrRecF->SetDirectory(outputFile);
269 histMcbgrRecC->SetDirectory(outputFile);
270
271 TFile *bgrFile=new TFile("testUnfold7_background.root");
272 TTree *bgrTree=(TTree *) bgrFile->Get("background");
273
274 if(!bgrTree) {
275 cout<<"could not read 'background' tree\n";
276 }
277
278 bgrTree->ResetBranchAddresses();
279 bgrTree->SetBranchAddress("ptrec",&ptRec);
280 //bgrTree->SetBranchAddress("discr",&discr);
281 bgrTree->SetBranchAddress("istriggered",&isTriggered);
282 bgrTree->SetBranchAddress("weight",&weight);
283 bgrTree->SetBranchStatus("*",1);
284
285 cout<<"loop over MC background events\n";
286
287 for(Int_t ievent=0;ievent<bgrTree->GetEntriesFast();ievent++) {
288 if(bgrTree->GetEntry(ievent)<=0) break;
289
290 // here, for background only reconstructed quantities are known
291 // and only the reconstructed events are relevant
292 if(isTriggered) {
293 int binF=fineBinning->GetGlobalBinNumber(VAR_REC);
294 int binC=coarseBinning->GetGlobalBinNumber(VAR_REC);
295 histMcbgrRecF->Fill(binF,weight);
296 histMcbgrRecC->Fill(binC,weight);
297 }
298 }
299
300 delete bgrTree;
301 delete bgrFile;
302
303 outputFile->Write();
304 delete outputFile;
305
306}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
float Float_t
Float 4 bytes (float).
Definition RtypesCore.h:71
externTSystem * gSystem
Definition TSystem.h:582
virtual TXMLDocument * GetXMLDocument() const
Returns the TXMLDocument.
Int_t ParseFile(const char *filename) override
Parse the XML file where filename is the XML file name.
Bool_t cd() override
Change current directory to "this" directory.
TObject * Get(const char *namecycle) override
Return pointer to object identified by namecycle.
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
Int_t Write(const char *name=nullptr, Int_t opt=0, Int_t bufsize=0) override
Write memory objects to this file.
Definition TFile.cxx:2488
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:109
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition TH1.cxx:9074
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3393
static void SetDefaultSumw2(Bool_t sumw2=kTRUE)
When this static function is called with sumw2=kTRUE, all new histograms will automatically activate ...
Definition TH1.cxx:6818
Service class for 2-D histogram classes.
Definition TH2.h:30
Int_t Fill(Double_t) override
Invalid Fill method.
Definition TH2.cxx:363
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition TObject.cxx:989
Basic string class.
Definition TString.h:138
A TTree represents a columnar dataset.
Definition TTree.h:89
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr, TClass *realClass, EDataType datatype, bool isptr, bool suppressMissingBranchError)
Definition TTree.cxx:8675
virtual void SetBranchStatus(const char *bname, bool status=true, UInt_t *found=nullptr)
Set branch status to Process or DoNotProcess.
Definition TTree.cxx:8779
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition TTree.cxx:5718
virtual Long64_t GetEntriesFast() const
Return a number greater or equal to the total number of entries in the dataset.
Definition TTree.h:552
virtual void ResetBranchAddresses()
Tell all of our branches to drop their current objects and allocate new ones.
Definition TTree.cxx:8268
static TUnfoldBinningXML * ImportXML(const TXMLDocument *document, const char *name)
import a binning scheme from an XML file
Binning schemes for use with the unfolding algorithm TUnfoldDensity.
void PrintStream(std::ostream &out, Int_t indent=0, int debug=0) const
print some information about this binning tree
TH1 * CreateHistogram(const char *histogramName, Bool_t originalAxisBinning=kFALSE, Int_t **binMap=nullptr, const char *histogramTitle=nullptr, const char *axisSteering=nullptr) const
create a THxx histogram capable to hold the bins of this binning node and its children
Int_t GetGlobalBinNumber(Double_t x) const
locate a bin in a one-dimensional distribution
static TH2D * CreateHistogramOfMigrations(TUnfoldBinning const *xAxis, TUnfoldBinning const *yAxis, char const *histogramName, Bool_t originalXAxisBinning=kFALSE, Bool_t originalYAxisBinning=kFALSE, char const *histogramTitle=nullptr)
create a TH2D histogram capable to hold the bins of the two input binning schemes on the x and y axes...
TXMLDocument contains a pointer to an xmlDoc structure, after the parser returns a tree built during ...