ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TSVDUnfoldExample.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_math
3 /// Data unfolding using Singular Value Decomposition
4 ///
5 /// TSVDUnfold example
6 ///
7 /// Data unfolding using Singular Value Decomposition (hep-ph/9509307)
8 ///
9 /// Example distribution and smearing model from Tim Adye (RAL)
10 ///
11 /// \macro_image
12 /// \macro_code
13 ///
14 /// \authors Kerstin Tackmann, Andreas Hoecker, Heiko Lacker
15 
16 #include <iostream>
17 
18 #include "TROOT.h"
19 #include "TSystem.h"
20 #include "TStyle.h"
21 #include "TRandom3.h"
22 #include "TString.h"
23 #include "TMath.h"
24 #include "TH1D.h"
25 #include "TH2D.h"
26 #include "TLegend.h"
27 #include "TCanvas.h"
28 #include "TColor.h"
29 #include "TLine.h"
30 
31 #if not defined(__CINT__) || defined(__MAKECINT__)
32 #include "TSVDUnfold.h"
33 #endif
34 
35 Double_t Reconstruct( Double_t xt, TRandom3& R )
36 {
37  // apply some Gaussian smearing + bias and efficiency corrections to fake reconstruction
38  const Double_t cutdummy = -99999.0;
39  Double_t xeff = 0.3 + (1.0 - 0.3)/20.0*(xt + 10.0); // efficiency
40  Double_t x = R.Rndm();
41  if (x > xeff) return cutdummy;
42  else {
43  Double_t xsmear= R.Gaus(-2.5,0.2); // bias and smear
44  return xt+xsmear;
45  }
46 }
47 
48 void TSVDUnfoldExample()
49 {
50  gROOT->Reset();
51  gROOT->SetStyle("Plain");
52  gStyle->SetOptStat(0);
53 
54  TRandom3 R;
55 
56  const Double_t cutdummy= -99999.0;
57 
58  // --- Data/MC toy generation -----------------------------------
59 
60  // The MC input
61  Int_t nbins = 40;
62  TH1D *xini = new TH1D("xini", "MC truth", nbins, -10.0, 10.0);
63  TH1D *bini = new TH1D("bini", "MC reco", nbins, -10.0, 10.0);
64  TH2D *Adet = new TH2D("Adet", "detector response", nbins, -10.0, 10.0, nbins, -10.0, 10.0);
65 
66  // Data
67  TH1D *data = new TH1D("data", "data", nbins, -10.0, 10.0);
68  // Data "truth" distribution to test the unfolding
69  TH1D *datatrue = new TH1D("datatrue", "data truth", nbins, -10.0, 10.0);
70  // Statistical covariance matrix
71  TH2D *statcov = new TH2D("statcov", "covariance matrix", nbins, -10.0, 10.0, nbins, -10.0, 10.0);
72 
73  // Fill the MC using a Breit-Wigner, mean 0.3 and width 2.5.
74  for (Int_t i= 0; i<100000; i++) {
75  Double_t xt = R.BreitWigner(0.3, 2.5);
76  xini->Fill(xt);
77  Double_t x = Reconstruct( xt, R );
78  if (x != cutdummy) {
79  Adet->Fill(x, xt);
80  bini->Fill(x);
81  }
82  }
83 
84  // Fill the "data" with a Gaussian, mean 0 and width 2.
85  for (Int_t i=0; i<10000; i++) {
86  Double_t xt = R.Gaus(0.0, 2.0);
87  datatrue->Fill(xt);
88  Double_t x = Reconstruct( xt, R );
89  if (x != cutdummy)
90  data->Fill(x);
91  }
92 
93  cout << "Created toy distributions and errors for: " << endl;
94  cout << "... \"true MC\" and \"reconstructed (smeared) MC\"" << endl;
95  cout << "... \"true data\" and \"reconstructed (smeared) data\"" << endl;
96  cout << "... the \"detector response matrix\"" << endl;
97 
98  // Fill the data covariance matrix
99  for (int i=1; i<=data->GetNbinsX(); i++) {
100  statcov->SetBinContent(i,i,data->GetBinError(i)*data->GetBinError(i));
101  }
102 
103  // --- Here starts the actual unfolding -------------------------
104 
105  // Create TSVDUnfold object and initialise
106  TSVDUnfold *tsvdunf = new TSVDUnfold( data, statcov, bini, xini, Adet );
107 
108  // It is possible to normalise unfolded spectrum to unit area
109  tsvdunf->SetNormalize( kFALSE ); // no normalisation here
110 
111  // Perform the unfolding with regularisation parameter kreg = 13
112  // - the larger kreg, the finer grained the unfolding, but the more fluctuations occur
113  // - the smaller kreg, the stronger is the regularisation and the bias
114  TH1D* unfres = tsvdunf->Unfold( 13 );
115 
116  // Get the distribution of the d to cross check the regularization
117  // - choose kreg to be the point where |d_i| stop being statistically significantly >>1
118  TH1D* ddist = tsvdunf->GetD();
119 
120  // Get the distribution of the singular values
121  TH1D* svdist = tsvdunf->GetSV();
122 
123  // Compute the error matrix for the unfolded spectrum using toy MC
124  // using the measured covariance matrix as input to generate the toys
125  // 100 toys should usually be enough
126  // The same method can be used for different covariance matrices separately.
127  TH2D* ustatcov = tsvdunf->GetUnfoldCovMatrix( statcov, 100 );
128 
129  // Now compute the error matrix on the unfolded distribution originating
130  // from the finite detector matrix statistics
131  TH2D* uadetcov = tsvdunf->GetAdetCovMatrix( 100 );
132 
133  // Sum up the two (they are uncorrelated)
134  ustatcov->Add( uadetcov );
135 
136  //Get the computed regularized covariance matrix (always corresponding to total uncertainty passed in constructor) and add uncertainties from finite MC statistics.
137  TH2D* utaucov = tsvdunf->GetXtau();
138  utaucov->Add( uadetcov );
139 
140  //Get the computed inverse of the covariance matrix
141  TH2D* uinvcov = tsvdunf->GetXinv();
142 
143 
144  // --- Only plotting stuff below ------------------------------
145 
146  for (int i=1; i<=unfres->GetNbinsX(); i++) {
147  unfres->SetBinError(i, TMath::Sqrt(utaucov->GetBinContent(i,i)));
148  }
149 
150  // Renormalize just to be able to plot on the same scale
151  xini->Scale(0.7*datatrue->Integral()/xini->Integral());
152 
153  TLegend *leg = new TLegend(0.58,0.68,0.99,0.88);
154  leg->SetBorderSize(0);
155  leg->SetFillColor(0);
156  leg->SetFillStyle(0);
157  leg->AddEntry(unfres,"Unfolded Data","p");
158  leg->AddEntry(datatrue,"True Data","l");
159  leg->AddEntry(data,"Reconstructed Data","l");
160  leg->AddEntry(xini,"True MC","l");
161 
162  TCanvas *c1 = new TCanvas( "c1", "Unfolding toy example with TSVDUnfold", 800, 700 );
163 
164  // --- Style settings -----------------------------------------
165  Int_t c_Canvas = TColor::GetColor( "#f0f0f0" );
166  Int_t c_FrameFill = TColor::GetColor( "#fffffd" );
167  Int_t c_TitleBox = TColor::GetColor( "#6D7B8D" );
168  Int_t c_TitleText = TColor::GetColor( "#FFFFFF" );
169 
170  c1->SetFrameFillColor( c_FrameFill );
171  c1->SetFillColor ( c_Canvas );
172  c1->Divide(1,2);
173  TVirtualPad * c11 = c1->cd(1);
174  c11->SetFrameFillColor( c_FrameFill );
175  c11->SetFillColor ( c_Canvas );
176 
177  gStyle->SetTitleFillColor( c_TitleBox );
178  gStyle->SetTitleTextColor( c_TitleText );
180  gStyle->SetTitleH( 0.052 );
181  gStyle->SetTitleX( c1->GetLeftMargin() );
182  gStyle->SetTitleY( 1 - c1->GetTopMargin() + gStyle->GetTitleH() );
183  gStyle->SetTitleW( 1 - c1->GetLeftMargin() - c1->GetRightMargin() );
184 
185  TH1D* frame = new TH1D( *unfres );
186  frame->SetTitle( "Unfolding toy example with TSVDUnfold" );
187  frame->GetXaxis()->SetTitle( "x variable" );
188  frame->GetYaxis()->SetTitle( "Events" );
189  frame->GetXaxis()->SetTitleOffset( 1.25 );
190  frame->GetYaxis()->SetTitleOffset( 1.29 );
191  frame->Draw();
192 
193  data->SetLineStyle(2);
194  data->SetLineColor(4);
195  data->SetLineWidth(2);
196  unfres->SetMarkerStyle(20);
197  datatrue->SetLineColor(2);
198  datatrue->SetLineWidth(2);
199  xini->SetLineStyle(2);
200  xini->SetLineColor(8);
201  xini->SetLineWidth(2);
202  // ------------------------------------------------------------
203 
204  // add histograms
205  unfres->Draw("same");
206  datatrue->Draw("same");
207  data->Draw("same");
208  xini->Draw("same");
209 
210  leg->Draw();
211 
212  // covariance matrix
213  gStyle->SetPalette(1,0);
214  TVirtualPad * c12 = c1->cd(2);
215  c12->Divide(2,1);
216  TVirtualPad * c2 = c12->cd(1);
217  c2->SetFrameFillColor( c_FrameFill );
218  c2->SetFillColor ( c_Canvas );
219  c2->SetRightMargin ( 0.15 );
220 
221  TH2D* covframe = new TH2D( *ustatcov );
222  covframe->SetTitle( "TSVDUnfold covariance matrix" );
223  covframe->GetXaxis()->SetTitle( "x variable" );
224  covframe->GetYaxis()->SetTitle( "x variable" );
225  covframe->GetXaxis()->SetTitleOffset( 1.25 );
226  covframe->GetYaxis()->SetTitleOffset( 1.29 );
227  covframe->Draw();
228 
229  ustatcov->SetLineWidth( 2 );
230  ustatcov->Draw( "colzsame" );
231 
232  // distribution of the d quantity
233  TVirtualPad * c3 = c12->cd(2);
234  c3->SetFrameFillColor( c_FrameFill );
235  c3->SetFillColor ( c_Canvas );
236  c3->SetLogy();
237 
238  TLine *line = new TLine( 0.,1.,40.,1. );
239  line->SetLineStyle(2);
240 
241  TH1D* dframe = new TH1D( *ddist );
242  dframe->SetTitle( "TSVDUnfold |d_{i}|" );
243  dframe->GetXaxis()->SetTitle( "i" );
244  dframe->GetYaxis()->SetTitle( "|d_{i}|" );
245  dframe->GetXaxis()->SetTitleOffset( 1.25 );
246  dframe->GetYaxis()->SetTitleOffset( 1.29 );
247  dframe->SetMinimum( 0.001 );
248  dframe->Draw();
249 
250  ddist->SetLineWidth( 2 );
251  ddist->Draw( "same" );
252  line->Draw();
253 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:245
SVD Approach to Data Unfolding.
Definition: TSVDUnfold.h:54
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition: TH1.cxx:6174
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
Random number generator class based on M.
Definition: TRandom3.h:29
virtual void SetLogy(Int_t value=1)=0
void SetTitleY(Float_t y=0.985)
Definition: TStyle.h:408
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:35
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:235
void SetTitleW(Float_t w=0)
Definition: TStyle.h:409
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:373
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
TH1D * Unfold(Int_t kreg)
Perform the unfolding with regularisation parameter kreg.
Definition: TSVDUnfold.cxx:243
TH2D * GetXinv() const
Returns the computed inverse of the covariance matrix.
Definition: TSVDUnfold.cxx:610
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:395
#define gROOT
Definition: TROOT.h:344
TH2D * GetUnfoldCovMatrix(const TH2D *cov, Int_t ntoys, Int_t seed=1)
Determine for given input error matrix covariance matrix of unfolded spectrum from toy simulation giv...
Definition: TSVDUnfold.cxx:411
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:254
virtual void SetFillStyle(Style_t fstyle)
Definition: TAttFill.h:52
virtual Int_t GetNbinsX() const
Definition: TH1.h:296
virtual TVirtualPad * cd(Int_t subpadnumber=0)=0
void SetTitleBorderSize(Width_t size=2)
Definition: TStyle.h:402
Float_t GetTopMargin() const
Definition: TAttPad.h:56
void SetNormalize(Bool_t normalize)
Definition: TSVDUnfold.h:74
Float_t GetRightMargin() const
Definition: TAttPad.h:55
TH1D * GetSV() const
Returns singular values vector.
Definition: TSVDUnfold.cxx:593
void SetTitleX(Float_t x=0)
Definition: TStyle.h:407
TH1D * GetD() const
Returns d vector (for choosing appropriate regularisation)
Definition: TSVDUnfold.cxx:582
void SetTitleTextColor(Color_t color=1)
Definition: TStyle.h:399
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom3.cxx:94
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:59
virtual void SetBinError(Int_t bin, Double_t error)
see convention for numbering bins in TH1::GetBin
Definition: TH1.cxx:8528
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH2.h:90
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
TH2D * GetXtau() const
Returns the computed regularized covariance matrix corresponding to total uncertainties on measured s...
Definition: TSVDUnfold.cxx:602
virtual Double_t Integral(Option_t *option="") const
Return integral of bin contents.
Definition: TH1.cxx:7378
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
TH2D * GetAdetCovMatrix(Int_t ntoys, Int_t seed=1)
Determine covariance matrix of unfolded spectrum from finite statistics in response matrix using pseu...
Definition: TSVDUnfold.cxx:517
A simple line.
Definition: TLine.h:41
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb"...
Definition: TColor.cxx:1023
virtual void SetMarkerStyle(Style_t mstyle=1)
Definition: TAttMarker.h:53
TAxis * GetYaxis()
Definition: TH1.h:320
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:613
void SetTitleH(Float_t h=0)
Definition: TStyle.h:410
Float_t GetLeftMargin() const
Definition: TAttPad.h:54
The Canvas class.
Definition: TCanvas.h:48
double Double_t
Definition: RtypesCore.h:55
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:280
virtual Double_t BreitWigner(Double_t mean=0, Double_t gamma=1)
Return a number distributed following a BreitWigner function with mean and gamma. ...
Definition: TRandom.cxx:186
virtual void SetLineStyle(Style_t lstyle)
Definition: TAttLine.h:56
virtual Bool_t Add(TF1 *h1, Double_t c1=1, Option_t *option="")
Performs the operation: this = this + c1*f1 if errors are defined (see TH1::Sumw2), errors are also recalculated.
Definition: TH1.cxx:780
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)=0
virtual void SetRightMargin(Float_t rightmargin)
Set Pad right margin in fraction of the pad width.
Definition: TAttPad.cxx:117
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1073
void SetTitleFillColor(Color_t color=1)
Definition: TStyle.h:398
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1252
void SetFrameFillColor(Color_t color=1)
Definition: TAttPad.h:83
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6268
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content.
Definition: TH2.cxx:2699
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
virtual Double_t GetBinError(Int_t bin) const
Return value of error associated to bin number bin.
Definition: TH1.cxx:8395
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:287
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
void SetPalette(Int_t ncolors=kBird, Int_t *colors=0, Float_t alpha=1.)
See TColor::SetPalette.
Definition: TStyle.cxx:1445
TRandom3 R
a TMatrixD.
Definition: testIO.cxx:28
virtual void SetBorderSize(Int_t bordersize=4)
Definition: TPave.h:82
TAxis * GetXaxis()
Definition: TH1.h:319
Float_t GetTitleH() const
Definition: TStyle.h:291
2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:297