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