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.4 by jengbou, Fri Sep 3 05:05:19 2010 UTC vs.
Revision 1.9 by jengbou, Thu Sep 9 08:03:25 2010 UTC

# Line 1 | Line 1
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: 2.79/pb
23 < const double weight_[3]    = {0.0507928, //QCD
24 <                              0.0088539, //WJets
25 <                              0.000296   //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;
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_Incl3/";
37   // User defined parameters
38 < bool useInv           = true; // whether to use n-1 QCD template
39 < bool realData         = true;
38 > bool realData        = true;
39 > bool useInv          = true; // whether to use n-1 QCD template
40 > bool useInvSelShape  = false; // whether to use shape from inverse selection for dataKS plots. Use when useInv is true
41 > double yLowBound     = 1.e-10;
42 > // Ntuples to use
43 > int n_sels           = 5; // 5:Run all selections
44 > TString suffixs[5]   = {"Incl3_Sel0","Incl3_Sel1","Incl3_Sel2","Incl3_Sel3","Incl3_Sel4"}; // Suffix of selection
45 > TString invNames[2]  = {"RelIsogt0p1","D0gt0p02"};
46 > TString invLabels[2] = {"RelIso > 0.1","d_{0} > 0.02"};
47 > map<TString,TCanvas*> cvs; // map of usual histogram
48 > bool debug_ = false;
49  
50   //=================================
51   // Main program
52   //=================================
53   void ikstest() {
54 +  CMSTopStyle style;
55    gROOT->SetStyle("CMS");
56    TLatex *latex = new TLatex();
57    latex->SetNDC();
58 <  TString suffix = "Sel3";
59 <  TString desDir = "Results_2.79pb-1/";
60 <  //TString invName = "RelIsoge0p1";
61 <  //TString invName = "D0ge0p03";
62 <  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){
58 >
59 >  struct stat stbDir;
60 >  if (stat(baseDir,&stbDir)){
61 >    cout << "Creating folder : " << baseDir << endl;
62 >    if (mkdir(baseDir,0755) == -1){
63        std::cerr << "Error creating folder" << endl;
64        return;
65      }
66    }
67  
68 <  ofstream outprint(TString(desDir+"Results_"+suffix+".txt"));
69 <  //open the files with histograms
70 <  map<string,TFile*> mfile;
71 <  mfile["Data"] = TFile::Open("skimmed_Data_2.79pb-1/Data_RefSel3.root");
72 <  // n-1 cuts
73 <  if (useInv) {
74 <    if (realData)
75 <      mfile["InvSel"] = TFile::Open(TString("skimmed_Data_2.79pb-1/Data_Sel3_"+invName+".root"));
76 <    else
77 <      mfile["InvSel"] = TFile::Open(TString("skimmed_MC/QCD_Sel3_"+invName+".root"));
78 <  }
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;
68 >  int size_ninv = (useInv ? 2 : 1);
69 >  for (int ninv = 0; ninv < size_ninv; ++ninv) {
70 >    TString invName = invNames[ninv];
71 >    TString desDir;
72 >    // create output directory
73 >    if (!useInv) {
74 >      if (!realData) desDir = baseDir + "MC/";
75 >      else desDir = baseDir + "Data_MC/";
76 >    } else {
77 >      if (!realData) desDir = baseDir + "MC_"+invName+"/";
78 >      else desDir = baseDir + "Data_"+invName+"/";
79      }
80 <    else {
81 <      TH1F *htmp = (TH1F*) mfile["Data"]->Get(TString(histoName[i]));
82 <      mixh_[i] -> Add(htmp,1.);
83 <    }
84 <    mixh_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
85 <    mixh_[i]->GetYaxis()->SetTitle("Entries");
86 <    mixh_[i]->DrawClone();
87 <  }
88 <  canvas0->SaveAs(TString(desDir+"Data_distributions.pdf"));
89 <
90 <  //define the weight corrections for each sample
91 <  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);
80 >    struct stat stDir;
81 >    if (!stat(desDir,&stDir)){
82 >      cout << "Output folder exists! Continue to overwrite it? (enter to continue; 'q' for quit)" << endl;
83 >      char incmd;
84 >      cin.get(incmd);
85 >      if (incmd == 'q') return;
86 >    } else {
87 >      cout << "Creating folder : " << desDir << endl;
88 >      if (mkdir(desDir,0755) == -1){
89 >        std::cerr << "Error creating folder" << endl;
90 >        return;
91 >      }
92      }
93 <    delete data;
94 <  }
95 <
96 <  //calculate the final scale factors
97 <  SFbackg = sumSFbackg/allKS;
98 <  SFsample = sumSFsample/allKS;
99 <  outprint << "allKS = " << allKS << "\tbackground = " << SFbackg << "\tsample = " << SFsample << endl;
100 <  outprint << "==> Scale Factor for QCD MC = " << SFbackg*corr_NevQCD_NEW/corr_NevQCD << endl;
101 <  outprint << "\tcombined percent_B of Data = "
102 <           << SFbackg*corr_NevQCD_NEW*100/(realData ? NevData : corr_Nevmix) << endl;
103 <  outprint << "\tcombined percent_S of Data = "
104 <           << SFsample*corr_NevWjets*100/(realData ? NevData : corr_Nevmix) << endl;
105 <  outprint << "\n" << endl;
106 <  outprint << "=================================" << endl;
107 <  outprint << "\n" << endl;
108 <
109 <
110 <  //=================================
111 <  // Plots
112 <  //=================================
113 <  for (int i = 0; i < 3; i++) {// 3 variables
114 <    hKSres_[i] -> Add(hQCD_NEW[i],h_[i+3],SFbackg,SFsample);
115 <    outprint << "hKSres->Integral() = " << hKSres_[i]->Integral() << endl;
116 <    outprint << "Data->Integral()   = " << mixh_[i]->Integral() << endl;
117 <
118 <    mixh_[i]->Rebin(2);
119 <    hQCD_NEW[i]->Rebin(2);
120 <    h_[i]->Rebin(2);
121 <    h_[i+3]->Rebin(2);
122 <    hKSres_[i]->Rebin(2);
123 <    //hKSvalues_[i]->Rebin(2);
124 <
125 <    mixh_[i]     ->SetLineColor(1);
126 <    hQCD_NEW[i]  ->SetLineColor(2);
127 <    h_[i]        ->SetLineColor(4);
128 <    h_[i+3]      ->SetLineColor(3);
129 <    hKSres_[i]   ->SetLineColor(2);
130 <    hKSvalues_[i]->SetLineColor(i+1);
131 <
132 <    mixh_[i]     ->SetMarkerColor(1);
133 <    hQCD_NEW[i]  ->SetMarkerColor(2);
134 <    h_[i]        ->SetMarkerColor(4);
135 <    h_[i+3]      ->SetMarkerColor(3);
136 <    hKSres_[i]   ->SetMarkerColor(2);
137 <    hKSvalues_[i]->SetMarkerColor(i+1);
138 <
139 <    mixh_[i]     ->SetMarkerStyle(24);
140 <    hQCD_NEW[i]  ->SetMarkerStyle(20);
141 <    h_[i]        ->SetMarkerStyle(20);
142 <    h_[i+3]      ->SetMarkerStyle(20);
143 <    hKSres_[i]   ->SetMarkerStyle(20);
144 <    hKSvalues_[i]->SetMarkerStyle(20);
145 <
146 <    mixh_[i]     ->SetMarkerSize(1.4);
147 <    hQCD_NEW[i]  ->SetMarkerSize(1.1);
148 <    h_[i]        ->SetMarkerSize(1.1);
149 <    h_[i+3]      ->SetMarkerSize(1.1);
150 <    hKSres_[i]   ->SetMarkerSize(0.9);
151 <    hKSvalues_[i]->SetMarkerSize(1.1);
152 <
153 <    mixh_[i]     ->SetStats(0);
154 <    hQCD_NEW[i]  ->SetStats(0);
155 <    h_[i]        ->SetStats(0);
156 <    h_[i+3]      ->SetStats(0);
157 <    hKSres_[i]   ->SetStats(0);
158 <    hKSvalues_[i]->SetStats(0);
159 <
160 <    hKSres_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
161 <    hKSres_[i]->GetYaxis()->SetTitle("Entries");
162 <    hKSvalues_[i]->GetXaxis()->SetTitle("iteration #");
163 <    hKSvalues_[i]->GetYaxis()->SetTitle("KS test values");
164 <    h_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
165 <    h_[i]->GetYaxis()->SetTitle("A.U.");
166 <
167 <    TString nameCanvas1 = desDir+histoName[i]+"_QCD_"+suffix+".pdf";
168 <    TCanvas *canvas1 = new TCanvas(TString(histoName[i]+"_QCD"),"");
169 <    hQCD_NEW[i] -> Scale(1./hQCD_NEW[i]->Integral());
170 <    h_[i] -> Scale(1./h_[i]->Integral());
171 <    h_[i+3] -> Scale(1./h_[i+3]->Integral());
172 <    outprint << "For " << histoName[i] << " , the KStest result btw MC_QCD/InvSel is = "
173 <             << h_[i] -> KolmogorovTest(hQCD_NEW[i],"") << endl;
174 <    h_[i]->Draw("P");
175 <    if (useInv)
176 <      hQCD_NEW[i]->Draw("sameP");
177 <    h_[i+3]->Draw("sameP");
178 <    TLegend *legend1 = new TLegend(0.7, 0.70, 0.9, 0.85);
179 <    legend1->AddEntry(h_[i], "QCD");
180 <    if (useInv)
181 <      legend1->AddEntry(hQCD_NEW[i], "QCD - InvSel");
182 <    legend1->AddEntry(h_[i+3], "W+jets");
183 <    legend1->Draw();
184 <    legend1->SetFillColor(kWhite);
185 <    //latex->DrawLatex(0.22,0.91,histoName[i].c_str());
186 <    canvas1->SetLogy();
187 <    canvas1->SaveAs(nameCanvas1);
188 <
189 <    TString nameCanvas2 = desDir+histoName[i]+"_dataKS_"+suffix+".pdf";
190 <    TCanvas *canvas2 = new TCanvas(TString(histoName[i]+"_dataKS"),"");
191 <    hKSres_[i]->Draw("P");
192 <    mixh_[i]->Draw("sameP");
193 <    TLegend *legend2 = new TLegend(0.7, 0.70, 0.9, 0.85);
194 <    legend2->AddEntry(mixh_[i], "Data");
195 <    legend2->AddEntry(hKSres_[i], "KS result");
196 <    legend2->Draw();
197 <    legend2->SetFillColor(kWhite);
198 <    //latex->DrawLatex(0.22,0.91,histoName[i].c_str());
199 <    canvas2->SetLogy();
200 <    canvas2->SaveAs(nameCanvas2);
201 <
202 <  }
203 <
204 <
205 <  TCanvas *canvas3 = new TCanvas("KStestValues","");
206 <  //hKSvalues_[0]->GetXaxis()->SetRangeUser(0.9,1.2);
207 <  hKSvalues_[0]->GetYaxis()->SetRangeUser(1e-36,1.2);
208 <  hKSvalues_[0]->Draw();
209 <  hKSvalues_[1]->Draw("same");
210 <  hKSvalues_[2]->Draw("same");
211 <  TLegend *legend3 = new TLegend(0.7, 0.70, 0.9, 0.85);
212 <  legend3->AddEntry(hKSvalues_[0], "muon_pT");
213 <  legend3->AddEntry(hKSvalues_[1], "MET");
214 <  legend3->AddEntry(hKSvalues_[2], "W_mT");
215 <  legend3->Draw();
216 <  legend3->SetFillColor(kWhite);
217 <  //latex->DrawLatex(0.22,0.91,"KS test values");
218 <  canvas3->SetLogy();
219 <  TString nameCanvas3 = desDir+"KStestValues_newQCD"+suffix+".pdf";
220 <  canvas3->SaveAs(nameCanvas3);
93 >    
94 >    for (int nsel = 0; nsel < n_sels; ++nsel) {
95 >      TString suffix = suffixs[nsel];
96 >
97 >      ofstream outprint(TString(desDir+"Results_"+suffix+".txt"));
98 >      //open the files with histograms
99 >      map<string,TFile*> mfile;
100 >      mfile.clear();
101 >      mfile["Data"] = TFile::Open(TString("skimmed_Data_2.88pb-1/Data_"+suffix+".root"));
102 >      // n-1 cuts
103 >      if (useInv) {
104 >        if (realData)
105 >          mfile["InvSel"] = TFile::Open(TString("skimmed_Data_2.88pb-1/Data_"+suffix+"_"+invName+".root"));
106 >        else
107 >          mfile["InvSel"] = TFile::Open(TString("skimmed_MC/v5/QCD_"+suffix+"_"+invName+".root"));
108 >      }
109 >      // RefSel MC
110 >      mfile["0"] = TFile::Open(TString("skimmed_MC/v5/QCD_"+suffix+".root"));
111 >      mfile["1"] = TFile::Open(TString("skimmed_MC/v5/WJets_"+suffix+".root"));
112 >      mfile["2"] = TFile::Open(TString("skimmed_MC/v5/TTbar_"+suffix+".root"));
113 >      mfile["3"] = TFile::Open(TString("skimmed_MC/v5/ZJets_"+suffix+".root"));
114 >      mfile["4"] = TFile::Open(TString("skimmed_MC/v5/STtch_"+suffix+".root"));
115 >
116 >      //define histograms and related parameters
117 >      string histoName[3] = {"h_mu_pt_calo","h_met_calo","h_mt_calo"};
118 >      string histoLabelX[3] = {"p_{T}^{#mu} [GeV/c]", "#slash{E}_{T} [GeV/c]", "M_{T}^{W} [GeV/c^{2}]"};
119 >      Int_t xbins[3] = {20,20,40};
120 >      Double_t xlow[3] = {0.,0.,0.};
121 >      Double_t xhigh[3] = {100.,100.,200.};
122 >      string sample[5] = {"QCD","WJets","TTbar","ZJets","STtch"};
123 >
124 >      TH1F* hMC_[5][3];   // MC histograms
125 >      TH1F* hData_[3];    // Data or mix MC
126 >      TH1F* hQCD_NEW[3];  // InvSel QCD shape
127 >      TH1F* hKSres_[3];
128 >      TH1F* hKSvalues_[3];
129 >      TH1F* hQCD_KS[3];
130 >      TH1F* hWJets_KS[3];
131 >
132 >      //load the histograms from the root files
133 >      for (int vi = 0; vi < 3; ++vi) {// 3 variables
134 >        //cout << "file[" << vi << "] : " << endl;
135 >        string nameNewHisto = "mix_"+histoName[vi];
136 >        string nameNewHistoSFKS = "finalSF_"+histoName[vi];
137 >        string nameNewHistoKSvalues = "KSvalues_"+histoLabelX[vi];
138 >
139 >        hData_[vi] = new TH1F(nameNewHisto.c_str(),"",xbins[vi],xlow[vi],xhigh[vi]);
140 >        hKSres_[vi] = new TH1F(nameNewHistoSFKS.c_str(),"",xbins[vi],xlow[vi],xhigh[vi]);
141 >        hKSvalues_[vi] = new TH1F(nameNewHistoKSvalues.c_str(),"",2./stepsize, stepsize, 2.+stepsize);
142 >
143 >        ostringstream ssc;
144 >        ssc << vi;
145 >
146 >        if (!useInv) {//use QCD MC sample
147 >          hQCD_NEW[vi] = (TH1F*) mfile["0"]->Get(TString(histoName[vi]))->Clone();
148 >          hQCD_NEW[vi] -> Scale(weight_[0]);
149 >          hQCD_NEW[vi] -> SetName(TString("InvSel_"+histoName[vi]+"_"+ssc.str()));
150 >        }
151 >        else {
152 >          hQCD_NEW[vi] = (TH1F*) mfile["InvSel"]->Get(TString(histoName[vi]))->Clone();
153 >          if (!realData) hQCD_NEW[vi] -> Scale(weight_[0]);
154 >          hQCD_NEW[vi] -> SetName(TString("InvSel_"+histoName[vi]+"_"+ssc.str()));
155 >        }
156 >        if (debug_) cout << "hQCD_NEW[" << vi << "] @ " << hQCD_NEW[vi] << endl;
157 >
158 >        hData_[vi] -> Sumw2();
159 >        hKSres_[vi] -> Sumw2();
160 >        hKSvalues_[vi] -> Sumw2();
161 >      }
162 >
163 >      for (int n = 0; n < 5; ++n) {// 3 MC samples
164 >        for (int ihisto = 0; ihisto < 3; ihisto++) {// 3 variables
165 >          //cout << "Variable[" << ihisto << "]" << endl;
166 >          string histo_name = histoName[ihisto]+sample[n];
167 >          ostringstream ss;
168 >          ss << n;
169 >          hMC_[n][ihisto] = (TH1F*) mfile[ss.str()]->Get(TString(histoName[ihisto]))->Clone();
170 >          if (debug_) {
171 >            cout << "File[" << n << "] @" << mfile[ss.str()]
172 >                 << "; histo[" << ihisto << "] @ " <<  mfile[ss.str()]->Get(TString(histoName[ihisto]))
173 >                 <<"; hMC_[" << n << "][" << ihisto << "] raw evts = "
174 >                 << setw(12) << hMC_[n][ihisto]->Integral();
175 >          }
176 >          hMC_[n][ihisto] -> Scale(weight_[n]);
177 >          hMC_[n][ihisto] -> SetName(TString("MC_"+sample[n]+"_"+histoName[ihisto]));
178 >          if (debug_) cout << "; weighted num evts = " << setw(8) << hMC_[n][ihisto]->Integral() << endl;
179 >        }
180 >      }
181 >
182 >      //create the mixed samples = "data"
183 >      TString cvsName0 = "Data";
184 >      if (useInv) {
185 >        cvsName0 += "_";
186 >        cvsName0 += invName;
187 >      }
188 >      cvs[cvsName0] = new TCanvas(cvsName0,"Data distributions",600,700);
189 >      cvs[cvsName0]->Divide(3,1);
190 >      for (int i = 0; i < 3; i++) {
191 >        cvs[cvsName0]->cd(i+1);
192 >        if (!realData) {
193 >          hData_[i] -> Add(hMC_[0][i],hMC_[1][i], procQCD,procWJets);
194 >          hData_[i] -> Add(hMC_[2][i], procTTbar);
195 >          hData_[i] -> Add(hMC_[3][i], procZJets);
196 >          hData_[i] -> Add(hMC_[4][i], procSTtch);
197 >        }
198 >        else {
199 >          TH1F *htmp = (TH1F*) mfile["Data"]->Get(TString(histoName[i]));
200 >          hData_[i] -> Add(htmp,1.);
201 >        }
202 >        hData_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
203 >        hData_[i]->GetYaxis()->SetTitle("Events");
204 >        if (i == 0) // mu pT plot
205 >          hData_[0]->GetXaxis()->SetRangeUser(20.,xhigh[0]);
206 >        hData_[i]->DrawClone("hist");
207 >      }
208 >      cvs[cvsName0]->SaveAs(TString(desDir+"Data_distributions.pdf"));
209 >      cvs[cvsName0]->SaveAs(TString(desDir+"Data_distributions.png"));
210 >
211 >      //Calculate num of events for each sample
212 >      vector<double> vNev_;
213 >
214 >      double NevData                = hData_[2]->Integral();
215 >      double corr_NevQCD          = hMC_[0][2]->Integral();
216 >      double corr_NevQCD_NEW        = hQCD_NEW[2]->Integral();
217 >      double corr_NevWJets        = hMC_[1][2]->Integral();
218 >      double corr_NevTTbar        = hMC_[2][2]->Integral();
219 >      double corr_NevZJets        = hMC_[3][2]->Integral();
220 >      double corr_NevSTtch        = hMC_[4][2]->Integral();
221 >      cout << "corr_NevSTtch = " << corr_NevSTtch << endl;
222 >      //     double corr_Nevmix            = procQCD*corr_NevQCD + procWJets*corr_NevWJets
223 >      //       + procTTbar*corr_NevTTbar+procZJets*corr_NevZJets + procSTtch*corr_NevSTtch;//should equal NevData for MC
224 >      // store nev in a vector
225 >      vNev_.push_back(NevData);
226 >      vNev_.push_back((useInv ? corr_NevQCD_NEW : corr_NevQCD));
227 >      vNev_.push_back(corr_NevWJets);
228 >
229 >      // Non WJets (use MC expected values):
230 >      if (procTTbar > 0.) vNev_.push_back(corr_NevTTbar*procTTbar);
231 >      if (procZJets > 0.) vNev_.push_back(corr_NevZJets*procZJets);
232 >      if (procSTtch > 0.) vNev_.push_back(corr_NevSTtch*procSTtch);
233 >
234 >      outprint << "Events in Data       = " << NevData << endl;
235 >      outprint << "Events QCD sample    = " << corr_NevQCD << endl;
236 >      outprint << "Events InvSel sample = " << corr_NevQCD_NEW << endl;
237 >      outprint << "---------------------------" << endl;
238 >      outprint << "Events WJets sample  = " << corr_NevWJets << endl;
239 >      outprint << "Events TTbar sample  = " << corr_NevTTbar << endl;
240 >      outprint << "Events ZJets sample  = " << corr_NevZJets << endl;
241 >      outprint << "Events STtch sample  = " << corr_NevSTtch << endl;
242 >
243 >      //define the containers for chosen numbers (coressponding to the max KStest result)
244 >      testMC maxProb[3];
245 >
246 >      //define the scale factors calculated using information obtained from all parameters
247 >      Double_t SFbackg = 0.0;
248 >      Double_t sumSFbackg = 0.0;
249 >      Double_t SFsample = 0.0;
250 >      Double_t sumSFsample = 0.0;
251 >      Double_t allKS = 0.0;
252 >
253 >      //do the KS test by varying the scale factors
254 >      for (int i = 0; i < 3; i++) { // 3 variables
255 >        TH1F *data = (TH1F*)hData_[i]->Clone("data");
256 >        data -> SetName("dataClone");
257 >        map<string,TH1F*> mHisto_;
258 >        mHisto_.clear();
259 >        mHisto_["Data"]  = data;
260 >        mHisto_["QCD"]   = (useInv ? (TH1F*)hQCD_NEW[i]->Clone() : (TH1F*)hMC_[0][i]->Clone());
261 >        mHisto_["WJets"] = (TH1F*)hMC_[1][i]->Clone();//WJets
262 >        if (procTTbar > 0.) mHisto_["TTbar"] = (TH1F*)hMC_[2][i]->Clone();//TTbar
263 >        if (procZJets > 0.) mHisto_["ZJets"] = (TH1F*)hMC_[3][i]->Clone();//ZJets
264 >        if (procSTtch > 0.) mHisto_["STtch"] = (TH1F*)hMC_[4][i]->Clone();//STtch
265 >
266 >        //data -> Scale(1./data->Integral());
267 >        vector<testMC> resultsKS = doKStest(vNev_,mHisto_);
268 >        testMC tksmax = getMax(resultsKS);
269 >        maxProb[i] = tksmax;
270 >        outprint << "\nFor the plot " << histoLabelX[i] << " the results are:"<< endl;
271 >        outprint << "\tmax Probability = " << maxProb[i].prob << endl;
272 >        outprint << "\tproc_background = " << maxProb[i].scaleF_backg << endl;
273 >        outprint << "\tproc_sample     = " << maxProb[i].scaleF_sample << endl;
274 >
275 >        outprint << "\n\tpercent_B of Data = "
276 >                 << maxProb[i].scaleF_backg*corr_NevQCD_NEW*100/NevData << endl;
277 >        outprint << "\tpercent_S of Data = "
278 >                 << maxProb[i].scaleF_sample*corr_NevWJets*100/NevData << endl;
279 >        outprint << "---------------------------" << endl;
280 >
281 >        //create the mixed samples with KS test results
282 >        sumSFbackg += maxProb[i].prob*maxProb[i].scaleF_backg;
283 >        sumSFsample += maxProb[i].prob*maxProb[i].scaleF_sample;
284 >        allKS += maxProb[i].prob;
285 >
286 >        //fill a histogram with the results from the KS test for each variable
287 >        for (unsigned int jiter = 0; jiter < resultsKS.size(); jiter++) {
288 >          if (resultsKS.at(jiter).prob == 1.)
289 >            cout << "variable [" << i << "]: prob[" << jiter << "]= " << resultsKS.at(jiter).prob << endl;
290 >          hKSvalues_[i]->SetBinContent(jiter,resultsKS.at(jiter).prob);
291 >        }
292 >        delete data;
293 >      }
294 >
295 >      //calculate the final scale factors
296 >      SFbackg = sumSFbackg/allKS;
297 >      SFsample = sumSFsample/allKS;
298 >      double SF_QCD = SFbackg*corr_NevQCD_NEW/corr_NevQCD;
299 >      outprint << "allKS = " << allKS << "\tbackground = " << SFbackg << "\tsample = " << SFsample << endl;
300 >      outprint << "==> Scale Factor for QCD MC = " << SF_QCD << endl;
301 >      outprint << "\tcombined percent_B of Data = "
302 >               << SFbackg*corr_NevQCD_NEW*100/NevData << endl;
303 >      outprint << "\tcombined percent_S of Data = "
304 >               << SFsample*corr_NevWJets*100/NevData << endl;
305 >      outprint << "\n" << endl;
306 >      outprint << "=================================" << endl;
307 >      outprint << "\n" << endl;
308 >
309 >
310 >      //=================================
311 >      // Plots
312 >      //=================================
313 >      for (int i = 0; i < 3; i++) {// 3 variables
314 >        gStyle->SetErrorX(0.);
315 >        hKSres_[i] -> Add((TH1F*)hQCD_NEW[i]->Clone(),(TH1F*)hMC_[1][i]->Clone(),SFbackg,SFsample);
316 >        hKSres_[i] -> Add((TH1F*)hMC_[2][i]->Clone(),procTTbar);
317 >        hKSres_[i] -> Add((TH1F*)hMC_[3][i]->Clone(),procZJets);
318 >        hKSres_[i] -> Add((TH1F*)hMC_[4][i]->Clone(),procSTtch);
319 >
320 >        outprint << "hKSres->Integral() = " << hKSres_[i]->Integral() << endl;
321 >        outprint << "Data->Integral()   = " << hData_[i]->Integral() << endl;
322 >
323 >        hData_[i]->Rebin(2);
324 >        hQCD_NEW[i]->Rebin(2);
325 >        hMC_[0][i]->Rebin(2);
326 >        hMC_[1][i]->Rebin(2);
327 >        hKSres_[i]->Rebin(2);
328 >        //hKSvalues_[i]->Rebin(2);
329 >
330 >        // Stack Wjets and QCD after scaled by KS factors
331 >        if (useInv && useInvSelShape) {
332 >          hQCD_KS[i] = (TH1F*) hQCD_NEW[i]->Clone();
333 >          hQCD_KS[i]->Scale(SFbackg);
334 >        }
335 >        else {// Use MC QCD
336 >          hQCD_KS[i] = (TH1F*) hMC_[0][i]->Clone();
337 >          hQCD_KS[i]->Scale(SF_QCD);
338 >        }
339 >        hQCD_KS[i]->SetLineColor(style.QCDColor);
340 >        hQCD_KS[i]->SetFillColor(style.QCDColor);
341 >        hQCD_KS[i]->SetFillStyle(style.QCDFill);
342 >
343 >        hWJets_KS[i] = (TH1F*) hMC_[1][i]->Clone();
344 >        hWJets_KS[i]->Scale(SFsample);
345 >        hWJets_KS[i]->SetLineColor(style.WJetsColor);
346 >        hWJets_KS[i]->SetFillColor(style.WJetsColor);
347 >        hWJets_KS[i]->SetFillStyle(style.WJetsFill);
348 >
349 >        if (procTTbar > 0.) {
350 >          hMC_[2][i]->Rebin(2);
351 >          hMC_[2][i]->SetLineColor(style.TtbarColor);
352 >          hMC_[2][i]->SetFillColor(style.TtbarColor);
353 >          hMC_[2][i]->SetFillStyle(style.TtbarFill);
354 >        }
355 >        if (procZJets > 0.) {
356 >          hMC_[3][i]->Rebin(2);
357 >          hMC_[3][i]->SetLineColor(style.DYZJetsColor);
358 >          hMC_[3][i]->SetFillColor(style.DYZJetsColor);
359 >          hMC_[3][i]->SetFillStyle(style.DYZJetsFill);
360 >        }
361 >        if (procSTtch > 0.) {
362 >          hMC_[4][i]->Rebin(2);
363 >          hMC_[4][i]->SetLineColor(style.ST_tColor);
364 >          hMC_[4][i]->SetFillColor(style.ST_tColor);
365 >          hMC_[4][i]->SetFillStyle(style.ST_tFill);
366 >        }
367 >
368 >        THStack *hst = new THStack(invName,invName);
369 >        hst->Add((TH1F*)hQCD_KS[i]->Clone());
370 >        if (procSTtch > 0) hst->Add((TH1F*)hMC_[4][i]->Clone());
371 >        if (procZJets > 0) hst->Add((TH1F*)hMC_[3][i]->Clone());
372 >        hst->Add((TH1F*)hWJets_KS[i]->Clone());
373 >        if (procTTbar > 0) hst->Add((TH1F*)hMC_[2][i]->Clone());
374 >
375 >        // Set plotting parameters
376 >        hData_[i]    ->SetLineColor(1);
377 >        hQCD_NEW[i]  ->SetLineColor(2);
378 >        hMC_[0][i]   ->SetLineColor(4);
379 >        hMC_[1][i]   ->SetLineColor(3);
380 >        hKSres_[i]   ->SetLineColor(9);
381 >        hKSvalues_[i]->SetLineColor(i+1);
382 >
383 >        hData_[i]    ->SetMarkerColor(1);
384 >        hQCD_NEW[i]  ->SetMarkerColor(2);
385 >        hMC_[0][i]   ->SetMarkerColor(4);
386 >        hMC_[1][i]   ->SetMarkerColor(3);
387 >        hKSres_[i]   ->SetMarkerColor(9);
388 >        hKSvalues_[i]->SetMarkerColor(i+1);
389 >
390 >        hData_[i]    ->SetMarkerStyle((realData ? 20 : 24));
391 >        hQCD_NEW[i]  ->SetMarkerStyle(20);
392 >        hMC_[0][i]   ->SetMarkerStyle(20);
393 >        hMC_[1][i]   ->SetMarkerStyle(20);
394 >        hKSres_[i]   ->SetMarkerStyle(20);
395 >        hKSvalues_[i]->SetMarkerStyle(20);
396 >
397 >        hData_[i]    ->SetMarkerSize((realData ? 1.2 : 1.4));
398 >        hQCD_NEW[i]  ->SetMarkerSize(1.1);
399 >        hMC_[0][i]   ->SetMarkerSize(1.1);
400 >        hMC_[1][i]   ->SetMarkerSize(1.1);
401 >        hKSres_[i]   ->SetMarkerSize(1.18);
402 >        hKSvalues_[i]->SetMarkerSize(1.1);
403 >
404 >        hData_[i]    ->SetStats(0);
405 >        hQCD_NEW[i]  ->SetStats(0);
406 >        hMC_[0][i]   ->SetStats(0);
407 >        hMC_[1][i]   ->SetStats(0);
408 >        hKSres_[i]   ->SetStats(0);
409 >        hKSvalues_[i]->SetStats(0);
410 >        hQCD_KS[i]   ->SetStats(0);
411 >        hWJets_KS[i] ->SetStats(0);
412 >
413 >        hKSres_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
414 >        hKSres_[i]->GetYaxis()->SetTitle("Events");
415 >        hKSvalues_[i]->GetXaxis()->SetTitle("#lambda_{wjets}");
416 >        hKSvalues_[i]->GetYaxis()->SetTitle("KS test values");
417 >        hMC_[0][i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
418 >        hMC_[0][i]->GetYaxis()->SetTitle("A.U.");
419 >
420 >        TLatex* text1 = new TLatex(1., 1., "2.88 pb^{-1} at #sqrt{s} = 7 TeV");
421 >        text1->SetNDC();
422 >        text1->SetTextAlign(13);
423 >        text1->SetX(0.63);
424 >        text1->SetY(0.91);
425 >        text1->SetTextFont(42);
426 >        text1->SetTextSize(0.03);
427 >        text1->SetTextSizePixels(20);// dflt=28
428 >
429 >        // QCD shape comparison
430 >        TString cvsName1 = histoName[i]+"_QCD";
431 >        double val_Max_ = 0.;
432 >
433 >        if(useInv) cvsName1 = cvsName1 + "_" + invName;
434 >        cvs[cvsName1] = new TCanvas(cvsName1,"",600,700);
435 >        hQCD_NEW[i] -> Scale(1./hQCD_NEW[i]->Integral());
436 >        hMC_[0][i] -> Scale(1./hMC_[0][i]->Integral());
437 >        hMC_[1][i] -> Scale(1./hMC_[1][i]->Integral());
438 >        outprint << "For " << histoName[i] << " , the KStest result btw MC_QCD/InvSel is = "
439 >                 << hMC_[0][i] -> KolmogorovTest(hQCD_NEW[i],"") << endl;
440 >
441 >        hMC_[0][i]->GetXaxis()->SetLabelFont(42);
442 >        hMC_[0][i]->GetXaxis()->SetLabelSize(0.04);
443 >        hMC_[0][i]->GetXaxis()->SetTitleFont(42);
444 >        hMC_[0][i]->GetXaxis()->SetTitleSize(0.05);
445 >        hMC_[0][i]->GetXaxis()->SetTitleOffset(1.2);
446 >        hMC_[0][i]->GetYaxis()->SetLabelFont(42);
447 >        hMC_[0][i]->GetYaxis()->SetLabelSize(0.04);
448 >        hMC_[0][i]->GetYaxis()->SetTitleFont(42);
449 >        hMC_[0][i]->GetYaxis()->SetTitleSize(0.05);
450 >        hMC_[0][i]->GetYaxis()->SetTitleOffset(1.3);
451 >        // Get better Y range
452 >        Int_t nbMax = hMC_[0][i]->GetMaximumBin();
453 >        double tmpval = hMC_[0][i]->GetBinContent(nbMax) + hMC_[0][i]->GetBinError(nbMax);
454 >        val_Max_ = (val_Max_ > 1.2*tmpval ? val_Max_ : 1.2*tmpval);
455 >
456 >        if (useInv) {
457 >          nbMax = hQCD_NEW[i]->GetMaximumBin();
458 >          tmpval = hQCD_NEW[i]->GetBinContent(nbMax) + hQCD_NEW[i]->GetBinError(nbMax);
459 >          val_Max_ = (val_Max_ > 1.2*tmpval ? val_Max_ : 1.2*tmpval);
460 >        }
461 >        hMC_[0][i]->GetYaxis()->SetRangeUser(0.,(val_Max_ > 1. ? 1. : val_Max_));
462 >        if (i == 0) // mu pT plot
463 >          hMC_[0][0]->GetXaxis()->SetRangeUser(20.,xhigh[0]);
464 >        hMC_[0][i]->DrawCopy("histe");
465 >
466 >        if (useInv)
467 >          hQCD_NEW[i]->DrawCopy("samehiste");
468 >
469 >        hMC_[1][i]->DrawCopy("samehiste");
470 >        TLegend *legend1 = new TLegend(0.6, 0.65, 0.92, 0.9);
471 >        legend1->SetFillColor(0);
472 >        legend1->SetFillStyle(0);
473 >
474 >        legend1->AddEntry(hMC_[0][i], "QCD");
475 >        if (useInv)
476 >          if (!realData) legend1->AddEntry(hQCD_NEW[i], TString("QCD (" + invLabels[ninv] + ")"));
477 >          else legend1->AddEntry(hQCD_NEW[i], TString("Data (" + invLabels[ninv]) + ")");
478 >
479 >        legend1->AddEntry(hMC_[1][i], style.WJetsText);
480 >        legend1->Draw();
481 >        legend1->SetFillColor(kWhite);
482 >        //latex->DrawLatex(0.22,0.91,histoName[i].c_str());
483 >        //cvs[cvsName1]->SetLogy();
484 >        TString nameCanvas1 = desDir+histoName[i]+"_QCD_"+suffix;
485 >        cvs[cvsName1]->SaveAs(nameCanvas1+".pdf");
486 >        cvs[cvsName1]->SaveAs(nameCanvas1+".png");
487 >
488 >
489 >        // KS results vs. (pseudo) Data
490 >        TString cvsName2 = histoName[i]+"_dataKS";
491 >        if(useInv) cvsName2 = cvsName2 + "_" + invName;
492 >        cvs[cvsName2] = new TCanvas(cvsName2,"",600,700);
493 >
494 >        // Get better Y range
495 >        val_Max_ = 0.;
496 >        nbMax = hData_[i]->GetMaximumBin();
497 >        tmpval = hData_[i]->GetBinContent(nbMax) + hData_[i]->GetBinError(nbMax);
498 >        val_Max_ = (val_Max_ > 1.05*tmpval ? val_Max_ : 1.05*tmpval);
499 >        nbMax = hKSres_[i]->GetMaximumBin();
500 >        tmpval = hKSres_[i]->GetBinContent(nbMax) + hKSres_[i]->GetBinError(nbMax);
501 >        val_Max_ = (val_Max_ > 1.05*tmpval ? val_Max_ : 1.05*tmpval);
502 >
503 >        hData_[i]->GetYaxis()->SetRangeUser(0.,val_Max_);
504 >
505 >        if (i == 0) // mu pT plot
506 >          hData_[0]->GetXaxis()->SetRangeUser(20.,xhigh[0]);
507 >        hData_[i]->Draw("P");
508 >        hst->Draw("samehist");
509 >        hKSres_[i]->Draw("samehiste");
510 >        hData_[i]->Draw("sameP");
511 >        TLegend *legend2 = new TLegend(0.7, 0.64, 0.9, 0.88);
512 >        legend2->AddEntry(hData_[i], (realData ? "Data" : "Pseudo Data"),"p");
513 >        legend2->AddEntry(hKSres_[i], "KS result");
514 >        if (procTTbar > 0.) legend2->AddEntry(hMC_[2][i], style.TtbarText);
515 >        legend2->AddEntry(hWJets_KS[i], style.WJetsText);
516 >        if (procZJets > 0.) legend2->AddEntry(hMC_[3][i], style.DYZJetsText);
517 >        if (procSTtch > 0.) legend2->AddEntry(hMC_[4][i], style.ST_tText);
518 >        if (realData)
519 >          legend2->AddEntry(hQCD_KS[i], ((!useInvSelShape || !useInv) ? style.QCDText :
520 >                                         TString("#splitline{Data (" + invLabels[ninv] + ",}{Scaled by IKS)}")));
521 >        else
522 >          legend2->AddEntry(hQCD_KS[i], ((!useInvSelShape || !useInv) ? style.QCDText :
523 >                                         TString("QCD (" + invLabels[ninv] + ")")));
524 >        legend2->SetFillColor(0);
525 >        legend2->SetFillStyle(0);
526 >        legend2->Draw();
527 >        legend2->SetFillColor(kWhite);
528 >        //latex->DrawLatex(0.22,0.91,histoName[i].c_str());
529 >        //cvs[cvsName2]->SetLogy();
530 >        text1->Draw();
531 >        TString nameCanvas2 = desDir+histoName[i]+"_dataKS_"+suffix;
532 >        cvs[cvsName2]->SaveAs(nameCanvas2+".pdf");
533 >        cvs[cvsName2]->SaveAs(nameCanvas2+".png");
534 >
535 >      }
536 >
537 >      // KS fit results
538 >      TString cvsName3 = "KStestValues";
539 >      if(useInv) cvsName3 = cvsName3 + "_" + invName;
540 >      cvs[cvsName3] = new TCanvas(cvsName3,"",600,700);
541 >      // Zoom-in
542 >      Int_t nbins_ = hKSvalues_[0]->GetNbinsX();;
543 >      Int_t nbMax_ = 1;
544 >      Int_t nbMin_ = nbins_;
545 >      double val_Min_ = 1.;
546 >      for (int i = 0; i < 3; ++i) {
547 >        Int_t tmp = findFirstBinAbove(yLowBound, hKSvalues_[i]);
548 >        val_Min_ = (hKSvalues_[i]->GetBinContent(tmp) < 0.8*val_Min_ ?
549 >                    hKSvalues_[i]->GetBinContent(tmp) : 0.8*val_Min_);
550 >        nbMin_ = (nbMin_ < tmp ? nbMin_ : tmp);
551 >        tmp = findLastBinAbove(0., hKSvalues_[i]);
552 >        nbMax_ = (nbMax_ > tmp ? nbMax_ : tmp);
553 >      }
554 >      nbMin_ -= (0.1/stepsize);
555 >      nbMax_ += (0.1/stepsize);
556 >
557 >      hKSvalues_[0]->GetXaxis()->SetRange(nbMin_,nbMax_);
558 >      hKSvalues_[0]->GetYaxis()->SetRangeUser((1e-36 < val_Min_ ? val_Min_ : 1e-36),1.1);    
559 >      hKSvalues_[0]->Draw();
560 >      hKSvalues_[1]->Draw("same");
561 >      hKSvalues_[2]->Draw("same");
562 >      TLegend *legend3 = new TLegend(0.2, 0.75, 0.38, 0.93);
563 >      legend3->AddEntry(hKSvalues_[0], "muon_pT");
564 >      legend3->AddEntry(hKSvalues_[1], "MET");
565 >      legend3->AddEntry(hKSvalues_[2], "W_mT");
566 >      legend3->SetFillColor(0);
567 >      legend3->SetFillStyle(0);
568 >      legend3->Draw();
569 >      legend3->SetFillColor(kWhite);
570 >      //latex->DrawLatex(0.22,0.91,"KS test values");
571 >      cvs[cvsName3]->SetLogy();
572 >      TString nameCanvas3 = desDir+"KStestValues_newQCD_"+suffix;
573 >      cvs[cvsName3]->SaveAs(nameCanvas3+".pdf");
574 >      cvs[cvsName3]->SaveAs(nameCanvas3+".png");
575  
576 +    } //End of nsel loop
577 +  } // End of ninv loop
578   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines