]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/hough/AliL3Histogram.cxx
Some new changes in the adaptive histogram. Binsize in kappa are determined
[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 <string.h>
7 #include "AliL3Logging.h"
8 #include "AliL3Histogram.h"
9
10 //_____________________________________________________________
11 // AliL3Histogram
12 //
13 // 2D histogram class
14
15 ClassImp(AliL3Histogram)
16
17 AliL3Histogram::AliL3Histogram()
18 {
19   fNxbins = 0;
20   fNybins = 0;
21   fNcells = 0;
22   fXmin = 0;
23   fYmin = 0;
24   fXmax = 0;
25   fYmax = 0;
26   fFirstXbin = 0;
27   fLastXbin = 0;
28   fFirstYbin = 0;
29   fLastYbin = 0;
30   fEntries = 0;
31   fContent = 0;
32   fThreshold = 0;
33 #ifdef use_root
34   fRootHisto = 0;
35 #endif
36 }
37
38   
39 AliL3Histogram::AliL3Histogram(Char_t *name,Char_t *id,
40                                Int_t nxbin,Double_t xmin,Double_t xmax,
41                                Int_t nybin,Double_t ymin,Double_t ymax) 
42 {
43   
44   strcpy(fName,name);
45   fNxbins = nxbin;
46   fNybins = nybin;
47   fNcells = (nxbin+2)*(nybin+2);
48   
49   fXmin = xmin;
50   fYmin = ymin;
51   fXmax = xmax;
52   fYmax = ymax;
53   fEntries = 0;
54   fFirstXbin = 1;
55   fFirstYbin = 1;
56   fLastXbin = nxbin;
57   fLastYbin = nybin;
58 #ifdef use_root
59   fRootHisto = 0;
60 #endif
61   fThreshold = 0;
62
63   fContent = new Int_t[fNcells];
64   Reset();
65 }
66
67 AliL3Histogram::~AliL3Histogram()
68 {
69   //Destructor
70   if(fContent)
71     delete [] fContent;
72 #ifdef use_root
73   if(fRootHisto)
74     delete fRootHisto;
75 #endif
76   
77 }
78
79
80 void AliL3Histogram::Reset()
81 {
82   
83   for(Int_t i=0; i<fNcells; i++)
84     fContent[i] = 0;
85   fEntries=0;
86 }
87
88 void AliL3Histogram::Fill(Double_t x,Double_t y,Int_t weight)
89 {
90   Int_t bin = FindBin(x,y);
91   AddBinContent(bin,weight);
92
93 }
94
95 Int_t AliL3Histogram::FindBin(Double_t x,Double_t y)
96 {
97   
98   Int_t xbin = FindXbin(x);
99   Int_t ybin = FindYbin(y);
100   
101   return GetBin(xbin,ybin);
102 }
103
104 Int_t AliL3Histogram::FindXbin(Double_t x)
105 {
106   if(x < fXmin || x > fXmax)
107     return 0;
108   
109   return 1 + (Int_t)(fNxbins*(x-fXmin)/(fXmax-fXmin));
110
111 }
112
113 Int_t AliL3Histogram::FindYbin(Double_t y)
114 {
115   if(y < fYmin || y > fYmax)
116     return 0;
117   
118   return 1 + (Int_t)(fNybins*(y-fYmin)/(fYmax-fYmin));
119
120 }
121
122 Int_t AliL3Histogram::GetBin(Int_t xbin,Int_t ybin)
123 {
124   if(xbin < 0 || xbin > GetLastXbin())
125     {
126       LOG(AliL3Log::kError,"AliL3Histogram::GetBin","array")<<AliL3Log::kDec<<
127         "xbin out of range "<<xbin<<ENDLOG;
128       return 0;
129     }
130   if(ybin < 0 || ybin > GetLastYbin())
131     {
132       LOG(AliL3Log::kError,"AliL3Histogram::Getbin","array")<<AliL3Log::kDec<<
133         "ybin out of range "<<xbin<<ENDLOG;
134       return 0;
135     }
136     
137   return xbin + ybin*(fNxbins+2);
138 }
139
140 Int_t AliL3Histogram::GetBinContent(Int_t bin)
141 {
142   if(bin >= fNcells)
143     {
144       LOG(AliL3Log::kError,"AliL3Histogram::GetBinContent","array")<<AliL3Log::kDec<<
145         "bin out of range "<<bin<<ENDLOG;
146       return 0;
147     }
148   
149   if(fContent[bin] < fThreshold)
150     return 0;
151   return fContent[bin];
152 }
153
154 void AliL3Histogram::SetBinContent(Int_t xbin,Int_t ybin,Int_t value)
155 {
156   Int_t bin = GetBin(xbin,ybin);
157   if(bin == 0) 
158     return;
159   SetBinContent(bin,value);
160 }
161
162 void AliL3Histogram::SetBinContent(Int_t bin,Int_t value)
163 {
164
165   if(bin >= fNcells)
166     {
167       LOG(AliL3Log::kError,"AliL3Histogram::SetBinContent","array")<<AliL3Log::kDec<<
168         "bin out of range "<<bin<<ENDLOG;
169       return;
170     }
171   if(bin == 0)
172     return;
173   fContent[bin]=value;
174   
175 }
176
177 void AliL3Histogram::AddBinContent(Int_t xbin,Int_t ybin,Int_t weight)
178 {
179   Int_t bin = GetBin(xbin,ybin);
180   if(bin == 0)
181     return;
182   AddBinContent(bin,weight);
183
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)
232 {
233   
234   Double_t binwidth = (fXmax - fXmin) / fNxbins;
235   return fXmin + (xbin-1) * binwidth + 0.5*binwidth;
236   
237 }
238
239 Double_t AliL3Histogram::GetBinCenterY(Int_t ybin)
240 {
241   
242   Double_t binwidth = (fYmax - fYmin) / fNybins;
243   return fYmin + (ybin-1) * binwidth + 0.5*binwidth;
244   
245 }
246
247
248 void AliL3Histogram::Draw(Char_t *option)
249 {
250 #ifdef use_root
251   if(!fRootHisto)
252     CreateRootHisto();
253   for(Int_t bin=0; bin<fNcells; bin++)
254     {
255       fRootHisto->AddBinContent(bin,GetBinContent(bin));
256     }
257   
258   fRootHisto->SetStats(kFALSE);
259   fRootHisto->Draw(option);
260   return;
261 #endif
262   cerr<<"AliL3Histogram::Draw : You need to compile with ROOT in order to draw histogram"<<endl;
263   
264 }
265
266 void AliL3Histogram::CreateRootHisto()
267 {
268 #ifdef use_root
269   fRootHisto = new TH2F(fName,"",fNxbins,fXmin,fXmax,fNybins,fYmin,fYmax);
270   return;
271 #endif
272   cerr<<"AliL3Histogram::CreateRootHisto : You need to compile with ROOT in order to create ROOT histogram"<<endl;
273 }