ra4_macros  bede988c286599a3a84b77a4d788ac0a971e89f9
plot_rmt_njet_gamma.cxx
Go to the documentation of this file.
1 // plot_kappa: Plots kappa for the different fitting methods.
2 // Uncertainties found fluctuating yields with Gamma distributions
3 
4 #include <fstream>
5 #include <iostream>
6 #include <vector>
7 #include <ctime>
8 
9 #include <unistd.h>
10 #include <getopt.h>
11 
12 #include "TMath.h"
13 #include "TChain.h"
14 #include "TH1D.h"
15 #include "TCanvas.h"
16 #include "TLegend.h"
17 #include "TLine.h"
18 #include "TString.h"
19 #include "TLatex.h"
20 #include "TArrow.h"
21 #include "TGraphAsymmErrors.h"
22 
23 #include "styles.hpp"
24 #include "utilities.hpp"
25 #include "utilities_macros.hpp"
26 
27 namespace {
28  TString ntuple_date("2015_09_14");
29  TString lumi("10");
30  int nrep = 100000; // Fluctuations of Gamma distribution
31  bool do_2l(false); // Calculate dilepton closure stat uncertainty
32  bool do_1ltt(false); // Kappa just for 1l ttbar
33  bool do_2ltt(false); // Kappa just for 2l ttbar
34  bool do_ttbar(true); // Include ttbar in kappa
35  bool do_mj200(false);
36  TString plot_type=".pdf";
37  bool do_sigma_avError(true);
38 
39  TString mjthresh("400");
40 }
41 
42 using namespace std;
43 
44 void plotKappa(vector<vector<double> > &vx, vector<vector<double> > &vy, vector<vector<double> > &vexl,
45  vector<vector<double> > &vexh, vector<vector<double> > &veyl, vector<vector<double> > &veyh,
46  unsigned idata, TH1D &histo, vector<TString> &nbcuts);
47 int main(int argc, char *argv[]){
48  float time_setup(0.), time_ntu(0.), time_gen(0.);
49  time_t begtime, endtime;
50  time(&begtime);
51 
52  int c(0);
53  while((c=getopt(argc, argv, "m:n:to12j"))!=-1){
54  switch(c){
55  case 'm':
56  mjthresh=optarg;
57  break;
58  case 'n':
59  nrep=atoi(optarg);
60  break;
61  case 't':
62  do_ttbar = true;
63  break;
64  case '1':
65  do_1ltt = true;
66  break;
67  case '2':
68  do_2ltt = true;
69  break;
70  case 'j':
71  do_mj200 = true;
72  break;
73  default:
74  break;
75  }
76  }
77 
78  styles style("RA4long"); //style.LabelSize = 0.05;
79  style.setDefaultStyle();
80 
81  TString folder="/cms5r0/rohan/archive/"+ntuple_date+"/skim/";
82  TString folder_1l="/cms5r0/rohan/archive/"+ntuple_date+"/skim_1l/";
83  TString folder_2l="/cms5r0/rohan/archive/"+ntuple_date+"/skim_2l/";
84  TString folder_genht="/cms5r0/rohan/archive/"+ntuple_date+"/skim_genht/";
85 
86  vector<TString> s_tt;
87  if(do_1ltt) {
88  s_tt.push_back(folder_genht+"*_TTJets*SingleLept*");
89  s_tt.push_back(folder_1l+"*_TTJets*HT*");
90  }
91  else if(do_2ltt){
92  s_tt.push_back(folder_genht+"*_TTJets*DiLept*");
93  s_tt.push_back(folder_2l+"*_TTJets*HT*");
94  }
95  else{
96  s_tt.push_back(folder_genht+"*_TTJets*Lept*");
97  s_tt.push_back(folder+"*_TTJets*HT*");
98  }
99  // Reading ntuples
100  vector<sfeats> Samples;
101  Samples.push_back(sfeats(s_tt, "t#bar{t}", 46,1));
102 
103  // Adding non-skimmed samples
104  vector<int> ra4_sam, ra4_sam_ns;
105  unsigned nsam(Samples.size());
106  for(unsigned sam(0); sam < nsam; sam++){
107  ra4_sam.push_back(sam);
108  } // Loop over samples
109 
110  // Reading ntuples
111  vector<TChain *> chain;
112  for(unsigned sam(0); sam < Samples.size(); sam++){
113  chain.push_back(new TChain("tree"));
114  for(unsigned insam(0); insam < Samples[sam].file.size(); insam++)
115  chain[sam]->Add(Samples[sam].file[insam]);
116  }
117 
118  float mSigma, pSigma;
119  vector<float> powersk;
120  vector<TString> cuts;
121  if(!do_2l){
122  powersk.push_back(-1); cuts.push_back("mt<=140");
123  powersk.push_back(1); cuts.push_back("mt>140");
124  } else {
125  powersk.push_back(-1); cuts.push_back("mj<="+mjthresh); // R3
126  powersk.push_back(1); cuts.push_back("mj>"+mjthresh); // R4
127  }
128 
129  TString baseline("ht>500&&met>200&&(nmus+nels)==1");
130  if(do_mj200) baseline += "&&mj>200";
131  if(do_2l) baseline = "(nmus+nels)==2&&ht>500&&met>200&&njets>=6&&nbm>=1";
132  vector<TString> mjcuts, njcuts, nbcuts, mjnames, njnames;
133  mjcuts.push_back("mj<="+mjthresh);
134  mjcuts.push_back("mj>"+mjthresh);
135  if(!do_2l){
136  njcuts.push_back("njets==4");
137  njcuts.push_back("njets==5");
138  njcuts.push_back("njets==6");
139  njcuts.push_back("njets==7");
140  njcuts.push_back("njets==8");
141  njcuts.push_back("njets==9");
142  njcuts.push_back("njets==10");
143  njcuts.push_back("njets>=11");
144  //njcuts.push_back("njets==11");
145  // njcuts.push_back("njets>=12");
146  } else {
147  njcuts.push_back("njets<=7");
148  njcuts.push_back("njets>=8");
149  }
150  if(do_2l) nbcuts.push_back("nbm<=2");
151  else {
152  nbcuts.push_back("nbm==1");
153  nbcuts.push_back("nbm>=2");
154  //nbcuts.push_back("nbm==2");
155  //nbcuts.push_back("nbm>=3");
156  }
157  for(unsigned imj(0); imj<mjcuts.size(); imj++){
158  mjnames.push_back(mjcuts[imj]);
159  mjnames[imj].ReplaceAll("mj","M_{J}");
160  mjnames[imj].ReplaceAll("<="," #leq ");
161  mjnames[imj].ReplaceAll("=="," = ");
162  mjnames[imj].ReplaceAll(">"," > ");
163  }
164 
165  for(unsigned inj(0); inj<njcuts.size(); inj++){
166  njnames.push_back(njcuts[inj]);
167  njnames[inj].ReplaceAll("njets","n_{j}");
168  njnames[inj].ReplaceAll(">="," #geq ");
169  njnames[inj].ReplaceAll("=="," = ");
170  }
171 
172  float minh(0), maxh(mjcuts.size()*njcuts.size()), wtot(maxh-minh);
173  float wmj(wtot/static_cast<float>(mjcuts.size()));
174  float wnj(wmj/static_cast<float>(njcuts.size()));
175  float wnb(wnj/static_cast<float>(nbcuts.size()+4));
176  // These vectors have indices vx[4][nbsize][njsize*metsize]
177  // The first index is: 0 -> k MC, 1 -> k data, 2 -> N4 MC, 3 -> N4 data
178  vector<vector<vector<double> > > vx, vy, vexl, vexh, veyl, veyh;
179  for(unsigned idata(0); idata<4; idata++){
180  vx.push_back (vector<vector<double> >());
181  vy.push_back (vector<vector<double> >());
182  vexl.push_back(vector<vector<double> >());
183  vexh.push_back(vector<vector<double> >());
184  veyl.push_back(vector<vector<double> >());
185  veyh.push_back(vector<vector<double> >());
186  for(unsigned inb(0); inb<nbcuts.size(); inb++){
187  vx[idata].push_back (vector<double>());
188  vy[idata].push_back (vector<double>());
189  vexl[idata].push_back(vector<double>());
190  vexh[idata].push_back(vector<double>());
191  veyl[idata].push_back(vector<double>());
192  veyh[idata].push_back(vector<double>());
193  }
194  }
195 
196  time(&endtime); time_setup = difftime(endtime, begtime);
197  time(&begtime);
198 
199  TString totcut("");
200  for(unsigned imj(0); imj<mjcuts.size(); imj++){
201  for(unsigned inj(0); inj<njcuts.size(); inj++){
202  for(unsigned inb(0); inb<nbcuts.size(); inb++){
203  vector<vector<float> > entries;
204  vector<vector<float> > weights;
205  for(unsigned obs(0); obs < powersk.size(); obs++) {
206  entries.push_back(vector<float>());
207  weights.push_back(vector<float>());
208  float yield_singlet(0);
209  for(unsigned sam(0); sam < ra4_sam.size(); sam++) {
210  totcut = (lumi+"*weight*("+baseline+"&&"+nbcuts[inb]+"&&"+mjcuts[imj]+"&&"+cuts[obs]+
211  "&&"+Samples[ra4_sam[sam]].cut);
212  totcut += "&&"+njcuts[inj]+")";
213  cout << totcut<<endl;
214  double yield(0.), sigma(0.), avWeight(1.);
215  int Nentries(0);
216  Nentries = getYieldErr(*chain[ra4_sam[sam]], totcut, yield, sigma);
217  // Zero-ing out the single t, not adding its uncertainty
218  if(Samples[ra4_sam[sam]].label=="Single t"){
219  if(yield>0) yield_singlet = yield;
220  continue;
221  }
222  if(Samples[ra4_sam[sam]].label=="Other") yield += yield_singlet;
223  if(yield<=0) entries[obs].push_back(0);
224  else {
225  if(do_sigma_avError) entries[obs].push_back(yield*yield/pow(sigma,2));
226  else entries[obs].push_back(Nentries);
227  }
228  if(Nentries==0){ // If no entries, find average weight in signal bin
229  totcut = (lumi+"*weight*("+baseline+"&&"+cuts[obs]+")");
230  Nentries = getYieldErr(*chain[ra4_sam[sam]], totcut, yield, sigma);
231  // If no entries, find average weight in baseline region
232  if(Nentries==0){
233  totcut = (lumi+"*weight*("+baseline+")");
234  Nentries = getYieldErr(*chain[ra4_sam[sam]], totcut, yield, sigma);
235  }
236  }
237  if(do_sigma_avError) avWeight = sigma*sigma/yield;
238  else avWeight = fabs(yield/static_cast<double>(Nentries));
239  weights[obs].push_back(avWeight);
240  //cout<<obs<<","<<sam<<": entries "<<entries[obs][sam]<<", weight "<<avWeight<<", yield "<<yield<<endl;
241  } // Loop over samples
242  } // Loop over number of observables going into kappa
243 
244  time(&endtime); time_ntu += difftime(endtime, begtime);
245  time(&begtime);
246  for(unsigned idata(0); idata<1; idata++){
247  if(do_2l && idata>=2) continue;
248  double kappa(0);
249  kappa = calcKappa(entries, weights, powersk, mSigma, pSigma, (idata%2)==1, false);
250  float xpoint = inj*wnj+imj*wmj+(inb+2)*wnb;
251  vx[idata][inb].push_back(xpoint);
252  vy[idata][inb].push_back(kappa);
253  vexl[idata][inb].push_back(0);
254  vexh[idata][inb].push_back(0);
255  veyl[idata][inb].push_back(mSigma);
256  veyh[idata][inb].push_back(pSigma);
257  } // Loop over MC and data
258  time(&endtime); time_gen += difftime(endtime, begtime);
259  time(&begtime);
260  } // Loop over nb cuts
261  } // Loop over met cuts
262  } // Loop over nj cuts
263 
264  vector<unsigned> ind(nbcuts.size(),0);
265  if(do_2l) {
266  for(unsigned imj(0); imj<mjcuts.size(); imj++){
267  for(unsigned inj(0); inj<njcuts.size(); inj++){
268  unsigned inb(0);
269  float MC(vy[0][inb][ind[inb]]), Data(vy[1][inb][ind[inb]]);
270  float epMC(veyh[0][inb][ind[inb]]), emMC(veyl[0][inb][ind[inb]]);
271  float epData(veyh[1][inb][ind[inb]]), emData(veyl[1][inb][ind[inb]]);
272  float epTotal(sqrt(pow(epMC/MC,2)+pow(epData/Data,2))), emTotal(sqrt(pow(emMC/MC,2)+pow(emData/Data,2)));
273 
274  mjcuts[imj].ReplaceAll("mj>200&&","");
275  TString cutname = njcuts[inj]+", "+mjcuts[imj];
276 
277  cout<<endl<<cutname<<": Data +"<<RoundNumber(epData*100,1,Data)<<"% -"<<RoundNumber(emData*100,1,Data)
278  <<"%\t MC +"<<RoundNumber(epMC*100,1,MC)<<"% -"<<RoundNumber(emMC*100,1,MC)
279  <<"% \t Total +"<<RoundNumber(epTotal*100,1)<<"% -"<<RoundNumber(emTotal*100,1)<<endl;
280  ind[inb]++;
281  }
282  } // Loop over nj cuts
283  return 0;
284  }
285 
286  TH1D histo("histo",cuts2title(baseline),njcuts.size()*mjcuts.size(), minh, maxh);
287  for(unsigned imj(0); imj<mjcuts.size(); imj++)
288  for(unsigned inj(0); inj<njcuts.size(); inj++)
289  histo.GetXaxis()->SetBinLabel(1+inj+imj*njcuts.size(), njnames[inj]);
290  for(unsigned idata(0); idata<1; idata++)
291  plotKappa(vx[idata], vy[idata], vexl[idata], vexh[idata], veyl[idata], veyh[idata], idata, histo, nbcuts);
292 
293 
294  time(&endtime); time_setup += difftime(endtime, begtime);
295  time(&begtime);
296  cout<<endl<<"Total time: set up "<<time_setup<<" s, finding yields "<<time_ntu
297  <<" s, toys "<<time_gen<<" s"<<endl<<endl;
298 }
299 
300 void plotKappa(vector<vector<double> > &vx, vector<vector<double> > &vy, vector<vector<double> > &vexl,
301  vector<vector<double> > &vexh, vector<vector<double> > &veyl, vector<vector<double> > &veyh,
302  unsigned idata, TH1D &histo, vector<TString> &nbcuts){
303 
304  bool do_data((idata%2)==1), do_pred(true);
305  styles style("RA4long"); //style.LabelSize = 0.05;
306  style.setDefaultStyle();
307  float max_axis(3.2), max_kappa(0.);
308  unsigned nbsize(vx.size());
309  float minh(histo.GetBinLowEdge(1)), maxh(histo.GetBinLowEdge(histo.GetNbinsX()+1));
310  float wtot(maxh-minh);
311  for(unsigned inb(0); inb<nbsize; inb++){
312  for(unsigned ik(0); ik<vy[inb].size(); ik++){
313  if((vy[inb][ik]+veyh[inb][ik]) > max_kappa) max_kappa = vy[inb][ik]+veyh[inb][ik];
314  }
315  }
316  max_axis = max_kappa*1.1;
317  TCanvas can;
318  TLine line; line.SetLineColor(28); line.SetLineWidth(4); line.SetLineStyle(3);
319  histo.Draw();
320  TString ytitle("#kappa^{MC}");
321  if(do_pred) ytitle = "R_{m_{T}}";
322  histo.SetYTitle(ytitle);
323  histo.SetMaximum(max_axis);
324  style.moveYAxisLabel(&histo, 1000, false);
325  if(!do_pred) line.DrawLine(minh, 1, maxh, 1);
326  line.SetLineColor(1); line.SetLineWidth(2);
327  line.DrawLine(minh+wtot/2., 0, minh+wtot/2, max_axis);
328 
329  int acolor(kGreen+3);
330  float alength((maxh-minh)/8.), yarrow(max_axis/15.), llength(max_axis/1.5);
331  TArrow arrow; arrow.SetLineColor(acolor); arrow.SetLineWidth(1); arrow.SetArrowSize(0.02);
332  TLatex label; label.SetNDC(kFALSE);label.SetTextAlign(11); label.SetTextColor(acolor);
333  float binw(histo.GetBinWidth(1));
334  line.SetLineColor(acolor); line.SetLineWidth(1); line.SetLineStyle(2);
335  float xarrow(minh+binw*3);
336  line.DrawLine(xarrow, 0, xarrow, llength);
337  arrow.DrawArrow(xarrow, yarrow, xarrow+alength, yarrow);
338  label.DrawLatex(xarrow+0.1, yarrow+max_axis/80., "Baseline");
339 
340  xarrow = minh+wtot/2+binw*3;
341  line.DrawLine(xarrow, 0, xarrow, llength);
342  arrow.DrawArrow(xarrow, yarrow, xarrow+alength, yarrow);
343  label.DrawLatex(xarrow+0.1, yarrow+max_axis/80., "Baseline");
344 
345  double legX(style.PadLeftMargin+0.0), legY(0.88), legSingle = 0.052;
346  //if(do_pred) legX = 0.62;
347  double legW = 0.29, legH = legSingle*nbsize;
348  if(nbsize>3) legH = legSingle*((nbsize+1)/2);
349  TLegend leg(legX, legY-legH, legX+legW, legY);
350  leg.SetTextSize(style.LegendSize); leg.SetFillColor(0);
351  leg.SetFillStyle(0); leg.SetBorderSize(0);
352  leg.SetTextFont(style.nFont);
353  if(nbsize>3) leg.SetNColumns(2);
354  TGraphAsymmErrors graph[20];
355  int colors[] = {2,4,kMagenta+2, kGreen+3}, styles[] = {20, 21, 22, 23};
356  for(unsigned inb(0); inb<nbsize; inb++){
357  graph[inb] = TGraphAsymmErrors(vx[inb].size(), &(vx[inb][0]), &(vy[inb][0]),
358  &(vexl[inb][0]), &(vexh[inb][0]), &(veyl[inb][0]), &(veyh[inb][0]));
359  graph[inb].SetMarkerStyle(styles[inb]); graph[inb].SetMarkerSize(1.4);
360  graph[inb].SetMarkerColor(colors[inb]); graph[inb].SetLineColor(colors[inb]);
361  graph[inb].Draw("p same");
362  nbcuts[inb].ReplaceAll("nbm","n_{b}");
363  nbcuts[inb].ReplaceAll("=="," = ");
364  nbcuts[inb].ReplaceAll(">="," #geq ");
365  leg.AddEntry(&graph[inb], nbcuts[inb], "p");
366  }
367  leg.Draw();
368  label.SetNDC(kTRUE); label.SetTextAlign(22); label.SetTextColor(1);
369  TString cutname;
370  label.DrawLatex(0.37,0.03,"M_{J} #leq "+mjthresh);
371  label.DrawLatex(0.73,0.03,"M_{J} > "+mjthresh);
372 
373  TString pname = "plots/ratio_mt";
374  if(do_1ltt) pname += "_1ltt";
375  else {
376  if(do_2ltt) pname += "_2ltt";
377  else {
378  if(do_ttbar) pname += "_tt";
379  }
380  }
381  if(do_mj200) pname += +"_mj200";
382  if(do_data) pname += "_data";
383  else pname += "_mc";
384  pname += plot_type;
385  if(do_pred) pname.ReplaceAll("kappa","npred");
386  can.SaveAs(pname);
387 }
388 
long getYieldErr(TChain &tree, TString cut, double &yield, double &uncertainty)
void setDefaultStyle()
Definition: styles.cpp:36
int main(int argc, char *argv[])
TString cuts2title(TString title)
STL namespace.
void moveYAxisLabel(TH1 *h, float maxi, bool isLog=false)
Definition: styles.cpp:96
void plotKappa(vector< vector< double > > &vx, vector< vector< double > > &vy, vector< vector< double > > &vexl, vector< vector< double > > &vexh, vector< vector< double > > &veyl, vector< vector< double > > &veyh, unsigned idata, TH1D &histo, vector< TString > &nbcuts)
float LegendSize
Definition: styles.hpp:35
tuple kappa
Definition: parse_card.py:264
TString RoundNumber(double num, int decimals, double denom=1.)
Definition: utilities.cpp:191
int nFont
Definition: styles.hpp:33
tuple file
Definition: parse_card.py:238
float PadLeftMargin
Definition: styles.hpp:36
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, bool do_plot=false, int nrep=100000)