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