susy_cfa  b611ccad937ea179f86a1f5663960264616c0a20
iso_transfer.cxx
Go to the documentation of this file.
1 #include "iso_transfer.hpp"
2 
3 #include <iostream>
4 #include <iomanip>
5 #include <vector>
6 
7 #include "TH2D.h"
8 #include "TCanvas.h"
9 #include "TColor.h"
10 #include "TStyle.h"
11 #include "TString.h"
12 #include "TLine.h"
13 
14 #include "styles.hpp"
15 #include "small_tree.hpp"
16 #include "timer.hpp"
17 #include "cut.hpp"
18 
19 const float SigLepPt(20.);
20 const float SigMiniMu(0.08);
21 const float SigMiniEl(0.12);
22 const float VetMiniMu(0.025);
23 const float VetMiniEl(0.12);
24 
25 using namespace std;
26 
27 int main(){
28  const double lumi = 4.0;
29 
30  styles style("1Dtitle"); style.LabelSize = 0.095; style.TitleSize = 0.062;
31  style.setDefaultStyle();
32 
33  TH1::SetDefaultSumw2(true);
34 
35  int pos_cols[999], sym_cols[999];
36  PositiveColors(pos_cols);
37  SymmetricColors(sym_cols);
38 
39  TString sample;
40  string basefolder("/cms5r0/ald77/archive/20150108/");
41  vector<string> treenames;
42  vector<TString> samplenames;
43  treenames.push_back(basefolder+"*T1tttt_2J_mGl-1500_mLSP-100*"); samplenames.push_back("t1tttt1500");
44  treenames.push_back(basefolder+"skim/small_TTJet_ht750_met200.root"); samplenames.push_back("ttbar");
45 
46  //treenames.push_back(basefolder+"*SMS-T1bbbb_2J_mGl-1500_mLSP-100*"); samplenames.push_back("t1bbbb1500");
47  //treenames.push_back(basefolder+"skim/small_TTJet_ht30_500_met200.root"); samplenames.push_back("ttbar");
48  //treenames.push_back(basefolder+"skim/small_QCD_ht750_met200.root"); samplenames.push_back("qcd");
49 
50  //treenames.push_back(basefolder+"*SMS-T1tttt_2J_mGl-1200_mLSP-800_*"); samplenames.push_back("t1tttt1200");
51  //treenames.push_back(basefolder+"*TTJet*.root"); samplenames.push_back("ttbar");
52  //treenames.push_back(basefolder+"*QCD*.root"); samplenames.push_back("qcd");
53 
54  for(unsigned itree(0); itree<treenames.size(); itree++){
55  small_tree tree(treenames[itree]); sample = samplenames[itree];
56  vector<CutBase*> base_cuts;
57  TString cut_string = "UNKNOWN";
58  switch(0){//Pick a set of cuts here
59  default:
60  cut_string = "inclusive";
61  break;
62  case 0:
63  cut_string = "ra4";
64  base_cuts.push_back(NewCut(&tree, &small_tree::ht, 750.f, kGreater));
65  base_cuts.push_back(NewCut(&tree, &small_tree::met, 200.f, kGreater));
66  base_cuts.push_back(NewCut(&tree, &small_tree::njets, 6, kGreaterEqual));
67  base_cuts.push_back(NewCut(&tree, &small_tree::nbm, 2, kGreaterEqual));
68  break;
69  case 1: //RA2b
70  cut_string = "ra2b";
71  base_cuts.push_back(NewCut(&tree, &small_tree::ht30, 800.f, kGreater));
72  base_cuts.push_back(NewCut(&tree, &small_tree::met, 500.f, kGreater));
73  base_cuts.push_back(NewCut(&tree, &small_tree::njets30, 4, kGreaterEqual));
74  //base_cuts.push_back(NewCut(&tree, &small_tree::nbm, 2, kGreaterEqual));
75  base_cuts.push_back(NewCut(&tree, &small_tree::mindphin_metjet, 4.f, kGreater));
76  break;
77  }
78 
79  vector<pair<IsoCut,TString> > iso_cuts;
80  //TString isveto("veto ");
81  TString isveto("");
82  if(isveto=="veto "){
83  iso_cuts.push_back(pair<IsoCut, TString>(VetoStandardIso, "vetostandard"));
84  iso_cuts.push_back(pair<IsoCut, TString>(VetoMiniIso, "vetominiso"));
85  } else {
86  iso_cuts.push_back(pair<IsoCut, TString>(StandardIso, "standard"));
87  iso_cuts.push_back(pair<IsoCut, TString>(MiniIso, "miniso"));
88  }
89 
90  // //Put cuts only applied for specific number of reco leptons here
91  // //Outer index is number of leptons
92  // vector<vector<CutBase*> > lepton_num_cuts;
93 
94  vector<TH2D> histos(iso_cuts.size(),
95  TH2D("", ";Num Reco "+isveto+"e+#mu;Num Gen e+#mu", 6, -1.5, 4.5, 6, -1.5, 4.5));
96  TLine hline(-1.5, -0.5, 4.5, -0.5);
97  TLine vline(-0.5, -1.5, -0.5, 4.5);
98  SetLineStyle(hline);
99  SetLineStyle(vline);
100 
101  long num_entries = tree.GetEntries();
102  Timer timer(num_entries, 1.0);
103  timer.Start();
104  for(long entry = 0; entry < num_entries; ++entry){
105  timer.Iterate();
106  tree.GetEntry(entry);
107 
108  if(!PassesCuts(base_cuts)) continue;
109 
110  for(size_t icut = 0; icut < iso_cuts.size(); ++icut){
111  int best_el = -1, best_mu = -1;
112  int count = (iso_cuts.at(icut).first)(tree, best_el, best_mu);
113 
114  // bool pass_lep_cuts;
115  // if(!lepton_num_cuts.size()){
116  // pass_lep_cuts = true;
117  // }else if(static_cast<size_t>(count)>=lepton_num_cuts.size()){
118  // pass_lep_cuts = PassesCuts(lepton_num_cuts.back());
119  // }else{
120  // pass_lep_cuts = PassesCuts(lepton_num_cuts.at(count));
121  // }
122  // if(!pass_lep_cuts) continue;
123 
124  float mt(99999);
125  if(count==1){
126  float lep_pt(0), lep_phi(0);
127  if(best_el>=0){
128  lep_pt = tree.els_pt()[best_el];
129  lep_phi = tree.els_phi()[best_el];
130  } else if(best_mu>=0) {
131  lep_pt = tree.mus_pt()[best_mu];
132  lep_phi = tree.mus_phi()[best_mu];
133  } else {cout<<"Either mu or el have to be best"<<endl; return 1;}
134  mt = sqrt(2*lep_pt* tree.met()*(1-cos(tree.met_phi()-lep_phi)));
135  }
136  if(false && mt<150) continue;
137 
138  histos.at(icut).Fill(count, (tree.mc_type() & 0xF00) >> 8, lumi*tree.weight());
139  }
140  }
141 
142  Delete(base_cuts);
143 
144  for(size_t ihist = 0; ihist < histos.size(); ++ihist){
145  TH2D &hist = histos.at(ihist);
146  hist.SetMarkerSize(3.4);
147  hist.SetMarkerStyle(20);
148 
149  //Sum column
150  for(int xbin = 0; xbin <= hist.GetNbinsX()+1; ++xbin){
151  double error = 0.;
152  double integral = hist.IntegralAndError(xbin, xbin, 2, hist.GetNbinsY()+1, error);
153  hist.SetBinContent(xbin, 1, integral);
154  hist.SetBinError(xbin, 1, error);
155  }
156 
157  //Sum row
158  for(int ybin = 0; ybin <= hist.GetNbinsY()+1; ++ybin){
159  double error = 0.;
160  double integral = hist.IntegralAndError(2, hist.GetNbinsX()+1, ybin, ybin, error);
161  hist.SetBinContent(1, ybin, integral);
162  hist.SetBinError(1, ybin, error);
163  }
164 
165  hist.GetXaxis()->SetBinLabel(1, "Any");
166  for(int i = 2; i <= hist.GetNbinsX(); ++i){
167  hist.GetXaxis()->SetBinLabel(i, TString::Itoa(i-2,10));
168  }
169  hist.GetYaxis()->SetBinLabel(1, "Any");
170  for(int i = 2; i <= hist.GetNbinsY(); ++i){
171  hist.GetYaxis()->SetBinLabel(i, TString::Itoa(i-2,10));
172  }
173  }
174 
175  for(size_t ihist = 0; ihist < histos.size(); ++ihist){
176  gStyle->SetPalette(999, pos_cols);
177  TString title = "eps/trans_single_"
178  +sample+"_"
179  +cut_string+"_"
180  +iso_cuts.at(ihist).second+".eps";
181  TCanvas c;
182  TString htitle("Yields for ");
183  htitle += lumi; htitle += " fb^{-1}";
184  histos.at(ihist).SetTitle(htitle);
185  gStyle->SetPaintTextFormat(".1f");
186  histos.at(ihist).Draw("col");
187  //histos.at(ihist).Draw("boxsame");
188  histos.at(ihist).Draw("textsame");
189  hline.Draw("same");
190  vline.Draw("same");
191  c.Print(title);
192  gStyle->SetPaintTextFormat(".1f");
193  for(size_t jhist = 0; jhist < histos.size(); ++jhist){
194  if(jhist==ihist) continue;
195  gStyle->SetPalette(999, sym_cols);
196  TH2D delta = histos.at(ihist);
197  for(int x = 0; x <= delta.GetNbinsX()+1; ++x){
198  for(int y = 0; y <= delta.GetNbinsY()+1; ++y){
199  delta.SetBinContent(x, y,
200  histos.at(ihist).GetBinContent(x, y)
201  -histos.at(jhist).GetBinContent(x, y));
202  }
203  }
204  double maxi = fabs(delta.GetBinContent(delta.GetMaximumBin()));
205  double mini = fabs(delta.GetBinContent(delta.GetMinimumBin()));
206  if(mini>maxi) maxi=mini;
207  delta.SetMinimum(-maxi);
208  delta.SetMaximum(maxi);
209  title = "eps/trans_delta_"
210  +sample+"_"
211  +cut_string+"_"
212  +iso_cuts.at(ihist).second
213  +"_minus_"
214  +iso_cuts.at(jhist).second+".eps";
215  htitle = "Yield difference for Miniso - Reliso";
216  delta.SetTitle(htitle);
217  delta.Draw("col");
218  delta.Draw("textsame");
219  hline.Draw("same");
220  vline.Draw("same");
221  if(!title.Contains("minus_miniso") && !title.Contains("minus_vetominiso")) c.Print(title);
222  }
223  gStyle->SetPaintTextFormat(".0f");
224  for(size_t jhist = 0; jhist < histos.size(); ++jhist){
225  if(jhist==ihist) continue;
226  gStyle->SetPalette(999, sym_cols);
227  TH2D delta = histos.at(ihist);
228  for(int x = 0; x <= delta.GetNbinsX()+1; ++x){
229  for(int y = 0; y <= delta.GetNbinsY()+1; ++y){
230  float den(histos.at(jhist).GetBinContent(x, y));
231  float z(0);
232  if(den>0.2) z = 100*(histos.at(ihist).GetBinContent(x, y)-den)/den;
233  //if(den) z = 100*(histos.at(ihist).GetBinContent(x, y)-den)/den;
234  if(fabs(z)<0.5) z = 0;
235  delta.SetBinContent(x, y, z);
236  }
237  }
238  double maxi = fabs(delta.GetBinContent(delta.GetMaximumBin()));
239  double mini = fabs(delta.GetBinContent(delta.GetMinimumBin()));
240  if(mini>maxi) maxi=mini;
241  delta.SetMinimum(-maxi);
242  delta.SetMaximum(maxi);
243  title = "eps/trans_ratio_"
244  +sample+"_"
245  +cut_string+"_"
246  +iso_cuts.at(ihist).second
247  +"_over_"
248  +iso_cuts.at(jhist).second+".eps";
249  htitle = "Yield difference for Miniso - Reliso (%)";
250  delta.SetTitle(htitle);
251  delta.Draw("col");
252  delta.Draw("textsame");
253  hline.Draw("same");
254  vline.Draw("same");
255  if(!title.Contains("over_miniso") && !title.Contains("over_vetominiso")) c.Print(title);
256  }
257 
258  }
259  }
260 }
261 
262 bool PassesCuts(const vector<CutBase*> &cuts){
263  for(vector<CutBase*>::const_iterator cut = cuts.begin();
264  cut != cuts.end();
265  ++cut){
266  if(!(*cut)->Pass()) return false;
267  }
268  return true;
269 }
270 
271 int GetLeptons(const small_tree &tree,
272  IsoCut cut,
273  int &best_el,
274  int &best_mu){
275  return cut(tree, best_el, best_mu);
276 }
277 
278 void Fix(const small_tree &tree,
279  int &best_el, int &best_mu){
280  if(best_el < 0 || static_cast<unsigned>(best_el) >= tree.els_pt().size()) best_el = -1;
281  if(best_mu < 0 || static_cast<unsigned>(best_mu) >= tree.mus_pt().size()) best_mu = -1;
282  if(best_el != -1 && best_mu != -1){
283  if(tree.els_pt().at(best_el) >= tree.mus_pt().at(best_mu)){
284  best_mu = -1;
285  }else{
286  best_el = -1;
287  }
288  }
289 }
290 
291 int StandardIso(const small_tree &tree,
292  int &best_el,
293  int &best_mu){
294  int leps = StandardEl(tree, best_el);
295  leps += StandardMu(tree, best_mu);
296  Fix(tree, best_el, best_mu);
297  return leps;
298 }
299 
300 int StandardEl(const small_tree &tree,
301  int &best_el){
302  int num_els = 0;
303  double max_pt = -1.0;
304  best_el = -1;
305  for(size_t iel = 0; iel < tree.els_eta().size(); ++iel){
306  if((fabs(tree.els_eta().at(iel))<=1.479 && tree.els_reliso_r03().at(iel)<0.2179)
307  || (fabs(tree.els_eta().at(iel))>1.479&& fabs(tree.els_eta().at(iel))<2.5
308  && tree.els_reliso_r03().at(iel)<0.254)){
309  if(!(tree.els_sigid().at(iel) && tree.els_ispf().at(iel) && tree.els_pt().at(iel)>SigLepPt)) continue;
310  ++num_els;
311  if(tree.els_pt().at(iel) > max_pt){
312  max_pt = tree.els_pt().at(iel);
313  best_el = iel;
314  }
315  }
316  }
317 
318  return num_els;
319 }
320 
321 int StandardMu(const small_tree &tree,
322  int &best_mu){
323  int num_mus = 0;
324  double max_pt = -1.0;
325  best_mu = -1;
326  for(size_t imu = 0; imu < tree.mus_reliso_r04().size(); ++imu){
327  if(tree.mus_reliso_r04().at(imu) < 0.2){
328  if(!(tree.mus_sigid().at(imu) && tree.mus_pt().at(imu)>SigLepPt)) continue;
329  ++num_mus;
330  if(tree.mus_pt().at(imu) > max_pt){
331  max_pt = tree.mus_pt().at(imu);
332  best_mu = imu;
333  }
334  }
335  }
336 
337  return num_mus;
338 }
339 
340 int MiniIso(const small_tree &tree,
341  int &best_el,
342  int &best_mu){
343  int leps = MiniEl(tree, best_el);
344  leps += MiniMu(tree, best_mu);
345  Fix(tree, best_el, best_mu);
346  return leps;
347 }
348 
349 int MiniEl(const small_tree &tree,
350  int &best_el){
351  int num_els = 0;
352  double max_pt = -1.0;
353  best_el = -1;
354  for(size_t iel = 0; iel < tree.els_miniso_tr10().size(); ++iel){
355  if(min(tree.els_reliso_r02().at(iel),tree.els_miniso_tr10().at(iel)) < SigMiniEl){
356  if(!(tree.els_sigid().at(iel) && tree.els_ispf().at(iel) && tree.els_pt().at(iel)>SigLepPt)) continue;
357  ++num_els;
358  if(tree.els_pt().at(iel) > max_pt){
359  max_pt = tree.els_pt().at(iel);
360  best_el = iel;
361  }
362  }
363  }
364  return num_els;
365 }
366 
367 int MiniMu(const small_tree &tree,
368  int &best_mu){
369  int num_mus = 0;
370  double max_pt = -1.0;
371  best_mu = -1;
372  for(size_t imu = 0; imu < tree.mus_miniso_tr10().size(); ++imu){
373  if(min(tree.mus_reliso_r02().at(imu),tree.mus_miniso_tr10().at(imu)) < SigMiniMu){
374  if(!(tree.mus_sigid().at(imu) && tree.mus_pt().at(imu)>SigLepPt)) continue;
375  ++num_mus;
376  if(tree.mus_pt().at(imu) > max_pt){
377  max_pt = tree.mus_pt().at(imu);
378  best_mu = imu;
379  }
380  }
381  }
382  return num_mus;
383 }
384 
385 int VetoStandardIso(const small_tree &tree,
386  int &best_el,
387  int &best_mu){
388  int leps = VetoStandardEl(tree, best_el);
389  leps += VetoStandardMu(tree, best_mu);
390  Fix(tree, best_el, best_mu);
391  return leps;
392 }
393 
394 int VetoStandardEl(const small_tree &tree,
395  int &best_el){
396  int num_els = 0;
397  double max_pt = -1.0;
398  best_el = -1;
399  for(size_t iel = 0; iel < tree.els_eta().size(); ++iel){
400  if((fabs(tree.els_eta().at(iel))<=1.479 && tree.els_reliso_r03().at(iel)<0.3313)
401  || (fabs(tree.els_eta().at(iel))>1.479&& fabs(tree.els_eta().at(iel))<2.5
402  && tree.els_reliso_r03().at(iel)<0.3816)){
403  if(!(tree.els_ispf().at(iel))) continue;
404  ++num_els;
405  if(tree.els_pt().at(iel) > max_pt){
406  max_pt = tree.els_pt().at(iel);
407  best_el = iel;
408  }
409  }
410  }
411 
412  return num_els;
413 }
414 
415 int VetoStandardMu(const small_tree &tree,
416  int &best_mu){
417  int num_mus = 0;
418  double max_pt = -1.0;
419  best_mu = -1;
420  for(size_t imu = 0; imu < tree.mus_reliso_r04().size(); ++imu){
421  if(tree.mus_reliso_r04().at(imu) < 0.2){
422  ++num_mus;
423  if(tree.mus_pt().at(imu) > max_pt){
424  max_pt = tree.mus_pt().at(imu);
425  best_mu = imu;
426  }
427  }
428  }
429 
430  return num_mus;
431 }
432 
433 int VetoMiniIso(const small_tree &tree,
434  int &best_el,
435  int &best_mu){
436  int leps = VetoMiniEl(tree, best_el);
437  leps += VetoMiniMu(tree, best_mu);
438  Fix(tree, best_el, best_mu);
439  return leps;
440 }
441 
442 int VetoMiniEl(const small_tree &tree,
443  int &best_el){
444  int num_els = 0;
445  double max_pt = -1.0;
446  best_el = -1;
447  for(size_t iel = 0; iel < tree.els_miniso_tr10().size(); ++iel){
448  if(min(tree.els_reliso_r02().at(iel),tree.els_miniso_tr10().at(iel)) < VetMiniEl){
449  if(!(tree.els_ispf().at(iel))) continue;
450  ++num_els;
451  if(tree.els_pt().at(iel) > max_pt){
452  max_pt = tree.els_pt().at(iel);
453  best_el = iel;
454  }
455  }
456  }
457  return num_els;
458 }
459 
460 int VetoMiniMu(const small_tree &tree,
461  int &best_mu){
462  int num_mus = 0;
463  double max_pt = -1.0;
464  best_mu = -1;
465  for(size_t imu = 0; imu < tree.mus_miniso_tr10().size(); ++imu){
466  if(min(tree.mus_reliso_r02().at(imu),tree.mus_miniso_tr10().at(imu)) < VetMiniMu){
467  ++num_mus;
468  if(tree.mus_pt().at(imu) > max_pt){
469  max_pt = tree.mus_pt().at(imu);
470  best_mu = imu;
471  }
472  }
473  }
474  return num_mus;
475 }
476 
477 void PositiveColors(int pos_cols[]){
478  const unsigned num_conts = 999;
479  const unsigned num_stops = 2;
480  double stops[num_stops] = {0., 1.};
481  double red[num_stops] = {1., 0.3};
482  double green[num_stops] = {1., 0.3};
483  double blue[num_stops] = {1., 1.};
484  gStyle->SetNumberContours(num_conts);
485  int index = TColor::CreateGradientColorTable(num_stops, stops, red, green, blue, num_conts);
486  for(int i = 0; i < 999; ++i){
487  pos_cols[i] = index+i;
488  }
489 }
490 
491 void SymmetricColors(int sym_cols[]){
492  const unsigned num_conts = 999;
493  const unsigned num_stops = 3;
494  double stops[num_stops] = {0., 0.5, 1.};
495  double red[num_stops] = {1., 1., 0.};
496  double green[num_stops] = {0., 1., 1.};
497  double blue[num_stops] = {0., 1., 0.};
498  gStyle->SetNumberContours(num_conts);
499  int index = TColor::CreateGradientColorTable(num_stops, stops, red, green, blue, num_conts);
500  for(int i = 0; i < 999; ++i){
501  sym_cols[i] = index+i;
502  }
503 }
504 
505 void SetLineStyle(TLine &line){
506  line.SetLineWidth(4);
507  line.SetLineColor(1);
508  line.SetLineStyle(1);
509 }
const float SigLepPt(20.)
std::vector< bool > const & els_ispf() const
int VetoStandardMu(const small_tree &tree, int &best_mu)
const float VetMiniMu(0.025)
Definition: timer.hpp:6
void Fix(const small_tree &tree, int &best_el, int &best_mu)
int const & nbm() const
float const & weight() const
void SymmetricColors(int sym_cols[])
void setDefaultStyle()
Definition: styles.cpp:36
int MiniIso(const small_tree &tree, int &best_el, int &best_mu)
float LabelSize
Definition: styles.hpp:35
int VetoMiniMu(const small_tree &tree, int &best_mu)
STL namespace.
std::vector< float > const & els_eta() const
void Delete(T &vec)
std::vector< float > const & mus_phi() const
Definition: cut.hpp:12
void PositiveColors(int pos_cols[])
std::vector< bool > const & els_sigid() const
int StandardEl(const small_tree &tree, int &best_el)
float const & met_phi() const
float const & met() const
const float SigMiniEl(0.12)
int VetoMiniIso(const small_tree &tree, int &best_el, int &best_mu)
int MiniEl(const small_tree &tree, int &best_el)
std::vector< float > const & mus_pt() const
float TitleSize
Definition: styles.hpp:35
void SetLineStyle(TLine &line)
int StandardMu(const small_tree &tree, int &best_mu)
Cut< T > * NewCut(small_tree const *tree, const T &(small_tree::*func)() const, const T &cut_val, InequalityType compare=kGreaterEqual)
Definition: cut_impl.hpp:48
int(* IsoCut)(const small_tree &, int &, int &)
const float VetMiniEl(0.12)
int VetoMiniEl(const small_tree &tree, int &best_el)
int const & njets() const
int main()
const float SigMiniMu(0.08)
std::vector< bool > const & mus_sigid() const
std::vector< float > const & els_phi() const
bool PassesCuts(const vector< CutBase * > &cuts)
int VetoStandardEl(const small_tree &tree, int &best_el)
float const & mindphin_metjet() const
int MiniMu(const small_tree &tree, int &best_mu)
int StandardIso(const small_tree &tree, int &best_el, int &best_mu)
long GetEntries() const
void Iterate()
Definition: timer.cpp:28
virtual void GetEntry(const long entry)
void Start()
Definition: timer.cpp:22
int GetLeptons(const small_tree &tree, IsoCut cut, int &best_el, int &best_mu)
int VetoStandardIso(const small_tree &tree, int &best_el, int &best_mu)
float const & ht() const
std::vector< float > const & els_pt() const