]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/hough/AliL3Histogram.cxx
New class to make V2 clusters starting from digits or hits (fast simulation). Origin...
[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 "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 < 0 || xbin > GetLastXbin())
126     {
127       LOG(AliL3Log::kError,"AliL3Histogram::GetBin","array")<<AliL3Log::kDec<<
128         "xbin out of range "<<xbin<<ENDLOG;
129       return 0;
130     }
131
132   if(ybin < 0 || ybin > GetLastYbin())
133     {
134       LOG(AliL3Log::kError,"AliL3Histogram::Getbin","array")<<AliL3Log::kDec<<
135         "ybin out of range "<<xbin<<ENDLOG;
136       return 0;
137     }
138     
139   return xbin + ybin*(fNxbins+2);
140 }
141
142 Int_t AliL3Histogram::GetBinContent(Int_t bin)
143 {
144   if(bin >= fNcells)
145     {
146       LOG(AliL3Log::kError,"AliL3Histogram::GetBinContent","array")<<AliL3Log::kDec<<
147         "bin out of range "<<bin<<ENDLOG;
148       return 0;
149     }
150   
151   if(fContent[bin] < fThreshold)
152     return 0;
153   return fContent[bin];
154 }
155
156 void AliL3Histogram::SetBinContent(Int_t xbin,Int_t ybin,Int_t value)
157 {
158   Int_t bin = GetBin(xbin,ybin);
159   if(bin == 0) 
160     return;
161   SetBinContent(bin,value);
162 }
163
164 void AliL3Histogram::SetBinContent(Int_t bin,Int_t value)
165 {
166
167   if(bin >= fNcells)
168     {
169       LOG(AliL3Log::kError,"AliL3Histogram::SetBinContent","array")<<AliL3Log::kDec<<
170         "bin out of range "<<bin<<ENDLOG;
171       return;
172     }
173
174   if(bin == 0)
175     return;
176   fContent[bin]=value;
177 }
178
179 void AliL3Histogram::AddBinContent(Int_t xbin,Int_t ybin,Int_t weight)
180 {
181   Int_t bin = GetBin(xbin,ybin);
182   if(bin == 0)
183     return;
184   AddBinContent(bin,weight);
185 }
186
187 void AliL3Histogram::AddBinContent(Int_t bin,Int_t weight)
188 {
189   if(bin < 0 || bin > fNcells)
190     {
191       LOG(AliL3Log::kError,"AliL3Histogram::AddBinContent","array")<<AliL3Log::kDec<<
192         "bin-value out of range "<<bin<<ENDLOG;
193       return;
194     }
195   if(bin == 0)
196     return;
197   fEntries++;
198   fContent[bin] += weight;
199 }
200
201 void AliL3Histogram::Add(AliL3Histogram *h1,Double_t weight)
202 {
203   //Adding two histograms. Should be identical.
204   
205   if(!h1)
206     {
207       LOG(AliL3Log::kError,"AliL3Histogram::Add","Pointer")<<
208         "Attempting to add a non-existing histogram"<<ENDLOG;
209       return;
210     }
211   
212   if(h1->GetNbinsX()!=fNxbins || h1->GetNbinsY()!=fNybins)
213     {
214       LOG(AliL3Log::kError,"AliL3Histogram::Add","array")<<
215         "Mismatch in the number of bins "<<ENDLOG;
216       return;
217     }
218   if(h1->GetFirstXbin()!=fFirstXbin || h1->GetLastXbin()!=fLastXbin ||
219      h1->GetFirstYbin()!=fFirstYbin || h1->GetLastYbin()!=fLastYbin)
220     {
221       LOG(AliL3Log::kError,"AliL3Histogram::Add","array")<<
222         "Mismatch in the bin numbering "<<ENDLOG;
223       return;
224     }
225   
226   for(Int_t bin=0; bin<fNcells; bin++)
227     fContent[bin] += h1->GetBinContent(bin);
228   
229   fEntries += h1->GetNEntries();
230 }
231
232 Double_t AliL3Histogram::GetBinCenterX(Int_t xbin)
233 {
234   //  return fXmin + (xbin-1) * fBinwidthX + 0.5*fBinwidthX;
235   return fXmin + (xbin-0.5) * fBinwidthX;
236 }
237
238 Double_t AliL3Histogram::GetBinCenterY(Int_t ybin)
239 {
240   //  return fYmin + (ybin-1) * fBinwidthY + 0.5*fBinwidthY;
241   return fYmin + (ybin-0.5) * fBinwidthY;
242 }
243
244 void AliL3Histogram::Draw(Char_t *option)
245 {
246 #ifdef use_root
247   if(!fRootHisto)
248     CreateRootHisto();
249   for(Int_t bin=0; bin<fNcells; bin++)
250     {
251       fRootHisto->AddBinContent(bin,GetBinContent(bin));
252     }
253   
254   fRootHisto->SetStats(kFALSE);
255   fRootHisto->Draw(option);
256   return;
257 #endif
258   cerr<<"AliL3Histogram::Draw : You need to compile with ROOT in order to draw histogram"<<endl;
259   
260 }
261
262 void AliL3Histogram::CreateRootHisto()
263 {
264 #ifdef use_root
265   fRootHisto = new TH2F(fName,"",fNxbins,fXmin,fXmax,fNybins,fYmin,fYmax);
266   return;
267 #endif
268   cerr<<"AliL3Histogram::CreateRootHisto : You need to compile with ROOT in order to create ROOT histogram"<<endl;
269 }