StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
franks1Histo.hh
1 #ifndef FRANKS1HISTO__HH
2 #define FRANKS1HISTO__HH
3 
4 
5 #ifndef __CINT__
6 #include <Stiostream.h>
7 #include <stdio>
8 #include <math.h>
9 #ifdef GNU_GCC
10 # include <stddef.h>
11 #endif
12 #endif
13 
14 //#ifndef ST_NO_MEMBER_TEMPLATES
15 //#define ST_NO_MEMBER_TEMPLATES
16 //#endif
17 
18 #ifdef ST_NO_TEMPLATE_DEF_ARGS
19 template<class T>
20 #else
21 template<class T = double>
22 #endif
23 
24 class franks1Histo {
25 private:
26  char* mC1;
27  char* mC2;
28  int mBins;
29  int mPos;
30  T mXmin;
31  T mXmax;
32  T mStep;
33  T *vec;
34  int mEntries;
35 
36 public:
37  // constructor and deconstructor
38  franks1Histo(const char* c1, const char* c2,
39  int bins, T xmin, T xmax);
40  ~franks1Histo();
41 
42  // member functions
43 #ifndef ST_NO_MEMBER_TEMPLATES
44  template<class X, class Y> void Add( franks1Histo<X>* , franks1Histo<X>* , Y w1=1., Y w2=1., const char* c="");
45  template<class X, class Y> void Divide( franks1Histo<X>* , franks1Histo<X>* , Y w1=1., Y w2=1., const char* c="");
46  template<class X> void Fill( X value);
47  template<class X, class Y> void Fill( X value, Y weight);
48  template<class X> int GetBin(X value);
49  template<class X> void Scale(X scale);
50 #endif
51  // methods without template arguments
52  void Draw(const char* c="");
53  void SetDirectory(int dummy) { /* no-op */};
54  void Sumw2() {/* no-op */};
55  int GetNbinsX() { return mBins; }
56  T GetBinContent(int bin) { return vec[bin]; }
57  T GetBinCenter(int bin);
58  T GetMaximum();
59  T GetMinimum();
60  T GetMean();
61  T GetRMS();
62  T GetEntries();
63  T Integral();
64  void Reset(const char* c="");
65 };
66 
67 #ifndef __CINT__
68 
69 // ***********
70 // constructor
71 // ***********
72 template<class T>
73 inline franks1Histo<T>::franks1Histo(const char* c1, const char* c2, int bins, T xmin, T xmax) {
74  mC1 = c1;
75  mC2 = c2;
76  mBins = bins;
77  mXmin = xmin;
78  mXmax = xmax;
79  mStep = (xmax-xmin)/bins;
80  vec = new T[bins];
81 }
82 // *************
83 // deconstructor
84 // *************
85 template<class T>
87  delete vec;
88 }
89 
90 // ******************************
91 // definition of member functions
92 // ******************************
93 #ifndef ST_NO_MEMBER_TEMPLATES
94 // *************************************************************************************************
95  template<class T>
96  template<class X, class Y>
97  inline void franks1Histo<T>::Add( franks1Histo<X>* h1, franks1Histo<X>* h2, Y w1, Y w2, const char* c) {
98  for (int i=0; i < mBins; i++) {
99  vec[i] = h1->vec[i]*w1 + h2->vec[i]*w2;
100  }
101 }
102 // *************************************************************************************************
103  template<class T>
104  template<class X, class Y>
105  inline void franks1Histo<T>::Divide( franks1Histo<X>* h1, franks1Histo<X>* h2, Y w1, Y w2, const char* c) {
106  for (int i=0; i < mBins; i++) {
107  if (h2->vec[i]*w2 !=0 )
108  vec[i] = h1->vec[i]*w1 / h2->vec[i]*w2;
109  else
110  vec[i]=0;
111  }
112  }
113 // *************************************************************************************************
114 template<class T>
115 inline void franks1Histo<T>::Draw(const char* c) {
116  cout << c << " " << mC1 << " " << endl;
117  T min=GetMinimum();
118  T max=GetMaximum();
119  T step = (max-min)/50.;
120  cout << " minimum=" << min << " maximum=" << max << " step=" << step << endl;
121  for (int i=0; i < mBins; i++) {
122  printf(" (%3i) %+e %+e ",i, GetBinCenter(i), vec[i]);
123  for ( int j=0; j < floor( (vec[i]-min)/step ); j++) {
124  cout << "*";
125  }
126  cout << endl;
127  }
128 };
129 // *************************************************************************************************
130 template<class T>
131 template<class X>
132 inline void franks1Histo<T>::Fill( X value) {
133  mPos = (int) abs( (value-mXmin)/mStep );
134  if ( mPos>=0 && mPos < mBins)
135  vec[mPos]++;
136  cout << ".";
137 }
138 // *************************************************************************************************
139 template<class T>
140 template<class X, class Y>
141 inline void franks1Histo<T>::Fill( X value, Y weight) {
142  mPos = (int) abs( (value-mXmin)/mStep );
143  if ( mPos>=0 && mPos < mBins)
144  vec[mPos] = vec[mPos] + weight;
145  cout << ".";
146 }
147 // *************************************************************************************************
148 template<class T>
149 template<class X>
150 inline int franks1Histo<T>::GetBin(X value) {
151  int bin = (int) floor( (value-mXmin)/mStep );
152  if( !(bin >=0 && bin < mBins) ) bin=-1;
153  return bin;
154 }
155 // *************************************************************************************************
156 template<class T>
157 inline T franks1Histo<T>::GetBinCenter(int bin) {
158  double center=0;
159  if ( bin >=0 && bin < mBins)
160  center= mXmin + (0.5+bin)*mStep;
161  return center;
162 }
163 // *************************************************************************************************
164 template<class T>
165 inline T franks1Histo<T>::GetMean() {
166  T mean=0;
167  for (int i=0; i< mBins; i++)
168  mean+=vec[i]*GetBinCenter(i);
169  mean/=mBins;
170  return mean;
171 }
172 // *************************************************************************************************
173 template<class T>
174 inline T franks1Histo<T>::GetMaximum() {
175  T max=vec[0];
176  for (int i=0; i< mBins; i++) {
177  if (vec[i] > max)
178  max=vec[i];
179  }
180  return max;
181 }
182 // *************************************************************************************************
183 template<class T>
184 inline T franks1Histo<T>::GetMinimum() {
185  T min=vec[0];
186  for (int i=0; i< mBins; i++) {
187  if (vec[i] < min)
188  min=vec[i];
189  }
190  return min;
191 }
192 // *************************************************************************************************
193 template<class T>
194 inline T franks1Histo<T>::GetRMS() {
195  T mean = GetMean();
196  for (int i=0; i< mBins; i++)
197  mean+=vec[i]*GetBinCenter(i);
198  mean/=mBins;
199  return mean;
200 }
201 // *************************************************************************************************
202 template<class T>
203 inline void franks1Histo<T>::Reset(const char*) {
204  for (int i=0; i < mBins; i++)
205  vec[i] = 0;
206 }
207 // *************************************************************************************************
208 template<class T>
209 template<class X>
210 inline void franks1Histo<T>::Scale(X scale) {
211  for (int i=0; i < mBins; i++)
212  vec[i] *=scale;
213 }
214 // *************************************************************************************************
215 template<class T>
216 inline T franks1Histo<T>::Integral() {
217  T Integral=0;
218  for (int i=0; i < mBins; i++) {
219  Integral+=vec[i];
220  //cout << i << " " << vec[i] << " " << Integral << endl;
221  }
222  return Integral;
223 }
224 #endif
225 
226 #endif // __CINT__
227 
228 #endif // FRANKS2DHISTO_HH