ra4_macros  bede988c286599a3a84b77a4d788ac0a971e89f9
table_cutflow.cxx
Go to the documentation of this file.
1 // table_cutflow: Generates a LaTeX file with a cutflow table for RA4
2 
3 #include <fstream>
4 #include <iostream>
5 #include <cmath>
6 #include <string>
7 #include <sstream>
8 #include "TChain.h"
9 #include "TFile.h"
10 #include "TString.h"
11 #include "TH1D.h"
12 #include "TMath.h"
13 #include "RooStats/NumberCountingUtils.h"
14 
15 #include "styles.hpp"
16 #include "utilities.hpp"
17 #include "utilities_macros.hpp"
18 
19 namespace {
20  TString ntuple_date("2015_05_25");
21  TString minjets("7"), midjets("9");
22  TString mjthresh("400");
23  TString luminosity="3";
24  bool do_2l=false;
25  bool do_note=true;
26 }
27 
28 using namespace std;
29 using std::cout;
30 using std::endl;
31 
32 TString YieldsCut(TString title, TString cuts, vector<TChain*> chain, vector<sfeats> Samples, int nsig);
33 
34 int main(){
35 
36  // Reading ntuples
37  TString folder="/cms2r0/babymaker/babies/2015_10_19/mc/skim_1lht400mc/";
38  TString folder_ns="/cms2r0/babymaker/babies/2015_10_19/mc/";
39  TString folder_data="/cms2r0/babymaker/babies/2015_10_19/data/singlelep/combined/skim_1lht400/";
40  TString folder_data_ns="/cms2r0/babymaker/babies/2015_10_19/data/singlelep/combined/";
41 
42  vector<TString> s_tt;
43  s_tt.push_back(folder+"*_TTJets*Lept*");
44  s_tt.push_back(folder+"*_TTJets_HT-*");
45  vector<TString> s_wjets;
46  s_wjets.push_back(folder+"*_WJetsToLNu_HT*");
47  vector<TString> s_single;
48  s_single.push_back(folder+"*_ST_*");
49  vector<TString> s_ttv;
50  s_ttv.push_back(folder+"*TTW*");
51  s_ttv.push_back(folder+"*TTZ*");
52  vector<TString> s_qcd;
53  s_qcd.push_back(folder+"*_QCD_HT*");
54  vector<TString> s_other;
55  s_other.push_back(folder+"*_DYJetsToLL_M-50_HT*");
56  // s_other.push_back(folder+"*_ZJet*");
57  s_other.push_back(folder+"*_ttHJetTobb*");
58  s_other.push_back(folder+"*_WWTo2L2Nu*");
59  s_other.push_back(folder+"*_WWToLNuQQ*");
60  s_other.push_back(folder+"*_ggZH_HToBB*");
61  vector<TString> s_t1t;
62  s_t1t.push_back(folder+"*-T1tttt_mGluino-1500_mLSP-100*");
63  vector<TString> s_t1tc;
64  s_t1tc.push_back(folder+"*-T1tttt_mGluino-1200_mLSP-800*");
65  // vector<TString> s_trig_sl;
66  // s_trig_sl.push_back(folder_data+"/*Single*");
67 
68  vector<TChain *> chain, chain_ns;
69  vector<sfeats> Samples;
70  Samples.push_back(sfeats(s_other, "Other", 1001));
71  Samples.push_back(sfeats(s_qcd, "QCD", 1005,1));
72  Samples.push_back(sfeats(s_ttv, "$t\\bar{t}V$", 1002));
73  Samples.push_back(sfeats(s_single, "Single $t$", 1005));
74  Samples.push_back(sfeats(s_wjets, "W+jets", 1004));
75  Samples.push_back(sfeats(s_tt, "$t\\bar{t}$ (1$\\ell$)", 1000,1,"ntruleps==1"));
76  Samples.push_back(sfeats(s_tt, "$t\\bar{t}$ ($2\\ell$)", 1006,1,"ntruleps>=2"));
77  int nsig(2);
78  Samples.push_back(sfeats(s_t1t, "T1tttt NC", 2));
79  Samples.push_back(sfeats(s_t1tc, "T1tttt C", 2,2));
80  // Samples.push_back(sfeats(s_trig_sl, "Data",kBlack,1,"(trig[4]||trig[8])&&nonblind")); Samples.back().isData = true;
81 
82  TString file_ns;
83  for(unsigned sam(0); sam < Samples.size(); sam++){
84  chain.push_back(new TChain("tree"));
85  chain_ns.push_back(new TChain("tree"));
86  for(unsigned insam(0); insam < Samples[sam].file.size(); insam++){
87  chain[sam]->Add(Samples[sam].file[insam]);
88  file_ns = Samples[sam].file[insam];
89  if(Samples[sam].isData) file_ns.ReplaceAll(folder_data, folder_data_ns);
90  else file_ns.ReplaceAll(folder, folder_ns);
91  chain_ns[sam]->Add(file_ns);
92  // cout<<"Pushing "<<file_ns<<endl;
93  // cout<<chain_ns[sam]->GetEntries()<<" into "<<Samples[sam].label<<endl;
94 
95  }
96  }
97 
98  bool do_zbi=false;
99  TString minjets_2l(""), midjets_2l("");
100  minjets_2l += (minjets.Atoi()-1); midjets_2l += (midjets.Atoi()-1);
101  TString fom("$Z_{\\rm bi}$");
102  if(!do_zbi) fom = "S/B";
103  TString outname = "txt/cutflow_mj"+mjthresh+"_njets"+minjets+midjets+"_lumi"+luminosity+"_"+ntuple_date+".tex";
104  if(do_2l) outname.ReplaceAll("yields","yields_2l");
105  if(do_zbi) outname.ReplaceAll("yields","yields_zbi");
106 
107  ifstream header("txt/header.tex");
108  ifstream footer("txt/footer.tex");
109  ofstream file(outname);
110  file<<header.rdbuf();
111  file<<"\\vspace{80 mm}\n";
112  file << "\n\\begin{tabular}[!htb]{ l | ";
113  for(unsigned sam(0); sam < Samples.size()-nsig; sam++) file << "r";
114  file<<" | r ";
115  for(int sam(0); sam < nsig; sam++) file<<"| r ";
116  file<<"}\\hline\\hline\n";
117  file << " \\multicolumn{1}{c|}{${\\cal L} = "<<luminosity<<"$ fb$^{-1}$} ";
118  for(unsigned sam(0); sam < Samples.size()-nsig; sam++)
119  file << " & "<<Samples[sam].label;
120  file<< " & SM bkg. ";
121  for(unsigned sam(Samples.size()-nsig); sam < Samples.size(); sam++)
122  file << " & "<<Samples[sam].label;//<< " & "+fom;
123  file << "\\\\ \\hline \n ";
124 
126  TString cuts("1");
127  file << YieldsCut("No selection", cuts, chain_ns, Samples, nsig);
128  cuts += "&&(nmus+nels)==1";
129  file << YieldsCut("$1\\ell$", cuts, chain_ns, Samples, nsig);
130  cuts += "&ht>500";
131  file << YieldsCut("$H_T>500$ GeV", cuts, chain, Samples, nsig);
132  cuts += "&&met>200";
133  file << YieldsCut("${\\rm MET}>200$ GeV", cuts, chain, Samples, nsig);
134  cuts += "&&njets>="+minjets;
135  file << YieldsCut("$n_{\\rm jets}\\geq "+minjets+"$", cuts, chain, Samples, nsig);
136  cuts += "&&nbm>=2";
137  file << YieldsCut("$n_{\\rm b}\\geq 2$", cuts, chain, Samples, nsig);
138  cuts += "&&mt>140";
139  file << YieldsCut("$m_T>140$ GeV", cuts, chain, Samples, nsig);
140  cuts += "&&mj>"+mjthresh;
141  file << YieldsCut("$M_J>"+mjthresh+"$ GeV", cuts, chain, Samples, nsig);
142  cuts += "&&met>400";
143  file << YieldsCut("${\\rm MET}>400$ GeV", cuts, chain, Samples, nsig);
144  cuts += "&&njets>="+midjets;
145  file << YieldsCut("$n_{\\rm jets}\\geq "+midjets+"$", cuts, chain, Samples, nsig);
146 
147 
149 
150 
151  file << "\\hline\\multicolumn{1}{c|}{} ";
152  for(unsigned sam(0); sam < Samples.size()-nsig; sam++)
153  file << " & "<<Samples[sam].label;
154  file<< " & SM bkg. ";
155  for(unsigned sam(Samples.size()-nsig); sam < Samples.size(); sam++)
156  file << " & "<<Samples[sam].label;//<< " & "+fom;
157  file << "\\\\ \n ";
158 
159  file<< "\\hline\\hline\n\\end{tabular}"<<endl<<endl;
160  file<<footer.rdbuf();
161  file.close();
162  cout<<endl<<"Written "<<outname<<endl;
163 }
164 
165 TString YieldsCut(TString title, TString cuts, vector<TChain*> chain, vector<sfeats> Samples, int nsig){
166  TString totCut, Hname = "histo", out;
167  vector<double> yield, error;
168  double bkg(0), bkg_err(0), err;
169  int nsam(chain.size());
170  TH1D histo(Hname, "",100, 0, 10000);
171  histo.Sumw2();
172  for(int sam(0); sam < nsam; sam++){
173  totCut = luminosity+"*weight*("+cuts+"&&"+Samples[sam].cut+")";
174  chain[sam]->Project(Hname, "met", totCut);
175  yield.push_back(histo.IntegralAndError(0,101,err));
176  error.push_back(err);
177  if(sam<nsam-nsig) {
178  if(yield[sam]>0) bkg += yield[sam];
179  bkg_err = sqrt(pow(bkg_err,2)+pow(err,2));
180  }
181  // cout<<sam<<": yield "<<Samples[sam].label<<" "<<yield[sam]<<" \t "<<totCut<<endl;
182  }
183 
184  cout<<title<<": B = "<<(RoundNumber(bkg,1)+" +- "+RoundNumber(bkg_err,1));
185  out = title;
186  for(int sam(0); sam < nsam-nsig; sam++) out += (" \t & " + RoundNumber(yield[sam],1));
187  if(!do_note) out += (" \t & $"+RoundNumber(bkg,1))+" \\pm "+RoundNumber(bkg_err,1)+"$";
188  else out += (" \t & $"+RoundNumber(bkg,1))+"$";
189  for(int sam(nsam-nsig); sam < nsam; sam++) {
190  // float fracerr(sqrt(pow(bkg_err/bkg,2)+0.3*0.3+0.24*0.24));
191  if(!do_note) out += (" \t& $" + RoundNumber(yield[sam],1)+" \\pm "+RoundNumber(error[sam],1))+"$";// + "$ \t& ");
192  else out += (" \t& $" + RoundNumber(yield[sam],1))+"$";// + "$ \t& ");
193  // if(do_zbi) out += RoundNumber(RooStats::NumberCountingUtils::BinomialExpZ(yield[sam], bkg, fracerr),2);
194  // else out += RoundNumber(yield[sam],2,bkg);
195 
196  // cout<<", S = "<<RoundNumber(yield[sam],1)+" +- "+RoundNumber(error[sam],1)<<" with Zbi = ";
197  // cout<<RoundNumber(RooStats::NumberCountingUtils::BinomialExpZ(yield[sam], bkg, fracerr),2);
198  }
199  out += " \\\\ \n";
200  cout<<endl;
201  return out;
202 }
int main()
STL namespace.
TString YieldsCut(TString title, TString cuts, vector< TChain * > chain, vector< sfeats > Samples, int nsig)
TString RoundNumber(double num, int decimals, double denom=1.)
Definition: utilities.cpp:191
tuple file
Definition: parse_card.py:238