susy_cfa  b611ccad937ea179f86a1f5663960264616c0a20
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 #include "TString.h"
16 #include "TSystemDirectory.h"
17 #include "TSystemFile.h"
18 #include "TSystem.h"
19 #include "TList.h"
20 #include "TCollection.h"
21 #include "TH1D.h"
22 #include "TTree.h"
23 #include "TGraph.h"
24 
25 using namespace std;
26 
27 // Returns cross section of sample in pb
28 float cross_section(const TString &file){
29  float xsec(0.);
30 
31  if(file.Contains("Run2015")) xsec = 1.;
32 
33 
34  // From https://twiki.cern.ch/twiki/bin/view/LHCPhysics/SUSYCrossSections13TeVgluglu
35  if(file.Contains("T1tttt") && file.Contains("825_")) xsec = 1.2167;
36  if(file.Contains("T1tttt") && file.Contains("1025_")) xsec = 0.272778;
37  if(file.Contains("T1tttt") && file.Contains("1150_")) xsec = 0.117687;
38  if(file.Contains("T1tttt") && file.Contains("1200_")) xsec = 0.0856418;
39  if(file.Contains("T1tttt") && file.Contains("1500_")) xsec = 0.0141903;
40 
41  if(file.Contains("T2tt") && file.Contains("650_")) xsec = 0.107045;
42  if(file.Contains("T2tt") && file.Contains("850_")) xsec = 0.0189612;
43 
44  if(file.Contains("SMS-T2tt_2J_mStop-425_mLSP-325")) xsec = 1.31169;
45  if(file.Contains("SMS-T2tt_2J_mStop-500_mLSP-325")) xsec = 0.51848;
46  if(file.Contains("SMS-T1bbbb_2J_mGl-1500_mLSP-100")) xsec = 0.0141903;
47  if(file.Contains("SMS-T1bbbb_2J_mGl-1000_mLSP-900")) xsec = 0.325388;
48  if(file.Contains("SMS-T1qqqq_2J_mGl-1400_mLSP-100")) xsec = 0.0252977;
49  if(file.Contains("SMS-T1qqqq_2J_mGl-1000_mLSP-800")) xsec = 0.325388;
50  if(file.Contains("SMS-T2bb_2J_mStop-600_mLSP-580")) xsec = 0.174599;
51  if(file.Contains("SMS-T2bb_2J_mStop-900_mLSP-100")) xsec = 0.0128895;
52 
53  // Cross-section taken from https://twiki.cern.ch/twiki/bin/view/LHCPhysics/TtbarNNLO
54  // Alternative option: https://twiki.cern.ch/twiki/bin/view/Sandbox/FullNNLOcrossSections#Top_cross_section_for_13_TeV
55  if(file.Contains("TTJets_Tune") || file.Contains("TT_")) xsec = 815.96;
56  if(file.Contains("TTJets_HT")){//LO cross sections with k-factor of 1.625 already applied
57  if(file.Contains("2500toInf")) xsec = 0.0023234211;
58  if(file.Contains("1200to2500")) xsec = 0.194972521;
59  if(file.Contains("800to1200")) xsec = 1.07722318;
60  if(file.Contains("600to800")) xsec = 2.61537118;
61 
62  }
63 
64  if(file.Contains("TTJets_DiLept")) xsec = 85.66; // (3*0.108)^2*815.96
65  if(file.Contains("TTJets_SingleLept")) xsec = 178.7; //(1- ((1-3*0.108)^2+(3*0.108)^2))*815.96*0.5 per half
66 
67 
68 
69 
70  // From https://cms-pdmv.cern.ch/mcm
71  // k-factors from https://mangano.web.cern.ch/mangano/public/MECCA/samples_50ns_miniaod.txt
72  // k-factors are ratio of https://twiki.cern.ch/twiki/bin/viewauth/CMS/StandardModelCrossSectionsat13TeV
73  // NLO/NNLO cross-sections to that of an inclusive sample in mcm at lower order (LO/NLO)
74 
75  if(file.Contains("WJetsToLNu") && file.Contains("amcatnloFXFX")) xsec=61526.7; //NNLO from Lesya's summary table
76 
77  //cross-section per slice changed due to change in genHT definition
78  if (file.Contains("RunIISpring15DR74")){
79  if(file.Contains("WJetsToLNu_HT-100To200")) xsec = 1347.*1.21; //updated based on MCM
80  if(file.Contains("WJetsToLNu_HT-200To400")) xsec = 360.*1.21;
81  if(file.Contains("WJetsToLNu_HT-400To600")) xsec = 48.9*1.21;
82  if(file.Contains("WJetsToLNu_HT-600ToInf")) xsec = 18.77*1.21;
83  } else {
84  if(file.Contains("WJetsToLNu_HT-100to200")) xsec = 1817.0*1.23;
85  if(file.Contains("WJetsToLNu_HT-200to400")) xsec = 471.6*1.23;
86  if(file.Contains("WJetsToLNu_HT-400to600")) xsec = 55.61*1.23;
87  if(file.Contains("WJetsToLNu_HT-600toInf")) xsec = 18.81*1.23;
88  }
89  if(file.Contains("WToENu")) xsec = 16000.0;
90  if(file.Contains("WToMuNu")) xsec = 16100.0;
91 
92  if(file.Contains("QCD_HT-100To250_13TeV-madgraph")) xsec = 28730000.;
93  if(file.Contains("QCD_HT_250To500_13TeV-madgraph")) xsec = 670500.0;
94  if(file.Contains("QCD_HT-500To1000_13TeV-madgraph")) xsec = 26740.0;
95  if(file.Contains("QCD_HT_1000ToInf_13TeV-madgraph")) xsec = 769.7;
96 
97  if(file.Contains("QCD_HT200to300_TuneCUETP8M1_13TeV-madgraphMLM-pythia8")) xsec = 1735000;
98  if(file.Contains("QCD_HT300to500_TuneCUETP8M1_13TeV-madgraphMLM-pythia8")) xsec = 366800;
99  if(file.Contains("QCD_HT500to700_TuneCUETP8M1_13TeV-madgraphMLM-pythia8")) xsec = 29370;
100  if(file.Contains("QCD_HT700to1000_TuneCUETP8M1_13TeV-madgraphMLM-pythia8")) xsec = 6524;
101  if(file.Contains("QCD_HT1000to1500_TuneCUETP8M1_13TeV-madgraphMLM-pythia8")) xsec = 1064;
102  if(file.Contains("QCD_HT1500to2000_TuneCUETP8M1_13TeV-madgraphMLM-pythia8")) xsec = 121.5;
103  if(file.Contains("QCD_HT2000toInf_TuneCUETP8M1_13TeV-madgraphMLM-pythia8")) xsec = 25.42;
104 
105  if(file.Contains("QCD_Pt-1800_")) xsec = 0.1091;
106 
107  // only thing that changed for Spring 15 is "QCD_Pt-" --> "QCD_Pt_"
108  if (file.Contains("QCD_Pt")){
109  if(file.Contains("5to10")) xsec = 80710000000;
110  if(file.Contains("10to15")) xsec = 7528000000;
111  if(file.Contains("15to30")) xsec = 2237000000;
112  if(file.Contains("30to50")) xsec = 161500000;
113  if(file.Contains("50to80")) xsec = 22110000;
114  if(file.Contains("80to120")) xsec = 2762530;// xsec = 3000114.3;
115  if(file.Contains("120to170")) xsec = 471100;//493200;
116  if(file.Contains("170to300")) xsec = 117276;//120300;
117  if(file.Contains("300to470")) xsec = 7823;//7475;
118  if(file.Contains("470to600")) xsec = 648.2;//587.1;
119  if(file.Contains("600to800")) xsec = 186.9;//167;
120  if(file.Contains("800to1000")) xsec = 32.293;//28.25;
121  if(file.Contains("1000to1400")) xsec = 9.4183;//8.195;
122  if(file.Contains("1400to1800")) xsec = 0.84265;// 0.7346;
123  if(file.Contains("1800to2400")) xsec = 0.114943;//0.102;
124  if(file.Contains("2400to3200")) xsec = 0.00682981;//0.00644;
125  if(file.Contains("3200toInf")) xsec = 0.000165445;// 0.000163;
126  }
127 
128  // Cross sections from https://twiki.cern.ch/twiki/bin/view/LHCPhysics/SingleTopRefXsec
129  // multiplied by BF(W->mu,e,tau) = 0.324
130  if (file.Contains("RunIISpring15DR74")){
131  if (file.Contains("ST_s-channel_4f_leptonDecays_13TeV-amcatnlo-pythia8")) xsec = 3.34;
132  if (file.Contains("ST_t-channel_antitop_4f_leptonDecays_13TeV-powheg-pythia8")) xsec = 26.23;
133  if (file.Contains("ST_t-channel_top_4f_leptonDecays_13TeV-powheg-pythia8")) xsec = 44.07;
134  if (file.Contains("ST_tW_antitop_5f_inclusiveDecays_13TeV-powheg-pythia8")) xsec = 35.8;
135  if (file.Contains("ST_tW_top_5f_inclusiveDecays_13TeV-powheg-pythia8")) xsec = 35.8;
136  } else {
137  if(file.Contains("TBarToLeptons_s-channel")) xsec = 1.29;
138  if(file.Contains("TToLeptons_s-channel")) xsec = 2.06;
139  if(file.Contains("TBarToLeptons_t-channel")) xsec = 26.23;
140  if(file.Contains("TToLeptons_t-channel")) xsec = 44.07;
141  if(file.Contains("Tbar_tW-channel-DR")) xsec = 35.8;
142  if(file.Contains("T_tW-channel-DR")) xsec = 35.8;
143  }
144 
145  if(file.Contains("DYJetsToLL_M-10to50_TuneCUETP8M1_13TeV-amcatnloFXFX-pythia8")) xsec = 18610;
146  if(file.Contains("DYJetsToLL_M-50_TuneCUETP8M1_13TeV-amcatnloFXFX-pythia8")) xsec = 6104;
147 
148  if(file.Contains("madgraphMLM")){
149  if(file.Contains("DYJetsToLL_M-50_HT-100to200")) xsec = 171.46;
150  if(file.Contains("DYJetsToLL_M-50_HT-200to400")) xsec = 52.58;
151  if(file.Contains("DYJetsToLL_M-50_HT-400to600")) xsec = 6.761;
152  if(file.Contains("DYJetsToLL_M-50_HT-600toInf")) xsec = 2.713;
153  }
154  else{
155  if(file.Contains("DYJetsToLL_M-50_HT-100to200")) xsec = 194.3*1.27;
156  if(file.Contains("DYJetsToLL_M-50_HT-200to400")) xsec = 52.24*1.27;
157  if(file.Contains("DYJetsToLL_M-50_HT-400to600")) xsec = 6.546*1.27;
158  if(file.Contains("DYJetsToLL_M-50_HT-600toInf")) xsec = 2.179*1.27;
159  }
160 
161  if(file.Contains("ZJetsToNuNu_HT-100to200_Tune4C_13TeV-madgraph-tauola")) xsec =372.6*1.27;
162  if(file.Contains("ZJetsToNuNu_HT-200to400_Tune4C_13TeV-madgraph-tauola")) xsec =100.8*1.27;
163  if(file.Contains("ZJetsToNuNu_HT-400to600_Tune4C_13TeV-madgraph-tauola")) xsec =11.99*1.27;
164  if(file.Contains("ZJetsToNuNu_HT-600toInf_Tune4C_13TeV-madgraph-tauola")) xsec =4.113*1.27;
165 
166  if(file.Contains("TTZJets_Tune4C_13TeV-madgraph-tauola")) xsec = 0.7598;
167  if(file.Contains("TTWJets_Tune4C_13TeV-madgraph-tauola")) xsec = 0.5662;
168  // Calculated at 13 TeV in
169  // https://twiki.cern.ch/twiki/bin/view/LHCPhysics/CERNYellowReportPageAt1314TeV
170  // Higgs branching ratios from
171  // https://twiki.cern.ch/twiki/bin/view/LHCPhysics/CERNYellowReportPageBR
172  if(file.Contains("ZH_HToBB_ZToLL_M-125_13TeV_powheg-herwigpp")) xsec = 0.569*0.033658*0.8696;
173  if(file.Contains("ZH_HToBB_ZToNuNu_M-125_13TeV_powheg-herwigpp")) xsec = 0.569*0.2*0.8696;
174  if(file.Contains("WH_HToBB_WToLNu_M-125_13TeV_powheg-herwigpp")) xsec = 0.569*0.1086*1.380;
175 
176  if(xsec<=0) cout<<"Cross section not found for "<<file<<endl;
177 
178  return xsec;
179 }
180 
181 // Returns list of directorites or files in folder
182 vector<TString> dirlist(const TString &folder,
183  const TString &inname,
184  const TString &tag){
185  TString pwd(gSystem->pwd());
186  vector<TString> v_dirs;
187  TSystemDirectory dir(folder, folder);
188  TList *files = dir.GetListOfFiles();
189  if (files) {
190  TSystemFile *file;
191  TString fname;
192  TIter next(files);
193  while ((file=static_cast<TSystemFile*>(next()))) {
194  fname = file->GetName();
195  if (inname=="dir") {
196  if ((file->IsDirectory() && !fname.Contains(".") && fname.Contains(tag))) v_dirs.push_back(fname);
197  } else if(fname.Contains(inname)) v_dirs.push_back(fname);
198  }
199  } // if(files)
200  gSystem->cd(pwd); // The TSystemDirectory object seems to change current folder
201  return v_dirs;
202 }
203 
204 bool eigen2x2(float matrix[2][2], float &eig1, float &eig2){
205  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];
206  if(root<0) return false;
207 
208  eig1 = (matrix[0][0]+matrix[1][1]+sqrt(root))/2.;
209  eig2 = (matrix[0][0]+matrix[1][1]-sqrt(root))/2.;
210  return true;
211 }
212 
213 bool id_big2small(const int_double& left, const int_double& right){
214  return left.second > right.second;
215 }
216 
217 bool dd_small2big(const double_double& left, const double_double& right){
218  return left.first < right.first;
219 }
220 
221 bool dd_big2small(const double_double& left, const double_double& right){
222  return left.first > right.first;
223 }
224 
225 long double DeltaPhi(long double phi1, long double phi2){
226  long double dphi = fmod(fabs(phi2-phi1), 2.L*PI);
227  return dphi>PI ? 2.L*PI-dphi : dphi;
228 }
229 
230 long double SignedDeltaPhi(long double phi1, long double phi2){
231  long double dphi = fmod(phi2-phi1, 2.L*PI);
232  if(dphi>PI){
233  return dphi-2.L*PI;
234  }else if(dphi<-PI){
235  return dphi+2.L*PI;
236  }else{
237  return dphi;
238  }
239 }
240 
241 float dR(float eta1, float eta2, float phi1, float phi2) {
242  return AddInQuadrature(eta1-eta2, DeltaPhi(phi1,phi2));
243 }
244 
245 TString roundNumber(double num, int decimals, double denom){
246  if(denom==0) return " - ";
247  double neg = 1; if(num*denom<0) neg = -1;
248  num /= neg*denom; num += 0.5*pow(10.,-decimals);
249  long num_int = static_cast<long>(num);
250  long num_dec = static_cast<long>((1+num-num_int)*pow(10.,decimals));
251  TString s_dec = ""; s_dec += num_dec; s_dec.Remove(0,1);
252  TString result="";
253  if(neg<0) result+="-";
254  result+= num_int;
255  if(decimals>0) {
256  result+="."; result+=s_dec;
257  }
258 
259  TString afterdot = result;
260  afterdot.Remove(0,afterdot.First(".")+1);
261  for(int i=0; i<decimals-afterdot.Length(); i++)
262  result += "0";
263  return result;
264 }
265 
266 TString addCommas(double num){
267  TString result(""); result += num;
268  int posdot(result.First('.'));
269  if(posdot==-1) posdot = result.Length();
270  for(int ind(posdot-3); ind > 0; ind -= 3)
271  result.Insert(ind, ",");
272  return result;
273 }
274 
275 long double AddInQuadrature(long double x, long double y){
276  if(fabs(y)>fabs(x)){
277  const long double temp = y;
278  y=x;
279  x=temp;
280  }
281  if(x==0.) return y;
282  const long double rat=y/x;
283  return fabs(x)*sqrt(1.0L+rat*rat);
284 }
285 
286 long double GetMass(long double e, long double px, long double py, long double pz){
287  px/=e; py/=e; pz/=e;
288  return fabs(e)*sqrt(1.0L-px*px-py*py-pz*pz);
289 }
290 
291 long double GetMT(long double m1, long double pt1, long double phi1,
292  long double m2, long double pt2, long double phi2){
293  return sqrt(m1*m1+m2*m2+2.L*(sqrt((m1*m1+pt1*pt1)*(m2*m2+pt2*pt2))-pt1*pt2*cos(phi2-phi1)));
294 }
295 
296 long double GetMT(long double pt1, long double phi1,
297  long double pt2, long double phi2){
298  //Faster calculation in massless case
299  return sqrt(2.L*pt1*pt2*(1.L-cos(phi2-phi1)));
300 }
301 
302 bool Contains(const string& text, const string& pattern){
303  return text.find(pattern) != string::npos;
304 }
305 
306 vector<string> Tokenize(const string& input,
307  const string& tokens){
308  char* ipt(new char[input.size()+1]);
309  memcpy(ipt, input.data(), input.size());
310  ipt[input.size()]=static_cast<char>(0);
311  char* ptr(strtok(ipt, tokens.c_str()));
312  vector<string> output(0);
313  while(ptr!=NULL){
314  output.push_back(ptr);
315  ptr=strtok(NULL, tokens.c_str());
316  }
317  return output;
318 }
319 
320 void get_count_and_uncertainty(TTree& tree,
321  const string& cut,
322  double& count,
323  double& uncertainty){
324  const string hist_name("temp");
325  TH1D temp(hist_name.c_str(), "", 1, -1.0, 1.0);
326  tree.Project(hist_name.c_str(), "0.0", cut.c_str());
327  count=temp.IntegralAndError(0,2,uncertainty);
328 }
329 
330 void AddPoint(TGraph& graph, const double x, const double y){
331  graph.SetPoint(graph.GetN(), x, y);
332 }
333 
334 string execute(const string &cmd){
335  FILE *pipe = popen(cmd.c_str(), "r");
336  if(!pipe) throw runtime_error("Could not open pipe.");
337  const size_t buffer_size = 128;
338  char buffer[buffer_size];
339  string result = "";
340  while(!feof(pipe)){
341  if(fgets(buffer, buffer_size, pipe) != NULL) result += buffer;
342  }
343 
344  pclose(pipe);
345  return result;
346 }
347 
348 string RemoveTrailingNewlines(string str){
349  while(!str.empty() && str.at(str.length()-1) == '\n'){
350  str.erase(str.length()-1);
351  }
352  return str;
353 }
354 
355 vector<double> LinearSpacing(size_t npts, double low, double high){
356  vector<double> pts(npts,low+0.5*(high-low));
357  if(npts>1){
358  double gap = (high-low)/(npts-1.0);
359  for(size_t pt = 0; pt < npts; ++pt){
360  pts.at(pt) = low+pt*gap;
361  }
362  }
363  return pts;
364 }
365 
const long double PI
Definition: utilities.hpp:21
bool dd_small2big(const double_double &left, const double_double &right)
Definition: utilities.cpp:217
string files
Definition: data_combine.py:33
bool Contains(const string &text, const string &pattern)
Definition: utilities.cpp:302
bool id_big2small(const int_double &left, const int_double &right)
Definition: utilities.cpp:213
void get_count_and_uncertainty(TTree &tree, const string &cut, double &count, double &uncertainty)
Definition: utilities.cpp:320
STL namespace.
long double SignedDeltaPhi(long double phi1, long double phi2)
Definition: utilities.cpp:230
vector< TString > dirlist(const TString &folder, const TString &inname, const TString &tag)
Definition: utilities.cpp:182
TString addCommas(double num)
Definition: utilities.cpp:266
long double GetMass(long double e, long double px, long double py, long double pz)
Definition: utilities.cpp:286
long double AddInQuadrature(long double x, long double y)
Definition: utilities.cpp:275
vector< string > Tokenize(const string &input, const string &tokens)
Definition: utilities.cpp:306
TString roundNumber(double num, int decimals, double denom)
Definition: utilities.cpp:245
long double GetMT(long double m1, long double pt1, long double phi1, long double m2, long double pt2, long double phi2)
Definition: utilities.cpp:291
long double DeltaPhi(long double phi1, long double phi2)
Definition: utilities.cpp:225
bool dd_big2small(const double_double &left, const double_double &right)
Definition: utilities.cpp:221
std::pair< int, double > int_double
Definition: utilities.hpp:19
void AddPoint(TGraph &graph, const double x, const double y)
Definition: utilities.cpp:330
std::pair< double, double > double_double
Definition: utilities.hpp:20
vector< double > LinearSpacing(size_t npts, double low, double high)
Definition: utilities.cpp:355
float cross_section(const TString &file)
Definition: utilities.cpp:28
bool eigen2x2(float matrix[2][2], float &eig1, float &eig2)
Definition: utilities.cpp:204
float dR(float eta1, float eta2, float phi1, float phi2)
Definition: utilities.cpp:241
string RemoveTrailingNewlines(string str)
Definition: utilities.cpp:348
string execute(const string &cmd)
Definition: utilities.cpp:334