MINT2
GlobalFunctions.h
Go to the documentation of this file.
1 /*
2  * <B>HyperPlot</B>,
3  * Author: Sam Harnew, sam.harnew@gmail.com ,
4  * Date: Dec 2015
5  *
6  * Global functions that are useful for various things
7  */
8 
9 #ifndef GLOBAL_FUNCTIONS_HH
10 #define GLOBAL_FUNCTIONS_HH
11 
12 #include <string>
13 #include <sstream>
14 #include <vector>
15 #include "TH1D.h"
16 #include "TH2D.h"
17 #include <cmath>
18 #include "TMath.h"
19 #include <limits>
20 
23 inline bool isInf(double val)
24 {
25  //bool decision = val >= std::numeric_limits<double>::min() && val <= std::numeric_limits<double>::max();
26  if ( (val != 0.0) && (val + val == val) && (val == val) ) return 1;
27  return 0;
28 }
29 
32 inline bool isNaN(double val)
33 {
34  if (val != val) return 1;
35  return 0;
36 }
37 
40 inline bool isError(double val){
41  return isInf(val) || isNaN(val);
42 }
43 
46 template <class typeToString> std::string makeString(const typeToString& thingToString)
47 {
48  std::ostringstream ss;
49  ss << thingToString;
50  return ss.str();
51 }
52 
55 template <class arrayType> std::vector<arrayType> gVectorFromArray(arrayType* a, int size){
56 
57  std::vector<arrayType> returnVector;
58  for (int i = 0; i < size; i++) returnVector.push_back(a[i]);
59  return returnVector;
60 
61 }
62 
65 inline void gQuadraticSolver(double a, double b, double c, double& sol1, double& sol2){
66 
67  double root = b*b - 4.0*a*c;
68  if (root >= 0.0){
69  sol1 = (-b + sqrt(root))/(2.0*a);
70  sol2 = (-b - sqrt(root))/(2.0*a);
71  }
72  else{
73  sol1 = -99999999.9;
74  sol2 = -99999999.9;
75  }
76 
77 }
78 
81 inline TH1D gDivideTH1D(TH1D const* hist1, TH1D const* hist2, TString name){
82 
83  TH1D outputHist(*hist1 );
84  outputHist.SetName(name);
85 
86  for(int bin = 1; bin <= hist1->GetNbinsX(); bin++){
87  double cont1 = hist1->GetBinContent(bin);
88  double cont2 = hist2->GetBinContent(bin);
89  double err1 = hist1->GetBinError(bin);
90  double err2 = hist2->GetBinError(bin);
91  double frac1 = err1/cont1;
92  double frac2 = err2/cont2;
93  double cont = cont1/cont2;
94  double err = cont*sqrt(frac1*frac1 + frac2*frac2);
95  if (cont != cont) {cont = 0.0;err = 0.0;}
96  if (cont2 == 0.0) {cont = 0.0;err = 0.0;}
97  if (err != err) {err = 0.0;}
98  outputHist.SetBinContent(bin, cont);
99  outputHist.SetBinError(bin, err);
100  }
101 
102  return outputHist;
103 
104 }
105 
108 inline TH2D gDivideTH2D(TH2D const* hist1, TH2D const* hist2, TString name){
109 
110  TH2D outputHist(*hist1 );
111  outputHist.SetName(name);
112 
113  for(int binX = 1; binX <= hist1->GetNbinsX(); binX++){
114  for(int binY = 1; binY <= hist1->GetNbinsY(); binY++){
115  double cont1 = hist1->GetBinContent(binX, binY);
116  double cont2 = hist2->GetBinContent(binX, binY);
117  double err1 = hist1->GetBinError(binX, binY);
118  double err2 = hist2->GetBinError(binX, binY);
119  double frac1 = err1/cont1;
120  double frac2 = err2/cont2;
121  double cont = cont1/cont2;
122  double err = cont*sqrt(frac1*frac1 + frac2*frac2);
123  if (cont != cont) {cont = 0.0;err = 0.0;}
124  if (cont2 == 0.0) {cont = 0.0;err = 0.0;}
125  if (err != err) {err = 0.0;}
126  outputHist.SetBinContent(binX, binY, cont);
127  outputHist.SetBinError(binX, binY, err);
128  }
129  }
130 
131  return outputHist;
132 
133 }
134 
137 inline TH1D hardCopyTH1D(TH1D const* hist, TString name){
138  TH1D temp(name,name,hist->GetNbinsX(),hist->GetXaxis()->GetXbins()->GetArray());
139  for(int bin = 1; bin <= hist->GetNbinsX(); bin++){
140  temp.SetBinContent(bin, hist->GetBinContent(bin));
141  temp.SetBinError (bin, hist->GetBinError (bin));
142  }
143  return temp;
144 }
145 
148 inline TH1D gPullTH1D(TH1D const* hist1, TH1D const* hist2, TString name){
149 
150  TH1D outputHist( *hist1 );
151  outputHist.SetName(name);
152 
153  for(int bin = 1; bin <= hist1->GetNbinsX(); bin++){
154  double cont1 = hist1->GetBinContent(bin);
155  double cont2 = hist2->GetBinContent(bin);
156  double err1 = hist1->GetBinError(bin);
157  double err2 = hist2->GetBinError(bin);
158  double err1minus2 = sqrt(err1*err1 + err2*err2);
159  double cont = (cont1 - cont2)/err1minus2;
160  double err = 0.0;
161  outputHist.SetBinContent(bin, cont);
162  outputHist.SetBinError(bin, err);
163  }
164 
165  return outputHist;
166 
167 }
168 
171 inline TH1D gMultiplyTH1D(TH1D const* hist1, TH1D const* hist2, TString name){
172 
173 
174  TH1D outputHist( *hist1 );
175 
176  for(int bin = 1; bin <= hist1->GetNbinsX(); bin++){
177  double cont1 = hist1->GetBinContent(bin);
178  double cont2 = hist2->GetBinContent(bin);
179  double err1 = hist1->GetBinError(bin);
180  double err2 = hist2->GetBinError(bin);
181  double frac1 = err1/cont1;
182  double frac2 = err2/cont2;
183  double cont = cont1*cont2;
184  double err = cont*sqrt(frac1*frac1 + frac2*frac2);
185  outputHist.SetBinContent(bin, cont);
186  outputHist.SetBinError(bin, err);
187  }
188 
189  outputHist.SetName(name);
190 
191  return outputHist;
192 
193 }
194 
197 inline TH2D gMultiplyTH2D(TH2D const* hist1, TH2D const* hist2, TString name){
198 
199  TH2D outputHist( *hist1 );
200 
201  for(int binX = 1; binX <= hist1->GetNbinsX(); binX++){
202  for(int binY = 1; binY <= hist1->GetNbinsY(); binY++){
203  double cont1 = hist1->GetBinContent(binX, binY);
204  double cont2 = hist2->GetBinContent(binX, binY);
205  double err1 = hist1->GetBinError(binX, binY);
206  double err2 = hist2->GetBinError(binX, binY);
207  double frac1 = err1/cont1;
208  double frac2 = err2/cont2;
209  double cont = cont1*cont2;
210  double err = cont*sqrt(frac1*frac1 + frac2*frac2);
211  outputHist.SetBinContent(binX, binY, cont);
212  outputHist.SetBinError(binX, binY, err);
213  }
214  }
215 
216  outputHist.SetName(name);
217 
218  return outputHist;
219 
220 }
221 
224 inline TH1D gSqrtTH1D(TH1D const* hist1, TString name){
225 
226 
227  TH1D outputHist( *hist1 );
228 
229  for(int bin = 1; bin <= hist1->GetNbinsX(); bin++){
230  double cont = hist1->GetBinContent(bin);
231  double err = hist1->GetBinError(bin);
232  outputHist.SetBinContent(bin, sqrt(cont));
233  outputHist.SetBinError(bin, err/(2.0*sqrt(cont)));
234  }
235 
236  outputHist.SetName(name);
237 
238  return outputHist;
239 
240 }
241 
244 inline TH1D gAddTH1D(TH1D const* hist1, TH1D const* hist2, TString name){
245 
246  TH1D outputHist(name,name, hist1->GetNbinsX(), hist1->GetXaxis()->GetXbins()->GetArray() );
247 
248  for(int bin = 1; bin <= hist1->GetNbinsX(); bin++){
249  double cont1 = hist1->GetBinContent(bin);
250  double cont2 = hist2->GetBinContent(bin);
251  double err1 = hist1->GetBinError(bin);
252  double err2 = hist2->GetBinError(bin);
253  double cont = cont1 + cont2;
254  double err = sqrt(err1*err1 + err2*err2);
255  outputHist.SetBinContent(bin, cont);
256  outputHist.SetBinError(bin, err);
257  }
258 
259  return outputHist;
260 }
261 
264 inline bool printInterationStatus(int interation, int total){
265 
266  int frequency = pow( 10, floor(log10(total)) - 1);
267 
268  double nPrints = double(total)/double(frequency);
269 
270  if (nPrints > 50) frequency *= 5;
271  else if (nPrints > 20) frequency *= 2;
272 
273  if (interation < 4){
274  //std::cout << "Interation " << interation << " of " << total << std::endl;
275  return true;
276  }
277  else if (interation % frequency == 0) {
278  //std::cout << "Interation " << interation << " of " << total << std::endl;
279  return true;
280  }
281  return false;
282 }
283 
286 inline TH2D gAddTH2D(TH2D const* hist1, TH2D const* hist2, TString name){
287 
288  TH2D outputHist(*hist1);
289  outputHist.SetName(name);
290 
291  for(int binX = 1; binX <= hist1->GetNbinsX(); binX++){
292  for(int binY = 1; binY <= hist1->GetNbinsY(); binY++){
293  double cont1 = hist1->GetBinContent(binX, binY);
294  double cont2 = hist2->GetBinContent(binX, binY);
295  double err1 = hist1->GetBinError(binX, binY);
296  double err2 = hist2->GetBinError(binX, binY);
297  double cont = cont1 + cont2;
298  double err = sqrt(err1*err1 + err2*err2);
299  outputHist.SetBinContent(binX, binY, cont);
300  outputHist.SetBinError(binX, binY, err);
301  }
302  }
303 
304  return outputHist;
305 }
306 
309 inline TH1D gMinusTH1D(TH1D const* hist1, TH1D const* hist2, TString name){
310 
311 
312  TH1D outputHist(name,name, hist1->GetNbinsX(), hist1->GetXaxis()->GetXbins()->GetArray() );
313  outputHist.SetName(name);
314 
315  for(int bin = 1; bin <= hist1->GetNbinsX(); bin++){
316  double cont1 = hist1->GetBinContent(bin);
317  double cont2 = hist2->GetBinContent(bin);
318  double err1 = hist1->GetBinError(bin);
319  double err2 = hist2->GetBinError(bin);
320  double cont = cont1 - cont2;
321  double err = sqrt(err1*err1 + err2*err2);
322  outputHist.SetBinContent(bin, cont);
323  outputHist.SetBinError(bin, err);
324  }
325 
326  return outputHist;
327 }
328 
331 inline TH2D gMinusTH2D(TH2D const* hist1, TH2D const* hist2, TString name){
332 
333  TH2D outputHist(*hist1);
334  outputHist.SetName(name);
335 
336  for(int binX = 1; binX <= hist1->GetNbinsX(); binX++){
337  for(int binY = 1; binY <= hist1->GetNbinsY(); binY++){
338  double cont1 = hist1->GetBinContent(binX, binY);
339  double cont2 = hist2->GetBinContent(binX, binY);
340  double err1 = hist1->GetBinError(binX, binY);
341  double err2 = hist2->GetBinError(binX, binY);
342  double cont = cont1 - cont2;
343  double err = sqrt(err1*err1 + err2*err2);
344  outputHist.SetBinContent(binX, binY, cont);
345  outputHist.SetBinError(binX, binY, err);
346  }
347  }
348 
349  return outputHist;
350 }
351 
354 inline double degreesToRadians(double degrees) { return (degrees*TMath::Pi())/180.0; }
355 
358 inline double radiansToDegrees(double radians) { return (radians/TMath::Pi())*180.0; }
359 
362 inline double shiftAngleToDomain(double angle, int degrees = 1){
363 
364  if (degrees == 1){
365  if ((angle >= 0.0) && (angle < 360.0)) {
366  return angle;
367  }
368  else {
369  double val = angle - 360.0*floor(angle/360.0);
370  return val;
371  }
372  }
373  if (degrees == 0){
374  if ((angle >= 0.0) && (angle < 2.0*TMath::Pi())) {
375  return angle;
376  }
377  else {
378  double val = angle - 2.0*TMath::Pi()*floor(angle/(2.0*TMath::Pi()));
379  return val;
380  }
381  }
382 
383  return -9999.9;
384 
385 }
386 
387 #endif
std::vector< arrayType > gVectorFromArray(arrayType *a, int size)
double radiansToDegrees(double radians)
bool isError(double val)
TH2D gMinusTH2D(TH2D const *hist1, TH2D const *hist2, TString name)
double shiftAngleToDomain(double angle, int degrees=1)
bool isNaN(double val)
TH2D gMultiplyTH2D(TH2D const *hist1, TH2D const *hist2, TString name)
void gQuadraticSolver(double a, double b, double c, double &sol1, double &sol2)
std::string makeString(const typeToString &thingToString)
double degreesToRadians(double degrees)
TH1D gMinusTH1D(TH1D const *hist1, TH1D const *hist2, TString name)
TH1D gMultiplyTH1D(TH1D const *hist1, TH1D const *hist2, TString name)
TH2D gAddTH2D(TH2D const *hist1, TH2D const *hist2, TString name)
bool printInterationStatus(int interation, int total)
TH2D gDivideTH2D(TH2D const *hist1, TH2D const *hist2, TString name)
TH1D hardCopyTH1D(TH1D const *hist, TString name)
TH1D gDivideTH1D(TH1D const *hist1, TH1D const *hist2, TString name)
bool isInf(double val)
TH1D gAddTH1D(TH1D const *hist1, TH1D const *hist2, TString name)
TH1D gSqrtTH1D(TH1D const *hist1, TString name)
TH1D gPullTH1D(TH1D const *hist1, TH1D const *hist2, TString name)