Logo ROOT   6.07/09
Reference Guide
testUnfold5c.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_unfold5
3 /// \notebook -nodraw
4 /// Version 17.0 example for multi-dimensional unfolding
5 ///
6 /// \macro_output
7 /// \macro_code
8 ///
9 /// \author Stefan Schmitt, DESY
10 
11 #include <iostream>
12 #include <map>
13 #include <cmath>
14 #include <TMath.h>
15 #include <TFile.h>
16 #include <TTree.h>
17 #include <TH1.h>
18 #include "TUnfoldBinning.h"
19 
20 using namespace std;
21 
22 void testUnfold5c()
23 {
24  // switch on histogram errors
26 
27  //=======================================================
28  // Step 1: open file to save histograms and binning schemes
29 
30  TFile *outputFile=new TFile("testUnfold5_histograms.root","recreate");
31 
32  //=======================================================
33  // Step 2: read binning from file
34  // and save them to output file
35 
36  TFile *binningSchemes=new TFile("testUnfold5_binning.root");
37 
38  TUnfoldBinning *detectorBinning,*generatorBinning;
39 
40  outputFile->cd();
41 
42  binningSchemes->GetObject("detector",detectorBinning);
43  binningSchemes->GetObject("generator",generatorBinning);
44 
45  delete binningSchemes;
46 
47  detectorBinning->Write();
48  generatorBinning->Write();
49 
50  if(detectorBinning) {
51  detectorBinning->PrintStream(cout);
52  } else {
53  cout<<"could not read 'detector' binning\n";
54  }
55  if(generatorBinning) {
56  generatorBinning->PrintStream(cout);
57  } else {
58  cout<<"could not read 'generator' binning\n";
59  }
60 
61  // pointers to various nodes in the bining scheme
62  const TUnfoldBinning *detectordistribution=
63  detectorBinning->FindNode("detectordistribution");
64 
65  const TUnfoldBinning *signalBinning=
66  generatorBinning->FindNode("signal");
67 
68  const TUnfoldBinning *bgrBinning=
69  generatorBinning->FindNode("background");
70 
71  // write binnig schemes to output file
72 
73  //=======================================================
74  // Step 3: book and fill data histograms
75 
76  Float_t etaRec,ptRec,discr,etaGen,ptGen;
77  Int_t istriggered,issignal;
78 
79  outputFile->cd();
80 
81  TH1 *histDataReco=detectorBinning->CreateHistogram("histDataReco");
82  TH1 *histDataTruth=generatorBinning->CreateHistogram("histDataTruth");
83 
84  TFile *dataFile=new TFile("testUnfold5_data.root");
85  TTree *dataTree=(TTree *) dataFile->Get("data");
86 
87  if(!dataTree) {
88  cout<<"could not read 'data' tree\n";
89  }
90 
91  dataTree->ResetBranchAddresses();
92  dataTree->SetBranchAddress("etarec",&etaRec);
93  dataTree->SetBranchAddress("ptrec",&ptRec);
94  dataTree->SetBranchAddress("discr",&discr);
95  // for real data, only the triggered events are available
96  dataTree->SetBranchAddress("istriggered",&istriggered);
97  // data truth parameters
98  dataTree->SetBranchAddress("etagen",&etaGen);
99  dataTree->SetBranchAddress("ptgen",&ptGen);
100  dataTree->SetBranchAddress("issignal",&issignal);
101  dataTree->SetBranchStatus("*",1);
102 
103 
104  cout<<"loop over data events\n";
105 
106  for(Int_t ievent=0;ievent<dataTree->GetEntriesFast();ievent++) {
107  if(dataTree->GetEntry(ievent)<=0) break;
108  // fill histogram with reconstructed quantities
109  if(istriggered) {
110  Int_t binNumber=
111  detectordistribution->GetGlobalBinNumber(ptRec,etaRec,discr);
112  histDataReco->Fill(binNumber);
113  }
114  // fill histogram with data truth parameters
115  if(issignal) {
116  // signal has true eta and pt
117  Int_t binNumber=signalBinning->GetGlobalBinNumber(ptGen,etaGen);
118  histDataTruth->Fill(binNumber);
119  } else {
120  // background only has reconstructed pt and eta
121  Int_t binNumber=bgrBinning->GetGlobalBinNumber(ptRec,etaRec);
122  histDataTruth->Fill(binNumber);
123  }
124  }
125 
126  delete dataTree;
127  delete dataFile;
128 
129  //=======================================================
130  // Step 4: book and fill histogram of migrations
131  // it receives events from both signal MC and background MC
132 
133  outputFile->cd();
134 
136  (generatorBinning,detectorBinning,"histMCGenRec");
137 
138  TFile *signalFile=new TFile("testUnfold5_signal.root");
139  TTree *signalTree=(TTree *) signalFile->Get("signal");
140 
141  if(!signalTree) {
142  cout<<"could not read 'signal' tree\n";
143  }
144 
145  signalTree->ResetBranchAddresses();
146  signalTree->SetBranchAddress("etarec",&etaRec);
147  signalTree->SetBranchAddress("ptrec",&ptRec);
148  signalTree->SetBranchAddress("discr",&discr);
149  signalTree->SetBranchAddress("istriggered",&istriggered);
150  signalTree->SetBranchAddress("etagen",&etaGen);
151  signalTree->SetBranchAddress("ptgen",&ptGen);
152  signalTree->SetBranchStatus("*",1);
153 
154  cout<<"loop over MC signal events\n";
155 
156  for(Int_t ievent=0;ievent<signalTree->GetEntriesFast();ievent++) {
157  if(signalTree->GetEntry(ievent)<=0) break;
158 
159  // bin number on generator level for signal
160  Int_t genBin=signalBinning->GetGlobalBinNumber(ptGen,etaGen);
161 
162  // bin number on reconstructed level
163  // bin number 0 corresponds to non-reconstructed events
164  Int_t recBin=0;
165  if(istriggered) {
166  recBin=detectordistribution->GetGlobalBinNumber(ptRec,etaRec,discr);
167  }
168  histMCGenRec->Fill(genBin,recBin);
169  }
170 
171  delete signalTree;
172  delete signalFile;
173 
174  TFile *bgrFile=new TFile("testUnfold5_background.root");
175  TTree *bgrTree=(TTree *) bgrFile->Get("background");
176 
177  if(!bgrTree) {
178  cout<<"could not read 'background' tree\n";
179  }
180 
181  bgrTree->ResetBranchAddresses();
182  bgrTree->SetBranchAddress("etarec",&etaRec);
183  bgrTree->SetBranchAddress("ptrec",&ptRec);
184  bgrTree->SetBranchAddress("discr",&discr);
185  bgrTree->SetBranchAddress("istriggered",&istriggered);
186  bgrTree->SetBranchStatus("*",1);
187 
188  cout<<"loop over MC background events\n";
189 
190  for(Int_t ievent=0;ievent<bgrTree->GetEntriesFast();ievent++) {
191  if(bgrTree->GetEntry(ievent)<=0) break;
192 
193  // here, for background only reconstructed quantities are known
194  // and only the reconstructed events are relevant
195  if(istriggered) {
196  // bin number on generator level for background
197  Int_t genBin=bgrBinning->GetGlobalBinNumber(ptRec,etaRec);
198  // bin number on reconstructed level
199  Int_t recBin=detectordistribution->GetGlobalBinNumber
200  (ptRec,etaRec,discr);
201  histMCGenRec->Fill(genBin,recBin);
202  }
203  }
204 
205  delete bgrTree;
206  delete bgrFile;
207 
208  outputFile->Write();
209  delete outputFile;
210 
211 }
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:830
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3127
virtual void SetBranchStatus(const char *bname, Bool_t status=1, UInt_t *found=0)
Set branch status to Process or DoNotProcess.
Definition: TTree.cxx:7865
float Float_t
Definition: RtypesCore.h:53
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition: TTree.cxx:5210
int Int_t
Definition: RtypesCore.h:41
STL namespace.
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:5969
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7719
TH1 * CreateHistogram(const char *histogramName, Bool_t originalAxisBinning=kFALSE, Int_t **binMap=0, const char *histogramTitle=0, const char *axisSteering=0) const
void GetObject(const char *namecycle, T *&ptr)
Service class for 2-Dim histogram classes.
Definition: TH2.h:36
virtual Int_t Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0)
Write memory objects to this file.
Definition: TFile.cxx:2268
Int_t GetGlobalBinNumber(Double_t x) const
static TH2D * CreateHistogramOfMigrations(TUnfoldBinning const *xAxis, TUnfoldBinning const *yAxis, char const *histogramName, Bool_t originalXAxisBinning=kFALSE, Bool_t originalYAxisBinning=kFALSE, char const *histogramTitle=0)
void PrintStream(std::ostream &out, Int_t indent=0) const
This class serves as a container of analysis bins analysis bins are specified by defining the axes of...
virtual Long64_t GetEntriesFast() const
Definition: TTree.h:394
virtual void ResetBranchAddresses()
Tell all of our branches to drop their current objects and allocate new ones.
Definition: TTree.cxx:7475
The TH1 histogram class.
Definition: TH1.h:80
TUnfoldBinning const * FindNode(char const *name) const
A TTree object has a header with a name and a title.
Definition: TTree.h:98
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:292