ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/scripts/fitRelIso_Data.C
Revision: 1.3
Committed: Wed Jul 21 00:55:58 2010 UTC (14 years, 9 months ago) by jengbou
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +552 -305 lines
Log Message:
update

File Contents

# User Rev Content
1 jengbou 1.1 #include <vector>
2     #include <TROOT.h>
3     #include <TChain.h>
4     #include <TFile.h>
5     #include <TTree.h>
6     #include <TH1.h>
7     #include <TH2.h>
8     #include <THStack.h>
9     #include <TStyle.h>
10     #include <TCanvas.h>
11     #include <TMath.h>
12     #include <TMultiGraph.h>
13     #include <TGraphErrors.h>
14     #include <TLegend.h>
15     #include <TPaveStats.h>
16     #include <TLatex.h>
17     #include "Math/GSLIntegrator.h"
18     #include "Math/WrappedTF1.h"
19     #include "map.h"
20     #include <sstream>
21     #include <fstream>
22 jengbou 1.3 #include "TopStyle/tdrstyle.C"
23     #include "TopStyle/CMSTopStyle.cc"
24 jengbou 1.1
25     using namespace std;
26    
27     double sf_mc = 1.;
28    
29     // Dynamic(true) or static(false) range
30     bool dynamic = true;
31     TString isoname = "New";
32 jengbou 1.2 TString s0 = "fuser";//fuser,Landau,gauss
33 jengbou 1.3 TString fitOpt = "QLLMER0";
34 jengbou 1.1 bool debug = true;
35     double yMax = 0.;
36     bool DrawOverFlow = true;
37 jengbou 1.3 bool drawtail = true;
38 jengbou 1.1 double wxmin = 1.;
39     double wxmax = 1.;
40     double plot_xMax = 1.4; // max of x axis for output plots; 1.0 for Old reliso
41 jengbou 1.3 double plot_xMin = 0.3; // min of x axis for for Old reliso
42     int scaleType = 0; // 0: scale MC w.r.t. total number of events; 1: scale QCD number of event to fitted result
43     bool drawStats = false;
44 jengbou 1.1
45     map<TString,TCanvas*> cvs; // map of usual histogram
46    
47     double round(double num, int places)
48     {
49     double temp, mult;
50     mult = pow(10.0, places);
51     temp = floor(num * mult + 0.5);
52     temp = temp / mult;
53     return temp;
54     }
55    
56     double stdDev(int data[], int n, double mean)
57     {
58     double dev = 0;
59     double sum2 = 0;
60    
61     for ( int i = 0; i < n; i++ ) {
62     sum2 += pow((data[i] - mean),2);
63     }
64     dev = sqrt(sum2/(n - 1));
65     return dev;
66     }
67    
68     void cmsPrel(double intLumi){
69     TLatex latex;
70     latex.SetNDC();
71     latex.SetTextSize(0.05);
72    
73     latex.SetTextAlign(31); // align right
74     latex.DrawLatex(0.98,0.945,"#sqrt{s} = 7 TeV");
75     if (intLumi > 0.) {
76     latex.SetTextAlign(31); // align right
77     latex.DrawLatex(0.98,0.68,Form("#int #font[12]{L}dt = %.1fnb^{-1}",intLumi));
78     }
79     latex.SetTextAlign(11); // align left
80     latex.DrawLatex(0.02,0.945,"#font[22]{CMS preliminary 2010}");
81     return;
82     }
83    
84     void fitRelIso_Data()
85     {
86 jengbou 1.3 //setTDRStyle();
87     CMSTopStyle style;
88     gROOT->SetStyle("CMS");
89     style.setupICHEPv1();
90    
91 jengbou 1.1 //New RelIso
92     if (isoname == "New") {
93 jengbou 1.3 //s0 = "landau";
94     wxmax = 4.;//4
95     wxmin = 2.;
96     plot_xMin = 0.;
97 jengbou 1.1 // Select fitting function:
98     }
99     //Old RelIso
100     if (isoname == "Old") {
101     s0 = "gaus"; // for old
102 jengbou 1.3 //s0 = "fuser";
103     wxmin = 2.;
104     wxmax = 1.;
105 jengbou 1.1 DrawOverFlow = false;
106     plot_xMax = 1.;
107     }
108    
109     cout << " =========================== " << endl;
110     cout << " = Start fitting = " << endl;
111     cout << " =========================== " << endl;
112    
113 jengbou 1.3 // ////////////////////////
114     // Input/output fileName
115     // ////////////////////////
116     TString infile1,infile2,infile3,infile4,outFile;
117     if (isoname == "New") {//skimmed361p3_20100624: Loose ; skimmed361p3_20100623: tight
118     infile1 = "skimmed361p3_20100720/Data_250_Incl_NewRelIso_wErrors_TandL_4";
119     infile2 = "skimmed361p3_MC/MC_Mu15_Incl_NewRelIso_wErrors_TandL_4"; //InclusiveMu15
120     //infile2 = "skimmed361p3_MC/MC_ppMuX_Incl_NewRelIso_wErrors_TandL_4";
121     infile3 = "skimmed361p3_MC/MC_Wjets_Incl_NewRelIso_wErrors_TandL_4";
122     infile4 = "skimmed361p3_MC/MC_Zjets_Incl_NewRelIso_wErrors_TandL_4";
123 jengbou 1.1
124 jengbou 1.3 //outFile = "Plots/20100720/QCD_data_LandauFit_4_Incl_ppMuX_Scale0_250_125_"; //ppMuX
125     outFile = "Plots/20100720/QCD_data_LandauFit_4_Incl_Scale0_250_125_"; //InclusiveMu15
126     }
127     if (isoname == "Old") {
128     infile1 = "skimmed361p3_20100720/Data_250_Incl1_OldRelIso_wErrors_TandL_4";
129     infile2 = "skimmed361p3_MC/MC_Mu15_Incl1_OldRelIso_wErrors_TandL_4"; //InclusiveMu15
130     //infile2 = "skimmed361p3_MC/MC_ppMuX_Incl_OldRelIso_wErrors_TandL_4";
131     infile3 = "skimmed361p3_MC/MC_Wjets_Incl1_OldRelIso_wErrors_TandL_4";
132     infile4 = "skimmed361p3_MC/MC_Zjets_Incl1_OldRelIso_wErrors_TandL_4";
133    
134     outFile = "Plots/20100720/QCD_data_GaussFit_4_Incl1_Scale0_250_";
135     }
136 jengbou 1.1 // Output file
137 jengbou 1.3
138 jengbou 1.1 ofstream outTxtFile;
139     outTxtFile.open(TString(outFile+"Results.txt"));
140 jengbou 1.3
141     double binwds[5] = {0.05,0.02,0.01,0.005,0.002};
142     double binwidth[4];
143     if (isoname == "New") {
144     for (int i=0; i<4;++i)
145     binwidth[i] = binwds[i];
146     }
147     else {
148     for (int i=0; i<4;++i)
149     binwidth[i] = binwds[i+1];
150     }
151 jengbou 1.1
152 jengbou 1.3 // ////////////////////////
153     // Looping of files of different binning
154     // ////////////////////////
155     int res_nbkg[4] = {0,0,0,0};
156     int res_nsig[4] = {0,0,0,0};
157 jengbou 1.1 int nfiles = sizeof(binwidth)/sizeof(double);
158 jengbou 1.3
159 jengbou 1.1 for(int ibin = 0; ibin < nfiles ; ++ibin) {
160 jengbou 1.3 // ////////////////////////
161     // define fit region:
162     // ////////////////////////
163     double ax0 = 0.0;
164     double axf = 0.0;
165     double bx0 = 0.0;
166     double bxf = 0.0;
167     double loosecut = 0.;
168    
169     cout<<">>>>> Use "<<isoname<<" RelIso"<<endl;
170    
171     if ( isoname.Contains("Old") ) {
172     // old
173     loosecut = 0.9; // 10/11
174     ax0 = 20./21.; // 0.95
175     axf = 1.0 + binwidth[ibin];
176     bx0 = 0.35; // Must be smaller than x at peak //0.35
177     bxf = 0.9; // Should not overlap signal region //0.9
178     }
179     else if ( isoname.Contains("New") ) {
180     // new
181     ax0 = 0.0;
182     axf = 0.05; // 1./19. = old 0.95
183     loosecut = 0.125; // 1./9. = old 0.9
184     bx0 = 0.1;//0.2
185     bxf = 1.;//1.2
186     }
187    
188     //if (isoname == "Old" && ibin == 0) continue;
189 jengbou 1.1 TString plotFile = outFile;
190 jengbou 1.3 if (drawStats) gStyle->SetOptFit(100);
191 jengbou 1.1 cout << " =========================== " << endl;
192     cout << " = Bin width = " << binwidth[ibin] << endl;
193     cout << " =========================== " << endl;
194    
195     map<TString,TFile*> fs;
196     map<TString,TH1*> hs; // map of usual histogram
197     map<TString,TH1*> hso; // map of histo with overflow bin drawn
198     map<TString,std::pair<TString,double> > m_fs;
199     map<TString,THStack*> hst; // map of stacked histogram
200    
201     ostringstream suffix1,suffix2;
202     suffix1 << binwidth[ibin];
203     suffix2 << ibin;
204    
205     // Input files
206     // Data
207     m_fs["Data"] = pair<TString,double>(TString(infile1+"_"+suffix1.str()+".root"),1.);
208     // MC
209 jengbou 1.3 m_fs["QCD"] = pair<TString,double>(TString(infile2+"_"+suffix1.str()+".root"),0.0062348); //InclusiveMu15
210     //m_fs["QCD"] = pair<TString,double>(TString(infile2+"_"+suffix1.str()+".root"),2.9647348);
211     m_fs["WJets"] = pair<TString,double>(TString(infile3+"_"+suffix1.str()+".root"),0.0007934);
212     m_fs["ZJets"] = pair<TString,double>(TString(infile4+"_"+suffix1.str()+".root"),0.0007024);
213 jengbou 1.1
214     // User fit function
215 jengbou 1.3 // TF1 *fuser = new TF1("fuser","1./(1.+[0]*TMath::Landau(x,[1],[2],0))",ax0,axf);
216     // TF1 *fu0 = new TF1("fu0","landau",0.,ax0);
217     // TF1 *fu1 = new TF1("fu1","gaus",0.,ax0);
218     // TF1 *fu2 = new TF1("fu2","landau",ax0,axf);
219     // TF1 *fu3 = new TF1("fu3","landau(3)+gaus(2)",0.,ax0);
220     // TF1 *fuser = new TF1("fuser","[0]*TMath::Landau(x,[1],[2],1)",ax0,axf);
221     TF1 *fuser = new TF1("fuser","landau",ax0,axf);
222     fuser->SetParameters(10.,0.3,1000.);//const ==> Remember to change according to L_{int}
223     fuser->SetParLimits(2,0.15,1.);//MPV
224     fuser->SetParLimits(3,0.,1.);//sigma
225     //fuser->SetParLimits(3,0.,0.5);
226 jengbou 1.1
227     // File type
228     TString fileName = m_fs["Data"].first;
229     //cout << fileName << endl;
230    
231     Int_t jbin;
232 jengbou 1.3 Double_t na[4],na1[4];
233 jengbou 1.1 int j = 0;
234     for (std::map<TString,pair<TString,double> >::iterator itFile = m_fs.begin();
235     itFile != m_fs.end(); ++itFile, ++j) {
236     cout << (*itFile).first << "; " << ((*itFile).second).first << endl;
237     fs[(*itFile).first] = TFile::Open(((*itFile).second).first,"READ");
238     hs[(*itFile).first] = (TH1D*) fs[(*itFile).first]->Get("histos/h_RelIso_all");
239     TTree *tree = (TTree*) fs[(*itFile).first]->Get("myTree");
240     tree->SetBranchAddress("na",&na[j]);
241     if (fileName.Contains("TandL"))
242     tree->SetBranchAddress("na1",&na1[j]);
243     if ((*itFile).first == "Data") {
244     tree->SetBranchAddress("jbin",&jbin);
245 jengbou 1.3 if (isoname == "New") sf_mc = hs[(*itFile).first]->Integral(1,round(0.8/binwidth[ibin],0));
246     if (isoname == "Old") sf_mc = hs[(*itFile).first]->Integral(round(0.45/binwidth[ibin],0),hs[(*itFile).first]->GetMaximumBin());
247 jengbou 1.1 // sf_mc = hs[(*itFile).first]->GetEntries();
248 jengbou 1.3 if (scaleType == 0) cout << ">>>>> Total events of data = " << sf_mc << " <<<<<<<<<<<<<<<<" << endl;
249 jengbou 1.1 }
250     else
251     hs[(*itFile).first]->Scale(((*itFile).second).second);
252     tree->GetEntry(0);
253     }
254    
255     // Calculate scale factor for MC; normalized to total number of events from data
256     hs["MCBKG"] = (TH1D*) hs["QCD"]->Clone();
257     hs["MCBKG"]->Add((TH1D*) hs["WJets"]->Clone());
258 jengbou 1.3 hs["MCBKG"]->Add((TH1D*) hs["ZJets"]->Clone());
259     double nTotMC = 0;
260     if (isoname == "New") nTotMC = hs["MCBKG"]->Integral(1,round(0.8/binwidth[ibin],0));
261     if (isoname == "Old") nTotMC = hs["MCBKG"]->Integral(round(0.45/binwidth[ibin],0),hs["MCBKG"]->GetMaximumBin());
262     if (scaleType == 0) cout << ">>>>> Total weighted MC BKG = " << nTotMC << " <<<<<<<<<<<<<<<<" << endl;
263 jengbou 1.1 //double nTotMC = hs["QCD"]->GetEntries()*m_fs["QCD"].second + hs["WJets"]->GetEntries()*m_fs["WJets"].second;
264     //cout << ">>>>> Total weighted MC BKG = " << nTotMC << " <<<<<<<<<<<<<<<<" << endl;
265     sf_mc /= nTotMC;
266 jengbou 1.3 if (scaleType == 0) cout << ">>>>> Scale factor for MC (sf_mc) = " << sf_mc << " <<<<<<<<<<<<<<<<" << endl;
267 jengbou 1.1
268     int niter = 1;
269     double pass[3] = {0.,0.,0.};
270     double bound[2] = {0.,0.};
271     double ns_tight[3] = {0.,0.,0.};
272     double ns_loose[3] = {0.,0.,0.};
273     double ns2 = 0.;
274 jengbou 1.3 double chi2[2] = {0.,0.};
275     int ndof[2] = {0,0};
276 jengbou 1.1
277     // Fit QCD in background region
278     hs["data_all_clone"] = (TH1D*) hs["Data"]->Clone();
279 jengbou 1.3 //hs["data_all_clone"]->SetLineWidth(2);
280 jengbou 1.1 hs["data_all_clone"]->SetMinimum(0);
281     hs["mc_qcd_clone"] = (TH1D*) hs["QCD"]->Clone();
282     hs["mc_wj_clone"] = (TH1D*) hs["WJets"]->Clone();
283 jengbou 1.3 hs["mc_zj_clone"] = (TH1D*) hs["ZJets"]->Clone();
284 jengbou 1.1
285     // Get num of bin at peak and bin width
286     Int_t nbxMax = hs["data_all_clone"]->GetXaxis()->FindBin(2.0);
287 jengbou 1.3 hs["data_all_clone"]->GetXaxis()->SetRange(round(0.05/binwidth[ibin],0),hs["data_all_clone"]->GetXaxis()->FindBin(bxf));
288 jengbou 1.1 Int_t nbMax = hs["data_all_clone"]->GetMaximumBin();
289     cout << ">>>>> Bin number at peak = " << nbMax << endl;
290     hs["data_all_clone"]->GetXaxis()->SetRange(1,nbxMax);
291     TAxis *axis0 = hs["data_all_clone"]->GetXaxis();
292     double bwidth = axis0->GetBinWidth(nbMax);
293     cout << ">>>>> Histo bin width = " << bwidth << endl;
294    
295 jengbou 1.3 TString title;
296     if (isoname == "New") title = "Relative Isolation";
297     if (isoname == "Old") title = "Relative Isolation (old)";
298     // if ( jbin == -1 )
299     // title += " (N_{jets} #geq 0)";
300     // else if ( jbin == 5 ) {
301     // title += "(#geq 1 jets (up to 3 jets))";
302     // }
303     // else if ( jbin > 10) {
304     // ostringstream tmp_;
305     // tmp_ << (jbin-10);
306     // title += " (N_{jets} = ";
307     // title += tmp_.str();
308     // title += ")";
309     // }
310     // else if ( jbin <= 4 ) {
311     // title += " (N_{jets} #geq ";
312     // title += jbin;
313     // title += ")";
314     // }
315     // else {
316     // cout<<"jbin = " << jbin << " not supported!"<<endl;
317     // return;
318     // }
319 jengbou 1.1
320 jengbou 1.3 cvs[TString("cv"+suffix2.str())] = new TCanvas(TString("cv"+suffix2.str()),TString("cv"+suffix2.str()),10,10,700,500);
321 jengbou 1.1 cvs[TString("cv"+suffix2.str())]->cd();
322    
323     // Data
324     hs["data_all_clone"]->GetXaxis()->SetTitle(title);
325     hs["data_all_clone"]->GetXaxis()->SetLabelFont(42);
326 jengbou 1.3 hs["data_all_clone"]->GetXaxis()->SetLabelSize(0.04);
327 jengbou 1.1 hs["data_all_clone"]->GetXaxis()->SetTitleFont(42);
328 jengbou 1.3 hs["data_all_clone"]->GetXaxis()->SetTitleSize(0.05);
329 jengbou 1.1 hs["data_all_clone"]->GetXaxis()->SetTitleOffset(1.2);
330    
331 jengbou 1.3 hs["data_all_clone"]->GetYaxis()->SetTitle(TString("Events / "+suffix1.str()));
332     //hs["data_all_clone"]->GetYaxis()->SetTitle(TString("Events"));
333 jengbou 1.1 hs["data_all_clone"]->GetYaxis()->SetLabelFont(42);
334 jengbou 1.3 hs["data_all_clone"]->GetYaxis()->SetLabelSize(0.04);
335 jengbou 1.1 hs["data_all_clone"]->GetYaxis()->SetTitleFont(42);
336 jengbou 1.3 hs["data_all_clone"]->GetYaxis()->SetTitleSize(0.05);
337 jengbou 1.1 hs["data_all_clone"]->GetYaxis()->SetTitleOffset(1.4);
338 jengbou 1.3 hs["data_all_clone"]->GetXaxis()->SetRangeUser(plot_xMin,plot_xMax);
339 jengbou 1.1 hs["data_all_clone"]->SetMarkerStyle(20);
340     hs["data_all_clone"]->SetMarkerSize(0.8);
341     hs["data_tmp_clone"] = (TH1D*) hs["data_all_clone"]->Clone(); // for zoomed in plot
342    
343     // Find good y range first
344     if (isoname == "New") {
345     if ((hs["data_all_clone"]->GetBinContent(1) + hs["data_all_clone"]->GetBinError(1))*1.05 > yMax)
346     yMax = (hs["data_all_clone"]->GetBinContent(1) + hs["data_all_clone"]->GetBinError(1))*1.05;
347     if ((hs["mc_qcd_clone"]->GetBinContent(1)+hs["mc_wj_clone"]->GetBinContent(1))*1.05 > yMax)
348     yMax = (hs["mc_qcd_clone"]->GetBinContent(1)+hs["mc_wj_clone"]->GetBinContent(1))*1.05;
349     }
350     if (isoname == "Old") {
351     if ((hs["data_all_clone"]->GetBinContent(nbMax) + hs["data_all_clone"]->GetBinError(nbMax))*1.05 > yMax)
352     yMax = (hs["data_all_clone"]->GetBinContent(nbMax) + hs["data_all_clone"]->GetBinError(nbMax))*1.05;
353     if ((hs["mc_qcd_clone"]->GetBinContent(nbMax)+hs["mc_wj_clone"]->GetBinContent(nbMax))*1.05 > yMax)
354     yMax = (hs["mc_qcd_clone"]->GetBinContent(nbMax)+hs["mc_wj_clone"]->GetBinContent(nbMax))*1.05;
355     }
356    
357     cout << " ===============> YMax = " << yMax << endl;
358     //hs["data_all_clone"]->GetYaxis()->SetRangeUser(0.,yMax);
359     hs["data_all_clone"]->Draw("e");
360    
361     cout << "\n =========================== " << endl;
362     cout << " = Iteration step: "<< niter << endl;
363     cout << " =========================== " << endl;
364     cout << ">>>>> Start fitting control region: " << bx0 << " to " << bxf << endl;
365    
366     // Very first fit
367 jengbou 1.3 cout << ">>>>> Fitting with " << s0 << " function!" << endl;
368     fuser->SetParameters(10.,0.3,100.);//const
369     fuser->SetParLimits(2,0.15,1.);//MPV
370     fuser->SetParLimits(3,0.,1.);//sigma
371 jengbou 1.1 hs["data_all_clone"]->Fit(s0,fitOpt,"",bx0,bxf);
372    
373     // Very first fit function
374     TF1 *f0 = (TF1*) hs["data_all_clone"]->GetFunction(s0)->Clone();
375     Int_t npar = f0->GetNpar();
376 jengbou 1.3 chi2[0] = f0->GetChisquare();
377     ndof[0] = f0->GetNDF();
378     pass[0] = chi2[0]/ndof[0]; // Very first fit norm chi2. Fixed range.
379 jengbou 1.1 cout << ">>> Norm chi2 = " << pass[0] << endl;
380     if (!dynamic)
381     pass[1] = pass[0];
382     else
383     pass[1] = 999.; // require at least 2 iterations
384    
385     cout << " >> Peak at " << f0->GetMaximumX(bx0,bxf) << endl;
386    
387     double * par0 = new double [npar];
388     double * parErr0 = new double [npar];
389    
390     cout << " >> Parameters of initial fit: " << endl;
391     for (Int_t i=0;i<npar;i++) {
392     printf(" >> %s = %g +- %g\n",
393     f0->GetParName(i),
394     f0->GetParameter(i),
395     f0->GetParError(i)
396     );
397     par0[i] = f0->GetParameter(i);
398     parErr0[i] = f0->GetParError(i);
399     }
400     if(dynamic) {
401     bx0 = f0->GetParameter(1) - wxmin*f0->GetParameter(2);
402     bxf = f0->GetParameter(1) + wxmax*f0->GetParameter(2);
403 jengbou 1.3 cout << "[Before] bx0 = " << bx0 << endl;
404     cout << "[Before] bxf = " << bxf << endl;
405 jengbou 1.1 }
406     if (isoname == "New") {
407     bx0 = ( bx0 <= loosecut ? loosecut : bx0 );
408     bxf = ( bxf >= hs["data_all_clone"]->GetXaxis()->GetXmax() ?
409     hs["data_all_clone"]->GetXaxis()->GetXmax() :
410     ( bxf > bx0 ? bxf :
411     (bx0 + wxmax*f0->GetParameter(2) >= 2. ? 2. : bx0 + wxmax*f0->GetParameter(2) ) ) );
412     }
413     if (isoname == "Old") {
414     bxf = ( bxf > loosecut ? loosecut : bxf );
415     bx0 = ( bx0 < 0. ? 0. : bx0 );
416     }
417 jengbou 1.3 // if (isoname == "New") {
418     // bx0 = ( bx0 <= loosecut ? loosecut :
419     // ( bx0 < hs["data_all_clone"]->GetXaxis()->GetXmax() ? bx0 :
420     // ( hs["data_all_clone"]->GetXaxis()->GetXmax() + loosecut )/2. ) );
421     // bxf = ( bxf >= hs["data_all_clone"]->GetXaxis()->GetXmax() ?
422     // hs["data_all_clone"]->GetXaxis()->GetXmax() :
423     // ( bxf > bx0 ? bxf :
424     // ( bx0 + hs["data_all_clone"]->GetXaxis()->GetXmax() )/2. ) );
425     // }
426     // if (isoname == "Old") {
427     // bxf = ( bxf > loosecut ? loosecut :
428     // ( bxf > hs["data_all_clone"]->GetXaxis()->GetXmin() ? bxf :
429     // ( hs["data_all_clone"]->GetXaxis()->GetXmin() + loosecut)/2. ) );
430     // bx0 = ( bx0 < hs["data_all_clone"]->GetXaxis()->GetXmin() ? hs["data_all_clone"]->GetXaxis()->GetXmin() :
431     // ( bx0 > bxf ? ( bxf + hs["data_all_clone"]->GetXaxis()->GetXmin() )/2. : bx0) );
432     // }
433 jengbou 1.1 bound[0] = bx0;
434     bound[1] = bxf;
435     if (dynamic) {
436     cout << ">>> Range for next iteration (bx0, bxf) = (" << bx0;
437     cout << ", " << bxf << ")" << endl;
438     }
439    
440     double delta = pass[1]-pass[2];
441    
442 jengbou 1.3 while (dynamic && niter <= 20 && abs(delta) > 0.000001 && ndof[0] >= 4) {
443 jengbou 1.1 if (niter > 1)
444     pass[1] = pass[2];
445     if (delta != 0){
446     niter++;
447     cout << "\n =========================== " << endl;
448     cout << " = Iteration step: "<< niter << endl;
449     cout << " =========================== " << endl;
450     cout << ">>>>> Previous step norm. chi2 = " << pass[1];
451     if (niter == 2)
452     cout << "; pass[0] = " << pass[0];
453    
454     cout << "\n>>> Start fitting new range (bx0, bxf) = (" << bx0
455     << ", " << bxf << ")" << endl;
456    
457 jengbou 1.3 fuser->SetParameters(10.,0.3,100.);//const
458     fuser->SetParLimits(2,0.15,1.);//MPV
459     fuser->SetParLimits(3,0.,1.);//sigma
460    
461 jengbou 1.1 hs["data_all_clone"]->Fit(s0,fitOpt,"",bx0,bxf);
462     TF1 *fi = (TF1*) hs["data_all_clone"]->GetFunction(s0)->Clone();
463     cout << " >> fi->GetChisquare() = " << fi->GetChisquare() << "; fi->GetNDF() = " << fi->GetNDF() << endl;
464 jengbou 1.3 if ( fi->GetNDF() > 0 )
465     pass[2] = fi->GetChisquare()/fi->GetNDF();
466     else {
467     chi2[1] = 999.;
468     ndof[1] = 1.;
469     pass[2] = 999.;
470     }
471 jengbou 1.1 cout << " >> Current step norm. chi2 = " << pass[2] << endl;
472     delta = pass[1]-pass[2];
473     cout << " >> Delta = " << delta << endl;
474    
475     if (delta > 0 && bx0 >= loosecut && bxf <= hs["data_all_clone"]->GetXaxis()->GetXmax() && bx0 < bxf) {
476 jengbou 1.3 chi2[1] = fi->GetChisquare();
477     ndof[1] = fi->GetNDF();
478 jengbou 1.1 bound[0] = bx0;
479     bound[1] = bxf;
480     printf(" >> Function has %i parameters. Chisquare = %g\n",
481     npar,
482     fi->GetChisquare());
483     for (Int_t i=0;i<npar;i++) {
484     printf(" >> %s = %g +- %g\n",
485     fi->GetParName(i),
486     fi->GetParameter(i),
487     fi->GetParError(i)
488     );
489     par0[i] = fi->GetParameter(i);
490     parErr0[i] = fi->GetParError(i);
491     // cout<<"Par["<<i<<"]="<<par0[i]<<"; Error="<<parErr0[i]<<endl;
492     }
493     bx0 = fi->GetParameter(1) - wxmin*fi->GetParameter(2);
494     bxf = fi->GetParameter(1) + wxmax*fi->GetParameter(2);
495 jengbou 1.3 cout << "[Before] bx0 = " << bx0 << endl;
496     cout << "[Before] bxf = " << bxf << endl;
497    
498 jengbou 1.1 if (isoname == "New") {
499     bx0 = ( bx0 < loosecut ? loosecut : bx0 );
500     bxf = ( bxf > 2. ? 2. : ( bxf > bx0 ? bxf : 1.1*bx0 ) );
501     }
502     if (isoname == "Old") {
503     bxf = ( bxf > loosecut ? loosecut : bxf );
504     bxf = ( bx0 < 0. ? 0. : bx0 );
505     }
506 jengbou 1.3 // if (isoname == "New") {
507     // bx0 = ( bx0 <= loosecut ? loosecut :
508     // ( bx0 < hs["data_all_clone"]->GetXaxis()->GetXmax() ? bx0 :
509     // ( hs["data_all_clone"]->GetXaxis()->GetXmax() + loosecut )/2. ) );
510     // bxf = ( bxf >= hs["data_all_clone"]->GetXaxis()->GetXmax() ?
511     // hs["data_all_clone"]->GetXaxis()->GetXmax() :
512     // ( bxf > bx0 ? bxf :
513     // ( bx0 + hs["data_all_clone"]->GetXaxis()->GetXmax() )/2. ) );
514     // }
515     // if (isoname == "Old") {
516     // bxf = ( bxf > loosecut ? loosecut :
517     // ( bxf > hs["data_all_clone"]->GetXaxis()->GetXmin() ? bxf :
518     // ( hs["data_all_clone"]->GetXaxis()->GetXmin() + loosecut)/2. ) );
519     // bx0 = ( bx0 < hs["data_all_clone"]->GetXaxis()->GetXmin() ? hs["data_all_clone"]->GetXaxis()->GetXmin() :
520     // ( bx0 > bxf ? ( bxf + hs["data_all_clone"]->GetXaxis()->GetXmin() )/2. :bx0 ) );
521     // }
522 jengbou 1.1 cout << ">>> Range for next iteration (bx0, bxf) = (" << bx0;
523     cout << ", " << bxf << ")" << endl;
524     }
525     else if (delta < 0) {
526     cout << ">>> Use previous fit parameters for extrapolation fit" << endl;
527     delta = 0;
528     }
529     else { // delta == 0
530     cout << ">> Fit converges." << endl;
531     cout << ">> Best fitting region = (" << bound[0];
532     cout << ", " << bound[1] << ")" << endl;
533     }
534     if (niter == 2)
535     pass[0]=pass[2]; // First dynamic fit norm. chi2
536     } // delta != 0
537     } // End while
538 jengbou 1.3
539     // Output equivalent range
540     if (dynamic) {
541     if (bound[0]/binwidth[ibin] - floor(bound[0]/binwidth[ibin]) > 0.5)
542     bound[0] = round(bound[0]/binwidth[ibin],0) * binwidth[ibin];
543     else
544     bound[0] = (floor(bound[0]/binwidth[ibin])+0.5) * binwidth[ibin];
545     if (bound[1]/binwidth[ibin] - floor(bound[1]/binwidth[ibin]) >= 0.5)
546     bound[1] = round(bound[1]/binwidth[ibin],0) * binwidth[ibin];
547     else
548     bound[1] = round(bound[1]/binwidth[ibin],0) * binwidth[ibin];
549     }
550    
551 jengbou 1.1 if(debug){
552     cout << ">>> First dynamic fit norm. chi2 = " << pass[0] << endl;
553     cout << ">>> Min dynamic norm. chi2 = " << pass[1] << endl;
554     cout << ">>> Last fitting region (bx0, bxf) = (" << bx0 << ", " << bxf << ")"<< endl;
555     cout << ">>> Best fitting region (bx0, bxf) = (" << bound[0] << ", ";
556     cout << bound[1] << ")"<< endl;
557     }
558    
559     // Get number of events within fitted region
560     TAxis *axis = hs["data_all_clone"]->GetXaxis();
561     int bmin = axis->FindBin(bound[0]);
562     int bmax = axis->FindBin(bound[1]);
563     double nfac1 = hs["data_all_clone"]->Integral(bmin,bmax);
564     cout << ">>> Initial Nfac = " << nfac1 << endl;
565     nfac1 -= (hs["data_all_clone"]->GetBinContent(bmin))*(bound[0]-axis->GetBinLowEdge(bmin))/axis->GetBinWidth(bmin);
566     nfac1 -= (hs["data_all_clone"]->GetBinContent(bmax))*(axis->GetBinUpEdge(bmax)-bound[1])/axis->GetBinWidth(bmax);
567     cout << ">>> Final Nfac = " << nfac1 << endl;
568    
569     TF1 *f1 = (TF1*) f0->Clone();
570     f1->SetRange(bound[0],bound[1]);
571    
572     cout << ">>> Parameters used for " << s0 << " function: " << endl;
573     for (int i=0;i<npar;i++) {
574     cout << " >> Par[" << i << "] = " << par0[i] << "; Error = " << parErr0[i] << endl;
575     f1->SetParameter(i,par0[i]);
576     }
577     f1->SetLineColor(2);
578    
579     TF1 *f2 = (TF1*) f1->Clone();
580     f2->SetRange(ax0,axf);
581     f2->SetLineColor(4);
582     //f2->SetLineWidth(3.5);
583    
584     // ////////////////////////
585     // Extract number of qcd events
586     // ////////////////////////
587     // Method 1: Ratio
588     int np = 100;
589     double *x=new double[np];
590     double *w=new double[np];
591     f2->CalcGaussLegendreSamplingPoints(np,x,w,1e-15);
592    
593     double nfac0 = 0.;
594     if (fileName.Contains("0053") ||
595     fileName.Contains("TandL") ||
596     fileName.Contains("Tight")) {
597     ns_tight[0] = f2->IntegralFast(np,x,w,ax0,axf);
598     nfac0 = f2->Integral(bound[0],bound[1]);
599     cout << " >> Area of signal region = " << ns_tight[0] << endl;
600     cout << " >> Area of control region = " << nfac0 << endl;
601     ns_tight[0]/=nfac0;
602     ns_tight[0]*=nfac1;
603     }
604     else if (fileName.Contains("011") ||
605     fileName.Contains("Loose")) {
606     ns_tight[0] = f2->IntegralFast(np,x,w,ax0,loosecut);
607     nfac0 = f2->Integral(bound[0],bound[1]);
608     ns_tight[0]/=nfac0;
609     ns_tight[0]*=nfac1;
610     }
611    
612     ns_loose[0] = f2->IntegralFast(np,x,w,ax0,loosecut);
613     ns_loose[0]/=(f2->Integral(bound[0],bound[1]));
614     ns_loose[0]*=nfac1;
615    
616     cout << ">>> Tight Ns by usual integral = " << ns_tight[0] << endl;
617     if (fileName.Contains("TandL") || fileName.Contains("011"))
618     cout << ">>> Loose Ns by usual integral = " << ns_loose[0] << endl;
619    
620     delete [] x;
621     delete [] w;
622    
623     // Another way to do integration
624     TF1 *g = (TF1*)f1->Clone();
625     ROOT::Math::GSLIntegrator ig(1.E-8,1.E-8,1000);
626     ROOT::Math::WrappedTF1 wf(*g);
627     ig.SetFunction(wf);
628     if (fileName.Contains("0053") ||
629     fileName.Contains("TandL") ||
630     fileName.Contains("Tight"))
631     ns2 = ig.Integral(ax0,axf);
632     else if (fileName.Contains("011") ||
633     fileName.Contains("Loose"))
634     ns2 = ig.Integral(ax0,loosecut);
635    
636     ns2/=(g->Integral(bound[0],bound[1]));
637     ns2*=nfac1;
638     cout<<">>> Ns by MathMore integral = "<<ns2<<endl;
639    
640     // Method 2: area of trapezoid
641     ns_tight[1] = (TMath::Landau(ax0,par0[1],par0[2],kFALSE)*par0[0] + TMath::Landau(axf,par0[1],par0[2],kFALSE)*par0[0])
642     * fabs(axf-ax0)/(2*bwidth);
643     ns_loose[1] = (TMath::Landau(ax0,par0[1],par0[2],kFALSE)*par0[0] + TMath::Landau(loosecut,par0[1],par0[2],kFALSE)*par0[0])
644     * fabs(loosecut-ax0)/(2*bwidth);
645 jengbou 1.3 // Method 3: area of Landau function (default)
646 jengbou 1.1 ns_tight[2] = f2->Integral(ax0,axf)/bwidth;
647     ns_loose[2] = f2->Integral(ax0,loosecut)/bwidth;
648    
649 jengbou 1.3 // ////////////////////////
650     // Histos and extrapolation
651     // ////////////////////////
652    
653     // Draw stacked MC
654     double sf_mc1 = 0.;
655     if (isoname == "New") sf_mc1 = ns_tight[2]/hs["QCD"]->Integral(1,round(0.05/binwidth[ibin],0));
656     if (isoname == "Old") sf_mc1 = ns_tight[2]/hs["QCD"]->Integral(round(0.95/binwidth[ibin],0)+1,hs["QCD"]->GetMaximumBin());
657     if (scaleType == 0) {
658     hs["mc_qcd_clone"]->Scale(sf_mc);
659     hs["mc_wj_clone"]->Scale(sf_mc);
660     }
661     if (scaleType == 1) {
662     sf_mc = sf_mc1;
663     hs["mc_qcd_clone"]->Scale(sf_mc1);
664     hs["mc_wj_clone"]->Scale(sf_mc1);
665     cout << ">>>>> Scale factor for MC (sf_mc) = " << sf_mc1 << " <<<<<<<<<<<<<<<<" << endl;
666     }
667    
668     if (DrawOverFlow) { // store histo with overflow bin drawn
669     int nbins_ = hs["data_all_clone"]->GetXaxis()->FindBin(plot_xMax);
670     for (std::map<TString,TH1*>::iterator itHS = hs.begin();
671     itHS != hs.end(); ++itHS) {
672     hso[(*itHS).first] = (TH1D*) ((*itHS).second)->Clone();
673     hso[(*itHS).first]->SetBins(nbins_,0.,plot_xMax+bwidth);
674     double v_ovf = 0.;
675     for (int i=0; i < ((*itHS).second)->GetNbinsX()+1 ; i++) {
676     if (i < nbins_) {
677     hso[(*itHS).first]->SetBinContent(i+1,hs[(*itHS).first]->GetBinContent(i+1));
678     hso[(*itHS).first]->SetBinError(i+1,hs[(*itHS).first]->GetBinError(i+1));
679     }
680     else {
681     v_ovf += hs[(*itHS).first]->GetBinContent(i+1);
682     }
683     }
684     hso[(*itHS).first]->SetBinContent(nbins_,v_ovf);
685     hso[(*itHS).first]->SetBinError(nbins_,TMath::Sqrt(v_ovf));
686     }
687     }
688    
689     hst[TString("Data"+suffix2.str())] = new THStack(TString("Data"+suffix2.str()),TString("Data"+suffix2.str()+"stacked"));
690     if (DrawOverFlow) {
691     hso["mc_qcd_clone"]->SetStats(0);
692     //hso["mc_qcd_clone"]->SetLineWidth(2);
693     //hso["mc_qcd_clone"]->SetLineColor(style.QCDColor);
694     hso["mc_qcd_clone"]->SetFillColor(style.QCDColor);
695     hso["mc_qcd_clone"]->SetFillStyle(style.QCDFill);
696    
697     hso["mc_zj_clone"]->SetStats(0);
698     hso["mc_zj_clone"]->SetFillStyle(style.DYZJetsFill);
699     hso["mc_zj_clone"]->SetFillColor(style.DYZJetsColor);
700    
701     hso["mc_wj_clone"]->SetStats(0);
702     //hso["mc_wj_clone"]->SetLineWidth(2);
703     //hso["mc_wj_clone"]->SetLineColor(style.WJetsColor);
704     hso["mc_wj_clone"]->SetFillStyle(style.WJetsFill);
705     hso["mc_wj_clone"]->SetFillColor(style.WJetsColor);
706     hst[TString("Data"+suffix2.str())]->Add(hso["mc_qcd_clone"]);
707     hst[TString("Data"+suffix2.str())]->Add(hso["mc_zj_clone"]);
708     hst[TString("Data"+suffix2.str())]->Add(hso["mc_wj_clone"]);
709     }
710     else {
711     hs["mc_qcd_clone"]->SetStats(0);
712     //hs["mc_qcd_clone"]->SetLineWidth(2);
713     //hs["mc_qcd_clone"]->SetLineColor(style.QCDColor);
714     hs["mc_qcd_clone"]->SetFillStyle(style.QCDFill);
715     hs["mc_qcd_clone"]->SetFillColor(style.QCDColor);
716    
717     hs["mc_zj_clone"]->SetStats(0);
718     hs["mc_zj_clone"]->SetFillStyle(style.DYZJetsFill);
719     hs["mc_zj_clone"]->SetFillColor(style.DYZJetsColor);
720    
721     hs["mc_wj_clone"]->SetStats(0);
722     //hs["mc_wj_clone"]->SetLineWidth(2);
723     //hs["mc_wj_clone"]->SetLineColor(style.WJetsColor);
724     hs["mc_wj_clone"]->SetFillStyle(style.WJetsFill);
725     hs["mc_wj_clone"]->SetFillColor(style.WJetsColor);
726    
727     hst[TString("Data"+suffix2.str())]->Add(hs["mc_qcd_clone"]);
728     hst[TString("Data"+suffix2.str())]->Add(hs["mc_zj_clone"]);
729     hst[TString("Data"+suffix2.str())]->Add(hs["mc_wj_clone"]);
730     }
731    
732     // Draw data
733     if (DrawOverFlow) {
734     hso["data_all_clone"]->GetYaxis()->SetRangeUser(0.,yMax);
735     hso["data_all_clone"]->Draw("esame");
736     }
737    
738     hst[TString("Data"+suffix2.str())]->Draw("sames hist");
739    
740     // Draw data again
741     gStyle->SetErrorX(0.5);
742    
743     if (DrawOverFlow) {
744     hso["data_all_clone"]->Draw("esame");
745 jengbou 1.1 }
746     else {
747 jengbou 1.3 hs["data_all_clone"]->DrawClone("esame"); // Draw on top of others
748 jengbou 1.1 }
749    
750 jengbou 1.3 // Draw fit function
751    
752     // fit function in control region
753     //f1->SetLineWidth(3.5);
754     f1->Draw("same");
755    
756     // fit fuction in signal region
757     f2->Draw("same");
758    
759     // Connect fit function between regions and tail
760     TF1 *fin = (TF1*) f1->Clone();
761     if (isoname == "New") {
762     fin->SetRange(axf,bound[0]);
763     fin->SetLineStyle(2);
764     fin->SetLineColor(9);
765     fin->SetLineWidth(3.5);
766     fin->Draw("same");
767    
768     if (drawtail){
769     TF1 *fine = (TF1*) f1->Clone();
770     fine->SetRange(bound[1],2.0);
771     fine->SetLineStyle(2);
772     fine->SetLineColor(9);
773     fine->SetLineWidth(3.5);
774     fine->Draw("same");
775     }
776     }
777     if (isoname == "Old") {
778     fin->SetRange(bound[1],ax0);
779     fin->SetLineStyle(2);
780     fin->SetLineColor(9);
781     fin->SetLineWidth(3.5);
782     fin->Draw("same");
783    
784     if (drawtail){
785     TF1 *fine = (TF1*) f1->Clone();
786     fine->SetRange(0.,bound[0]);
787     fine->SetLineStyle(2);
788     fine->SetLineColor(9);
789     fine->SetLineWidth(3.5);
790     fine->Draw("same");
791     }
792     }
793 jengbou 1.1
794 jengbou 1.2 TString mytxt;
795     if (isoname == "New") mytxt = "Landau Fit";
796     if (isoname == "Old") mytxt = "Gaussian Fit";
797 jengbou 1.1
798 jengbou 1.3 if (drawStats) {
799     // Draw Stats box
800     TPaveStats * tps1;
801     if(!dynamic){
802     cvs[TString("cv"+suffix2.str())]->Update();
803     tps1 = (TPaveStats*) cvs[TString("cv"+suffix2.str())]->GetPrimitive("stats");
804     }
805     else {
806     hs["data_all_clone"]->Fit(s0,fitOpt,"",bound[0],bound[1]);
807     cvs[TString("cv"+suffix2.str())]->Update();
808     tps1 = (TPaveStats*) cvs[TString("cv"+suffix2.str())]->GetPrimitive("stats");
809     }
810    
811     tps1->SetName(TString(s0+" fit"+suffix2.str()));
812    
813     TList *list = tps1->GetListOfLines();
814     // TText *tconst = tps1->GetLineWith("Constant");
815     // list->Remove(tconst);
816     TLatex *myt = new TLatex(0,0,mytxt);
817     //TLatex *myt = new TLatex(0,0,"#font[22]{Landau Fit}");
818     list->AddFirst(myt);
819     hs["data_all_clone"]->SetStats(0);
820     gStyle->SetOptFit(0);
821     cvs[TString("cv"+suffix2.str())]->Modified();
822    
823     tps1->SetTextFont(42);
824     tps1->SetTextSize(0.03);
825     tps1->SetFillColor(0);
826     tps1->SetFillStyle(0);
827     tps1->SetBorderSize(0);
828     tps1->SetX1NDC(0.63);
829     tps1->SetX2NDC(0.88);
830     tps1->SetY1NDC(0.74);
831     tps1->SetY2NDC(0.87);
832     cvs[TString("cv"+suffix2.str())]->cd();
833     gPad->Update();
834     }
835    
836     TLatex *text1, *text2;
837     text1 = new TLatex(3.570061, 23.08044, "CMS Preliminary");
838     text1->SetNDC();
839     text1->SetTextAlign(13);
840     if (isoname == "New")
841     text1->SetX(0.24);
842     else
843     text1->SetX(0.54);
844     text1->SetY(0.92);
845     //text1->SetLineWidth(2);
846     text1->SetTextFont(42);
847     text1->SetTextSize(0.05);
848     text1->SetTextSizePixels(24);// dflt=28
849     text1->Draw();
850    
851     text2 = new TLatex(13.570061, 23.08044, "0.25 pb^{-1} at #sqrt{s} = 7 TeV");
852     text2->SetNDC();
853     text2->SetTextAlign(13);
854     if (isoname == "New")
855     text2->SetX(0.24);
856     else
857     text2->SetX(0.54);
858     text2->SetY(0.85);
859     //text2->SetLineWidth(2);
860     text2->SetTextFont(42);
861     text2->SetTextSize(0.05);
862     text2->SetTextSizePixels(24);// dflt=28
863     text2->Draw();
864    
865     //cmsPrel(35);
866 jengbou 1.1 // Label histo
867 jengbou 1.3 //TLegend * leg1 = new TLegend(0.58,0.45,0.85,0.8);
868     TLegend * leg1;
869     if (isoname == "New")
870     leg1 = new TLegend(0.66,0.52,0.95,0.92);
871     else
872     leg1 = new TLegend(0.28,0.57,0.55,0.92);
873     //leg1->SetHeader("#font[22]{CMS preliminary 2010 #sqrt{s} = 7 TeV }");
874 jengbou 1.1 leg1->SetTextSize(0.03);
875     leg1->SetFillColor(0);
876     leg1->SetFillStyle(0);
877     if (DrawOverFlow) {
878 jengbou 1.3 //leg1->AddEntry(hso["data_all_clone"],"Data #int#font[12]{L}dt = 35 nb^{-1}","p");
879     leg1->AddEntry(hso["data_all_clone"],"Data","lp");
880     // leg1->AddEntry(f1,TString(mytxt+" in control region"),"l");
881     // leg1->AddEntry(f2,"Extrapolation to signal region","l");
882     leg1->AddEntry(f1,mytxt,"l");
883     leg1->AddEntry(f2,"Extrapolation","l");
884     leg1->AddEntry(hso["mc_wj_clone"],style.WJetsText,"f");
885     leg1->AddEntry(hso["mc_zj_clone"],style.DYZJetsText,"f");
886     leg1->AddEntry(hso["mc_qcd_clone"],style.QCDText,"f");
887 jengbou 1.1 }
888     else {
889 jengbou 1.3 //leg1->AddEntry(hs["data_all_clone"],"Data #int#font[12]{L}dt = 35 nb^{-1}","p");
890     leg1->AddEntry(hs["data_all_clone"],"Data","lp");
891     leg1->AddEntry(f1,mytxt,"l");
892     leg1->AddEntry(f2,"Extrapolation","l");
893     leg1->AddEntry(hs["mc_wj_clone"],style.WJetsText,"f");
894     leg1->AddEntry(hs["mc_zj_clone"],style.DYZJetsText,"f");
895     leg1->AddEntry(hs["mc_qcd_clone"],style.QCDText,"f");
896 jengbou 1.1 }
897     leg1->Draw();
898    
899     plotFile = plotFile + suffix1.str() + ".pdf";
900 jengbou 1.3 // if (ibin == 1 || ibin == 2)
901     cvs[TString("cv"+suffix2.str())]->Print(plotFile);
902 jengbou 1.1
903     // Print results
904     cout << "\n";
905     cout << " =========================== " << endl;
906     cout << " = Results = " << endl;
907     cout << " =========================== " << endl;
908    
909     if ( jbin == -1 )
910     cout << ">>>>> Inclusive jet bin " << ":" << endl;
911     else
912     cout << ">>>>> Jet bin " << jbin << ":" << endl;
913     cout << ">>>>> Bin number at peak = " << nbMax << endl;
914     cout << ">>>>> Bin width = " << bwidth << endl;
915     if (dynamic) {
916     cout << ">>>>> First dynamic fit norm. chi2 = " << pass[0] << endl;
917     cout << ">>>>> Min dynamic fit norm. chi2 = " << pass[1] << endl;
918     cout << ">>>>> Last fitting region (bx0, bxf) = (" << bx0 << ", " << bxf << ")"<< endl;
919     cout << ">>>>> Best fitting region (bx0, bxf) = (" << bound[0] << ", " << bound[1] << ")"<< endl;
920     }
921     cout << ">>>>> Calculated Ns (ratio) = " << ns_tight[0] << endl;
922     cout << ">>>>> Calculated Ns (trapezoid) = " << ns_tight[1] << endl;
923     cout << ">>>>> Calculated Ns (integral) = " << ns_tight[2] << endl;
924     cout << ">>>>> Total events expected Na = " << na[0] << endl;
925     cout << ">>>>> Num of Signal events = " << na[0] - round(ns_tight[2],0) << endl;
926    
927     res_nbkg[ibin] = round(ns_tight[2],0);
928     res_nsig[ibin] = na[0]-round(ns_tight[2],0);
929    
930     // if (fileName.Contains("TandL")) {
931     // cout << "====================================" << endl;
932     // cout << ">>>>> Calculated Loose Ns (ratio) = " << ns_loose[0] << endl;
933     // cout << ">>>>> Calculated Loose Ns (trapezoid) = " << ns_loose[1] << endl;
934     // cout << ">>>>> Calculated Loose Ns (integral) = " << ns_loose[2] << endl;
935     // cout << ">>>>> Total expected events Loose Na = " << na1[0] << endl;
936     // cout << ">>>>> Num of Signal events = " << na1[0] - round(ns_loose[2],0) << endl;
937     // }
938     // cout << "Landau(0.05) = " << TMath::Landau(0.05,par0[1],par0[2],kFALSE)*par0[0] << endl;
939     // cout << "Landau(0.1) = " << TMath::Landau(0.1,par0[1],par0[2],kFALSE)*par0[0] << endl;
940     cout << " ===========================\n\n " << endl;
941    
942     outTxtFile << "===========================" << endl;
943 jengbou 1.3 outTxtFile << ">>>>> Bin width = " << bwidth << endl;
944     //outTxtFile << ">>>>> Calculated Ns (ratio) = " << ns_tight[0] << endl;
945     //outTxtFile << ">>>>> Calculated Ns (trapezoid) = " << ns_tight[1] << endl;
946     if (dynamic) {
947     outTxtFile << ">>>>> Min dynamic norm. chi2 = " << pass[1] << endl;
948     outTxtFile << ">>>>> chi2/ndof = " << chi2[1] << "/" << ndof[1] << " = "
949     << chi2[1]/ndof[1] << endl;
950     }
951     else {
952     outTxtFile << ">>>>> Min norm. chi2 = " << pass[1] << endl;
953     outTxtFile << ">>>>> chi2/ndof = " << chi2[0] << "/" << ndof[0] << " = " << chi2[0]/ndof[0] << endl;
954     }
955     outTxtFile << ">>>>> Best fitting region = (" << bound[0];
956     outTxtFile << " - " << bound[1] << ")" << endl;
957     outTxtFile << ">>>>> Total number of events = " << na[0] << endl;
958     outTxtFile << ">>>>> Calculated bkg (integral) = " << round(ns_tight[2],0) << endl;
959     outTxtFile << ">>>>> Num of Signal events = " << na[0] - round(ns_tight[2],0) << endl;
960     outTxtFile << ">>>>> Total MC QCD expected(N) = " << round(na[1]*((m_fs["QCD"]).second)*sf_mc,1) << " +/- " <<
961     round(TMath::Sqrt(na[1])*((m_fs["QCD"]).second)*sf_mc,1) << endl;
962     outTxtFile << ">>>>> Total MC WJets expected(N) = " << round(na[2]*((m_fs["WJets"]).second)*sf_mc,1) << " +/- " <<
963     round(TMath::Sqrt(na[2])*((m_fs["WJets"]).second)*sf_mc,1) << endl;
964     outTxtFile << ">>>>> Total MC ZJets expected(N) = " << round(na[3]*((m_fs["ZJets"]).second)*sf_mc,1) << " +/- " <<
965     round(TMath::Sqrt(na[3])*((m_fs["ZJets"]).second)*sf_mc,1) << endl;
966     outTxtFile << ">>>>> Total MC QCD expected(R) = " << round(na[1]*((m_fs["QCD"]).second),1) << " +/- " <<
967     round(TMath::Sqrt(na[1])*((m_fs["QCD"]).second),1)<< endl;
968     outTxtFile << ">>>>> Total MC WJets expected(R) = " << round(na[2]*((m_fs["WJets"]).second),1) << " +/- " <<
969     round(TMath::Sqrt(na[2])*((m_fs["WJets"]).second),1) << endl;
970     outTxtFile << ">>>>> Total MC ZJets expected(R) = " << round(na[3]*((m_fs["ZJets"]).second),1) << " +/- " <<
971     round(TMath::Sqrt(na[3])*((m_fs["ZJets"]).second),2) << endl;
972 jengbou 1.1 outTxtFile << "===========================\n\n " << endl;
973    
974    
975 jengbou 1.3 // /////////////
976     // Zoom-in plot
977     // /////////////
978     cvs[TString("cv1"+suffix2.str())] = new TCanvas(TString("cv1"+suffix2.str()),TString("cv1"+suffix2.str()),10,10,700,500);
979 jengbou 1.1 cvs[TString("cv1"+suffix2.str())]->cd();
980 jengbou 1.3
981 jengbou 1.2 if (isoname == "New") {
982 jengbou 1.3 hs["data_tmp_clone"]->GetXaxis()->SetRangeUser(0.,0.06);
983     hs["data_tmp_clone"]->GetYaxis()->SetRangeUser(0.3,10000.*binwidth[ibin]/binwidth[3]);
984 jengbou 1.2 }
985 jengbou 1.3 if (isoname == "Old") {
986 jengbou 1.1 hs["data_tmp_clone"]->GetXaxis()->SetRangeUser(0.9,axf);
987 jengbou 1.3 hs["data_tmp_clone"]->GetYaxis()->SetRangeUser(0.3,10000.*binwidth[ibin]/binwidth[3]);
988     }
989 jengbou 1.1 hs["data_tmp_clone"]->Draw("e");
990     hst[TString("Data"+suffix2.str())]->Draw("sames hist");
991     hs["data_tmp_clone"]->Draw("esame");
992    
993 jengbou 1.3 text1->Draw();
994     text2->Draw();
995    
996     TLegend * leg2;
997     if (isoname == "New")
998     leg2 = new TLegend(0.66,0.52,0.95,0.92);
999     else
1000     leg2 = new TLegend(0.28,0.57,0.55,0.92);
1001     //leg2->SetHeader("#font[22]{CMS preliminary 2010 #sqrt{s} = 7 TeV }");
1002 jengbou 1.1 leg2->SetTextSize(0.03);
1003     leg2->SetFillColor(0);
1004     leg2->SetFillStyle(0);
1005 jengbou 1.3 //leg2->AddEntry(hs["data_all_clone"],"Data #int#font[12]{L}dt = 35 nb^{-1}","p");
1006 jengbou 1.1 if (DrawOverFlow) {
1007 jengbou 1.3 //leg2->AddEntry(hso["data_all_clone"],"Data #int#font[12]{L}dt = 35 nb^{-1}","p");
1008     leg2->AddEntry(hso["data_all_clone"],"Data","lp");
1009     //leg2->AddEntry(f2,TString("Extrapolation of "+mytxt),"l");
1010     leg2->AddEntry(f2,"Extrapolation","l");
1011     leg2->AddEntry(hso["mc_wj_clone"],style.WJetsText,"f");
1012     leg2->AddEntry(hso["mc_zj_clone"],style.DYZJetsText,"f");
1013     leg2->AddEntry(hso["mc_qcd_clone"],style.QCDText,"f");
1014     }
1015     else {
1016     //leg2->AddEntry(hs["data_all_clone"],"Data #int#font[12]{L}dt = 35 nb^{-1}","p");
1017     leg2->AddEntry(hs["data_all_clone"],"Data","lp");
1018     //leg2->AddEntry(f2,TString("Extrapolation of "+mytxt),"l");
1019     leg2->AddEntry(f2,"Extrapolation","l");
1020     leg2->AddEntry(hs["mc_wj_clone"],style.WJetsText,"f");
1021     leg2->AddEntry(hs["mc_zj_clone"],style.DYZJetsText,"f");
1022     leg2->AddEntry(hs["mc_qcd_clone"],style.QCDText,"f");
1023 jengbou 1.1 }
1024     leg2->Draw();
1025    
1026     // TF1 *fb2 = new TF1("fa3","[0]*TMath::Landau(x,[1],[2],0)",ax0,axf);
1027     // fb2->SetParameters(par0[0],par0[1],par0[2]);
1028     // fb2->SetLineColor(5);
1029     // fb2->Draw("same");
1030 jengbou 1.3 f2->Draw("same");
1031     fin->Draw("same");
1032     //hs["Data"]->GetXaxis()->SetRangeUser(0.,0.06);
1033     //hs["Data"]->GetYaxis()->SetRangeUser(0.3,300.);
1034     hs["Data"]->SetLineWidth(2);
1035     cvs[TString("cv1"+suffix2.str())]->SetLogy();
1036     hs["Data"]->DrawClone("esame"); // Draw on top of others
1037 jengbou 1.1
1038     plotFile = plotFile(0,plotFile.Length()-4) + "_zoomed.pdf";
1039 jengbou 1.3 //if (ibin == 2 || ibin == 3)
1040     cvs[TString("cv1"+suffix2.str())]->Print(plotFile);
1041 jengbou 1.1
1042     hs.clear();
1043     hso.clear();
1044     fs.clear();
1045     m_fs.clear();
1046     }
1047     // Calculate errors:
1048    
1049     float sum[2] = {0.,0.};
1050     float average[2] = {0.,0.};
1051     float errors = 0.;
1052    
1053 jengbou 1.3 if ( nfiles > 1) {
1054     for (int ii=0; ii<nfiles; ii++) {
1055     sum[0] += res_nbkg[ii];
1056     sum[1] += res_nsig[ii];
1057     }
1058     average[0] = round(sum[0] / nfiles,0);
1059     average[1] = round(sum[1] / nfiles,0);
1060     errors = stdDev(res_nbkg,nfiles,average[0]);
1061    
1062     cout << "Mean bkg estimated = " << average[0] << endl;
1063     cout << "Mean sig estimated = " << average[1] << endl;
1064     cout << "Standard deviation = " << errors << endl;
1065     cout << "Uncertainties = " << errors/average[0]*100. << "% " << endl;
1066    
1067     outTxtFile << "Estimated bkg = ";
1068     for (int jj=0; jj<nfiles; ++jj)
1069     outTxtFile << res_nbkg[jj] << " ";
1070     outTxtFile << endl;
1071     outTxtFile << "Mean bkg estimated = " << average[0] << endl;
1072     outTxtFile << "Mean sig estimated = " << average[1] << endl;
1073     outTxtFile << "Standard deviation = " << errors << endl;
1074     outTxtFile << "Uncertainties = " << errors/average[0]*100. << "% " << endl;
1075 jengbou 1.1 }
1076     outTxtFile.close();
1077     }