ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/tools/ikstest.cc
Revision: 1.10
Committed: Fri Dec 10 19:06:15 2010 UTC (14 years, 4 months ago) by jengbou
Content type: text/plain
Branch: MAIN
CVS Tags: gregj-20110224, gregj-20110223-v1, gregj-20110223, gregj-20110205, gregj-20110114, gregj-20110107, gregj-20101223, gregj-20101212, gregj-20101211, gregj-20101210-new, gregj-20101210, HEAD
Changes since 1.9: +24 -21 lines
Log Message:
update

File Contents

# Content
1 #include "ikstest.h"
2 #include "TopStyle/tdrstyle.C"
3 #include "TopStyle/CMSTopStyle.cc"
4
5 //=================================
6 // Program to run IKS test
7 // * Input directories:
8 // * Data: skimmed_Data_x.xxpb-1
9 // => default selection : Data_<suffix>.root
10 // => n-1 selection : Data_<suffix>_<invName>.root
11 // * MC : skimmed_MC {QCD,WJets,TTbar...}
12 // => default selection : QCD_<suffix>.root
13 // => n-1 selection : Data_<suffix>_<invName>.root
14 // * Upmost output dir specified by <desDir>
15 // subdirectory created according to <useInv> and <realData>
16 // => <desDir'><xyz><suffix>.pdf (.txt)
17 // * e.g. to do KS test on data with MC QCD shape in signal region:
18 // useInv=false; realData=true
19 //=================================
20
21 using namespace std;
22
23 //define the constants: 35./pb
24 double Lint_ = 35.;
25 TString sNames[] = {"QCD","WJets","TTbar","ZJets","STtch"};
26 const Double_t procQCD = 2.5;
27 const Double_t procWJets = 1.3;
28 const Double_t procTTbar = 1.;
29 const Double_t procZJets = 1.;
30 const Double_t procSTtch = 1.;
31
32 // Output directory
33 TString baseDir = "Results_34.72pb-1_";
34 TString jbinNames[] = {"Incl1","Incl2","Incl3","Incl4","Excl1","Excl2","Excl3"};
35 //TString jbinNames[] = {"Incl3"};
36 TString postDir = "_KSopt/";
37 // User defined parameters
38 bool realData = true;
39 bool useInv = true; // whether to use n-1 QCD template
40 bool plotInvSelShape = true; // whether to use shape from inverse selection for dataKS plots. Available when useInv is true
41 bool useIKSopt = true;
42 double yLowBound = 1.e-36;
43 // Ntuples to use
44 TString selNames[] = {"Sel51"}; // Suffix of selection
45 TString jetName = "PF";// Calo, JPT, PF
46 TString invNames[] = {"RelIsogt0p1ORD0gt0p02","RelIsogt0p15lt0p7ORD0gt0p025"};
47 TString invLabels[] = {"RelIso > 0.1 or d_{0} > 0.02","#splitline{0.1< RelIso <0.7 or}{d_{0} > 0.025 cm}"};
48 bool debug_ = false;
49 bool UseOverflow = true;
50 bool overwriteAll = true;
51
52 //=================================
53 // Main program
54 //=================================
55 void ikstest() {
56 map<TString,double> weight_;
57 weight_["QCD"] = 79688.0/4377187*Lint_; //QCD
58 weight_["WJets"] = 31314.0/10068895*Lint_*procWJets; //WJets
59 weight_["TTbar"] = 157.5/1483404*Lint_*procTTbar; //TTbar
60 weight_["ZJets"] = 3048.0/1084921*Lint_*procZJets; //ZJets
61 weight_["STtch"] = 20.93/528593*Lint_*procSTtch; //STtch
62
63 CMSTopStyle style;
64 setTDRStyle();
65 style.setupICHEPv1();
66 TLatex *latex = new TLatex();
67 latex->SetNDC();
68
69 int n_Invs_ = (useInv ? sizeof(invNames)/sizeof(invNames[0]) : 1);
70 int n_Sels_ = sizeof(selNames)/sizeof(selNames[0]);
71 int n_jbins_ = sizeof(jbinNames)/sizeof(jbinNames[0]);
72
73 cout << "size of selNames = " << n_Sels_ << endl;
74 cout << "size of jbinNames = " << n_jbins_ << endl;
75 cout << "size of invNames = " << n_Invs_ << endl;
76
77 for (int i = 0; i < n_jbins_; ++i){
78 struct stat stbDir;
79 TString tmpDir = baseDir + jbinNames[i] + postDir;
80 if (stat(tmpDir,&stbDir)){
81 cout << "Creating folder : " << tmpDir << endl;
82 if (mkdir(tmpDir,0755) == -1){
83 std::cerr << "Error creating folder" << endl;
84 return;
85 }
86 }
87 }
88
89 bool exclusive_ = false;
90 for (int ninv = 0; ninv < n_Invs_; ++ninv) {
91 TString invName = invNames[ninv];
92 for (int nsel = 0; nsel < n_Sels_; ++nsel) {
93 ostringstream ssel_;
94 ssel_ << nsel;
95 TString suffix0_ = selNames[nsel];
96
97 //open the files with histograms
98 map<string,TFile*> mfile;
99 map<TString,TCanvas*> cvs; // map of usual histogram
100 mfile.clear();
101 cvs.clear();
102 mfile["Data"] = TFile::Open(TString("merged_Data/34.72pb-1_v2/Data_"+suffix0_+"_"+jetName+"_TCHEM.root"));
103 // n-1 cuts
104 if (useInv) {
105 if (realData)
106 mfile["InvSel"] = TFile::Open(TString("merged_Data/34.72pb-1_v2/Data_"+suffix0_+"_"+invName+"_"+jetName+"_TCHEM.root"));
107 else
108 mfile["InvSel"] = TFile::Open(TString("skimmed_MC/V00-01-04-07_v2_PF/QCD_"+suffix0_+"_"+invName+"_"+jetName+"_TCHEM.root"));
109 }
110 // RefSel MC
111 mfile["0"] = TFile::Open(TString("skimmed_MC/V00-01-04-07_v2_PF/QCD_"+suffix0_+"_"+jetName+"_TCHEM.root"));
112 mfile["1"] = TFile::Open(TString("skimmed_MC/V00-01-04-07_v2_PF/WJets_"+suffix0_+"_"+jetName+"_TCHEM.root"));
113 mfile["2"] = TFile::Open(TString("skimmed_MC/V00-01-04-07_v2_PF/TTbar_"+suffix0_+"_"+jetName+"_TCHEM.root"));
114 mfile["3"] = TFile::Open(TString("skimmed_MC/V00-01-04-07_v2_PF/ZJets_"+suffix0_+"_"+jetName+"_TCHEM.root"));
115 mfile["4"] = TFile::Open(TString("skimmed_MC/V00-01-04-07_v2_PF/STtch_"+suffix0_+"_"+jetName+"_TCHEM.root"));
116
117 for (int njbin = 0; njbin < n_jbins_; ++njbin) {
118 exclusive_ = false;
119 if (jbinNames[njbin].Contains("Excl")) exclusive_ = true;
120 char nthjbin_ = jbinNames[njbin](jbinNames[njbin].Length()-1);
121
122 TString desDir;
123 TString tmpDir = baseDir + jbinNames[njbin] + postDir;
124 // create output directory
125 if (!useInv) {
126 if (!realData) desDir = tmpDir + "MC/";
127 else desDir = tmpDir + "Data_MC/";
128 } else {
129 if (!realData) desDir = tmpDir + "MC_"+invName+"/";
130 else desDir = tmpDir + "Data_"+invName+"/";
131 }
132
133 if (nsel == 0) {
134 struct stat stDir;
135 if (!stat(desDir,&stDir)){
136 cout << "\nOutput folder \"" << desDir
137 << "\" exists! Continue to overwrite it? (enter to continue; 'q' for quit)" << endl;
138 char incmd;
139 if (overwriteAll) incmd='a';
140 else cin.get(incmd);
141 if (incmd == 'q') return;
142 } else {
143 cout << "Creating folder : " << desDir << endl;
144 if (mkdir(desDir,0755) == -1){
145 std::cerr << "Error creating folder" << endl;
146 return;
147 }
148 }
149 }
150
151 TString suffix1_ = jbinNames[njbin] + "_" + selNames[nsel] + "_" + jetName;
152
153 ofstream outprint(TString(desDir+"Results_"+suffix1_+".txt"));
154
155 //define histograms and related parameters
156 TString histoName[] = {"h_mu_pt","h_met","h_mt"}; // h_mu_eta, h_Ht
157 const size_t nvars_ = sizeof(histoName)/sizeof(histoName[0]);
158 if (nvars_ == 1) useIKSopt = false;
159 cout << "===> Fit " << nvars_ << " variable(s): " << endl;
160
161 string histoTitle[nvars_];
162 string histoLabelX[nvars_];
163
164 //Histo binning
165 Int_t xbins[nvars_];
166 Double_t xlow[nvars_];
167 Double_t xhigh[nvars_];
168 string sample[5] = {"QCD","WJets","TTbar","ZJets","STtch"};
169
170 TH1F* hMC_[5][nvars_]; // MC histograms
171 TH1F* hData_[nvars_]; // Data or mix MC
172 TH1F* hQCD_NEW[nvars_]; // InvSel QCD shape
173 TH1F* hKSres_[nvars_];
174 TH1F* hKSvalues_[(nvars_ == 1 ? 1 : nvars_+1)];
175 TH1F* hQCD_KS[nvars_];
176 TH1F* hWJets_KS[nvars_];
177
178 if (useIKSopt) hKSvalues_[nvars_] = new TH1F(TString("productKSvalues_"+suffix1_),"",2./stepsize, stepsize, 2.+stepsize);
179
180 string suffixj_ = "_";
181 if (exclusive_) suffixj_ += "excl";
182
183 //load the histograms from the root files
184 for (size_t vi = 0; vi < nvars_; ++vi) {// nvars_ variables
185 ostringstream svi_;
186 svi_ << vi;
187
188 if (!useInv) {//use QCD MC sample
189 hQCD_NEW[vi] = (TH1F*) mfile["0"]->Get(TString(histoName[vi]+suffixj_+nthjbin_))->Clone();
190 hQCD_NEW[vi] -> Scale(weight_["QCD"]);
191 hQCD_NEW[vi] -> SetName(TString("InvSel_"+histoName[vi]+suffixj_+svi_.str()+"_"+nthjbin_));
192 }
193 else {
194 hQCD_NEW[vi] = (TH1F*) mfile["InvSel"]->Get(TString(histoName[vi]+suffixj_+nthjbin_))->Clone();
195 if (!realData) hQCD_NEW[vi] -> Scale(weight_["QCD"]);
196 hQCD_NEW[vi] -> SetName(TString("InvSel_"+histoName[vi]+suffixj_+svi_.str()+"_"+nthjbin_));
197 }
198
199 xbins[vi] = hQCD_NEW[vi]->GetNbinsX();
200 xlow[vi] = hQCD_NEW[vi]->GetXaxis()->GetXmin();
201 xhigh[vi] = hQCD_NEW[vi]->GetXaxis()->GetXmax();
202 histoTitle[vi] = hQCD_NEW[vi]->GetTitle();
203 histoLabelX[vi] = hQCD_NEW[vi]->GetXaxis()->GetTitle();
204 cout << histoTitle[vi] << ": nbins = " << xbins[vi] << "; x = (" << xlow[vi] << ", " << xhigh[vi] << ")" << endl;
205
206 if (debug_) cout << "hQCD_NEW[" << vi << "] @ " << hQCD_NEW[vi] << endl;
207 if (UseOverflow) {
208 hQCD_NEW[vi]->SetBinContent(xbins[vi],
209 hQCD_NEW[vi]->GetBinContent(xbins[vi]+1)+
210 hQCD_NEW[vi]->GetBinContent(xbins[vi]));
211 double err_ = sqrt(hQCD_NEW[vi]->GetBinError(xbins[vi]+1)*hQCD_NEW[vi]->GetBinError(xbins[vi]+1)+
212 hQCD_NEW[vi]->GetBinError(xbins[vi])*hQCD_NEW[vi]->GetBinError(xbins[vi]));
213 hQCD_NEW[vi]->SetBinError(xbins[vi],err_);
214 }
215 TString nameNewHisto = "mix_"+histoName[vi]+suffixj_+nthjbin_;
216 TString nameNewHistoSFKS = "finalSF_"+histoName[vi]+suffixj_+nthjbin_;
217 TString nameNewHistoKSvalues = "KSvalues_"+histoLabelX[vi]+suffixj_+nthjbin_;
218 hData_[vi] = new TH1F(nameNewHisto,"",xbins[vi],xlow[vi],xhigh[vi]);
219 hKSres_[vi] = new TH1F(nameNewHistoSFKS,"",xbins[vi],xlow[vi],xhigh[vi]);
220 hKSvalues_[vi] = new TH1F(nameNewHistoKSvalues,"",2./stepsize, stepsize, 2.+stepsize);
221 hData_[vi]->Sumw2();
222 hKSres_[vi]->Sumw2();
223 hKSvalues_[vi]->Sumw2();
224 }
225
226 for (size_t n = 0; n < 5; ++n) {// MC samples
227 for (size_t ihisto = 0; ihisto < nvars_; ihisto++) {// nvars_ variables
228 //TString histo_name = histoName[ihisto]+suffixj_+nthjbin_+"_"+sample[n];
229 //cout << "[histo] " << histo_name << endl;
230 ostringstream ss_;
231 ss_ << n;
232
233 hMC_[n][ihisto] = (TH1F*) mfile[ss_.str()]->Get(TString(histoName[ihisto]+suffixj_+nthjbin_))->Clone();
234 if (UseOverflow) {
235 hMC_[n][ihisto]->SetBinContent(xbins[ihisto],
236 hMC_[n][ihisto]->GetBinContent(xbins[ihisto]+1)+
237 hMC_[n][ihisto]->GetBinContent(xbins[ihisto]));
238 double err_ = sqrt(hMC_[n][ihisto]->GetBinError(xbins[ihisto]+1)*hMC_[n][ihisto]->GetBinError(xbins[ihisto]+1)+
239 hMC_[n][ihisto]->GetBinError(xbins[ihisto])*hMC_[n][ihisto]->GetBinError(xbins[ihisto]));
240 hMC_[n][ihisto]->SetBinError(xbins[ihisto],err_);
241 }
242 if (debug_) {
243 cout << "File[" << n << "] @" << mfile[ss_.str()]
244 << "; histo[" << ihisto << "] @ " << mfile[ss_.str()]->Get(TString(histoName[ihisto]+suffixj_+nthjbin_))
245 <<"; hMC_[" << n << "][" << ihisto << "] raw evts = "
246 << setw(12) << hMC_[n][ihisto]->Integral();
247 }
248
249 hMC_[n][ihisto] -> Scale(weight_[sNames[n]]);
250 hMC_[n][ihisto] -> SetName(TString("MC_"+sample[n]+"_"+histoName[ihisto]+suffixj_+nthjbin_));
251 if (debug_) cout << "; weighted num evts = " << setw(8) << hMC_[n][ihisto]->Integral() << endl;
252 }
253 }
254
255 //create the mixed samples = "data"
256 TString cvsName0 = "Data";
257 if (useInv) {
258 cvsName0 += "_";
259 cvsName0 += invName;
260 }
261 cvsName0 += TString("_" + ssel_.str() + jbinNames[njbin] + "_" + jetName);
262 cvs[cvsName0] = new TCanvas(cvsName0,"Data distributions",300*nvars_,600);
263 cvs[cvsName0]->Divide(nvars_,1);
264 for (size_t i = 0; i < nvars_; i++) {
265 cvs[cvsName0]->cd(i+1);
266 if (!realData) {
267 hData_[i] -> Add(hMC_[0][i],hMC_[1][i], procQCD,procWJets);
268 hData_[i] -> Add(hMC_[2][i], procTTbar);
269 hData_[i] -> Add(hMC_[3][i], procZJets);
270 hData_[i] -> Add(hMC_[4][i], procSTtch);
271 }
272 else {
273 TH1F *htmp = (TH1F*) mfile["Data"]->Get(TString(histoName[i]+suffixj_+nthjbin_));
274 hData_[i] -> Add(htmp,1.);
275 if (UseOverflow) {
276 hData_[i]->SetBinContent(xbins[i],
277 hData_[i]->GetBinContent(xbins[i]+1)+
278 hData_[i]->GetBinContent(xbins[i]));
279 double err_ = sqrt(hData_[i]->GetBinError(xbins[i]+1)*hData_[i]->GetBinError(xbins[i]+1)+
280 hData_[i]->GetBinError(xbins[i])*hData_[i]->GetBinError(xbins[i]));
281 hData_[i]->SetBinError(xbins[i],err_);
282 }
283 }
284 hData_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
285 hData_[i]->GetYaxis()->SetTitle("Events");
286 if (histoName[i].Contains("mu_pt")) hData_[i]->GetXaxis()->SetRangeUser(20.,xhigh[0]);
287 if (histoName[i].Contains("met")) hData_[i]->GetXaxis()->SetRange((size_t)findFirstBinAbove(0.,hData_[i]),xbins[i]);
288 hData_[i]->DrawClone("hist");
289 }
290 //cvs[cvsName0]->SaveAs(TString(desDir+"Data_distributions_"+selNames[nsel]+".pdf"));
291 //cvs[cvsName0]->SaveAs(TString(desDir+"Data_distributions_"+selNames[nsel]+".png"));
292
293 //Calculate num of events for each sample
294 vector<double> vNev_;
295 double errNevt[8];
296 double NevData = hData_[nvars_-1]->IntegralAndError(1,xbins[nvars_-1],errNevt[0]);
297 double corr_NevQCD = hMC_[0][nvars_-1]->IntegralAndError(1,xbins[nvars_-1],errNevt[1]);
298 double corr_NevQCD_NEW = hQCD_NEW[nvars_-1]->IntegralAndError(1,xbins[nvars_-1],errNevt[2]);
299 double corr_NevWJets = hMC_[1][nvars_-1]->IntegralAndError(1,xbins[nvars_-1],errNevt[3]);
300 double corr_NevTTbar = hMC_[2][nvars_-1]->IntegralAndError(1,xbins[nvars_-1],errNevt[4]);
301 double corr_NevZJets = hMC_[3][nvars_-1]->IntegralAndError(1,xbins[nvars_-1],errNevt[5]);
302 double corr_NevSTtch = hMC_[4][nvars_-1]->IntegralAndError(1,xbins[nvars_-1],errNevt[6]);
303 cout << "corr_NevSTtch = " << corr_NevSTtch << endl;
304 double corr_Nevmix = corr_NevQCD + corr_NevWJets + corr_NevTTbar
305 + corr_NevZJets + corr_NevSTtch;//should equal NevData for MC
306 errNevt[7] = sqrt(errNevt[1]*errNevt[1]+errNevt[3]*errNevt[3]+errNevt[4]*errNevt[4]+
307 errNevt[5]*errNevt[5]+errNevt[6]*errNevt[6]);
308
309 // store nev in a vector
310 vNev_.push_back(NevData);
311 vNev_.push_back((useInv ? corr_NevQCD_NEW : corr_NevQCD));
312 vNev_.push_back(corr_NevWJets);
313
314 // Non WJets (use MC expected values):
315 if (procTTbar > 0.) vNev_.push_back(corr_NevTTbar*procTTbar);
316 if (procZJets > 0.) vNev_.push_back(corr_NevZJets*procZJets);
317 if (procSTtch > 0.) vNev_.push_back(corr_NevSTtch*procSTtch);
318
319 outprint << "Events in Data = " << NevData << " +/- " << errNevt[0] << endl;
320 outprint << "Events QCD sample = " << corr_NevQCD << " +/- " << errNevt[1] << endl;
321 outprint << "Events InvSel sample = " << corr_NevQCD_NEW << " +/- " << errNevt[2] << endl;
322 outprint << "---------------------------" << endl;
323 outprint << "Events WJets sample = " << corr_NevWJets << " +/- " << errNevt[3] << endl;
324 outprint << "Events TTbar sample = " << corr_NevTTbar << " +/- " << errNevt[4] << endl;
325 outprint << "Events ZJets sample = " << corr_NevZJets << " +/- " << errNevt[5] << endl;
326 outprint << "Events STtch sample = " << corr_NevSTtch << " +/- " << errNevt[6] << endl;
327 outprint << "Total MC Events = " << corr_Nevmix << " +/- " << errNevt[7] << endl;
328
329 //define the containers for chosen numbers (coressponding to the max KStest result)
330 testMC maxProb[nvars_];
331
332 //define the scale factors calculated using information obtained from all parameters
333 Double_t SFbackg = 0.0;
334 Double_t sumSFbackg = 0.0;
335 Double_t SFsample = 0.0;
336 Double_t sumSFsample = 0.0;
337 Double_t allKS = 0.0;
338
339 vector<testMC> resultsKS[nvars_];
340 //do the KS test by varying the scale factors
341 for (size_t i = 0; i < nvars_; i++) { // nvars_ variables
342 TH1F *data = (TH1F*)hData_[i]->Clone("data");
343 data -> SetName("dataClone");
344 map<string,TH1F*> mHisto_;
345 mHisto_.clear();
346 mHisto_["Data"] = data;
347 mHisto_["QCD"] = (useInv ? (TH1F*)hQCD_NEW[i]->Clone() : (TH1F*)hMC_[0][i]->Clone());
348 mHisto_["WJets"] = (TH1F*)hMC_[1][i]->Clone();//WJets
349 if (procTTbar > 0.) {
350 mHisto_["TTbar"] = (TH1F*)hMC_[2][i]->Clone();
351 mHisto_["TTbar"]->Scale(procTTbar);
352 }
353 if (procZJets > 0.) {
354 mHisto_["ZJets"] = (TH1F*)hMC_[3][i]->Clone();
355 mHisto_["ZJets"]->Scale(procZJets);
356 }
357 if (procSTtch > 0.) {
358 mHisto_["STtch"] = (TH1F*)hMC_[4][i]->Clone();
359 mHisto_["STtch"]->Scale(procSTtch);
360 }
361
362 //data -> Scale(1./data->Integral());
363 resultsKS[i] = doKStest(vNev_,mHisto_);
364 testMC tksmax = getMax(resultsKS[i]);
365 maxProb[i] = tksmax;
366 outprint << "\nFor the plot " << histoLabelX[i] << " the results are:"<< endl;
367 outprint << "\tmax Probability = " << maxProb[i].prob << endl;
368 outprint << "\tproc_background = " << maxProb[i].scaleF_backg << endl;
369 outprint << "\tproc_sample = " << maxProb[i].scaleF_sample << endl;
370
371 outprint << "\n\tpercent_B of Data = "
372 << maxProb[i].scaleF_backg*corr_NevQCD_NEW*100/NevData << endl;
373 outprint << "\tpercent_S of Data = "
374 << maxProb[i].scaleF_sample*corr_NevWJets*100/NevData << endl;
375 outprint << "---------------------------" << endl;
376
377 //create the mixed samples with KS test results
378 sumSFbackg += maxProb[i].prob*maxProb[i].scaleF_backg;
379 sumSFsample += maxProb[i].prob*maxProb[i].scaleF_sample;
380 allKS += maxProb[i].prob;
381
382 //fill a histogram with the results from the KS test for each variable
383 for (size_t jiter = 0; jiter < resultsKS[i].size(); jiter++) {
384 if (resultsKS[i].at(jiter).prob == 1.)
385 cout << "variable [" << i << "]: prob[" << jiter << "]= " << resultsKS[i].at(jiter).prob << endl;
386 hKSvalues_[i]->SetBinContent(jiter,resultsKS[i].at(jiter).prob);
387 }
388 delete data;
389 }
390 double SF_QCD = 0.;
391 //calculate the final scale factors
392 if (!useIKSopt) {
393 SFbackg = sumSFbackg/allKS;
394 SFsample = sumSFsample/allKS;
395 SF_QCD = SFbackg*corr_NevQCD_NEW/corr_NevQCD;
396 outprint << "allKS = " << allKS << "\tbackground = " << SFbackg << "\tsample = " << SFsample << endl;
397 outprint << "==> Scale Factor for QCD MC = " << SF_QCD << endl;
398 outprint << "\tcombined percent_B of Data = "
399 << SFbackg*corr_NevQCD_NEW*100/NevData << endl;
400 outprint << "\tcombined percent_S of Data = "
401 << SFsample*corr_NevWJets*100/NevData << endl;
402 outprint << "\n" << endl;
403 outprint << "=================================" << endl;
404 outprint << "\n" << endl;
405 } else {
406 testMC tksopt = getMaxL(resultsKS);
407 SFbackg = tksopt.scaleF_backg;
408 SFsample = tksopt.scaleF_sample;
409 SF_QCD = SFbackg*corr_NevQCD_NEW/corr_NevQCD;
410 outprint << "allKS (KSopt) = " << tksopt.prob
411 << "\tbackground = " << SFbackg << "\tsample = " << SFsample << endl;
412 outprint << "==> Scale Factor for QCD MC = " << SF_QCD << endl;
413 outprint << "\tcombined percent_B of Data = "
414 << SFbackg*corr_NevQCD_NEW*100/NevData << endl;
415 outprint << "\tcombined percent_S of Data = "
416 << SFsample*corr_NevWJets*100/NevData << endl;
417 outprint << "\n" << endl;
418 outprint << "=================================" << endl;
419 outprint << "\n" << endl;
420
421 for (size_t jiter = 0; jiter < resultsKS[0].size(); jiter++) {
422 double val_ = 1.;
423 for (size_t nvi = 0; nvi < nvars_; nvi++) {
424 val_ *= resultsKS[nvi].at(jiter).prob;
425 }
426 hKSvalues_[nvars_]->SetBinContent(jiter,val_);
427 //cout << "prod of IKS prob = " << val_ << endl;
428 }
429 }
430
431 //=================================
432 // Plots
433 //=================================
434 for (size_t i = 0; i < nvars_; i++) {// nvars_ variables
435 gStyle->SetErrorX(0.);
436 hKSres_[i] -> Add((TH1F*)hQCD_NEW[i]->Clone(),(TH1F*)hMC_[1][i]->Clone(),SFbackg,SFsample);
437 hKSres_[i] -> Add((TH1F*)hMC_[2][i]->Clone(),procTTbar);
438 hKSres_[i] -> Add((TH1F*)hMC_[3][i]->Clone(),procZJets);
439 hKSres_[i] -> Add((TH1F*)hMC_[4][i]->Clone(),procSTtch);
440
441 outprint << "hKSres->Integral() = " << hKSres_[i]->Integral() << endl;
442 outprint << "Data->Integral() = " << hData_[i]->Integral() << endl;
443
444 hData_[i]->Rebin(2);
445 hQCD_NEW[i]->Rebin(2);
446 hMC_[0][i]->Rebin(2);
447 hMC_[1][i]->Rebin(2);
448 hKSres_[i]->Rebin(2);
449 //hKSvalues_[i]->Rebin(2);
450
451 // Stack Wjets and QCD after scaled by KS factors
452 if (useInv && plotInvSelShape) {
453 hQCD_KS[i] = (TH1F*) hQCD_NEW[i]->Clone();
454 hQCD_KS[i]->Scale(SFbackg);
455 }
456 else {// Use MC QCD
457 hQCD_KS[i] = (TH1F*) hMC_[0][i]->Clone();
458 hQCD_KS[i]->Scale(SF_QCD);
459 }
460 hQCD_KS[i]->SetLineColor(style.QCDColor);
461 hQCD_KS[i]->SetFillColor(style.QCDColor);
462 hQCD_KS[i]->SetFillStyle(style.QCDFill);
463
464 hWJets_KS[i] = (TH1F*) hMC_[1][i]->Clone();
465 hWJets_KS[i]->Scale(SFsample);
466 hWJets_KS[i]->SetLineColor(style.WJetsColor);
467 hWJets_KS[i]->SetFillColor(style.WJetsColor);
468 hWJets_KS[i]->SetFillStyle(style.WJetsFill);
469
470 if (procTTbar > 0.) {
471 hMC_[2][i]->Rebin(2);
472 hMC_[2][i]->SetLineColor(style.TtbarColor);
473 hMC_[2][i]->SetFillColor(style.TtbarColor);
474 hMC_[2][i]->SetFillStyle(style.TtbarFill);
475 }
476 if (procZJets > 0.) {
477 hMC_[3][i]->Rebin(2);
478 hMC_[3][i]->SetLineColor(style.DYZJetsColor);
479 hMC_[3][i]->SetFillColor(style.DYZJetsColor);
480 hMC_[3][i]->SetFillStyle(style.DYZJetsFill);
481 }
482 if (procSTtch > 0.) {
483 hMC_[4][i]->Rebin(2);
484 hMC_[4][i]->SetLineColor(style.ST_tColor);
485 hMC_[4][i]->SetFillColor(style.ST_tColor);
486 hMC_[4][i]->SetFillStyle(style.ST_tFill);
487 }
488
489 THStack *hst = new THStack(invName,invName);
490 hst->Add((TH1F*)hQCD_KS[i]->Clone());
491 if (procSTtch > 0) {
492 TH1F *tmp = (TH1F*) hMC_[4][i]->Clone();
493 tmp->Scale(procSTtch);
494 hst->Add(tmp);
495 }
496 if (procZJets > 0) {
497 TH1F *tmp = (TH1F*) hMC_[3][i]->Clone();
498 tmp->Scale(procZJets);
499 hst->Add(tmp);
500 }
501 hst->Add((TH1F*)hWJets_KS[i]->Clone());
502 if (procTTbar > 0) {
503 TH1F *tmp = (TH1F*) hMC_[2][i]->Clone();
504 tmp->Scale(procTTbar);
505 hst->Add(tmp);
506 }
507
508 // Set plotting parameters
509 hData_[i] ->SetLineColor(1);
510 hQCD_NEW[i] ->SetLineColor(2);
511 hMC_[0][i] ->SetLineColor(4);
512 hMC_[1][i] ->SetLineColor(3);
513 hKSres_[i] ->SetLineColor(9);
514 hKSvalues_[i]->SetLineColor(i+1);
515
516 hData_[i] ->SetMarkerColor(1);
517 hQCD_NEW[i] ->SetMarkerColor(2);
518 hMC_[0][i] ->SetMarkerColor(4);
519 hMC_[1][i] ->SetMarkerColor(3);
520 hKSres_[i] ->SetMarkerColor(9);
521 hKSvalues_[i]->SetMarkerColor(i+1);
522
523 hData_[i] ->SetMarkerStyle((realData ? 20 : 24));
524 hQCD_NEW[i] ->SetMarkerStyle(20);
525 hMC_[0][i] ->SetMarkerStyle(20);
526 hMC_[1][i] ->SetMarkerStyle(20);
527 hKSres_[i] ->SetMarkerStyle(20);
528 hKSvalues_[i]->SetMarkerStyle(20);
529
530 hData_[i] ->SetMarkerSize((realData ? 1.2 : 1.4));
531 hQCD_NEW[i] ->SetMarkerSize(1.1);
532 hMC_[0][i] ->SetMarkerSize(1.1);
533 hMC_[1][i] ->SetMarkerSize(1.1);
534 hKSres_[i] ->SetMarkerSize(1.18);
535 hKSvalues_[i]->SetMarkerSize(1.1);
536
537 hData_[i] ->SetStats(0);
538 hQCD_NEW[i] ->SetStats(0);
539 hMC_[0][i] ->SetStats(0);
540 hMC_[1][i] ->SetStats(0);
541 hKSres_[i] ->SetStats(0);
542 hKSvalues_[i]->SetStats(0);
543 hQCD_KS[i] ->SetStats(0);
544 hWJets_KS[i] ->SetStats(0);
545
546 hKSres_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
547 hKSres_[i]->GetYaxis()->SetTitle("Events");
548 hKSvalues_[i]->GetXaxis()->SetTitle("#lambda_{wjets}");
549 hKSvalues_[i]->GetYaxis()->SetTitle("KS test values");
550 hMC_[0][i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
551 hMC_[0][i]->GetYaxis()->SetTitle("A.U.");
552
553 TLatex* text1 = new TLatex(1., 1., "35 pb^{-1} at #sqrt{s} = 7 TeV");
554 text1->SetNDC();
555 text1->SetTextAlign(13);
556 text1->SetX(0.58);
557 text1->SetY(0.91);
558 text1->SetTextFont(42);
559 text1->SetTextSize(0.032);
560 text1->SetTextSizePixels(20);// dflt=28
561
562 // QCD shape comparison
563 TString cvsName1 = histoName[i]+"_QCD";
564 double val_Max_ = 0.;
565
566 if(useInv) cvsName1 = cvsName1 + "_" + invName;
567 cvsName1 += TString("_" + ssel_.str() + jbinNames[njbin] + "_" + jetName);
568 cvs[cvsName1] = new TCanvas(cvsName1,"");
569 hQCD_NEW[i] -> Scale(1./hQCD_NEW[i]->Integral());
570 hMC_[0][i] -> Scale(1./hMC_[0][i]->Integral());
571 hMC_[1][i] -> Scale(1./hMC_[1][i]->Integral());
572 outprint << "For " << histoName[i] << " , the KStest result btw MC_QCD/InvSel is = "
573 << hMC_[0][i] -> KolmogorovTest(hQCD_NEW[i],"") << endl;
574
575 hMC_[0][i]->GetXaxis()->SetLabelFont(42);
576 hMC_[0][i]->GetXaxis()->SetLabelSize(0.04);
577 hMC_[0][i]->GetXaxis()->SetTitleFont(42);
578 hMC_[0][i]->GetXaxis()->SetTitleSize(0.05);
579 hMC_[0][i]->GetXaxis()->SetTitleOffset(1.2);
580 hMC_[0][i]->GetYaxis()->SetLabelFont(42);
581 hMC_[0][i]->GetYaxis()->SetLabelSize(0.04);
582 hMC_[0][i]->GetYaxis()->SetTitleFont(42);
583 hMC_[0][i]->GetYaxis()->SetTitleSize(0.05);
584 hMC_[0][i]->GetYaxis()->SetTitleOffset(1.3);
585 // Get better Y range
586 Int_t nbMax = hMC_[0][i]->GetMaximumBin();
587 double tmpval = hMC_[0][i]->GetBinContent(nbMax) + hMC_[0][i]->GetBinError(nbMax);
588 val_Max_ = (val_Max_ > 1.2*tmpval ? val_Max_ : 1.2*tmpval);
589
590 if (useInv) {
591 nbMax = hQCD_NEW[i]->GetMaximumBin();
592 tmpval = hQCD_NEW[i]->GetBinContent(nbMax) + hQCD_NEW[i]->GetBinError(nbMax);
593 val_Max_ = (val_Max_ > 1.2*tmpval ? val_Max_ : 1.2*tmpval);
594 }
595 hMC_[0][i]->GetYaxis()->SetRangeUser(0.,(val_Max_ > 1. ? 1. : val_Max_));
596 if (histoName[i].Contains("mu_pt")) hMC_[0][i]->GetXaxis()->SetRangeUser(20.,xhigh[0]);
597 if (histoName[i].Contains("met")) hMC_[0][i]->GetXaxis()->SetRange((size_t)findFirstBinAbove(0.,hData_[i]),xbins[i]);
598 hMC_[0][i]->DrawCopy("histe");
599
600 if (useInv)
601 hQCD_NEW[i]->DrawCopy("samehiste");
602
603 hMC_[1][i]->DrawCopy("samehiste");
604 TLegend *legend1 = new TLegend(0.58, 0.65, 0.92, 0.9);
605 legend1->SetFillColor(0);
606 legend1->SetFillStyle(0);
607
608 legend1->AddEntry(hMC_[0][i], "QCD");
609 if (useInv)
610 if (!realData) legend1->AddEntry(hQCD_NEW[i], TString("QCD (" + invLabels[ninv] + ")"));
611 else legend1->AddEntry(hQCD_NEW[i], TString("Data (" + invLabels[ninv]) + ")");
612
613 legend1->AddEntry(hMC_[1][i], style.WJetsText);
614 legend1->Draw();
615 legend1->SetFillColor(kWhite);
616 //latex->DrawLatex(0.22,0.91,histoName[i].c_str());
617 //cvs[cvsName1]->SetLogy();
618 TString nameCanvas1 = desDir+histoName[i]+"_QCD_"+suffix1_;
619 cvs[cvsName1]->SaveAs(nameCanvas1+".pdf");
620 cvs[cvsName1]->SaveAs(nameCanvas1+".png");
621
622
623 // KS results vs. (pseudo) Data
624 TString cvsName2 = histoName[i]+"_dataKS";
625 if(useInv) cvsName2 = cvsName2 + "_" + invName;
626 cvsName2 += TString("_" + ssel_.str() + jbinNames[njbin] + "_" + jetName);
627 cvs[cvsName2] = new TCanvas(cvsName2,"");
628
629 // Get better Y range
630 val_Max_ = 0.;
631 nbMax = hData_[i]->GetMaximumBin();
632 tmpval = hData_[i]->GetBinContent(nbMax) + hData_[i]->GetBinError(nbMax);
633 val_Max_ = (val_Max_ > 1.05*tmpval ? val_Max_ : 1.05*tmpval);
634 nbMax = hKSres_[i]->GetMaximumBin();
635 tmpval = hKSres_[i]->GetBinContent(nbMax) + hKSres_[i]->GetBinError(nbMax);
636 val_Max_ = (val_Max_ > 1.05*tmpval ? val_Max_ : 1.05*tmpval);
637
638 hData_[i]->GetYaxis()->SetRangeUser(0.,val_Max_);
639
640 if (histoName[i].Contains("mu_pt")) hData_[i]->GetXaxis()->SetRangeUser(20.,xhigh[i]);
641 if (histoName[i].Contains("met")) hData_[i]->GetXaxis()->SetRange((size_t)findFirstBinAbove(0.,hData_[i]),xbins[i]);
642
643 hData_[i]->Draw("P");
644 hst->Draw("samehist");
645 hKSres_[i]->Draw("samehiste");
646 hData_[i]->Draw("sameP");
647 TLegend *legend2 = new TLegend(0.56, 0.45, 0.9, 0.88);
648 legend2->AddEntry(hData_[i], (realData ? "Data" : "Pseudo Data"),"p");
649 legend2->AddEntry(hKSres_[i], "KS result");
650 if (procTTbar > 0.) legend2->AddEntry(hMC_[2][i], style.TtbarText);
651 legend2->AddEntry(hWJets_KS[i], style.WJetsText);
652 if (procZJets > 0.) legend2->AddEntry(hMC_[3][i], style.DYZJetsText);
653 if (procSTtch > 0.) legend2->AddEntry(hMC_[4][i], style.ST_tText);
654 if (realData)
655 legend2->AddEntry(hQCD_KS[i], ((!plotInvSelShape || !useInv) ? style.QCDText :
656 TString("#splitline{Data (" + invLabels[ninv] + ")}{Scaled by IKS}")));
657 else
658 legend2->AddEntry(hQCD_KS[i], ((!plotInvSelShape || !useInv) ? style.QCDText :
659 TString("QCD (" + invLabels[ninv] + ")")));
660 legend2->SetFillColor(0);
661 legend2->SetFillStyle(0);
662 legend2->Draw();
663 legend2->SetFillColor(kWhite);
664 //latex->DrawLatex(0.22,0.91,histoName[i].c_str());
665 //cvs[cvsName2]->SetLogy();
666 text1->Draw();
667 gPad->RedrawAxis();
668 TString nameCanvas2 = desDir+histoName[i]+"_dataKS_"+suffix1_;
669 cvs[cvsName2]->SaveAs(nameCanvas2+".pdf");
670 cvs[cvsName2]->SaveAs(nameCanvas2+".png");
671
672 }
673
674 // KS fit results
675 TString cvsName3 = "KStestValues";
676 if(useInv) cvsName3 = cvsName3 + "_" + invName;
677 cvsName3 += TString("_" + ssel_.str() + jbinNames[njbin] + "_" + jetName);
678 cvs[cvsName3] = new TCanvas(cvsName3,"");
679 // Zoom-in
680 Int_t nbins_ = hKSvalues_[0]->GetNbinsX();;
681 Int_t nbMax_ = 1;
682 Int_t nbMin_ = nbins_;
683 double val_Min_ = 1.;
684 for (size_t i = 0; i < nvars_; ++i) {
685 Int_t tmp = findFirstBinAbove(yLowBound, hKSvalues_[i]);
686 val_Min_ = (hKSvalues_[i]->GetBinContent(tmp) < 0.8*val_Min_ ?
687 hKSvalues_[i]->GetBinContent(tmp) : 0.8*val_Min_);
688 nbMin_ = (nbMin_ < tmp ? nbMin_ : tmp);
689 tmp = findLastBinAbove(0., hKSvalues_[i]);
690 nbMax_ = (nbMax_ > tmp ? nbMax_ : tmp);
691 }
692 nbMin_ -= (0.1/stepsize);
693 nbMax_ += (0.1/stepsize);
694
695 hKSvalues_[0]->GetXaxis()->SetRange(nbMin_,nbMax_);
696 hKSvalues_[0]->GetYaxis()->SetRangeUser((1e-36 < val_Min_ ? val_Min_ : 1e-36),1.1);
697 hKSvalues_[0]->Draw();
698 if (nvars_ > 1)
699 for (size_t i = 1; i < nvars_; i++) {
700 hKSvalues_[i]->Draw("same");
701 }
702
703 TLegend *legend3 = new TLegend(0.2, (0.92-nvars_*0.07), 0.38, 0.92);
704 for (size_t i = 0; i < nvars_; i++)
705 legend3->AddEntry(hKSvalues_[i], TString(histoTitle[i]));
706
707 legend3->SetFillColor(0);
708 legend3->SetFillStyle(0);
709 legend3->Draw();
710 legend3->SetFillColor(kWhite);
711 //latex->DrawLatex(0.22,0.91,"KS test values");
712 cvs[cvsName3]->SetLogy();
713 TString nameCanvas3 = desDir+"KStestValues_newQCD_"+suffix1_;
714 cvs[cvsName3]->SaveAs(nameCanvas3+".pdf");
715 cvs[cvsName3]->SaveAs(nameCanvas3+".png");
716
717 // KSopt
718 if (useIKSopt) {
719 TString cvsName4 = "KStestValues";
720 if(useInv) cvsName4 = cvsName4 + "_" + invName;
721 cvsName4 += TString("_" + ssel_.str() + jbinNames[njbin] + "_" + jetName + "_KSopt");
722
723 cvs[cvsName4] = new TCanvas(cvsName4,"");
724 // Zoom-in
725 nbins_ = hKSvalues_[nvars_]->GetNbinsX();;
726 nbMax_ = 1;
727 nbMin_ = nbins_;
728 val_Min_ = 1.;
729 Int_t tmp = findFirstBinAbove(1.e-50, hKSvalues_[nvars_]);
730 val_Min_ = (hKSvalues_[nvars_]->GetBinContent(tmp) < 0.8*val_Min_ ?
731 hKSvalues_[nvars_]->GetBinContent(tmp) : 0.8*val_Min_);
732 nbMin_ = (nbMin_ < tmp ? nbMin_ : tmp);
733 tmp = findLastBinAbove(0., hKSvalues_[nvars_]);
734 nbMax_ = (nbMax_ > tmp ? nbMax_ : tmp);
735 nbMin_ -= (0.1/stepsize);
736 nbMax_ += (0.1/stepsize);
737
738 hKSvalues_[nvars_]->GetXaxis()->SetRange(nbMin_,nbMax_);
739 //hKSvalues_[nvars_]->GetYaxis()->SetRangeUser(1.e-100,1.e-50);
740 hKSvalues_[nvars_]->GetXaxis()->SetTitle("#lambda_{wjets}");
741 hKSvalues_[nvars_]->SetLineColor(4);
742 hKSvalues_[nvars_]->SetMarkerColor(4);
743 hKSvalues_[nvars_]->SetMarkerStyle(20);
744 hKSvalues_[nvars_]->SetMarkerSize(1.1);
745 hKSvalues_[nvars_]->SetStats(0);
746 hKSvalues_[nvars_]->Draw();
747
748 TLegend *legend4 = new TLegend(0.2, 0.75, 0.6, 0.93);
749 legend4->AddEntry(hKSvalues_[nvars_], "Product of KS test values");
750 legend4->SetFillColor(0);
751 legend4->SetFillStyle(0);
752 legend4->Draw();
753 legend4->SetFillColor(kWhite);
754 cvs[cvsName4]->SetLogy();
755 TString nameCanvas4 = desDir+"KStestValues_newQCD_"+suffix1_+"_KSopt";
756 cvs[cvsName4]->SaveAs(nameCanvas4+".pdf");
757 cvs[cvsName4]->SaveAs(nameCanvas4+".png");
758 }
759 } // End of njbin loop
760 } // End of nsel loop
761 } // End of ninv loop
762 }