]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/hough/AliL3Histogram.cxx
Added files for the reference version based on an old version of Anders'
[u/mrichter/AliRoot.git] / HLT / hough / AliL3Histogram.cxx
index afc15a9e2828992e21a0a404b29b67ec05b6fb10..e006985ed04313e37ac2df63f264009cea9d023c 100644 (file)
@@ -1,21 +1,35 @@
-//$Id$
+// @(#) $Id$
 
 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
-//*-- Copyright &copy ASV
+//*-- Copyright &copy ALICE HLT Group
+
+#include "AliL3StandardIncludes.h"
 
-#include <string.h>
 #include "AliL3Logging.h"
 #include "AliL3Histogram.h"
 
+#if __GNUC__ >= 3
+using namespace std;
+#endif
+
+/** \class AliL3Histogram
+<pre>
 //_____________________________________________________________
 // AliL3Histogram
 //
 // 2D histogram class
+//
+</pre>
+*/
+
+//uncomment if you want overflow checks
+//#define _IFON_
 
 ClassImp(AliL3Histogram)
 
 AliL3Histogram::AliL3Histogram()
 {
+  // Default constructor
   fNxbins = 0;
   fNybins = 0;
   fNcells = 0;
@@ -23,6 +37,8 @@ AliL3Histogram::AliL3Histogram()
   fYmin = 0;
   fXmax = 0;
   fYmax = 0;
+  fBinwidthX = 0;
+  fBinwidthY = 0;  
   fFirstXbin = 0;
   fLastXbin = 0;
   fFirstYbin = 0;
@@ -30,23 +46,28 @@ AliL3Histogram::AliL3Histogram()
   fEntries = 0;
   fContent = 0;
   fThreshold = 0;
+#ifdef use_root
+  fRootHisto = 0;
+#endif
 }
 
-  
-AliL3Histogram::AliL3Histogram(Char_t *name,Char_t *id,
+AliL3Histogram::AliL3Histogram(Char_t *name,Char_t */*id*/,
                               Int_t nxbin,Double_t xmin,Double_t xmax,
                               Int_t nybin,Double_t ymin,Double_t ymax) 
 {
-  
+  // Normal constructor
   strcpy(fName,name);
+
   fNxbins = nxbin;
   fNybins = nybin;
   fNcells = (nxbin+2)*(nybin+2);
-  
   fXmin = xmin;
   fYmin = ymin;
   fXmax = xmax;
   fYmax = ymax;
+  fBinwidthX = (fXmax - fXmin) / fNxbins;
+  fBinwidthY = (fYmax - fYmin) / fNybins;
+  
   fEntries = 0;
   fFirstXbin = 1;
   fFirstYbin = 1;
@@ -66,77 +87,140 @@ AliL3Histogram::~AliL3Histogram()
   //Destructor
   if(fContent)
     delete [] fContent;
-
 #ifdef use_root
   if(fRootHisto)
     delete fRootHisto;
 #endif
-
 }
 
-
 void AliL3Histogram::Reset()
 {
-  
-  for(Int_t i=0; i<fNcells; i++)
-    fContent[i] = 0;
+  // Reset histogram contents
+  if(fContent)
+    for(Int_t i=0; i<fNcells; i++) fContent[i] = 0;
+
   fEntries=0;
 }
 
 void AliL3Histogram::Fill(Double_t x,Double_t y,Int_t weight)
 {
+  // Fill the weight into a bin which correspond to x and y
   Int_t bin = FindBin(x,y);
+#ifdef _IFON_
+  if(bin < 0)
+    return;
+#endif
+  
   AddBinContent(bin,weight);
+}
 
+void AliL3Histogram::Fill(Double_t x,Int_t ybin,Int_t weight)
+{
+  // Fill the weight into a bin which correspond to x and ybin
+  Int_t xbin = FindXbin(x);
+  Int_t bin = GetBin(xbin,ybin);
+#ifdef _IFON_
+  if(bin < 0)
+    return;
+#endif
+  
+  AddBinContent(bin,weight);
 }
 
-Int_t AliL3Histogram::FindBin(Double_t x,Double_t y)
+void AliL3Histogram::Fill(Int_t xbin,Double_t y,Int_t weight)
 {
+  // Fill the weight into a bin which correspond to xbin and y
+  Int_t ybin = FindYbin(y);
+  Int_t bin = GetBin(xbin,ybin);
+#ifdef _IFON_
+  if(bin < 0)
+    return;
+#endif
+  
+  AddBinContent(bin,weight);
+}
+
+void AliL3Histogram::Fill(Int_t xbin,Int_t ybin,Int_t weight)
+{
+  // Fill the weight into a bin which correspond to xbin and ybin
+  Int_t bin = GetBin(xbin,ybin);
+#ifdef _IFON_
+  if(bin < 0)
+    return;
+#endif
   
+  AddBinContent(bin,weight);
+}
+
+Int_t AliL3Histogram::FindBin(Double_t x,Double_t y) const
+{
+  // Finds the bin which correspond to x and y
   Int_t xbin = FindXbin(x);
   Int_t ybin = FindYbin(y);
+#ifdef _IFON_
+  if(!xbin || !ybin)
+    return -1;
+#endif
   
   return GetBin(xbin,ybin);
 }
 
-Int_t AliL3Histogram::FindXbin(Double_t x)
+Int_t AliL3Histogram::FindLabelBin(Double_t x,Double_t y) const
+{
+  // Returns the corresponding bin with the mc labels
+  Int_t xbin = FindXbin(x);
+  Int_t ybin = FindYbin(y);
+#ifdef _IFON_
+  if(!xbin || !ybin)
+    return -1;
+#endif
+  
+  return GetLabelBin(xbin,ybin);
+}
+
+Int_t AliL3Histogram::FindXbin(Double_t x) const
 {
+  // Finds the bin which correspond to x
   if(x < fXmin || x > fXmax)
     return 0;
   
   return 1 + (Int_t)(fNxbins*(x-fXmin)/(fXmax-fXmin));
-
 }
 
-Int_t AliL3Histogram::FindYbin(Double_t y)
+Int_t AliL3Histogram::FindYbin(Double_t y) const
 {
+  // Finds the bin which correspond to y
   if(y < fYmin || y > fYmax)
     return 0;
   
   return 1 + (Int_t)(fNybins*(y-fYmin)/(fYmax-fYmin));
-
 }
 
-Int_t AliL3Histogram::GetBin(Int_t xbin,Int_t ybin)
+Int_t AliL3Histogram::GetBin(Int_t xbin,Int_t ybin) const
 {
-  if(xbin < 0 || xbin > GetLastXbin())
-    {
-      LOG(AliL3Log::kError,"AliL3Histogram::GetBin","array")<<AliL3Log::kDec<<
-       "xbin out of range "<<xbin<<ENDLOG;
-      return 0;
-    }
-  if(ybin < 0 || ybin > GetLastYbin())
-    {
-      LOG(AliL3Log::kError,"AliL3Histogram::FindYbin","array")<<AliL3Log::kDec<<
-       "ybin out of range "<<xbin<<ENDLOG;
-      return 0;
-    }
+  // Returns the bin which correspond to xbin and ybin
+  if(xbin < fFirstXbin || xbin > fLastXbin)
+    return 0;
+  if(ybin < fFirstYbin || ybin > fLastYbin)
+    return 0;
     
   return xbin + ybin*(fNxbins+2);
 }
 
-Int_t AliL3Histogram::GetBinContent(Int_t bin)
+Int_t AliL3Histogram::GetLabelBin(Int_t xbin,Int_t ybin) const
 {
+  // Returns the corresponding bin with the mc labels
+  if(xbin < fFirstXbin || xbin > fLastXbin)
+    return -1;
+  if(ybin < fFirstYbin || ybin > fLastYbin)
+    return -1;
+    
+  return (Int_t)(xbin/2) + ((Int_t)(ybin/2))*((Int_t)((fNxbins+3)/2));
+}
+
+Int_t AliL3Histogram::GetBinContent(Int_t bin) const
+{
+  // Return the bin content
   if(bin >= fNcells)
     {
       LOG(AliL3Log::kError,"AliL3Histogram::GetBinContent","array")<<AliL3Log::kDec<<
@@ -151,14 +235,19 @@ Int_t AliL3Histogram::GetBinContent(Int_t bin)
 
 void AliL3Histogram::SetBinContent(Int_t xbin,Int_t ybin,Int_t value)
 {
+  // Set bin content
   Int_t bin = GetBin(xbin,ybin);
+#ifdef _IFON_
   if(bin == 0) 
     return;
+#endif
+
   SetBinContent(bin,value);
 }
 
 void AliL3Histogram::SetBinContent(Int_t bin,Int_t value)
 {
+  // Set bin content
 
   if(bin >= fNcells)
     {
@@ -166,23 +255,27 @@ void AliL3Histogram::SetBinContent(Int_t bin,Int_t value)
        "bin out of range "<<bin<<ENDLOG;
       return;
     }
+
   if(bin == 0)
     return;
   fContent[bin]=value;
-  
 }
 
 void AliL3Histogram::AddBinContent(Int_t xbin,Int_t ybin,Int_t weight)
 {
+  // Adds weight to bin content
   Int_t bin = GetBin(xbin,ybin);
+#ifdef _IFON_
   if(bin == 0)
     return;
-  AddBinContent(bin,weight);
+#endif
 
+  AddBinContent(bin,weight);
 }
 
 void AliL3Histogram::AddBinContent(Int_t bin,Int_t weight)
 {
+  // Adds weight to bin content
   if(bin < 0 || bin > fNcells)
     {
       LOG(AliL3Log::kError,"AliL3Histogram::AddBinContent","array")<<AliL3Log::kDec<<
@@ -195,7 +288,7 @@ void AliL3Histogram::AddBinContent(Int_t bin,Int_t weight)
   fContent[bin] += weight;
 }
 
-void AliL3Histogram::Add(AliL3Histogram *h1,Double_t weight)
+void AliL3Histogram::Add(AliL3Histogram *h1,Double_t /*weight*/)
 {
   //Adding two histograms. Should be identical.
   
@@ -212,6 +305,7 @@ void AliL3Histogram::Add(AliL3Histogram *h1,Double_t weight)
        "Mismatch in the number of bins "<<ENDLOG;
       return;
     }
+
   if(h1->GetFirstXbin()!=fFirstXbin || h1->GetLastXbin()!=fLastXbin ||
      h1->GetFirstYbin()!=fFirstYbin || h1->GetLastYbin()!=fLastYbin)
     {
@@ -223,48 +317,106 @@ void AliL3Histogram::Add(AliL3Histogram *h1,Double_t weight)
   for(Int_t bin=0; bin<fNcells; bin++)
     fContent[bin] += h1->GetBinContent(bin);
   
+  fEntries += h1->GetNEntries();
 }
 
-Double_t AliL3Histogram::GetBinCenterX(Int_t xbin)
+Double_t AliL3Histogram::GetBinCenterX(Int_t xbin) const
 {
-  
-  Double_t binwidth = (fXmax - fXmin) / fNxbins;
-  return fXmin + (xbin-1) * binwidth + 0.5*binwidth;
-  
+  // Returns the position of the center of a bin
+  if(xbin < fFirstXbin || xbin > fLastXbin)
+    {
+      LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterX","xbin")
+       <<"Bin-value out of range "<<xbin<<ENDLOG;
+      return -1;
+    }
+
+  return fXmin + (xbin-0.5) * fBinwidthX;
 }
 
-Double_t AliL3Histogram::GetBinCenterY(Int_t ybin)
+Double_t AliL3Histogram::GetBinCenterY(Int_t ybin) const
 {
-  
-  Double_t binwidth = (fYmax - fYmin) / fNybins;
-  return fYmin + (ybin-1) * binwidth + 0.5*binwidth;
-  
+  // Returns the position of the center of a bin
+  if(ybin < fFirstYbin || ybin > fLastYbin)
+    {
+      LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterY","ybin")
+       <<"Bin-value out of range "<<ybin<<ENDLOG;
+      return -1;
+    }
+
+  return fYmin + (ybin-0.5) * fBinwidthY;
 }
 
+Double_t AliL3Histogram::GetPreciseBinCenterX(Float_t xbin) const
+{
+  // Returns the position of the center of a bin using precise values inside the bin
+  if(xbin < (fFirstXbin-1.5) || xbin > (fLastXbin+1.5))
+    {
+      LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterX","xbin")
+       <<"Bin-value out of range "<<xbin<<ENDLOG;
+      return -1;
+    }
+  //  return fXmin + (xbin-1) * fBinwidthX + 0.5*fBinwidthX;
+  return fXmin + (xbin-0.5) * fBinwidthX;
+}
+
+Double_t AliL3Histogram::GetPreciseBinCenterY(Float_t ybin) const
+{
+  // Returns the position of the center of a bin using precise values inside the bin
+  if(ybin < (fFirstYbin-1.5) || ybin > (fLastYbin+1.5))
+    {
+      LOG(AliL3Log::kError,"AliL3Histogram::GetBinCenterY","ybin")
+       <<"Bin-value out of range "<<ybin<<ENDLOG;
+      return -1;
+    }
+  //  return fYmin + (ybin-1) * fBinwidthY + 0.5*fBinwidthY;
+  return fYmin + (ybin-0.5) * fBinwidthY;
+}
 
 void AliL3Histogram::Draw(Char_t *option)
 {
+  // Fill the contents of the corresponding ROOT histogram and draws it 
 #ifdef use_root
   if(!fRootHisto)
     CreateRootHisto();
-  for(Int_t bin=0; bin<fNcells; bin++)
+  
+  for(Int_t xbin=GetFirstXbin(); xbin<=GetLastXbin(); xbin++)
     {
-      fRootHisto->AddBinContent(bin,GetBinContent(bin));
+      for(Int_t ybin=GetFirstYbin(); ybin<=GetLastYbin(); ybin++)
+       {
+         Int_t bin = GetBin(xbin,ybin);
+         fRootHisto->Fill(GetBinCenterX(xbin),GetBinCenterY(ybin),GetBinContent(bin));
+       }
     }
   
-  fRootHisto->SetStats(kFALSE);
+  //fRootHisto->SetStats(kFALSE);
   fRootHisto->Draw(option);
   return;
-#endif
+#else
   cerr<<"AliL3Histogram::Draw : You need to compile with ROOT in order to draw histogram"<<endl;
-  
+#endif  
 }
 
 void AliL3Histogram::CreateRootHisto()
 {
+  // Create ROOT histogram out of AliL3Histogram
 #ifdef use_root
   fRootHisto = new TH2F(fName,"",fNxbins,fXmin,fXmax,fNybins,fYmin,fYmax);
   return;
-#endif
+#else
   cerr<<"AliL3Histogram::CreateRootHisto : You need to compile with ROOT in order to create ROOT histogram"<<endl;
+#endif
+}
+
+ofstream& operator<<(ofstream &o, const AliL3Histogram &h)
+{
+  for(Int_t xbin=h.GetFirstXbin(); xbin<=h.GetLastXbin(); xbin++)
+    {
+      for(Int_t ybin=h.GetFirstYbin(); ybin<=h.GetLastYbin(); ybin++)
+       {
+         Int_t bin = h.GetBin(xbin,ybin);
+         o << h.GetBinContent(bin) << " ";
+       }
+      o << endl;
+    }
+  return o;
 }