]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/hough/AliL3Histogram.cxx
Little changes to make g++ version 3.2 compile the hough library.
[u/mrichter/AliRoot.git] / HLT / hough / AliL3Histogram.cxx
1 //$Id$
2
3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright &copy ASV
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   AddBinContent(bin,weight);
98 }
99
100 Int_t AliL3Histogram::FindBin(Double_t x,Double_t y)
101 {
102   Int_t xbin = FindXbin(x);
103   Int_t ybin = FindYbin(y);
104   
105   return GetBin(xbin,ybin);
106 }
107
108 Int_t AliL3Histogram::FindXbin(Double_t x)
109 {
110   if(x < fXmin || x > fXmax)
111     return 0;
112   
113   return 1 + (Int_t)(fNxbins*(x-fXmin)/(fXmax-fXmin));
114 }
115
116 Int_t AliL3Histogram::FindYbin(Double_t y)
117 {
118   if(y < fYmin || y > fYmax)
119     return 0;
120   
121   return 1 + (Int_t)(fNybins*(y-fYmin)/(fYmax-fYmin));
122 }
123
124 Int_t AliL3Histogram::GetBin(Int_t xbin,Int_t ybin)
125 {
126   if(xbin < 0 || xbin > GetLastXbin())
127     {
128       LOG(AliL3Log::kError,"AliL3Histogram::GetBin","array")<<AliL3Log::kDec<<
129         "xbin out of range "<<xbin<<ENDLOG;
130       return 0;
131     }
132
133   if(ybin < 0 || ybin > GetLastYbin())
134     {
135       LOG(AliL3Log::kError,"AliL3Histogram::Getbin","array")<<AliL3Log::kDec<<
136         "ybin out of range "<<xbin<<ENDLOG;
137       return 0;
138     }
139     
140   return xbin + ybin*(fNxbins+2);
141 }
142
143 Int_t AliL3Histogram::GetBinContent(Int_t bin)
144 {
145   if(bin >= fNcells)
146     {
147       LOG(AliL3Log::kError,"AliL3Histogram::GetBinContent","array")<<AliL3Log::kDec<<
148         "bin out of range "<<bin<<ENDLOG;
149       return 0;
150     }
151   
152   if(fContent[bin] < fThreshold)
153     return 0;
154   return fContent[bin];
155 }
156
157 void AliL3Histogram::SetBinContent(Int_t xbin,Int_t ybin,Int_t value)
158 {
159   Int_t bin = GetBin(xbin,ybin);
160   if(bin == 0) 
161     return;
162   SetBinContent(bin,value);
163 }
164
165 void AliL3Histogram::SetBinContent(Int_t bin,Int_t value)
166 {
167
168   if(bin >= fNcells)
169     {
170       LOG(AliL3Log::kError,"AliL3Histogram::SetBinContent","array")<<AliL3Log::kDec<<
171         "bin out of range "<<bin<<ENDLOG;
172       return;
173     }
174
175   if(bin == 0)
176     return;
177   fContent[bin]=value;
178 }
179
180 void AliL3Histogram::AddBinContent(Int_t xbin,Int_t ybin,Int_t weight)
181 {
182   Int_t bin = GetBin(xbin,ybin);
183   if(bin == 0)
184     return;
185   AddBinContent(bin,weight);
186 }
187
188 void AliL3Histogram::AddBinContent(Int_t bin,Int_t weight)
189 {
190   if(bin < 0 || bin > fNcells)
191     {
192       LOG(AliL3Log::kError,"AliL3Histogram::AddBinContent","array")<<AliL3Log::kDec<<
193         "bin-value out of range "<<bin<<ENDLOG;
194       return;
195     }
196   if(bin == 0)
197     return;
198   fEntries++;
199   fContent[bin] += weight;
200 }
201
202 void AliL3Histogram::Add(AliL3Histogram *h1,Double_t weight)
203 {
204   //Adding two histograms. Should be identical.
205   
206   if(!h1)
207     {
208       LOG(AliL3Log::kError,"AliL3Histogram::Add","Pointer")<<
209         "Attempting to add a non-existing histogram"<<ENDLOG;
210       return;
211     }
212   
213   if(h1->GetNbinsX()!=fNxbins || h1->GetNbinsY()!=fNybins)
214     {
215       LOG(AliL3Log::kError,"AliL3Histogram::Add","array")<<
216         "Mismatch in the number of bins "<<ENDLOG;
217       return;
218     }
219   if(h1->GetFirstXbin()!=fFirstXbin || h1->GetLastXbin()!=fLastXbin ||
220      h1->GetFirstYbin()!=fFirstYbin || h1->GetLastYbin()!=fLastYbin)
221     {
222       LOG(AliL3Log::kError,"AliL3Histogram::Add","array")<<
223         "Mismatch in the bin numbering "<<ENDLOG;
224       return;
225     }
226   
227   for(Int_t bin=0; bin<fNcells; bin++)
228     fContent[bin] += h1->GetBinContent(bin);
229   
230   fEntries += h1->GetNEntries();
231 }
232
233 Double_t AliL3Histogram::GetBinCenterX(Int_t xbin)
234 {
235   //  return fXmin + (xbin-1) * fBinwidthX + 0.5*fBinwidthX;
236   return fXmin + (xbin-0.5) * fBinwidthX;
237 }
238
239 Double_t AliL3Histogram::GetBinCenterY(Int_t ybin)
240 {
241   //  return fYmin + (ybin-1) * fBinwidthY + 0.5*fBinwidthY;
242   return fYmin + (ybin-0.5) * fBinwidthY;
243 }
244
245 void AliL3Histogram::Draw(Char_t *option)
246 {
247 #ifdef use_root
248   if(!fRootHisto)
249     CreateRootHisto();
250   for(Int_t bin=0; bin<fNcells; bin++)
251     {
252       fRootHisto->AddBinContent(bin,GetBinContent(bin));
253     }
254   
255   fRootHisto->SetStats(kFALSE);
256   fRootHisto->Draw(option);
257   return;
258 #endif
259   cerr<<"AliL3Histogram::Draw : You need to compile with ROOT in order to draw histogram"<<endl;
260   
261 }
262
263 void AliL3Histogram::CreateRootHisto()
264 {
265 #ifdef use_root
266   fRootHisto = new TH2F(fName,"",fNxbins,fXmin,fXmax,fNybins,fYmin,fYmax);
267   return;
268 #endif
269   cerr<<"AliL3Histogram::CreateRootHisto : You need to compile with ROOT in order to create ROOT histogram"<<endl;
270 }