3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright © ALICE HLT Group
6 #include "AliL3StandardIncludes.h"
8 #include "AliL3Logging.h"
9 #include "AliL3Histogram.h"
15 /** \class AliL3Histogram
17 //_____________________________________________________________
25 //uncomment if you want overflow checks
28 ClassImp(AliL3Histogram)
30 AliL3Histogram::AliL3Histogram()
32 // Default constructor
54 AliL3Histogram::AliL3Histogram(Char_t *name,Char_t */*id*/,
55 Int_t nxbin,Double_t xmin,Double_t xmax,
56 Int_t nybin,Double_t ymin,Double_t ymax)
63 fNcells = (nxbin+2)*(nybin+2);
68 fBinwidthX = (fXmax - fXmin) / fNxbins;
69 fBinwidthY = (fYmax - fYmin) / fNybins;
81 fContent = new Int_t[fNcells];
85 AliL3Histogram::~AliL3Histogram()
96 void AliL3Histogram::Reset()
98 // Reset histogram contents
100 for(Int_t i=0; i<fNcells; i++) fContent[i] = 0;
105 void AliL3Histogram::Fill(Double_t x,Double_t y,Int_t weight)
107 // Fill the weight into a bin which correspond to x and y
108 Int_t bin = FindBin(x,y);
114 AddBinContent(bin,weight);
117 void AliL3Histogram::Fill(Double_t x,Int_t ybin,Int_t weight)
119 // Fill the weight into a bin which correspond to x and ybin
120 Int_t xbin = FindXbin(x);
121 Int_t bin = GetBin(xbin,ybin);
127 AddBinContent(bin,weight);
130 void AliL3Histogram::Fill(Int_t xbin,Double_t y,Int_t weight)
132 // Fill the weight into a bin which correspond to xbin and y
133 Int_t ybin = FindYbin(y);
134 Int_t bin = GetBin(xbin,ybin);
140 AddBinContent(bin,weight);
143 void AliL3Histogram::Fill(Int_t xbin,Int_t ybin,Int_t weight)
145 // Fill the weight into a bin which correspond to xbin and ybin
146 Int_t bin = GetBin(xbin,ybin);
152 AddBinContent(bin,weight);
155 Int_t AliL3Histogram::FindBin(Double_t x,Double_t y) const
157 // Finds the bin which correspond to x and y
158 Int_t xbin = FindXbin(x);
159 Int_t ybin = FindYbin(y);
165 return GetBin(xbin,ybin);
168 Int_t AliL3Histogram::FindLabelBin(Double_t x,Double_t y) const
170 // Returns the corresponding bin with the mc labels
171 Int_t xbin = FindXbin(x);
172 Int_t ybin = FindYbin(y);
178 return GetLabelBin(xbin,ybin);
181 Int_t AliL3Histogram::FindXbin(Double_t x) const
183 // Finds the bin which correspond to x
184 if(x < fXmin || x > fXmax)
187 return 1 + (Int_t)(fNxbins*(x-fXmin)/(fXmax-fXmin));
190 Int_t AliL3Histogram::FindYbin(Double_t y) const
192 // Finds the bin which correspond to y
193 if(y < fYmin || y > fYmax)
196 return 1 + (Int_t)(fNybins*(y-fYmin)/(fYmax-fYmin));
199 Int_t AliL3Histogram::GetBin(Int_t xbin,Int_t ybin) const
201 // Returns the bin which correspond to xbin and ybin
202 if(xbin < fFirstXbin || xbin > fLastXbin)
204 if(ybin < fFirstYbin || ybin > fLastYbin)
207 return xbin + ybin*(fNxbins+2);
210 Int_t AliL3Histogram::GetLabelBin(Int_t xbin,Int_t ybin) const
212 // Returns the corresponding bin with the mc labels
213 if(xbin < fFirstXbin || xbin > fLastXbin)
215 if(ybin < fFirstYbin || ybin > fLastYbin)
218 return (Int_t)(xbin/2) + ((Int_t)(ybin/2))*((Int_t)((fNxbins+3)/2));
221 Int_t AliL3Histogram::GetBinContent(Int_t bin) const
223 // Return the bin content
226 LOG(AliL3Log::kError,"AliL3Histogram::GetBinContent","array")<<AliL3Log::kDec<<
227 "bin out of range "<<bin<<ENDLOG;
231 if(fContent[bin] < fThreshold)
233 return fContent[bin];
236 void AliL3Histogram::SetBinContent(Int_t xbin,Int_t ybin,Int_t value)
239 Int_t bin = GetBin(xbin,ybin);
245 SetBinContent(bin,value);
248 void AliL3Histogram::SetBinContent(Int_t bin,Int_t value)
254 LOG(AliL3Log::kError,"AliL3Histogram::SetBinContent","array")<<AliL3Log::kDec<<
255 "bin out of range "<<bin<<ENDLOG;
264 void AliL3Histogram::AddBinContent(Int_t xbin,Int_t ybin,Int_t weight)
266 // Adds weight to bin content
267 Int_t bin = GetBin(xbin,ybin);
273 AddBinContent(bin,weight);
276 void AliL3Histogram::AddBinContent(Int_t bin,Int_t weight)
278 // Adds weight to bin content
279 if(bin < 0 || bin > fNcells)
281 LOG(AliL3Log::kError,"AliL3Histogram::AddBinContent","array")<<AliL3Log::kDec<<
282 "bin-value out of range "<<bin<<ENDLOG;
288 fContent[bin] += weight;
291 void AliL3Histogram::Add(AliL3Histogram *h1,Double_t /*weight*/)
293 //Adding two histograms. Should be identical.
297 LOG(AliL3Log::kError,"AliL3Histogram::Add","Pointer")<<
298 "Attempting to add a non-existing histogram"<<ENDLOG;
302 if(h1->GetNbinsX()!=fNxbins || h1->GetNbinsY()!=fNybins)
304 LOG(AliL3Log::kError,"AliL3Histogram::Add","array")<<
305 "Mismatch in the number of bins "<<ENDLOG;
309 if(h1->GetFirstXbin()!=fFirstXbin || h1->GetLastXbin()!=fLastXbin ||
310 h1->GetFirstYbin()!=fFirstYbin || h1->GetLastYbin()!=fLastYbin)
312 LOG(AliL3Log::kError,"AliL3Histogram::Add","array")<<
313 "Mismatch in the bin numbering "<<ENDLOG;
317 for(Int_t bin=0; bin<fNcells; bin++)
318 fContent[bin] += h1->GetBinContent(bin);
320 fEntries += h1->GetNEntries();
323 Double_t AliL3Histogram::GetBinCenterX(Int_t xbin) const
325 // Returns the position of the center of a bin
326 if(xbin < fFirstXbin || xbin > fLastXbin)
328 LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterX","xbin")
329 <<"Bin-value out of range "<<xbin<<ENDLOG;
333 return fXmin + (xbin-0.5) * fBinwidthX;
336 Double_t AliL3Histogram::GetBinCenterY(Int_t ybin) const
338 // Returns the position of the center of a bin
339 if(ybin < fFirstYbin || ybin > fLastYbin)
341 LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterY","ybin")
342 <<"Bin-value out of range "<<ybin<<ENDLOG;
346 return fYmin + (ybin-0.5) * fBinwidthY;
349 Double_t AliL3Histogram::GetPreciseBinCenterX(Float_t xbin) const
351 // Returns the position of the center of a bin using precise values inside the bin
352 if(xbin < (fFirstXbin-1.5) || xbin > (fLastXbin+1.5))
354 LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterX","xbin")
355 <<"Bin-value out of range "<<xbin<<ENDLOG;
358 // return fXmin + (xbin-1) * fBinwidthX + 0.5*fBinwidthX;
359 return fXmin + (xbin-0.5) * fBinwidthX;
362 Double_t AliL3Histogram::GetPreciseBinCenterY(Float_t ybin) const
364 // Returns the position of the center of a bin using precise values inside the bin
365 if(ybin < (fFirstYbin-1.5) || ybin > (fLastYbin+1.5))
367 LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterY","ybin")
368 <<"Bin-value out of range "<<ybin<<ENDLOG;
371 // return fYmin + (ybin-1) * fBinwidthY + 0.5*fBinwidthY;
372 return fYmin + (ybin-0.5) * fBinwidthY;
375 void AliL3Histogram::Draw(Char_t *option)
377 // Fill the contents of the corresponding ROOT histogram and draws it
382 for(Int_t xbin=GetFirstXbin(); xbin<=GetLastXbin(); xbin++)
384 for(Int_t ybin=GetFirstYbin(); ybin<=GetLastYbin(); ybin++)
386 Int_t bin = GetBin(xbin,ybin);
387 fRootHisto->Fill(GetBinCenterX(xbin),GetBinCenterY(ybin),GetBinContent(bin));
391 //fRootHisto->SetStats(kFALSE);
392 fRootHisto->Draw(option);
395 cerr<<"AliL3Histogram::Draw : You need to compile with ROOT in order to draw histogram"<<endl;
399 void AliL3Histogram::CreateRootHisto()
401 // Create ROOT histogram out of AliL3Histogram
403 fRootHisto = new TH2F(fName,"",fNxbins,fXmin,fXmax,fNybins,fYmin,fYmax);
406 cerr<<"AliL3Histogram::CreateRootHisto : You need to compile with ROOT in order to create ROOT histogram"<<endl;
410 ofstream& operator<<(ofstream &o, const AliL3Histogram &h)
412 for(Int_t xbin=h.GetFirstXbin(); xbin<=h.GetLastXbin(); xbin++)
414 for(Int_t ybin=h.GetFirstYbin(); ybin<=h.GetLastYbin(); ybin++)
416 Int_t bin = h.GetBin(xbin,ybin);
417 o << h.GetBinContent(bin) << " ";