Fixed missing initialization for some variables at the edges of the hough space
[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{
917e711b 32 // Default constructor
18758be6 33 fNxbins = 0;
34 fNybins = 0;
35 fNcells = 0;
36 fXmin = 0;
37 fYmin = 0;
38 fXmax = 0;
39 fYmax = 0;
e06900d5 40 fBinwidthX = 0;
41 fBinwidthY = 0;
4cafa5fc 42 fFirstXbin = 0;
43 fLastXbin = 0;
44 fFirstYbin = 0;
45 fLastYbin = 0;
18758be6 46 fEntries = 0;
47 fContent = 0;
e1842819 48 fThreshold = 0;
6b9816d6 49#ifdef use_root
50 fRootHisto = 0;
51#endif
4de874d1 52}
53
dd7d3870 54AliL3Histogram::AliL3Histogram(Char_t *name,Char_t */*id*/,
e1842819 55 Int_t nxbin,Double_t xmin,Double_t xmax,
56 Int_t nybin,Double_t ymin,Double_t ymax)
18758be6 57{
917e711b 58 // Normal constructor
18758be6 59 strcpy(fName,name);
e06900d5 60
18758be6 61 fNxbins = nxbin;
62 fNybins = nybin;
63 fNcells = (nxbin+2)*(nybin+2);
18758be6 64 fXmin = xmin;
65 fYmin = ymin;
66 fXmax = xmax;
67 fYmax = ymax;
e06900d5 68 fBinwidthX = (fXmax - fXmin) / fNxbins;
69 fBinwidthY = (fYmax - fYmin) / fNybins;
70
18758be6 71 fEntries = 0;
4cafa5fc 72 fFirstXbin = 1;
73 fFirstYbin = 1;
74 fLastXbin = nxbin;
75 fLastYbin = nybin;
36d25d02 76#ifdef use_root
4cafa5fc 77 fRootHisto = 0;
36d25d02 78#endif
e1842819 79 fThreshold = 0;
4cafa5fc 80
cfc41e5b 81 fContent = new Int_t[fNcells];
18758be6 82 Reset();
83}
4de874d1 84
85AliL3Histogram::~AliL3Histogram()
86{
87 //Destructor
18758be6 88 if(fContent)
89 delete [] fContent;
36d25d02 90#ifdef use_root
4cafa5fc 91 if(fRootHisto)
92 delete fRootHisto;
36d25d02 93#endif
18758be6 94}
95
18758be6 96void AliL3Histogram::Reset()
97{
917e711b 98 // Reset histogram contents
e06900d5 99 if(fContent)
100 for(Int_t i=0; i<fNcells; i++) fContent[i] = 0;
101
18758be6 102 fEntries=0;
103}
104
105void AliL3Histogram::Fill(Double_t x,Double_t y,Int_t weight)
106{
917e711b 107 // Fill the weight into a bin which correspond to x and y
18758be6 108 Int_t bin = FindBin(x,y);
910f8d43 109#ifdef _IFON_
b2a02bce 110 if(bin < 0)
111 return;
910f8d43 112#endif
113
114 AddBinContent(bin,weight);
115}
116
117void AliL3Histogram::Fill(Double_t x,Int_t ybin,Int_t weight)
118{
917e711b 119 // Fill the weight into a bin which correspond to x and ybin
910f8d43 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
130void AliL3Histogram::Fill(Int_t xbin,Double_t y,Int_t weight)
131{
917e711b 132 // Fill the weight into a bin which correspond to xbin and y
910f8d43 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
143void AliL3Histogram::Fill(Int_t xbin,Int_t ybin,Int_t weight)
144{
917e711b 145 // Fill the weight into a bin which correspond to xbin and ybin
910f8d43 146 Int_t bin = GetBin(xbin,ybin);
147#ifdef _IFON_
148 if(bin < 0)
149 return;
150#endif
b2a02bce 151
18758be6 152 AddBinContent(bin,weight);
18758be6 153}
154
1f1942b8 155Int_t AliL3Histogram::FindBin(Double_t x,Double_t y) const
4cafa5fc 156{
917e711b 157 // Finds the bin which correspond to x and y
4cafa5fc 158 Int_t xbin = FindXbin(x);
159 Int_t ybin = FindYbin(y);
910f8d43 160#ifdef _IFON_
b2a02bce 161 if(!xbin || !ybin)
162 return -1;
910f8d43 163#endif
7a21af2f 164
4cafa5fc 165 return GetBin(xbin,ybin);
166}
167
e81ffe36 168Int_t AliL3Histogram::FindLabelBin(Double_t x,Double_t y) const
169{
917e711b 170 // Returns the corresponding bin with the mc labels
e81ffe36 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
1f1942b8 181Int_t AliL3Histogram::FindXbin(Double_t x) const
18758be6 182{
917e711b 183 // Finds the bin which correspond to x
18758be6 184 if(x < fXmin || x > fXmax)
7a21af2f 185 return 0;
4cafa5fc 186
187 return 1 + (Int_t)(fNxbins*(x-fXmin)/(fXmax-fXmin));
4cafa5fc 188}
189
1f1942b8 190Int_t AliL3Histogram::FindYbin(Double_t y) const
4cafa5fc 191{
917e711b 192 // Finds the bin which correspond to y
18758be6 193 if(y < fYmin || y > fYmax)
7a21af2f 194 return 0;
18758be6 195
4cafa5fc 196 return 1 + (Int_t)(fNybins*(y-fYmin)/(fYmax-fYmin));
4cafa5fc 197}
198
1f1942b8 199Int_t AliL3Histogram::GetBin(Int_t xbin,Int_t ybin) const
4cafa5fc 200{
917e711b 201 // Returns the bin which correspond to xbin and ybin
0a86fbb7 202 if(xbin < fFirstXbin || xbin > fLastXbin)
203 return 0;
204 if(ybin < fFirstYbin || ybin > fLastYbin)
205 return 0;
4cafa5fc 206
18758be6 207 return xbin + ybin*(fNxbins+2);
4cafa5fc 208}
209
e81ffe36 210Int_t AliL3Histogram::GetLabelBin(Int_t xbin,Int_t ybin) const
211{
917e711b 212 // Returns the corresponding bin with the mc labels
e81ffe36 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
1f1942b8 221Int_t AliL3Histogram::GetBinContent(Int_t bin) const
4cafa5fc 222{
917e711b 223 // Return the bin content
4cafa5fc 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
e1842819 231 if(fContent[bin] < fThreshold)
232 return 0;
4cafa5fc 233 return fContent[bin];
234}
235
7a21af2f 236void AliL3Histogram::SetBinContent(Int_t xbin,Int_t ybin,Int_t value)
4cafa5fc 237{
917e711b 238 // Set bin content
4cafa5fc 239 Int_t bin = GetBin(xbin,ybin);
910f8d43 240#ifdef _IFON_
7a21af2f 241 if(bin == 0)
242 return;
910f8d43 243#endif
244
7a21af2f 245 SetBinContent(bin,value);
4cafa5fc 246}
247
7a21af2f 248void AliL3Histogram::SetBinContent(Int_t bin,Int_t value)
4cafa5fc 249{
917e711b 250 // Set bin content
4cafa5fc 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 }
e06900d5 258
7a21af2f 259 if(bin == 0)
260 return;
261 fContent[bin]=value;
18758be6 262}
263
264void AliL3Histogram::AddBinContent(Int_t xbin,Int_t ybin,Int_t weight)
265{
917e711b 266 // Adds weight to bin content
4cafa5fc 267 Int_t bin = GetBin(xbin,ybin);
910f8d43 268#ifdef _IFON_
7a21af2f 269 if(bin == 0)
270 return;
910f8d43 271#endif
272
18758be6 273 AddBinContent(bin,weight);
18758be6 274}
275
276void AliL3Histogram::AddBinContent(Int_t bin,Int_t weight)
277{
917e711b 278 // Adds weight to bin content
18758be6 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 }
7a21af2f 285 if(bin == 0)
286 return;
18758be6 287 fEntries++;
288 fContent[bin] += weight;
289}
290
dd7d3870 291void AliL3Histogram::Add(AliL3Histogram *h1,Double_t /*weight*/)
ad11f553 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 }
910f8d43 308
ad11f553 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
e7139de1 320 fEntries += h1->GetNEntries();
ad11f553 321}
322
1f1942b8 323Double_t AliL3Histogram::GetBinCenterX(Int_t xbin) const
18758be6 324{
917e711b 325 // Returns the position of the center of a bin
0a86fbb7 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 }
1f1942b8 332
e06900d5 333 return fXmin + (xbin-0.5) * fBinwidthX;
18758be6 334}
335
1f1942b8 336Double_t AliL3Histogram::GetBinCenterY(Int_t ybin) const
18758be6 337{
917e711b 338 // Returns the position of the center of a bin
0a86fbb7 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 }
1f1942b8 345
e06900d5 346 return fYmin + (ybin-0.5) * fBinwidthY;
18758be6 347}
348
0bd0c1ef 349Double_t AliL3Histogram::GetPreciseBinCenterX(Float_t xbin) const
350{
917e711b 351 // Returns the position of the center of a bin using precise values inside the bin
74e77d1b 352 if(xbin < (fFirstXbin-1.5) || xbin > (fLastXbin+1.5))
0bd0c1ef 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
362Double_t AliL3Histogram::GetPreciseBinCenterY(Float_t ybin) const
363{
917e711b 364 // Returns the position of the center of a bin using precise values inside the bin
74e77d1b 365 if(ybin < (fFirstYbin-1.5) || ybin > (fLastYbin+1.5))
0bd0c1ef 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
4cafa5fc 375void AliL3Histogram::Draw(Char_t *option)
18758be6 376{
917e711b 377 // Fill the contents of the corresponding ROOT histogram and draws it
237d3f5c 378#ifdef use_root
7b9d6d4e 379 if(!fRootHisto)
380 CreateRootHisto();
3e87ef69 381
382 for(Int_t xbin=GetFirstXbin(); xbin<=GetLastXbin(); xbin++)
18758be6 383 {
3e87ef69 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 }
18758be6 389 }
18758be6 390
3e87ef69 391 //fRootHisto->SetStats(kFALSE);
4cafa5fc 392 fRootHisto->Draw(option);
237d3f5c 393 return;
394#endif
395 cerr<<"AliL3Histogram::Draw : You need to compile with ROOT in order to draw histogram"<<endl;
4cafa5fc 396
4de874d1 397}
7b9d6d4e 398
399void AliL3Histogram::CreateRootHisto()
400{
917e711b 401 // Create ROOT histogram out of AliL3Histogram
7b9d6d4e 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}
1f1942b8 408
910f8d43 409ofstream& operator<<(ofstream &o, const AliL3Histogram &h)
1f1942b8 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}