ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
plot_2016-08-01_mjshapes_with_slides_pies.cxx
Go to the documentation of this file.
1 #include <cmath>
2 #include <stdio.h>
3 #include <chrono>
4 
5 #include "TError.h"
6 #include "TVector2.h"
7 
8 #include "core/plot_maker.hpp"
9 #include "core/plot_opt.hpp"
10 #include "core/palette.hpp"
11 #include "core/histo_stack.hpp"
12 #include "core/event_scan.hpp"
13 #include "core/utilities.hpp"
14 #include "core/table.hpp"
15 #include "core/slide_maker.hpp"
16 
17 using namespace std;
18 using namespace PlotOptTypes;
19 
20 namespace{
21  //beware setting all to true, results in many...many...plots...
22  bool do_metbins = false;
23  bool do_met150 = true;
24  bool do_htopts = false; // if false, does Jae's proposal only; if true, produces a pdf with comparisons
25  bool compare_mj_nolep = true;
26 }
27 
28 int main(){
29  gErrorIgnoreLevel = 6000;
30 
31  chrono::high_resolution_clock::time_point begTime;
32  begTime = chrono::high_resolution_clock::now();
33 
34  double lumi = 2.6;
35  string bfolder("");
36  string hostname(execute("echo $HOSTNAME"));
37  if(Contains(hostname, "cms") || Contains(hostname, "compute-"))
38  bfolder = "/net/cms2"; // In laptops, you can't create a /net folder
39 
40  Palette colors("txt/colors.txt", "default");
41 
42  //if folder name contains "reclustered", plots will also be made for MJ_nolep
43  string folder_mc(bfolder+"/cms2r0/babymaker/babies/reclustered/2016_06_14/mc/merged_nl1st500met150nj5/");
44  //speed up if met 150-200 not needed
45  if (!do_met150) folder_mc = bfolder+"/cms2r0/babymaker/babies/reclustered/2016_06_14/mc/merged_nleps1met200nj5/";
46 
47  set<string> files_tt({folder_mc+"*_TTJets*Lept*.root", folder_mc+"*_TTJets_HT*.root"});
48  set<string> files_wjets({folder_mc+"*_WJetsToLNu*.root"});
49  set<string> files_st({folder_mc+"*_ST_*.root"});
50  set<string> files_other({
51  folder_mc+"*DYJetsToLL*.root", folder_mc+"*_QCD_HT*.root",
52  folder_mc+"*_ZJet*.root", folder_mc+"*_WWTo*.root",
53  folder_mc+"*ggZH_HToBB*.root", folder_mc+"*ttHJetTobb*.root",
54  folder_mc+"*_TTGJets*.root", folder_mc+"*_TTTT_*.root",
55  folder_mc+"*_TTWJets*.root", folder_mc+"*_TTZTo*.root",
56  folder_mc+"*_WH_HToBB*.root", folder_mc+"*_WZTo*.root",
57  folder_mc+"*_ZH_HToBB*.root", folder_mc+"_ZZ_*.root"});
58 
59  set<string> files_nontt(files_other);
60  files_nontt.insert(files_wjets.begin(), files_wjets.end());
61  files_nontt.insert(files_st.begin(), files_st.end());
62 
63  set<string> files_all(files_nontt);
64  files_all.insert(files_tt.begin(), files_tt.end());
65 
66  PlotOpt log_shapes("txt/plot_styles.txt", "CMSPaper");
67  log_shapes.Title(TitleType::info)
68  .Bottom(BottomType::ratio)
69  .YAxis(YAxisType::log)
70  .Stack(StackType::shapes)
71  .RatioMaximum(2.4);
72  PlotOpt lin_shapes = log_shapes().YAxis(YAxisType::linear);
73  vector<PlotOpt> plot_types = {lin_shapes};
74 
75  NamedFunc baseline_1l = "stitch && nleps==1 && nveto==0 && nbm>=1 && weight<1";
76  NamedFunc baseline_2l = "stitch && nleps==2";
77  NamedFunc baseline_lveto = "stitch && nleps==1 && nveto==1 && nbm>=1 && mt>140";
78 
79  auto tt1l_lowmt = Process::MakeShared<Baby_full>("t#bar{t}(1l), m_{T} #leq 140",
80  Process::Type::background, colors("tt_1l"), files_tt, baseline_1l && "ntruleps>=1 && mt<=140");
81  tt1l_lowmt->SetLineStyle(2);
82  auto tt1l_highmt = Process::MakeShared<Baby_full>("t#bar{t}(1l), m_{T} > 140",
83  Process::Type::background, colors("tt_1l"), files_tt, baseline_1l && "ntruleps>=1 && mt>140");
84  auto tt2l = Process::MakeShared<Baby_full>("t#bar{t}(2l)",
85  Process::Type::background, kGreen+2, files_tt, baseline_2l && "ntruleps>=1");
86  auto ttlveto = Process::MakeShared<Baby_full>("t#bar{t}(lv), m_{T} > 140",
87  Process::Type::background, kOrange+1, files_tt, baseline_lveto && "ntruleps>=1");
88 
89  auto tt1l_highmt_2trul = Process::MakeShared<Baby_full>("t#bar{t}(1l), m_{T} > 140, tru: 2l",
90  Process::Type::background, colors("tt_2l"), files_tt,
91  baseline_1l && "ntruleps>=1 && ntruels+ntrumus+ntrutausl==2 && mt>140");
92  auto tt1l_highmt_1trul_1trutauh = Process::MakeShared<Baby_full>("t#bar{t}(1l), m_{T} > 140, tru: l#tau_{h}",
93  Process::Type::background, kBlue-6, files_tt,
94  baseline_1l && "ntruleps>=1 && ntruels+ntrumus+ntrutausl==1 && ntrutaush==1 && mt>140");
95  auto tt1l_highmt_1trul = Process::MakeShared<Baby_full>("t#bar{t}(1l), m_{T} > 140, tru: 1l",
96  Process::Type::background, kTeal-6, files_tt,
97  baseline_1l && "ntruleps>=1 && ntruels+ntrumus+ntrutausl==1 && ntrutaush==0 && mt>140");
98 
99  auto bkg1l_lowmt = Process::MakeShared<Baby_full>("Tot. bkgd (1l), m_{T}#leq140",
100  Process::Type::background, kGray+3, files_all, baseline_1l && "mt<=140");
101  bkg1l_lowmt->SetLineStyle(2);
102  auto bkg1l_highmt = Process::MakeShared<Baby_full>("Tot. bkgd (1l), m_{T}>140",
103  Process::Type::background, kGray+3, files_all, baseline_1l && "mt>140");
104  auto bkg2l = Process::MakeShared<Baby_full>("Tot. bkgd (2l)",
105  Process::Type::background, kGreen-5, files_all, baseline_2l);
106  auto bkglveto = Process::MakeShared<Baby_full>("Tot. bkgd (lv), m_{T} > 140",
107  Process::Type::background, kOrange+1, files_all, baseline_lveto);
108 
109  auto wjets1l_lowmt = Process::MakeShared<Baby_full>("W+jets (1l), m_{T}#leq140",
110  Process::Type::background, colors("wjets"), files_wjets, baseline_1l && "mt<=140");
111  // wjets1l_lowmt->SetLineStyle(2);
112  auto st1l_lowmt = Process::MakeShared<Baby_full>("Single t (1l), m_{T}#leq140",
113  Process::Type::background, colors("single_t"), files_st, baseline_1l && "mt<=140");
114  // st1l_lowmt->SetLineStyle(2);
115  auto other1l_lowmt = Process::MakeShared<Baby_full>("Other (1l), m_{T}#leq140",
116  Process::Type::background, colors("other"), files_other, baseline_1l && "mt<=140");
117  // other1l_lowmt->SetLineStyle(2);
118  auto nontt2l = Process::MakeShared<Baby_full>("Non-t#bar{t} (2l)",
119  Process::Type::background, colors("other"), files_nontt, baseline_2l);
120  auto nontt1l_highmt = Process::MakeShared<Baby_full>("Non-t#bar{t} (1l), m_{T}>140",
121  Process::Type::background, colors("other"), files_nontt, baseline_1l && "mt>140");
122 
123  map<string, vector<shared_ptr<Process> > > procs, procs_pie, procs_isr;
124  // compare the shapes of the low-mT components: tt, st, w, other
125  procs["lowmt"] = vector<shared_ptr<Process> >({tt1l_lowmt, other1l_lowmt, st1l_lowmt, wjets1l_lowmt});
126  procs_pie["lowmt"] = vector<shared_ptr<Process> >({tt1l_lowmt, other1l_lowmt, st1l_lowmt, wjets1l_lowmt});
127  // compare the tt@low-mT shape to the high-mT components: tru 2l, tru 1l+tauh, non-tt
128  procs["highmt"] = vector<shared_ptr<Process> >({tt1l_lowmt, tt1l_highmt_2trul,
129  tt1l_highmt_1trul_1trutauh, tt1l_highmt_1trul, nontt1l_highmt});
130  procs_isr["highmt"] = vector<shared_ptr<Process> >({tt1l_lowmt, tt1l_highmt_2trul,
131  tt1l_highmt_1trul_1trutauh, tt1l_highmt_1trul});
132  procs_pie["highmt"] = vector<shared_ptr<Process> >({tt1l_highmt_2trul,
133  tt1l_highmt_1trul_1trutauh, tt1l_highmt_1trul, nontt1l_highmt});
134  // compare the tt@low-mT shape to the dilepton test pieces: tt 2l, non-tt 2l and lveto
135  procs["dilep"] = vector<shared_ptr<Process> >({tt1l_lowmt, nontt2l, ttlveto, tt2l});
136  procs_isr["dilep"] = vector<shared_ptr<Process> >({tt1l_lowmt, ttlveto, tt2l});
137  procs_pie["dilep"] = vector<shared_ptr<Process> >({nontt2l, ttlveto, tt2l});
138  // compare full background: low-mT, high-mT, 2l and lveto
139  procs["totbkg"] = vector<shared_ptr<Process> >({bkg1l_lowmt, bkg2l, bkglveto, bkg1l_highmt});
140  procs_pie["totbkg"] = vector<shared_ptr<Process> >({bkg2l, bkglveto, bkg1l_highmt});
141 
142  vector<NamedFunc> htopt;
143  if (do_htopts) {
144  htopt.push_back(NamedFunc("ht"));
145  htopt.push_back(NamedFunc("st", [](const Baby &b) -> NamedFunc::ScalarType{
146  float st = b.ht();
147  for (const auto &pt: *(b.leps_pt())) st += pt;
148  return st;
149  }));
150  htopt.push_back(NamedFunc("ht1l_stmin2l", [](const Baby &b) -> NamedFunc::ScalarType{
151  float ht_proxy = b.ht();
152  if (b.nleps()==2) ht_proxy = b.ht()+b.leps_pt()->at(1);
153  return ht_proxy;
154  }));
155  htopt.push_back(NamedFunc("ht1l_stave2l", [](const Baby &b) -> NamedFunc::ScalarType{
156  float ht_proxy = b.ht();
157  if (b.nleps()==2) ht_proxy = b.ht()+(b.leps_pt()->at(0)+b.leps_pt()->at(1))/2.;
158  return ht_proxy;
159  }));
160  }
161  htopt.push_back(NamedFunc("ht1l_stmax2l", [](const Baby &b) -> NamedFunc::ScalarType{
162  float ht_proxy = b.ht();
163  if (b.nleps()==2) ht_proxy = b.ht()+b.leps_pt()->at(0);
164  return ht_proxy;
165  }));
166 
167  vector<NamedFunc> metbins;
168  if (do_met150) metbins.push_back(NamedFunc("met>150&&met<=200"));
169  if (do_metbins) {
170  metbins.push_back(NamedFunc("met>200&&met<=350"));
171  metbins.push_back(NamedFunc("met>350&&met<=500"));
172  metbins.push_back(NamedFunc("met>500"));
173  } else {
174  metbins.push_back(NamedFunc("met>200"));
175  }
176 
177  NamedFunc ave_toppt("ave_toppt",[](const Baby &b) -> NamedFunc::ScalarType{
178  float toppt = 0;
179  for (size_t imc(0); imc<b.mc_pt()->size(); imc++){
180  if (fabs(b.mc_id()->at(imc))==6 && b.mc_status()->at(imc)==62) toppt += b.mc_pt()->at(imc);
181  }
182  return toppt/2.;
183  });
184 
185  map<bool, vector<string> > nobjbins;
186  // with leptons clustered
187  nobjbins[true] = vector<string>();
188  nobjbins[true].push_back("njets+nleps==6");
189  nobjbins[true].push_back("njets+nleps>=7 && njets+nleps<=9");
190  nobjbins[true].push_back("njets+nleps>=10");
191  // without leptons clustered
192  nobjbins[false] = vector<string>();
193  nobjbins[false].push_back("njets==5");
194  nobjbins[false].push_back("njets>=6 && njets<=8");
195  nobjbins[false].push_back("njets>=9");
196 
197  set<string> pie_names, plot_names;
198  int Nplots(0);
199  PlotMaker pm;
200  vector<bool> rcopts = {true, false}; // plot both
201  if (!Contains(folder_mc,"reclustered") || !compare_mj_nolep) rcopts = {true};
202  for (auto mj_lep: {true, false}){
203  string xtitle = mj_lep ? "M_{J} [GeV]" : "M_{J} (no lep) [GeV]";
204  string var = (Contains(folder_mc,"reclustered") && mj_lep) ? "mj14_original" : "mj14";
205  for (auto &iproc:procs){
206  vector<TableRow> table_cuts;
207  for (auto &imet: metbins) {
208  for (auto &inobj: nobjbins[mj_lep]) {
209  for (auto &iht: htopt){
210  // no dilepton test for njets==5
211  if(iproc.first == "dilep" && Contains(inobj,"==6")) continue;
212  vector<shared_ptr<Process> > procs_tmp = iproc.second;
213  //similarly, remove 2l for njets==5 category
214  if (iproc.first == "totbkg" && Contains(inobj,"==6"))
215  procs_tmp = vector<shared_ptr<Process> >({bkg1l_lowmt, bkglveto, bkg1l_highmt});
216  // histograms
217  NamedFunc icut = imet && inobj && iht>500;
218  pm.Push<HistoStack>(HistoDef(iproc.first+"_"+iht.PlainName(),
219  10, 100., 850., var, xtitle, icut, "weight", {250.,400.}), procs_tmp, plot_types);
220  plot_names.insert(pm.GetLast<HistoStack>()->definition_.Name()+"_OPT_"+plot_types[0].TypeString()+".pdf");
221 
222  if (procs_isr.find(iproc.first)!=procs_isr.end()) {
223  pm.Push<HistoStack>(HistoDef(iproc.first+"_"+iht.PlainName(),
224  10, 0., 800., ave_toppt, "Ave. top p_{T} [GeV]", icut && var+">250", "weight"), procs_isr[iproc.first], plot_types);
225  plot_names.insert(pm.GetLast<HistoStack>()->definition_.Name()+"_OPT_"+plot_types[0].TypeString()+".pdf");
226  pm.Push<HistoStack>(HistoDef(iproc.first+"_"+iht.PlainName(),
227  10, 0., 800., "isr_tru_pt", "True ISR p_{T} [GeV]", icut && var+">250", "weight"), procs_isr[iproc.first], plot_types);
228  plot_names.insert(pm.GetLast<HistoStack>()->definition_.Name()+"_OPT_"+plot_types[0].TypeString()+".pdf");
229  }
230  // pies
231  table_cuts.push_back(TableRow("",icut));
232  pie_names.insert("pie_"+iproc.first+"_"+icut.PlainName()+"_perc_lumi"+RoundNumber(lumi,0).Data()+".pdf");
233  Nplots++;
234  } // loop over ht options
235  } // loop over nobj bins
236  } // loop over met bins
237  pm.Push<Table>(iproc.first, table_cuts, procs_pie[iproc.first], false, true, true);
238  } // loop over proc sets
239  } // loop over cluster vs not-cluster leptons
240  pm.min_print_ = true;
241  pm.MakePlots(lumi);
242 
243  // slides comparing closure for MJ_lep and MJ_nolep
244  if (Contains(folder_mc,"reclustered") && compare_mj_nolep) {
245  SlideMaker sm_mjnolep("mjlep_vs_nolep.tex","1610");
246  for (auto &iht: htopt){
247  for (auto &imet: metbins) {
248  for (size_t inobj(0); inobj<nobjbins[true].size(); inobj++) {
249  vector<string> pnames;
250  for (auto mj_lep: {true, false}) {
251  for (auto &iproc: procs){
252  string proc = iproc.first;
253  if(proc == "dilep" && (Contains(nobjbins[mj_lep][inobj],"==6") || Contains(nobjbins[mj_lep][inobj],"==5"))) continue;
254  NamedFunc icut = imet && nobjbins[mj_lep][inobj] && iht>500;
255  string var = mj_lep ? "mj14_original" : "mj14";
256  string iname = proc+"_"+iht.PlainName()+"_VAR_"+var+"_CUT_"+icut.PlainName()
257  +"_WGT_weight_OPT_"+plot_types[0].TypeString()+".pdf";
258  if (plot_names.find(iname)!=plot_names.end()) pnames.push_back(iname);
259  }
260  }
261  sm_mjnolep.AddSlide(pnames,4);
262  }
263  }
264  }
265  sm_mjnolep.Close();
266  }
267 
268  // Make slides to show comparison between the different modified HT options
269  if (do_htopts){
270  for (auto mj_lep: {true, false}){
271  string sname = mj_lep ? "plots_mj.tex" : "plots_mj_nolep.tex";
272  SlideMaker sm_htopt(sname);
273  string var = (Contains(folder_mc,"reclustered") && mj_lep) ? "mj14_original" : "mj14";
274  for (auto &iproc: procs){
275  string proc = iproc.first;
276  for (auto &imet: metbins) {
277  for (auto &inobj: nobjbins[mj_lep]) {
278  vector<string> pnames(15,"");
279  for (size_t iht(0); iht<htopt.size(); iht++){
280  NamedFunc icut = imet && inobj && htopt[iht]>500;
281  string iname = proc+"_"+htopt[iht].PlainName()+"_VAR_"+var + "_CUT_" + icut.PlainName()
282  + "_WGT_weight_OPT_"+plot_types[0].TypeString()+".pdf";
283  if (plot_names.find(iname)!=plot_names.end()){
284  pnames[iht] = "pie_"+proc+"_"+icut.PlainName()+"_perc_lumi"+RoundNumber(lumi,0).Data()+".pdf";
285  pnames[htopt.size()+iht] = iname;
286  }
287  icut = icut && var+">250";
288  iname = proc+"_"+htopt[iht].PlainName()+"_VAR_isr_tru_pt_CUT_" + icut.PlainName()
289  + "_WGT_weight_OPT_"+plot_types[0].TypeString()+".pdf";
290  if (plot_names.find(iname)!=plot_names.end()) pnames[2*htopt.size()+iht] = iname;
291  }
292  sm_htopt.AddSlide(pnames,5);
293  }
294  }
295  }
296  sm_htopt.Close();
297  }
298  }
299 
300  double seconds = (chrono::duration<double>(chrono::high_resolution_clock::now() - begTime)).count();
301  TString hhmmss = HoursMinSec(seconds);
302  cout<<endl<<"Making "<<Nplots<<" plots took "<<round(seconds)<<" seconds ("<<hhmmss<<")"<<endl<<endl;
303 }
304 
std::vector< int > *const & mc_status() const
Get mc_status for current event and cache it.
Definition: baby.cpp:4625
PlotOpt & Stack(PlotOptTypes::StackType stack_type)
Definition: plot_opt.cpp:120
PlotOpt & YAxis(PlotOptTypes::YAxisType y_axis_type)
Definition: plot_opt.cpp:102
TString HoursMinSec(float fseconds)
Definition: utilities.cpp:337
std::vector< float > *const & leps_pt() const
Get leps_pt for current event and cache it.
Definition: baby.cpp:4493
int const & nleps() const
Get nleps for current event and cache it.
Definition: baby.cpp:6053
Abstract base class for access to ntuple variables.
Definition: baby.hpp:16
void Close()
Definition: slide_maker.cpp:37
STL namespace.
Combines a callable function taking a Baby and returning a scalar or vector with its string represent...
Definition: named_func.hpp:13
bool Contains(const std::string &str, const std::string &pat)
Definition: utilities.cpp:44
std::string execute(const std::string &cmd)
Definition: utilities.cpp:65
double ScalarType
Definition: named_func.hpp:15
std::vector< float > *const & mc_pt() const
Get mc_pt for current event and cache it.
Definition: baby.cpp:4613
FigureType & Push(Args &&...args)
Definition: plot_maker.hpp:24
bool min_print_
Definition: plot_maker.hpp:44
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
PlotOpt & Bottom(PlotOptTypes::BottomType bottom_type)
Definition: plot_opt.cpp:93
std::string PlainName() const
PlotOpt & RatioMaximum(double ratio_maximum)
Definition: plot_opt.cpp:368
std::vector< int > *const & mc_id() const
Get mc_id for current event and cache it.
Definition: baby.cpp:4553
FigureType * GetLast()
Definition: plot_maker.hpp:34
Definition: table.hpp:15
const NamedFunc st("st", [](const Baby &b) -> NamedFunc::ScalarType{float stvar=b.ht();for(const auto &pt:*(b.leps_pt())) stvar+=pt;return stvar;})
void AddSlide(std::vector< std::string > pnames, int ncols=-1, std::string title="")
Definition: slide_maker.cpp:15
void MakePlots(double luminosity, const std::string &subdir="")
Prints all added plots with given luminosity.
Definition: plot_maker.cpp:54
PlotOpt & Title(PlotOptTypes::TitleType title_type)
Definition: plot_opt.cpp:111
float const & ht() const
Get ht for current event and cache it.
Definition: baby.cpp:3929
Loads colors from a text configuration file.
Definition: palette.hpp:8