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