ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/scripts/ikstest.cc
(Generate patch)

Comparing UserCode/Jeng/scripts/ikstest.cc (file contents):
Revision 1.3 by jengbou, Thu Sep 2 05:36:21 2010 UTC vs.
Revision 1.4 by jengbou, Fri Sep 3 05:05:19 2010 UTC

# Line 1 | Line 1
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 <sstream>
12 < #include <fstream>
13 < #include <iomanip>
14 < #include <map>
1 > #include "ikstest.h"
2  
3   using namespace std;
4  
5 < // User defined parameters
6 < bool useInv           = false; // whether to use n-1 QCD template
7 < bool realData         = false;
8 < const double stepsize = 0.001;
22 <
23 < //define the constants: 2.78/pb
24 < const double weight_[3]    = {0.0506107, //QCD
25 <                              0.0088222, //WJets
26 <                              0.000295   //TTbar
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 < struct testMC {
15 <  testMC(Double_t p = 0., Double_t sb = 0., Double_t ss = 0.){prob=p; scaleF_backg = sb; scaleF_sample = ss;}
16 <  Double_t prob;
35 <  Double_t scaleF_backg;
36 <  Double_t scaleF_sample;
37 < };
38 <
39 < //function for doing KS test
40 < vector<testMC> doKStest(Double_t NsS, Double_t Ns1, Double_t Ns2, TH1F* mixS, TH1F* s1, TH1F* s2) {
41 <  vector<testMC> output;
42 <  //define the scale factors
43 <  Double_t sf1 = 0.0; // QCD
44 <  Double_t sf2 = 0.0; // Wjets
45 <  //KS test
46 <  do {
47 <    sf1 = (NsS - Ns2*sf2)/Ns1;
48 <    if (sf1 < 0) break;
49 <    //cout << "..........sf1 = " << sf1 << endl;
50 <    int nbins = mixS->GetNbinsX();
51 <    double xmin = mixS->GetXaxis()->GetBinLowEdge(1);
52 <    double xmax = mixS->GetXaxis()->GetBinUpEdge(nbins);
53 <    TH1F *test = new TH1F("test", "", nbins, xmin, xmax);
54 <    test -> Sumw2();
55 <    test -> Add(s1,s2,sf1,sf2);
56 <    //test->Scale(1./(1.*test->Integral()));
57 <    Double_t probability = mixS -> KolmogorovTest(test,"");
58 <    testMC temp = testMC(probability,sf1,sf2);
59 <    output.push_back(temp);
60 < //     cout << "probability = " << setw(15) << temp.prob
61 < //       << "; sfQCD = "     << setw(10) << temp.scaleF_backg
62 < //       << "; sfWjets = "   << setw(6)  << temp.scaleF_sample << endl;
63 <    delete test;
64 <    sf2 = sf2 + stepsize;
65 <  } while(sf1 > 0 && sf2 <= 2.0);
66 <  return output;
67 < }
68 <
69 < //get the maximum KS test result
70 < testMC getMax(vector<testMC> vec) {
71 <  testMC maxKSRes;
72 <  Double_t maximum = 0.0;
73 <  for (size_t i = 0; i < vec.size(); i++) {
74 <    if (maximum < vec.at(i).prob)  {
75 <      maximum = vec.at(i).prob;
76 <      maxKSRes = vec.at(i);
77 <    }
78 <  }
79 <  cout << "for maximum: " << setw(12) << maxKSRes.prob
80 <       << "; sb = " << setw(10) << maxKSRes.scaleF_backg
81 <       << "; ss = " << setw(5) << maxKSRes.scaleF_sample << endl;
82 <  return maxKSRes;
83 < }
84 <
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 <  //Style();
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( "ikstest_results_20100901.txt" );
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_20100901/Data_RefSel_v3.root");
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("skimmed_Data_20100825/Data_D0ge0p03.root");
102 <      mfile["InvSel"] = TFile::Open("skimmed_Data_20100901/Data_RelIsoge0p1_v3.root");
58 >      mfile["InvSel"] = TFile::Open(TString("skimmed_Data_2.79pb-1/Data_Sel3_"+invName+".root"));
59      else
60 < //       mfile["InvSel"] = TFile::Open("skimmed_MC/QCD_D0ge0p03.root");
105 <      mfile["InvSel"] = TFile::Open("skimmed_MC/QCD_RelIsoge0p1_v3.root");
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_RefSel_v3.root");
65 <  mfile["1"] = TFile::Open("skimmed_MC/WJets_RefSel_v3.root");
66 <  mfile["2"] = TFile::Open("skimmed_MC/TTbar_RefSel_v3.root");
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}^{good Muons}", "E_{T}^{#nu}", "m_{T}^{W}"};
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.};
# Line 177 | Line 132 | void ikstest() {
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("Data_distributions.pdf");
139 >  canvas0->SaveAs(TString(desDir+"Data_distributions.pdf"));
140  
141    //define the weight corrections for each sample
142    double NevData                = mixh_[2]->Integral();
# Line 273 | Line 230 | void ikstest() {
230      //hKSvalues_[i]->Rebin(2);
231  
232      mixh_[i]     ->SetLineColor(1);
233 <    hQCD_NEW[i]  ->SetLineColor(3);
234 <    h_[i]        ->SetLineColor(6);
235 <    h_[i+3]      ->SetLineColor(4);
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(3);
241 <    h_[i]        ->SetMarkerColor(6);
242 <    h_[i+3]      ->SetMarkerColor(4);
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  
# Line 307 | Line 264 | void ikstest() {
264      hKSres_[i]   ->SetStats(0);
265      hKSvalues_[i]->SetStats(0);
266  
310    mixh_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
311    mixh_[i]->GetYaxis()->SetTitle("Entries");
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 <    string nameCanvas1 = histoName[i]+"_QCD.pdf";
275 <    TCanvas *canvas1 = new TCanvas(nameCanvas1.c_str(), "");
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 <    hQCD_NEW[i]->Draw("P");
282 <    h_[i]->Draw("sameP");
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], "default");
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");
330    legend1->AddEntry(hQCD_NEW[i], "new");
290      legend1->Draw();
291      legend1->SetFillColor(kWhite);
292 <    latex->DrawLatex(0.22,0.91,histoName[i].c_str());
292 >    //latex->DrawLatex(0.22,0.91,histoName[i].c_str());
293      canvas1->SetLogy();
294 <    canvas1->SaveAs(nameCanvas1.c_str());
294 >    canvas1->SaveAs(nameCanvas1);
295  
296 <    string nameCanvas2 = histoName[i]+"_dataKS.pdf";
297 <    TCanvas *canvas2 = new TCanvas(nameCanvas2.c_str(), "");
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);
# Line 343 | Line 302 | void ikstest() {
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());
305 >    //latex->DrawLatex(0.22,0.91,histoName[i].c_str());
306      canvas2->SetLogy();
307 <    canvas2->SaveAs(nameCanvas2.c_str());
307 >    canvas2->SaveAs(nameCanvas2);
308  
309    }
310  
311  
312 <  TCanvas *canvas3 = new TCanvas("KStestValues", "");
313 <  //hKSvalues_[0]->GetXaxis()->SetRangeUser(0.9,1.1);
314 <  //hKSvalues_[0]->GetYaxis()->SetRangeUser(1e-3,1.1);
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();
357  hKSvalues_[1]->SetLineColor(2);
316    hKSvalues_[1]->Draw("same");
359  hKSvalues_[2]->SetLineColor(4);
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");
# Line 364 | Line 321 | void ikstest() {
321    legend3->AddEntry(hKSvalues_[2], "W_mT");
322    legend3->Draw();
323    legend3->SetFillColor(kWhite);
324 <  latex->DrawLatex(0.22,0.91,"KS test values");
324 >  //latex->DrawLatex(0.22,0.91,"KS test values");
325    canvas3->SetLogy();
326 <  string nameCanvas3 = "KStestValues_newQCD.pdf";
327 <  canvas3->SaveAs(nameCanvas3.c_str());
326 >  TString nameCanvas3 = desDir+"KStestValues_newQCD"+suffix+".pdf";
327 >  canvas3->SaveAs(nameCanvas3);
328  
329   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines