MINT2
Functions
GlobalFunctions.h File Reference
#include <string>
#include <sstream>
#include <vector>
#include "TH1D.h"
#include "TH2D.h"
#include <cmath>
#include "TMath.h"
#include <limits>

Go to the source code of this file.

Functions

bool isInf (double val)
 
bool isNaN (double val)
 
bool isError (double val)
 
template<class typeToString >
std::string makeString (const typeToString &thingToString)
 
template<class arrayType >
std::vector< arrayType > gVectorFromArray (arrayType *a, int size)
 
void gQuadraticSolver (double a, double b, double c, double &sol1, double &sol2)
 
TH1D gDivideTH1D (TH1D const *hist1, TH1D const *hist2, TString name)
 
TH2D gDivideTH2D (TH2D const *hist1, TH2D const *hist2, TString name)
 
TH1D hardCopyTH1D (TH1D const *hist, TString name)
 
TH1D gPullTH1D (TH1D const *hist1, TH1D const *hist2, TString name)
 
TH1D gMultiplyTH1D (TH1D const *hist1, TH1D const *hist2, TString name)
 
TH2D gMultiplyTH2D (TH2D const *hist1, TH2D const *hist2, TString name)
 
TH1D gSqrtTH1D (TH1D const *hist1, TString name)
 
TH1D gAddTH1D (TH1D const *hist1, TH1D const *hist2, TString name)
 
bool printInterationStatus (int interation, int total)
 
TH2D gAddTH2D (TH2D const *hist1, TH2D const *hist2, TString name)
 
TH1D gMinusTH1D (TH1D const *hist1, TH1D const *hist2, TString name)
 
TH2D gMinusTH2D (TH2D const *hist1, TH2D const *hist2, TString name)
 
double degreesToRadians (double degrees)
 
double radiansToDegrees (double radians)
 
double shiftAngleToDomain (double angle, int degrees=1)
 

Function Documentation

◆ degreesToRadians()

double degreesToRadians ( double  degrees)
inline

Convert degrees to radians

Definition at line 354 of file GlobalFunctions.h.

354 { return (degrees*TMath::Pi())/180.0; }

◆ gAddTH1D()

TH1D gAddTH1D ( TH1D const *  hist1,
TH1D const *  hist2,
TString  name 
)
inline

Add two TH1D's

Definition at line 244 of file GlobalFunctions.h.

244  {
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 }

◆ gAddTH2D()

TH2D gAddTH2D ( TH2D const *  hist1,
TH2D const *  hist2,
TString  name 
)
inline

Add two TH2D's

Definition at line 286 of file GlobalFunctions.h.

286  {
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 }

◆ gDivideTH1D()

TH1D gDivideTH1D ( TH1D const *  hist1,
TH1D const *  hist2,
TString  name 
)
inline

Divide two TH1D's (don't trust ROOT)

Definition at line 81 of file GlobalFunctions.h.

81  {
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 }

◆ gDivideTH2D()

TH2D gDivideTH2D ( TH2D const *  hist1,
TH2D const *  hist2,
TString  name 
)
inline

Divide two TH2D's (don't trust ROOT)

Definition at line 108 of file GlobalFunctions.h.

108  {
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 }

◆ gMinusTH1D()

TH1D gMinusTH1D ( TH1D const *  hist1,
TH1D const *  hist2,
TString  name 
)
inline

Minus two TH1D's (hist1 - hist2)

Definition at line 309 of file GlobalFunctions.h.

309  {
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 }

◆ gMinusTH2D()

TH2D gMinusTH2D ( TH2D const *  hist1,
TH2D const *  hist2,
TString  name 
)
inline

Minus two TH2D's (hist1 - hist2)

Definition at line 331 of file GlobalFunctions.h.

331  {
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 }

◆ gMultiplyTH1D()

TH1D gMultiplyTH1D ( TH1D const *  hist1,
TH1D const *  hist2,
TString  name 
)
inline

Multiply two TH1D's

Definition at line 171 of file GlobalFunctions.h.

171  {
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 }

◆ gMultiplyTH2D()

TH2D gMultiplyTH2D ( TH2D const *  hist1,
TH2D const *  hist2,
TString  name 
)
inline

Multiply two TH2D's

Definition at line 197 of file GlobalFunctions.h.

197  {
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 }

◆ gPullTH1D()

TH1D gPullTH1D ( TH1D const *  hist1,
TH1D const *  hist2,
TString  name 
)
inline

Make a pull histogram between two TH1D's

Definition at line 148 of file GlobalFunctions.h.

148  {
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 }

◆ gQuadraticSolver()

void gQuadraticSolver ( double  a,
double  b,
double  c,
double &  sol1,
double &  sol2 
)
inline

Solve a quadratic equation

Definition at line 65 of file GlobalFunctions.h.

65  {
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 }

◆ gSqrtTH1D()

TH1D gSqrtTH1D ( TH1D const *  hist1,
TString  name 
)
inline

Square root of a TH1D

Definition at line 224 of file GlobalFunctions.h.

224  {
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 }

◆ gVectorFromArray()

template<class arrayType >
std::vector<arrayType> gVectorFromArray ( arrayType *  a,
int  size 
)

Convert an array into a vector

Definition at line 55 of file GlobalFunctions.h.

55  {
56 
57  std::vector<arrayType> returnVector;
58  for (int i = 0; i < size; i++) returnVector.push_back(a[i]);
59  return returnVector;
60 
61 }

◆ hardCopyTH1D()

TH1D hardCopyTH1D ( TH1D const *  hist,
TString  name 
)
inline

Make a hard copy of a TH1D - probably not needed

Definition at line 137 of file GlobalFunctions.h.

137  {
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 }

◆ isError()

bool isError ( double  val)
inline

Check if a double is NaN or ±inf

Definition at line 40 of file GlobalFunctions.h.

40  {
41  return isInf(val) || isNaN(val);
42 }
bool isNaN(double val)
bool isInf(double val)

◆ isInf()

bool isInf ( double  val)
inline

Check if a double is inf or -inf

Definition at line 23 of file GlobalFunctions.h.

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 }

◆ isNaN()

bool isNaN ( double  val)
inline

Check if a double is NaN

Definition at line 32 of file GlobalFunctions.h.

33 {
34  if (val != val) return 1;
35  return 0;
36 }

◆ makeString()

template<class typeToString >
std::string makeString ( const typeToString &  thingToString)

Convert things into strings

Definition at line 46 of file GlobalFunctions.h.

47 {
48  std::ostringstream ss;
49  ss << thingToString;
50  return ss.str();
51 }

◆ printInterationStatus()

bool printInterationStatus ( int  interation,
int  total 
)
inline

Function for printing the status of a loop

Definition at line 264 of file GlobalFunctions.h.

264  {
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 }

◆ radiansToDegrees()

double radiansToDegrees ( double  radians)
inline

Convert radians to degrees

Definition at line 358 of file GlobalFunctions.h.

358 { return (radians/TMath::Pi())*180.0; }

◆ shiftAngleToDomain()

double shiftAngleToDomain ( double  angle,
int  degrees = 1 
)
inline

Shift angle to the domain [0, 360] / [0,2pi] (use degrees = 0 for radians)

Definition at line 362 of file GlobalFunctions.h.

362  {
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 }