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