ra4_macros  bede988c286599a3a84b77a4d788ac0a971e89f9
plot_ratios.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include <vector>
3 
4 #include "TString.h"
5 #include "TGraphErrors.h"
6 
7 #include "TChain.h"
8 #include "TH1D.h"
9 #include "TCanvas.h"
10 #include "TLegend.h"
11 #include "TLine.h"
12 #include "TColor.h"
13 #include "TMath.h"
14 #include "TF1.h"
15 #include "TLatex.h"
16 
17 #include "styles.hpp"
18 #include "utilities.hpp"
19 #include "utilities_macros.hpp"
20 
21 using namespace std;
22 
23 void GetKappa(double &kappa,double &error, TString baseline,TString external,TString internal_cut,TString ratio_cut, sfeats Sample,TString luminosity);
24 void GetRatio(double &ratio,double &error, TString baseline,TString external,TString internal_cut,TString ratio_cut, sfeats Sample, TString luminosity);
25 void MakeGraph(TString baseline,vector<TString> external,TString internal_cut, TString ratio_cut, vector<sfeats> samples, vector<TString> binlabels, TString luminosity);
26 
27 int main(){
28  TString luminosity = "10";
29  TString folder="/cms5r0/ald77/archive/2015_05_21/skim/";
30  TString folder_noskim="/cms5r0/ald77/archive/2015_05_21/";
31  vector<TString> s_t1t;
32  s_t1t.push_back(folder+"*T1tttt*1500_*PU20*");
33  vector<TString> s_t1tc;
34  s_t1tc.push_back(folder+"*T1tttt*1200_*PU20*");
35  vector<TString> s_tt;
36  s_tt.push_back(folder+"*_TTJet*");
37  vector<TString> s_tt_noskim;
38  s_tt_noskim.push_back(folder_noskim+"*_TTJet*12.root");
39  vector<TString> s_wjets;
40  s_wjets.push_back(folder+"*WJetsToLNu_HT*");
41  vector<TString> s_singlet;
42  s_singlet.push_back(folder+"*_T*channel*");
43  vector<TString> s_ttv;
44  s_ttv.push_back(folder+"*TTW*");
45  s_ttv.push_back(folder+"*TTZ*");
46  vector<TString> s_other;
47  s_other.push_back(folder+"*QCD_HT*");
48  s_other.push_back(folder+"*_ZJet*");
49  s_other.push_back(folder+"*DY*");
50  s_other.push_back(folder+"*WH_HToBB*");
51 
52  // Reading ntuples
53  vector<sfeats> Samples;
54  Samples.push_back(sfeats(s_t1t, "T1tttt(1500,100)", ra4::c_t1tttt));//0
55  Samples.push_back(sfeats(s_t1tc, "T1tttt(1200,800)", ra4::c_t1tttt,2));//1
56  Samples.push_back(sfeats(s_tt, "t#bar{t}, 1 l", ra4::c_tt_1l, 1,"ntruleps<=1"));//2
57  Samples.push_back(sfeats(s_tt, "t#bar{t}, 2 l", ra4::c_tt_2l,1,"ntruleps>=2"));//3
58  Samples.push_back(sfeats(s_wjets, "W+jets", ra4::c_wjets));//4
59  Samples.push_back(sfeats(s_singlet, "Single t", ra4::c_singlet));//5
60  Samples.push_back(sfeats(s_ttv, "ttV", ra4::c_ttv));//6
61  Samples.push_back(sfeats(s_other, "Other", ra4::c_other));//7
62  Samples.push_back(sfeats(s_tt_noskim, "t#bar{t}", ra4::c_tt_1l,1));//8
63 
64  Samples.push_back(sfeats(s_tt, "t#bar{t} 5j", 1, 1,"njets==5"));//9
65  Samples.push_back(sfeats(s_tt, "t#bar{t} 6j", 2, 1,"njets==6"));//10
66  Samples.push_back(sfeats(s_tt, "t#bar{t} 7j", 4, 1,"njets==7"));//11
67  Samples.push_back(sfeats(s_tt, "t#bar{t} 8j", 8, 1,"njets==8"));//12
68  Samples.push_back(sfeats(s_tt, "t#bar{t} 9+j", 15, 1,"njets>=9"));//13
69 
70  Samples.push_back(sfeats(s_tt, "t#bar{t}", 15, 1));//14
71 
72 
73  vector<int> ra4_sam;
74  ra4_sam.push_back(0);
75  ra4_sam.push_back(1);
76  ra4_sam.push_back(2);
77  ra4_sam.push_back(3);
78  ra4_sam.push_back(4);
79  ra4_sam.push_back(5);
80  ra4_sam.push_back(6);
81  ra4_sam.push_back(7);
82 
83  vector<int> ra4_tt_t1;
84  ra4_tt_t1.push_back(0);
85  ra4_tt_t1.push_back(1);
86  ra4_tt_t1.push_back(2);
87 
88  // const int nR=5;
89  TString baseline = "ht>500&&met>200&&nbm>=2&&(nmus+nels)==1";
90 
91  vector<TString> externalvec;
92  externalvec.push_back("njets>=6&&njets<=7&&met>200&&met<=400&&nbm==2");
93  externalvec.push_back("njets>=6&&njets<=7&&met>200&&met<=400&&nbm>=3");
94  externalvec.push_back("njets>=6&&njets<=7&&met>400&&nbm>=2");
95  externalvec.push_back("njets>=8&&met>200&&met<=400&&nbm==2");
96  externalvec.push_back("njets>=8&&met>200&&met<=400&&nbm>=3");
97  externalvec.push_back("njets>=8&&met>400&&nbm>=2");
98 
99  TString internal_cut = "mj>400";
100 
101  TString ratio_cut = "mt>140";
102 
103  vector<sfeats> samples_MJ;
104  samples_MJ.push_back(sfeats(s_tt, "t#bar{t}", 31, 1));
105 
106  vector<TString> binlabels; //push back twice for each njets bin
107  binlabels.push_back("n_{B}=2, low MET");binlabels.push_back("n_{B}=3+, low MET");binlabels.push_back("n_{B}=2+, high MET");
108  binlabels.push_back("n_{B}=2, low MET");binlabels.push_back("n_{B}=3+, low MET");binlabels.push_back("n_{B}=2+, high MET");
109 
110  MakeGraph(baseline,externalvec,internal_cut,ratio_cut,samples_MJ,binlabels,luminosity);
111 }
112 
113 void MakeGraph(TString baseline,vector<TString> external,TString internal_cut,TString ratio_cut, vector<sfeats> samples, vector<TString> binlabels,TString luminosity){
114 
115  TString not_internal_cut = invertcut(internal_cut);
116 
117  int nsamples = samples.size();
118  vector<TGraphErrors*> graphs;
119  TGraphErrors* kappas;
120  float max = 0.01;
121  int nR = static_cast<int>(external.size());
122  vector<double> x;
123  vector<double> x_err;
124 
125  for(int isam = 0; isam<nsamples; isam++){
126  vector<double> R;
127  vector<double> R_err;
128 
129  for(int iR=0;iR<nR;iR++){ //For one internal_cut
130  double rholder;
131  double r_errholder;
132 
133  GetRatio(rholder,r_errholder,baseline,external[iR],internal_cut,ratio_cut,samples.at(isam),luminosity);
134  R.push_back(rholder);
135  R_err.push_back(r_errholder);
136 
137  x.push_back(iR);//=iR; //just a placeholder for a label
138  x_err.push_back(0);//x_err[iR]=0;
139  }
140 
141  TGraphErrors *g1 = new TGraphErrors(nR,&x[0],&R[0],&x_err[0],&R_err[0]); // &foo[0] convert vector "foo" to an array because TGraphErrors only accepts arrays
142  graphs.push_back(g1);
143 
144  R.clear();
145  R_err.clear();
146 
147  for(int iR=0;iR<nR;iR++){ //For the inverse internal_cut
148  double rholder;
149  double r_errholder;
150 
151  GetRatio(rholder,r_errholder,baseline,external[iR],not_internal_cut,ratio_cut,samples.at(isam),luminosity);
152  R.push_back(rholder);
153  R_err.push_back(r_errholder);
154 
155  x.push_back(iR);//=iR; //just a placeholder for a label
156  x_err.push_back(0);//x_err[iR]=0;
157  }
158 
159  TGraphErrors *g2 = new TGraphErrors(nR,&x[0],&R[0],&x_err[0],&R_err[0]); // &foo[0] converts vector "foo" to an array because TGraphErrors only accepts arrays
160  graphs.push_back(g2);
161 
162  for(int imax = 0;imax<nR;imax++){
163  if(R[imax]>max) max=R[imax];
164  }
165  }
166  vector<double> kappa;
167  vector<double> kappa_err;
168  for(int iR=0;iR<nR;iR++){
169  double kholder;
170  double k_errholder;
171 
172  GetKappa(kholder,k_errholder,baseline,external[iR],internal_cut,ratio_cut,samples.at(0),luminosity);
173  kappa.push_back(kholder);
174  kappa_err.push_back(k_errholder);
175  }
176 
177  kappas = new TGraphErrors(nR,&x[0],&kappa[0],&x_err[0],&kappa_err[0]);
178 
179  styles style("RA4"); style.setDefaultStyle();
180  TCanvas can;
181  //TPad *pad = static_cast<TPad *>(can.cd());
182  TString plot_tag("_lumi"+luminosity+".eps");
183 
184  //make hist to define pad with labels on x-axis
185  TH1F *h = new TH1F("for_axis_label",cuts2title(baseline),nR,-0.5,nR-0.5);
186  for (int ih=1;ih<=nR;ih++){ h->GetXaxis()->SetBinLabel(ih,cuts2title(external[ih-1]));
187  if(external[0].Contains("nbm")) h->GetXaxis()->SetBinLabel(ih,binlabels[ih-1]);
188  // if(external[0].Contains("const")) h->GetXaxis()->SetBinLabel(ih,binlabels3[ih-1]);
189  }
190  //h->SetMaximum(1.5*max);
191  h->SetMaximum(1.5*max);
192  h->SetMinimum(0.0);
193  if(ratio_cut.Contains("mt")){
194  h->GetYaxis()->SetTitle("R_{mT}");
195  }
196  else {if(ratio_cut.Contains("400")) h->GetYaxis()->SetTitle("R_{MJ 400}");
197  if(ratio_cut.Contains("500")) h->GetYaxis()->SetTitle("R_{MJ 500}");
198  if(ratio_cut.Contains("600")) h->GetYaxis()->SetTitle("R_{MJ 600}");
199  }
200  h->GetXaxis()->SetLabelSize(0.03);
201  h->Draw();
202 
203  double legX = 0.65, legY = 0.89, legSingle = 0.14;
204  double legW = 0.22, legH = legSingle;
205  TLegend leg(legX, legY-legH, legX+legW, legY);
206  leg.SetTextSize(0.057); leg.SetFillColor(0); leg.SetFillStyle(0); leg.SetBorderSize(0);
207  leg.SetTextFont(132);
208 
209  for(int ig = 0;ig<nsamples;ig++){
210  graphs.at(0)->SetMarkerStyle(20);
211  graphs.at(0)->SetMarkerColor(31);
212  graphs.at(0)->SetLineColor(31);
213  graphs.at(0)->Draw("PZ");
214  leg.AddEntry(graphs.at(0), samples.at(ig).label+" "+cuts2title(internal_cut),"p");
215 
216  graphs.at(1)->SetMarkerStyle(20);
217  graphs.at(1)->SetMarkerColor(46);
218  graphs.at(1)->SetLineColor(46);
219  graphs.at(1)->Draw("PZ");
220  leg.AddEntry(graphs.at(1), samples.at(ig).label+" "+cuts2title(not_internal_cut),"p");
221  }
222 
223  leg.Draw("p");
224 
225  TLine line; line.SetLineColor(28); line.SetLineWidth(4); line.SetLineStyle(2);
226  if(external[0].Contains("nbm")){ line.DrawLine(2.5, 0, 2.5, 1.5*max);
227  TLatex *text67 = new TLatex(0.35,0.03,"n_{jets}= 6-7");
228  text67->SetNDC();
229  text67->SetTextSize(0.03);
230  text67->SetLineWidth(2);
231  text67->Draw();
232  TLatex *text8 = new TLatex(0.7,0.03,"n_{jets}#geq 8");
233  text8->SetNDC();
234  text8->SetTextSize(0.03);
235  text8->SetLineWidth(2);
236  text8->Draw();
237 
238  }
239 
240  TString pname1 = "plots/ratios/ratios_"+format_tag(ratio_cut+samples.at(0).cut+external[0]+baseline)+".eps";
241  can.SaveAs(pname1);
242 
243  graphs.at(0)->Delete();
244  graphs.at(1)->Delete();
245 
246  //now plot Kappa
247  TCanvas can2;
248 
249  //make hist to define pad with labels on x-axis
250  h->SetMaximum(2.0);
251  h->GetYaxis()->SetTitle("\\kappa");
252  h->Draw();
253 
254  kappas->SetMarkerStyle(20);
255  kappas->SetMarkerColor(kBlack);
256  kappas->SetLineColor(kBlack);
257  kappas->Draw("PZ");
258  line.DrawLine(h->GetBinLowEdge(1), 1, h->GetBinLowEdge(h->GetNbinsX()+1), 1);
259 
260  if(external[0].Contains("nbm")){ line.DrawLine(2.5, 0, 2.5, 1.5*max);
261  TLatex *text67 = new TLatex(0.35,0.03,"n_{jets}= 6-7");
262  text67->SetNDC();
263  text67->SetTextSize(0.03);
264  text67->SetLineWidth(2);
265  text67->Draw();
266  TLatex *text8 = new TLatex(0.7,0.03,"n_{jets}= 8");
267  text8->SetNDC();
268  text8->SetTextSize(0.03);
269  text8->SetLineWidth(2);
270  text8->Draw();
271  }
272 
273  TString pname2 = "plots/ratios/kappa_"+format_tag(ratio_cut+samples.at(0).cut+external[0]+baseline)+".eps";
274  can2.SaveAs(pname2);
275  h->Delete();
276 }
277 
278 void GetRatio(double &ratio,double &error, TString baseline,TString external,TString internal_cut,TString ratio_cut, sfeats Sample,TString luminosity){
279 
280  TChain * chain = new TChain("tree");
281  for(unsigned insam(0); insam < Sample.file.size(); insam++)
282  chain->Add(Sample.file[insam]);
283 
284  TH1D h_numerator("num", "",100, 0, 10000);
285  h_numerator.Sumw2();
286  TH1D h_denominator("den", "",100, 0, 10000);
287  h_denominator.Sumw2();
288  TH1D h_weight("weight", "",2000, 0, 2);
289  h_weight.Sumw2();
290 
291  TString numCut = luminosity+"*weight*("+baseline+"&&"+external+"&&"+internal_cut+"&&"+ratio_cut+"&&"+Sample.cut+")";
292  TString denCut = luminosity+"*weight*("+baseline+"&&"+external+"&&"+internal_cut+"&&!("+ratio_cut+")&&"+Sample.cut+")";
293 
294  chain->Project("num", "met", numCut);
295  chain->Project("den", "met", denCut);
296  chain->Project("weight", "weight*"+luminosity);
297 
298  double weight = h_weight.GetMean(); // Really shitty way of getting the weight. Only works if all the weights are the same.
299 
300  double numerator, denominator,numerator_err, denominator_err;
301  numerator = h_numerator.IntegralAndError(0,101,numerator_err);
302  denominator = h_denominator.IntegralAndError(0,101,denominator_err);
303 
304  if(numerator==0 && denominator==0){
305  ratio = 0;
306  error = 0;
307  }
308  else if(numerator==0){ // Error on zero is ~1 event
309  numerator = 0;
310  numerator_err = weight;
311  }
312  else if(denominator==0){ //Pretend as if you saw 1 event.
313  denominator = weight*1.;
314  denominator_err = weight;
315  }
316  ratio = numerator/denominator;
317  error = sqrt(pow(numerator_err/denominator,2)+pow(numerator/pow(denominator,2)*denominator_err,2));
318 }
319 
320 void GetKappa(double &kappa,double &error, TString baseline,TString external,TString internal_cut,TString ratio_cut, sfeats Sample,TString luminosity){
321 
322  if( !(external.Contains(">" )&& internal_cut.Contains(">")) )
323  cout<<"WARNING: GetKappa needs \"external\" and \"internal_cut\" to use a \">\" sign"<<endl;
324 
325  TChain * chain = new TChain("tree");
326  for(unsigned insam(0); insam < Sample.file.size(); insam++)
327  chain->Add(Sample.file[insam]);
328 
329  // Note: These yields numberings do not correspond to the regions
330  TH1D h_yields1("yields1", "",100, 0, 10000);
331  h_yields1.Sumw2();
332  TH1D h_yields2("yields2", "",100, 0, 10000);
333  h_yields2.Sumw2();
334  TH1D h_yields3("yields3", "",100, 0, 10000);
335  h_yields3.Sumw2();
336  TH1D h_yields4("yields4", "",100, 0, 10000);
337  h_yields4.Sumw2();
338  TH1D h_weight("weight", "",2000, 0, 2);
339  h_weight.Sumw2();
340 
341  TString yields1Cut = luminosity+"*weight*("+baseline+"&&"+external+"&&!("+internal_cut+")&&!("+ratio_cut+")&&"+Sample.cut+")";
342  TString yields2Cut = luminosity+"*weight*("+baseline+"&&"+external+"&&!("+internal_cut+")&&"+ratio_cut+"&&"+Sample.cut+")";
343  TString yields3Cut = luminosity+"*weight*("+baseline+"&&"+external+"&&"+internal_cut+"&&!("+ratio_cut+")&&"+Sample.cut+")";
344  TString yields4Cut = luminosity+"*weight*("+baseline+"&&"+external+"&&"+internal_cut+"&&"+ratio_cut+"&&"+Sample.cut+")";
345 
346  chain->Project("yields1", "met", yields1Cut);
347  chain->Project("yields2", "met", yields2Cut);
348  chain->Project("yields3", "met", yields3Cut);
349  chain->Project("yields4", "met", yields4Cut);
350  chain->Project("weight", "weight*"+luminosity);
351 
352  double weight = h_weight.GetMean(); // This method could be improved. Only works if all the weights are the same.
353 
354  double n1, n2, n3, n4, n1_err, n2_err, n3_err, n4_err;
355  n1 = h_yields1.IntegralAndError(0,101,n1_err);
356  n2 = h_yields2.IntegralAndError(0,101,n2_err);
357  n3 = h_yields3.IntegralAndError(0,101,n3_err);
358  n4 = h_yields4.IntegralAndError(0,101,n4_err);
359 
360  int nzeroes = 0;
361  if(n1==0) nzeroes++;
362  if(n2==0) nzeroes++;
363  if(n3==0) nzeroes++;
364  if(n4==0) nzeroes++;
365 
366  if(nzeroes>=2){ // If more than 1 zero, don't calculate since one R-factor is indeterminate
367  kappa = 0;
368  error = 0;
369  }
370  else if(n1==0){ // Error on zero is ~1 event
371  n1 = 0;
372  n1_err = weight;
373  }
374  else if(n4==0){ // Error on zero is ~1 event
375  n4 = 0;
376  n4_err = weight;
377  }
378  else if(n2==0){ // Pretend as if you saw 1 event
379  n2 = weight*1;
380  n2_err = weight;
381  }
382  else if(n3==0){ // Pretend as if you saw 1 event
383  n3 = weight*1;
384  n3_err = weight;
385  }
386  kappa = (n4*n1)/(n3*n2);
387  error = sqrt(pow((n4/(n2*n3))*n1_err,2)+pow((n1/(n2*n3))*n4_err,2)+pow(((n1*n4)/(pow(n2,2)*n3))*n2_err,2)+pow(((n1*n4)/(n2*pow(n3,2)))*n3_err,2));
388 }
void GetRatio(double &ratio, double &error, TString baseline, TString external, TString internal_cut, TString ratio_cut, sfeats Sample, TString luminosity)
int main()
Definition: plot_ratios.cxx:27
void setDefaultStyle()
Definition: styles.cpp:36
bool Contains(const std::string &text, const std::string &pattern)
TString cuts2title(TString title)
STL namespace.
void GetKappa(double &kappa, double &error, TString baseline, TString external, TString internal_cut, TString ratio_cut, sfeats Sample, TString luminosity)
TString luminosity
TString format_tag(TString tag)
std::vector< TString > file
tuple kappa
Definition: parse_card.py:264
void MakeGraph(TString baseline, vector< TString > external, TString internal_cut, TString ratio_cut, vector< sfeats > samples, vector< TString > binlabels, TString luminosity)
TString cut
TString invertcut(TString cut)