]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking/AliHLTTPCHistogram.cxx
make hough tracking code compiling
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking / AliHLTTPCHistogram.cxx
1 // @(#) $Id: AliHLTHistogram.cxx,v 1.1 2006/11/30 17:45:42 hristov Exp 
2 // origin: hough/AliL3Histogram.cxx,v 1.31 Thu Jun 23 17:46:54 2005 UTC by hristov
3
4 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
5 //*-- Copyright &copy ALICE HLT Group
6
7 #include "AliHLTStdIncludes.h"
8
9 #include "AliHLTTPCLogging.h"
10 #include "AliHLTTPCHistogram.h"
11
12 #if __GNUC__ >= 3
13 using namespace std;
14 #endif
15
16 /** \class AliHLTTPCHistogram
17 <pre>
18 //_____________________________________________________________
19 // AliHLTTPCHistogram
20 //
21 // 2D histogram class
22 //
23 </pre>
24 */
25
26 //uncomment if you want overflow checks
27 //#define _IFON_
28
29 ClassImp(AliHLTTPCHistogram)
30
31 AliHLTTPCHistogram::AliHLTTPCHistogram()
32 {
33   // Default constructor
34   fNxbins = 0;
35   fNybins = 0;
36   fNcells = 0;
37   fXmin = 0;
38   fYmin = 0;
39   fXmax = 0;
40   fYmax = 0;
41   fBinwidthX = 0;
42   fBinwidthY = 0;  
43   fFirstXbin = 0;
44   fLastXbin = 0;
45   fFirstYbin = 0;
46   fLastYbin = 0;
47   fEntries = 0;
48   fContent = 0;
49   fThreshold = 0;
50   fRootHisto = 0;
51 }
52
53 AliHLTTPCHistogram::AliHLTTPCHistogram(Char_t *name,Char_t */*id*/,
54                                Int_t nxbin,Double_t xmin,Double_t xmax,
55                                Int_t nybin,Double_t ymin,Double_t ymax) 
56 {
57   // Normal constructor
58   strcpy(fName,name);
59
60   fNxbins = nxbin;
61   fNybins = nybin;
62   fNcells = (nxbin+2)*(nybin+2);
63   fXmin = xmin;
64   fYmin = ymin;
65   fXmax = xmax;
66   fYmax = ymax;
67   fBinwidthX = (fXmax - fXmin) / fNxbins;
68   fBinwidthY = (fYmax - fYmin) / fNybins;
69   
70   fEntries = 0;
71   fFirstXbin = 1;
72   fFirstYbin = 1;
73   fLastXbin = nxbin;
74   fLastYbin = nybin;
75   fRootHisto = 0;
76   fThreshold = 0;
77
78   fContent = new Int_t[fNcells];
79   Reset();
80 }
81
82 AliHLTTPCHistogram::~AliHLTTPCHistogram()
83 {
84   //Destructor
85   if(fContent)
86     delete [] fContent;
87   if(fRootHisto)
88     delete fRootHisto;
89 }
90
91 void AliHLTTPCHistogram::Reset()
92 {
93   // Reset histogram contents
94   if(fContent)
95     for(Int_t i=0; i<fNcells; i++) fContent[i] = 0;
96
97   fEntries=0;
98 }
99
100 void AliHLTTPCHistogram::Fill(Double_t x,Double_t y,Int_t weight)
101 {
102   // Fill the weight into a bin which correspond to x and y
103   Int_t bin = FindBin(x,y);
104 #ifdef _IFON_
105   if(bin < 0)
106     return;
107 #endif
108   
109   AddBinContent(bin,weight);
110 }
111
112 void AliHLTTPCHistogram::Fill(Double_t x,Int_t ybin,Int_t weight)
113 {
114   // Fill the weight into a bin which correspond to x and ybin
115   Int_t xbin = FindXbin(x);
116   Int_t bin = GetBin(xbin,ybin);
117 #ifdef _IFON_
118   if(bin < 0)
119     return;
120 #endif
121   
122   AddBinContent(bin,weight);
123 }
124
125 void AliHLTTPCHistogram::Fill(Int_t xbin,Double_t y,Int_t weight)
126 {
127   // Fill the weight into a bin which correspond to xbin and y
128   Int_t ybin = FindYbin(y);
129   Int_t bin = GetBin(xbin,ybin);
130 #ifdef _IFON_
131   if(bin < 0)
132     return;
133 #endif
134   
135   AddBinContent(bin,weight);
136 }
137
138 void AliHLTTPCHistogram::Fill(Int_t xbin,Int_t ybin,Int_t weight)
139 {
140   // Fill the weight into a bin which correspond to xbin and ybin
141   Int_t bin = GetBin(xbin,ybin);
142 #ifdef _IFON_
143   if(bin < 0)
144     return;
145 #endif
146   
147   AddBinContent(bin,weight);
148 }
149
150 Int_t AliHLTTPCHistogram::FindBin(Double_t x,Double_t y) const
151 {
152   // Finds the bin which correspond to x and y
153   Int_t xbin = FindXbin(x);
154   Int_t ybin = FindYbin(y);
155 #ifdef _IFON_
156   if(!xbin || !ybin)
157     return -1;
158 #endif
159   
160   return GetBin(xbin,ybin);
161 }
162
163 Int_t AliHLTTPCHistogram::FindLabelBin(Double_t x,Double_t y) const
164 {
165   // Returns the corresponding bin with the mc labels
166   Int_t xbin = FindXbin(x);
167   Int_t ybin = FindYbin(y);
168 #ifdef _IFON_
169   if(!xbin || !ybin)
170     return -1;
171 #endif
172   
173   return GetLabelBin(xbin,ybin);
174 }
175
176 Int_t AliHLTTPCHistogram::FindXbin(Double_t x) const
177 {
178   // Finds the bin which correspond to x
179   if(x < fXmin || x > fXmax)
180     return 0;
181   
182   return 1 + (Int_t)(fNxbins*(x-fXmin)/(fXmax-fXmin));
183 }
184
185 Int_t AliHLTTPCHistogram::FindYbin(Double_t y) const
186 {
187   // Finds the bin which correspond to y
188   if(y < fYmin || y > fYmax)
189     return 0;
190   
191   return 1 + (Int_t)(fNybins*(y-fYmin)/(fYmax-fYmin));
192 }
193
194 Int_t AliHLTTPCHistogram::GetBin(Int_t xbin,Int_t ybin) const
195 {
196   // Returns the bin which correspond to xbin and ybin
197   if(xbin < fFirstXbin || xbin > fLastXbin)
198     return 0;
199   if(ybin < fFirstYbin || ybin > fLastYbin)
200     return 0;
201     
202   return xbin + ybin*(fNxbins+2);
203 }
204
205 Int_t AliHLTTPCHistogram::GetLabelBin(Int_t xbin,Int_t ybin) const
206 {
207   // Returns the corresponding bin with the mc labels
208   if(xbin < fFirstXbin || xbin > fLastXbin)
209     return -1;
210   if(ybin < fFirstYbin || ybin > fLastYbin)
211     return -1;
212     
213   return (Int_t)(xbin/2) + ((Int_t)(ybin/2))*((Int_t)((fNxbins+3)/2));
214 }
215
216 Int_t AliHLTTPCHistogram::GetBinContent(Int_t bin) const
217 {
218   // Return the bin content
219   if(bin >= fNcells)
220     {
221       LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::GetBinContent","array")<<AliHLTTPCLog::kDec<<
222         "bin out of range "<<bin<<ENDLOG;
223       return 0;
224     }
225   
226   if(fContent[bin] < fThreshold)
227     return 0;
228   return fContent[bin];
229 }
230
231 void AliHLTTPCHistogram::SetBinContent(Int_t xbin,Int_t ybin,Int_t value)
232 {
233   // Set bin content
234   Int_t bin = GetBin(xbin,ybin);
235 #ifdef _IFON_
236   if(bin == 0) 
237     return;
238 #endif
239
240   SetBinContent(bin,value);
241 }
242
243 void AliHLTTPCHistogram::SetBinContent(Int_t bin,Int_t value)
244 {
245   // Set bin content
246
247   if(bin >= fNcells)
248     {
249       LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::SetBinContent","array")<<AliHLTTPCLog::kDec<<
250         "bin out of range "<<bin<<ENDLOG;
251       return;
252     }
253
254   if(bin == 0)
255     return;
256   fContent[bin]=value;
257 }
258
259 void AliHLTTPCHistogram::AddBinContent(Int_t xbin,Int_t ybin,Int_t weight)
260 {
261   // Adds weight to bin content
262   Int_t bin = GetBin(xbin,ybin);
263 #ifdef _IFON_
264   if(bin == 0)
265     return;
266 #endif
267
268   AddBinContent(bin,weight);
269 }
270
271 void AliHLTTPCHistogram::AddBinContent(Int_t bin,Int_t weight)
272 {
273   // Adds weight to bin content
274   if(bin < 0 || bin > fNcells)
275     {
276       LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::AddBinContent","array")<<AliHLTTPCLog::kDec<<
277         "bin-value out of range "<<bin<<ENDLOG;
278       return;
279     }
280   if(bin == 0)
281     return;
282   fEntries++;
283   fContent[bin] += weight;
284 }
285
286 void AliHLTTPCHistogram::Add(AliHLTTPCHistogram *h1,Double_t /*weight*/)
287 {
288   //Adding two histograms. Should be identical.
289   
290   if(!h1)
291     {
292       LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::Add","Pointer")<<
293         "Attempting to add a non-existing histogram"<<ENDLOG;
294       return;
295     }
296   
297   if(h1->GetNbinsX()!=fNxbins || h1->GetNbinsY()!=fNybins)
298     {
299       LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::Add","array")<<
300         "Mismatch in the number of bins "<<ENDLOG;
301       return;
302     }
303
304   if(h1->GetFirstXbin()!=fFirstXbin || h1->GetLastXbin()!=fLastXbin ||
305      h1->GetFirstYbin()!=fFirstYbin || h1->GetLastYbin()!=fLastYbin)
306     {
307       LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::Add","array")<<
308         "Mismatch in the bin numbering "<<ENDLOG;
309       return;
310     }
311   
312   for(Int_t bin=0; bin<fNcells; bin++)
313     fContent[bin] += h1->GetBinContent(bin);
314   
315   fEntries += h1->GetNEntries();
316 }
317
318 Double_t AliHLTTPCHistogram::GetBinCenterX(Int_t xbin) const
319 {
320   // Returns the position of the center of a bin
321   if(xbin < fFirstXbin || xbin > fLastXbin)
322     {
323       LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::GetBinCenterX","xbin")
324         <<"Bin-value out of range "<<xbin<<ENDLOG;
325       return -1;
326     }
327
328   return fXmin + (xbin-0.5) * fBinwidthX;
329 }
330
331 Double_t AliHLTTPCHistogram::GetBinCenterY(Int_t ybin) const
332 {
333   // Returns the position of the center of a bin
334   if(ybin < fFirstYbin || ybin > fLastYbin)
335     {
336       LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::GetBinCenterY","ybin")
337         <<"Bin-value out of range "<<ybin<<ENDLOG;
338       return -1;
339     }
340
341   return fYmin + (ybin-0.5) * fBinwidthY;
342 }
343
344 Double_t AliHLTTPCHistogram::GetPreciseBinCenterX(Float_t xbin) const
345 {
346   // Returns the position of the center of a bin using precise values inside the bin
347   if(xbin < (fFirstXbin-1.5) || xbin > (fLastXbin+1.5))
348     {
349       LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::GetBinCenterX","xbin")
350         <<"Bin-value out of range "<<xbin<<ENDLOG;
351       return -1;
352     }
353   //  return fXmin + (xbin-1) * fBinwidthX + 0.5*fBinwidthX;
354   return fXmin + (xbin-0.5) * fBinwidthX;
355 }
356
357 Double_t AliHLTTPCHistogram::GetPreciseBinCenterY(Float_t ybin) const
358 {
359   // Returns the position of the center of a bin using precise values inside the bin
360   if(ybin < (fFirstYbin-1.5) || ybin > (fLastYbin+1.5))
361     {
362       LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::GetBinCenterY","ybin")
363         <<"Bin-value out of range "<<ybin<<ENDLOG;
364       return -1;
365     }
366   //  return fYmin + (ybin-1) * fBinwidthY + 0.5*fBinwidthY;
367   return fYmin + (ybin-0.5) * fBinwidthY;
368 }
369
370 void AliHLTTPCHistogram::Draw(Char_t *option)
371 {
372   // Fill the contents of the corresponding ROOT histogram and draws it 
373   if(!fRootHisto)
374     CreateRootHisto();
375   
376   for(Int_t xbin=GetFirstXbin(); xbin<=GetLastXbin(); xbin++)
377     {
378       for(Int_t ybin=GetFirstYbin(); ybin<=GetLastYbin(); ybin++)
379         {
380           Int_t bin = GetBin(xbin,ybin);
381           fRootHisto->Fill(GetBinCenterX(xbin),GetBinCenterY(ybin),GetBinContent(bin));
382         }
383     }
384   
385   //fRootHisto->SetStats(kFALSE);
386   fRootHisto->Draw(option);
387   return;
388 }
389
390 void AliHLTTPCHistogram::CreateRootHisto()
391 {
392   // Create ROOT histogram out of AliHLTTPCHistogram
393 #ifdef use_root
394   fRootHisto = new TH2F(fName,"",fNxbins,fXmin,fXmax,fNybins,fYmin,fYmax);
395   return;
396 #else
397   cerr<<"AliHLTTPCHistogram::CreateRootHisto : You need to compile with ROOT in order to create ROOT histogram"<<endl;
398 #endif
399 }
400
401 ofstream& operator<<(ofstream &o, const AliHLTTPCHistogram &h)
402 {
403   for(Int_t xbin=h.GetFirstXbin(); xbin<=h.GetLastXbin(); xbin++)
404     {
405       for(Int_t ybin=h.GetFirstYbin(); ybin<=h.GetLastYbin(); ybin++)
406         {
407           Int_t bin = h.GetBin(xbin,ybin);
408           o << h.GetBinContent(bin) << " ";
409         }
410       o << endl;
411     }
412   return o;
413 }