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