1 // @(#) $Id: AliHLTHistogram.cxx,v 1.1 2006/11/30 17:45:42 hristov Exp
2 // origin: hough/AliL3Histogram.cxx,v 1.31 Thu Jun 23 17:46:54 2005 UTC by hristov
4 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
5 //*-- Copyright © ALICE HLT Group
7 #include "AliHLTStdIncludes.h"
9 #include "AliHLTTPCLogging.h"
10 #include "AliHLTTPCHistogram.h"
16 /** \class AliHLTTPCHistogram
18 //_____________________________________________________________
26 //uncomment if you want overflow checks
29 ClassImp(AliHLTTPCHistogram)
31 AliHLTTPCHistogram::AliHLTTPCHistogram()
33 // Default constructor
53 AliHLTTPCHistogram::AliHLTTPCHistogram(Char_t *name,Char_t */*id*/,
54 Int_t nxbin,Double_t xmin,Double_t xmax,
55 Int_t nybin,Double_t ymin,Double_t ymax)
62 fNcells = (nxbin+2)*(nybin+2);
67 fBinwidthX = (fXmax - fXmin) / fNxbins;
68 fBinwidthY = (fYmax - fYmin) / fNybins;
78 fContent = new Int_t[fNcells];
82 AliHLTTPCHistogram::~AliHLTTPCHistogram()
91 void AliHLTTPCHistogram::Reset()
93 // Reset histogram contents
95 for(Int_t i=0; i<fNcells; i++) fContent[i] = 0;
100 void AliHLTTPCHistogram::Fill(Double_t x,Double_t y,Int_t weight)
102 // Fill the weight into a bin which correspond to x and y
103 Int_t bin = FindBin(x,y);
109 AddBinContent(bin,weight);
112 void AliHLTTPCHistogram::Fill(Double_t x,Int_t ybin,Int_t weight)
114 // Fill the weight into a bin which correspond to x and ybin
115 Int_t xbin = FindXbin(x);
116 Int_t bin = GetBin(xbin,ybin);
122 AddBinContent(bin,weight);
125 void AliHLTTPCHistogram::Fill(Int_t xbin,Double_t y,Int_t weight)
127 // Fill the weight into a bin which correspond to xbin and y
128 Int_t ybin = FindYbin(y);
129 Int_t bin = GetBin(xbin,ybin);
135 AddBinContent(bin,weight);
138 void AliHLTTPCHistogram::Fill(Int_t xbin,Int_t ybin,Int_t weight)
140 // Fill the weight into a bin which correspond to xbin and ybin
141 Int_t bin = GetBin(xbin,ybin);
147 AddBinContent(bin,weight);
150 Int_t AliHLTTPCHistogram::FindBin(Double_t x,Double_t y) const
152 // Finds the bin which correspond to x and y
153 Int_t xbin = FindXbin(x);
154 Int_t ybin = FindYbin(y);
160 return GetBin(xbin,ybin);
163 Int_t AliHLTTPCHistogram::FindLabelBin(Double_t x,Double_t y) const
165 // Returns the corresponding bin with the mc labels
166 Int_t xbin = FindXbin(x);
167 Int_t ybin = FindYbin(y);
173 return GetLabelBin(xbin,ybin);
176 Int_t AliHLTTPCHistogram::FindXbin(Double_t x) const
178 // Finds the bin which correspond to x
179 if(x < fXmin || x > fXmax)
182 return 1 + (Int_t)(fNxbins*(x-fXmin)/(fXmax-fXmin));
185 Int_t AliHLTTPCHistogram::FindYbin(Double_t y) const
187 // Finds the bin which correspond to y
188 if(y < fYmin || y > fYmax)
191 return 1 + (Int_t)(fNybins*(y-fYmin)/(fYmax-fYmin));
194 Int_t AliHLTTPCHistogram::GetBin(Int_t xbin,Int_t ybin) const
196 // Returns the bin which correspond to xbin and ybin
197 if(xbin < fFirstXbin || xbin > fLastXbin)
199 if(ybin < fFirstYbin || ybin > fLastYbin)
202 return xbin + ybin*(fNxbins+2);
205 Int_t AliHLTTPCHistogram::GetLabelBin(Int_t xbin,Int_t ybin) const
207 // Returns the corresponding bin with the mc labels
208 if(xbin < fFirstXbin || xbin > fLastXbin)
210 if(ybin < fFirstYbin || ybin > fLastYbin)
213 return (Int_t)(xbin/2) + ((Int_t)(ybin/2))*((Int_t)((fNxbins+3)/2));
216 Int_t AliHLTTPCHistogram::GetBinContent(Int_t bin) const
218 // Return the bin content
221 LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::GetBinContent","array")<<AliHLTTPCLog::kDec<<
222 "bin out of range "<<bin<<ENDLOG;
226 if(fContent[bin] < fThreshold)
228 return fContent[bin];
231 void AliHLTTPCHistogram::SetBinContent(Int_t xbin,Int_t ybin,Int_t value)
234 Int_t bin = GetBin(xbin,ybin);
240 SetBinContent(bin,value);
243 void AliHLTTPCHistogram::SetBinContent(Int_t bin,Int_t value)
249 LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::SetBinContent","array")<<AliHLTTPCLog::kDec<<
250 "bin out of range "<<bin<<ENDLOG;
259 void AliHLTTPCHistogram::AddBinContent(Int_t xbin,Int_t ybin,Int_t weight)
261 // Adds weight to bin content
262 Int_t bin = GetBin(xbin,ybin);
268 AddBinContent(bin,weight);
271 void AliHLTTPCHistogram::AddBinContent(Int_t bin,Int_t weight)
273 // Adds weight to bin content
274 if(bin < 0 || bin > fNcells)
276 LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::AddBinContent","array")<<AliHLTTPCLog::kDec<<
277 "bin-value out of range "<<bin<<ENDLOG;
283 fContent[bin] += weight;
286 void AliHLTTPCHistogram::Add(AliHLTTPCHistogram *h1,Double_t /*weight*/)
288 //Adding two histograms. Should be identical.
292 LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::Add","Pointer")<<
293 "Attempting to add a non-existing histogram"<<ENDLOG;
297 if(h1->GetNbinsX()!=fNxbins || h1->GetNbinsY()!=fNybins)
299 LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::Add","array")<<
300 "Mismatch in the number of bins "<<ENDLOG;
304 if(h1->GetFirstXbin()!=fFirstXbin || h1->GetLastXbin()!=fLastXbin ||
305 h1->GetFirstYbin()!=fFirstYbin || h1->GetLastYbin()!=fLastYbin)
307 LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::Add","array")<<
308 "Mismatch in the bin numbering "<<ENDLOG;
312 for(Int_t bin=0; bin<fNcells; bin++)
313 fContent[bin] += h1->GetBinContent(bin);
315 fEntries += h1->GetNEntries();
318 Double_t AliHLTTPCHistogram::GetBinCenterX(Int_t xbin) const
320 // Returns the position of the center of a bin
321 if(xbin < fFirstXbin || xbin > fLastXbin)
323 LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::GetBinCenterX","xbin")
324 <<"Bin-value out of range "<<xbin<<ENDLOG;
328 return fXmin + (xbin-0.5) * fBinwidthX;
331 Double_t AliHLTTPCHistogram::GetBinCenterY(Int_t ybin) const
333 // Returns the position of the center of a bin
334 if(ybin < fFirstYbin || ybin > fLastYbin)
336 LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::GetBinCenterY","ybin")
337 <<"Bin-value out of range "<<ybin<<ENDLOG;
341 return fYmin + (ybin-0.5) * fBinwidthY;
344 Double_t AliHLTTPCHistogram::GetPreciseBinCenterX(Float_t xbin) const
346 // Returns the position of the center of a bin using precise values inside the bin
347 if(xbin < (fFirstXbin-1.5) || xbin > (fLastXbin+1.5))
349 LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::GetBinCenterX","xbin")
350 <<"Bin-value out of range "<<xbin<<ENDLOG;
353 // return fXmin + (xbin-1) * fBinwidthX + 0.5*fBinwidthX;
354 return fXmin + (xbin-0.5) * fBinwidthX;
357 Double_t AliHLTTPCHistogram::GetPreciseBinCenterY(Float_t ybin) const
359 // Returns the position of the center of a bin using precise values inside the bin
360 if(ybin < (fFirstYbin-1.5) || ybin > (fLastYbin+1.5))
362 LOG(AliHLTTPCLog::kError,"AliHLTTPCHistogram::GetBinCenterY","ybin")
363 <<"Bin-value out of range "<<ybin<<ENDLOG;
366 // return fYmin + (ybin-1) * fBinwidthY + 0.5*fBinwidthY;
367 return fYmin + (ybin-0.5) * fBinwidthY;
370 void AliHLTTPCHistogram::Draw(Char_t *option)
372 // Fill the contents of the corresponding ROOT histogram and draws it
376 for(Int_t xbin=GetFirstXbin(); xbin<=GetLastXbin(); xbin++)
378 for(Int_t ybin=GetFirstYbin(); ybin<=GetLastYbin(); ybin++)
380 Int_t bin = GetBin(xbin,ybin);
381 fRootHisto->Fill(GetBinCenterX(xbin),GetBinCenterY(ybin),GetBinContent(bin));
385 //fRootHisto->SetStats(kFALSE);
386 fRootHisto->Draw(option);
390 void AliHLTTPCHistogram::CreateRootHisto()
392 // Create ROOT histogram out of AliHLTTPCHistogram
394 fRootHisto = new TH2F(fName,"",fNxbins,fXmin,fXmax,fNybins,fYmin,fYmax);
397 cerr<<"AliHLTTPCHistogram::CreateRootHisto : You need to compile with ROOT in order to create ROOT histogram"<<endl;
401 ofstream& operator<<(ofstream &o, const AliHLTTPCHistogram &h)
403 for(Int_t xbin=h.GetFirstXbin(); xbin<=h.GetLastXbin(); xbin++)
405 for(Int_t ybin=h.GetFirstYbin(); ybin<=h.GetLastYbin(); ybin++)
407 Int_t bin = h.GetBin(xbin,ybin);
408 o << h.GetBinContent(bin) << " ";