]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/hough/AliL3Histogram.cxx
55403cf0185086ac999d1b5657e9504324fc8bba
[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   if(bin < 0)
97     return;
98   
99   AddBinContent(bin,weight);
100 }
101
102 Int_t AliL3Histogram::FindBin(Double_t x,Double_t y)
103 {
104   Int_t xbin = FindXbin(x);
105   Int_t ybin = FindYbin(y);
106   if(!xbin || !ybin)
107     return -1;
108   
109   return GetBin(xbin,ybin);
110 }
111
112 Int_t AliL3Histogram::FindXbin(Double_t x)
113 {
114   if(x < fXmin || x > fXmax)
115     return 0;
116   
117   return 1 + (Int_t)(fNxbins*(x-fXmin)/(fXmax-fXmin));
118 }
119
120 Int_t AliL3Histogram::FindYbin(Double_t y)
121 {
122   if(y < fYmin || y > fYmax)
123     return 0;
124   
125   return 1 + (Int_t)(fNybins*(y-fYmin)/(fYmax-fYmin));
126 }
127
128 Int_t AliL3Histogram::GetBin(Int_t xbin,Int_t ybin)
129 {
130   if(xbin < fFirstXbin || xbin > fLastXbin)
131     return 0;
132   if(ybin < fFirstYbin || ybin > fLastYbin)
133     return 0;
134     
135   return xbin + ybin*(fNxbins+2);
136 }
137
138 Int_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
170   if(bin == 0)
171     return;
172   fContent[bin]=value;
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 void AliL3Histogram::AddBinContent(Int_t bin,Int_t weight)
184 {
185   if(bin < 0 || bin > fNcells)
186     {
187       LOG(AliL3Log::kError,"AliL3Histogram::AddBinContent","array")<<AliL3Log::kDec<<
188         "bin-value out of range "<<bin<<ENDLOG;
189       return;
190     }
191   if(bin == 0)
192     return;
193   fEntries++;
194   fContent[bin] += weight;
195 }
196
197 void AliL3Histogram::Add(AliL3Histogram *h1,Double_t weight)
198 {
199   //Adding two histograms. Should be identical.
200   
201   if(!h1)
202     {
203       LOG(AliL3Log::kError,"AliL3Histogram::Add","Pointer")<<
204         "Attempting to add a non-existing histogram"<<ENDLOG;
205       return;
206     }
207   
208   if(h1->GetNbinsX()!=fNxbins || h1->GetNbinsY()!=fNybins)
209     {
210       LOG(AliL3Log::kError,"AliL3Histogram::Add","array")<<
211         "Mismatch in the number of bins "<<ENDLOG;
212       return;
213     }
214   if(h1->GetFirstXbin()!=fFirstXbin || h1->GetLastXbin()!=fLastXbin ||
215      h1->GetFirstYbin()!=fFirstYbin || h1->GetLastYbin()!=fLastYbin)
216     {
217       LOG(AliL3Log::kError,"AliL3Histogram::Add","array")<<
218         "Mismatch in the bin numbering "<<ENDLOG;
219       return;
220     }
221   
222   for(Int_t bin=0; bin<fNcells; bin++)
223     fContent[bin] += h1->GetBinContent(bin);
224   
225   fEntries += h1->GetNEntries();
226 }
227
228 Double_t AliL3Histogram::GetBinCenterX(Int_t xbin)
229 {
230   if(xbin < fFirstXbin || xbin > fLastXbin)
231     {
232       LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterX","xbin")
233         <<"Bin-value out of range "<<xbin<<ENDLOG;
234       return -1;
235     }
236   //  return fXmin + (xbin-1) * fBinwidthX + 0.5*fBinwidthX;
237   return fXmin + (xbin-0.5) * fBinwidthX;
238 }
239
240 Double_t AliL3Histogram::GetBinCenterY(Int_t ybin)
241 {
242   if(ybin < fFirstYbin || ybin > fLastYbin)
243     {
244       LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterY","ybin")
245         <<"Bin-value out of range "<<ybin<<ENDLOG;
246       return -1;
247     }
248   //  return fYmin + (ybin-1) * fBinwidthY + 0.5*fBinwidthY;
249   return fYmin + (ybin-0.5) * fBinwidthY;
250 }
251
252 void AliL3Histogram::Draw(Char_t *option)
253 {
254 #ifdef use_root
255   if(!fRootHisto)
256     CreateRootHisto();
257   
258   for(Int_t xbin=GetFirstXbin(); xbin<=GetLastXbin(); xbin++)
259     {
260       for(Int_t ybin=GetFirstYbin(); ybin<=GetLastYbin(); ybin++)
261         {
262           Int_t bin = GetBin(xbin,ybin);
263           fRootHisto->Fill(GetBinCenterX(xbin),GetBinCenterY(ybin),GetBinContent(bin));
264         }
265     }
266   
267   //fRootHisto->SetStats(kFALSE);
268   fRootHisto->Draw(option);
269   return;
270 #endif
271   cerr<<"AliL3Histogram::Draw : You need to compile with ROOT in order to draw histogram"<<endl;
272   
273 }
274
275 void AliL3Histogram::CreateRootHisto()
276 {
277 #ifdef use_root
278   fRootHisto = new TH2F(fName,"",fNxbins,fXmin,fXmax,fNybins,fYmin,fYmax);
279   return;
280 #endif
281   cerr<<"AliL3Histogram::CreateRootHisto : You need to compile with ROOT in order to create ROOT histogram"<<endl;
282 }