ra4_stats  0341147a0dc35f80f4e12c6003afb76a38e2ed6e
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utilities.cpp
Go to the documentation of this file.
1 #include "utilities.hpp"
2 
3 #include <cstdlib>
4 #include <cstring>
5 #include <stdlib.h>
6 #include <unistd.h>
7 
8 #include <string>
9 #include <vector>
10 #include <memory>
11 
12 #include <unistd.h>
13 
14 #include "TTree.h"
15 #include "TH1D.h"
16 
17 #include "RooWorkspace.h"
18 
19 using namespace std;
20 
21 void parseMasses(const string &prs, int &mglu, int &mlsp){
22  mglu = stoi(prs.substr(prs.find("ino-")+4,prs.find("_mLSP")-prs.find("ino-")-4));
23  mlsp = stoi(prs.substr(prs.find("LSP-")+4,prs.find("_Tune")-prs.find("LSP-")-4));
24 }
25 
26 bool Contains(const string &str, const string &pat){
27  return str.find(pat) != string::npos;
28 }
29 
30 bool StartsWith(const string &str, const string &pat){
31  return str.find(pat) == 0;
32 }
33 
34 void ReplaceAll(string &str, const string &orig, const string &rep){
35  size_t loc = 0;
36  while ((loc = str.find(orig, loc)) != string::npos) {
37  str.replace(loc, orig.length(), rep);
38  loc += rep.length();
39  }
40 }
41 
42 void RmCutOn(string &cut, const string &to_rm, const string &rep){
43  size_t loc = 0;
44  while ((loc = cut.find(to_rm, loc)) != string::npos) {
45  size_t end = cut.find_first_of(")&| ", loc);
46  cut.replace(loc, end-loc, rep);
47  loc += rep.length();
48  }
49 }
50 
51 size_t MaxIndex(const vector<double> &v){
52  if(v.size() == 0) return -1;
53  size_t imax = 0;
54  for(size_t i = 1; i < v.size(); ++i){
55  if(v.at(i) > v.at(imax)){
56  imax = i;
57  }
58  }
59  return imax;
60 }
61 
62 void DefineSet(RooWorkspace &w,
63  const string &set_name,
64  const vector<string> &var_names){
65  if(var_names.size()==0){
66  w.defineSet(set_name.c_str(), "");
67  }else{
68  string cat_names = var_names.at(0);
69  for(size_t ivar = 1; ivar < var_names.size(); ++ivar){
70  cat_names += ("," + var_names.at(ivar));
71  }
72  w.defineSet(set_name.c_str(), cat_names.c_str());
73  }
74 }
75 
76 void GetCountAndUncertainty(TTree &tree,
77  const Cut &cut,
78  double &count,
79  double &uncertainty){
80  const string hist_name{"temp"};
81  TH1D temp{hist_name.c_str(), "", 1, -1.0, 1.0};
82  temp.Sumw2();
83  tree.Project(hist_name.c_str(), "0.", static_cast<const char *>(cut));
84  count=temp.IntegralAndError(0,2,uncertainty);
85 }
86 
87 string execute(const string &cmd){
88  FILE *pipe = popen(cmd.c_str(), "r");
89  if(!pipe) ERROR("Could not open pipe.");
90  const size_t buffer_size = 128;
91  char buffer[buffer_size];
92  string result = "";
93  while(!feof(pipe)){
94  if(fgets(buffer, buffer_size, pipe) != NULL) result += buffer;
95  }
96 
97  pclose(pipe);
98  return result;
99 }
100 
101 vector<string> Tokenize(const string& input,
102  const string& tokens){
103  char* ipt(new char[input.size()+1]);
104  memcpy(ipt, input.data(), input.size());
105  ipt[input.size()]=static_cast<char>(0);
106  char* ptr(strtok(ipt, tokens.c_str()));
107  vector<string> output(0);
108  while(ptr!=NULL){
109  output.push_back(ptr);
110  ptr=strtok(NULL, tokens.c_str());
111  }
112  return output;
113 }
114 
115 string ChangeExtension(string path, const string &new_ext){
116  auto pos = path.rfind(".");
117  if(pos == string::npos){
118  path += new_ext;
119  }else{
120  auto count = path.size() - pos;
121  path = path.replace(pos, count, new_ext);
122  }
123  return path;
124 }
125 
126 string MakeDir(string prefix){
127  prefix += "XXXXXX";
128  char *dir_name = new char[prefix.size()];
129  if(dir_name == nullptr) ERROR("Could not allocate directory name");
130  strcpy(dir_name, prefix.c_str());
131  mkdtemp(dir_name);
132  prefix = dir_name;
133  delete[] dir_name;
134  return prefix;
135 }
string MakeDir(string prefix)
Definition: utilities.cpp:126
void GetCountAndUncertainty(TTree &tree, const Cut &cut, double &count, double &uncertainty)
Definition: utilities.cpp:76
void parseMasses(const string &prs, int &mglu, int &mlsp)
Definition: utilities.cpp:21
string ChangeExtension(string path, const string &new_ext)
Definition: utilities.cpp:115
Definition: cut.hpp:8
STL namespace.
size_t MaxIndex(const vector< double > &v)
Definition: utilities.cpp:51
void ReplaceAll(string &str, const string &orig, const string &rep)
Definition: utilities.cpp:34
#define ERROR(x)
Definition: utilities.hpp:15
vector< string > Tokenize(const string &input, const string &tokens)
Definition: utilities.cpp:101
void DefineSet(RooWorkspace &w, const string &set_name, const vector< string > &var_names)
Definition: utilities.cpp:62
bool StartsWith(const string &str, const string &pat)
Definition: utilities.cpp:30
tuple mlsp
Definition: change_r.py:18
tuple mglu
Definition: change_r.py:17
void RmCutOn(string &cut, const string &to_rm, const string &rep)
Definition: utilities.cpp:42
bool Contains(const string &str, const string &pat)
Definition: utilities.cpp:26
string execute(const string &cmd)
Definition: utilities.cpp:87