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