1 |
< |
#include <vector> |
2 |
< |
#include <TFile.h> |
3 |
< |
#include <TH1.h> |
4 |
< |
#include <TH2.h> |
5 |
< |
#include <TLatex.h> |
6 |
< |
#include <TCanvas.h> |
7 |
< |
#include <TMath.h> |
8 |
< |
#include <TLegend.h> |
9 |
< |
#include <TObject.h> |
10 |
< |
#include <iostream> |
11 |
< |
#include <fstream> |
12 |
< |
#include <iomanip> |
1 |
> |
#include "ikstest.h" |
2 |
> |
#include "TopStyle/CMSTopStyle.cc" |
3 |
> |
|
4 |
> |
//================================= |
5 |
> |
// Program to run IKS test |
6 |
> |
// * Input directories: |
7 |
> |
// * Data: skimmed_Data_x.xxpb-1 |
8 |
> |
// => default selection : Data_<suffix>.root |
9 |
> |
// => n-1 selection : Data_<suffix>_<invName>.root |
10 |
> |
// * MC : skimmed_MC {QCD,WJets,TTbar...} |
11 |
> |
// => default selection : QCD_<suffix>.root |
12 |
> |
// => n-1 selection : Data_<suffix>_<invName>.root |
13 |
> |
// * Upmost output dir specified by <desDir> |
14 |
> |
// subdirectory created according to <useInv> and <realData> |
15 |
> |
// => <desDir'><xyz><suffix>.pdf (.txt) |
16 |
> |
// * e.g. to do KS test on data with MC QCD shape in signal region: |
17 |
> |
// useInv=false; realData=true |
18 |
> |
//================================= |
19 |
|
|
20 |
|
using namespace std; |
21 |
|
|
22 |
< |
//define the constants |
23 |
< |
double const weight_QCD = 0.015292368; |
24 |
< |
double const weight_Wjets = 0.002665824; |
25 |
< |
double const weight_ttjets = 0.00009072; |
26 |
< |
Double_t const procQCD = 1.51; |
27 |
< |
Double_t const procWjets = 1.05; |
22 |
< |
Double_t const procttjets = 1.0; |
23 |
< |
|
24 |
< |
struct testMC { |
25 |
< |
testMC(Double_t p = 0., Double_t sb = 0., Double_t ss = 0.){prob=p; scaleF_backg = sb; scaleF_sample = ss;} |
26 |
< |
Double_t prob; |
27 |
< |
Double_t scaleF_backg; |
28 |
< |
Double_t scaleF_sample; |
22 |
> |
//define the constants: 2.88/pb |
23 |
> |
const Double_t weight_[5] = {0.0524313, //QCD |
24 |
> |
0.0089567, //WJets |
25 |
> |
0.000306, //TTbar |
26 |
> |
0.0080911, //ZJets |
27 |
> |
0.000114 //STtch |
28 |
|
}; |
29 |
+ |
const Double_t procQCD = 1.46; |
30 |
+ |
const Double_t procWJets = 1.03; |
31 |
+ |
const Double_t procTTbar = 1.; |
32 |
+ |
const Double_t procZJets = 1.; |
33 |
+ |
const Double_t procSTtch = 1.; |
34 |
+ |
|
35 |
+ |
// Output directory |
36 |
+ |
TString baseDir = "Results_2.88pb-1_NEW/"; |
37 |
+ |
// User defined parameters |
38 |
+ |
bool useInv = false; // whether to use n-1 QCD template |
39 |
+ |
bool realData = false; |
40 |
+ |
// Ntuples to use |
41 |
+ |
TString suffix = "Sel0"; // Suffix of selection |
42 |
+ |
TString invNames[2] = {"RelIsogt0p1","D0gt0p02"}; |
43 |
+ |
map<TString,TCanvas*> cvs; // map of usual histogram |
44 |
+ |
bool debug_ = false; |
45 |
+ |
|
46 |
+ |
//================================= |
47 |
+ |
// Main program |
48 |
+ |
//================================= |
49 |
+ |
void ikstest() { |
50 |
+ |
CMSTopStyle style; |
51 |
+ |
gROOT->SetStyle("CMS"); |
52 |
+ |
TLatex *latex = new TLatex(); |
53 |
+ |
latex->SetNDC(); |
54 |
|
|
55 |
< |
//function for doing KS test |
56 |
< |
vector<testMC> doKStest(Double_t NmixS, Double_t Ns1, Double_t Ns2, TH1F* mixS, TH1F* s1, TH1F* s2) { |
57 |
< |
vector<testMC> output; |
58 |
< |
//define the scale factors |
59 |
< |
Double_t sf1 = 0.0; // QCD |
60 |
< |
Double_t sf2 = 0.0; // Wjets |
37 |
< |
//KS test |
38 |
< |
do { |
39 |
< |
sf1 = (NmixS - Ns2*sf2)/Ns1; |
40 |
< |
if (sf1 < 0) break; |
41 |
< |
//cout << "..........sf1 = " << sf1 << endl; |
42 |
< |
int nbins = mixS->GetNbinsX(); |
43 |
< |
double xmin = mixS->GetXaxis()->GetBinLowEdge(1); |
44 |
< |
double xmax = mixS->GetXaxis()->GetBinUpEdge(nbins); |
45 |
< |
TH1F *test = new TH1F("test", "", nbins, xmin, xmax); |
46 |
< |
test -> Sumw2(); |
47 |
< |
test -> Add(s1,s2,sf1,sf2); |
48 |
< |
//test->Scale(1./(1.*test->Integral())); |
49 |
< |
Double_t probability = test -> KolmogorovTest(mixS,"N"); |
50 |
< |
testMC temp = testMC(probability,sf1,sf2); |
51 |
< |
output.push_back(temp); |
52 |
< |
// cout << "probability = " << setw(15) << temp.prob |
53 |
< |
// << "; sfQCD = " << setw(10) << temp.scaleF_backg |
54 |
< |
// << "; sfWjets = " << setw(6) << temp.scaleF_sample << endl; |
55 |
< |
delete test; |
56 |
< |
sf2 = sf2 + 0.001; |
57 |
< |
} while(sf1 > 0 && sf2 <= 4.0); |
58 |
< |
return output; |
59 |
< |
} |
60 |
< |
|
61 |
< |
//get the maximum KS test result |
62 |
< |
testMC getMax(vector<testMC> vec) { |
63 |
< |
testMC maxKSRes; |
64 |
< |
Double_t maximum = 0.0; |
65 |
< |
for (size_t i = 0; i < vec.size(); i++) { |
66 |
< |
if (maximum < vec.at(i).prob) { |
67 |
< |
maximum = vec.at(i).prob; |
68 |
< |
maxKSRes = vec.at(i); |
55 |
> |
struct stat stbDir; |
56 |
> |
if (stat(baseDir,&stbDir)){ |
57 |
> |
cout << "Creating folder : " << baseDir << endl; |
58 |
> |
if (mkdir(baseDir,0755) == -1){ |
59 |
> |
std::cerr << "Error creating folder" << endl; |
60 |
> |
return; |
61 |
|
} |
62 |
|
} |
71 |
– |
cout << "for maximum: " << maxKSRes.prob |
72 |
– |
<< "\tsb = " << maxKSRes.scaleF_backg |
73 |
– |
<< "\tss = " << maxKSRes.scaleF_sample << endl; |
74 |
– |
return maxKSRes; |
75 |
– |
} |
63 |
|
|
64 |
< |
void ikstest() { |
65 |
< |
//Style(); |
66 |
< |
TLatex *latex = new TLatex(); |
67 |
< |
latex->SetNDC(); |
64 |
> |
int size_ninv = (useInv ? 2 : 1); |
65 |
> |
for (int ninv = 0;ninv < size_ninv; ++ninv) { |
66 |
> |
TString invName = invNames[ninv]; |
67 |
> |
TString desDir; |
68 |
> |
// create output directory |
69 |
> |
if (!useInv) { |
70 |
> |
if (!realData) desDir = baseDir + "MC/"; |
71 |
> |
else desDir = baseDir + "Data_MC/"; |
72 |
> |
} else { |
73 |
> |
if (!realData) desDir = baseDir + "MC_"+invName+"/"; |
74 |
> |
else desDir = baseDir + "Data_"+invName+"/"; |
75 |
> |
} |
76 |
> |
struct stat stDir; |
77 |
> |
if (!stat(desDir,&stDir)){ |
78 |
> |
cout << "Output folder exists! Continue to overwrite it? (enter to continue; 'q' for quit)" << endl; |
79 |
> |
char incmd; |
80 |
> |
cin.get(incmd); |
81 |
> |
if (incmd == 'q') return; |
82 |
> |
} else { |
83 |
> |
cout << "Creating folder : " << desDir << endl; |
84 |
> |
if (mkdir(desDir,0755) == -1){ |
85 |
> |
std::cerr << "Error creating folder" << endl; |
86 |
> |
return; |
87 |
> |
} |
88 |
> |
} |
89 |
|
|
90 |
< |
ofstream outprint( "ikstest_results.txt" ); |
90 |
> |
ofstream outprint(TString(desDir+"Results_"+suffix+".txt")); |
91 |
> |
//open the files with histograms |
92 |
> |
map<string,TFile*> mfile; |
93 |
> |
mfile.clear(); |
94 |
> |
mfile["Data"] = TFile::Open(TString("skimmed_Data_2.88pb-1/Data_"+suffix+".root")); |
95 |
> |
// n-1 cuts |
96 |
> |
if (useInv) { |
97 |
> |
if (realData) |
98 |
> |
mfile["InvSel"] = TFile::Open(TString("skimmed_Data_2.88pb-1/Data_"+suffix+"_"+invName+".root")); |
99 |
> |
else |
100 |
> |
mfile["InvSel"] = TFile::Open(TString("skimmed_MC/v5/QCD_"+suffix+"_"+invName+".root")); |
101 |
> |
} |
102 |
> |
// RefSel MC |
103 |
> |
mfile["0"] = TFile::Open(TString("skimmed_MC/v5/QCD_"+suffix+".root")); |
104 |
> |
mfile["1"] = TFile::Open(TString("skimmed_MC/v5/WJets_"+suffix+".root")); |
105 |
> |
mfile["2"] = TFile::Open(TString("skimmed_MC/v5/TTbar_"+suffix+".root")); |
106 |
> |
mfile["3"] = TFile::Open(TString("skimmed_MC/v5/ZJets_"+suffix+".root")); |
107 |
> |
mfile["4"] = TFile::Open(TString("skimmed_MC/v5/STtch_"+suffix+".root")); |
108 |
> |
|
109 |
> |
//define histograms and related parameters |
110 |
> |
string histoName[3] = {"h_mu_pt_calo","h_met_calo","h_mt_calo"}; |
111 |
> |
string histoLabelX[3] = {"p_{T}^{#mu} [GeV/c]", "#slash{E}_{T} [GeV/c]", "M_{T}^{W} [GeV/c^{2}]"}; |
112 |
> |
Int_t xbins[3] = {20,20,40}; |
113 |
> |
Double_t xlow[3] = {0.,0.,0.}; |
114 |
> |
Double_t xhigh[3] = {100.,100.,200.}; |
115 |
> |
string sample[5] = {"QCD","WJets","TTbar","ZJets","STtch"}; |
116 |
> |
|
117 |
> |
TH1F* hMC_[5][3]; // MC histograms |
118 |
> |
TH1F* hData_[3]; // Data or mix MC |
119 |
> |
TH1F* hQCD_NEW[3]; // InvSel QCD shape |
120 |
> |
TH1F* hKSres_[3]; |
121 |
> |
TH1F* hKSvalues_[3]; |
122 |
> |
TH1F* hQCD_KS[3]; |
123 |
> |
TH1F* hWJets_KS[3]; |
124 |
> |
|
125 |
> |
//load the histograms from the root files |
126 |
> |
for (int vi = 0; vi < 3; ++vi) {// 3 variables |
127 |
> |
//cout << "file[" << vi << "] : " << endl; |
128 |
> |
string nameNewHisto = "mix_"+histoName[vi]; |
129 |
> |
string nameNewHistoSFKS = "finalSF_"+histoName[vi]; |
130 |
> |
string nameNewHistoKSvalues = "KSvalues_"+histoLabelX[vi]; |
131 |
> |
|
132 |
> |
hData_[vi] = new TH1F(nameNewHisto.c_str(),"",xbins[vi],xlow[vi],xhigh[vi]); |
133 |
> |
hKSres_[vi] = new TH1F(nameNewHistoSFKS.c_str(),"",xbins[vi],xlow[vi],xhigh[vi]); |
134 |
> |
hKSvalues_[vi] = new TH1F(nameNewHistoKSvalues.c_str(),"",2./stepsize, stepsize, 2.+stepsize); |
135 |
> |
|
136 |
> |
ostringstream ssc; |
137 |
> |
ssc << vi; |
138 |
> |
|
139 |
> |
if (!useInv) {//use QCD MC sample |
140 |
> |
hQCD_NEW[vi] = (TH1F*) mfile["0"]->Get(TString(histoName[vi]))->Clone(); |
141 |
> |
hQCD_NEW[vi] -> Scale(weight_[0]); |
142 |
> |
hQCD_NEW[vi] -> SetName(TString("InvSel_"+histoName[vi]+"_"+ssc.str())); |
143 |
> |
} |
144 |
> |
else { |
145 |
> |
hQCD_NEW[vi] = (TH1F*) mfile["InvSel"]->Get(TString(histoName[vi]))->Clone(); |
146 |
> |
if (!realData) hQCD_NEW[vi] -> Scale(weight_[0]); |
147 |
> |
hQCD_NEW[vi] -> SetName(TString("InvSel_"+histoName[vi]+"_"+ssc.str())); |
148 |
> |
} |
149 |
> |
if (debug_) cout << "hQCD_NEW[" << vi << "] @ " << hQCD_NEW[vi] << endl; |
150 |
> |
|
151 |
> |
hData_[vi] -> Sumw2(); |
152 |
> |
hKSres_[vi] -> Sumw2(); |
153 |
> |
hKSvalues_[vi] -> Sumw2(); |
154 |
> |
} |
155 |
|
|
156 |
< |
//open the files with histograms |
157 |
< |
TFile *file[3]; |
158 |
< |
file[0] = TFile::Open("InclusiveMu15_Spring10_all.root"); |
159 |
< |
file[1] = TFile::Open("WJets_madgraph_Spring10_all.root"); |
160 |
< |
file[2] = TFile::Open("TTbarJets_madgraph_Spring10_all.root"); |
161 |
< |
|
162 |
< |
//define histograms and related parameters |
163 |
< |
string histoN[3] = {"h_mu_pt","h_met0","h_mt0"}; |
164 |
< |
string hQCD_new[3] = {"h_mu_pt","h_met0","h_mt0"}; |
165 |
< |
string histoLabelX[3] = {"p_{T}^{good Muons}", "E_{T}^{#nu}", "m_{T}^{W}"}; |
166 |
< |
Int_t xbins[3] = {10,30,30}; |
167 |
< |
Double_t xlow[3] = {0.,0.,0.}; |
168 |
< |
Double_t xhigh[3] = {100.,200.,200.}; |
169 |
< |
string sample[3] = {"QCD","Wjets","ttjets"}; |
170 |
< |
|
171 |
< |
TH1F* h_[9]; |
172 |
< |
TH1F* mixh_[3]; |
101 |
< |
TH1F* hQCD_NEW[3]; |
102 |
< |
TH1F* hKSres_[3]; |
103 |
< |
TH1F* hKSvalues_[3]; |
104 |
< |
|
105 |
< |
//load the histograms from the root files |
106 |
< |
for (int i = 0; i < 3; i++) {// 3 variables |
107 |
< |
//cout << "file[" << i << "] : " << endl; |
108 |
< |
string nameNewHisto = "mix_"+histoN[i]; |
109 |
< |
string nameNewHistoSFKS = "finalSF_"+histoN[i]; |
110 |
< |
string nameNewHistoKSvalues = "KSvalues_"+histoLabelX[i]; |
111 |
< |
|
112 |
< |
mixh_[i] = new TH1F(nameNewHisto.c_str(),"",xbins[i],xlow[i],xhigh[i]); |
113 |
< |
hKSres_[i] = new TH1F(nameNewHistoSFKS.c_str(),"",xbins[i],xlow[i],xhigh[i]); |
114 |
< |
hKSvalues_[i] = new TH1F(nameNewHistoKSvalues.c_str(),"",300, 0.5, 300.5); |
115 |
< |
//cout << TString("demo/"+hQCD_new[i]) << endl; |
116 |
< |
hQCD_NEW[i] = (TH1F*)file[0]->Get(TString("demo/"+hQCD_new[i])); // corresponds to h_[i]; i=0..2 for now |
117 |
< |
hQCD_NEW[i] -> SetName((hQCD_new[i]).c_str()); |
118 |
< |
|
119 |
< |
mixh_[i] -> Sumw2(); |
120 |
< |
hKSres_[i] -> Sumw2(); |
121 |
< |
hKSvalues_[i] -> Sumw2(); |
122 |
< |
|
123 |
< |
for (int ihisto = 0; ihisto < 3; ihisto++) { |
124 |
< |
//cout << "Variable[" << ihisto << "]" << endl; |
125 |
< |
string histo_name = histoN[ihisto]+sample[i]; |
126 |
< |
//cout << TString("demo/"+histoN[ihisto]) << endl; |
127 |
< |
h_[i*3+ihisto] = (TH1F*)file[i]->Get(TString("demo/"+histoN[ihisto])); |
128 |
< |
h_[i*3+ihisto] -> SetName(histo_name.c_str()); |
156 |
> |
for (int n = 0; n < 5; ++n) {// 3 MC samples |
157 |
> |
for (int ihisto = 0; ihisto < 3; ihisto++) {// 3 variables |
158 |
> |
//cout << "Variable[" << ihisto << "]" << endl; |
159 |
> |
string histo_name = histoName[ihisto]+sample[n]; |
160 |
> |
ostringstream ss; |
161 |
> |
ss << n; |
162 |
> |
hMC_[n][ihisto] = (TH1F*) mfile[ss.str()]->Get(TString(histoName[ihisto]))->Clone(); |
163 |
> |
if (debug_) { |
164 |
> |
cout << "File[" << n << "] @" << mfile[ss.str()] |
165 |
> |
<< "; histo[" << ihisto << "] @ " << mfile[ss.str()]->Get(TString(histoName[ihisto])) |
166 |
> |
<<"; hMC_[" << n << "][" << ihisto << "] raw evts = " |
167 |
> |
<< setw(12) << hMC_[n][ihisto]->Integral(); |
168 |
> |
} |
169 |
> |
hMC_[n][ihisto] -> Scale(weight_[n]); |
170 |
> |
hMC_[n][ihisto] -> SetName(TString("MC_"+sample[n]+"_"+histoName[ihisto])); |
171 |
> |
if (debug_) cout << "; weighted num evts = " << setw(8) << hMC_[n][ihisto]->Integral() << endl; |
172 |
> |
} |
173 |
|
} |
130 |
– |
} |
174 |
|
|
175 |
< |
//create the mixed samples = "data" |
176 |
< |
for (int i = 0; i < 3; i++) { |
177 |
< |
mixh_[i] -> Add(h_[i],h_[i+3], procQCD,procWjets); |
178 |
< |
//mixh_[i] -> Add(mixh_[i],h_[i+6], 1,procttjets); |
179 |
< |
//cout << "histo_name: " << mixh_[0]->GetNbinsX() << endl; |
180 |
< |
} |
175 |
> |
//create the mixed samples = "data" |
176 |
> |
TString cvsName0 = "Data"; |
177 |
> |
if (useInv) { |
178 |
> |
cvsName0 += "_"; |
179 |
> |
cvsName0 += invName; |
180 |
> |
} |
181 |
> |
cvs[cvsName0] = new TCanvas(cvsName0,"Data distributions",600,700); |
182 |
> |
cvs[cvsName0]->Divide(3,1); |
183 |
> |
for (int i = 0; i < 3; i++) { |
184 |
> |
cvs[cvsName0]->cd(i+1); |
185 |
> |
if (!realData) { |
186 |
> |
hData_[i] -> Add(hMC_[0][i],hMC_[1][i], procQCD,procWJets); |
187 |
> |
hData_[i] -> Add(hMC_[2][i], procTTbar); |
188 |
> |
hData_[i] -> Add(hMC_[3][i], procZJets); |
189 |
> |
hData_[i] -> Add(hMC_[4][i], procSTtch); |
190 |
> |
} |
191 |
> |
else { |
192 |
> |
TH1F *htmp = (TH1F*) mfile["Data"]->Get(TString(histoName[i])); |
193 |
> |
hData_[i] -> Add(htmp,1.); |
194 |
> |
} |
195 |
> |
hData_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str()); |
196 |
> |
hData_[i]->GetYaxis()->SetTitle("Entries"); |
197 |
> |
hData_[i]->DrawClone(); |
198 |
> |
} |
199 |
> |
cvs[cvsName0]->SaveAs(TString(desDir+"Data_distributions.pdf")); |
200 |
|
|
201 |
< |
//define the weight corrections for each sample |
202 |
< |
double corr_NevQCD = (h_[2]->GetEntries())*weight_QCD; |
141 |
< |
double corr_NevQCD_NEW = (hQCD_NEW[2]->GetEntries())*weight_QCD; |
142 |
< |
double corr_NevWjets = (h_[5]->GetEntries())*weight_Wjets; |
143 |
< |
double corr_Nevttjets = (h_[8]->GetEntries())*weight_ttjets; |
144 |
< |
double corr_Nevmix = procQCD*corr_NevQCD+procWjets*corr_NevWjets; |
145 |
< |
//double corr_Nevmix = procQCD*corr_NevQCD+procWjets*corr_NevWjets+procttjets*corr_Nevttjets; |
146 |
< |
outprint << "Events mix sample = " << corr_Nevmix << endl; |
147 |
< |
outprint << "Events QCD sample = " << corr_NevQCD << endl; |
148 |
< |
outprint << "Events Wjets sample = " << corr_NevWjets << endl; |
149 |
< |
outprint << "Events Nev revIso QCD sample = " << corr_NevQCD_NEW << endl; |
150 |
< |
|
151 |
< |
//define the containers for chosen numbers (coressponding to the max KStest result) |
152 |
< |
testMC maxProb[3]; |
153 |
< |
|
154 |
< |
//define the scale factors calculated using information obtained from all parameters |
155 |
< |
Double_t SFbackg = 0.0; |
156 |
< |
Double_t sumSFbackg = 0.0; |
157 |
< |
Double_t SFsample = 0.0; |
158 |
< |
Double_t sumSFsample = 0.0; |
159 |
< |
Double_t allKS = 0.0; |
160 |
< |
|
161 |
< |
//do the KS test by varying the scale factors |
162 |
< |
for (int i = 0; i < 3; i++) { // 3 variables |
163 |
< |
TH1F *data = (TH1F*)mixh_[i]->Clone(); |
164 |
< |
data -> SetName("dataClone"); |
165 |
< |
//data -> Scale(1./data->Integral()); |
166 |
< |
vector<testMC> resultsKS = doKStest(corr_Nevmix, corr_NevQCD_NEW, corr_NevWjets, data, hQCD_NEW[i], h_[i+3]); |
167 |
< |
testMC tksmax = getMax(resultsKS); |
168 |
< |
maxProb[i] = tksmax; |
169 |
< |
outprint << "\nFor the plot " << histoLabelX[i] << " the results are:"<< endl; |
170 |
< |
outprint << "\tmax Probability = " << maxProb[i].prob << endl; |
171 |
< |
outprint << "\tproc_background = " << maxProb[i].scaleF_backg << endl; |
172 |
< |
outprint << "\tproc_sample = " << maxProb[i].scaleF_sample << endl; |
201 |
> |
//Calculate num of events for each sample |
202 |
> |
vector<double> vNev_; |
203 |
|
|
204 |
< |
outprint << "\n\tpercent_B of Data = " << maxProb[i].scaleF_backg*corr_NevQCD_NEW*100/corr_Nevmix << endl; |
205 |
< |
outprint << "\tpercent_S of Data = " << maxProb[i].scaleF_sample*corr_NevWjets*100/corr_Nevmix << endl; |
204 |
> |
double NevData = hData_[2]->Integral(); |
205 |
> |
double corr_NevQCD = hMC_[0][2]->Integral(); |
206 |
> |
double corr_NevQCD_NEW = hQCD_NEW[2]->Integral(); |
207 |
> |
double corr_NevWJets = hMC_[1][2]->Integral(); |
208 |
> |
double corr_NevTTbar = hMC_[2][2]->Integral(); |
209 |
> |
double corr_NevZJets = hMC_[3][2]->Integral(); |
210 |
> |
double corr_NevSTtch = hMC_[4][2]->Integral(); |
211 |
> |
cout << "corr_NevSTtch = " << corr_NevSTtch << endl; |
212 |
> |
// double corr_Nevmix = procQCD*corr_NevQCD + procWJets*corr_NevWJets |
213 |
> |
// + procTTbar*corr_NevTTbar+procZJets*corr_NevZJets + procSTtch*corr_NevSTtch;//should equal NevData for MC |
214 |
> |
// store nev in a vector |
215 |
> |
vNev_.push_back(NevData); |
216 |
> |
vNev_.push_back((useInv ? corr_NevQCD_NEW : corr_NevQCD)); |
217 |
> |
vNev_.push_back(corr_NevWJets); |
218 |
> |
|
219 |
> |
// Non WJets (use MC expected values): |
220 |
> |
if (procTTbar > 0.) vNev_.push_back(corr_NevTTbar*procTTbar); |
221 |
> |
if (procZJets > 0.) vNev_.push_back(corr_NevZJets*procZJets); |
222 |
> |
if (procSTtch > 0.) vNev_.push_back(corr_NevSTtch*procSTtch); |
223 |
> |
|
224 |
> |
outprint << "Events in Data = " << NevData << endl; |
225 |
> |
outprint << "Events QCD sample = " << corr_NevQCD << endl; |
226 |
> |
outprint << "Events InvSel sample = " << corr_NevQCD_NEW << endl; |
227 |
|
outprint << "---------------------------" << endl; |
228 |
< |
|
229 |
< |
//create the mixed samples with KS test results |
230 |
< |
sumSFbackg += maxProb[i].prob*maxProb[i].scaleF_backg; |
231 |
< |
sumSFsample += maxProb[i].prob*maxProb[i].scaleF_sample; |
232 |
< |
allKS += maxProb[i].prob; |
233 |
< |
|
234 |
< |
//fill a histogram with the results from the KS test for each variable |
235 |
< |
for (int jiter = 0; jiter < resultsKS.size(); jiter++) { |
236 |
< |
//cout << "prob = " << resultsKS.at(jiter).prob << endl; |
237 |
< |
hKSvalues_[i]->SetBinContent(jiter,resultsKS.at(jiter).prob); |
228 |
> |
outprint << "Events WJets sample = " << corr_NevWJets << endl; |
229 |
> |
outprint << "Events TTbar sample = " << corr_NevTTbar << endl; |
230 |
> |
outprint << "Events ZJets sample = " << corr_NevZJets << endl; |
231 |
> |
outprint << "Events STtch sample = " << corr_NevSTtch << endl; |
232 |
> |
|
233 |
> |
//define the containers for chosen numbers (coressponding to the max KStest result) |
234 |
> |
testMC maxProb[3]; |
235 |
> |
|
236 |
> |
//define the scale factors calculated using information obtained from all parameters |
237 |
> |
Double_t SFbackg = 0.0; |
238 |
> |
Double_t sumSFbackg = 0.0; |
239 |
> |
Double_t SFsample = 0.0; |
240 |
> |
Double_t sumSFsample = 0.0; |
241 |
> |
Double_t allKS = 0.0; |
242 |
> |
|
243 |
> |
//do the KS test by varying the scale factors |
244 |
> |
for (int i = 0; i < 3; i++) { // 3 variables |
245 |
> |
TH1F *data = (TH1F*)hData_[i]->Clone("data"); |
246 |
> |
data -> SetName("dataClone"); |
247 |
> |
map<string,TH1F*> mHisto_; |
248 |
> |
mHisto_.clear(); |
249 |
> |
mHisto_["Data"] = data; |
250 |
> |
mHisto_["QCD"] = (useInv ? (TH1F*)hQCD_NEW[i]->Clone() : (TH1F*)hMC_[0][i]->Clone()); |
251 |
> |
mHisto_["WJets"] = (TH1F*)hMC_[1][i]->Clone();//WJets |
252 |
> |
if (procTTbar > 0.) mHisto_["TTbar"] = (TH1F*)hMC_[2][i]->Clone();//TTbar |
253 |
> |
if (procZJets > 0.) mHisto_["ZJets"] = (TH1F*)hMC_[3][i]->Clone();//ZJets |
254 |
> |
if (procSTtch > 0.) mHisto_["STtch"] = (TH1F*)hMC_[4][i]->Clone();//STtch |
255 |
> |
|
256 |
> |
//data -> Scale(1./data->Integral()); |
257 |
> |
vector<testMC> resultsKS = doKStest(vNev_,mHisto_); |
258 |
> |
testMC tksmax = getMax(resultsKS); |
259 |
> |
maxProb[i] = tksmax; |
260 |
> |
outprint << "\nFor the plot " << histoLabelX[i] << " the results are:"<< endl; |
261 |
> |
outprint << "\tmax Probability = " << maxProb[i].prob << endl; |
262 |
> |
outprint << "\tproc_background = " << maxProb[i].scaleF_backg << endl; |
263 |
> |
outprint << "\tproc_sample = " << maxProb[i].scaleF_sample << endl; |
264 |
> |
|
265 |
> |
outprint << "\n\tpercent_B of Data = " |
266 |
> |
<< maxProb[i].scaleF_backg*corr_NevQCD_NEW*100/NevData << endl; |
267 |
> |
outprint << "\tpercent_S of Data = " |
268 |
> |
<< maxProb[i].scaleF_sample*corr_NevWJets*100/NevData << endl; |
269 |
> |
outprint << "---------------------------" << endl; |
270 |
> |
|
271 |
> |
//create the mixed samples with KS test results |
272 |
> |
sumSFbackg += maxProb[i].prob*maxProb[i].scaleF_backg; |
273 |
> |
sumSFsample += maxProb[i].prob*maxProb[i].scaleF_sample; |
274 |
> |
allKS += maxProb[i].prob; |
275 |
> |
|
276 |
> |
//fill a histogram with the results from the KS test for each variable |
277 |
> |
for (unsigned int jiter = 0; jiter < resultsKS.size(); jiter++) { |
278 |
> |
if (resultsKS.at(jiter).prob == 1.) |
279 |
> |
cout << "variable [" << i << "]: prob[" << jiter << "]= " << resultsKS.at(jiter).prob << endl; |
280 |
> |
hKSvalues_[i]->SetBinContent(jiter,resultsKS.at(jiter).prob); |
281 |
> |
} |
282 |
> |
delete data; |
283 |
|
} |
188 |
– |
delete data; |
189 |
– |
} |
284 |
|
|
285 |
< |
//calculate the final scale factors |
286 |
< |
SFbackg = sumSFbackg/allKS; |
287 |
< |
SFsample = sumSFsample/allKS; |
288 |
< |
outprint << "allKS = " << allKS << "\tbackground = " << SFbackg << "\tsample = " << SFsample << endl; |
289 |
< |
outprint << "\tcombined percent_B of Data = " << SFbackg*corr_NevQCD_NEW*100/corr_Nevmix << endl; |
290 |
< |
outprint << "\tcombined percent_S of Data = " << SFsample*corr_NevWjets*100/corr_Nevmix << endl; |
291 |
< |
outprint << "\n" << endl; |
292 |
< |
outprint << "=================================" << endl; |
293 |
< |
outprint << "\n" << endl; |
285 |
> |
//calculate the final scale factors |
286 |
> |
SFbackg = sumSFbackg/allKS; |
287 |
> |
SFsample = sumSFsample/allKS; |
288 |
> |
outprint << "allKS = " << allKS << "\tbackground = " << SFbackg << "\tsample = " << SFsample << endl; |
289 |
> |
outprint << "==> Scale Factor for QCD MC = " << SFbackg*corr_NevQCD_NEW/corr_NevQCD << endl; |
290 |
> |
outprint << "\tcombined percent_B of Data = " |
291 |
> |
<< SFbackg*corr_NevQCD_NEW*100/NevData << endl; |
292 |
> |
outprint << "\tcombined percent_S of Data = " |
293 |
> |
<< SFsample*corr_NevWJets*100/NevData << endl; |
294 |
> |
outprint << "\n" << endl; |
295 |
> |
outprint << "=================================" << endl; |
296 |
> |
outprint << "\n" << endl; |
297 |
> |
|
298 |
> |
|
299 |
> |
//================================= |
300 |
> |
// Plots |
301 |
> |
//================================= |
302 |
> |
for (int i = 0; i < 3; i++) {// 3 variables |
303 |
> |
hKSres_[i] -> Add((TH1F*)hQCD_NEW[i]->Clone(),(TH1F*)hMC_[1][i]->Clone(),SFbackg,SFsample); |
304 |
> |
hKSres_[i] -> Add((TH1F*)hMC_[2][i]->Clone(),procTTbar); |
305 |
> |
hKSres_[i] -> Add((TH1F*)hMC_[3][i]->Clone(),procZJets); |
306 |
> |
hKSres_[i] -> Add((TH1F*)hMC_[4][i]->Clone(),procSTtch); |
307 |
> |
|
308 |
> |
outprint << "hKSres->Integral() = " << hKSres_[i]->Integral() << endl; |
309 |
> |
outprint << "Data->Integral() = " << hData_[i]->Integral() << endl; |
310 |
> |
|
311 |
> |
hData_[i]->Rebin(2); |
312 |
> |
hQCD_NEW[i]->Rebin(2); |
313 |
> |
hMC_[0][i]->Rebin(2); |
314 |
> |
hMC_[1][i]->Rebin(2); |
315 |
> |
hKSres_[i]->Rebin(2); |
316 |
> |
//hKSvalues_[i]->Rebin(2); |
317 |
> |
|
318 |
> |
// Stack Wjets and QCD after scaled by KS factors |
319 |
> |
hQCD_KS[i] = (TH1F*) hQCD_NEW[i]->Clone(); |
320 |
> |
hQCD_KS[i]->Scale(SFbackg); |
321 |
> |
hQCD_KS[i]->SetLineColor(style.QCDColor); |
322 |
> |
hQCD_KS[i]->SetFillColor(style.QCDColor); |
323 |
> |
hQCD_KS[i]->SetFillStyle(style.QCDFill); |
324 |
> |
|
325 |
> |
hWJets_KS[i] = (TH1F*) hMC_[1][i]->Clone(); |
326 |
> |
hWJets_KS[i]->Scale(SFsample); |
327 |
> |
hWJets_KS[i]->SetLineColor(style.WJetsColor); |
328 |
> |
hWJets_KS[i]->SetFillColor(style.WJetsColor); |
329 |
> |
hWJets_KS[i]->SetFillStyle(style.WJetsFill); |
330 |
> |
|
331 |
> |
if (procTTbar > 0.) { |
332 |
> |
hMC_[2][i]->Rebin(2); |
333 |
> |
hMC_[2][i]->SetLineColor(style.TtbarColor); |
334 |
> |
hMC_[2][i]->SetFillColor(style.TtbarColor); |
335 |
> |
hMC_[2][i]->SetFillStyle(style.TtbarFill); |
336 |
> |
} |
337 |
> |
if (procZJets > 0.) { |
338 |
> |
hMC_[3][i]->Rebin(2); |
339 |
> |
hMC_[3][i]->SetLineColor(style.DYZJetsColor); |
340 |
> |
hMC_[3][i]->SetFillColor(style.DYZJetsColor); |
341 |
> |
hMC_[3][i]->SetFillStyle(style.DYZJetsFill); |
342 |
> |
} |
343 |
> |
if (procSTtch > 0.) { |
344 |
> |
hMC_[4][i]->Rebin(2); |
345 |
> |
hMC_[4][i]->SetLineColor(style.ST_t_sColor); |
346 |
> |
hMC_[4][i]->SetFillColor(style.ST_t_sColor); |
347 |
> |
hMC_[4][i]->SetFillStyle(style.ST_t_sFill); |
348 |
> |
} |
349 |
> |
THStack *hst = new THStack(invName,invName); |
350 |
> |
hst->Add((TH1F*)hQCD_KS[i]->Clone()); |
351 |
> |
if (procSTtch > 0) hst->Add((TH1F*)hMC_[4][i]->Clone()); |
352 |
> |
if (procZJets > 0) hst->Add((TH1F*)hMC_[3][i]->Clone()); |
353 |
> |
hst->Add((TH1F*)hWJets_KS[i]->Clone()); |
354 |
> |
if (procTTbar > 0) hst->Add((TH1F*)hMC_[2][i]->Clone()); |
355 |
> |
|
356 |
> |
// Set plotting parameters |
357 |
> |
hData_[i] ->SetLineColor(1); |
358 |
> |
hQCD_NEW[i] ->SetLineColor(2); |
359 |
> |
hMC_[0][i] ->SetLineColor(4); |
360 |
> |
hMC_[1][i] ->SetLineColor(3); |
361 |
> |
hKSres_[i] ->SetLineColor(2); |
362 |
> |
hKSvalues_[i]->SetLineColor(i+1); |
363 |
> |
|
364 |
> |
hData_[i] ->SetMarkerColor(1); |
365 |
> |
hQCD_NEW[i] ->SetMarkerColor(2); |
366 |
> |
hMC_[0][i] ->SetMarkerColor(4); |
367 |
> |
hMC_[1][i] ->SetMarkerColor(3); |
368 |
> |
hKSres_[i] ->SetMarkerColor(2); |
369 |
> |
hKSvalues_[i]->SetMarkerColor(i+1); |
370 |
> |
|
371 |
> |
hData_[i] ->SetMarkerStyle(24); |
372 |
> |
hQCD_NEW[i] ->SetMarkerStyle(20); |
373 |
> |
hMC_[0][i] ->SetMarkerStyle(20); |
374 |
> |
hMC_[1][i] ->SetMarkerStyle(20); |
375 |
> |
hKSres_[i] ->SetMarkerStyle(20); |
376 |
> |
hKSvalues_[i]->SetMarkerStyle(20); |
377 |
> |
|
378 |
> |
hData_[i] ->SetMarkerSize(1.4); |
379 |
> |
hQCD_NEW[i] ->SetMarkerSize(1.1); |
380 |
> |
hMC_[0][i] ->SetMarkerSize(1.1); |
381 |
> |
hMC_[1][i] ->SetMarkerSize(1.1); |
382 |
> |
hKSres_[i] ->SetMarkerSize(0.9); |
383 |
> |
hKSvalues_[i]->SetMarkerSize(1.1); |
384 |
> |
|
385 |
> |
hData_[i] ->SetStats(0); |
386 |
> |
hQCD_NEW[i] ->SetStats(0); |
387 |
> |
hMC_[0][i] ->SetStats(0); |
388 |
> |
hMC_[1][i] ->SetStats(0); |
389 |
> |
hKSres_[i] ->SetStats(0); |
390 |
> |
hKSvalues_[i]->SetStats(0); |
391 |
> |
hQCD_KS[i] ->SetStats(0); |
392 |
> |
hWJets_KS[i] ->SetStats(0); |
393 |
> |
|
394 |
> |
hKSres_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str()); |
395 |
> |
hKSres_[i]->GetYaxis()->SetTitle("Entries"); |
396 |
> |
hKSvalues_[i]->GetXaxis()->SetTitle("iteration #"); |
397 |
> |
hKSvalues_[i]->GetYaxis()->SetTitle("KS test values"); |
398 |
> |
hMC_[0][i]->GetXaxis()->SetTitle(histoLabelX[i].c_str()); |
399 |
> |
hMC_[0][i]->GetYaxis()->SetTitle("A.U."); |
400 |
> |
|
401 |
> |
|
402 |
> |
TString nameCanvas1 = desDir+histoName[i]+"_QCD_"+suffix+".pdf"; |
403 |
> |
TString cvsName1 = histoName[i]+"_QCD"; |
404 |
> |
if(useInv) cvsName1 = cvsName1 + "_" + invName; |
405 |
> |
cvs[cvsName1] = new TCanvas(cvsName1,"",600,700); |
406 |
> |
hQCD_NEW[i] -> Scale(1./hQCD_NEW[i]->Integral()); |
407 |
> |
hMC_[0][i] -> Scale(1./hMC_[0][i]->Integral()); |
408 |
> |
hMC_[1][i] -> Scale(1./hMC_[1][i]->Integral()); |
409 |
> |
outprint << "For " << histoName[i] << " , the KStest result btw MC_QCD/InvSel is = " |
410 |
> |
<< hMC_[0][i] -> KolmogorovTest(hQCD_NEW[i],"") << endl; |
411 |
> |
hMC_[0][i]->DrawCopy("P"); |
412 |
> |
if (useInv) |
413 |
> |
hQCD_NEW[i]->DrawCopy("sameP"); |
414 |
> |
hMC_[1][i]->DrawCopy("sameP"); |
415 |
> |
TLegend *legend1 = new TLegend(0.7, 0.65, 0.9, 0.85); |
416 |
> |
legend1->AddEntry(hMC_[0][i], "QCD"); |
417 |
> |
if (useInv) |
418 |
> |
legend1->AddEntry(hQCD_NEW[i], "QCD - InvSel"); |
419 |
> |
legend1->AddEntry(hMC_[1][i], style.WJetsText); |
420 |
> |
legend1->Draw(); |
421 |
> |
legend1->SetFillColor(kWhite); |
422 |
> |
//latex->DrawLatex(0.22,0.91,histoName[i].c_str()); |
423 |
> |
//cvs[cvsName1]->SetLogy(); |
424 |
> |
cvs[cvsName1]->SaveAs(nameCanvas1); |
425 |
> |
|
426 |
> |
|
427 |
> |
TString nameCanvas2 = desDir+histoName[i]+"_dataKS_"+suffix+".pdf"; |
428 |
> |
TString cvsName2 = histoName[i]+"_dataKS"; |
429 |
> |
if(useInv) cvsName2 = cvsName2 + "_" + invName; |
430 |
> |
cvs[cvsName2] = new TCanvas(cvsName2,"",600,700); |
431 |
> |
hst->Draw("hist"); |
432 |
> |
hKSres_[i]->Draw("sameP"); |
433 |
> |
hData_[i]->Draw("sameP"); |
434 |
> |
|
435 |
> |
TLegend *legend2 = new TLegend(0.7, 0.65, 0.9, 0.85); |
436 |
> |
legend2->AddEntry(hData_[i], "Data"); |
437 |
> |
legend2->AddEntry(hKSres_[i], "KS result"); |
438 |
> |
if (procTTbar > 0.) legend2->AddEntry(hMC_[2][i], style.TtbarText); |
439 |
> |
legend2->AddEntry(hWJets_KS[i], style.WJetsText); |
440 |
> |
if (procZJets > 0.) legend2->AddEntry(hMC_[3][i], style.DYZJetsText); |
441 |
> |
if (procSTtch > 0.) legend2->AddEntry(hMC_[4][i], style.ST_t_sText); |
442 |
> |
legend2->AddEntry(hQCD_KS[i], style.QCDText); |
443 |
> |
legend2->Draw(); |
444 |
> |
legend2->SetFillColor(kWhite); |
445 |
> |
//latex->DrawLatex(0.22,0.91,histoName[i].c_str()); |
446 |
> |
//cvs[cvsName2]->SetLogy(); |
447 |
> |
cvs[cvsName2]->SaveAs(nameCanvas2); |
448 |
|
|
449 |
+ |
} |
450 |
+ |
|
451 |
+ |
TString cvsName3 = "KStestValues"; |
452 |
+ |
if(useInv) cvsName3 = cvsName3 + "_" + invName; |
453 |
+ |
cvs[cvsName3] = new TCanvas(cvsName3,"",600,700); |
454 |
+ |
if (!realData) hKSvalues_[0]->GetXaxis()->SetRangeUser(0.95,1.1); |
455 |
+ |
hKSvalues_[0]->GetYaxis()->SetRangeUser(1e-36,1.2); |
456 |
+ |
hKSvalues_[0]->Draw(); |
457 |
+ |
hKSvalues_[1]->Draw("same"); |
458 |
+ |
hKSvalues_[2]->Draw("same"); |
459 |
+ |
TLegend *legend3 = new TLegend(0.7, 0.70, 0.9, 0.85); |
460 |
+ |
legend3->AddEntry(hKSvalues_[0], "muon_pT"); |
461 |
+ |
legend3->AddEntry(hKSvalues_[1], "MET"); |
462 |
+ |
legend3->AddEntry(hKSvalues_[2], "W_mT"); |
463 |
+ |
legend3->Draw(); |
464 |
+ |
legend3->SetFillColor(kWhite); |
465 |
+ |
//latex->DrawLatex(0.22,0.91,"KS test values"); |
466 |
+ |
cvs[cvsName3]->SetLogy(); |
467 |
+ |
TString nameCanvas3 = desDir+"KStestValues_newQCD"+suffix+".pdf"; |
468 |
+ |
cvs[cvsName3]->SaveAs(nameCanvas3); |
469 |
+ |
} |
470 |
|
} |