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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines