1 |
#include <vector>
|
2 |
using namespace std;
|
3 |
#include <TROOT.h>
|
4 |
#include <TChain.h>
|
5 |
#include <TFile.h>
|
6 |
#include <TTree.h>
|
7 |
#include <TH1.h>
|
8 |
#include <TH2.h>
|
9 |
#include <THStack.h>
|
10 |
#include <TStyle.h>
|
11 |
#include <TCanvas.h>
|
12 |
#include <TMath.h>
|
13 |
#include <TMultiGraph.h>
|
14 |
#include <TGraphErrors.h>
|
15 |
#include <TLegend.h>
|
16 |
#include <TPaveStats.h>
|
17 |
#include "Math/GSLIntegrator.h"
|
18 |
#include "Math/WrappedTF1.h"
|
19 |
|
20 |
TString fileName = "QCDbin4.root";
|
21 |
// Histo range
|
22 |
double xMin = 0.0;
|
23 |
double xMax = 2.0;
|
24 |
// fit region weight
|
25 |
double wxmin = 2.1;
|
26 |
double wxmax = 0.8;
|
27 |
// Select fitting function:
|
28 |
TString s0 = "landau";
|
29 |
// Select New or Old reliso:
|
30 |
TString isoname = "New";
|
31 |
|
32 |
void fitLandau()
|
33 |
{
|
34 |
gStyle->SetOptFit(01111);
|
35 |
|
36 |
TFile *file = TFile::Open(fileName);
|
37 |
TTree *tree = (TTree*)file->Get("myTree");
|
38 |
TH1D *h0 = (TH1D*)file->Get("histos/h_RelIso_all");
|
39 |
TH1D *h1 = (TH1D*)file->Get("histos/h_RelIso_qcd");
|
40 |
Int_t jbin;
|
41 |
Double_t na;
|
42 |
tree->SetBranchAddress("jbin",&jbin);
|
43 |
tree->SetBranchAddress("na",&na);
|
44 |
// Int_t nentries = (Int_t)tree->GetEntries();
|
45 |
tree->GetEntry(0);
|
46 |
// define fit region:
|
47 |
double ax0 = 0.;
|
48 |
double axf = 0.;
|
49 |
double bx0= 0.;
|
50 |
double bxf = 0.;
|
51 |
|
52 |
cout<<"Use "<<isoname<<" RelIso"<<endl;
|
53 |
|
54 |
if ( isoname.Contains("Old") ) {
|
55 |
// old
|
56 |
ax0 = 0.95;
|
57 |
axf = 1.0;
|
58 |
bx0 = 0.2;
|
59 |
bxf = 0.8;
|
60 |
}
|
61 |
else if ( isoname.Contains("New") ) {
|
62 |
// new
|
63 |
ax0 = 0.0;
|
64 |
axf = 0.053;
|
65 |
bx0 = 0.4;
|
66 |
bxf = 2.0;
|
67 |
}
|
68 |
|
69 |
int niter = 1;
|
70 |
double pass[3] = {0.,0.,0.};
|
71 |
double ns, ns2;
|
72 |
ns = ns2 = 0.;
|
73 |
|
74 |
TCanvas *cv = new TCanvas("cv","cv",800,600);
|
75 |
|
76 |
// Fit QCD in background region
|
77 |
cout<<"Fitting with "<<s0<<" function!"<<endl;
|
78 |
TH1D *h_all = (TH1D*)h0->Clone();
|
79 |
TH1D *h_qcd = (TH1D*)h1->Clone();
|
80 |
h_all->SetLineWidth(2);
|
81 |
h_all->SetMinimum(0);
|
82 |
if( jbin == 4)
|
83 |
h_all->SetTitle(isoname+" RelIso distribution (#geq 4 jets)");
|
84 |
else if ( jbin < 4 ) {
|
85 |
TString title = isoname+" RelIso distribution (";
|
86 |
title += jbin;
|
87 |
title += " jets)";
|
88 |
h_all->SetTitle(title);
|
89 |
}
|
90 |
else{
|
91 |
cout << "Wrong bin number!" << endl;
|
92 |
}
|
93 |
h_all->Draw();
|
94 |
|
95 |
cout << "\n>>>>> Iteration step: "<< niter << endl;
|
96 |
h_all->Fit(s0,"0","",bx0,bxf);
|
97 |
|
98 |
// Very first fit function
|
99 |
TF1 *f0 = (TF1*)h_all->GetFunction(s0);
|
100 |
Int_t npar = f0->GetNpar();
|
101 |
Double_t chi2 = f0->GetChisquare();
|
102 |
Int_t ndof = f0->GetNDF();
|
103 |
pass[0] = chi2/ndof;
|
104 |
pass[1] = pass[0];
|
105 |
cout << ">> Norm chi2 = " << pass[0] << endl;
|
106 |
|
107 |
bx0 = f0->GetParameter(1) - wxmin*f0->GetParameter(2);
|
108 |
bxf = f0->GetParameter(1) + wxmax*f0->GetParameter(2);
|
109 |
|
110 |
double * par0 = new double [npar];
|
111 |
double * parErr0 = new double [npar];
|
112 |
|
113 |
double delta = pass[1]-pass[2];
|
114 |
|
115 |
while (niter <= 20 && abs(pass[1]-pass[2]) > 0.00001) {
|
116 |
if (niter > 1)
|
117 |
pass[1]=pass[2];
|
118 |
if (delta >= 0){
|
119 |
niter++;
|
120 |
cout << "\n>>>>> Iteration step: "<< niter << endl;
|
121 |
cout << ">> Pass[1] = " << pass[1] << endl;
|
122 |
|
123 |
h_all->Fit(s0,"0","",bx0,bxf);
|
124 |
TF1 *fi = (TF1*)h_all->GetFunction(s0);
|
125 |
pass[2] = fi->GetChisquare()/fi->GetNDF();
|
126 |
cout << ">> Norm chi2 = pass[2] = " << pass[2] << endl;
|
127 |
delta = pass[1]-pass[2];
|
128 |
cout << ">> Delta = " << delta << endl;
|
129 |
|
130 |
if (delta >= 0) {
|
131 |
printf("Function has %i parameters. Chisquare = %g\n",
|
132 |
npar,
|
133 |
fi->GetChisquare());
|
134 |
for (Int_t i=0;i<npar;i++) {
|
135 |
printf("%s = %g +- %g\n",
|
136 |
fi->GetParName(i),
|
137 |
fi->GetParameter(i),
|
138 |
fi->GetParError(i)
|
139 |
);
|
140 |
par0[i] = fi->GetParameter(i);
|
141 |
parErr0[i] = fi->GetParError(i);
|
142 |
// cout<<"Par["<<i<<"]="<<par0[i]<<"; Error="<<parErr0[i]<<endl;
|
143 |
}
|
144 |
bx0 = fi->GetParameter(1) - wxmin*fi->GetParameter(2);
|
145 |
bxf = fi->GetParameter(1) + wxmax*fi->GetParameter(2);
|
146 |
cout << ">> bx0, bxf = " << bx0 << ", " << bxf << endl;
|
147 |
}
|
148 |
else {
|
149 |
cout << ">> Use previous fit!" << endl;
|
150 |
}
|
151 |
}
|
152 |
}
|
153 |
|
154 |
cout << ">>> Final fitting resion (bx0, bxf) = " << bx0 << ", " << bxf << endl;
|
155 |
|
156 |
// Get number of events within fitted region
|
157 |
TAxis *axis = h_all->GetXaxis();
|
158 |
int bmin = axis->FindBin(bx0);
|
159 |
int bmax = axis->FindBin(bxf);
|
160 |
double nfac1 = h_all->Integral(bmin,bmax);
|
161 |
nfac1 -= (h_all->GetBinContent(bmin))*(bx0-axis->GetBinLowEdge(bmin))/axis->GetBinWidth(bmin);
|
162 |
nfac1 -= (h_all->GetBinContent(bmax))*(axis->GetBinUpEdge(bmax)-bxf)/axis->GetBinWidth(bmax);
|
163 |
cout << ">>> Final Nfac = " << nfac1 << endl;
|
164 |
|
165 |
// ////////////////////////
|
166 |
// Histos and extrapolation
|
167 |
|
168 |
TF1 *f1 = (TF1*)f0->Clone();
|
169 |
f1->SetRange(bx0,bxf);
|
170 |
for (int i=0;i<npar;i++) {
|
171 |
cout<<" >> Par["<<i<<"]="<<par0[i]<<"; Error="<<parErr0[i]<<endl;
|
172 |
f1->SetParameter(i,par0[i]);
|
173 |
}
|
174 |
|
175 |
f1->SetLineColor(2);
|
176 |
h_qcd->SetLineColor(40);
|
177 |
h_qcd->SetFillStyle(3018);
|
178 |
h_qcd->SetFillColor(38);
|
179 |
h_qcd->Draw("same");
|
180 |
f1->Draw("same");
|
181 |
|
182 |
// Connect fitted and extrapolated region
|
183 |
TF1 *fin = (TF1*)f1->Clone();
|
184 |
fin->SetRange(axf,bx0);
|
185 |
fin->SetLineStyle(2);
|
186 |
fin->SetLineColor(8);
|
187 |
fin->Draw("same");
|
188 |
|
189 |
TF1 *f2 = (TF1*)f1->Clone();
|
190 |
f2->SetRange(ax0,axf);
|
191 |
f2->SetLineColor(4);
|
192 |
f2->Draw("same");
|
193 |
int np = 100;
|
194 |
double *x=new double[np];
|
195 |
double *w=new double[np];
|
196 |
f2->CalcGaussLegendreSamplingPoints(np,x,w,1e-15);
|
197 |
ns = f2->IntegralFast(np,x,w,ax0,axf);
|
198 |
ns/=(f2->Integral(bx0,bxf));
|
199 |
ns*=nfac1;
|
200 |
cout<<">>> Ns by usual integral = "<<ns<<endl;
|
201 |
delete [] x;
|
202 |
delete [] w;
|
203 |
|
204 |
// Another way to do integration
|
205 |
TF1 *g = (TF1*)f1->Clone();
|
206 |
ROOT::Math::GSLIntegrator ig(1.E-8,1.E-8,1000);
|
207 |
ROOT::Math::WrappedTF1 wf(*g);
|
208 |
ig.SetFunction(wf);
|
209 |
ns2 = ig.Integral(ax0,axf);
|
210 |
ns2/=(g->Integral(bx0,bxf));
|
211 |
ns2*=nfac1;
|
212 |
cout<<">>> Ns by MathMore integral = "<<ns2<<endl;
|
213 |
|
214 |
cv->Update();
|
215 |
|
216 |
TPaveStats *p1 = (TPaveStats*)h_all->GetListOfFunctions()->FindObject("stats");
|
217 |
p1->SetTextColor(kBlue);
|
218 |
p1->SetX1NDC(0.6);
|
219 |
p1->SetX2NDC(0.88);
|
220 |
p1->SetY1NDC(0.62);
|
221 |
p1->SetY2NDC(0.88);
|
222 |
p1->Draw();
|
223 |
|
224 |
// Label histo
|
225 |
TLegend * leg1 = new TLegend(0.3,0.15,0.68,0.45);
|
226 |
leg1->AddEntry(h_all,"All events (S+B)");
|
227 |
leg1->AddEntry(h_qcd,"QCD events");
|
228 |
leg1->AddEntry(f1,"Fit of all events in control region");
|
229 |
leg1->AddEntry(f2,"Extrapolation to signal region");
|
230 |
leg1->Draw();
|
231 |
// Print results
|
232 |
cout << "\n";
|
233 |
cout << ">>>>> Jet bin " << jbin << ":" << endl;
|
234 |
cout << ">>>>> Observed Ns = " << ns << endl;
|
235 |
cout << ">>>>> Expected Na = " << na << endl;
|
236 |
cout << "Deviation = " << (ns-na)/na*100.0 << " %" <<endl;
|
237 |
|
238 |
}
|