ra4_macros  bede988c286599a3a84b77a4d788ac0a971e89f9
plot_mj_met.cxx
Go to the documentation of this file.
1 // plot_mj_macros: Macro that plots variables both lumi weighted and normalized to the same area.
2 
3 #include <iostream>
4 #include <vector>
5 
6 #include "TChain.h"
7 #include "TH1D.h"
8 #include "TCanvas.h"
9 #include "TLegend.h"
10 #include "TLine.h"
11 #include "TString.h"
12 #include "TColor.h"
13 #include "TMath.h"
14 
15 #include "styles.hpp"
16 #include "utilities.hpp"
17 #include "utilities_macros.hpp"
18 
19 using namespace std;
20 using std::cout;
21 using std::endl;
22 
23 int main(){
24  TH1::SetDefaultSumw2(true);
25 
26  styles style("LargeLabels"); style.setDefaultStyle();
27  vector<hfeats> vars;
28  TCanvas can;
29 
30  TColor ucsb_blue(1000, 1/255.,57/255.,166/255.);
31  TColor ucsb_gold(1001, 255/255.,200/255.,47/255);
32  TColor penn_red(1002, 149/255.,0/255.,26/255.);
33  TColor uf_orange(1003, 255/255.,74/255.,0/255.);
34  TColor uo_green(1004, 0/255.,79/255.,39/255.);
35  TColor tcu_purple(1005, 72/255.,42/255.,100/255.);
36  TColor tar_heel_blue(1006, 86/255.,160/255.,211/255.);
37  TColor sig_teal(1007, 96/255.,159/255.,128/255.);
38  TColor sig_gold(1008, 215/255.,162/255.,50/255.);
39  TColor seal_brown(1010, 89/255.,38/255.,11/255.);
40 
41  TString folder="archive/15-03-17/skims/";
42  //TString folder="archive/15-03-03/";
43  vector<TString> s_tt;
44  s_tt.push_back(folder+"*_TTJet*");
45  vector<TString> s_wjets;
46  s_wjets.push_back(folder+"*_WJets*");
47  vector<TString> s_single;
48  s_single.push_back(folder+"*_T*channel*");
49  vector<TString> s_ttv;
50  s_ttv.push_back(folder+"*TTW*");
51  s_ttv.push_back(folder+"*TTZ*");
52  vector<TString> s_other;
53  s_other.push_back(folder+"*QCD_HT*");
54  s_other.push_back(folder+"*_ZJet*");
55  s_other.push_back(folder+"*DY*");
56  s_other.push_back(folder+"*WH_HToBB*");
57  vector<TString> s_t1t;
58  s_t1t.push_back(folder+"*T1tttt*1500_*PU20*");
59  vector<TString> s_t1tc;
60  s_t1tc.push_back(folder+"*T1tttt*1200_*PU20*");
61 
62  // Reading ntuples
63  vector<TChain *> chain;
64  vector<sfeats> Samples;
65  Samples.push_back(sfeats(s_other, "Other", 1001));
66  Samples.push_back(sfeats(s_ttv, "ttV", 1002));
67  Samples.push_back(sfeats(s_single, "Single top", 1005));
68  Samples.push_back(sfeats(s_wjets, "W + jets", 1004));
69  Samples.push_back(sfeats(s_tt, "t#bar{t}, 2 l", 1006,1,"((mc_type&0x0F00)/0x100+(mc_type&0x000F)-(mc_type&0x00F0)/0x10)>=2"));
70  Samples.push_back(sfeats(s_tt, "t#bar{t}, 1 l", 1000,1,"((mc_type&0x0F00)/0x100+(mc_type&0x000F)-(mc_type&0x00F0)/0x10)<=1"));
71  Samples.push_back(sfeats(s_t1t, "T1tttt(1500,100)", 2));
72  Samples.push_back(sfeats(s_t1tc, "T1tttt(1200,800)", 2,2));
73 
74  Samples.push_back(sfeats(s_tt, "t#bar{t} 2l, 200<MET<400", 1,1,
75  "met>200&&met<400&&((mc_type&0x0F00)/0x100+(mc_type&0x000F)-(mc_type&0x00F0)/0x10)>=2"));
76  Samples.push_back(sfeats(s_tt, "t#bar{t} 2l, MET #geq 400", 1,2,
77  "met>=400&&((mc_type&0x0F00)/0x100+(mc_type&0x000F)-(mc_type&0x00F0)/0x10)>=2"));
78  // Samples.push_back(sfeats(s_tt, "t#bar{t} 1l, 200<MET<400", 1,1,
79  // "met>200&&met<400&&((mc_type&0x0F00)/0x100+(mc_type&0x000F)-(mc_type&0x00F0)/0x10)<=1"));
80  // Samples.push_back(sfeats(s_tt, "t#bar{t} 1l, MET #geq 400", 1,2,
81  // "met>=400&&((mc_type&0x0F00)/0x100+(mc_type&0x000F)-(mc_type&0x00F0)/0x10)<=1"));
82  Samples.push_back(sfeats(s_t1t, "T1tttt(1500,100), 200<MET<400", 2,1,"met>200&&met<400"));
83  Samples.push_back(sfeats(s_t1t, "T1tttt(1500,100), MET#geq400", 2,2,"met>=400"));
84  Samples.push_back(sfeats(s_t1tc, "T1tttt(1200,800), 200<MET<400", 4,1,"met>200&&met<400"));
85  Samples.push_back(sfeats(s_t1tc, "T1tttt(1200,800), MET#geq400", 4,2,"met>=400"));
86 
87  for(unsigned sam(0); sam < Samples.size(); sam++){
88  chain.push_back(new TChain("tree"));
89  for(unsigned insam(0); insam < Samples[sam].file.size(); insam++)
90  chain[sam]->Add(Samples[sam].file[insam]);
91  }
92 
93  vector<int> all_sam;
94  all_sam.push_back(0);
95  all_sam.push_back(1);
96  all_sam.push_back(2);
97  all_sam.push_back(3);
98  all_sam.push_back(4);
99  all_sam.push_back(5);
100  all_sam.push_back(6);
101  all_sam.push_back(7);
102 
103  TString cuts_1b("ht>500&&met>200&&nbm==1&&njets>=6&&mt>=150&&(nmus+nels)==1");
104  TString cuts_2b("ht>500&&met>200&&nbm>=2&&njets>=6&&mt>=150&&(nmus+nels)==1");
105  //int mj_nbins = 6;
106  //float mj_binning[] = {0,200,300,400,500,600,1500};
107  int mj_nbins = 15;
108  float mj_binning[] = {0,100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500};
109  // int met_nbins = 16;
110  // float met_binning[] = {200,250,300,350,400,450,500,550,600,650,700,750,800,850,900,950,1000};
111  // vars.push_back(hfeats("mj",mj_nbins,mj_binning, all_sam, "M_{J} (GeV)", cuts_1b,600));
112  // vars.push_back(hfeats("mj",mj_nbins,mj_binning, all_sam, "M_{J} (GeV)", cuts_2b,600));
113  // vars.push_back(hfeats("met",met_nbins,met_binning, all_sam, "MET (GeV)",cuts_1b,400));
114  // vars.push_back(hfeats("met",met_nbins,met_binning, all_sam, "MET (GeV)",cuts_2b,400));
115 
116  vector<int> slices;
117  slices.push_back(8);
118  slices.push_back(9);
119  slices.push_back(10);
120  slices.push_back(11);
121  slices.push_back(12);
122  slices.push_back(13);
123  vars.push_back(hfeats("mj",mj_nbins,mj_binning, slices, "M_{J} (GeV)", cuts_1b));
124  vars.push_back(hfeats("mj",mj_nbins,mj_binning, slices, "M_{J} (GeV)", cuts_2b));
125 
126 
127  vector<int> bkg_ind(vars.size(), -1);
128 
129  float binW = 100; // Entries normalized to bin width binW
130 
131  TString luminosity="4";
132  float minLog = 0.04, maxLog = 10;
133  double legX = 0.37, legY = 0.87, legSingle = 0.055;
134  double legW = 0.12, legH = legSingle*vars[0].samples.size();
135  TLegend leg(legX, legY-legH, legX+legW, legY);
136  leg.SetTextSize(0.052); leg.SetFillColor(0); leg.SetFillStyle(0); leg.SetBorderSize(0);
137  leg.SetTextFont(132);
138 
139  TLine line; line.SetLineColor(28); line.SetLineWidth(4); line.SetLineStyle(2);
140  vector< vector<TH1D*> > histo[2];
141  vector<TH1D*> varhisto;
142  vector<float> nentries;
143  TString hname, pname, variable, leghisto, totCut, title, ytitle;
144  for(unsigned var(0); var<vars.size(); var++){
145  cout<<endl;
146  // Generating vector of histograms
147  title = vars[var].cuts; if(title=="1") title = "";
148  title.ReplaceAll("nvmus==1&&nmus==1&&nvels==0","1 #mu");
149  title.ReplaceAll("(nmus+nels)","n_{lep}");
150  title.ReplaceAll("nvmus10==0&&nvels10==0", "0 leptons");
151  title.ReplaceAll("(Max$(els_pt*els_sigid*(els_miniso_tr10<0.1))>20||Max$(mus_pt*mus_sigid*(mus_miniso_tr10<0.4))>20)&&(nmus+nels)", "n_{lep}"); title.ReplaceAll("njets30","n_{jets}^{30}");
152  title.ReplaceAll("els_pt","p^{e}_{T}");title.ReplaceAll("mus_pt","p^{#mu}_{T}");
153  title.ReplaceAll("mus_reliso","RelIso"); title.ReplaceAll("els_reliso","RelIso");
154  title.ReplaceAll("mus_miniso_tr15","MiniIso"); title.ReplaceAll("els_miniso_tr15","MiniIso");
155  title.ReplaceAll("njets","n_{jets}");title.ReplaceAll("abs(lep_id)==13&&","");
156  title.ReplaceAll(">=", " #geq "); title.ReplaceAll(">", " > "); title.ReplaceAll("&&", ", ");
157  title.ReplaceAll("<", " < ");
158  title.ReplaceAll("met", "MET"); title.ReplaceAll("ht", "H_{T}"); title.ReplaceAll("mt", "m_{T}");
159  title.ReplaceAll("nleps==1", "1 lepton"); title.ReplaceAll("nbm","n_{b}"); title.ReplaceAll("==", " = ");
160  title.ReplaceAll("nbl[1]","n_{b,l}");
161  for(unsigned his(0); his < 2; his++){
162  varhisto.resize(0);
163  for(unsigned sam(0); sam < vars[var].samples.size(); sam++){
164  hname = "histo"; hname += var; hname += his; hname += sam;
165  varhisto.push_back(new TH1D(hname, title, vars[var].nbins, vars[var].binning));
166  }
167  histo[his].push_back(varhisto);
168  }
169 
171  leg.Clear();
172  nentries.resize(0);
173  variable = vars[var].varname;
174  float maxhisto(-999);
175  for(unsigned sam(0); sam < vars[var].samples.size(); sam++){
176  int isam = vars[var].samples[sam];
177  bool isSig = Samples[isam].isSig;
178  totCut = Samples[isam].factor+"*"+luminosity+"*weight*("+vars[var].cuts+"&&"+Samples[isam].cut+")";
179  //cout<<totCut<<endl;
180  histo[0][var][sam]->Sumw2();
181  chain[isam]->Project(histo[0][var][sam]->GetName(), variable, totCut);
182  histo[0][var][sam]->SetBinContent(vars[var].nbins,
183  histo[0][var][sam]->GetBinContent(vars[var].nbins)+
184  histo[0][var][sam]->GetBinContent(vars[var].nbins+1));
185  histo[0][var][sam]->SetBinError(vars[var].nbins,
186  sqrt(pow(histo[0][var][sam]->GetBinError(vars[var].nbins),2)+
187  pow(histo[0][var][sam]->GetBinError(vars[var].nbins+1),2)));
188  nentries.push_back(histo[0][var][sam]->Integral(1,vars[var].nbins));
189  histo[0][var][sam]->SetXTitle(vars[var].title);
190  ytitle = "Entries for "+luminosity+" fb^{-1}";
191  if(vars[var].unit!="") {
192  int digits(0);
193  float binwidth((vars[var].maxx-vars[var].minx)/static_cast<float>(vars[var].nbins));
194  //binwidth = binW;
195  if(binwidth<1) digits = 1;
196  ytitle += ("/("+RoundNumber(binwidth,digits) +" "+vars[var].unit+")");
197  }
198  histo[0][var][sam]->SetYTitle(ytitle);
199  // Cloning histos for later
200  for(int bin(0); bin<=histo[0][var][sam]->GetNbinsX()+1; bin++){
201  float val(histo[0][var][sam]->GetBinContent(bin)), errval(histo[0][var][sam]->GetBinError(bin));
202  float thisbinW(histo[0][var][sam]->GetBinWidth(bin));
203  histo[0][var][sam]->SetBinContent(bin, val*binW/thisbinW);
204  histo[0][var][sam]->SetBinError(bin, errval*binW/thisbinW);
205  histo[1][var][sam]->SetBinContent(bin, histo[0][var][sam]->GetBinContent(bin));
206  }
207  if(!isSig){ // Adding previous bkg histos
208  bkg_ind[var] = sam;
209  for(int bsam(sam-1); bsam >= 0; bsam--){
210  histo[0][var][sam]->Add(histo[0][var][bsam]);
211  break;
212  // if(!Samples[vars[var].samples[bsam]].file[0].Contains("T1tttt")){
213  // histo[0][var][sam]->Add(histo[0][var][bsam]);
214  // break;
215  // }
216  }
217  histo[0][var][sam]->SetFillColor(Samples[isam].color);
218  histo[0][var][sam]->SetFillStyle(1001);
219  histo[0][var][sam]->SetLineColor(1);
220  histo[0][var][sam]->SetLineWidth(1);
221  } else {
222  histo[0][var][sam]->SetLineColor(Samples[isam].color);
223  histo[0][var][sam]->SetLineStyle(Samples[isam].style);
224  histo[0][var][sam]->SetLineWidth(3);
225  }
226  if(maxhisto < histo[0][var][sam]->GetMaximum()) maxhisto = histo[0][var][sam]->GetMaximum();
227  } // First loop over samples
228  int firstplotted(-1);
229  for(int sam(vars[var].samples.size()-1); sam >= 0; sam--){
230  int isam = vars[var].samples[sam];
231  leghisto = Samples[isam].label+" [N = " + RoundNumber(nentries[sam],0) + "]";
232  leg.AddEntry(histo[0][var][sam], leghisto);
233  bool isSig = Samples[isam].isSig;
234  if(!isSig){
235  if(firstplotted < 0) {
236  histo[0][var][sam]->Draw("hist");
237  firstplotted = sam;
238  } else histo[0][var][sam]->Draw("hist same");
239  }
240  }
241  for(int sam(vars[var].samples.size()-1); sam >= 0; sam--){
242  int isam = vars[var].samples[sam];
243  bool isSig = Samples[isam].isSig;
244  if(isSig) histo[0][var][sam]->Draw("hist same");
245  }
246  legH = legSingle*vars[var].samples.size(); leg.SetY1NDC(legY-legH);
247  leg.Draw();
248  if(histo[0][var][firstplotted]->GetMinimum() > minLog) histo[0][var][firstplotted]->SetMinimum(minLog);
249  histo[0][var][firstplotted]->SetMinimum(minLog);
250  histo[0][var][firstplotted]->SetMaximum(maxhisto*maxLog);
251  if(variable=="mt" && var==vars.size()-1) {
252  histo[0][var][firstplotted]->SetMinimum(0.2);
253  histo[0][var][firstplotted]->SetMaximum(maxhisto*2);
254  }
255  if(vars[var].cut>0) line.DrawLine(vars[var].cut, 0, vars[var].cut, maxhisto*maxLog);
256  can.SetLogy(1);
257  pname = "plots/1d/log_lumi_"+vars[var].tag+".eps";
258  can.SaveAs(pname);
259  histo[0][var][firstplotted]->SetMinimum(0);
260  histo[0][var][firstplotted]->SetMaximum(maxhisto*1.1);
261  can.SetLogy(0);
262  pname = "plots/1d/lumi_"+vars[var].tag+".eps";
263  can.SaveAs(pname);
264 
266  leg.Clear(); maxhisto = -999;
267  for(unsigned sam(0); sam < vars[var].samples.size(); sam++){
268  int isam = vars[var].samples[sam];
269  histo[1][var][sam]->SetLineColor(Samples[isam].color);
270  histo[1][var][sam]->SetLineStyle(Samples[isam].style);
271  histo[1][var][sam]->SetLineWidth(3);
272  if(nentries[sam]) histo[1][var][sam]->Scale(100./nentries[sam]);
273  if(maxhisto < histo[1][var][sam]->GetMaximum()) maxhisto = histo[1][var][sam]->GetMaximum();
274  if(sam==0){
275  histo[1][var][sam]->SetXTitle(vars[var].title);
276  histo[1][var][sam]->SetYTitle("Entries (%)");
277  histo[1][var][sam]->Draw();
278  } else histo[1][var][sam]->Draw("same");
279  leghisto = Samples[isam].label+" [#mu = ";
280  int digits(0);
281  leghisto += RoundNumber(histo[1][var][sam]->GetMean(),digits) + "]";
282  leg.AddEntry(histo[1][var][sam], leghisto);
283  } // Loop over samples
284  leg.Draw();
285  if(vars[var].cut>0) line.DrawLine(vars[var].cut, 0, vars[var].cut, maxhisto*1.1);
286  histo[1][var][0]->SetMaximum(maxhisto*1.1);
287  can.SetLogy(0);
288  pname = "plots/1d/shapes_"+vars[var].tag+".eps";
289  can.SaveAs(pname);
290  histo[1][var][0]->SetMaximum(maxhisto*maxLog);
291  can.SetLogy(1);
292  pname = "plots/1d/log_shapes_"+vars[var].tag+".eps";
293  can.SaveAs(pname);
294  }// Loop over variables
295 
296  cout<<endl<<endl;
297 
298  //for(unsigned var(0); var<vars.size(); var++){
299  // for(unsigned var(0); var<4; var++){
300  // for(unsigned sam(0); sam < vars[var].samples.size(); sam++){
301  // int isam = vars[var].samples[sam];
302  // bool isSig = Samples[isam].isSig;
303  // if(isSig){
304  // histo[0][var][isam]->Divide(histo[0][var][bkg_ind[var]]);
305  // histo[0][var][isam]->SetYTitle("S/B");
306  // histo[0][var][isam]->SetMarkerStyle(20);
307  // histo[0][var][isam]->SetMarkerColor(2);
308  // histo[0][var][isam]->Draw();
309  // histo[0][var][isam]->SetMinimum(0);
310  // histo[0][var][isam]->SetMaximum(3.3);
311  // line.DrawLine(0,1,mj_binning[mj_nbins],1);
312  // can.SetLogy(0);
313  // pname = "plots/1d/sbratio_"+Samples[isam].label+vars[var].tag+".eps";
314  // pname.ReplaceAll("(",""); pname.ReplaceAll(")",""); pname.ReplaceAll(",","");
315  // can.SaveAs(pname);
316 
317  // }
318  // } // Loop over samples
319  // }// Loop over variables
320 
321  for(unsigned his(0); his < 2; his++){
322  for(unsigned var(0); var<vars.size(); var++){
323  for(unsigned sam(0); sam < vars[var].samples.size(); sam++)
324  if(histo[his][var][sam]) histo[his][var][sam]->Delete();
325  }
326  }
327 }
328 
TColor tcu_purple(2005, 52/255., 42/255., 123/255.)
TColor ucsb_gold(2001, 255/255., 200/255., 47/255)
void setDefaultStyle()
Definition: styles.cpp:36
TColor uf_orange(2003, 255/255., 74/255., 0/255.)
TColor tar_heel_blue(2006, 86/255., 160/255., 211/255.)
STL namespace.
TColor ucsb_blue(2000, 1/255., 57/255., 166/255.)
TString luminosity
int main()
Definition: plot_mj_met.cxx:23
TString RoundNumber(double num, int decimals, double denom=1.)
Definition: utilities.cpp:191
TColor sig_gold(2008, 215/255., 162/255., 50/255.)
tuple file
Definition: parse_card.py:238
TColor uo_green(2004, 0/255., 79/255., 39/255.)
TColor penn_red(2002, 149/255., 0/255., 26/255.)
TColor seal_brown(2010, 89/255., 38/255., 11/255.)
TColor sig_teal(2007, 96/255., 159/255., 128/255.)