Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
testUnfold5c.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 is an example of unfolding a two-dimensional distribution
9/// also using an auxiliary measurement to constrain some background
10///
11/// The example comprises several macros
12/// - testUnfold5a.C create root files with TTree objects for
13/// signal, background and data
14/// - write files testUnfold5_signal.root
15/// testUnfold5_background.root
16/// testUnfold5_data.root
17///
18/// - testUnfold5b.C create a root file with the TUnfoldBinning objects
19/// - write file testUnfold5_binning.root
20///
21/// - testUnfold5c.C loop over trees and fill histograms based on the
22/// TUnfoldBinning objects
23/// - read testUnfold5_binning.root
24/// testUnfold5_signal.root
25/// testUnfold5_background.root
26/// testUnfold5_data.root
27///
28/// - write testUnfold5_histograms.root
29///
30/// - testUnfold5d.C run the unfolding
31/// - read testUnfold5_histograms.root
32/// - write testUnfold5_result.root
33/// testUnfold5_result.ps
34///
35/// \macro_output
36/// \macro_code
37///
38/// **Version 17.6, in parallel to changes in TUnfold**
39///
40/// #### History:
41/// - Version 17.5, updated for reading binning from XML file
42/// - Version 17.4, updated for reading binning from XML file
43/// - Version 17.3, updated for reading binning from XML file
44/// - Version 17.2, updated for reading binning from XML file
45/// - Version 17.1, in parallel to changes in TUnfold
46/// - Version 17.0 example for multi-dimensional unfolding
47///
48/// This file is part of TUnfold.
49///
50/// TUnfold is free software: you can redistribute it and/or modify
51/// it under the terms of the GNU General Public License as published by
52/// the Free Software Foundation, either version 3 of the License, or
53/// (at your option) any later version.
54///
55/// TUnfold is distributed in the hope that it will be useful,
56/// but WITHOUT ANY WARRANTY; without even the implied warranty of
57/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58/// GNU General Public License for more details.
59///
60/// You should have received a copy of the GNU General Public License
61/// along with TUnfold. If not, see <http://www.gnu.org/licenses/>.
62///
63/// \author Stefan Schmitt DESY, 14.10.2008
64
65// uncomment this to read the binning schemes from the root file
66// by default the binning is read from the XML file
67// #define READ_BINNING_CINT
68
69
70#include <iostream>
71#include <cmath>
72#include <TMath.h>
73#include <TFile.h>
74#include <TTree.h>
75#include <TH1.h>
76#ifndef READ_BINNING_CINT
77#include <TDOMParser.h>
78#include <TXMLDocument.h>
79#include "TUnfoldBinningXML.h"
80#else
81#include "TUnfoldBinning.h"
82#endif
83
84using std::cout;
85
86
87void testUnfold5c()
88{
89 // switch on histogram errors
91
92 //=======================================================
93 // Step 1: open file to save histograms and binning schemes
94
95 TFile *outputFile=new TFile("testUnfold5_histograms.root","recreate");
96
97 //=======================================================
98 // Step 2: read binning from XML
99 // and save them to output file
100
101#ifdef READ_BINNING_CINT
102 TFile *binningSchemes=new TFile("testUnfold5_binning.root");
103#endif
104
105 TUnfoldBinning *detectorBinning,*generatorBinning;
106
107 outputFile->cd();
108
109 // read binning schemes in XML format
110#ifndef READ_BINNING_CINT
111 TDOMParser parser;
112 Int_t error=parser.ParseFile("testUnfold5binning.xml");
113 if(error) cout<<"error="<<error<<" from TDOMParser\n";
114 TXMLDocument const *XMLdocument=parser.GetXMLDocument();
115 detectorBinning=
116 TUnfoldBinningXML::ImportXML(XMLdocument,"detector");
117 generatorBinning=
118 TUnfoldBinningXML::ImportXML(XMLdocument,"generator");
119#else
120 binningSchemes->GetObject("detector",detectorBinning);
121 binningSchemes->GetObject("generator",generatorBinning);
122
123 delete binningSchemes;
124#endif
125 detectorBinning->Write();
126 generatorBinning->Write();
127
128 if(detectorBinning) {
129 detectorBinning->PrintStream(cout);
130 } else {
131 cout<<"could not read 'detector' binning\n";
132 }
133 if(generatorBinning) {
134 generatorBinning->PrintStream(cout);
135 } else {
136 cout<<"could not read 'generator' binning\n";
137 }
138
139 // pointers to various nodes in the binning scheme
140 const TUnfoldBinning *detectordistribution=
141 detectorBinning->FindNode("detectordistribution");
142
143 const TUnfoldBinning *signalBinning=
144 generatorBinning->FindNode("signal");
145
146 const TUnfoldBinning *bgrBinning=
147 generatorBinning->FindNode("background");
148
149 // write binning schemes to output file
150
151 //=======================================================
152 // Step 3: book and fill data histograms
153
154 Float_t etaRec,ptRec,discr,etaGen,ptGen;
155 Int_t istriggered,issignal;
156
157 outputFile->cd();
158
159 TH1 *histDataReco=detectorBinning->CreateHistogram("histDataReco");
160 TH1 *histDataTruth=generatorBinning->CreateHistogram("histDataTruth");
161
162 TFile *dataFile=new TFile("testUnfold5_data.root");
163 TTree *dataTree=(TTree *) dataFile->Get("data");
164
165 if(!dataTree) {
166 cout<<"could not read 'data' tree\n";
167 }
168
169 dataTree->ResetBranchAddresses();
170 dataTree->SetBranchAddress("etarec",&etaRec);
171 dataTree->SetBranchAddress("ptrec",&ptRec);
172 dataTree->SetBranchAddress("discr",&discr);
173 // for real data, only the triggered events are available
174 dataTree->SetBranchAddress("istriggered",&istriggered);
175 // data truth parameters
176 dataTree->SetBranchAddress("etagen",&etaGen);
177 dataTree->SetBranchAddress("ptgen",&ptGen);
178 dataTree->SetBranchAddress("issignal",&issignal);
179 dataTree->SetBranchStatus("*",true);
180
181
182 cout<<"loop over data events\n";
183
184 for(Int_t ievent=0;ievent<dataTree->GetEntriesFast();ievent++) {
185 if(dataTree->GetEntry(ievent)<=0) break;
186 // fill histogram with reconstructed quantities
187 if(istriggered) {
188 Int_t binNumber=
189 detectordistribution->GetGlobalBinNumber(ptRec,etaRec,discr);
190 histDataReco->Fill(binNumber);
191 }
192 // fill histogram with data truth parameters
193 if(issignal) {
194 // signal has true eta and pt
195 Int_t binNumber=signalBinning->GetGlobalBinNumber(ptGen,etaGen);
196 histDataTruth->Fill(binNumber);
197 } else {
198 // background only has reconstructed pt and eta
199 Int_t binNumber=bgrBinning->GetGlobalBinNumber(ptRec,etaRec);
200 histDataTruth->Fill(binNumber);
201 }
202 }
203
204 delete dataTree;
205 delete dataFile;
206
207 //=======================================================
208 // Step 4: book and fill histogram of migrations
209 // it receives events from both signal MC and background MC
210
211 outputFile->cd();
212
214 (generatorBinning,detectorBinning,"histMCGenRec");
215
216 TFile *signalFile=new TFile("testUnfold5_signal.root");
217 TTree *signalTree=(TTree *) signalFile->Get("signal");
218
219 if(!signalTree) {
220 cout<<"could not read 'signal' tree\n";
221 }
222
223 signalTree->ResetBranchAddresses();
224 signalTree->SetBranchAddress("etarec",&etaRec);
225 signalTree->SetBranchAddress("ptrec",&ptRec);
226 signalTree->SetBranchAddress("discr",&discr);
227 signalTree->SetBranchAddress("istriggered",&istriggered);
228 signalTree->SetBranchAddress("etagen",&etaGen);
229 signalTree->SetBranchAddress("ptgen",&ptGen);
230 signalTree->SetBranchStatus("*",true);
231
232 cout<<"loop over MC signal events\n";
233
234 for(Int_t ievent=0;ievent<signalTree->GetEntriesFast();ievent++) {
235 if(signalTree->GetEntry(ievent)<=0) break;
236
237 // bin number on generator level for signal
238 Int_t genBin=signalBinning->GetGlobalBinNumber(ptGen,etaGen);
239
240 // bin number on reconstructed level
241 // bin number 0 corresponds to non-reconstructed events
242 Int_t recBin=0;
243 if(istriggered) {
244 recBin=detectordistribution->GetGlobalBinNumber(ptRec,etaRec,discr);
245 }
246 histMCGenRec->Fill(genBin,recBin);
247 }
248
249 delete signalTree;
250 delete signalFile;
251
252 TFile *bgrFile=new TFile("testUnfold5_background.root");
253 TTree *bgrTree=(TTree *) bgrFile->Get("background");
254
255 if(!bgrTree) {
256 cout<<"could not read 'background' tree\n";
257 }
258
259 bgrTree->ResetBranchAddresses();
260 bgrTree->SetBranchAddress("etarec",&etaRec);
261 bgrTree->SetBranchAddress("ptrec",&ptRec);
262 bgrTree->SetBranchAddress("discr",&discr);
263 bgrTree->SetBranchAddress("istriggered",&istriggered);
264 bgrTree->SetBranchStatus("*",true);
265
266 cout<<"loop over MC background events\n";
267
268 for(Int_t ievent=0;ievent<bgrTree->GetEntriesFast();ievent++) {
269 if(bgrTree->GetEntry(ievent)<=0) break;
270
271 // here, for background only reconstructed quantities are known
272 // and only the reconstructed events are relevant
273 if(istriggered) {
274 // bin number on generator level for background
275 Int_t genBin=bgrBinning->GetGlobalBinNumber(ptRec,etaRec);
276 // bin number on reconstructed level
277 Int_t recBin=detectordistribution->GetGlobalBinNumber
278 (ptRec,etaRec,discr);
279 histMCGenRec->Fill(genBin,recBin);
280 }
281 }
282
283 delete bgrTree;
284 delete bgrFile;
285
286 outputFile->Write();
287 delete outputFile;
288
289}
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
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.
void GetObject(const char *namecycle, T *&ptr)
Get an object with proper type checking.
Definition TDirectory.h:212
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
Int_t Write(const char *name=nullptr, Int_t opt=0, Int_t bufsiz=0) override
Write memory objects to this file.
Definition TFile.cxx:2433
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3346
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:6732
Service class for 2-D histogram classes.
Definition TH2.h:30
Int_t Fill(Double_t) override
Invalid Fill method.
Definition TH2.cxx:393
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:898
A TTree represents a columnar dataset.
Definition TTree.h:79
virtual void SetBranchStatus(const char *bname, bool status=true, UInt_t *found=nullptr)
Set branch status to Process or DoNotProcess.
Definition TTree.cxx:8524
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:5628
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=nullptr)
Change branch address, dealing with clone trees properly.
Definition TTree.cxx:8375
virtual Long64_t GetEntriesFast() const
Return a number greater or equal to the total number of entries in the dataset.
Definition TTree.h:505
virtual void ResetBranchAddresses()
Tell all of our branches to drop their current objects and allocate new ones.
Definition TTree.cxx:8065
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...
TUnfoldBinning const * FindNode(char const *name) const
traverse the tree and return the first node which matches the given name
TXMLDocument contains a pointer to an xmlDoc structure, after the parser returns a tree built during ...