]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/hough/AliHLTHistogram.cxx
o) Performance studies added (mainly in runMultiplicitySelector) that evaluate the...
[u/mrichter/AliRoot.git] / HLT / hough / AliHLTHistogram.cxx
CommitLineData
3e87ef69 1// @(#) $Id$
95a00d93 2
b1886074 3// Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
3e87ef69 4//*-- Copyright &copy ALICE HLT Group
4de874d1 5
4aa41877 6#include "AliHLTStandardIncludes.h"
1f1942b8 7
4aa41877 8#include "AliHLTLogging.h"
9#include "AliHLTHistogram.h"
4de874d1 10
5929c18d 11#if __GNUC__ >= 3
e06900d5 12using namespace std;
13#endif
14
4aa41877 15/** \class AliHLTHistogram
0bd0c1ef 16<pre>
b1886074 17//_____________________________________________________________
4aa41877 18// AliHLTHistogram
b1886074 19//
20// 2D histogram class
0bd0c1ef 21//
22</pre>
23*/
24
25//uncomment if you want overflow checks
26//#define _IFON_
18758be6 27
4aa41877 28ClassImp(AliHLTHistogram)
4de874d1 29
4aa41877 30AliHLTHistogram::AliHLTHistogram()
4de874d1 31{
917e711b 32 // Default constructor
18758be6 33 fNxbins = 0;
34 fNybins = 0;
35 fNcells = 0;
36 fXmin = 0;
37 fYmin = 0;
38 fXmax = 0;
39 fYmax = 0;
e06900d5 40 fBinwidthX = 0;
41 fBinwidthY = 0;
4cafa5fc 42 fFirstXbin = 0;
43 fLastXbin = 0;
44 fFirstYbin = 0;
45 fLastYbin = 0;
18758be6 46 fEntries = 0;
47 fContent = 0;
e1842819 48 fThreshold = 0;
6b9816d6 49#ifdef use_root
50 fRootHisto = 0;
51#endif
4de874d1 52}
53
4aa41877 54AliHLTHistogram::AliHLTHistogram(Char_t *name,Char_t */*id*/,
e1842819 55 Int_t nxbin,Double_t xmin,Double_t xmax,
56 Int_t nybin,Double_t ymin,Double_t ymax)
18758be6 57{
917e711b 58 // Normal constructor
18758be6 59 strcpy(fName,name);
e06900d5 60
18758be6 61 fNxbins = nxbin;
62 fNybins = nybin;
63 fNcells = (nxbin+2)*(nybin+2);
18758be6 64 fXmin = xmin;
65 fYmin = ymin;
66 fXmax = xmax;
67 fYmax = ymax;
e06900d5 68 fBinwidthX = (fXmax - fXmin) / fNxbins;
69 fBinwidthY = (fYmax - fYmin) / fNybins;
70
18758be6 71 fEntries = 0;
4cafa5fc 72 fFirstXbin = 1;
73 fFirstYbin = 1;
74 fLastXbin = nxbin;
75 fLastYbin = nybin;
36d25d02 76#ifdef use_root
4cafa5fc 77 fRootHisto = 0;
36d25d02 78#endif
e1842819 79 fThreshold = 0;
4cafa5fc 80
cfc41e5b 81 fContent = new Int_t[fNcells];
18758be6 82 Reset();
83}
4de874d1 84
4aa41877 85AliHLTHistogram::~AliHLTHistogram()
4de874d1 86{
87 //Destructor
18758be6 88 if(fContent)
89 delete [] fContent;
36d25d02 90#ifdef use_root
4cafa5fc 91 if(fRootHisto)
92 delete fRootHisto;
36d25d02 93#endif
18758be6 94}
95
4aa41877 96void AliHLTHistogram::Reset()
18758be6 97{
917e711b 98 // Reset histogram contents
e06900d5 99 if(fContent)
100 for(Int_t i=0; i<fNcells; i++) fContent[i] = 0;
101
18758be6 102 fEntries=0;
103}
104
4aa41877 105void AliHLTHistogram::Fill(Double_t x,Double_t y,Int_t weight)
18758be6 106{
917e711b 107 // Fill the weight into a bin which correspond to x and y
18758be6 108 Int_t bin = FindBin(x,y);
910f8d43 109#ifdef _IFON_
b2a02bce 110 if(bin < 0)
111 return;
910f8d43 112#endif
113
114 AddBinContent(bin,weight);
115}
116
4aa41877 117void AliHLTHistogram::Fill(Double_t x,Int_t ybin,Int_t weight)
910f8d43 118{
917e711b 119 // Fill the weight into a bin which correspond to x and ybin
910f8d43 120 Int_t xbin = FindXbin(x);
121 Int_t bin = GetBin(xbin,ybin);
122#ifdef _IFON_
123 if(bin < 0)
124 return;
125#endif
126
127 AddBinContent(bin,weight);
128}
129
4aa41877 130void AliHLTHistogram::Fill(Int_t xbin,Double_t y,Int_t weight)
910f8d43 131{
917e711b 132 // Fill the weight into a bin which correspond to xbin and y
910f8d43 133 Int_t ybin = FindYbin(y);
134 Int_t bin = GetBin(xbin,ybin);
135#ifdef _IFON_
136 if(bin < 0)
137 return;
138#endif
139
140 AddBinContent(bin,weight);
141}
142
4aa41877 143void AliHLTHistogram::Fill(Int_t xbin,Int_t ybin,Int_t weight)
910f8d43 144{
917e711b 145 // Fill the weight into a bin which correspond to xbin and ybin
910f8d43 146 Int_t bin = GetBin(xbin,ybin);
147#ifdef _IFON_
148 if(bin < 0)
149 return;
150#endif
b2a02bce 151
18758be6 152 AddBinContent(bin,weight);
18758be6 153}
154
4aa41877 155Int_t AliHLTHistogram::FindBin(Double_t x,Double_t y) const
4cafa5fc 156{
917e711b 157 // Finds the bin which correspond to x and y
4cafa5fc 158 Int_t xbin = FindXbin(x);
159 Int_t ybin = FindYbin(y);
910f8d43 160#ifdef _IFON_
b2a02bce 161 if(!xbin || !ybin)
162 return -1;
910f8d43 163#endif
7a21af2f 164
4cafa5fc 165 return GetBin(xbin,ybin);
166}
167
4aa41877 168Int_t AliHLTHistogram::FindLabelBin(Double_t x,Double_t y) const
e81ffe36 169{
917e711b 170 // Returns the corresponding bin with the mc labels
e81ffe36 171 Int_t xbin = FindXbin(x);
172 Int_t ybin = FindYbin(y);
173#ifdef _IFON_
174 if(!xbin || !ybin)
175 return -1;
176#endif
177
178 return GetLabelBin(xbin,ybin);
179}
180
4aa41877 181Int_t AliHLTHistogram::FindXbin(Double_t x) const
18758be6 182{
917e711b 183 // Finds the bin which correspond to x
18758be6 184 if(x < fXmin || x > fXmax)
7a21af2f 185 return 0;
4cafa5fc 186
187 return 1 + (Int_t)(fNxbins*(x-fXmin)/(fXmax-fXmin));
4cafa5fc 188}
189
4aa41877 190Int_t AliHLTHistogram::FindYbin(Double_t y) const
4cafa5fc 191{
917e711b 192 // Finds the bin which correspond to y
18758be6 193 if(y < fYmin || y > fYmax)
7a21af2f 194 return 0;
18758be6 195
4cafa5fc 196 return 1 + (Int_t)(fNybins*(y-fYmin)/(fYmax-fYmin));
4cafa5fc 197}
198
4aa41877 199Int_t AliHLTHistogram::GetBin(Int_t xbin,Int_t ybin) const
4cafa5fc 200{
917e711b 201 // Returns the bin which correspond to xbin and ybin
0a86fbb7 202 if(xbin < fFirstXbin || xbin > fLastXbin)
203 return 0;
204 if(ybin < fFirstYbin || ybin > fLastYbin)
205 return 0;
4cafa5fc 206
18758be6 207 return xbin + ybin*(fNxbins+2);
4cafa5fc 208}
209
4aa41877 210Int_t AliHLTHistogram::GetLabelBin(Int_t xbin,Int_t ybin) const
e81ffe36 211{
917e711b 212 // Returns the corresponding bin with the mc labels
e81ffe36 213 if(xbin < fFirstXbin || xbin > fLastXbin)
214 return -1;
215 if(ybin < fFirstYbin || ybin > fLastYbin)
216 return -1;
217
218 return (Int_t)(xbin/2) + ((Int_t)(ybin/2))*((Int_t)((fNxbins+3)/2));
219}
220
4aa41877 221Int_t AliHLTHistogram::GetBinContent(Int_t bin) const
4cafa5fc 222{
917e711b 223 // Return the bin content
4cafa5fc 224 if(bin >= fNcells)
225 {
4aa41877 226 LOG(AliHLTLog::kError,"AliHLTHistogram::GetBinContent","array")<<AliHLTLog::kDec<<
4cafa5fc 227 "bin out of range "<<bin<<ENDLOG;
228 return 0;
229 }
230
e1842819 231 if(fContent[bin] < fThreshold)
232 return 0;
4cafa5fc 233 return fContent[bin];
234}
235
4aa41877 236void AliHLTHistogram::SetBinContent(Int_t xbin,Int_t ybin,Int_t value)
4cafa5fc 237{
917e711b 238 // Set bin content
4cafa5fc 239 Int_t bin = GetBin(xbin,ybin);
910f8d43 240#ifdef _IFON_
7a21af2f 241 if(bin == 0)
242 return;
910f8d43 243#endif
244
7a21af2f 245 SetBinContent(bin,value);
4cafa5fc 246}
247
4aa41877 248void AliHLTHistogram::SetBinContent(Int_t bin,Int_t value)
4cafa5fc 249{
917e711b 250 // Set bin content
4cafa5fc 251
252 if(bin >= fNcells)
253 {
4aa41877 254 LOG(AliHLTLog::kError,"AliHLTHistogram::SetBinContent","array")<<AliHLTLog::kDec<<
4cafa5fc 255 "bin out of range "<<bin<<ENDLOG;
256 return;
257 }
e06900d5 258
7a21af2f 259 if(bin == 0)
260 return;
261 fContent[bin]=value;
18758be6 262}
263
4aa41877 264void AliHLTHistogram::AddBinContent(Int_t xbin,Int_t ybin,Int_t weight)
18758be6 265{
917e711b 266 // Adds weight to bin content
4cafa5fc 267 Int_t bin = GetBin(xbin,ybin);
910f8d43 268#ifdef _IFON_
7a21af2f 269 if(bin == 0)
270 return;
910f8d43 271#endif
272
18758be6 273 AddBinContent(bin,weight);
18758be6 274}
275
4aa41877 276void AliHLTHistogram::AddBinContent(Int_t bin,Int_t weight)
18758be6 277{
917e711b 278 // Adds weight to bin content
18758be6 279 if(bin < 0 || bin > fNcells)
280 {
4aa41877 281 LOG(AliHLTLog::kError,"AliHLTHistogram::AddBinContent","array")<<AliHLTLog::kDec<<
18758be6 282 "bin-value out of range "<<bin<<ENDLOG;
283 return;
284 }
7a21af2f 285 if(bin == 0)
286 return;
18758be6 287 fEntries++;
288 fContent[bin] += weight;
289}
290
4aa41877 291void AliHLTHistogram::Add(AliHLTHistogram *h1,Double_t /*weight*/)
ad11f553 292{
293 //Adding two histograms. Should be identical.
294
295 if(!h1)
296 {
4aa41877 297 LOG(AliHLTLog::kError,"AliHLTHistogram::Add","Pointer")<<
ad11f553 298 "Attempting to add a non-existing histogram"<<ENDLOG;
299 return;
300 }
301
302 if(h1->GetNbinsX()!=fNxbins || h1->GetNbinsY()!=fNybins)
303 {
4aa41877 304 LOG(AliHLTLog::kError,"AliHLTHistogram::Add","array")<<
ad11f553 305 "Mismatch in the number of bins "<<ENDLOG;
306 return;
307 }
910f8d43 308
ad11f553 309 if(h1->GetFirstXbin()!=fFirstXbin || h1->GetLastXbin()!=fLastXbin ||
310 h1->GetFirstYbin()!=fFirstYbin || h1->GetLastYbin()!=fLastYbin)
311 {
4aa41877 312 LOG(AliHLTLog::kError,"AliHLTHistogram::Add","array")<<
ad11f553 313 "Mismatch in the bin numbering "<<ENDLOG;
314 return;
315 }
316
317 for(Int_t bin=0; bin<fNcells; bin++)
318 fContent[bin] += h1->GetBinContent(bin);
319
e7139de1 320 fEntries += h1->GetNEntries();
ad11f553 321}
322
4aa41877 323Double_t AliHLTHistogram::GetBinCenterX(Int_t xbin) const
18758be6 324{
917e711b 325 // Returns the position of the center of a bin
0a86fbb7 326 if(xbin < fFirstXbin || xbin > fLastXbin)
327 {
4aa41877 328 LOG(AliHLTLog::kError,"AliHLTHistogram::GetBinCenterX","xbin")
0a86fbb7 329 <<"Bin-value out of range "<<xbin<<ENDLOG;
330 return -1;
331 }
1f1942b8 332
e06900d5 333 return fXmin + (xbin-0.5) * fBinwidthX;
18758be6 334}
335
4aa41877 336Double_t AliHLTHistogram::GetBinCenterY(Int_t ybin) const
18758be6 337{
917e711b 338 // Returns the position of the center of a bin
0a86fbb7 339 if(ybin < fFirstYbin || ybin > fLastYbin)
340 {
4aa41877 341 LOG(AliHLTLog::kError,"AliHLTHistogram::GetBinCenterY","ybin")
0a86fbb7 342 <<"Bin-value out of range "<<ybin<<ENDLOG;
343 return -1;
344 }
1f1942b8 345
e06900d5 346 return fYmin + (ybin-0.5) * fBinwidthY;
18758be6 347}
348
4aa41877 349Double_t AliHLTHistogram::GetPreciseBinCenterX(Float_t xbin) const
0bd0c1ef 350{
917e711b 351 // Returns the position of the center of a bin using precise values inside the bin
74e77d1b 352 if(xbin < (fFirstXbin-1.5) || xbin > (fLastXbin+1.5))
0bd0c1ef 353 {
4aa41877 354 LOG(AliHLTLog::kError,"AliHLTHistogram::GetBinCenterX","xbin")
0bd0c1ef 355 <<"Bin-value out of range "<<xbin<<ENDLOG;
356 return -1;
357 }
358 // return fXmin + (xbin-1) * fBinwidthX + 0.5*fBinwidthX;
359 return fXmin + (xbin-0.5) * fBinwidthX;
360}
361
4aa41877 362Double_t AliHLTHistogram::GetPreciseBinCenterY(Float_t ybin) const
0bd0c1ef 363{
917e711b 364 // Returns the position of the center of a bin using precise values inside the bin
74e77d1b 365 if(ybin < (fFirstYbin-1.5) || ybin > (fLastYbin+1.5))
0bd0c1ef 366 {
4aa41877 367 LOG(AliHLTLog::kError,"AliHLTHistogram::GetBinCenterY","ybin")
0bd0c1ef 368 <<"Bin-value out of range "<<ybin<<ENDLOG;
369 return -1;
370 }
371 // return fYmin + (ybin-1) * fBinwidthY + 0.5*fBinwidthY;
372 return fYmin + (ybin-0.5) * fBinwidthY;
373}
374
4aa41877 375void AliHLTHistogram::Draw(Char_t *option)
18758be6 376{
917e711b 377 // Fill the contents of the corresponding ROOT histogram and draws it
237d3f5c 378#ifdef use_root
7b9d6d4e 379 if(!fRootHisto)
380 CreateRootHisto();
3e87ef69 381
382 for(Int_t xbin=GetFirstXbin(); xbin<=GetLastXbin(); xbin++)
18758be6 383 {
3e87ef69 384 for(Int_t ybin=GetFirstYbin(); ybin<=GetLastYbin(); ybin++)
385 {
386 Int_t bin = GetBin(xbin,ybin);
387 fRootHisto->Fill(GetBinCenterX(xbin),GetBinCenterY(ybin),GetBinContent(bin));
388 }
18758be6 389 }
18758be6 390
3e87ef69 391 //fRootHisto->SetStats(kFALSE);
4cafa5fc 392 fRootHisto->Draw(option);
237d3f5c 393 return;
e33f3609 394#else
4aa41877 395 cerr<<"AliHLTHistogram::Draw : You need to compile with ROOT in order to draw histogram"<<endl;
e33f3609 396#endif
4de874d1 397}
7b9d6d4e 398
4aa41877 399void AliHLTHistogram::CreateRootHisto()
7b9d6d4e 400{
4aa41877 401 // Create ROOT histogram out of AliHLTHistogram
7b9d6d4e 402#ifdef use_root
403 fRootHisto = new TH2F(fName,"",fNxbins,fXmin,fXmax,fNybins,fYmin,fYmax);
404 return;
e33f3609 405#else
4aa41877 406 cerr<<"AliHLTHistogram::CreateRootHisto : You need to compile with ROOT in order to create ROOT histogram"<<endl;
e33f3609 407#endif
7b9d6d4e 408}
1f1942b8 409
4aa41877 410ofstream& operator<<(ofstream &o, const AliHLTHistogram &h)
1f1942b8 411{
412 for(Int_t xbin=h.GetFirstXbin(); xbin<=h.GetLastXbin(); xbin++)
413 {
414 for(Int_t ybin=h.GetFirstYbin(); ybin<=h.GetLastYbin(); ybin++)
415 {
416 Int_t bin = h.GetBin(xbin,ybin);
417 o << h.GetBinContent(bin) << " ";
418 }
419 o << endl;
420 }
421 return o;
422}