Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DHistogram.h
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: DHistogram.h
4 // Created: Mon Aug 1 08:56:58 EDT 2011
5 // Creator: davidl (on Linux ifarm1102 2.6.18-128.7.1.el5 x86_64)
6 //
7 
8 #ifndef _DHistogram_
9 #define _DHistogram_
10 
11 
12 #include <iostream>
13 #include <cstddef> // for NULL
14 #include <cmath>
15 
16 #include <TH1.h>
17 
18 #include <JANA/jerror.h>
19 
20 class DHistogram{
21  public:
22  inline DHistogram(int Nbins, float lowEdge, float highEdge);
23  inline DHistogram(const DHistogram &hsrc);
24  inline virtual ~DHistogram();
25 
26  inline void Add(const DHistogram *h);
27 
28  inline float Fill(float x, float weight=1.0);
29  inline int FindBin(float x) const;
30  inline int FindFirstBinAbove(float val, int start_bin=1) const;
31  inline int FindFirstNonZeroBin(void) const;
32  inline int FindLastNonZeroBin(void) const;
33 
34  inline int GetNbins(void) const;
35  inline float GetBinLowEdge(int bin) const;
36  inline float GetBinCenter(int bin) const;
37  inline float GetBinContent(int bin) const;
38  inline float GetBinWidth(void) const;
39  inline float GetLowEdge(void) const;
40  inline float GetHighEdge(void) const;
41  inline float* GetContentPointer(void) const; ///< CAUTION!
42 
43  inline float Integral(void) const;
44  inline TH1D* MakeTH1D(string name, string title) const;
45 
46  inline void Reset(void);
47  inline void Scale(float scale);
48  inline void SetBinContent(int bin, float val);
49 
50  inline DHistogram& operator=(const DHistogram &hsrc);
51 
52  protected:
53 
54 
55  private:
56  DHistogram(); // prevent default constructor usage
57 
58  int Nbins;
59  float lowEdge;
60  float highEdge;
61  float binWidth;
64 
65  float *content;
66 };
67 
68 
69 //---------------------------------
70 // DHistogram (Constructor)
71 //---------------------------------
72 inline DHistogram::DHistogram(int Nbins, float lowEdge, float highEdge)
73 {
74 
75  this->Nbins = Nbins;
76  this->lowEdge = lowEdge;
77  this->highEdge = highEdge;
78  binWidth = (highEdge - lowEdge)/(float)Nbins;
79  half_binWidth = binWidth/2.0;
80  bin_zero_center = lowEdge - half_binWidth;
81 
82  if(Nbins>0 && Nbins<100000000){
83  content = new float[Nbins+1];
84 
85  for(int bin=1; bin<=Nbins; bin++){
86  content[bin] = 0.0;
87  }
88  }else{
89  content = NULL;
90  }
91 }
92 
93 //---------------------------------
94 // DHistogram (Constructor)
95 //---------------------------------
96 inline DHistogram::DHistogram(const DHistogram &hsrc):Nbins(0),lowEdge(0.0),highEdge(0.0),binWidth(0.0),half_binWidth(0.0),content(NULL)
97 {
98  *this = hsrc;
99 }
100 
101 //---------------------------------
102 // ~DHistogram (Destructor)
103 //---------------------------------
105 {
106  if(content!=NULL) delete[] content;
107 }
108 
109 //---------------------------------
110 // Add
111 //---------------------------------
112 inline void DHistogram::Add(const DHistogram *h)
113 {
114  if(h->GetBinWidth()!=binWidth || h->GetNbins()!=Nbins){
115  _DBG_<<"Histogram parameters don't match!!"<<endl;
116  return;
117  }
118 
119  for(int bin=1; bin<=Nbins; bin++){
120  content[bin] += h->GetBinContent(bin);
121  }
122 }
123 
124 //---------------------------------
125 // FindBin
126 //---------------------------------
127 inline int DHistogram::FindBin(float x) const
128 {
129  int bin = 1 + (int)floor((x-lowEdge)/binWidth);
130 
131  return bin;
132 }
133 
134 //---------------------------------
135 // FindFirstBinAbove
136 //---------------------------------
137 inline int DHistogram::FindFirstBinAbove(float val, int start_bin) const
138 {
139  for(int bin=start_bin; bin<=Nbins; bin++){
140  if(content[bin] >= val)return bin;
141  }
142 
143  return -1;
144 }
145 
146 //---------------------------------
147 // FindFirstNonZeroBin
148 //---------------------------------
150 {
151  for(int bin=1; bin<=Nbins; bin++){
152  if(content[bin] != 0.0)return bin;
153  }
154 
155  return -1;
156 }
157 
158 //---------------------------------
159 // FindLastNonZeroBin
160 //---------------------------------
162 {
163  for(int bin=Nbins; bin>=1; bin--){
164  if(content[bin] != 0.0)return bin;
165  }
166 
167  return -1;
168 }
169 
170 //---------------------------------
171 // Fill
172 //---------------------------------
173 inline float DHistogram::Fill(float x, float weight)
174 {
175  int bin = 1 + (int)floor((x-lowEdge)/binWidth);
176  if(bin<1 || bin>Nbins)return 0.0;
177 
178  content[bin] += weight;
179 
180  return content[bin];
181 }
182 
183 //---------------------------------
184 // GetNbins
185 //---------------------------------
186 inline int DHistogram::GetNbins(void) const
187 {
188  return Nbins;
189 }
190 
191 //---------------------------------
192 // GetBinLowEdge
193 //---------------------------------
194 inline float DHistogram::GetBinLowEdge(int bin) const
195 {
196  return lowEdge + (float)(bin-1)*binWidth;
197 }
198 
199 //---------------------------------
200 // GetBinCenter
201 //---------------------------------
202 inline float DHistogram::GetBinCenter(int bin) const
203 {
204  if(bin<1 || bin>Nbins)return 0.0;
205 
206  return bin_zero_center + binWidth*(float)bin;
207 }
208 
209 //---------------------------------
210 // GetBinContent
211 //---------------------------------
212 inline float DHistogram::GetBinContent(int bin) const
213 {
214  if(bin<1 || bin>Nbins)return 0.0;
215 
216  return content[bin];
217 }
218 
219 //---------------------------------
220 // GetBinWidth
221 //---------------------------------
222 inline float DHistogram::GetBinWidth(void) const
223 {
224  return binWidth;
225 }
226 
227 //---------------------------------
228 // GetLowEdge
229 //---------------------------------
230 inline float DHistogram::GetLowEdge(void) const
231 {
232  return lowEdge;
233 }
234 
235 //---------------------------------
236 // GetHighEdge
237 //---------------------------------
238 inline float DHistogram::GetHighEdge(void) const
239 {
240  return highEdge;
241 }
242 
243 //---------------------------------
244 // GetContentPointer
245 //---------------------------------
246 inline float* DHistogram::GetContentPointer(void) const
247 {
248  return content;
249 }
250 
251 //---------------------------------
252 // Integral
253 //---------------------------------
254 inline float DHistogram::Integral(void) const
255 {
256  float I=0.0;
257 
258  for(int bin=1; bin<=Nbins; bin++) I += content[bin];
259 
260  return I;
261 }
262 
263 //---------------------------------
264 // MakeTH1D
265 //---------------------------------
266 inline TH1D* DHistogram::MakeTH1D(string name, string title) const
267 {
268  TH1D *h = new TH1D(name.c_str(), title.c_str(), Nbins, lowEdge, highEdge);
269 
270  for(int bin=1; bin<=Nbins; bin++) h->SetBinContent(bin, content[bin]);
271 
272  return h;
273 }
274 
275 //---------------------------------
276 // Reset
277 //---------------------------------
278 inline void DHistogram::Reset(void)
279 {
280  for(int bin=1; bin<=Nbins; bin++)content[bin] = 0.0;
281 }
282 
283 //---------------------------------
284 // Scale
285 //---------------------------------
286 inline void DHistogram::Scale(float scale)
287 {
288  for(int bin=1; bin<=Nbins; bin++)content[bin] *= scale;
289 }
290 
291 //---------------------------------
292 // SetBinContent
293 //---------------------------------
294 inline void DHistogram::SetBinContent(int bin, float val)
295 {
296  if(bin<1 || bin>Nbins)return;
297 
298  content[bin] = val;
299 }
300 
301 //---------------------------------
302 // operator=
303 //---------------------------------
305 {
306  // If histogram has different parameters, reallocate
307  if( (hsrc.GetNbins()!=Nbins) || (hsrc.GetLowEdge()!=lowEdge) || (hsrc.GetHighEdge()!=highEdge)){
308  this->Nbins = hsrc.GetNbins();
309  this->lowEdge = hsrc.GetLowEdge();
310  this->highEdge = hsrc.GetHighEdge();
311  binWidth = (highEdge - lowEdge)/(float)Nbins;
312  half_binWidth = binWidth/2.0;
314 
315  if(content!=NULL) delete[] content;
316 
317  if((Nbins>0) && (Nbins<100000000) && (hsrc.content!=NULL)){
318  content = new float[Nbins+1];
319  }else{
320  content = NULL;
321  }
322  }
323 
324  if(content!=NULL){
325 
326  float *ysrc = hsrc.content;
327  float *ydest = content;
328 
329  for(int bin=0; bin<=Nbins; bin++){
330  *ydest++ = *ysrc++;
331  }
332  }
333 
334  return *this;
335 }
336 
337 
338 #endif // _DHistogram_
339 
TH1D * MakeTH1D(string name, string title) const
Definition: DHistogram.h:266
int FindLastNonZeroBin(void) const
Definition: DHistogram.h:161
float binWidth
Definition: DHistogram.h:61
float GetLowEdge(void) const
Definition: DHistogram.h:230
float * GetContentPointer(void) const
CAUTION!
Definition: DHistogram.h:246
float GetHighEdge(void) const
Definition: DHistogram.h:238
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
void SetBinContent(int bin, float val)
Definition: DHistogram.h:294
void Scale(float scale)
Definition: DHistogram.h:286
float GetBinContent(int bin) const
Definition: DHistogram.h:212
float half_binWidth
Definition: DHistogram.h:62
void Reset(void)
Definition: DHistogram.h:278
virtual ~DHistogram()
Definition: DHistogram.h:104
float GetBinWidth(void) const
Definition: DHistogram.h:222
DHistogram & operator=(const DHistogram &hsrc)
Definition: DHistogram.h:304
int FindFirstBinAbove(float val, int start_bin=1) const
Definition: DHistogram.h:137
#define _DBG_
Definition: HDEVIO.h:12
float Fill(float x, float weight=1.0)
Definition: DHistogram.h:173
float lowEdge
Definition: DHistogram.h:59
int FindBin(float x) const
Definition: DHistogram.h:127
void Add(const DHistogram *h)
Definition: DHistogram.h:112
float Integral(void) const
Definition: DHistogram.h:254
float highEdge
Definition: DHistogram.h:60
float GetBinLowEdge(int bin) const
Definition: DHistogram.h:194
float bin_zero_center
Definition: DHistogram.h:63
float * content
Definition: DHistogram.h:65
int GetNbins(void) const
Definition: DHistogram.h:186
#define I(x, y, z)
float GetBinCenter(int bin) const
Definition: DHistogram.h:202
int FindFirstNonZeroBin(void) const
Definition: DHistogram.h:149