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