babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
utilities.cc
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 // utilities - Various functions used accross the code
3 //----------------------------------------------------------------------------
4 
5 
6 #include <cmath>
7 #include <deque>
8 #include <iostream>
9 #include <string>
10 #include <stdexcept>
11 #include <iomanip> // setw
12 
13 #include "TCollection.h"
14 #include "TFile.h"
15 #include "TGraph.h"
16 #include "TH1D.h"
17 #include "TList.h"
18 #include "TString.h"
19 #include "TSystemDirectory.h"
20 #include "TSystemFile.h"
21 #include "TSystem.h"
22 #include "TTree.h"
23 #include "TChain.h"
24 #include "TRegexp.h"
25 
26 #include "cross_sections.hh"
27 #include "utilities.hh"
28 
29 using namespace std;
30 
32 int change_branch_one(TString indir, TString name, TString outdir, vector<TString> var_type, vector<TString> var,
33  vector<vector<TString> > var_val, int totentries){
34 
35  if(var_type.size()!=var.size() || var_type.size()!=var_val.size())
36  { cout<<"[Change Branch One] ERROR: Branch vectors are not the same size"<<endl; exit(0); }
37 
38  const int nvar = var.size();
39 
40  //Setup
41  vector<varType> ivar_type;
42  vector<bool> isLep(nvar, false);
43  vector<vector<bool> > multiply(nvar);
44  for(int isetup=0; isetup<nvar; isetup++){
45  size_t vlength(var_val[isetup].size());
46  multiply[isetup] = vector<bool>(vlength, false);
47  //Set multiply
48  for(unsigned int idx=0; idx<vlength; idx++){
49  if(var_val[isetup][idx].BeginsWith("*") || var_val[isetup][idx].EndsWith("*")){
50  var_val[isetup][idx].Remove(TString::kBoth,'*');
51  multiply[isetup][idx]=true;
52  }
53  }
54 
55  //Handle bools
56  if(var_type[isetup]=="bool" || var_type[isetup]=="vbool"){
57  for(unsigned int idx=0; idx<var_val[isetup].size(); idx++){
58  if(var_val[isetup][idx]=="true")
59  var_val[isetup][idx]="1";
60  else if(var_val[isetup][idx]=="false")
61  var_val[isetup][idx]="0";
62  }
63  }
64 
65  // Converting string types to int and bool for speed
66  if(var[isetup].Contains("_lep")) isLep[isetup] = true;
67 
68  if(var_type[isetup]=="int") ivar_type.push_back(kInt);
69  else if(var_type[isetup]=="float") ivar_type.push_back(kFloat);
70  else if(var_type[isetup]=="double") ivar_type.push_back(kDouble);
71  else if(var_type[isetup]=="bool") ivar_type.push_back(kBool);
72  else if(var_type[isetup]=="vint") ivar_type.push_back(kvInt);
73  else if(var_type[isetup]=="vfloat") ivar_type.push_back(kvFloat);
74  else if(var_type[isetup]=="vdouble") ivar_type.push_back(kvDouble);
75  else if(var_type[isetup]=="vbool") ivar_type.push_back(kvBool);
76  else {
77  cout<<"var_type "<<var_type[isetup]<<" not supported. Exiting"<<endl;
78  exit(1);
79  }
80  } // Loop over variables
81 
82  //Set up old tree and branch
83  TFile* oldfile = new TFile(indir+name);
84  TTree* oldtree = static_cast<TTree*>(oldfile->Get("tree"));
85  TTree* oldtreeglobal = static_cast<TTree*>(oldfile->Get("treeglobal"));
86 
87  vector<int> new_var_int_(nvar,-999);
88  vector<float> new_var_flt_(nvar,-999);
89  vector<double> new_var_dbl_(nvar,-999);
90  deque<bool> new_var_bool_(nvar, false); // vector<bool>is not a vector in C++... so can't pass bools by reference
91  vector<vector<int> * > new_var_vint_(nvar);
92  vector<vector<float> * > new_var_vflt_(nvar);
93  vector<vector<double> * > new_var_vdbl_(nvar);
94  vector<vector<bool> * > new_var_vbool_(nvar);
95 
96  // Hack to protect total weight from NaN, and not include w_pu
97  float w_lumi(1.), w_lumi_old(1.), w_lep(1.), w_fs_lep(1.), w_btag_old(1.), w_corr(1.);
98  float w_isr_old=1., w_pu_old=1.;
99  //Branches
100  int nleps_=0, mgluino_(0);
101  float eff_trig_(0), eff_jetid_(0), nisr_(0);
102  oldtree->SetBranchAddress("eff_jetid",&eff_jetid_);
103  oldtree->SetBranchAddress("eff_trig",&eff_trig_);
104  oldtree->SetBranchAddress("nleps",&nleps_);
105  oldtree->SetBranchAddress("nisr",&nisr_);
106  oldtree->SetBranchAddress("mgluino",&mgluino_);
107 
108  for(int ibch=0; ibch<nvar; ibch++){
109  switch (ivar_type[ibch]){
110  default:
111  case kInt:
112  new_var_int_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_int_[ibch]); break;
113  case kFloat:
114  new_var_flt_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_flt_[ibch]); break;
115  case kDouble:
116  new_var_dbl_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_dbl_[ibch]); break;
117  case kBool:
118  new_var_bool_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_bool_[ibch]); break;
119  case kvInt:
120  new_var_vint_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_vint_[ibch]); break;
121  case kvFloat:
122  new_var_vflt_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_vflt_[ibch]); break;
123  case kvDouble:
124  new_var_vdbl_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_vdbl_[ibch]); break;
125  case kvBool:
126  new_var_vbool_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_vbool_[ibch]);break;
127  }
128  }
129 
130  //Set up new tree
131  name.ReplaceAll(".root","_renorm.root");
132  TFile* newfile = new TFile(outdir+name,"recreate");
133  TTree* newtree = oldtree->CloneTree(0);
134 
135  //Loop and fill events with new weights
136  int nentries = oldtree->GetEntries();
137  time_t begtime, endtime;
138  time(&begtime);
139  for(int i=0; i<nentries; i++){
140  //if(i==10) exit(0);
141  oldtree->GetEntry(i);
142 
143  w_corr = 1;
144  if(i%500000==0 || i==nentries-1) {
145  time(&endtime);
146  int seconds(difftime(endtime,begtime));
147  cout<<"Doing entry "<<setw(10)<<addCommas(i+1)<<" of "<<addCommas(nentries)
148  <<" Took "<<setw(6)<<seconds<<" seconds at "
149  <<setw(4)<<roundNumber(i,1,seconds*1000.)<<" kHz"<<endl;
150  }
151 
152  // float minpdf(1e10);
153 
154  //Set vars
155  for(int iset=0; iset<nvar; iset++){
156  // // Hack to recompute sys_pdf[1] which had a 1e-3 cut
157  // if(var[iset].Contains("w_pdf")){
158  // for(unsigned int isys=0;isys<new_var_vflt_[iset]->size();isys++)
159  // if(new_var_vflt_[iset]->at(isys) < minpdf) minpdf = new_var_vflt_[iset]->at(isys);
160  // }
161 
163  if(isLep[iset] && nleps_!=0) {
164  // Hack to protect total weight from NaN, and not include w_pu
165  if(var[iset].Contains("w_lep")) w_lep = noNaN(new_var_flt_[iset]);
166  if(var[iset].Contains("w_fs_lep")) w_fs_lep = noNaN(new_var_flt_[iset]);
167  continue; // For lepton scale factors
168  }
169 
171  if(var[iset].Contains("w_pu")) {w_pu_old = noNaN(new_var_flt_[iset]); w_corr *= var_val[iset][0].Atof(); }
172  if(var[iset].Contains("w_isr")) {w_isr_old = noNaN(new_var_flt_[iset]); w_corr *= var_val[iset][0].Atof(); }
173  //if(var[iset].Contains("w_toppt")) {w_toppt_old = noNaN(new_var_flt_[iset]); w_corr *= var_val[iset][0].Atof(); }
174  if(var[iset].Contains("w_btag")) {w_btag_old = noNaN(new_var_flt_[iset]); w_corr *= var_val[iset][0].Atof(); }
175  if(var[iset].Contains("w_lumi")) {w_lumi_old = noNaN(new_var_flt_[iset]); }
176 
177  // // Hack for empty pdf branches
178  // if(var[iset].Contains("w_pdf")){
179  // if(new_var_vflt_[iset]->size()==0){
180  // new_var_vflt_[iset]->resize(100,1);
181  // if (i==0) cout<<"\n[Change Branch One] WARNING: Empty branch of \"w_pdf\". Setting values to 1"<<endl;
182  // continue;
183  // }
184  // }
185  // else if(var[iset].Contains("sys_pdf")){
186  // if(new_var_vflt_[iset]->size()==0){
187  // new_var_vflt_[iset]->resize(2,1);
188  // if (i==0) cout<<"[Change Branch One] WARNING: Empty branch of \"sys_pdf\". Setting values to 1"<<endl;
189  // continue;
190  // }
191  // }
192  for(unsigned int vidx=0; vidx<var_val[iset].size(); vidx++){
193  if(!multiply[iset][vidx]){
194  switch (ivar_type[iset]){
195  default:
196  case kInt: new_var_int_[iset] = var_val[iset][vidx].Atoi(); break;
197  case kFloat: new_var_flt_[iset] = var_val[iset][vidx].Atof(); break;
198  case kDouble: new_var_dbl_[iset] = static_cast<double>(var_val[iset][vidx].Atof()); break;
199  case kBool: new_var_bool_[iset] = var_val[iset][vidx].Atoi(); break;
200  case kvInt: new_var_vint_[iset]->at(vidx) = var_val[iset][vidx].Atoi(); break;
201  case kvFloat: new_var_vflt_[iset]->at(vidx) = var_val[iset][vidx].Atof(); break;
202  case kvDouble: new_var_vdbl_[iset]->at(vidx) = static_cast<double>(var_val[iset][vidx].Atof()); break;
203  case kvBool: new_var_vbool_[iset]->at(vidx) = var_val[iset][vidx].Atoi(); break;
204  }
205  } else {
206  switch (ivar_type[iset]){
207  default:
208  case kInt: new_var_int_[iset] *= var_val[iset][vidx].Atoi(); break;
209  case kFloat: new_var_flt_[iset] *= var_val[iset][vidx].Atof(); break;
210  case kDouble: new_var_dbl_[iset] *= static_cast<double>(var_val[iset][vidx].Atof()); break;
211  case kvInt: new_var_vint_[iset]->at(vidx) *= var_val[iset][vidx].Atoi(); break;
212  case kvFloat: new_var_vflt_[iset]->at(vidx) *= var_val[iset][vidx].Atof(); break;
213  case kvDouble: new_var_vdbl_[iset]->at(vidx) *= static_cast<double>(var_val[iset][vidx].Atof()); break;
214  case kBool:
215  case kvBool:
216  cout<<"[Change Branch One] WARNING: You cannot multiply Booleans. Skipping branch"<<endl;
217  break;
218  }
219  } // if multiply
220  if(ivar_type[iset] == kFloat) {
221  if(isnan(new_var_flt_[iset])) {
222  new_var_flt_[iset] = 1.;
223  if(i==0) cout<<endl<<"==== WARNING: Branch \""<<var[iset]<<"\" is NaN ====="<<endl<<endl;
224  } else new_var_flt_[iset] = noNaN(new_var_flt_[iset]);
225  }
226  if(ivar_type[iset] == kvFloat) new_var_vflt_[iset]->at(vidx) = noNaN(new_var_vflt_[iset]->at(vidx));
227  if (name.Contains("SMS") && !name.Contains("TChi")){
228  vector<float> sys_isr;
229  w_isr_old = wISR(nisr_, sys_isr); // this is the "old" value, since it is in change_weight to calculate weight
230  if(var[iset].Contains("sys_isr")) new_var_vflt_[iset]->at(vidx) = sys_isr[vidx] * var_val[iset][vidx].Atof();
231  if(var[iset].Contains("w_isr")) {
232  new_var_flt_[iset] = w_isr_old * var_val[iset][vidx].Atof();
233  // cout<<"nisr "<<nisr_<<", w_isr "<< w_isr_old <<", sys_isr[0] "<<sys_isr[0]<<" and new w "
234  // << w_isr_old * var_val[iset][vidx].Atof()<<", new sys "<< sys_isr[vidx] * var_val[iset][vidx].Atof()<<endl;
235  }
236  } // it strong SUSY
237  } // Loop over elements in each variable
238  // if(var[iset].Contains("sys_pdf")) new_var_vflt_[iset]->at(1) = minpdf*var_val[iset][1].Atof();
239 
240  // Hack to protect total weight from NaN, and not include w_pu
241  if(var[iset].Contains("w_lep")) {w_lep = new_var_flt_[iset]; w_corr *= var_val[iset][0].Atof(); }
242  if(var[iset].Contains("w_fs_lep")) {w_fs_lep = new_var_flt_[iset]; w_corr *= var_val[iset][0].Atof(); }
243  if(var[iset].Contains("w_lumi")) {
244  w_lumi = new_var_flt_[iset];
245  if(w_lumi_old<0) {
246  w_lumi *= -1;
247  new_var_flt_[iset] *= -1;
248  }
249  w_corr *= w_lumi/w_lumi_old; // Not currently used
250  }
251  } // Loop over variables
252  // Hack to protect total weight from NaN, and not include w_pu
253  for(int iset=0; iset<nvar; iset++)
254  if(var[iset].Contains("weight")){
255  new_var_flt_[iset] = w_lep * w_fs_lep * w_btag_old * w_isr_old * w_pu_old * w_lumi * eff_jetid_ //* eff_trig_
256  * var_val[iset][0].Atof();
257  }
258  newtree->Fill();
259  }
260 
261  unsigned int new_nev_sample(0);
262  float new_xsec(0.);
263  float xsec(0.), exsec(0.);
264  oldtreeglobal->SetBranchAddress("nev_sample", &new_nev_sample);
265  if(mgluino_>0) oldtreeglobal->SetBranchAddress("xsec", &new_xsec);
266  TTree* newtreeglobal = oldtreeglobal->CloneTree(0);
267  long nentriesg = oldtreeglobal->GetEntries();
268  for(int i=0; i<nentriesg; i++){
269  oldtreeglobal->GetEntry(i);
270  new_nev_sample = totentries;
271  if(mgluino_>0) {
272  if(name.Contains("T1") || name.Contains("T5")) xsec::signalCrossSection(mgluino_, xsec, exsec);
273  else xsec::stopCrossSection(mgluino_, xsec, exsec);
274  new_xsec = xsec;
275  }
276  newtreeglobal->Fill();
277  }
278  //Save tree
279  newtree->AutoSave();
280  newtreeglobal->AutoSave();
281  delete oldfile;
282  delete newfile;
283  return nentries;
284 }
285 
286 // Returns list of directorites or files in folder
287 vector<TString> dirlist(const TString &folder,
288  const TString &inname,
289  const TString &tag){
290  TRegexp regex_tag(tag,true), regex_inname(inname, true);
291  TString pwd(gSystem->pwd());
292  vector<TString> v_dirs;
293  TSystemDirectory dir(folder, folder);
294  TList *files = dir.GetListOfFiles();
295  if (files) {
296  TSystemFile *file;
297  TString fname;
298  TIter next(files);
299  while ((file=static_cast<TSystemFile*>(next()))) {
300  fname = file->GetName();
301  if (inname=="dir") {
302  if ((file->IsDirectory() && !fname.Contains(".") && fname.Contains(regex_tag))) v_dirs.push_back(fname);
303  } else if(fname.Contains(regex_inname)) v_dirs.push_back(fname);
304  }
305  } // if(files)
306  gSystem->cd(pwd); // The TSystemDirectory object seems to change current folder
307  return v_dirs;
308 }
309 
310 int change_branch_one(TString indir, TString name, TString outdir, vector<TString> var_type, vector<TString> var,
311  vector<TString> var_val, TString newname){
312 
313  if(var_type.size()!=var.size() || var_type.size()!=var_val.size())
314  { cout<<"[Change Branch One] Error: Branch vectors are not the same size"<<endl; exit(0); }
315 
316  const int nvar = var.size();
317  bool verbose = false;
318 
319  //Setup
320  vector<bool> multiply(nvar,false);
321  for(int isetup=0; isetup<nvar; isetup++){
322  //Set multiply
323  if(var_val[isetup].BeginsWith("*") || var_val[isetup].EndsWith("*")){
324  var_val[isetup].Remove(TString::kBoth,'*');
325  multiply[isetup]=true;
326  }
327 
328  //Handle bools
329  if(var_val[isetup]=="true")
330  var_val[isetup]="1";
331  else if(var_val[isetup]=="false")
332  var_val[isetup]="0";
333  }
334 
335  //Set up old tree and branch
336  TFile* oldfile = new TFile(indir+"/"+name);
337  TTree* oldtree = static_cast<TTree*>(oldfile->Get("tree"));
338  TTree* oldtreeglobal = static_cast<TTree*>(oldfile->Get("treeglobal"));
339 
340  vector<int> new_var_int_(nvar,-999);
341  vector<float> new_var_flt_(nvar,-999);
342  vector<double> new_var_dbl_(nvar,-999);
343  deque<bool> new_var_bool_(nvar, false); // vector<bool>is not a vector in C++... so can't pass bools by reference
344  vector<vector<int> * > new_var_vint_(nvar);
345  vector<vector<float> * > new_var_vflt_(nvar);
346  vector<vector<double> * > new_var_vdbl_(nvar);
347  vector<vector<bool> * > new_var_vbool_(nvar);
348 
349  //Branches
350  int nleps_=0;
351  oldtree->SetBranchAddress("nleps",&nleps_);
352 
353  for(int ibch=0; ibch<nvar; ibch++){
354  if(var_type[ibch]=="int") { new_var_int_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_int_[ibch]); }
355  else if(var_type[ibch]=="float") { new_var_flt_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_flt_[ibch]); }
356  else if(var_type[ibch]=="double") { new_var_dbl_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_dbl_[ibch]); }
357  else if(var_type[ibch]=="bool") { new_var_bool_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_bool_[ibch]);
358  if(multiply[ibch]) cout<<"[Change Branch One] Warning: Multiplying branch of type \"bool\". Skipping branch."<<endl; }
359  else if(var_type[ibch]=="vint") { new_var_vint_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_vint_[ibch]); }
360  else if(var_type[ibch]=="vfloat") { new_var_vflt_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_vflt_[ibch]); }
361  else if(var_type[ibch]=="vdouble") { new_var_vdbl_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_vdbl_[ibch]); }
362  else if(var_type[ibch]=="vbool") { new_var_vbool_[ibch] = 0; oldtree->SetBranchAddress(var[ibch], &new_var_vbool_[ibch]);
363  if(multiply[ibch]) cout<<"[Change Branch One] Warning: Multiplying branch of type \"vbool\". Skipping branch."<<endl; }
364 
365  else {cout<<"[Change Branch One] Error: Branch type invalid. Use only \"int\", \"float\", \"double\", \"bool\", \"vint\", \"vfloat\", \"vdouble\", or\"vbool\""<<endl; exit(0);}
366  }
367 
368  //Set up new tree
369  if(newname=="empty"){
370  newname = name;
371  newname.ReplaceAll(".root","_mod.root");
372  }
373  TFile* newfile = new TFile(outdir+newname,"recreate");
374  TTree* newtree = oldtree->CloneTree(0);
375  TTree* newtreeglobal = oldtreeglobal->CloneTree();
376 
377  //Loop and fill events with new weights
378  int nentries = oldtree->GetEntries();
379  for(int i=0; i<nentries; i++){
380  oldtree->GetEntry(i);
381 
382  if(verbose && ((i<100&&i%10==0) || (i<1000&&i%100==0) || (i<10000&&i%1000==0) || (i%10000==0))){
383  if(isatty(1)){
384  printf("\r[Change Branch One] Processsing File: %i / %i (%i%%)",i,nentries,static_cast<int>((i*100./nentries)));
385  fflush(stdout);
386  if((i<100&&i+10>=nentries) || (i<1000&&i+100>=nentries) || (i<10000&&i+1000>=nentries) || (i>=10000&&i+10000>=nentries)) printf("\n");
387  }
388  }
389 
390  //Set vars
391  for(int iset=0; iset<nvar; iset++){
392  if(var[iset].Contains("_lep")&&nleps_!=0) continue; // For lepton scale factors
393  if(!multiply[iset]){
394  if(var_type[iset]=="int") new_var_int_[iset] = var_val[iset].Atoi();
395  else if(var_type[iset]=="float") new_var_flt_[iset] = var_val[iset].Atof();
396  else if(var_type[iset]=="double") new_var_dbl_[iset] = static_cast<double>(var_val[iset].Atof());
397  else if(var_type[iset]=="bool") new_var_bool_[iset] = var_val[iset].Atoi();
398  else if(var_type[iset]=="vint")
399  for(unsigned int vidx=0; vidx<new_var_vint_[iset]->size(); vidx++)
400  new_var_vint_[iset]->at(vidx) = var_val[iset].Atoi();
401  else if(var_type[iset]=="vfloat")
402  for(unsigned int vidx=0; vidx<new_var_vflt_[iset]->size(); vidx++)
403  new_var_vflt_[iset]->at(vidx) = var_val[iset].Atof();
404  else if(var_type[iset]=="vdouble")
405  for(unsigned int vidx=0; vidx<new_var_vdbl_[iset]->size(); vidx++)
406  new_var_vdbl_[iset]->at(vidx) = static_cast<double>(var_val[iset].Atof());
407  else if(var_type[iset]=="vbool")
408  for(unsigned int vidx=0; vidx<new_var_vbool_[iset]->size(); vidx++)
409  new_var_vbool_[iset]->at(vidx) = var_val[iset].Atoi();
410  }
411  else {
412  if(var_type[iset]=="int") new_var_int_[iset] *= var_val[iset].Atoi();
413  else if(var_type[iset]=="float") new_var_flt_[iset] *= var_val[iset].Atof();
414  else if(var_type[iset]=="double") new_var_dbl_[iset] *= var_val[iset].Atof();
415  else if(var_type[iset]=="vint")
416  for(unsigned int vidx=0; vidx<new_var_vint_[iset]->size(); vidx++)
417  new_var_vint_[iset]->at(vidx) *= var_val[iset].Atoi();
418  else if(var_type[iset]=="vfloat")
419  for(unsigned int vidx=0; vidx<new_var_vflt_[iset]->size(); vidx++)
420  new_var_vflt_[iset]->at(vidx) *= var_val[iset].Atof();
421  else if(var_type[iset]=="vdouble")
422  for(unsigned int vidx=0; vidx<new_var_vdbl_[iset]->size(); vidx++)
423  new_var_vdbl_[iset]->at(vidx) *= static_cast<double>(var_val[iset].Atof());
424  }
425  }
426  newtree->Fill();
427  }
428 
429  //Save tree
430  newtree->AutoSave();
431  newtreeglobal->AutoSave();
432  delete oldfile;
433  delete newfile;
434  return nentries;
435 }
436 
437 bool eigen2x2(float matrix[2][2], float &eig1, float &eig2){
438  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];
439  if(root<0) return false;
440 
441  eig1 = (matrix[0][0]+matrix[1][1]+sqrt(root))/2.;
442  eig2 = (matrix[0][0]+matrix[1][1]-sqrt(root))/2.;
443  return true;
444 }
445 
446 bool id_big2small(const int_double& left, const int_double& right){
447  return left.second > right.second;
448 }
449 
450 bool dd_small2big(const double_double& left, const double_double& right){
451  return left.first < right.first;
452 }
453 
454 bool dd_big2small(const double_double& left, const double_double& right){
455  return left.first > right.first;
456 }
457 
458 long double DeltaPhi(long double phi1, long double phi2){
459  long double dphi = fmod(fabs(phi2-phi1), 2.L*PI);
460  return dphi>PI ? 2.L*PI-dphi : dphi;
461 }
462 
463 long double SignedDeltaPhi(long double phi1, long double phi2){
464  long double dphi = fmod(phi2-phi1, 2.L*PI);
465  if(dphi>PI){
466  return dphi-2.L*PI;
467  }else if(dphi<-PI){
468  return dphi+2.L*PI;
469  }else{
470  return dphi;
471  }
472 }
473 
474 float dR(float eta1, float eta2, float phi1, float phi2) {
475  return AddInQuadrature(eta1-eta2, DeltaPhi(phi1,phi2));
476 }
477 
478 TString roundNumber(double num, int decimals, double denom){
479  if(denom==0) return " - ";
480  double neg = 1; if(num*denom<0) neg = -1;
481  num /= neg*denom; num += 0.5*pow(10.,-decimals);
482  long num_int = static_cast<long>(num);
483  long num_dec = static_cast<long>((1+num-num_int)*pow(10.,decimals));
484  TString s_dec = ""; s_dec += num_dec; s_dec.Remove(0,1);
485  TString result="";
486  if(neg<0) result+="-";
487  result+= num_int;
488  if(decimals>0) {
489  result+="."; result+=s_dec;
490  }
491 
492  TString afterdot = result;
493  afterdot.Remove(0,afterdot.First(".")+1);
494  for(int i=0; i<decimals-afterdot.Length(); i++)
495  result += "0";
496  return result;
497 }
498 
499 TString addCommas(double num){
500  TString result(""); result += num;
501  int posdot(result.First('.'));
502  if(posdot==-1) posdot = result.Length();
503  for(int ind(posdot-3); ind > 0; ind -= 3)
504  result.Insert(ind, ",");
505  return result;
506 }
507 
508 long double AddInQuadrature(long double x, long double y){
509  if(fabs(y)>fabs(x)){
510  const long double temp = y;
511  y=x;
512  x=temp;
513  }
514  if(x==0.) return y;
515  const long double rat=y/x;
516  return fabs(x)*sqrt(1.0L+rat*rat);
517 }
518 
519 long double GetMass(long double e, long double px, long double py, long double pz){
520  px/=e; py/=e; pz/=e;
521  return fabs(e)*sqrt(1.0L-px*px-py*py-pz*pz);
522 }
523 
524 long double GetMT(long double m1, long double pt1, long double phi1,
525  long double m2, long double pt2, long double phi2){
526  return sqrt(m1*m1+m2*m2+2.L*(sqrt((m1*m1+pt1*pt1)*(m2*m2+pt2*pt2))-pt1*pt2*cos(phi2-phi1)));
527 }
528 
529 long double GetMT(long double pt1, long double phi1,
530  long double pt2, long double phi2){
531  //Faster calculation in massless case
532  return sqrt(2.L*pt1*pt2*(1.L-cos(phi2-phi1)));
533 }
534 
535 bool Contains(const string& text, const string& pattern){
536  return text.find(pattern) != string::npos;
537 }
538 
539 vector<string> Tokenize(const string& input,
540  const string& tokens){
541  char* ipt(new char[input.size()+1]);
542  memcpy(ipt, input.data(), input.size());
543  ipt[input.size()]=static_cast<char>(0);
544  char* ptr(strtok(ipt, tokens.c_str()));
545  vector<string> output(0);
546  while(ptr!=NULL){
547  output.push_back(ptr);
548  ptr=strtok(NULL, tokens.c_str());
549  }
550  return output;
551 }
552 
553 void get_count_and_uncertainty(TTree& tree,
554  const string& cut,
555  double& count,
556  double& uncertainty){
557  const string hist_name("temp");
558  TH1D temp(hist_name.c_str(), "", 1, -1.0, 1.0);
559  tree.Project(hist_name.c_str(), "0.0", cut.c_str());
560  count=temp.IntegralAndError(0,2,uncertainty);
561 }
562 
563 void AddPoint(TGraph& graph, const double x, const double y){
564  graph.SetPoint(graph.GetN(), x, y);
565 }
566 
567 string execute(const string &cmd){
568  FILE *pipe = popen(cmd.c_str(), "r");
569  if(!pipe) throw runtime_error("Could not open pipe.");
570  const size_t buffer_size = 128;
571  char buffer[buffer_size];
572  string result = "";
573  while(!feof(pipe)){
574  if(fgets(buffer, buffer_size, pipe) != NULL) result += buffer;
575  }
576 
577  pclose(pipe);
578  return result;
579 }
580 
581 string RemoveTrailingNewlines(string str){
582  while(!str.empty() && str.at(str.length()-1) == '\n'){
583  str.erase(str.length()-1);
584  }
585  return str;
586 }
587 
588 vector<double> LinearSpacing(size_t npts, double low, double high){
589  vector<double> pts(npts,low+0.5*(high-low));
590  if(npts>1){
591  double gap = (high-low)/(npts-1.0);
592  for(size_t pt = 0; pt < npts; ++pt){
593  pts.at(pt) = low+pt*gap;
594  }
595  }
596  return pts;
597 }
598 
599 TString hoursMinSec(long seconds){
600  int minutes((seconds/60)%60), hours(seconds/3600);
601  TString hhmmss("");
602  if(hours<10) hhmmss += "0";
603  hhmmss += hours; hhmmss += ":";
604  if(minutes<10) hhmmss += "0";
605  hhmmss += minutes; hhmmss += ":";
606  if((seconds%60)<10) hhmmss += "0";
607  hhmmss += seconds%60;
608 
609  return hhmmss;
610 }
611 
612 float wISR(int nisr, vector<float> & sys_isr){
613  float isr_wgt = -999.;
614  const float isr_norm_tt = 1.117; // normalization so that is roughly correct even without renormalization
615  if (nisr==0) isr_wgt = 1.;
616  else if (nisr==1) isr_wgt = 0.882;
617  else if (nisr==2) isr_wgt = 0.792;
618  else if (nisr==3) isr_wgt = 0.702;
619  else if (nisr==4) isr_wgt = 0.648;
620  else if (nisr==5) isr_wgt = 0.601;
621  else if (nisr>=6) isr_wgt = 0.515;
622 
623  //assign relative unc = 50% of the deviation from flat
624  float absolute_unc = (1-isr_wgt)/2.;
625  sys_isr.resize(2);
626  sys_isr[0] = isr_norm_tt*(isr_wgt+absolute_unc);
627  sys_isr[1] = isr_norm_tt*(isr_wgt-absolute_unc);
628 
629  return isr_wgt*isr_norm_tt;
630 }
631 
632 void mergeNtuples(vector<TString> ntuples, TString outname){
633 
634  // Merging tree TTrees
635  TChain chain("tree");
636  for(size_t ind=0; ind<ntuples.size(); ind++) chain.Add(ntuples[ind]);
637  chain.Merge(outname);
638 
639  // Merging treeglobal TTrees
640  TChain chaing("treeglobal");
641  for(size_t ind=0; ind<ntuples.size(); ind++) chaing.Add(ntuples[ind]);
642  TTree *tglobal = chaing.CopyTree("1");
643  tglobal->SetDirectory(0);
644  TFile rootfile(outname, "UPDATE");
645  rootfile.cd();
646  tglobal->Write();
647  rootfile.Close();
648 
649 }
TString addCommas(double num)
Definition: utilities.cc:499
tuple outdir
Definition: nev_check.py:24
void AddPoint(TGraph &graph, const double x, const double y)
Definition: utilities.cc:563
vector< double > LinearSpacing(size_t npts, double low, double high)
Definition: utilities.cc:588
int change_branch_one(TString indir, TString name, TString outdir, vector< TString > var_type, vector< TString > var, vector< vector< TString > > var_val, int totentries)
Definition: utilities.cc:32
string execute(const string &cmd)
Definition: utilities.cc:567
void stopCrossSection(int stop_mass, float &xsec, float &xsec_unc)
STL namespace.
TString roundNumber(double num, int decimals, double denom)
Definition: utilities.cc:478
void get_count_and_uncertainty(TTree &tree, const string &cut, double &count, double &uncertainty)
Definition: utilities.cc:553
string RemoveTrailingNewlines(string str)
Definition: utilities.cc:581
string cmd
Moving file.
float dR(float eta1, float eta2, float phi1, float phi2)
Definition: utilities.cc:474
std::pair< int, double > int_double
Definition: utilities.hh:20
void signalCrossSection(int glu_mass, float &xsec, float &xsec_unc)
vector< TString > dirlist(const TString &folder, const TString &inname, const TString &tag)
Definition: utilities.cc:287
T noNaN(T val, T defval=1.)
Definition: utilities.hh:61
bool dd_big2small(const double_double &left, const double_double &right)
Definition: utilities.cc:454
const long double PI
Definition: utilities.hh:22
tuple ind
Definition: resubmit.py:140
vector< string > Tokenize(const string &input, const string &tokens)
Definition: utilities.cc:539
void mergeNtuples(vector< TString > ntuples, TString outname)
Definition: utilities.cc:632
bool Contains(const string &text, const string &pattern)
Definition: utilities.cc:535
long double AddInQuadrature(long double x, long double y)
Definition: utilities.cc:508
long double GetMT(long double m1, long double pt1, long double phi1, long double m2, long double pt2, long double phi2)
Definition: utilities.cc:524
bool id_big2small(const int_double &left, const int_double &right)
Definition: utilities.cc:446
bool eigen2x2(float matrix[2][2], float &eig1, float &eig2)
Definition: utilities.cc:437
long double GetMass(long double e, long double px, long double py, long double pz)
Definition: utilities.cc:519
float wISR(int nisr, vector< float > &sys_isr)
Definition: utilities.cc:612
TString hoursMinSec(long seconds)
Definition: utilities.cc:599
long double SignedDeltaPhi(long double phi1, long double phi2)
Definition: utilities.cc:463
long double DeltaPhi(long double phi1, long double phi2)
Definition: utilities.cc:458
bool dd_small2big(const double_double &left, const double_double &right)
Definition: utilities.cc:450
std::pair< double, double > double_double
Definition: utilities.hh:21