simple-tof-analysis
 All Classes Namespaces Functions Variables Groups Pages
RootUtils.h
1 /*
2  * RootUtils.h
3  *
4  * Created on: Nov 9, 2013
5  * Author: Silvestro di Luise
6  * Silvestro.Di.Luise@cern.ch
7 
8  *
9  */
10 
11 #ifndef ROOTUTILS_H
12 #define ROOTUTILS_H
13 
14 #include <iostream>
15 
16 #include <limits>
17 #include <string>
18 #include <TString.h>
19 #include <TGraph.h>
20 #include <TGraphErrors.h>
21 #include <TGraphAsymmErrors.h>
22 
23 #include <TFile.h>
24 #include <TObject.h>
25 #include <TMath.h>
26 #include <TRandom.h>
27 
28 class TH2;
29 class TH1;
30 class TH2F;
31 class TH1F;
32 class TH2Poly;
33 
34 using namespace std;
35 
36 using std::cout;
37 using std::endl;
38 
39 namespace RootUtils {
40 
41 
42  TGraph* BuildGraph(TString,TString, int N, double *x, double *y, int closed);
43 
44  TGraph* BuildGraph(TString,TString, double xmin,double xmax,double ymin,double ymax);
45 
46  bool CompareGraphs(TGraph &g1, TGraph &g2);
47 
48  double ComputeEffEntries(TH1&,TString opt="");
49  double ComputeIntegral(TH1&,TString opt="");
50 
51  double* CreateBinsArray(int, double xmin, double xmax);
52 
53  std::vector<TString> DirFileList(TString path, TString ext,TString opt);
54 
55  void FillHistoFast(TH1F &, double x, double w);
56  void FillHistoFast(TH2F &, double x, double y, double w);
57 
58  int GetBin(int nx,int binx);
59 
60  int GetBin(int nx,int binx,int ny, int biny);
61 
62  TString GetBinRangeLabel(TH1 &h, int bin, TString axis);
63 
64  TObject* GetObjFromFile(TString file, TString obj);
65 
66  float GetOutliers(TH1&);
67 
68  TGraphAsymmErrors* GetPoissonErrors(TH1 &h, TString ="");
69 
70  void ImportBinning(TH2F *h2, TH1F *hx, TH1F *hy);
71 
72  void ImportBinning(TH1F *h, TH1F *hx);
73  template <typename T> bool IsTotalInside(T xp, T yp, Int_t np, T *x, T *y);
74 
75  bool IsInsideRectangular(TGraph *g, double x, double y);
76 
77  bool IsTH1(const TObject& );
78  bool IsTH2(const TObject& );
79  bool IsTH2Poly(const TObject& );
80 
81  bool IsTH1F(const TObject& );
82  bool IsTH2F(const TObject& );
83 
84  TH2F* InvertAxis(TH2 &,int opt=0);
85 
86  void MeanAndRMS(std::vector<double> *val, std::vector<double> *w, double &mean, double &rms);
87  void MeanAndRMSOld(std::vector<double> *val, std::vector<double> *w, double &mean, double &rms);
88 
89  double Normalize(TH1&,double norm,TString opt="");
90 
91  void Poisson(TH1 &in, TH1 &out, int opt=0);
92 
93  void Poisson(TH1 &, int opt=0);
94 
95  TH1F* newTH1F(TString name, TString title, int nbins, double low, double up);
96  TH2F* newTH2F(TString name, TString title, int nbinsx, double lowx, double upx
97  ,int nbinsy, double lowy, double upy);
98 
99 
100  bool OverlappingRectangulars(TGraph &g1, TGraph &g2);
101 
102  void PrintHistoInfo(TObject *h);
103 
104  TH1F* RatioHist1D(TString name,TString tit,TH1F &hnum, TH1F& hden, double hmin,double hmax,TString opt="");
105 
106  bool SameBinning(TH1 &h1, TH1 &h2);
107 
108 
109  //Math Utils
110  template<typename T>
111  bool IsZero(T val){
112  return TMath::AreEqualRel(val,0.,std::numeric_limits<T>::epsilon());
113  }
114 
115 
116  static TRandom fRandom;
117 
118  static TString STR_ERROR = " !!!!!! ";
119  static TString STR_WARNING = " ###### ";
120 
121 }
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 template <typename T> bool RootUtils::IsTotalInside(T xp, T yp, Int_t np, T *x, T *y)
132 {
133 
134  // Adapted from TMath::IsInside
135  // In this version the point must lie totally inside the a
136  // closed polygon (cannot be on a vertex or along a side)
137 
138  //TMath::IsInside:
139  // Function which returns kTRUE if point xp,yp lies inside the
140  // polygon defined by the np points in arrays x and y, kFALSE otherwise.
141  // Note that the polygon may be open or closed.
142 
143  Int_t i, j = np-1 ;
144  Bool_t oddNodes = kFALSE;
145 
146  for (i=0; i<np; i++) {
147 
148  bool y_range_1_1=(y[i]<yp);
149  bool y_range_1_2=(y[j]>yp);
150 
151  bool y_range_2_1=(y[j]<yp);
152  bool y_range_2_2=(y[i]>yp);
153 
154  //workaround...to be fixed properly
155  //example: the first inequality holds even though y[i] and yp are equal
156  //this further check seems to work
157  if( y_range_1_1 && TMath::AreEqualRel(y[i],yp,1e-5) ) y_range_1_1=false;
158  if( y_range_1_2 && TMath::AreEqualRel(y[j],yp,1e-5) ) y_range_1_2=false;
159 
160  if( y_range_2_1 && TMath::AreEqualRel(y[j],yp,1e-5) ) y_range_2_1=false;
161  if( y_range_2_2 && TMath::AreEqualRel(y[i],yp,1e-5) ) y_range_2_2=false;
162 
163  bool y_range_1 = y_range_1_1 && y_range_1_2;
164  bool y_range_2 = y_range_2_1 && y_range_2_2;
165 
166 
167  if( y_range_1 || y_range_2 ){//
168  //if ((y[i]<yp && y[j]>yp) || (y[j]<yp && y[i]>yp)) { //problem with inequalities
169  // if ((y[i]<yp && y[j]>=yp) || (y[j]<yp && y[i]>=yp)) {// Original TMath::IsInside
170 
171  if (x[i]+(yp-y[i])/(y[j]-y[i])*(x[j]-x[i])<xp) {
172  oddNodes = !oddNodes;
173  }
174  }
175  j=i;
176  }
177 
178 
179  return oddNodes;
180 
181 }
182 
183 #endif /* ROOTUTILS_H */