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