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