ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
plot_kappa_mismeas.cxx
Go to the documentation of this file.
1 
3 #include <fstream>
4 #include <iostream>
5 #include <vector>
6 #include <ctime>
7 #include <iomanip> // setw
8 
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <getopt.h>
12 
13 #include "TError.h" // Controls error level reporting
14 #include "TCanvas.h"
15 #include "TH1D.h"
16 #include "TLine.h"
17 #include "TLatex.h"
18 #include "TLegend.h"
19 #include "TGraphAsymmErrors.h"
20 #include "RooStats/NumberCountingUtils.h"
21 
22 #include "core/utilities.hpp"
23 #include "core/baby.hpp"
24 #include "core/process.hpp"
25 #include "core/named_func.hpp"
26 #include "core/plot_maker.hpp"
27 #include "core/plot_opt.hpp"
28 #include "core/palette.hpp"
29 #include "core/table.hpp"
30 #include "core/abcd_method.hpp"
31 #include "core/styles.hpp"
32 
33 using namespace std;
34 
35 namespace{
36  bool split_bkg = false;
37  bool only_dilepton = false;
38  bool do_leptons = false;
39  bool do_signal = false;
40  bool full_lumi = true;
41  bool unblind = false;
42  bool debug = false;
43  TString skim = "standard";
44  TString only_method = "";
45  float lumi;
46  int Nscen = 6;
47 }
48 
49 void plotKappa(abcd_method &abcd, vector<vector<vector<vector<float> > > > &allkappas);
50 void changeMMCut(TString &cut, vector<TString> &mm_cuts, TString method, TString index_s);
51 
52 TString printTable(abcd_method &abcd, vector<vector<GammaParams> > &allyields,
53  vector<vector<vector<float> > > &kappas, vector<vector<vector<float> > > &preds);
54 void findPreds(abcd_method &abcd, vector<vector<GammaParams> > &allyields,
55  vector<vector<vector<float> > > &kappas, vector<vector<vector<float> > > &preds);
56 void printDebug(abcd_method &abcd, vector<vector<GammaParams> > &allyields, TString baseline,
57  vector<vector<vector<float> > > &kappas, vector<vector<vector<float> > > &preds);
58 TString Zbi(double Nobs, double Nbkg, double Ebkg);
59 
60 void GetOptions(int argc, char *argv[]);
61 
62 int main(int argc, char *argv[]){
63  gErrorIgnoreLevel=6000; // Turns off ROOT errors due to missing branches
64  GetOptions(argc, argv);
65 
66  time_t begtime, endtime;
67  time(&begtime);
68 
71  string bfolder("");
72  string hostname = execute("echo $HOSTNAME");
73  if(Contains(hostname, "cms") || Contains(hostname, "compute-"))
74  bfolder = "/net/cms2"; // In laptops, you can't create a /net folder
75 
76  string foldermc(bfolder+"/cms2r0/babymaker/babies/mismeasured/2016_06_14/mc/merged_mm_std_nj5mj250/");
77  foldermc = "/net/cms29/cms29r0/babymaker/babies/mismeasured_v2/2016_06_14/mc/merged_mm_std_nj5mj250/";
78  string folderdata(bfolder+"/cms2r0/babymaker/babies/2016_06_26/data/merged_standard/");
79  folderdata = foldermc;
80  if(skim.Contains("met150")){
81  foldermc = bfolder+"/cms2r0/babymaker/babies/2016_06_14/mc/merged_met150/";
82  folderdata = bfolder+"/cms2r0/babymaker/babies/2016_06_21/data/skim_1lmet150/";
83  }
84  if(skim.Contains("2015")){
85  foldermc = bfolder+"/cms2r0/babymaker/babies/2016_04_29/mc/merged_1lht500met200/";
86  folderdata = bfolder+"/cms2r0/babymaker/babies/2016_04_29/data/merged_1lht500met200/";
87  }
88 
89  Palette colors("txt/colors.txt", "default");
90 
91  // Cuts in baseline speed up the yield finding
92  //string baseline = "mj14>250 && nleps>=1 && ht>500 && met>150 && pass && njets>=5";
93  string baseline = "1";
94  if(skim.Contains("2015")) {
95  lumi = 2.3;
96  }else if(!full_lumi) {
97  baseline += " && nonblind";
98  lumi = 0.815;
99  } else lumi = 2.6;
100  if(skim.Contains("mj12")) ReplaceAll(baseline, "mj14","mj");
101 
102  lumi = 100; // to easily see digits
103  auto proc_bkg = Process::MakeShared<Baby_full>("All_bkg", Process::Type::background, colors("tt_1l"),
104  {foldermc+"*_TTJets*Lept*.root", foldermc+"*_TTJets_HT*.root",
105  foldermc+"*_WJetsToLNu*.root",foldermc+"*_ST_*.root",
106  foldermc+"*_TTW*.root",foldermc+"*_TTZ*.root",
107  foldermc+"*DYJetsToLL*.root",foldermc+"*QCD_HT*.root",
108  foldermc+"*_ZJet*.root",foldermc+"*_ttHJetTobb*.root",
109  foldermc+"*_TTGJets*.root",foldermc+"*_TTTT*.root",
110  foldermc+"*_WH_HToBB*.root",foldermc+"*_ZH_HToBB*.root",
111  foldermc+"*_WWTo*.root",foldermc+"*_WZ*.root",foldermc+"*_ZZ_*.root"},
112  //{foldermc+"*TTJets_T*.root"},
113  baseline+" && stitch");
114 
115  auto proc_t1c = Process::MakeShared<Baby_full>("T1tttt(C)", Process::Type::signal, colors("t1tttt"),
116  {foldermc+"*mGluino-1200_mLSP-800_*root"},
117  baseline+" && stitch");
118  auto proc_t1nc = Process::MakeShared<Baby_full>("T1tttt(NC)", Process::Type::signal, colors("t1tttt"),
119  {foldermc+"*mGluino-1500_mLSP-100_*root"},
120  baseline+" && stitch");
121 
122  auto proc_tt1l = Process::MakeShared<Baby_full>("tt 1lep", Process::Type::background, colors("tt_1l"),
123  {foldermc+"*_TTJets*SingleLept*.root", foldermc+"*_TTJets_HT*.root"},
124  baseline+" && stitch && ntruleps==1");
125  auto proc_tt2l = Process::MakeShared<Baby_full>("tt 2lep", Process::Type::background, colors("tt_2l"),
126  {foldermc+"*_TTJets*DiLept*.root", foldermc+"*_TTJets_HT*.root"},
127  baseline+" && stitch && ntruleps==2");
128 
129  // auto proc_tt1l = Process::MakeShared<Baby_full>("tt 1lep", Process::Type::background, colors("tt_1l"),
130  // {foldermc+"*_TTJets*SingleLept*.root"},
131  // baseline+" && ntruleps==1");
132  // auto proc_tt2l = Process::MakeShared<Baby_full>("tt 2lep", Process::Type::background, colors("tt_2l"),
133  // {foldermc+"*_TTJets*DiLept*.root"},
134  // baseline+" && ntruleps==2");
135 
136  auto proc_other = Process::MakeShared<Baby_full>("Other", Process::Type::background, colors("other"),
137  {foldermc+"*_WJetsToLNu*.root",foldermc+"*_ST_*.root",
138  foldermc+"*_TTW*.root",foldermc+"*_TTZ*.root",
139  foldermc+"*DYJetsToLL*.root",foldermc+"*QCD_HT*.root",
140  foldermc+"*_ZJet*.root",foldermc+"*_ttHJetTobb*.root",
141  foldermc+"*_TTGJets*.root",foldermc+"*_TTTT*.root",
142  foldermc+"*_WH_HToBB*.root",foldermc+"*_ZH_HToBB*.root",
143  foldermc+"*_WWTo*.root",foldermc+"*_WZ*.root",foldermc+"*_ZZ_*.root"},
144  baseline+" && stitch");
145 
146  string trigs = "(trig[4]||trig[8]||trig[28]||trig[14])";
147  if(skim.Contains("2016")) trigs = "(trig[4]||trig[8]||trig[13]||trig[33])";
148 
149  auto proc_data = Process::MakeShared<Baby_full>("Data", Process::Type::data, kBlack,
150  {folderdata+"*.root"},baseline+" && "+trigs);
151 
152  DBG(lumi << " " << skim << " " << trigs);
153 
154  //vector<shared_ptr<Process> > all_procs = {proc_tt1l, proc_tt2l, proc_other};
155  vector<shared_ptr<Process> > all_procs = {proc_bkg};
156 
159  // baseline defined above
160 
162  TString c_vlowmet = "met>150 && met<=200";
163  TString c_lowmet = "met>200 && met<=350";
164  TString c_midmet = "met>350 && met<=500";
165  TString c_higmet = "met>500";
166 
168  TString c_lownb = "nbm==1";
169  TString c_midnb = "nbm==2";
170  TString c_hignb = "nbm>=3";
171 
173  TString c_vlownj = "njets>=4 && njets<=5";
174  TString c_lownj = "njets>=6 && njets<=8";
175  TString c_hignj = "njets>=9";
176  TString c_nj5 = "njets==5";
177 
179  vector<TString> abcdcuts_std = {"mt<=140 && mj14<=400 && nj_all_1l",
180  "mt<=140 && mj14>400 && nj_1l",
181  "mt>140 && mj14<=400 && nj_all_1l",
182  "mt>140 && mj14>400 && nj_1l"};
183 
184  vector<TString> abcdcuts_veto = {"mt<=140 && mj14<=400 && nleps==1 && nbm>=1 && nj_all_1l",
185  "mt<=140 && mj14>400 && nleps==1 && nbm>=1 && nj_1l",
186  "mt>140 && mj14<=400 && nleps==1 && nbm>=1 && nbm<=2 && nj_all_1l",
187  "mt>140 && mj14>400 && nleps==1 && nbm>=1 && nbm<=2 && nj_1l"};
188 
189  vector<TString> abcdcuts_2l = {"mt<=140 && mj14<=400 && nleps==1 && nbm>=1 && nj_all_1l",
190  "mt<=140 && mj14>400 && nleps==1 && nbm>=1 && nj_1l",
191  " mj14<=400 && nleps==2 && nbm<=2 && nj_all_2l",
192  " mj14>400 && nleps==2 && nbm<=2 && nj_2l"};
193 
194  vector<TString> abcdcuts_2lveto;
195  for(size_t ind=0; ind<2; ind++) abcdcuts_2lveto.push_back(abcdcuts_2l[ind]);
196  for(size_t ind=2; ind<abcdcuts_2l.size(); ind++){
197  abcdcuts_2lveto.push_back("(("+abcdcuts_2l[ind]+") || ("+abcdcuts_veto[ind]+"))");
198  abcdcuts_2lveto.back().ReplaceAll("(( ","((");
199  }
200 
203  vector<abcd_method> abcds;
204  vector<TString> abcdcuts, metcuts, bincuts;
205  PlotMaker pm;
206 
208  vector<TString> methods_std = {"m2lveto", "m2lonly", "mvetoonly", "signal", "m5j",
209  "agg_himet", "agg_mixed", "agg_himult", "agg_1b"};
210  vector<TString> methods_met150 = {"m2lvetomet150", "m2lonlymet150", "mvetoonlymet150", "m1lmet150"};
211  vector<TString> methods = methods_std;
212  if(skim.Contains("met150")) methods = methods_met150;
213  if(only_method!="") methods = vector<TString>({only_method});
214  if(do_leptons){
215  for(auto name: methods){
216  name += "_el";
217  methods.push_back(name);
218  name.ReplaceAll("_el", "_mu");
219  methods.push_back(name);
220  if(name.Contains("2l")){
221  name.ReplaceAll("_mu", "_emu");
222  methods.push_back(name);
223  }
224  }
225  }
226 
227  vector<TString> methods_ori = methods;
228  methods.clear();
229  for(auto name2: methods_ori){
230  for(int iscen=0; iscen < Nscen; iscen++){
231  if(iscen != 0 && iscen != 2 && iscen != 5) continue;
232  //if(iscen!=0 && iscen!=3) continue; // To just do the most extreme ones
233  TString name = name2;
234  name += "_mm"; name += iscen;
235  name += "_lep";
236  methods.push_back(name);
237  name.ReplaceAll("_lep", "_nolep");
238  methods.push_back(name);
239  } // Loop over mismeasurement scenarios
240  } // Loop over methods
241 
244  for(size_t iabcd=0; iabcd<methods.size(); iabcd++) {
245  TString method = methods[iabcd];
246  TString basecuts = "1", caption = "";
247 
249  if(method.Contains("2l") || method.Contains("veto")) {
250  metcuts = vector<TString>{c_lowmet, c_midmet};
251  bincuts = vector<TString>{c_lownj, c_hignj}; // 2l nj cuts automatically lowered in abcd_method
252  caption = "Dilepton validation regions (with filters). D3 and D4 have ";
253  } else {
254  if(only_dilepton) continue;
255  abcdcuts = abcdcuts_std;
256  basecuts = "nleps==1 && nbm>=1";
257  }
258 
260  if(method.Contains("2lonly")) {
261  abcdcuts = abcdcuts_2l;
262  caption += "two reconstructed leptons";
263  }
264  if(method.Contains("2lveto")) {
265  abcdcuts = abcdcuts_2lveto;
266  caption += "either two reconstructed leptons, or one lepton and one track";
267  }
268  if(method.Contains("vetoonly")) {
269  abcdcuts = abcdcuts_veto;
270  caption += "one lepton and one track";
271  }
272 
274  if(method.Contains("signal")) {
275  metcuts = vector<TString>{c_lowmet, c_midmet, c_higmet};
276  bincuts = vector<TString>{c_lownb+" && "+c_lownj, c_lownb+" && "+c_hignj,
277  c_midnb+" && "+c_lownj, c_midnb+" && "+c_hignj,
278  c_hignb+" && "+c_lownj, c_hignb+" && "+c_hignj};
279  caption = "Signal search regions";
280  }
281  if(method.Contains("m5j")) {
282  metcuts = vector<TString>{c_lowmet, c_midmet};
283  bincuts = vector<TString>{c_lownb+" && "+c_nj5, c_midnb+" && "+c_nj5, c_hignb+" && "+c_nj5};
284  caption = "Validation regions with $1\\ell, \\njets=5$";
285  }
287  if(method.Contains("agg_himet")) {
288  metcuts = vector<TString>{"met>500"};
289  bincuts = vector<TString>{"nbm>=3&&njets>=6"};
290  caption = "High-\\met aggregate region with $1\\ell$, $\\met>500\\text{ GeV}$, $\\njets\\geq6$, $\\nb\\geq3$";
291  }
292  if(method.Contains("agg_mixed")) {
293  metcuts = vector<TString>{"met>350"};
294  bincuts = vector<TString>{"nbm>=2&&njets>=9"};
295  caption = "Mixed aggregate region with $1\\ell$, $\\met>350\\text{ GeV}$, $\\njets\\geq9$, $\\nb\\geq2$";
296  }
297  if(method.Contains("agg_himult")) {
298  metcuts = vector<TString>{"met>200"};
299  bincuts = vector<TString>{"nbm>=3&&njets>=9"};
300  caption = "High-multiplicity aggregate region with $1\\ell$, $\\met>200\\text{ GeV}$, $\\njets\\geq9$, $\\nb\\geq3$";
301  }
302  if(method.Contains("agg_1b")) {
303  metcuts = vector<TString>{"met>500"};
304  bincuts = vector<TString>{"nbm>=1&&njets>=6"};
305  caption = "Single b-tag aggregate region with $1\\ell$, $\\met>500\\text{ GeV}$, $\\njets\\geq6$, $\\nb\\geq1$";
306  }
307 
309  if(method.Contains("met150")) {
310  metcuts = vector<TString>{c_vlowmet};
311  caption.ReplaceAll("regions", "region for very low \\met");
312  }
313  if(method.Contains("m1lmet150")) {
314  bincuts = vector<TString>{c_lownb+" && "+c_lownj, c_lownb+" && "+c_hignj,
315  c_midnb+" && "+c_lownj, c_midnb+" && "+c_hignj};
316  caption = "Single lepton validation region for very low \\met";
317  }
318  if(skim.Contains("2015")) {
319  caption += ". Data taken in 2015";
320  }
321 
323  if(method.Contains("_mm")){
324  vector<TString> mm_cuts({"met", "nleps", "njets", "nbm", "ht", "mt", "mj14"}); // cuts to change
325  basecuts += "&& mj14>250 && nleps>=1 && ht>500 && met>150 && pass && njets>=5"; // Adding the baseline here
326 
328  TString index_s = method;
329  index_s.Remove(0, index_s.Index("_mm")+3);
330  index_s.Remove(index_s.First('_'), index_s.Length());
331 
333  changeMMCut(basecuts, mm_cuts, method, index_s);
334  for(auto &cut : metcuts) changeMMCut(cut, mm_cuts, method, index_s);
335  for(auto &cut : bincuts) changeMMCut(cut, mm_cuts, method, index_s);
336  for(auto &cut : abcdcuts) changeMMCut(cut, mm_cuts, method, index_s);
337  } // If method is mismesurement
338 
340  abcds.push_back(abcd_method(method, metcuts, bincuts, abcdcuts, caption, basecuts));
341  if(skim.Contains("mj12")) {
342  // abcds.back().setMj12();
343  abcds.back().caption += ". Using $M_J^{1.2}$";
344  }
345  if(method.Contains("_el") || method.Contains("_mu") || method.Contains("_emu")) abcds.back().setLeptons();
346  if(method.Contains("_el")) abcds.back().caption += ". Only electrons";
347  if(method.Contains("_mu")) abcds.back().caption += ". Only muons";
348  if(method.Contains("_emu")) abcds.back().caption += ". Only $e/\\mu$ pairs in D3 and D4";
349  //if(method.Contains("agg_")) abcds.back().int_nbnj = false; // Only changes caption since there is only 1 bin
350 
351  //cout<<endl<<" ======== Method "<<method<<" =============="<<endl;
352  //abcds.back().printCuts();
353 
354  vector<TableRow> table_cuts;
355  for(size_t icut=0; icut < abcds.back().allcuts.size(); icut++)
356  table_cuts.push_back(TableRow(abcds.back().allcuts[icut].Data(), abcds.back().allcuts[icut].Data()));
357 
358  TString tname = "preds"; tname += iabcd;
359  pm.Push<Table>(tname.Data(), table_cuts, all_procs, false);
360  } // Loop over ABCD methods
361 
364 
365  bool single_thread = false;
366  if(single_thread) pm.multithreaded_ = false;
367  pm.MakePlots(lumi);
368 
371  vector<TString> tablenames;
372  vector<vector<vector<vector<float> > > > allkappas;
373  for(size_t imethod=0; imethod<abcds.size(); imethod++) {
374  Table * yield_table = static_cast<Table*>(pm.Figures()[imethod].get());
375  // allyields: [0] data, [1] bkg, [2] T1tttt(NC), [3] T1tttt(C)
376  // if split_bkg [2/4] Other, [3/5] tt1l, [4/6] tt2l
377  vector<vector<GammaParams> > allyields;
378  allyields.push_back(yield_table->DataYield());
379  allyields.push_back(yield_table->BackgroundYield(lumi));
380  if(do_signal){
381  allyields.push_back(yield_table->Yield(proc_t1nc.get(), lumi));
382  allyields.push_back(yield_table->Yield(proc_t1c.get(), lumi));
383  }
384  if(split_bkg){
385  allyields.push_back(yield_table->Yield(proc_other.get(), lumi));
386  allyields.push_back(yield_table->Yield(proc_tt1l.get(), lumi));
387  allyields.push_back(yield_table->Yield(proc_tt2l.get(), lumi));
388  }
389 
391  vector<vector<vector<float> > > kappas, preds;
392  findPreds(abcds[imethod], allyields, kappas, preds);
393 
394  allkappas.push_back(kappas);
395  plotKappa(abcds[imethod], allkappas);
396 
397  // //// Makes table MC/Data yields, kappas, preds, Zbi
398  // TString fullname = printTable(abcds[imethod], allyields, kappas, preds);
399  // tablenames.push_back(fullname);
400 
402  if(debug) printDebug(abcds[imethod], allyields, baseline, kappas, preds);
403 
404  } // Loop over ABCD methods
405 
406  // //// Printing names of ouput files
407  // cout<<endl<<"===== Tables to be moved to the AN/PAS/paper:"<<endl;
408  // for(size_t ind=0; ind<tablenames.size(); ind++){
409  // TString name=tablenames[ind]; name.ReplaceAll("fulltable","table");
410  // cout<<" mv "<<name<<" ${tables_folder}"<<endl;
411  // }
412  // cout<<endl<<"===== Tables that can be compiled"<<endl;
413  // for(size_t ind=0; ind<tablenames.size(); ind++)
414  // cout<<" pdflatex "<<tablenames[ind]<<" > /dev/null"<<endl;
415  // cout<<endl;
416 
417  time(&endtime);
418  cout<<endl<<"Finding "<<abcds.size()<<" tables took "<<difftime(endtime, begtime)<<" seconds"<<endl<<endl;
419 } // main
420 
425 
427 void changeMMCut(TString &cut, vector<TString> &mm_cuts, TString method, TString index_s){
428  for(auto &mm_cut : mm_cuts) {
429  cut.ReplaceAll(mm_cut, "mm_"+mm_cut+"["+index_s+"]");
430  if(mm_cut=="mj14"){
431  if(method.Contains("_nolep")){
432  cut.ReplaceAll("mj14", "mj14_nolep");
433  cut.ReplaceAll("_2l", "_1l"); // This avoids lowering number of jets
434  } else cut.ReplaceAll("mj14", "mj14_lep");
435  }
436  } // Loop over mismeasured variables
437 }
438 
440 // allyields: [0] data, [1] bkg, [2] T1tttt(NC), [3] T1tttt(C)
441 // if split_bkg: [2/4] Other, [3/5] tt1l, [4/6] tt2l
442 TString printTable(abcd_method &abcd, vector<vector<GammaParams> > &allyields,
443  vector<vector<vector<float> > > &kappas, vector<vector<vector<float> > > &preds){
445  int digits = 2;
446  TString ump = " & ";
447  bool do_zbi = true;
448  if(!unblind) do_zbi = false;
449  size_t Ncol = 6;
450  if(do_zbi) Ncol++;
451  if(do_signal) Ncol += 2;
452  if(split_bkg) Ncol += 3;
453  TString blind_s = "$\\spadesuit$";
454 
456  int digits_lumi = 1;
457  if(!full_lumi && !skim.Contains("2015")) digits_lumi = 3;
458  TString lumi_s = RoundNumber(lumi, digits_lumi);
459  TString outname = "tables/table_pred_lumi"+lumi_s; outname.ReplaceAll(".","p");
460  if(skim.Contains("2015")) outname += "_2015";
461  if(skim.Contains("mj12")) outname += "_mj12";
462  if(unblind) outname += "_unblind";
463  else outname += "_blind";
464  outname += "_"+abcd.method+".tex";
465  ofstream out(outname);
466 
468  if(abcd.method.Contains("signal") && Ncol>7) out << "\\resizebox{\\textwidth}{!}{\n";
469  out << "\\begin{tabular}[tbp!]{ l ";
470  if(do_signal) out << "|cc";
471  if(split_bkg) out << "|ccc";
472  out << "|cc|ccc";
473  if(do_zbi) out<<"c";
474  out<<"}\\hline\\hline\n";
475  out<<"${\\cal L}="<<lumi_s<<"$ fb$^{-1}$ ";
476  if(do_signal) out << " & T1tttt(NC) & T1tttt(C) ";
477  if(split_bkg) out << " & Other & $t\\bar{t}(1\\ell)$ & $t\\bar{t}(2\\ell)$ ";
478  out<<"& $\\kappa$ & MC bkg. & Pred. & Obs. & Obs./MC "<<(do_zbi?"& $Z_{\\rm Bi}$":"")<<" \\\\ \\hline\\hline\n";
479 
482  for(size_t iplane=0; iplane < abcd.planecuts.size(); iplane++) {
483  out<<endl<< "\\multicolumn{"<<Ncol<<"}{c}{$"<<CodeToLatex(abcd.planecuts[iplane].Data())<<"$} \\\\ \\hline\n";
484  for(size_t iabcd=0; iabcd < abcd.abcdcuts.size(); iabcd++){
485  for(size_t ibin=0; ibin < abcd.bincuts[iplane].size(); ibin++){
486  size_t index = abcd.indexBin(iplane, ibin, iabcd);
487  if(abcd.int_nbnj && iabcd%2==0 && ibin>0) continue;
488  if(iabcd==3 && ibin==0) out << "\\hline" << endl;
490  out << (iabcd<=1?"R":abcd.rd_letter) << iabcd+1 << ": ";
491  if(iabcd%2==0 && abcd.int_nbnj)
492  out << "All "<<(abcd.bincuts[iplane][ibin].Contains("nbm")?"\\nb, ":"")<<"\\njets" ;
493  else {
494  if(abcd.method.Contains("2lonly") && iabcd>=2) out<<"$"<<CodeToLatex(abcd.lowerNjets(abcd.bincuts[iplane][ibin]).Data())<<"$";
495  else if(abcd.method.Contains("2lveto") && iabcd>=2){
496  if(abcd.bincuts[iplane][ibin].Contains("6")) out<<"Low \\njets";
497  else out<<"High \\njets";
498  } else out<<"$"<<CodeToLatex(abcd.bincuts[iplane][ibin].Data())<<"$";
499  }
501  if(do_signal)
502  out<<ump<<RoundNumber(allyields[2][index].Yield(), digits)<< ump <<RoundNumber(allyields[3][index].Yield(), digits);
504  if(split_bkg){
505  size_t offset = (do_signal?2:0);
506  out << ump <<RoundNumber(allyields[offset+2][index].Yield(), digits)
507  << ump <<RoundNumber(allyields[offset+3][index].Yield(), digits)
508  << ump <<RoundNumber(allyields[offset+4][index].Yield(), digits);
509  }
511  out<<ump;
512  if(iabcd==3) out << "$" << RoundNumber(kappas[iplane][ibin][0], digits)
513  << "^{+" << RoundNumber(kappas[iplane][ibin][1], digits)
514  << "}_{-" << RoundNumber(kappas[iplane][ibin][2], digits) <<"}$ ";
516  out << ump << RoundNumber(allyields[1][index].Yield(), digits)<< ump;
518  if(iabcd==3) out << "$" << RoundNumber(preds[iplane][ibin][0], digits)
519  << "^{+" << RoundNumber(preds[iplane][ibin][1], digits)
520  << "}_{-" << RoundNumber(preds[iplane][ibin][2], digits) <<"}$ ";
522  if(!unblind && iabcd==3) out << ump << blind_s<< ump << blind_s;
523  else {
524  out << ump << RoundNumber(allyields[0][index].Yield(), 0);
525  TString ratio_s = "-";
526  double Nobs = allyields[0][index].Yield(), Nmc = allyields[1][index].Yield();
527  double Eobs = sqrt(Nobs), Emc = allyields[1][index].Uncertainty();
528  if(Nobs==0) Eobs=1;
529  if(Emc>0){
530  double ratio = Nobs/Nmc;
531  double Eratio = sqrt(pow(Eobs/Nmc,2) + pow(Nobs*Emc/Nmc/Nmc,2));
532  ratio_s = "$"+RoundNumber(ratio, 2)+"\\pm"+RoundNumber(Eratio,2)+"$";
533  }
534  out << ump << ratio_s;
535  }
537  if(do_zbi && iabcd==3) out << ump << Zbi(allyields[0][index].Yield(), preds[iplane][ibin][0], preds[iplane][ibin][1]);
538  out << "\\\\ \n";
539  } // Loop over bin cuts
540  } // Loop over ABCD cuts
541  out << "\\hline\\hline\n";
542  } // Loop over plane cuts
544 
546  out<< "\\end{tabular}"<<endl;
547  if(abcd.method.Contains("signal") && Ncol>7) out << "}\n"; // For resizebox
548  out.close();
549 
551  TString fullname = outname; fullname.ReplaceAll("table_","fulltable_");
552  ofstream full(fullname);
553  ifstream header("txt/header.tex");
554  full<<header.rdbuf();
555  header.close();
556  if(!abcd.method.Contains("signal")) full << "\\usepackage[landscape]{geometry}\n\n";
557  full << "\\begin{document}\n\n";
558  full << "\\begin{table}\n\\centering\n";
559  full << "\\caption{" << abcd.caption <<".}\\vspace{0.1in}\n\\label{tab:"<<abcd.method<<"}\n";
560  ifstream outtab(outname);
561  full << outtab.rdbuf();
562  outtab.close();
563  full << "\\end{table}\n\n";
564  full << "\\end{document}\n";
565  full.close();
566 
567  return fullname;
568 } // printTable
569 
571 TString Zbi(double Nobs, double Nbkg, double Ebkg){
572  double Nsig = Nobs-Nbkg;
573  double zbi = RooStats::NumberCountingUtils::BinomialExpZ(Nsig, Nbkg, Ebkg/Nbkg);
574  if(Nbkg==0) zbi = RooStats::NumberCountingUtils::BinomialWithTauExpZ(Nsig, Nbkg, 1/Ebkg);
575  if(zbi<0) zbi=0;
576  TString zbi_s = RoundNumber(zbi,1);
577  if(zbi_s!="-") zbi_s = "$"+zbi_s+"\\sigma$";
578  if(Nsig<=0 || Ebkg<=0) zbi_s = "-";
579  //cout<<"Zbi for Nobs "<<Nobs<<", Nbkg "<<Nbkg<<", Ebkg "<<Ebkg<<" is "<<zbi_s<<endl;
580  return zbi_s;
581 }
582 
584 // allyields: [0] data, [1] bkg, [2] T1tttt(NC), [3] T1tttt(C)
585 void findPreds(abcd_method &abcd, vector<vector<GammaParams> > &allyields,
586  vector<vector<vector<float> > > &kappas, vector<vector<vector<float> > > &preds){
587  // Powers for kappa: ({R1, R2, D3, R4})
588  vector<float> pow_kappa({ 1, -1, -1, 1});
589  // Powers for TotBkg pred:({R1, R2, D3, R1, R2, D3, D4})
590  vector<float> pow_totpred( {-1, 1, 1, 1, -1, -1, 1});
591 
592  float val(1.), valup(1.), valdown(1.);
593 
594  for(size_t iplane=0; iplane < abcd.planecuts.size(); iplane++) {
595  kappas.push_back(vector<vector<float> >());
596  preds.push_back(vector<vector<float> >());
597  for(size_t ibin=0; ibin < abcd.bincuts[iplane].size(); ibin++){
598  vector<vector<float> > entries;
599  vector<vector<float> > weights;
601  for(size_t iabcd=0; iabcd < 3; iabcd++){
602  size_t index = abcd.indexBin(iplane, ibin, iabcd);
603  entries.push_back(vector<float>());
604  weights.push_back(vector<float>());
605  entries.back().push_back(allyields[0][index].Yield());
606  weights.back().push_back(1.);
607  } // Loop over ABCD cuts
608 
609  vector<vector<float> > kentries;
610  vector<vector<float> > kweights;
612  for(size_t iabcd=0; iabcd < 4; iabcd++){
613  size_t index = abcd.indexBin(iplane, ibin, iabcd);
614  // Yields for predictions
615  entries.push_back(vector<float>());
616  weights.push_back(vector<float>());
617  entries.back().push_back(allyields[1][index].NEffective());
618  weights.back().push_back(allyields[1][index].Weight());
619  // Yields for kappas
620  kentries.push_back(vector<float>());
621  kweights.push_back(vector<float>());
622  kentries.back().push_back(allyields[1][index].NEffective());
623  kweights.back().push_back(allyields[1][index].Weight());
624  } // Loop over ABCD cuts
625 
626  // Throwing toys to find predictions and uncertainties
627  val = calcKappa(entries, weights, pow_totpred, valdown, valup);
628  if(valdown<0) valdown = 0;
629  preds[iplane].push_back(vector<float>({val, valup, valdown}));
630  // Throwing toys to find kappas and uncertainties
631  val = calcKappa(kentries, kweights, pow_kappa, valdown, valup);
632  if(valdown<0) valdown = 0;
633  kappas[iplane].push_back(vector<float>({val, valup, valdown}));
634  } // Loop over bin cuts
635  } // Loop over plane cuts
636 
637 } // findPreds
638 
639 // allyields: [0] data, [1] bkg, [2] T1tttt(NC), [3] T1tttt(C)
640 void printDebug(abcd_method &abcd, vector<vector<GammaParams> > &allyields, TString baseline,
641  vector<vector<vector<float> > > &kappas, vector<vector<vector<float> > > &preds){
642 
643  cout<<endl<<endl<<"=================== Printing cuts for method "<<abcd.method<<" ==================="<<endl;
644  cout<<"-- Baseline cuts: "<<baseline<<endl;
645  for(size_t iplane=0; iplane < abcd.planecuts.size(); iplane++) {
646  cout<<endl<<" **** Plane "<<abcd.planecuts[iplane]<<" ***"<<endl;
647  for(size_t ibin=0; ibin < abcd.bincuts[iplane].size(); ibin++){
648  for(size_t iabcd=0; iabcd < abcd.abcdcuts.size(); iabcd++){
649  size_t index = abcd.indexBin(iplane, ibin, iabcd);
650  cout<<"MC: "<<setw(7)<<RoundNumber(allyields[1][index].Yield(),2)
651  <<" Data: "<<setw(4)<<RoundNumber(allyields[0][index].Yield(), 0)
652  <<" - "<< abcd.allcuts[index]<<endl;
653  } // Loop over ABCD cuts
654  cout<<"Kappa = "<<RoundNumber(kappas[iplane][ibin][0],2)<<"+"<<RoundNumber(kappas[iplane][ibin][1],2)
655  <<"-"<<RoundNumber(kappas[iplane][ibin][2],2)<<", Prediction = "
656  <<RoundNumber(preds[iplane][ibin][0],2)<<"+"<<RoundNumber(preds[iplane][ibin][1],2)
657  <<"-"<<RoundNumber(preds[iplane][ibin][2],2)<<endl;
658  cout<<endl;
659  } // Loop over bin cuts
660  } // Loop over plane cuts
661 
662 } // printDebug
663 
665 void plotKappa(abcd_method &abcd, vector<vector<vector<vector<float> > > > &allkappas){
666 
667  bool label_up = false;
668 
670  PlotOpt opts("txt/plot_styles.txt", "Kappa");
671  if(label_up) opts.BottomMargin(0.11);
672  setPlotStyle(opts);
673 
674  struct kmarker{
675  TString cut;
676  int color;
677  int style;
678  vector<float> kappa;
679  };
681  vector<vector<vector<kmarker> > > k_ordered;
682  vector<kmarker> ind_bcuts; // nb cuts actually used in the plot
683  vector<float> zz; // Zero length vector for the kmarker constructor
684  vector<kmarker> bcuts({{"nbm==1",4,20,zz}, {"nbm==2",2,21,zz}, {"nbm>=3",kGreen+3,22,zz}, {"nbm>=2",kMagenta+2,23,zz}});
685 
686  int nbins = 0; // Total number of njets bins (used in the base histo)
687  int ind= allkappas.size()-1; // Index of the kappa we'll be plotting, just the last in the vector
688  vector<vector<vector<float> > > kappas = allkappas[ind];
689  for(size_t iplane=0; iplane < kappas.size(); iplane++) {
690  k_ordered.push_back(vector<vector<kmarker> >());
691  for(size_t ibin=0; ibin < kappas[iplane].size(); ibin++){
692  if(allkappas.size()>2){
693  kappas[iplane][ibin][0] /= allkappas[ind%2][iplane][ibin][0];
694  kappas[iplane][ibin][1] = 0.;
695  kappas[iplane][ibin][2] = 0.;
696  }
697  TString bincut = abcd.bincuts[iplane][ibin];
698  bincut.ReplaceAll(" ","");
699  bincut.ReplaceAll("mm_","");
700  int index;
701  do{
702  index = bincut.First('[');
703  bincut.Remove(index, bincut.First(']')-index+1);
704  }while(index>=0);
705  bool found=false;
706  for(size_t ib=0; ib<bcuts.size(); ib++){
707  if(bincut.Contains(bcuts[ib].cut)){
709  bool cutfound=false;
710  for(size_t indb=0; indb<ind_bcuts.size(); indb++)
711  if(ind_bcuts[indb].color == bcuts[ib].color) cutfound = true;
712  if(!cutfound) ind_bcuts.push_back(bcuts[ib]);
713 
715  bincut.ReplaceAll(bcuts[ib].cut+"&&","");
716  for(size_t ik=0; ik<k_ordered[iplane].size(); ik++){
718  if(bincut==k_ordered[iplane][ik][0].cut){
719  k_ordered[iplane][ik].push_back({bincut, bcuts[ib].color, bcuts[ib].style, kappas[iplane][ibin]});
720  found = true;
721  break;
722  } // if same njets cut
723  } // Loop over existing ordered kappas
725  if(!found) {
726  k_ordered[iplane].push_back(vector<kmarker>({{bincut, bcuts[ib].color, bcuts[ib].style, kappas[iplane][ibin]}}));
727  found = true;
728  nbins++;
729  }
730  } // if bincut.Contains(bcuts[ib].cut)
731  } // Loop over nb cuts
732 
734  if(!found) {
735  k_ordered[iplane].push_back(vector<kmarker>({{bincut, bcuts[0].color, bcuts[0].style, kappas[iplane][ibin]}}));
736  nbins++;
737  if(ind_bcuts.size()==0) ind_bcuts.push_back(bcuts[0]);
738  }
739  } // Loop over bin cuts
740  } // Loop over plane cuts
741 
742  // //// Printing all values of kappa
743  // for(size_t iplane=0; iplane < k_ordered.size(); iplane++) {
744  // cout<<endl<<"New plane"<<endl;
745  // for(size_t ibin=0; ibin < k_ordered[iplane].size(); ibin++){
746  // cout<<"Points for "<<k_ordered[iplane][ibin][0].cut<<endl;
747  // for(size_t ib=0; ib<k_ordered[iplane][ibin].size(); ib++){
748  // cout<<"kappa = "<<k_ordered[iplane][ibin][ib].kappa[0]<<", color "<<k_ordered[iplane][ibin][ib].color<<endl;
749  // }
750  // }
751  // }
752 
754  TCanvas can("can","",1100,700);
755  TLine line; line.SetLineWidth(2); line.SetLineStyle(2);
756  TLatex label; label.SetTextSize(0.05); label.SetTextFont(132); label.SetTextAlign(23);
757 
758  float minx = 0.5, maxx = nbins+0.5, miny = 0, maxy = 2.2;
759  if(label_up) maxy = 2.4;
760  TH1D histo("histo", abcd.method, nbins, minx, maxx);
761  histo.SetMinimum(miny);
762  histo.SetMaximum(maxy);
763  histo.GetYaxis()->CenterTitle(true);
764  histo.GetXaxis()->SetLabelOffset(0.008);
765  if(allkappas.size()<=2) histo.SetYTitle("#kappa");
766  else histo.SetYTitle("#kappa^{scenario}/#kappa^{nominal}");
767  histo.Draw();
768 
770  int bin = 0;
771  vector<vector<double> > vx(ind_bcuts.size()), vexh(ind_bcuts.size()), vexl(ind_bcuts.size());
772  vector<vector<double> > vy(ind_bcuts.size()), veyh(ind_bcuts.size()), veyl(ind_bcuts.size());
773  for(size_t iplane=0; iplane < k_ordered.size(); iplane++) {
774  for(size_t ibin=0; ibin < k_ordered[iplane].size(); ibin++){
775  bin++;
776  histo.GetXaxis()->SetBinLabel(bin, CodeToRootTex(k_ordered[iplane][ibin][0].cut.Data()).c_str());
777  // xval is the x position of the first marker in the group
778  double xval = bin, nbs = k_ordered[iplane][ibin].size(), minxb = 0.15, binw = 0;
779  // If there is more than one point in the group, it starts minxb to the left of the center of the bin
780  // binw is the distance between points in the njets group
781  if(nbs>1) {
782  xval -= minxb;
783  binw = 2*minxb/(nbs-1);
784  }
785  for(size_t ib=0; ib<k_ordered[iplane][ibin].size(); ib++){
787  for(size_t indb=0; indb<ind_bcuts.size(); indb++){
788  if(ind_bcuts[indb].color == k_ordered[iplane][ibin][ib].color){
789  vx[indb].push_back(xval);
790  xval += binw;
791  vexl[indb].push_back(0);
792  vexh[indb].push_back(0);
793  vy[indb].push_back(k_ordered[iplane][ibin][ib].kappa[0]);
794  veyl[indb].push_back(k_ordered[iplane][ibin][ib].kappa[1]);
795  veyh[indb].push_back(k_ordered[iplane][ibin][ib].kappa[2]);
796  }
797  } // Loop over nb cuts in ordered TGraphs
798  } // Loop over nb cuts in kappa plot
799  } // Loop over bin cuts
800 
801  // Drawing line separating MET planes
802  line.SetLineStyle(2); line.SetLineWidth(2);
803  if (iplane<k_ordered.size()-1) line.DrawLine(bin+0.5, miny, bin+0.5, maxy);
804  // Drawing MET labels
805  if(label_up) label.DrawLatex((2*bin-k_ordered[iplane].size()+1.)/2., maxy-0.1, CodeToRootTex(abcd.planecuts[iplane].Data()).c_str());
806  else label.DrawLatex((2*bin-k_ordered[iplane].size()+1.)/2., -0.25, CodeToRootTex(abcd.planecuts[iplane].Data()).c_str());
807  } // Loop over plane cuts
808 
810  double legX(opts.LeftMargin()+0.026), legY(1-opts.TopMargin()-0.04), legSingle = 0.05;
811  if(label_up) legY = 0.8;
812  double legW = 0.22, legH = legSingle*(ind_bcuts.size()+1)/2;
813  if(ind_bcuts.size()>3) legH = legSingle*((ind_bcuts.size()+1)/2);
814  TLegend leg(legX, legY-legH, legX+legW, legY);
815  leg.SetTextSize(opts.LegendEntryHeight()); leg.SetFillColor(0);
816  leg.SetFillStyle(0); leg.SetBorderSize(0);
817  leg.SetTextFont(42);
818  leg.SetNColumns(2);
819  TGraphAsymmErrors graph[20]; // There's problems with vectors of TGraphs, so using an array
820  for(size_t indb=0; indb<ind_bcuts.size(); indb++){
821  graph[indb] = TGraphAsymmErrors(vx[indb].size(), &(vx[indb][0]), &(vy[indb][0]),
822  &(vexl[indb][0]), &(vexh[indb][0]), &(veyl[indb][0]), &(veyh[indb][0]));
823  graph[indb].SetMarkerStyle(ind_bcuts[indb].style); graph[indb].SetMarkerSize(1.65);
824  graph[indb].SetMarkerColor(ind_bcuts[indb].color);
825  graph[indb].SetLineColor(ind_bcuts[indb].color); graph[indb].SetLineWidth(2);
826  graph[indb].Draw("p0 same");
827  leg.AddEntry(&graph[indb], CodeToRootTex(ind_bcuts[indb].cut.Data()).c_str(), "p");
828  } // Loop over TGraphs
829  if(ind_bcuts.size()>1) leg.Draw();
830 
831  line.SetLineStyle(3); line.SetLineWidth(1);
832  line.DrawLine(minx, 1, maxx, 1);
833 
834  TString fname="plots/kappa_"+abcd.method+".pdf";
835  can.SaveAs(fname);
836  cout<<endl<<" open "<<fname<<endl;
837 
838 }
839 
840 void GetOptions(int argc, char *argv[]){
841  while(true){
842  static struct option long_options[] = {
843  {"method", required_argument, 0, 'm'}, // Method to run on (if you just want one)
844  {"skim", required_argument, 0, 's'}, // Which skim to use: standard, met150, 2015 data
845  {"split_bkg", no_argument, 0, 'b'}, // Prints Other, tt1l, tt2l contributions
846  {"no_signal", no_argument, 0, 'n'}, // Does not print signal columns
847  {"do_leptons", no_argument, 0, 'l'}, // Does tables for e/mu/emu as well
848  {"unblind", no_argument, 0, 'u'}, // Unblinds R4/D4
849  {"full_lumi", no_argument, 0, 'f'}, // Uses all data (does not apply nonblind)
850  {"debug", no_argument, 0, 'd'}, // Debug: prints yields and cuts used
851  {"only_dilepton", no_argument, 0, '2'}, // Makes tables only for dilepton tests
852  {0, 0, 0, 0}
853  };
854 
855  char opt = -1;
856  int option_index;
857  opt = getopt_long(argc, argv, "m:s:ufdbnl2", long_options, &option_index);
858  if(opt == -1) break;
859 
860  string optname;
861  switch(opt){
862  case 'm':
863  only_method = optarg;
864  break;
865  case 's':
866  skim = optarg;
867  break;
868  case 'b':
869  split_bkg = true;
870  break;
871  case '2':
872  only_dilepton = true;
873  break;
874  case 'l':
875  do_leptons = true;
876  break;
877  case 'n':
878  do_signal = false;
879  break;
880  case 'u':
881  unblind = true;
882  break;
883  case 'd':
884  debug = true;
885  break;
886  case 'f':
887  full_lumi = true;
888  break;
889  case 0:
890  break;
891  default:
892  printf("Bad option! getopt_long returned character code 0%o\n", opt);
893  break;
894  }
895  }
896 }
PlotOpt & TopMargin(double top)
Definition: plot_opt.cpp:260
#define DBG(x)
Definition: utilities.hpp:18
void setPlotStyle(PlotOpt opts)
Definition: styles.cpp:7
PlotOpt & LeftMargin(double left)
Definition: plot_opt.cpp:233
std::vector< GammaParams > Yield(const Process *process, double luminosity) const
Definition: table.cpp:158
TString lowerNjets(TString &cut)
const std::vector< std::unique_ptr< Figure > > & Figures() const
Definition: plot_maker.cpp:63
void ReplaceAll(std::string &str, const std::string &orig, const std::string &rep)
Definition: utilities.cpp:52
TString caption
Definition: abcd_method.hpp:18
STL namespace.
size_t indexBin(size_t iplane, size_t ibin, size_t iabcd)
Definition: abcd_method.cpp:73
bool Contains(const std::string &str, const std::string &pat)
Definition: utilities.cpp:44
TString rd_letter
Definition: abcd_method.hpp:18
bool multithreaded_
Definition: plot_maker.hpp:43
std::string execute(const std::string &cmd)
Definition: utilities.cpp:65
void findPreds(abcd_method &abcd, vector< vector< GammaParams > > &allyields, vector< vector< vector< float > > > &kappas, vector< vector< vector< float > > > &preds)
std::vector< std::vector< TString > > bincuts
Definition: abcd_method.hpp:17
std::vector< TString > allcuts
Definition: abcd_method.hpp:16
TString Zbi(double Nobs, double Nbkg, double Ebkg)
std::vector< TString > abcdcuts
Definition: abcd_method.hpp:16
double calcKappa(std::vector< std::vector< float > > &entries, std::vector< std::vector< float > > &weights, std::vector< float > &powers, float &mSigma, float &pSigma, bool do_data=false, bool verbose=false, double syst=-1., bool do_plot=false, int nrep=100000)
Definition: utilities.cpp:469
FigureType & Push(Args &&...args)
Definition: plot_maker.hpp:24
std::vector< GammaParams > BackgroundYield(double luminosity) const
Definition: table.cpp:175
std::string CodeToRootTex(std::string code)
Definition: utilities.cpp:116
int main(int argc, char *argv[])
std::string CodeToLatex(std::string code)
Definition: utilities.cpp:242
void changeMMCut(TString &cut, vector< TString > &mm_cuts, TString method, TString index_s)
void plotKappa(abcd_method &abcd, vector< vector< vector< vector< float > > > > &allkappas)
TString RoundNumber(double num, int decimals, double denom=1.)
Definition: utilities.cpp:361
Organizes efficient production of plots with single loop over each process.
Definition: plot_maker.hpp:14
std::vector< TString > planecuts
Definition: abcd_method.hpp:16
TString method
Definition: abcd_method.hpp:15
PlotOpt & BottomMargin(double bottom)
Definition: plot_opt.cpp:251
TString printTable(abcd_method &abcd, vector< vector< GammaParams > > &allyields, vector< vector< vector< float > > > &kappas, vector< vector< vector< float > > > &preds)
std::vector< GammaParams > DataYield() const
Definition: table.cpp:188
Definition: table.hpp:15
void GetOptions(int argc, char *argv[])
PlotOpt & LegendEntryHeight(double height)
Definition: plot_opt.cpp:287
void MakePlots(double luminosity, const std::string &subdir="")
Prints all added plots with given luminosity.
Definition: plot_maker.cpp:54
void printDebug(abcd_method &abcd, vector< vector< GammaParams > > &allyields, TString baseline, vector< vector< vector< float > > > &kappas, vector< vector< vector< float > > > &preds)
Loads colors from a text configuration file.
Definition: palette.hpp:8