ra4_macros  bede988c286599a3a84b77a4d788ac0a971e89f9
utilities.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 // utilities - Various functions used accross the code
3 //----------------------------------------------------------------------------
4 
5 #ifndef INT_ROOT
6 #include "utilities.hpp"
7 #endif
8 
9 #include <cmath>
10 
11 #include <iostream>
12 #include <string>
13 #include <stdexcept>
14 
15 //#ifndef INT_ROOT
16 //#include "fastjet/PseudoJet.hh"
17 //#endif
18 
19 #include "TString.h"
20 #include "TSystemDirectory.h"
21 #include "TSystemFile.h"
22 #include "TSystem.h"
23 #include "TList.h"
24 #include "TCollection.h"
25 #include "TH1D.h"
26 #include "TTree.h"
27 #include "TGraph.h"
28 
29 using namespace std;
30 
31 // Returns cross section of sample in pb
32 float cross_section(const TString &file){
33  float xsec(0.);
34 
35  // From https://twiki.cern.ch/twiki/bin/view/LHCPhysics/SUSYCrossSections13TeVgluglu
36  if(file.Contains("T1tttt") && file.Contains("825_")) xsec = 1.2167;
37  if(file.Contains("T1tttt") && file.Contains("1025_")) xsec = 0.272778;
38  if(file.Contains("T1tttt") && file.Contains("1150_")) xsec = 0.117687;
39  if(file.Contains("T1tttt") && file.Contains("1200_")) xsec = 0.0856418;
40  if(file.Contains("T1tttt") && file.Contains("1500_")) xsec = 0.0141903;
41 
42  if(file.Contains("T2tt") && file.Contains("650_")) xsec = 0.107045;
43  if(file.Contains("T2tt") && file.Contains("850_")) xsec = 0.0189612;
44 
45  if(file.Contains("SMS-T2tt_2J_mStop-425_mLSP-325")) xsec = 1.31169;
46  if(file.Contains("SMS-T2tt_2J_mStop-500_mLSP-325")) xsec = 0.51848;
47  if(file.Contains("SMS-T1bbbb_2J_mGl-1500_mLSP-100")) xsec = 0.0141903;
48  if(file.Contains("SMS-T1bbbb_2J_mGl-1000_mLSP-900")) xsec = 0.325388;
49  if(file.Contains("SMS-T1qqqq_2J_mGl-1400_mLSP-100")) xsec = 0.0252977;
50  if(file.Contains("SMS-T1qqqq_2J_mGl-1000_mLSP-800")) xsec = 0.325388;
51  if(file.Contains("SMS-T2bb_2J_mStop-600_mLSP-580")) xsec = 0.174599;
52  if(file.Contains("SMS-T2bb_2J_mStop-900_mLSP-100")) xsec = 0.0128895;
53 
54  // Cross-section taken from https://twiki.cern.ch/twiki/bin/view/LHCPhysics/TtbarNNLO
55  // Alternative option: https://twiki.cern.ch/twiki/bin/view/Sandbox/FullNNLOcrossSections#Top_cross_section_for_13_TeV
56  if(file.Contains("TTJet") || file.Contains("TT_")) xsec = 815.96;
57 
58  // From https://cms-pdmv.cern.ch/mcm
59  // k-factors from https://mangano.web.cern.ch/mangano/public/MECCA/samples_50ns_miniaod.txt
60  // k-factors are ratio of https://twiki.cern.ch/twiki/bin/viewauth/CMS/StandardModelCrossSectionsat13TeV
61  // NLO/NNLO cross-sections to that of an inclusive sample in mcm at lower order (LO/NLO)
62  if(file.Contains("WJetsToLNu_HT-100to200")) xsec = 1817.0*1.23;
63  if(file.Contains("WJetsToLNu_HT-200to400")) xsec = 471.6*1.23;
64  if(file.Contains("WJetsToLNu_HT-400to600")) xsec = 55.61*1.23;
65  if(file.Contains("WJetsToLNu_HT-600toInf")) xsec = 18.81*1.23;
66 
67  if(file.Contains("WToENu")) xsec = 16000.0;
68  if(file.Contains("WToMuNu")) xsec = 16100.0;
69 
70  if(file.Contains("QCD_HT-100To250_13TeV-madgraph")) xsec = 28730000.;
71  if(file.Contains("QCD_HT_250To500_13TeV-madgraph")) xsec = 670500.0;
72  if(file.Contains("QCD_HT-500To1000_13TeV-madgraph")) xsec = 26740.0;
73  if(file.Contains("QCD_HT_1000ToInf_13TeV-madgraph")) xsec = 769.7;
74 
75  if(file.Contains("QCD_Pt-1800_")) xsec = 0.1091;
76 
77  if(file.Contains("QCD_Pt-5to10")) xsec = 80710000000;
78  if(file.Contains("QCD_Pt-10to15")) xsec = 7528000000;
79  if(file.Contains("QCD_Pt-15to30")) xsec = 2237000000;
80  if(file.Contains("QCD_Pt-30to50")) xsec = 161500000;
81  if(file.Contains("QCD_Pt-50to80")) xsec = 22110000;
82  if(file.Contains("QCD_Pt-80to120")) xsec = 3000114.3;
83  if(file.Contains("QCD_Pt-120to170")) xsec = 493200;
84  if(file.Contains("QCD_Pt-170to300")) xsec = 120300;
85  if(file.Contains("QCD_Pt-300to470")) xsec = 7475;
86  if(file.Contains("QCD_Pt-470to600")) xsec = 587.1;
87  if(file.Contains("QCD_Pt-600to800")) xsec = 167;
88  if(file.Contains("QCD_Pt-800to1000")) xsec = 28.25;
89  if(file.Contains("QCD_Pt-1000to1400")) xsec = 8.195;
90  if(file.Contains("QCD_Pt-1400to1800")) xsec = 0.7346;
91  if(file.Contains("QCD_Pt-1800to2400")) xsec = 0.102;
92  if(file.Contains("QCD_Pt-2400to3200")) xsec = 0.00644;
93  if(file.Contains("QCD_Pt-3200")) xsec = 0.000163;
94 
95  if(file.Contains("TToLeptons_s-channel")) xsec = 2.0;
96  if(file.Contains("TToLeptons_t-channel")) xsec = 103.4;
97  if(file.Contains("T_tW-channel-DR")) xsec = 35.0;
98  if(file.Contains("TBarToLeptons_s-channel")) xsec = 1.0;
99  if(file.Contains("TBarToLeptons_t-channel")) xsec = 61.6;
100  if(file.Contains("Tbar_tW-channel-DR")) xsec = 35.0;
101 
102  if(file.Contains("DYJetsToLL_M-50_HT-100to200")) xsec = 194.3*1.27;
103  if(file.Contains("DYJetsToLL_M-50_HT-200to400")) xsec = 52.24*1.27;
104  if(file.Contains("DYJetsToLL_M-50_HT-400to600")) xsec = 6.546*1.27;
105  if(file.Contains("DYJetsToLL_M-50_HT-600toInf")) xsec = 2.179*1.27;
106 
107  if(file.Contains("ZJetsToNuNu_HT-100to200_Tune4C_13TeV-madgraph-tauola")) xsec =372.6*1.27;
108  if(file.Contains("ZJetsToNuNu_HT-200to400_Tune4C_13TeV-madgraph-tauola")) xsec =100.8*1.27;
109  if(file.Contains("ZJetsToNuNu_HT-400to600_Tune4C_13TeV-madgraph-tauola")) xsec =11.99*1.27;
110  if(file.Contains("ZJetsToNuNu_HT-600toInf_Tune4C_13TeV-madgraph-tauola")) xsec =4.113*1.27;
111 
112  if(file.Contains("TTZJets_Tune4C_13TeV-madgraph-tauola")) xsec = 0.7598;
113  if(file.Contains("TTWJets_Tune4C_13TeV-madgraph-tauola")) xsec = 0.5662;
114  // Calculated at 13 TeV in
115  // https://twiki.cern.ch/twiki/bin/view/LHCPhysics/CERNYellowReportPageAt1314TeV
116  // Higgs branching ratios from
117  // https://twiki.cern.ch/twiki/bin/view/LHCPhysics/CERNYellowReportPageBR
118  if(file.Contains("ZH_HToBB_ZToLL_M-125_13TeV_powheg-herwigpp")) xsec = 0.569*0.033658*0.8696;
119  if(file.Contains("ZH_HToBB_ZToNuNu_M-125_13TeV_powheg-herwigpp")) xsec = 0.569*0.2*0.8696;
120  if(file.Contains("WH_HToBB_WToLNu_M-125_13TeV_powheg-herwigpp")) xsec = 0.569*0.1086*1.380;
121 
122  if(xsec<=0) cout<<"Cross section not found for "<<file<<endl;
123 
124  return xsec;
125 }
126 
127 // Returns list of directorites or files in folder
128 vector<TString> dirlist(const TString &folder,
129  const TString &inname,
130  const TString &tag){
131  TString pwd(gSystem->pwd());
132  vector<TString> v_dirs;
133  TSystemDirectory dir(folder, folder);
134  TList *files = dir.GetListOfFiles();
135  if (files) {
136  TSystemFile *file;
137  TString fname;
138  TIter next(files);
139  while ((file=static_cast<TSystemFile*>(next()))) {
140  fname = file->GetName();
141  if (inname=="dir") {
142  if ((file->IsDirectory() && !fname.Contains(".") && fname.Contains(tag))) v_dirs.push_back(fname);
143  } else if(fname.Contains(inname)) v_dirs.push_back(fname);
144  }
145  } // if(files)
146  gSystem->cd(pwd); // The TSystemDirectory object seems to change current folder
147  return v_dirs;
148 }
149 
150 bool eigen2x2(float matrix[2][2], float &eig1, float &eig2){
151  float root = pow(matrix[0][0],2) + pow(matrix[1][1],2)-2*matrix[0][0]*matrix[1][1]+4*matrix[0][1]*matrix[1][0];
152  if(root<0) return false;
153 
154  eig1 = (matrix[0][0]+matrix[1][1]+sqrt(root))/2.;
155  eig2 = (matrix[0][0]+matrix[1][1]-sqrt(root))/2.;
156  return true;
157 }
158 
159 bool id_big2small(const int_double& left, const int_double& right){
160  return left.second > right.second;
161 }
162 
163 bool dd_small2big(const double_double& left, const double_double& right){
164  return left.first < right.first;
165 }
166 
167 bool dd_big2small(const double_double& left, const double_double& right){
168  return left.first > right.first;
169 }
170 
171 long double DeltaPhi(long double phi1, long double phi2){
172  long double dphi = fmod(fabs(phi2-phi1), 2.L*PI);
173  return dphi>PI ? 2.L*PI-dphi : dphi;
174 }
175 
176 long double SignedDeltaPhi(long double phi1, long double phi2){
177  long double dphi = fmod(phi2-phi1, 2.L*PI);
178  if(dphi>PI){
179  return dphi-2.L*PI;
180  }else if(dphi<-PI){
181  return dphi+2.L*PI;
182  }else{
183  return dphi;
184  }
185 }
186 
187 float dR(float eta1, float eta2, float phi1, float phi2) {
188  return AddInQuadrature(eta1-eta2, DeltaPhi(phi1,phi2));
189 }
190 
191 TString RoundNumber(double num, int decimals, double denom){
192  if(denom==0) return " - ";
193  double neg = 1; if(num*denom<0) neg = -1;
194  num /= neg*denom; num += 0.5*pow(10.,-decimals);
195  long num_int = static_cast<long>(num);
196  long num_dec = static_cast<long>((1+num-num_int)*pow(10.,decimals));
197  TString s_dec = ""; s_dec += num_dec; s_dec.Remove(0,1);
198  TString result="";
199  if(neg<0) result+="-";
200  result+= num_int;
201  if(decimals>0) {
202  result+="."; result+=s_dec;
203  }
204 
205  TString afterdot = result;
206  afterdot.Remove(0,afterdot.First(".")+1);
207  for(int i=0; i<decimals-afterdot.Length(); i++)
208  result += "0";
209  return result;
210 }
211 
212 long double AddInQuadrature(long double x, long double y){
213  if(fabs(y)>fabs(x)){
214  const long double temp = y;
215  y=x;
216  x=temp;
217  }
218  if(x==0.) return y;
219  const long double rat=y/x;
220  return fabs(x)*sqrt(1.0L+rat*rat);
221 }
222 
223 long double GetMass(long double e, long double px, long double py, long double pz){
224  px/=e; py/=e; pz/=e;
225  return fabs(e)*sqrt(1.0L-px*px-py*py-pz*pz);
226 }
227 
228 long double GetMT(long double m1, long double pt1, long double phi1,
229  long double m2, long double pt2, long double phi2){
230  return sqrt(m1*m1+m2*m2+2.L*(sqrt((m1*m1+pt1*pt1)*(m2*m2+pt2*pt2))-pt1*pt2*cos(phi2-phi1)));
231 }
232 
233 long double GetMT(long double pt1, long double phi1,
234  long double pt2, long double phi2){
235  //Faster calculation in massless case
236  return sqrt(2.L*pt1*pt2*(1.L-cos(phi2-phi1)));
237 }
238 
239 bool Contains(const string& text, const string& pattern){
240  return text.find(pattern) != string::npos;
241 }
242 
243 vector<string> Tokenize(const string& input,
244  const string& tokens){
245  char* ipt(new char[input.size()+1]);
246  memcpy(ipt, input.data(), input.size());
247  ipt[input.size()]=static_cast<char>(0);
248  char* ptr(strtok(ipt, tokens.c_str()));
249  vector<string> output(0);
250  while(ptr!=NULL){
251  output.push_back(ptr);
252  ptr=strtok(NULL, tokens.c_str());
253  }
254  return output;
255 }
256 
257 void get_count_and_uncertainty(TTree& tree,
258  const string& cut,
259  double& count,
260  double& uncertainty){
261  const string hist_name("temp");
262  TH1D temp(hist_name.c_str(), "", 1, -1.0, 1.0);
263  tree.Project(hist_name.c_str(), "0.0", cut.c_str());
264  count=temp.IntegralAndError(0,2,uncertainty);
265 }
266 
267 void AddPoint(TGraph& graph, const double x, const double y){
268  graph.SetPoint(graph.GetN(), x, y);
269 }
270 
271 string execute(const string &cmd){
272  FILE *pipe = popen(cmd.c_str(), "r");
273  if(!pipe) throw runtime_error("Could not open pipe.");
274  const size_t buffer_size = 128;
275  char buffer[buffer_size];
276  string result = "";
277  while(!feof(pipe)){
278  if(fgets(buffer, buffer_size, pipe) != NULL) result += buffer;
279  }
280 
281  pclose(pipe);
282  return result;
283 }
284 
285 string RemoveTrailingNewlines(string str){
286  while(!str.empty() && str.at(str.length()-1) == '\n'){
287  str.erase(str.length()-1);
288  }
289  return str;
290 }
291 
292 vector<double> LinearSpacing(size_t npts, double low, double high){
293  vector<double> pts(npts,low+0.5*(high-low));
294  if(npts>1){
295  double gap = (high-low)/(npts-1.0);
296  for(size_t pt = 0; pt < npts; ++pt){
297  pts.at(pt) = low+pt*gap;
298  }
299  }
300  return pts;
301 }
302 
303 //#ifndef INT_ROOT
304 //using namespace fastjet;
305 //bool greater_m(const PseudoJet &a, const PseudoJet &b){
306 // return a.m() > b.m();
307 //}
308 
309 //vector<PseudoJet> sorted_by_m(vector<PseudoJet> pjs){
310 //sort(pjs.begin(), pjs.end(), greater_m);
311 // return pjs;
312 //}
313 //#endif
const long double PI
Definition: utilities.hpp:25
bool dd_small2big(const double_double &left, const double_double &right)
Definition: utilities.cpp:163
TString RoundNumber(double num, int decimals, double denom)
Definition: utilities.cpp:191
diff git a run rpv_bkg_syst py b run rpv_bkg_syst py index a run rpv_bkg_syst py b run rpv_bkg_syst py
Definition: log.txt:5
bool Contains(const string &text, const string &pattern)
Definition: utilities.cpp:239
bool id_big2small(const int_double &left, const int_double &right)
Definition: utilities.cpp:159
void get_count_and_uncertainty(TTree &tree, const string &cut, double &count, double &uncertainty)
Definition: utilities.cpp:257
STL namespace.
long double SignedDeltaPhi(long double phi1, long double phi2)
Definition: utilities.cpp:176
vector< TString > dirlist(const TString &folder, const TString &inname, const TString &tag)
Definition: utilities.cpp:128
long double GetMass(long double e, long double px, long double py, long double pz)
Definition: utilities.cpp:223
long double AddInQuadrature(long double x, long double y)
Definition: utilities.cpp:212
vector< string > Tokenize(const string &input, const string &tokens)
Definition: utilities.cpp:243
long double GetMT(long double m1, long double pt1, long double phi1, long double m2, long double pt2, long double phi2)
Definition: utilities.cpp:228
long double DeltaPhi(long double phi1, long double phi2)
Definition: utilities.cpp:171
bool dd_big2small(const double_double &left, const double_double &right)
Definition: utilities.cpp:167
tuple file
Definition: parse_card.py:238
tuple count
Definition: parse_card.py:261
std::pair< int, double > int_double
Definition: utilities.hpp:23
void AddPoint(TGraph &graph, const double x, const double y)
Definition: utilities.cpp:267
std::pair< double, double > double_double
Definition: utilities.hpp:24
vector< double > LinearSpacing(size_t npts, double low, double high)
Definition: utilities.cpp:292
float cross_section(const TString &file)
Definition: utilities.cpp:32
bool eigen2x2(float matrix[2][2], float &eig1, float &eig2)
Definition: utilities.cpp:150
float dR(float eta1, float eta2, float phi1, float phi2)
Definition: utilities.cpp:187
string RemoveTrailingNewlines(string str)
Definition: utilities.cpp:285
string execute(const string &cmd)
Definition: utilities.cpp:271