Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
noi_fnc.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file noi_fnc.h
1 // David Stewart, Dec 1 2022
2 // A way to pass
3 // used in conveinece for plotting
4 #ifndef noi_fnc__h
5 #define noi_fnc__h
6 
7 #include "noiDict.h"
8 #include "noi_fmt.h"
9 
10 string voi_stem(const string& path, const string& ext="") {
11  // copied from https://stackoverflow.com/questions/8520560/get-a-file-name-from-a-path
12  std::string base_filename = path.substr(path.find_last_of("/\\") + 1);
13  std::string::size_type const p(base_filename.find_last_of('.'));
14  std::string file_without_extension = base_filename.substr(0, p) + ext;
15  return file_without_extension;
16 }
17 
18 
19 const char* noiUniqueName(int i=0,const char* prefix="__pause_") {
20  while (gDirectory->FindObjectAny(Form("%s__%i",prefix,i))!=nullptr) ++i;
21  return Form("%s__%i",prefix,i);
22 };
23 void noiPause(int i=0) {
24  TCanvas* c = new TCanvas( noiUniqueName(i+100), noiUniqueName(i+100), 100,100);
25  c->SetFillColor(kCyan);
26  c->SetFillStyle(1001);
27  c->Draw();
28  c->WaitPrimitive();
29 };
30 
31 void noiDrawTLatex(const char* msg, double x, double y,
32  noiDict options={}, noiDict dict= {{
33  "TextColor",kBlack, "TextColorAlpha",kBlack, 1., "TextSize",22, "TextFont",43,
34  "TextAlign",12, "TextAngle",0., }})
35 {
36  dict += options;
37  TLatex tlatex;
38  tlatex.SetTextColorAlpha(dict("TextColorAlpha",1),dict("TextColorAlpha",2));
39  tlatex.SetTextAlign(dict("TextAlign"));
40  tlatex.SetTextSize (dict("TextSize"));
41  tlatex.SetTextFont (dict("TextFont"));
42  tlatex.SetTextAngle (dict("TextAngle"));
43  tlatex.DrawLatex(x, y, msg);
44 };
45 TLine* noiDrawTLine(double x0, double y0, double x1, double y1, noiDict options, noiDict dict={{
46  "LineColorAlpha",kBlack,1.0, "LineStyle",1, "LineWidth",1 }})
47 {
48  dict += options;
49  TLine* line = new TLine(x0,y0,x1,y1);
50  noi_fmt(line, options);
51  line->Draw();
52  return line;
53 };
54 string noiStripEnd(string word, string sub) {
55  auto i0 = sub.length();
56  auto i1 = word.length();
57  if (i1 < i0) return word;
58  return (word.substr(i1-i0,i0)==sub)
59  ? word.substr(0,i1-i0)
60  : word;
61 }
62 string noiStripEnds(string word, vector<string> endings) {
63  for (auto sub : endings) {
64  word = noiStripEnd(word,sub);
65  }
66  return word;
67 };
68 string noiStripStart(string word, string sub) {
69  auto i0 = sub.length();
70  auto i1 = word.length();
71  if (i0>i1) return word;
72  word = (word.substr(0,i0) == sub)
73  ? word.substr(i0,i1-i0)
74  : word;
75  return word;
76 };
77 string noiStripStarts(string word, vector<string> startings) {
78  for (auto pre : startings) {
79  word = noiStripStart(word,pre);
80  }
81  return word;
82 };
83 const char* noi_geant05_ascii(int geantid) {
84  switch (geantid) {
85  case 8: return "pi";
86  case 9: return "antipi";
87  case 11: return "K";
88  case 12: return "antiK";
89  case 14: return "p";
90  case 15: return "pbar";
91 
92  case 0: return "pi";
93  case 1: return "antipi";
94  case 2: return "K";
95  case 3: return "antiK";
96  case 4: return "p";
97  case 5: return "pbar";
98  }
99  return "none";
100 };
101 int noi_geant05(int geantid) {
102  switch (geantid) {
103  case 8: return 0;
104  case 9: return 1;
105  case 11: return 2;
106  case 12: return 3;
107  case 14: return 4;
108  case 15: return 5;
109  }
110  return -1;
111 };
112 const char* noi_geant05_greek(int geantid) {
113  switch (geantid) {
114  case 8: return "#pi";
115  case 9: return "#pi^{-}";
116  case 11: return "K";
117  case 12: return "K^{-}";
118  case 14: return "p";
119  case 15: return "#bar{p}";
120 
121  case 0: return "#pi";
122  case 1: return "#pi^{-}";
123  case 2: return "K";
124  case 3: return "K^{-}";
125  case 4: return "p";
126  case 5: return "#bar{p}";
127  }
128  return "none";
129 };
130 
131 TH1* noiDivide(TH1* num, TH1* den, noiDict opt={}, noiDict dict={}) {
132  dict += opt;
133 
134  double norm_num {1};
135  double norm_den {1};
136 
137  if (dict["norm"]) {
138  norm_num = num->Integral();
139  norm_den = den->Integral();
140  };
141  if (dict["print"]) cout << " norms: " << norm_num << " and " << norm_den << endl;
142 
143  TH1D* ret;
144  if (dict["style-den"]) {
145  ret = (TH1D*) den->Clone(noiUniqueName());
146  } else {
147  ret = (TH1D*) num->Clone(noiUniqueName());
148  }
149 
150  for (int j=1;j<den->GetNbinsX()+1;++j){
151  double n = num->GetBinContent(j) / norm_num;
152  double d = den->GetBinContent(j) / norm_den;
153  if (n == 0 || d == 0) {
154  ret->SetBinContent(j,0);
155  ret->SetBinError(j,0);
156  /* if (i==0) cout << "Jin: " << j << " val: " << den->GetBinContent(j) << endl; */
157  continue;
158  }
159  double n_err = num->GetBinError(j) / norm_num;
160  double d_err = den->GetBinError(j) / norm_den;
161  double val = n / d;
162  double err = val * pow( pow(n_err/n,2)+pow(d_err/d,2),0.5);
163  ret->SetBinContent(j,val);
164  ret->SetBinError(j,err);
165  }
166  return ret;
167 };
168 
169 string noiStripExtension(const char* in) {
170  string word = in;
171  if (word.find(".",1) != string::npos) word = word.substr(0,word.find(".",1));
172  return word;
173 };
174 
175 TGraph* noiMakeTGraph(vector<double>& x, vector<double>& y) {
176  if (x.size() != y.size())
177  throw std::runtime_error("noiMakeTGraph(vec, vec) required vectors of same length");
178  const unsigned int n = x.size();
179  double* xpts = new double[n];
180  double* ypts = new double[n];
181  for (unsigned int i{0}; i<n; ++i) {
182  xpts[i] = x[i];
183  ypts[i] = y[i];
184  }
185  return new TGraph(n,xpts,ypts);
186 };
187 #endif