]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/hough/AliL3Histogram.cxx
06db92748ecfb7ef4364b0fa59ab9e703e5e59c4
[u/mrichter/AliRoot.git] / HLT / hough / AliL3Histogram.cxx
1 // @(#) $Id$
2
3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright &copy ALICE HLT Group
5
6 #include "AliL3StandardIncludes.h"
7
8 #include "AliL3Logging.h"
9 #include "AliL3Histogram.h"
10
11 #if __GNUC__ == 3
12 using namespace std;
13 #endif
14
15 /** \class AliL3Histogram
16 <pre>
17 //_____________________________________________________________
18 // AliL3Histogram
19 //
20 // 2D histogram class
21 //
22 </pre>
23 */
24
25 //uncomment if you want overflow checks
26 //#define _IFON_
27
28 ClassImp(AliL3Histogram)
29
30 AliL3Histogram::AliL3Histogram()
31 {
32   // Default constructor
33   fNxbins = 0;
34   fNybins = 0;
35   fNcells = 0;
36   fXmin = 0;
37   fYmin = 0;
38   fXmax = 0;
39   fYmax = 0;
40   fBinwidthX = 0;
41   fBinwidthY = 0;  
42   fFirstXbin = 0;
43   fLastXbin = 0;
44   fFirstYbin = 0;
45   fLastYbin = 0;
46   fEntries = 0;
47   fContent = 0;
48   fThreshold = 0;
49 #ifdef use_root
50   fRootHisto = 0;
51 #endif
52 }
53
54 AliL3Histogram::AliL3Histogram(Char_t *name,Char_t */*id*/,
55                                Int_t nxbin,Double_t xmin,Double_t xmax,
56                                Int_t nybin,Double_t ymin,Double_t ymax) 
57 {
58   // Normal constructor
59   strcpy(fName,name);
60
61   fNxbins = nxbin;
62   fNybins = nybin;
63   fNcells = (nxbin+2)*(nybin+2);
64   fXmin = xmin;
65   fYmin = ymin;
66   fXmax = xmax;
67   fYmax = ymax;
68   fBinwidthX = (fXmax - fXmin) / fNxbins;
69   fBinwidthY = (fYmax - fYmin) / fNybins;
70   
71   fEntries = 0;
72   fFirstXbin = 1;
73   fFirstYbin = 1;
74   fLastXbin = nxbin;
75   fLastYbin = nybin;
76 #ifdef use_root
77   fRootHisto = 0;
78 #endif
79   fThreshold = 0;
80
81   fContent = new Int_t[fNcells];
82   Reset();
83 }
84
85 AliL3Histogram::~AliL3Histogram()
86 {
87   //Destructor
88   if(fContent)
89     delete [] fContent;
90 #ifdef use_root
91   if(fRootHisto)
92     delete fRootHisto;
93 #endif
94 }
95
96 void AliL3Histogram::Reset()
97 {
98   // Reset histogram contents
99   if(fContent)
100     for(Int_t i=0; i<fNcells; i++) fContent[i] = 0;
101
102   fEntries=0;
103 }
104
105 void AliL3Histogram::Fill(Double_t x,Double_t y,Int_t weight)
106 {
107   // Fill the weight into a bin which correspond to x and y
108   Int_t bin = FindBin(x,y);
109 #ifdef _IFON_
110   if(bin < 0)
111     return;
112 #endif
113   
114   AddBinContent(bin,weight);
115 }
116
117 void AliL3Histogram::Fill(Double_t x,Int_t ybin,Int_t weight)
118 {
119   // Fill the weight into a bin which correspond to x and ybin
120   Int_t xbin = FindXbin(x);
121   Int_t bin = GetBin(xbin,ybin);
122 #ifdef _IFON_
123   if(bin < 0)
124     return;
125 #endif
126   
127   AddBinContent(bin,weight);
128 }
129
130 void AliL3Histogram::Fill(Int_t xbin,Double_t y,Int_t weight)
131 {
132   // Fill the weight into a bin which correspond to xbin and y
133   Int_t ybin = FindYbin(y);
134   Int_t bin = GetBin(xbin,ybin);
135 #ifdef _IFON_
136   if(bin < 0)
137     return;
138 #endif
139   
140   AddBinContent(bin,weight);
141 }
142
143 void AliL3Histogram::Fill(Int_t xbin,Int_t ybin,Int_t weight)
144 {
145   // Fill the weight into a bin which correspond to xbin and ybin
146   Int_t bin = GetBin(xbin,ybin);
147 #ifdef _IFON_
148   if(bin < 0)
149     return;
150 #endif
151   
152   AddBinContent(bin,weight);
153 }
154
155 Int_t AliL3Histogram::FindBin(Double_t x,Double_t y) const
156 {
157   // Finds the bin which correspond to x and y
158   Int_t xbin = FindXbin(x);
159   Int_t ybin = FindYbin(y);
160 #ifdef _IFON_
161   if(!xbin || !ybin)
162     return -1;
163 #endif
164   
165   return GetBin(xbin,ybin);
166 }
167
168 Int_t AliL3Histogram::FindLabelBin(Double_t x,Double_t y) const
169 {
170   // Returns the corresponding bin with the mc labels
171   Int_t xbin = FindXbin(x);
172   Int_t ybin = FindYbin(y);
173 #ifdef _IFON_
174   if(!xbin || !ybin)
175     return -1;
176 #endif
177   
178   return GetLabelBin(xbin,ybin);
179 }
180
181 Int_t AliL3Histogram::FindXbin(Double_t x) const
182 {
183   // Finds the bin which correspond to x
184   if(x < fXmin || x > fXmax)
185     return 0;
186   
187   return 1 + (Int_t)(fNxbins*(x-fXmin)/(fXmax-fXmin));
188 }
189
190 Int_t AliL3Histogram::FindYbin(Double_t y) const
191 {
192   // Finds the bin which correspond to y
193   if(y < fYmin || y > fYmax)
194     return 0;
195   
196   return 1 + (Int_t)(fNybins*(y-fYmin)/(fYmax-fYmin));
197 }
198
199 Int_t AliL3Histogram::GetBin(Int_t xbin,Int_t ybin) const
200 {
201   // Returns the bin which correspond to xbin and ybin
202   if(xbin < fFirstXbin || xbin > fLastXbin)
203     return 0;
204   if(ybin < fFirstYbin || ybin > fLastYbin)
205     return 0;
206     
207   return xbin + ybin*(fNxbins+2);
208 }
209
210 Int_t AliL3Histogram::GetLabelBin(Int_t xbin,Int_t ybin) const
211 {
212   // Returns the corresponding bin with the mc labels
213   if(xbin < fFirstXbin || xbin > fLastXbin)
214     return -1;
215   if(ybin < fFirstYbin || ybin > fLastYbin)
216     return -1;
217     
218   return (Int_t)(xbin/2) + ((Int_t)(ybin/2))*((Int_t)((fNxbins+3)/2));
219 }
220
221 Int_t AliL3Histogram::GetBinContent(Int_t bin) const
222 {
223   // Return the bin content
224   if(bin >= fNcells)
225     {
226       LOG(AliL3Log::kError,"AliL3Histogram::GetBinContent","array")<<AliL3Log::kDec<<
227         "bin out of range "<<bin<<ENDLOG;
228       return 0;
229     }
230   
231   if(fContent[bin] < fThreshold)
232     return 0;
233   return fContent[bin];
234 }
235
236 void AliL3Histogram::SetBinContent(Int_t xbin,Int_t ybin,Int_t value)
237 {
238   // Set bin content
239   Int_t bin = GetBin(xbin,ybin);
240 #ifdef _IFON_
241   if(bin == 0) 
242     return;
243 #endif
244
245   SetBinContent(bin,value);
246 }
247
248 void AliL3Histogram::SetBinContent(Int_t bin,Int_t value)
249 {
250   // Set bin content
251
252   if(bin >= fNcells)
253     {
254       LOG(AliL3Log::kError,"AliL3Histogram::SetBinContent","array")<<AliL3Log::kDec<<
255         "bin out of range "<<bin<<ENDLOG;
256       return;
257     }
258
259   if(bin == 0)
260     return;
261   fContent[bin]=value;
262 }
263
264 void AliL3Histogram::AddBinContent(Int_t xbin,Int_t ybin,Int_t weight)
265 {
266   // Adds weight to bin content
267   Int_t bin = GetBin(xbin,ybin);
268 #ifdef _IFON_
269   if(bin == 0)
270     return;
271 #endif
272
273   AddBinContent(bin,weight);
274 }
275
276 void AliL3Histogram::AddBinContent(Int_t bin,Int_t weight)
277 {
278   // Adds weight to bin content
279   if(bin < 0 || bin > fNcells)
280     {
281       LOG(AliL3Log::kError,"AliL3Histogram::AddBinContent","array")<<AliL3Log::kDec<<
282         "bin-value out of range "<<bin<<ENDLOG;
283       return;
284     }
285   if(bin == 0)
286     return;
287   fEntries++;
288   fContent[bin] += weight;
289 }
290
291 void AliL3Histogram::Add(AliL3Histogram *h1,Double_t /*weight*/)
292 {
293   //Adding two histograms. Should be identical.
294   
295   if(!h1)
296     {
297       LOG(AliL3Log::kError,"AliL3Histogram::Add","Pointer")<<
298         "Attempting to add a non-existing histogram"<<ENDLOG;
299       return;
300     }
301   
302   if(h1->GetNbinsX()!=fNxbins || h1->GetNbinsY()!=fNybins)
303     {
304       LOG(AliL3Log::kError,"AliL3Histogram::Add","array")<<
305         "Mismatch in the number of bins "<<ENDLOG;
306       return;
307     }
308
309   if(h1->GetFirstXbin()!=fFirstXbin || h1->GetLastXbin()!=fLastXbin ||
310      h1->GetFirstYbin()!=fFirstYbin || h1->GetLastYbin()!=fLastYbin)
311     {
312       LOG(AliL3Log::kError,"AliL3Histogram::Add","array")<<
313         "Mismatch in the bin numbering "<<ENDLOG;
314       return;
315     }
316   
317   for(Int_t bin=0; bin<fNcells; bin++)
318     fContent[bin] += h1->GetBinContent(bin);
319   
320   fEntries += h1->GetNEntries();
321 }
322
323 Double_t AliL3Histogram::GetBinCenterX(Int_t xbin) const
324 {
325   // Returns the position of the center of a bin
326   if(xbin < fFirstXbin || xbin > fLastXbin)
327     {
328       LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterX","xbin")
329         <<"Bin-value out of range "<<xbin<<ENDLOG;
330       return -1;
331     }
332
333   return fXmin + (xbin-0.5) * fBinwidthX;
334 }
335
336 Double_t AliL3Histogram::GetBinCenterY(Int_t ybin) const
337 {
338   // Returns the position of the center of a bin
339   if(ybin < fFirstYbin || ybin > fLastYbin)
340     {
341       LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterY","ybin")
342         <<"Bin-value out of range "<<ybin<<ENDLOG;
343       return -1;
344     }
345
346   return fYmin + (ybin-0.5) * fBinwidthY;
347 }
348
349 Double_t AliL3Histogram::GetPreciseBinCenterX(Float_t xbin) const
350 {
351   // Returns the position of the center of a bin using precise values inside the bin
352   if(xbin < (fFirstXbin-0.5) || xbin > (fLastXbin+0.5))
353     {
354       LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterX","xbin")
355         <<"Bin-value out of range "<<xbin<<ENDLOG;
356       return -1;
357     }
358   //  return fXmin + (xbin-1) * fBinwidthX + 0.5*fBinwidthX;
359   return fXmin + (xbin-0.5) * fBinwidthX;
360 }
361
362 Double_t AliL3Histogram::GetPreciseBinCenterY(Float_t ybin) const
363 {
364   // Returns the position of the center of a bin using precise values inside the bin
365   if(ybin < (fFirstYbin-0.5) || ybin > (fLastYbin+0.5))
366     {
367       LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterY","ybin")
368         <<"Bin-value out of range "<<ybin<<ENDLOG;
369       return -1;
370     }
371   //  return fYmin + (ybin-1) * fBinwidthY + 0.5*fBinwidthY;
372   return fYmin + (ybin-0.5) * fBinwidthY;
373 }
374
375 void AliL3Histogram::Draw(Char_t *option)
376 {
377   // Fill the contents of the corresponding ROOT histogram and draws it 
378 #ifdef use_root
379   if(!fRootHisto)
380     CreateRootHisto();
381   
382   for(Int_t xbin=GetFirstXbin(); xbin<=GetLastXbin(); xbin++)
383     {
384       for(Int_t ybin=GetFirstYbin(); ybin<=GetLastYbin(); ybin++)
385         {
386           Int_t bin = GetBin(xbin,ybin);
387           fRootHisto->Fill(GetBinCenterX(xbin),GetBinCenterY(ybin),GetBinContent(bin));
388         }
389     }
390   
391   //fRootHisto->SetStats(kFALSE);
392   fRootHisto->Draw(option);
393   return;
394 #endif
395   cerr<<"AliL3Histogram::Draw : You need to compile with ROOT in order to draw histogram"<<endl;
396   
397 }
398
399 void AliL3Histogram::CreateRootHisto()
400 {
401   // Create ROOT histogram out of AliL3Histogram
402 #ifdef use_root
403   fRootHisto = new TH2F(fName,"",fNxbins,fXmin,fXmax,fNybins,fYmin,fYmax);
404   return;
405 #endif
406   cerr<<"AliL3Histogram::CreateRootHisto : You need to compile with ROOT in order to create ROOT histogram"<<endl;
407 }
408
409 ofstream& operator<<(ofstream &o, const AliL3Histogram &h)
410 {
411   for(Int_t xbin=h.GetFirstXbin(); xbin<=h.GetLastXbin(); xbin++)
412     {
413       for(Int_t ybin=h.GetFirstYbin(); ybin<=h.GetLastYbin(); ybin++)
414         {
415           Int_t bin = h.GetBin(xbin,ybin);
416           o << h.GetBinContent(bin) << " ";
417         }
418       o << endl;
419     }
420   return o;
421 }