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 outputFile->cd();
114
115 // read binning schemes in XML format
116
117 TDOMParser parser;
118 TString dir = gSystem->UnixPathName(gSystem->GetDirName(__FILE__));
119 Int_t error = parser.ParseFile(dir+"/testUnfold7binning.xml");
120 if(error) {
121 cout<<"error="<<error<<" from TDOMParser\n";
122 cout<<"==============================================================\n";
123 cout<<"Maybe the file testUnfold7binning.xml is missing?\n";
124 cout<<"The content of the file is included in the comments section\n";
125 cout<<"of this macro \"testUnfold7b.C\"\n";
126 cout<<"==============================================================\n";
127 }
128 TXMLDocument const *XMLdocument=parser.GetXMLDocument();
129 fineBinningRoot=
130 TUnfoldBinningXML::ImportXML(XMLdocument,"fine");
131 coarseBinningRoot=
132 TUnfoldBinningXML::ImportXML(XMLdocument,"coarse");
133
134 // write binning schemes to output file
135 fineBinningRoot->Write();
136 coarseBinningRoot->Write();
137
138 if(fineBinningRoot) {
139 fineBinningRoot->PrintStream(cout);
140 } else {
141 cout<<"could not read 'detector' binning\n";
142 }
143 if(coarseBinningRoot) {
144 coarseBinningRoot->PrintStream(cout);
145 } else {
146 cout<<"could not read 'generator' binning\n";
147 }
148
149 TUnfoldBinning const *fineBinning=fineBinningRoot;//->FindNode("ptfine");
150 TUnfoldBinning const *coarseBinning=coarseBinningRoot;//->FindNode("ptcoarse");
151
152
153 //=======================================================
154 // Step 3: book and fill data histograms
155
156 Float_t ptRec[3],ptGen[3],weight;
157 Int_t isTriggered,isSignal;
158
159 outputFile->cd();
160
161 TH1 *histDataRecF=fineBinning->CreateHistogram("histDataRecF");
162 TH1 *histDataRecC=coarseBinning->CreateHistogram("histDataRecC");
163 TH1 *histDataBgrF=fineBinning->CreateHistogram("histDataBgrF");
164 TH1 *histDataBgrC=coarseBinning->CreateHistogram("histDataBgrC");
165 TH1 *histDataGen=coarseBinning->CreateHistogram("histDataGen");
166
167 TFile *dataFile=new TFile("testUnfold7_data.root");
168 TTree *dataTree=(TTree *) dataFile->Get("data");
169
170 if(!dataTree) {
171 cout<<"could not read 'data' tree\n";
172 }
173
174 dataTree->ResetBranchAddresses();
175 dataTree->SetBranchAddress("ptrec",ptRec);
176 //dataTree->SetBranchAddress("discr",&discr);
177 // for real data, only the triggered events are available
178 dataTree->SetBranchAddress("istriggered",&isTriggered);
179 // data truth parameters
180 dataTree->SetBranchAddress("ptgen",ptGen);
181 dataTree->SetBranchAddress("issignal",&isSignal);
182 dataTree->SetBranchStatus("*",1);
183
184 cout<<"loop over data events\n";
185
186#define VAR_REC (ptRec[2])
187#define VAR_GEN (ptGen[2])
188
189 for(Int_t ievent=0;ievent<dataTree->GetEntriesFast();ievent++) {
190 if(dataTree->GetEntry(ievent)<=0) break;
191 // fill histogram with reconstructed quantities
192 if(isTriggered) {
193 int binF=fineBinning->GetGlobalBinNumber(VAR_REC);
194 int binC=coarseBinning->GetGlobalBinNumber(VAR_REC);
195 histDataRecF->Fill(binF);
196 histDataRecC->Fill(binC);
197 if(!isSignal) {
198 histDataBgrF->Fill(binF);
199 histDataBgrC->Fill(binC);
200 }
201 }
202 // fill histogram with data truth parameters
203 if(isSignal) {
204 int binGen=coarseBinning->GetGlobalBinNumber(VAR_GEN);
205 histDataGen->Fill(binGen);
206 }
207 }
208
209 delete dataTree;
210 delete dataFile;
211
212 //=======================================================
213 // Step 4: book and fill histogram of migrations
214 // it receives events from both signal MC and background MC
215
216 outputFile->cd();
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
226 TFile *signalFile=new TFile("testUnfold7_signal.root");
227 TTree *signalTree=(TTree *) signalFile->Get("signal");
228
229 if(!signalTree) {
230 cout<<"could not read 'signal' tree\n";
231 }
232
233 signalTree->ResetBranchAddresses();
234 signalTree->SetBranchAddress("ptrec",&ptRec);
235 //signalTree->SetBranchAddress("discr",&discr);
236 signalTree->SetBranchAddress("istriggered",&isTriggered);
237 signalTree->SetBranchAddress("ptgen",&ptGen);
238 signalTree->SetBranchAddress("weight",&weight);
239 signalTree->SetBranchStatus("*",1);
240
241 cout<<"loop over MC signal events\n";
242
243 for(Int_t ievent=0;ievent<signalTree->GetEntriesFast();ievent++) {
244 if(signalTree->GetEntry(ievent)<=0) break;
245
246 int binC=0,binF=0;
247 if(isTriggered) {
248 binF=fineBinning->GetGlobalBinNumber(VAR_REC);
249 binC=coarseBinning->GetGlobalBinNumber(VAR_REC);
250 }
251 int binGen=coarseBinning->GetGlobalBinNumber(VAR_GEN);
252 histMcsigGenRecF->Fill(binGen,binF,weight);
253 histMcsigGenRecC->Fill(binGen,binC,weight);
254 histMcsigRecF->Fill(binF,weight);
255 histMcsigRecC->Fill(binC,weight);
256 histMcsigGen->Fill(binGen,weight);
257 }
258
259 delete signalTree;
260 delete signalFile;
261
262 outputFile->cd();
263
264 TH1 *histMcbgrRecF=fineBinning->CreateHistogram("histMcbgrRecF");
265 TH1 *histMcbgrRecC=coarseBinning->CreateHistogram("histMcbgrRecC");
266
267 TFile *bgrFile=new TFile("testUnfold7_background.root");
268 TTree *bgrTree=(TTree *) bgrFile->Get("background");
269
270 if(!bgrTree) {
271 cout<<"could not read 'background' tree\n";
272 }
273
274 bgrTree->ResetBranchAddresses();
275 bgrTree->SetBranchAddress("ptrec",&ptRec);
276 //bgrTree->SetBranchAddress("discr",&discr);
277 bgrTree->SetBranchAddress("istriggered",&isTriggered);
278 bgrTree->SetBranchAddress("weight",&weight);
279 bgrTree->SetBranchStatus("*",1);
280
281 cout<<"loop over MC background events\n";
282
283 for(Int_t ievent=0;ievent<bgrTree->GetEntriesFast();ievent++) {
284 if(bgrTree->GetEntry(ievent)<=0) break;
285
286 // here, for background only reconstructed quantities are known
287 // and only the reconstructed events are relevant
288 if(isTriggered) {
289 int binF=fineBinning->GetGlobalBinNumber(VAR_REC);
290 int binC=coarseBinning->GetGlobalBinNumber(VAR_REC);
291 histMcbgrRecF->Fill(binF,weight);
292 histMcbgrRecC->Fill(binC,weight);
293 }
294 }
295
296 delete bgrTree;
297 delete bgrFile;
298
299 outputFile->Write();
300 delete outputFile;
301
302}
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
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 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:2429
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:3344
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:6671
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:880
Basic string class.
Definition TString.h:139
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1063
virtual TString GetDirName(const char *pathname)
Return the directory name in pathname.
Definition TSystem.cxx:1032
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:8534
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:5638
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=nullptr)
Change branch address, dealing with clone trees properly.
Definition TTree.cxx:8385
virtual Long64_t GetEntriesFast() const
Definition TTree.h:465
virtual void ResetBranchAddresses()
Tell all of our branches to drop their current objects and allocate new ones.
Definition TTree.cxx:8075
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 ...