Possible to specify a number of slices, instead of only one
[u/mrichter/AliRoot.git] / HLT / hough / AliL3Histogram.cxx
CommitLineData
18758be6 1//Author: Anders Strand Vestbo
2//Last Modified: 28.6.01
4de874d1 3
18758be6 4#include "AliL3Logging.h"
4de874d1 5#include "AliL3Histogram.h"
6
18758be6 7//2D histogram class.
8
4de874d1 9ClassImp(AliL3Histogram)
10
4de874d1 11AliL3Histogram::AliL3Histogram()
12{
18758be6 13 fNxbins = 0;
14 fNybins = 0;
15 fNcells = 0;
16 fXmin = 0;
17 fYmin = 0;
18 fXmax = 0;
19 fYmax = 0;
4cafa5fc 20 fFirstXbin = 0;
21 fLastXbin = 0;
22 fFirstYbin = 0;
23 fLastYbin = 0;
18758be6 24 fEntries = 0;
25 fContent = 0;
e1842819 26 fThreshold = 0;
4de874d1 27}
28
18758be6 29
e1842819 30AliL3Histogram::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)
18758be6 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;
4cafa5fc 45 fFirstXbin = 1;
46 fFirstYbin = 1;
47 fLastXbin = nxbin;
48 fLastYbin = nybin;
49 fRootHisto = 0;
e1842819 50 fThreshold = 0;
4cafa5fc 51
18758be6 52 fContent = new Double_t[fNcells];
53 Reset();
54}
4de874d1 55
56AliL3Histogram::~AliL3Histogram()
57{
58 //Destructor
18758be6 59 if(fContent)
60 delete [] fContent;
4cafa5fc 61 if(fRootHisto)
62 delete fRootHisto;
18758be6 63}
64
4de874d1 65
18758be6 66void AliL3Histogram::Reset()
67{
68
69 for(Int_t i=0; i<fNcells; i++)
70 fContent[i] = 0;
71 fEntries=0;
72}
73
74void 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
81Int_t AliL3Histogram::FindBin(Double_t x,Double_t y)
4cafa5fc 82{
83
84 Int_t xbin = FindXbin(x);
85 Int_t ybin = FindYbin(y);
7a21af2f 86
4cafa5fc 87 return GetBin(xbin,ybin);
88}
89
90Int_t AliL3Histogram::FindXbin(Double_t x)
18758be6 91{
92 if(x < fXmin || x > fXmax)
7a21af2f 93 return 0;
4cafa5fc 94
95 return 1 + (Int_t)(fNxbins*(x-fXmin)/(fXmax-fXmin));
96
97}
98
99Int_t AliL3Histogram::FindYbin(Double_t y)
100{
18758be6 101 if(y < fYmin || y > fYmax)
7a21af2f 102 return 0;
18758be6 103
4cafa5fc 104 return 1 + (Int_t)(fNybins*(y-fYmin)/(fYmax-fYmin));
18758be6 105
4cafa5fc 106}
107
108Int_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
18758be6 123 return xbin + ybin*(fNxbins+2);
4cafa5fc 124}
125
126Double_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
e1842819 135 if(fContent[bin] < fThreshold)
136 return 0;
4cafa5fc 137 return fContent[bin];
138}
139
7a21af2f 140void AliL3Histogram::SetBinContent(Int_t xbin,Int_t ybin,Int_t value)
4cafa5fc 141{
142 Int_t bin = GetBin(xbin,ybin);
7a21af2f 143 if(bin == 0)
144 return;
145 SetBinContent(bin,value);
4cafa5fc 146}
147
7a21af2f 148void AliL3Histogram::SetBinContent(Int_t bin,Int_t value)
4cafa5fc 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 }
7a21af2f 157 if(bin == 0)
158 return;
159 fContent[bin]=value;
18758be6 160
161}
162
163void AliL3Histogram::AddBinContent(Int_t xbin,Int_t ybin,Int_t weight)
164{
4cafa5fc 165 Int_t bin = GetBin(xbin,ybin);
7a21af2f 166 if(bin == 0)
167 return;
18758be6 168 AddBinContent(bin,weight);
169
170}
171
172void 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 }
7a21af2f 180 if(bin == 0)
181 return;
18758be6 182 fEntries++;
183 fContent[bin] += weight;
184}
185
ad11f553 186void 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
4cafa5fc 216Double_t AliL3Histogram::GetBinCenterX(Int_t xbin)
18758be6 217{
218
219 Double_t binwidth = (fXmax - fXmin) / fNxbins;
220 return fXmin + (xbin-1) * binwidth + 0.5*binwidth;
221
222}
223
4cafa5fc 224Double_t AliL3Histogram::GetBinCenterY(Int_t ybin)
18758be6 225{
226
227 Double_t binwidth = (fYmax - fYmin) / fNybins;
228 return fYmin + (ybin-1) * binwidth + 0.5*binwidth;
229
230}
231
232
4cafa5fc 233void AliL3Histogram::Draw(Char_t *option)
18758be6 234{
4cafa5fc 235 fRootHisto = new TH2F(fName,"",fNxbins,fXmin,fXmax,fNybins,fYmin,fYmax);
18758be6 236 for(Int_t bin=0; bin<fNcells; bin++)
237 {
e1842819 238 fRootHisto->AddBinContent(bin,GetBinContent(bin));
18758be6 239 }
18758be6 240
4cafa5fc 241 fRootHisto->Draw(option);
242
4de874d1 243}