ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/scripts/ikstest.cc
Revision: 1.4
Committed: Fri Sep 3 05:05:19 2010 UTC (14 years, 8 months ago) by jengbou
Content type: text/plain
Branch: MAIN
Changes since 1.3: +73 -116 lines
Log Message:
Update: move functions to header file

File Contents

# Content
1 #include "ikstest.h"
2
3 using namespace std;
4
5 //define the constants: 2.79/pb
6 const double weight_[3] = {0.0507928, //QCD
7 0.0088539, //WJets
8 0.000296 //TTbar
9 };
10 const Double_t procQCD = 1.46;
11 const Double_t procWjets = 1.03;
12 const Double_t procttjets = 1.0;
13
14 // User defined parameters
15 bool useInv = true; // whether to use n-1 QCD template
16 bool realData = true;
17
18 //=================================
19 // Main program
20 //=================================
21 void ikstest() {
22 gROOT->SetStyle("CMS");
23 TLatex *latex = new TLatex();
24 latex->SetNDC();
25 TString suffix = "Sel3";
26 TString desDir = "Results_2.79pb-1/";
27 //TString invName = "RelIsoge0p1";
28 //TString invName = "D0ge0p03";
29 TString invName = "METle15";
30 if (!useInv) {
31 if (!realData) desDir += "MC/";
32 else desDir += "Data/";
33 } else {
34 if (!realData) desDir += "MC_"+invName+"/";
35 else desDir += "Data_"+invName+"/";
36 }
37 struct stat stDir;
38 if (!stat(desDir,&stDir)){
39 cout << "Output folder exists! Continues? (enter to continue; 'q' for quit)" << endl;
40 char incmd;
41 cin.get(incmd);
42 if (incmd == 'q') return;
43 } else {
44 cout << "Creating folder : " << desDir << endl;
45 if (mkdir(desDir,0755) == -1){
46 std::cerr << "Error creating folder" << endl;
47 return;
48 }
49 }
50
51 ofstream outprint(TString(desDir+"Results_"+suffix+".txt"));
52 //open the files with histograms
53 map<string,TFile*> mfile;
54 mfile["Data"] = TFile::Open("skimmed_Data_2.79pb-1/Data_RefSel3.root");
55 // n-1 cuts
56 if (useInv) {
57 if (realData)
58 mfile["InvSel"] = TFile::Open(TString("skimmed_Data_2.79pb-1/Data_Sel3_"+invName+".root"));
59 else
60 mfile["InvSel"] = TFile::Open(TString("skimmed_MC/QCD_Sel3_"+invName+".root"));
61 }
62
63 // RefSel MC
64 mfile["0"] = TFile::Open("skimmed_MC/QCD_RefSel3.root");
65 mfile["1"] = TFile::Open("skimmed_MC/WJets_RefSel3.root");
66 mfile["2"] = TFile::Open("skimmed_MC/TTbar_RefSel3.root");
67
68 //define histograms and related parameters
69 string histoName[3] = {"h_mu_pt_calo","h_met_calo","h_mt_calo"};
70 string histoLabelX[3] = {"p_{T}^{#mu} [GeV/c]", "#slash{E}_{T} [GeV/c]", "M_{T}^{W} [GeV/c^{2}]"};
71 Int_t xbins[3] = {20,20,40};
72 Double_t xlow[3] = {0.,0.,0.};
73 Double_t xhigh[3] = {100.,100.,200.};
74 string sample[3] = {"QCD","Wjets","ttjets"};
75
76 TH1F* h_[9];
77 TH1F* mixh_[3];
78 TH1F* hQCD_NEW[3];
79 TH1F* hKSres_[3];
80 TH1F* hKSvalues_[3];
81
82 //load the histograms from the root files
83 for (int i = 0; i < 3; i++) {// 3 variables
84 //cout << "file[" << i << "] : " << endl;
85 string nameNewHisto = "mix_"+histoName[i];
86 string nameNewHistoSFKS = "finalSF_"+histoName[i];
87 string nameNewHistoKSvalues = "KSvalues_"+histoLabelX[i];
88
89 mixh_[i] = new TH1F(nameNewHisto.c_str(),"",xbins[i],xlow[i],xhigh[i]);
90 hKSres_[i] = new TH1F(nameNewHistoSFKS.c_str(),"",xbins[i],xlow[i],xhigh[i]);
91 hKSvalues_[i] = new TH1F(nameNewHistoKSvalues.c_str(),"",2./stepsize, stepsize, 2.+stepsize);
92
93 if (!useInv) {//use QCD MC sample
94 hQCD_NEW[i] = (TH1F*) mfile["0"]->Get(TString(histoName[i]))->Clone();
95 hQCD_NEW[i] -> Scale(weight_[0]);
96 hQCD_NEW[i] -> SetName((histoName[i]).c_str());
97 }
98 else {
99 hQCD_NEW[i] = (TH1F*) mfile["InvSel"]->Get(TString(histoName[i]));
100 if (!realData) hQCD_NEW[i] -> Scale(weight_[0]);
101 hQCD_NEW[i] -> SetName((histoName[i]).c_str());
102 }
103
104 mixh_[i] -> Sumw2();
105 hKSres_[i] -> Sumw2();
106 hKSvalues_[i] -> Sumw2();
107 }
108
109 for (int n = 0; n < 3; ++n) {// 3 MC samples
110 for (int ihisto = 0; ihisto < 3; ihisto++) {// 3 variables
111 //cout << "Variable[" << ihisto << "]" << endl;
112 string histo_name = histoName[ihisto]+sample[n];
113 ostringstream ss;
114 ss << n;
115 h_[n*3+ihisto] = (TH1F*) mfile[ss.str()]->Get(TString(histoName[ihisto]))->Clone();
116 h_[n*3+ihisto] -> Scale(weight_[n]);
117 h_[n*3+ihisto] -> SetName(histo_name.c_str());
118 }
119 }
120
121 //create the mixed samples = "data"
122 TCanvas *canvas0 = new TCanvas("Data","Data distributions");
123 canvas0->Divide(3,1);
124 for (int i = 0; i < 3; i++) {
125 canvas0->cd(i+1);
126 if (!realData) {
127 mixh_[i] -> Add(h_[i],h_[i+3], procQCD,procWjets);
128 //mixh_[i] -> Add(mixh_[i],h_[i+6], 1,procttjets);
129 //cout << "histo_name: " << mixh_[0]->GetNbinsX() << endl;
130 }
131 else {
132 TH1F *htmp = (TH1F*) mfile["Data"]->Get(TString(histoName[i]));
133 mixh_[i] -> Add(htmp,1.);
134 }
135 mixh_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
136 mixh_[i]->GetYaxis()->SetTitle("Entries");
137 mixh_[i]->DrawClone();
138 }
139 canvas0->SaveAs(TString(desDir+"Data_distributions.pdf"));
140
141 //define the weight corrections for each sample
142 double NevData = mixh_[2]->Integral();
143 double corr_NevQCD = h_[2]->Integral();
144 double corr_NevQCD_NEW = hQCD_NEW[2]->Integral();
145 double corr_NevWjets = h_[5]->Integral();
146 double corr_Nevttjets = h_[8]->Integral();
147 double corr_Nevmix = procQCD*corr_NevQCD+procWjets*corr_NevWjets;
148 //double corr_Nevmix = procQCD*corr_NevQCD+procWjets*corr_NevWjets+procttjets*corr_Nevttjets;
149 if (!realData)
150 outprint << "Events mix sample = " << corr_Nevmix << endl;
151 else
152 outprint << "Events in Data = " << NevData << endl;
153 outprint << "Events QCD sample = " << corr_NevQCD << endl;
154 outprint << "Events Wjets sample = " << corr_NevWjets << endl;
155 outprint << "Events InvSel sample = " << corr_NevQCD_NEW << endl;
156
157 //define the containers for chosen numbers (coressponding to the max KStest result)
158 testMC maxProb[3];
159
160 //define the scale factors calculated using information obtained from all parameters
161 Double_t SFbackg = 0.0;
162 Double_t sumSFbackg = 0.0;
163 Double_t SFsample = 0.0;
164 Double_t sumSFsample = 0.0;
165 Double_t allKS = 0.0;
166
167 //do the KS test by varying the scale factors
168 for (int i = 0; i < 3; i++) { // 3 variables
169 TH1F *data = (TH1F*)mixh_[i]->Clone();
170 data -> SetName("dataClone");
171 //data -> Scale(1./data->Integral());
172 vector<testMC> resultsKS = doKStest((realData ? NevData : corr_Nevmix),
173 (useInv ? corr_NevQCD_NEW : corr_NevQCD),
174 corr_NevWjets,
175 data, hQCD_NEW[i], h_[i+3]);
176 testMC tksmax = getMax(resultsKS);
177 maxProb[i] = tksmax;
178 outprint << "\nFor the plot " << histoLabelX[i] << " the results are:"<< endl;
179 outprint << "\tmax Probability = " << maxProb[i].prob << endl;
180 outprint << "\tproc_background = " << maxProb[i].scaleF_backg << endl;
181 outprint << "\tproc_sample = " << maxProb[i].scaleF_sample << endl;
182
183 outprint << "\n\tpercent_B of Data = "
184 << maxProb[i].scaleF_backg*corr_NevQCD_NEW*100/(realData ? NevData : corr_Nevmix) << endl;
185 outprint << "\tpercent_S of Data = "
186 << maxProb[i].scaleF_sample*corr_NevWjets*100/(realData ? NevData : corr_Nevmix) << endl;
187 outprint << "---------------------------" << endl;
188
189 //create the mixed samples with KS test results
190 sumSFbackg += maxProb[i].prob*maxProb[i].scaleF_backg;
191 sumSFsample += maxProb[i].prob*maxProb[i].scaleF_sample;
192 allKS += maxProb[i].prob;
193
194 //fill a histogram with the results from the KS test for each variable
195 for (int jiter = 0; jiter < resultsKS.size(); jiter++) {
196 if (resultsKS.at(jiter).prob == 1.)
197 cout << "variable [" << i << "]: prob[" << jiter << "]= " << resultsKS.at(jiter).prob << endl;
198 hKSvalues_[i]->SetBinContent(jiter,resultsKS.at(jiter).prob);
199 }
200 delete data;
201 }
202
203 //calculate the final scale factors
204 SFbackg = sumSFbackg/allKS;
205 SFsample = sumSFsample/allKS;
206 outprint << "allKS = " << allKS << "\tbackground = " << SFbackg << "\tsample = " << SFsample << endl;
207 outprint << "==> Scale Factor for QCD MC = " << SFbackg*corr_NevQCD_NEW/corr_NevQCD << endl;
208 outprint << "\tcombined percent_B of Data = "
209 << SFbackg*corr_NevQCD_NEW*100/(realData ? NevData : corr_Nevmix) << endl;
210 outprint << "\tcombined percent_S of Data = "
211 << SFsample*corr_NevWjets*100/(realData ? NevData : corr_Nevmix) << endl;
212 outprint << "\n" << endl;
213 outprint << "=================================" << endl;
214 outprint << "\n" << endl;
215
216
217 //=================================
218 // Plots
219 //=================================
220 for (int i = 0; i < 3; i++) {// 3 variables
221 hKSres_[i] -> Add(hQCD_NEW[i],h_[i+3],SFbackg,SFsample);
222 outprint << "hKSres->Integral() = " << hKSres_[i]->Integral() << endl;
223 outprint << "Data->Integral() = " << mixh_[i]->Integral() << endl;
224
225 mixh_[i]->Rebin(2);
226 hQCD_NEW[i]->Rebin(2);
227 h_[i]->Rebin(2);
228 h_[i+3]->Rebin(2);
229 hKSres_[i]->Rebin(2);
230 //hKSvalues_[i]->Rebin(2);
231
232 mixh_[i] ->SetLineColor(1);
233 hQCD_NEW[i] ->SetLineColor(2);
234 h_[i] ->SetLineColor(4);
235 h_[i+3] ->SetLineColor(3);
236 hKSres_[i] ->SetLineColor(2);
237 hKSvalues_[i]->SetLineColor(i+1);
238
239 mixh_[i] ->SetMarkerColor(1);
240 hQCD_NEW[i] ->SetMarkerColor(2);
241 h_[i] ->SetMarkerColor(4);
242 h_[i+3] ->SetMarkerColor(3);
243 hKSres_[i] ->SetMarkerColor(2);
244 hKSvalues_[i]->SetMarkerColor(i+1);
245
246 mixh_[i] ->SetMarkerStyle(24);
247 hQCD_NEW[i] ->SetMarkerStyle(20);
248 h_[i] ->SetMarkerStyle(20);
249 h_[i+3] ->SetMarkerStyle(20);
250 hKSres_[i] ->SetMarkerStyle(20);
251 hKSvalues_[i]->SetMarkerStyle(20);
252
253 mixh_[i] ->SetMarkerSize(1.4);
254 hQCD_NEW[i] ->SetMarkerSize(1.1);
255 h_[i] ->SetMarkerSize(1.1);
256 h_[i+3] ->SetMarkerSize(1.1);
257 hKSres_[i] ->SetMarkerSize(0.9);
258 hKSvalues_[i]->SetMarkerSize(1.1);
259
260 mixh_[i] ->SetStats(0);
261 hQCD_NEW[i] ->SetStats(0);
262 h_[i] ->SetStats(0);
263 h_[i+3] ->SetStats(0);
264 hKSres_[i] ->SetStats(0);
265 hKSvalues_[i]->SetStats(0);
266
267 hKSres_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
268 hKSres_[i]->GetYaxis()->SetTitle("Entries");
269 hKSvalues_[i]->GetXaxis()->SetTitle("iteration #");
270 hKSvalues_[i]->GetYaxis()->SetTitle("KS test values");
271 h_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
272 h_[i]->GetYaxis()->SetTitle("A.U.");
273
274 TString nameCanvas1 = desDir+histoName[i]+"_QCD_"+suffix+".pdf";
275 TCanvas *canvas1 = new TCanvas(TString(histoName[i]+"_QCD"),"");
276 hQCD_NEW[i] -> Scale(1./hQCD_NEW[i]->Integral());
277 h_[i] -> Scale(1./h_[i]->Integral());
278 h_[i+3] -> Scale(1./h_[i+3]->Integral());
279 outprint << "For " << histoName[i] << " , the KStest result btw MC_QCD/InvSel is = "
280 << h_[i] -> KolmogorovTest(hQCD_NEW[i],"") << endl;
281 h_[i]->Draw("P");
282 if (useInv)
283 hQCD_NEW[i]->Draw("sameP");
284 h_[i+3]->Draw("sameP");
285 TLegend *legend1 = new TLegend(0.7, 0.70, 0.9, 0.85);
286 legend1->AddEntry(h_[i], "QCD");
287 if (useInv)
288 legend1->AddEntry(hQCD_NEW[i], "QCD - InvSel");
289 legend1->AddEntry(h_[i+3], "W+jets");
290 legend1->Draw();
291 legend1->SetFillColor(kWhite);
292 //latex->DrawLatex(0.22,0.91,histoName[i].c_str());
293 canvas1->SetLogy();
294 canvas1->SaveAs(nameCanvas1);
295
296 TString nameCanvas2 = desDir+histoName[i]+"_dataKS_"+suffix+".pdf";
297 TCanvas *canvas2 = new TCanvas(TString(histoName[i]+"_dataKS"),"");
298 hKSres_[i]->Draw("P");
299 mixh_[i]->Draw("sameP");
300 TLegend *legend2 = new TLegend(0.7, 0.70, 0.9, 0.85);
301 legend2->AddEntry(mixh_[i], "Data");
302 legend2->AddEntry(hKSres_[i], "KS result");
303 legend2->Draw();
304 legend2->SetFillColor(kWhite);
305 //latex->DrawLatex(0.22,0.91,histoName[i].c_str());
306 canvas2->SetLogy();
307 canvas2->SaveAs(nameCanvas2);
308
309 }
310
311
312 TCanvas *canvas3 = new TCanvas("KStestValues","");
313 //hKSvalues_[0]->GetXaxis()->SetRangeUser(0.9,1.2);
314 hKSvalues_[0]->GetYaxis()->SetRangeUser(1e-36,1.2);
315 hKSvalues_[0]->Draw();
316 hKSvalues_[1]->Draw("same");
317 hKSvalues_[2]->Draw("same");
318 TLegend *legend3 = new TLegend(0.7, 0.70, 0.9, 0.85);
319 legend3->AddEntry(hKSvalues_[0], "muon_pT");
320 legend3->AddEntry(hKSvalues_[1], "MET");
321 legend3->AddEntry(hKSvalues_[2], "W_mT");
322 legend3->Draw();
323 legend3->SetFillColor(kWhite);
324 //latex->DrawLatex(0.22,0.91,"KS test values");
325 canvas3->SetLogy();
326 TString nameCanvas3 = desDir+"KStestValues_newQCD"+suffix+".pdf";
327 canvas3->SaveAs(nameCanvas3);
328
329 }