Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members | Related Pages

Binning.C

Go to the documentation of this file.
00001 
00031 #include "jbltools/sfh/Binning.h"
00032 
00033 #include <TAxis.h>
00034 
00035 #include <cmath>
00036 #include <cassert>
00037 
00038 static const char *ident="@(#)$Id: Binning.C,v 1.9 2005/09/29 08:56:40 boehmej Exp $";
00039 
00040 Binning::Binning ()
00041 : nbins(0), binedges(0), equidistant (true) 
00042 {}
00043 
00044 Binning::Binning (int nbins_, Axis_t xlow, Axis_t xhigh)
00045 : nbins(nbins_), binedges(0), equidistant (true) 
00046 {
00047   assert (xhigh > xlow);
00048   assert (nbins >= 1);
00049   binedges = new Axis_t[2];
00050   binedges[0] = xlow;
00051   binedges[1] = xhigh;
00052 }
00053 
00054 Binning::Binning (int nbins_, const float binedges_[])
00055 : nbins(nbins_), binedges(0), equidistant (false) 
00056 {
00057   assert (binedges_);
00058   binedges = new Axis_t[nbins+1];
00059   // C macho style:
00060   for (Axis_t *be=binedges; be != binedges+nbins+1;) *(be++) = *(binedges_++);
00061 }
00062 
00063 Binning::Binning (int nbins_, const double binedges_[])
00064 : nbins(nbins_), binedges(0), equidistant (false) 
00065 {
00066   assert (binedges_);
00067   binedges = new Axis_t[nbins+1];
00068   // C macho style:
00069   for (Axis_t *be=binedges; be != binedges+nbins+1;) *(be++) = *(binedges_++);
00070 }
00071 
00072 Binning::Binning (const TAxis& axis)
00073 : nbins(0), binedges(0), equidistant (true)  {
00074   assert (&axis);
00075   nbins = axis.GetNbins();
00076   const TArrayD *xbins = axis.GetXbins();
00077   if (!xbins || xbins->GetSize() == 0) {
00078     // equidistant binning
00079     equidistant = true;
00080     binedges = new Axis_t[2];
00081     binedges[0] = axis.GetXmin();
00082     binedges[1] = axis.GetXmax();
00083   }
00084   else {
00085     // non-equidistant binning
00086     assert (xbins->GetSize() == nbins+1);
00087     binedges = new Axis_t[nbins+1];
00088     const Double_t *array = xbins->GetArray();
00089     assert (array);
00090     // C macho style:
00091     for (Axis_t *be=binedges; be != binedges+nbins+1;) *(be++) = *(array++);
00092   }
00093 }
00094 
00095 Binning::Binning (const Binning& rhs)
00096 : nbins(rhs.nbins), binedges(0), equidistant (rhs.equidistant) {
00097   assert (rhs.binedges);
00098   binedges = new Axis_t[nbins+1];
00099   // C macho style:
00100   Axis_t *binedges_ = rhs.binedges;
00101   for (Axis_t *be=binedges; be != binedges+nbins+1;) *(be++) = *(binedges_++);
00102 } 
00103 
00104 Binning& Binning::operator= (const Binning& rhs)
00105 {
00106   if (this != &rhs) {
00107     delete[] binedges;
00108     nbins = rhs.nbins;
00109     equidistant = rhs.equidistant;
00110     binedges = new Axis_t[nbins+1];
00111     assert (rhs.binedges);
00112     // C macho style:
00113     Axis_t *binedges_ = rhs.binedges;
00114     for (Axis_t *be=binedges; be != binedges+nbins+1;) *(be++) = *(binedges_++);
00115   }
00116   return *this;
00117 } 
00118 
00119 int Binning::getBin(Axis_t x) const {
00120   if (!binedges) return -1;
00121   if (equidistant) {
00122     // assume binedges[0] and binedges[1] to be lower and upper limit
00123     if (x < binedges[0] || x >= binedges[1]) return -1;
00124     return static_cast<int> (std::floor(nbins*(x-binedges[0])/(binedges[1]-binedges[0])));
00125   }
00126   else {
00127     if (x < binedges[0] || x >= binedges[nbins]) return -1;
00128     for (int i = 1; i < nbins+1; ++i) 
00129       if (binedges[i] > x) return i-1;
00130   }
00131   return -1;    // should never happen!            
00132 }   
00133 
00134 int Binning::getNBins() const {
00135   return nbins;
00136 }
00137 Axis_t Binning::getLowerBinEdge(int i) const {
00138   if (!binedges || i < 0 || i > nbins-1) return 0;
00139   if (equidistant) return binedges[0]+i*(binedges[1]-binedges[0])/nbins;
00140   return binedges[i];
00141 }
00142 
00143 Axis_t Binning::getUpperBinEdge(int i) const {
00144   if (!binedges || i < 0 || i > nbins-1) return 0;
00145   if (equidistant) return binedges[0]+(i+1)*(binedges[1]-binedges[0])/nbins;
00146   return binedges[i+1];
00147 }
00148 
00149 Axis_t Binning::getLowerEdge() const {
00150   if (!binedges) return 0;
00151   return binedges[0];
00152 }
00153 
00154 Axis_t Binning::getUpperEdge() const {
00155   if (!binedges) return 0;
00156   return (equidistant) ? binedges[1] : binedges [nbins];
00157 }
00158 
00159 const Axis_t *Binning::getEdges() const {
00160   return binedges;
00161 }
00162 
00163 bool Binning::isEquidistant() const {
00164   return equidistant;
00165 }
00166 
00167 bool Binning::hasBinLabels() const {
00168   return false;
00169 }
00170     
00171 const char *Binning::getBinLabel(int i) const {
00172   return "";
00173 }
00174     
00175 Binning::~Binning() {
00176   delete[] binedges;
00177   binedges = 0;
00178 }

Generated on Thu Oct 26 12:52:55 2006 for SFH by doxygen 1.3.2