ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
plot_scalefactors.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include <string>
3 #include <vector>
4 #include <memory>
5 
6 #include "TError.h"
7 #include "TColor.h"
8 
9 #include "core/baby.hpp"
10 #include "core/process.hpp"
11 #include "core/named_func.hpp"
12 #include "core/plot_maker.hpp"
13 #include "core/plot_opt.hpp"
14 #include "core/palette.hpp"
15 #include "core/hist1d.hpp"
16 
17 using namespace std;
18 using namespace PlotOptTypes;
19 
20 double getWeight(const Baby &b, int bin);
24 
25 int main(){
26  gErrorIgnoreLevel = 6000;
27 
28  double lumi = 12.9;
29 
30  string mc_folder = "/net/cms29/cms29r0/babymaker/babies/2016_08_10/mc/skim_nleps1__metG250__htG250/";
31  string data_folder = "/net/cms29/cms29r0/babymaker/babies/2016_08_10/data/skim_nleps1__metG250__htG250/";
32 
33  Palette colors("txt/colors.txt", "default");
34 
35  auto data = Process::MakeShared<Baby_full>("Data", Process::Type::data, kBlack,
36  {data_folder+"*.root"},"pass&&(trig[13]||trig[33])&&json12p9");
37 
38  auto tt_1l = Process::MakeShared<Baby_full>("t#bar{t} (1l)", Process::Type::background, colors("tt_1l"),
39  {mc_folder+"*_TTJets*Lept*.root"},"pass&&ntruleps<=1");
40  auto tt_2l = Process::MakeShared<Baby_full>("t#bar{t} (2l)", Process::Type::background, colors("tt_2l"),
41  {mc_folder+"*_TTJets*Lept*.root"},"pass&&ntruleps>=2");
42  auto wjets = Process::MakeShared<Baby_full>("W+Jets", Process::Type::background, colors("wjets"),
43  {mc_folder+"*_WJets*HT*.root"},"pass");
44  auto st = Process::MakeShared<Baby_full>("Single t", Process::Type::background, colors("single_t"),
45  {mc_folder+"*_ST_*.root"},"pass");
46 
47  vector<shared_ptr<Process> > full_sam = {data, tt_1l, tt_2l, wjets, st};
48 
49  PlotOpt log_lumi("txt/plot_styles.txt", "CMSPaper");
50  log_lumi.Title(TitleType::preliminary)
51  .Bottom(BottomType::ratio)
52  .YAxis(YAxisType::log)
53  .Stack(StackType::data_norm)
54  .FileExtensions({"pdf"});
55  PlotOpt lin_lumi = log_lumi().YAxis(YAxisType::linear);
56  PlotOpt log_shapes = log_lumi().Stack(StackType::shapes)
57  .Bottom(BottomType::off)
58  .ShowBackgroundError(false);
59  PlotOpt lin_shapes = log_shapes().YAxis(YAxisType::linear);
60  PlotOpt log_lumi_info = log_lumi().Title(TitleType::info);
61  PlotOpt lin_lumi_info = lin_lumi().Title(TitleType::info);
62  PlotOpt log_shapes_info = log_shapes().Title(TitleType::info);
63  PlotOpt lin_shapes_info = lin_shapes().Title(TitleType::info);
64  vector<PlotOpt> all_plot_types = {log_lumi_info};
65 
66  PlotMaker pm;
67 
68  NamedFunc nels25("nels",nEls25);
69  NamedFunc nelstight25("n_{e}^{t}",nElsTight25);
70  NamedFunc nmus25("nmus",nMus25);
71  NamedFunc baseline = "ht>250 && met>250 && njets>=2"; // Need to check lepton definitions
72 
73  for(int ilep=0; ilep<3; ilep++){
74  NamedFunc lepcut = "1";
75  if(ilep==0) lepcut = nelstight25==1;
76  if(ilep==1) lepcut = nmus25==1;
77  if(ilep==2) lepcut = nelstight25+nmus25==1;
78 
79  for(int ictrl=0; ictrl<3; ictrl++){
80  NamedFunc ctrlreg = "1";
81  if(ictrl==0) ctrlreg = "nbm==0"; //WJets
82  if(ictrl==1) ctrlreg = "nbm>=1"; // ttbar
83  if(ictrl==2) ctrlreg = "nbm>=2"; // ttbar
84 
85  vector<NamedFunc> weights;
86  weights.push_back(NamedFunc("No SF", [](const Baby &b) -> NamedFunc::ScalarType{return getWeight(b, 0);}));
87  weights.push_back(NamedFunc("w_lep", [](const Baby &b) -> NamedFunc::ScalarType{return getWeight(b, 1);}));
88  weights.push_back(NamedFunc("w_btag", [](const Baby &b) -> NamedFunc::ScalarType{return getWeight(b, 2);}));
89  weights.push_back(NamedFunc("w_isr", [](const Baby &b) -> NamedFunc::ScalarType{return getWeight(b, 3);}));
90  weights.push_back(NamedFunc("w_lep*w_btag", [](const Baby &b) -> NamedFunc::ScalarType{return getWeight(b, 4);}));
91  weights.push_back(NamedFunc("w_lep*w_isr", [](const Baby &b) -> NamedFunc::ScalarType{return getWeight(b, 5);}));
92  weights.push_back(NamedFunc("w_btag*w_isr", [](const Baby &b) -> NamedFunc::ScalarType{return getWeight(b, 6);}));
93  weights.push_back(NamedFunc("w_lep*w_btag*w_isr", [](const Baby &b) -> NamedFunc::ScalarType{return getWeight(b, 7);}));
94 
95  for(const auto &iwght: weights){
96 
97  pm.Push<Hist1D>(Axis(25, 250., 1500., "ht", "H_{T} [GeV]"), baseline && ctrlreg && lepcut, full_sam, all_plot_types).Weight(iwght);
98  pm.Push<Hist1D>(Axis(20, 250., 1250., "met", "MET [GeV]"), baseline && ctrlreg && lepcut, full_sam, all_plot_types).Weight(iwght);
99  pm.Push<Hist1D>(Axis(10, 2., 12., "njets", "N_{jets}"), baseline && ctrlreg && lepcut, full_sam, all_plot_types).Weight(iwght);
100  pm.Push<Hist1D>(Axis(5, 0., 5., "nbm", "N_{b}^{med}"), baseline && ctrlreg && lepcut, full_sam, all_plot_types).Weight(iwght);
101  pm.Push<Hist1D>(Axis(20, 0, 1000, "mj14", "M_{J}"), baseline && ctrlreg && lepcut, full_sam, all_plot_types).Weight(iwght);
102  pm.Push<Hist1D>(Axis(12, 0, 420, "mt", "m_{T}"), baseline && ctrlreg && lepcut, full_sam, all_plot_types).Weight(iwght);
103  }
104  }
105  }
106  pm.MakePlots(lumi);
107 }
108 
109 
110 //NamedFunc::ScalarType getWeight(const Baby &b, int bin){
111 double getWeight(const Baby &b, int bin){
112 
113  double weight = -99999999999; //Will notice if weight is not being set
114 
115  if(bin==0)
116  weight = b.w_lumi()*b.w_pu();
117  else if(bin==1)
118  weight = b.w_lumi()*b.w_pu()*b.w_lep();
119  else if(bin==2)
120  weight = b.w_lumi()*b.w_pu()*b.w_btag();
121  else if(bin==3){
122  weight = b.w_lumi()*b.w_pu();
123  if(b.ntrupv()>=0) //Only apply to non-data samples
124  weight *= b.w_isr();
125  }
126  else if(bin==4)
127  weight = b.w_lumi()*b.w_pu()*b.w_lep()*b.w_btag();
128  else if(bin==5){
129  weight = b.w_lumi()*b.w_pu()*b.w_lep();
130  if(b.ntrupv()>=0)
131  weight *= b.w_isr();
132  }
133  else if(bin==6){
134  weight = b.w_lumi()*b.w_pu()*b.w_btag();
135  if(b.ntrupv()>=0)
136  weight *= b.w_isr();
137  }
138  else if(bin==7){
139  weight = b.w_lumi()*b.w_pu()*b.w_lep()*b.w_btag();
140  if(b.ntrupv()>=0)
141  weight *= b.w_isr();
142  }
143 
144  return weight;
145 }
146 
148  int nels = 0;
149  for (size_t iel(0); iel<b.els_pt()->size(); iel++){
150  if (b.els_pt()->at(iel)>25 && b.els_sig()->at(iel) && b.els_miniso()->at(iel)<0.1)
151  nels++;
152  }
153  return nels;
154 }
155 
157  int nels = 0;
158  for (size_t iel(0); iel<b.els_pt()->size(); iel++){
159  if (b.els_pt()->at(iel)>25 && b.els_tight()->at(iel) && b.els_miniso()->at(iel)<0.1)
160  nels++;
161  }
162  return nels;
163 }
164 
166  int nmus = 0;
167  for (size_t imu(0); imu<b.mus_pt()->size(); imu++){
168  if (b.mus_pt()->at(imu)>25 && b.mus_sig()->at(imu) && b.mus_miniso()->at(imu)<0.2)
169  nmus++;
170  }
171  return nmus;
172 }
float const & w_btag() const
Get w_btag for current event and cache it.
Definition: baby.cpp:7409
PlotOpt & Stack(PlotOptTypes::StackType stack_type)
Definition: plot_opt.cpp:120
PlotOpt & FileExtensions(const std::set< std::string > &file_extensions)
Definition: plot_opt.cpp:138
PlotOpt & YAxis(PlotOptTypes::YAxisType y_axis_type)
Definition: plot_opt.cpp:102
std::vector< float > *const & els_pt() const
Get els_pt for current event and cache it.
Definition: baby.cpp:3461
int main()
std::vector< bool > *const & els_sig() const
Get els_sig for current event and cache it.
Definition: baby.cpp:3497
float const & w_isr() const
Get w_isr for current event and cache it.
Definition: baby.cpp:7457
Abstract base class for access to ntuple variables.
Definition: baby.hpp:16
STL namespace.
std::vector< float > *const & mus_pt() const
Get mus_pt for current event and cache it.
Definition: baby.cpp:5633
Combines a callable function taking a Baby and returning a scalar or vector with its string represent...
Definition: named_func.hpp:13
NamedFunc::ScalarType nElsTight25(const Baby &b)
std::vector< float > *const & els_miniso() const
Get els_miniso for current event and cache it.
Definition: baby.cpp:3437
double ScalarType
Definition: named_func.hpp:15
FigureType & Push(Args &&...args)
Definition: plot_maker.hpp:24
Definition: axis.hpp:12
std::vector< bool > *const & els_tight() const
Get els_tight for current event and cache it.
Definition: baby.cpp:3521
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
float const & w_lep() const
Get w_lep for current event and cache it.
Definition: baby.cpp:7469
NamedFunc::ScalarType nEls25(const Baby &b)
float const & w_lumi() const
Get w_lumi for current event and cache it.
Definition: baby.cpp:7481
PlotOpt & ShowBackgroundError(bool show_background_error)
Definition: plot_opt.cpp:386
std::vector< bool > *const & mus_sig() const
Get mus_sig for current event and cache it.
Definition: baby.cpp:5669
A full 1D plot with stacked/overlayed histograms.
Definition: hist1d.hpp:23
double getWeight(const Baby &b, int bin)
const NamedFunc st("st", [](const Baby &b) -> NamedFunc::ScalarType{float stvar=b.ht();for(const auto &pt:*(b.leps_pt())) stvar+=pt;return stvar;})
std::vector< float > *const & mus_miniso() const
Get mus_miniso for current event and cache it.
Definition: baby.cpp:5585
int const & ntrupv() const
Get ntrupv for current event and cache it.
Definition: baby.cpp:6197
NamedFunc::ScalarType nMus25(const Baby &b)
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 & w_pu() const
Get w_pu for current event and cache it.
Definition: baby.cpp:7493
Loads colors from a text configuration file.
Definition: palette.hpp:8