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.6 by jengbou, Tue Sep 7 05:18:46 2010 UTC vs.
Revision 1.9 by jengbou, Thu Sep 9 08:03:25 2010 UTC

# Line 20 | Line 20
20   using namespace std;
21  
22   //define the constants: 2.88/pb
23 < const double weight_[3]    = {0.0524313, //QCD
24 <                              0.0091395, //WJets
25 <                              0.000306   //TTbar
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/";
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  = false;
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 < TString suffix = "Sel4"; // Suffix of selection
44 < TString invNames[2] = {"RelIsogt0p1","D0gt0p02"};
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
# Line 47 | Line 56 | void ikstest() {
56    TLatex *latex = new TLatex();
57    latex->SetNDC();
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    int size_ninv = (useInv ? 2 : 1);
69 <  for (int ninv = 0;ninv < size_ninv; ++ninv) {
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/";
# Line 60 | Line 79 | void ikstest() {
79      }
80      struct stat stDir;
81      if (!stat(desDir,&stDir)){
82 <      cout << "Output folder exists! Continues? (enter to continue; 'q' for quit)" << endl;
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;
# Line 71 | Line 90 | void ikstest() {
90          return;
91        }
92      }
93 <
94 <    ofstream outprint(TString(desDir+"Results_"+suffix+".txt"));
95 <    //open the files with histograms
96 <    map<string,TFile*> mfile;
97 <    mfile["Data"] = TFile::Open(TString("skimmed_Data_2.88pb-1/Data_"+suffix+".root"));
98 <    // n-1 cuts
99 <    if (useInv) {
100 <      if (realData)
101 <        mfile["InvSel"] = TFile::Open(TString("skimmed_Data_2.88pb-1/Data_"+suffix+"_"+invName+".root"));
102 <      else
103 <        mfile["InvSel"] = TFile::Open(TString("skimmed_MC/v5/QCD_"+suffix+"_"+invName+".root"));
104 <    }
105 <    // RefSel MC
106 <    mfile["0"] = TFile::Open(TString("skimmed_MC/v5/QCD_"+suffix+".root"));
107 <    mfile["1"] = TFile::Open(TString("skimmed_MC/v5/WJets_"+suffix+".root"));
89 <    mfile["2"] = TFile::Open(TString("skimmed_MC/v5/TTbar_"+suffix+".root"));
90 <
91 <    //define histograms and related parameters
92 <    string histoName[3] = {"h_mu_pt_calo","h_met_calo","h_mt_calo"};
93 <    string histoLabelX[3] = {"p_{T}^{#mu} [GeV/c]", "#slash{E}_{T} [GeV/c]", "M_{T}^{W} [GeV/c^{2}]"};
94 <    Int_t xbins[3] = {20,20,40};
95 <    Double_t xlow[3] = {0.,0.,0.};
96 <    Double_t xhigh[3] = {100.,100.,200.};
97 <    string sample[3] = {"QCD","Wjets","ttjets"};
98 <
99 <    TH1F* h_[9];
100 <    TH1F* mixh_[3];
101 <    TH1F* hQCD_NEW[3];
102 <    TH1F* hKSres_[3];
103 <    TH1F* hKSvalues_[3];
104 <    TH1F* hQCD_KS[3];
105 <    TH1F* hWJets_KS[3];
106 <
107 <    //load the histograms from the root files
108 <    for (int i = 0; i < 3; i++) {// 3 variables
109 <      //cout << "file[" << i << "] : " << endl;
110 <      string nameNewHisto = "mix_"+histoName[i];
111 <      string nameNewHistoSFKS = "finalSF_"+histoName[i];
112 <      string nameNewHistoKSvalues = "KSvalues_"+histoLabelX[i];
113 <
114 <      mixh_[i] = new TH1F(nameNewHisto.c_str(),"",xbins[i],xlow[i],xhigh[i]);
115 <      hKSres_[i] = new TH1F(nameNewHistoSFKS.c_str(),"",xbins[i],xlow[i],xhigh[i]);
116 <      hKSvalues_[i] = new TH1F(nameNewHistoKSvalues.c_str(),"",2./stepsize, stepsize, 2.+stepsize);
117 <
118 <      if (!useInv) {//use QCD MC sample
119 <        hQCD_NEW[i] = (TH1F*) mfile["0"]->Get(TString(histoName[i]))->Clone();
120 <        hQCD_NEW[i] -> Scale(weight_[0]);
121 <        hQCD_NEW[i] -> SetName((histoName[i]).c_str());
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 <      else {
110 <        hQCD_NEW[i] = (TH1F*) mfile["InvSel"]->Get(TString(histoName[i]));
111 <        if (!realData) hQCD_NEW[i] -> Scale(weight_[0]);
112 <        hQCD_NEW[i] -> SetName((histoName[i]).c_str());
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 <      mixh_[i] -> Sumw2();
164 <      hKSres_[i] -> Sumw2();
165 <      hKSvalues_[i] -> Sumw2();
166 <    }
167 <
168 <    for (int n = 0; n < 3; ++n) {// 3 MC samples
169 <      for (int ihisto = 0; ihisto < 3; ihisto++) {// 3 variables
170 <        //cout << "Variable[" << ihisto << "]" << endl;
171 <        string histo_name = histoName[ihisto]+sample[n];
172 <        ostringstream ss;
173 <        ss << n;
174 <        h_[n*3+ihisto] = (TH1F*) mfile[ss.str()]->Get(TString(histoName[ihisto]))->Clone();
175 <        h_[n*3+ihisto] -> Scale(weight_[n]);
176 <        h_[n*3+ihisto] -> SetName(histo_name.c_str());
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        }
144    }
181  
182 <    //create the mixed samples = "data"
183 <    TString cvsName0 = "Data";
184 <    if (useInv) {
185 <      cvsName0 += "_";
186 <      cvsName0 += invName;
151 <    }
152 <    cvs[cvsName0] = new TCanvas(cvsName0,"Data distributions",600,700);
153 <    cvs[cvsName0]->Divide(3,1);
154 <    for (int i = 0; i < 3; i++) {
155 <      cvs[cvsName0]->cd(i+1);
156 <      if (!realData) {
157 <        mixh_[i] -> Add(h_[i],h_[i+3], procQCD,procWjets);
158 <        //mixh_[i] -> Add(mixh_[i],h_[i+6], 1,procttjets);
159 <        //cout << "histo_name: " << mixh_[0]->GetNbinsX() << endl;
182 >      //create the mixed samples = "data"
183 >      TString cvsName0 = "Data";
184 >      if (useInv) {
185 >        cvsName0 += "_";
186 >        cvsName0 += invName;
187        }
188 <      else {
189 <        TH1F *htmp = (TH1F*) mfile["Data"]->Get(TString(histoName[i]));
190 <        mixh_[i] -> Add(htmp,1.);
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 <      mixh_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
209 <      mixh_[i]->GetYaxis()->SetTitle("Entries");
210 <      mixh_[i]->DrawClone();
211 <    }
212 <    cvs[cvsName0]->SaveAs(TString(desDir+"Data_distributions.pdf"));
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  
171    //define the weight corrections for each sample
172    double NevData                = mixh_[2]->Integral();
173    double corr_NevQCD            = h_[2]->Integral();
174    double corr_NevQCD_NEW        = hQCD_NEW[2]->Integral();
175    double corr_NevWjets          = h_[5]->Integral();
176    double corr_Nevttjets         = h_[8]->Integral();
177    double corr_Nevmix            = procQCD*corr_NevQCD+procWjets*corr_NevWjets;
178    //double corr_Nevmix            = procQCD*corr_NevQCD+procWjets*corr_NevWjets+procttjets*corr_Nevttjets;
179    if (!realData)
180      outprint << "Events mix sample    = " << corr_Nevmix << endl;
181    else
234        outprint << "Events in Data       = " << NevData << endl;
235 <    outprint << "Events QCD sample    = " << corr_NevQCD << endl;
236 <    outprint << "Events Wjets sample  = " << corr_NevWjets << endl;
185 <    outprint << "Events InvSel sample = " << corr_NevQCD_NEW << endl;
186 <
187 <    //define the containers for chosen numbers (coressponding to the max KStest result)
188 <    testMC maxProb[3];
189 <
190 <    //define the scale factors calculated using information obtained from all parameters
191 <    Double_t SFbackg = 0.0;
192 <    Double_t sumSFbackg = 0.0;
193 <    Double_t SFsample = 0.0;
194 <    Double_t sumSFsample = 0.0;
195 <    Double_t allKS = 0.0;
196 <
197 <    //do the KS test by varying the scale factors
198 <    for (int i = 0; i < 3; i++) { // 3 variables
199 <      TH1F *data = (TH1F*)mixh_[i]->Clone();
200 <      data -> SetName("dataClone");
201 <      //data -> Scale(1./data->Integral());
202 <      vector<testMC> resultsKS = doKStest((realData ? NevData : corr_Nevmix),
203 <                                          (useInv ? corr_NevQCD_NEW : corr_NevQCD),
204 <                                          corr_NevWjets,
205 <                                          data, hQCD_NEW[i], h_[i+3]);
206 <      testMC tksmax = getMax(resultsKS);
207 <      maxProb[i] = tksmax;
208 <      outprint << "\nFor the plot " << histoLabelX[i] << " the results are:"<< endl;
209 <      outprint << "\tmax Probability = " << maxProb[i].prob << endl;
210 <      outprint << "\tproc_background = " << maxProb[i].scaleF_backg << endl;
211 <      outprint << "\tproc_sample     = " << maxProb[i].scaleF_sample << endl;
212 <
213 <      outprint << "\n\tpercent_B of Data = "
214 <               << maxProb[i].scaleF_backg*corr_NevQCD_NEW*100/(realData ? NevData : corr_Nevmix) << endl;
215 <      outprint << "\tpercent_S of Data = "
216 <               << maxProb[i].scaleF_sample*corr_NevWjets*100/(realData ? NevData : corr_Nevmix) << 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  
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);
535        }
230      delete data;
231    }
536  
537 <    //calculate the final scale factors
538 <    SFbackg = sumSFbackg/allKS;
539 <    SFsample = sumSFsample/allKS;
540 <    outprint << "allKS = " << allKS << "\tbackground = " << SFbackg << "\tsample = " << SFsample << endl;
541 <    outprint << "==> Scale Factor for QCD MC = " << SFbackg*corr_NevQCD_NEW/corr_NevQCD << endl;
542 <    outprint << "\tcombined percent_B of Data = "
543 <             << SFbackg*corr_NevQCD_NEW*100/(realData ? NevData : corr_Nevmix) << endl;
544 <    outprint << "\tcombined percent_S of Data = "
545 <             << SFsample*corr_NevWjets*100/(realData ? NevData : corr_Nevmix) << endl;
546 <    outprint << "\n" << endl;
547 <    outprint << "=================================" << endl;
548 <    outprint << "\n" << endl;
549 <
550 <
551 <    //=================================
552 <    // Plots
553 <    //=================================
554 <    for (int i = 0; i < 3; i++) {// 3 variables
555 <      hKSres_[i] -> Add(hQCD_NEW[i],h_[i+3],SFbackg,SFsample);
252 <      outprint << "hKSres->Integral() = " << hKSres_[i]->Integral() << endl;
253 <      outprint << "Data->Integral()   = " << mixh_[i]->Integral() << endl;
254 <
255 <      mixh_[i]->Rebin(2);
256 <      hQCD_NEW[i]->Rebin(2);
257 <      h_[i]->Rebin(2);
258 <      h_[i+3]->Rebin(2);
259 <      hKSres_[i]->Rebin(2);
260 <      //hKSvalues_[i]->Rebin(2);
261 <
262 <      // Stack Wjets and QCD after scaled by KS factors
263 <      hQCD_KS[i] = (TH1F*) hQCD_NEW[i]->Clone();
264 <      hQCD_KS[i]->Scale(SFbackg);
265 <      hQCD_KS[i]->SetLineColor(style.QCDColor);
266 <      hQCD_KS[i]->SetFillColor(style.QCDColor);
267 <      hQCD_KS[i]->SetFillStyle(style.QCDFill);
268 <
269 <      hWJets_KS[i] = (TH1F*) h_[i+3]->Clone();
270 <      hWJets_KS[i]->Scale(SFsample);
271 <      hWJets_KS[i]->SetLineColor(style.WJetsColor);
272 <      hWJets_KS[i]->SetFillColor(style.WJetsColor);
273 <      hWJets_KS[i]->SetFillStyle(style.WJetsFill);
274 <
275 <      THStack *hst = new THStack(invName,invName);
276 <      hst->Add(hQCD_KS[i]);
277 <      hst->Add(hWJets_KS[i]);
278 <
279 <      // Set plotting parameters
280 <      mixh_[i]     ->SetLineColor(1);
281 <      hQCD_NEW[i]  ->SetLineColor(2);
282 <      h_[i]        ->SetLineColor(4);
283 <      h_[i+3]      ->SetLineColor(3);
284 <      hKSres_[i]   ->SetLineColor(2);
285 <      hKSvalues_[i]->SetLineColor(i+1);
286 <
287 <      mixh_[i]     ->SetMarkerColor(1);
288 <      hQCD_NEW[i]  ->SetMarkerColor(2);
289 <      h_[i]        ->SetMarkerColor(4);
290 <      h_[i+3]      ->SetMarkerColor(3);
291 <      hKSres_[i]   ->SetMarkerColor(2);
292 <      hKSvalues_[i]->SetMarkerColor(i+1);
293 <
294 <      mixh_[i]     ->SetMarkerStyle(24);
295 <      hQCD_NEW[i]  ->SetMarkerStyle(20);
296 <      h_[i]        ->SetMarkerStyle(20);
297 <      h_[i+3]      ->SetMarkerStyle(20);
298 <      hKSres_[i]   ->SetMarkerStyle(20);
299 <      hKSvalues_[i]->SetMarkerStyle(20);
300 <
301 <      mixh_[i]     ->SetMarkerSize(1.4);
302 <      hQCD_NEW[i]  ->SetMarkerSize(1.1);
303 <      h_[i]        ->SetMarkerSize(1.1);
304 <      h_[i+3]      ->SetMarkerSize(1.1);
305 <      hKSres_[i]   ->SetMarkerSize(0.9);
306 <      hKSvalues_[i]->SetMarkerSize(1.1);
307 <
308 <      mixh_[i]     ->SetStats(0);
309 <      hQCD_NEW[i]  ->SetStats(0);
310 <      h_[i]        ->SetStats(0);
311 <      h_[i+3]      ->SetStats(0);
312 <      hKSres_[i]   ->SetStats(0);
313 <      hKSvalues_[i]->SetStats(0);
314 <      hQCD_KS[i]   ->SetStats(0);
315 <      hWJets_KS[i] ->SetStats(0);
316 <
317 <      hKSres_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
318 <      hKSres_[i]->GetYaxis()->SetTitle("Entries");
319 <      hKSvalues_[i]->GetXaxis()->SetTitle("iteration #");
320 <      hKSvalues_[i]->GetYaxis()->SetTitle("KS test values");
321 <      h_[i]->GetXaxis()->SetTitle(histoLabelX[i].c_str());
322 <      h_[i]->GetYaxis()->SetTitle("A.U.");
323 <
324 <      TString nameCanvas1 = desDir+histoName[i]+"_QCD_"+suffix+".pdf";
325 <      TString cvsName1 = histoName[i]+"_QCD";
326 <      if(useInv) cvsName1 = cvsName1 + "_" + invName;
327 <      cvs[cvsName1] = new TCanvas(cvsName1,"",600,700);
328 <      hQCD_NEW[i] -> Scale(1./hQCD_NEW[i]->Integral());
329 <      h_[i] -> Scale(1./h_[i]->Integral());
330 <      h_[i+3] -> Scale(1./h_[i+3]->Integral());
331 <      outprint << "For " << histoName[i] << " , the KStest result btw MC_QCD/InvSel is = "
332 <               << h_[i] -> KolmogorovTest(hQCD_NEW[i],"") << endl;
333 <      h_[i]->Draw("P");
334 <      if (useInv)
335 <        hQCD_NEW[i]->Draw("sameP");
336 <      h_[i+3]->Draw("sameP");
337 <      TLegend *legend1 = new TLegend(0.7, 0.70, 0.9, 0.85);
338 <      legend1->AddEntry(h_[i], "QCD");
339 <      if (useInv)
340 <        legend1->AddEntry(hQCD_NEW[i], "QCD - InvSel");
341 <      legend1->AddEntry(h_[i+3], "W+jets");
342 <      legend1->Draw();
343 <      legend1->SetFillColor(kWhite);
344 <      //latex->DrawLatex(0.22,0.91,histoName[i].c_str());
345 <      //cvs[cvsName1]->SetLogy();
346 <      cvs[cvsName1]->SaveAs(nameCanvas1);
347 <
348 <      TString nameCanvas2 = desDir+histoName[i]+"_dataKS_"+suffix+".pdf";
349 <      TString cvsName2 = histoName[i]+"_dataKS";
350 <      if(useInv) cvsName2 = cvsName2 + "_" + invName;
351 <      cvs[cvsName2] = new TCanvas(cvsName2,"",600,700);
352 <      hst->Draw("hist");
353 <      hKSres_[i]->Draw("sameP");
354 <      mixh_[i]->Draw("sameP");
355 <
356 <      TLegend *legend2 = new TLegend(0.7, 0.70, 0.9, 0.85);
357 <      legend2->AddEntry(mixh_[i], "Data");
358 <      legend2->AddEntry(hKSres_[i], "KS result");
359 <      legend2->AddEntry(hWJets_KS[i], style.WJetsText);
360 <      legend2->AddEntry(hQCD_KS[i], style.QCDText);
361 <      legend2->Draw();
362 <      legend2->SetFillColor(kWhite);
363 <      //latex->DrawLatex(0.22,0.91,histoName[i].c_str());
364 <      //cvs[cvsName2]->SetLogy();
365 <      cvs[cvsName2]->SaveAs(nameCanvas2);
366 <    }
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 <    TString cvsName3 = "KStestValues";
558 <    if(useInv) cvsName3 = cvsName3 + "_" + invName;
559 <    cvs[cvsName3] = new TCanvas(cvsName3,"",600,700);
560 <    //hKSvalues_[0]->GetXaxis()->SetRangeUser(0.9,1.2);
561 <    hKSvalues_[0]->GetYaxis()->SetRangeUser(1e-36,1.2);
562 <    hKSvalues_[0]->Draw();
563 <    hKSvalues_[1]->Draw("same");
564 <    hKSvalues_[2]->Draw("same");
565 <    TLegend *legend3 = new TLegend(0.7, 0.70, 0.9, 0.85);
566 <    legend3->AddEntry(hKSvalues_[0], "muon_pT");
567 <    legend3->AddEntry(hKSvalues_[1], "MET");
568 <    legend3->AddEntry(hKSvalues_[2], "W_mT");
569 <    legend3->Draw();
570 <    legend3->SetFillColor(kWhite);
571 <    //latex->DrawLatex(0.22,0.91,"KS test values");
572 <    cvs[cvsName3]->SetLogy();
573 <    TString nameCanvas3 = desDir+"KStestValues_newQCD"+suffix+".pdf";
574 <    cvs[cvsName3]->SaveAs(nameCanvas3);
575 <  }
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