]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/hough/AliL3Histogram.cxx
Bugfix for gcc-3.2 and faster AliL3Histogram class.
[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 GCCVERSION == 3
12 using namespace std;
13 #endif
14
15 //_____________________________________________________________
16 // AliL3Histogram
17 //
18 // 2D histogram class
19
20 ClassImp(AliL3Histogram)
21
22 AliL3Histogram::AliL3Histogram()
23 {
24   fNxbins = 0;
25   fNybins = 0;
26   fNcells = 0;
27   fXmin = 0;
28   fYmin = 0;
29   fXmax = 0;
30   fYmax = 0;
31   fBinwidthX = 0;
32   fBinwidthY = 0;  
33   fFirstXbin = 0;
34   fLastXbin = 0;
35   fFirstYbin = 0;
36   fLastYbin = 0;
37   fEntries = 0;
38   fContent = 0;
39   fThreshold = 0;
40 #ifdef use_root
41   fRootHisto = 0;
42 #endif
43 }
44
45 AliL3Histogram::AliL3Histogram(Char_t *name,Char_t *id,
46                                Int_t nxbin,Double_t xmin,Double_t xmax,
47                                Int_t nybin,Double_t ymin,Double_t ymax) 
48 {
49   strcpy(fName,name);
50
51   fNxbins = nxbin;
52   fNybins = nybin;
53   fNcells = (nxbin+2)*(nybin+2);
54   fXmin = xmin;
55   fYmin = ymin;
56   fXmax = xmax;
57   fYmax = ymax;
58   fBinwidthX = (fXmax - fXmin) / fNxbins;
59   fBinwidthY = (fYmax - fYmin) / fNybins;
60   
61   fEntries = 0;
62   fFirstXbin = 1;
63   fFirstYbin = 1;
64   fLastXbin = nxbin;
65   fLastYbin = nybin;
66 #ifdef use_root
67   fRootHisto = 0;
68 #endif
69   fThreshold = 0;
70
71   fContent = new Int_t[fNcells];
72   Reset();
73 }
74
75 AliL3Histogram::~AliL3Histogram()
76 {
77   //Destructor
78   if(fContent)
79     delete [] fContent;
80 #ifdef use_root
81   if(fRootHisto)
82     delete fRootHisto;
83 #endif
84 }
85
86 void AliL3Histogram::Reset()
87 {
88   if(fContent)
89     for(Int_t i=0; i<fNcells; i++) fContent[i] = 0;
90
91   fEntries=0;
92 }
93
94 void AliL3Histogram::Fill(Double_t x,Double_t y,Int_t weight)
95 {
96   Int_t bin = FindBin(x,y);
97 #ifdef _IFON_
98   if(bin < 0)
99     return;
100 #endif
101   
102   AddBinContent(bin,weight);
103 }
104
105 void AliL3Histogram::Fill(Double_t x,Int_t ybin,Int_t weight)
106 {
107   Int_t xbin = FindXbin(x);
108   Int_t bin = GetBin(xbin,ybin);
109 #ifdef _IFON_
110   if(bin < 0)
111     return;
112 #endif
113   
114   AddBinContent(bin,weight);
115 }
116
117 void AliL3Histogram::Fill(Int_t xbin,Double_t y,Int_t weight)
118 {
119   Int_t ybin = FindYbin(y);
120   Int_t bin = GetBin(xbin,ybin);
121 #ifdef _IFON_
122   if(bin < 0)
123     return;
124 #endif
125   
126   AddBinContent(bin,weight);
127 }
128
129 void AliL3Histogram::Fill(Int_t xbin,Int_t ybin,Int_t weight)
130 {
131   Int_t bin = GetBin(xbin,ybin);
132 #ifdef _IFON_
133   if(bin < 0)
134     return;
135 #endif
136   
137   AddBinContent(bin,weight);
138 }
139
140 Int_t AliL3Histogram::FindBin(Double_t x,Double_t y) const
141 {
142   Int_t xbin = FindXbin(x);
143   Int_t ybin = FindYbin(y);
144 #ifdef _IFON_
145   if(!xbin || !ybin)
146     return -1;
147 #endif
148   
149   return GetBin(xbin,ybin);
150 }
151
152 Int_t AliL3Histogram::FindXbin(Double_t x) const
153 {
154   if(x < fXmin || x > fXmax)
155     return 0;
156   
157   return 1 + (Int_t)(fNxbins*(x-fXmin)/(fXmax-fXmin));
158 }
159
160 Int_t AliL3Histogram::FindYbin(Double_t y) const
161 {
162   if(y < fYmin || y > fYmax)
163     return 0;
164   
165   return 1 + (Int_t)(fNybins*(y-fYmin)/(fYmax-fYmin));
166 }
167
168 Int_t AliL3Histogram::GetBin(Int_t xbin,Int_t ybin) const
169 {
170   if(xbin < fFirstXbin || xbin > fLastXbin)
171     return 0;
172   if(ybin < fFirstYbin || ybin > fLastYbin)
173     return 0;
174     
175   return xbin + ybin*(fNxbins+2);
176 }
177
178 Int_t AliL3Histogram::GetBinContent(Int_t bin) const
179 {
180   if(bin >= fNcells)
181     {
182       LOG(AliL3Log::kError,"AliL3Histogram::GetBinContent","array")<<AliL3Log::kDec<<
183         "bin out of range "<<bin<<ENDLOG;
184       return 0;
185     }
186   
187   if(fContent[bin] < fThreshold)
188     return 0;
189   return fContent[bin];
190 }
191
192 void AliL3Histogram::SetBinContent(Int_t xbin,Int_t ybin,Int_t value)
193 {
194   Int_t bin = GetBin(xbin,ybin);
195 #ifdef _IFON_
196   if(bin == 0) 
197     return;
198 #endif
199
200   SetBinContent(bin,value);
201 }
202
203 void AliL3Histogram::SetBinContent(Int_t bin,Int_t value)
204 {
205
206   if(bin >= fNcells)
207     {
208       LOG(AliL3Log::kError,"AliL3Histogram::SetBinContent","array")<<AliL3Log::kDec<<
209         "bin out of range "<<bin<<ENDLOG;
210       return;
211     }
212
213   if(bin == 0)
214     return;
215   fContent[bin]=value;
216 }
217
218 void AliL3Histogram::AddBinContent(Int_t xbin,Int_t ybin,Int_t weight)
219 {
220   Int_t bin = GetBin(xbin,ybin);
221 #ifdef _IFON_
222   if(bin == 0)
223     return;
224 #endif
225
226   AddBinContent(bin,weight);
227 }
228
229 void AliL3Histogram::AddBinContent(Int_t bin,Int_t weight)
230 {
231   if(bin < 0 || bin > fNcells)
232     {
233       LOG(AliL3Log::kError,"AliL3Histogram::AddBinContent","array")<<AliL3Log::kDec<<
234         "bin-value out of range "<<bin<<ENDLOG;
235       return;
236     }
237   if(bin == 0)
238     return;
239   fEntries++;
240   fContent[bin] += weight;
241 }
242
243 void AliL3Histogram::Add(AliL3Histogram *h1,Double_t weight)
244 {
245   //Adding two histograms. Should be identical.
246   
247   if(!h1)
248     {
249       LOG(AliL3Log::kError,"AliL3Histogram::Add","Pointer")<<
250         "Attempting to add a non-existing histogram"<<ENDLOG;
251       return;
252     }
253   
254   if(h1->GetNbinsX()!=fNxbins || h1->GetNbinsY()!=fNybins)
255     {
256       LOG(AliL3Log::kError,"AliL3Histogram::Add","array")<<
257         "Mismatch in the number of bins "<<ENDLOG;
258       return;
259     }
260
261   if(h1->GetFirstXbin()!=fFirstXbin || h1->GetLastXbin()!=fLastXbin ||
262      h1->GetFirstYbin()!=fFirstYbin || h1->GetLastYbin()!=fLastYbin)
263     {
264       LOG(AliL3Log::kError,"AliL3Histogram::Add","array")<<
265         "Mismatch in the bin numbering "<<ENDLOG;
266       return;
267     }
268   
269   for(Int_t bin=0; bin<fNcells; bin++)
270     fContent[bin] += h1->GetBinContent(bin);
271   
272   fEntries += h1->GetNEntries();
273 }
274
275 Double_t AliL3Histogram::GetBinCenterX(Int_t xbin) const
276 {
277   if(xbin < fFirstXbin || xbin > fLastXbin)
278     {
279       LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterX","xbin")
280         <<"Bin-value out of range "<<xbin<<ENDLOG;
281       return -1;
282     }
283
284   return fXmin + (xbin-0.5) * fBinwidthX;
285 }
286
287 Double_t AliL3Histogram::GetBinCenterY(Int_t ybin) const
288 {
289   if(ybin < fFirstYbin || ybin > fLastYbin)
290     {
291       LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterY","ybin")
292         <<"Bin-value out of range "<<ybin<<ENDLOG;
293       return -1;
294     }
295
296   return fYmin + (ybin-0.5) * fBinwidthY;
297 }
298
299 void AliL3Histogram::Draw(Char_t *option)
300 {
301 #ifdef use_root
302   if(!fRootHisto)
303     CreateRootHisto();
304   
305   for(Int_t xbin=GetFirstXbin(); xbin<=GetLastXbin(); xbin++)
306     {
307       for(Int_t ybin=GetFirstYbin(); ybin<=GetLastYbin(); ybin++)
308         {
309           Int_t bin = GetBin(xbin,ybin);
310           fRootHisto->Fill(GetBinCenterX(xbin),GetBinCenterY(ybin),GetBinContent(bin));
311         }
312     }
313   
314   //fRootHisto->SetStats(kFALSE);
315   fRootHisto->Draw(option);
316   return;
317 #endif
318   cerr<<"AliL3Histogram::Draw : You need to compile with ROOT in order to draw histogram"<<endl;
319   
320 }
321
322 void AliL3Histogram::CreateRootHisto()
323 {
324 #ifdef use_root
325   fRootHisto = new TH2F(fName,"",fNxbins,fXmin,fXmax,fNybins,fYmin,fYmax);
326   return;
327 #endif
328   cerr<<"AliL3Histogram::CreateRootHisto : You need to compile with ROOT in order to create ROOT histogram"<<endl;
329 }
330
331 ofstream& operator<<(ofstream &o, const AliL3Histogram &h)
332 {
333   for(Int_t xbin=h.GetFirstXbin(); xbin<=h.GetLastXbin(); xbin++)
334     {
335       for(Int_t ybin=h.GetFirstYbin(); ybin<=h.GetLastYbin(); ybin++)
336         {
337           Int_t bin = h.GetBin(xbin,ybin);
338           o << h.GetBinContent(bin) << " ";
339         }
340       o << endl;
341     }
342   return o;
343 }