Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Bins2DHist.cxx

Go to the documentation of this file.
00001 
00016 #ifdef HAVE_CONFIG_H
00017 #include "config.h"
00018 #else
00019 
00020 #ifdef _MSC_VER
00021 // Include max() and min() missing from MicroSoft Visual C++.
00022 #include "msdevstudio/MSconfig.h"
00023 #endif
00024 
00025 #endif
00026 
00027 #include "Bins2DHist.h"
00028 
00029 #include "BinnerAxis.h"
00030 
00031 #include "datasrcs/DataPointTuple.h"
00032 #include "datasrcs/NTuple.h"
00033 
00034 #include <cmath>
00035 
00036 #include <cassert>
00037 
00038 using namespace hippodraw;
00039 
00040 #ifdef ITERATOR_MEMBER_DEFECT
00041 using namespace std;
00042 #else
00043 using std::fill;
00044 using std::list;
00045 using std::sqrt;
00046 using std::vector;
00047 #endif
00048 
00049 Bins2DHist::Bins2DHist ( )
00050   : Bins2DBase ( "Bins2DHist" )
00051 {
00052 }
00053 
00054 Bins2DHist::Bins2DHist ( const Bins2DHist & binner )
00055   : Bins2DBase( binner ),
00056     m_variance( binner.m_variance )
00057 {
00058   m_num_bins = binner.m_num_bins;
00059 
00060   for ( int i = 0; i < 3; i++ ) {
00061     m_x_moments[i] = binner.m_x_moments[i];
00062     m_y_moments[i] = binner.m_y_moments[i];
00063   }
00064 }
00065 
00066 BinsBase *
00067 Bins2DHist::clone () const
00068 {
00069   return new Bins2DHist ( *this );
00070 }
00071 
00072 Bins2DHist::~Bins2DHist ()
00073 {
00074 }
00075 
00076 void
00077 Bins2DHist::
00078 setNumberOfBins ( hippodraw::Axes::Type axis, int nb ) 
00079 {
00080   assert ( axis == Axes::X || axis == Axes::Y );
00081 
00082   if ( axis == Axes::X ) {
00083     m_data.resize ( nb + 2 ); //GB
00084     m_variance.resize ( nb ); //GB
00085     Bins2DBase::setNumberOfBins ( axis, nb );
00086   }
00087   else { // Y
00088     Bins2DBase::setNumberOfBins ( axis, nb );  
00089 
00090     int number_y = numberOfBins ( Axes::Y );
00091     
00092     unsigned int i = 0; // For Visual Studio C++ for scope bug
00093     for( ; i < m_data.size(); i++ ) {
00094       m_data[i].resize( number_y + 2 );
00095     }
00096 
00097     for( i = 0; i < m_variance.size(); i++ ) {
00098       m_variance[i].resize( number_y );
00099     }
00100   }
00101 
00102   reset ();
00103 }
00104 
00105 void Bins2DHist::reset ()
00106 {
00107   unsigned int i = 0; // For Visual Studio for scope bug.
00108   for ( ; i < m_data.size(); i++ ) {
00109     fill ( m_data[i].begin(), m_data[i].end(), 0.0 );
00110   }
00111 
00112   for ( i = 0; i < m_variance.size(); i++ ) {
00113     fill ( m_variance[i].begin(), m_variance[i].end(), 0.0 );
00114   }
00115 
00116   fill ( m_x_moments, m_x_moments + 3, 0.0 );
00117   fill ( m_y_moments, m_y_moments + 3, 0.0 );
00118 
00119   m_empty = true;
00120 }
00121 
00122 void Bins2DHist::accumulate( double x, double y, double wt, double unused )
00123 {
00124   int i = binNumberX ( x );
00125   int j = binNumberY ( y );
00126 
00127   if( i > 0 && i <= numberOfBins ( Axes::X ) && 
00128       j > 0 && j <= numberOfBins ( Axes::Y ) ) {
00129     m_variance[i-1][j-1] += wt * wt;
00130     m_x_moments[0] += wt;
00131     m_x_moments[1] += wt * x;
00132     m_x_moments[2] += wt * x * x;
00133     m_y_moments[0] += wt;
00134     m_y_moments[1] += wt * y;
00135     m_y_moments[2] += wt * y * y;
00136   }
00137   m_data[i][j] += wt;
00138 
00139   m_empty = false;
00140 }
00141 
00142 double Bins2DHist::getZValue ( double x, double y ) const
00143 {
00144   int i = binNumberX( x );
00145   int j = binNumberY( y );
00146   double widthX = binWidthX ( i-1 );
00147   double widthY = binWidthY ( j-1 );
00148   double v = m_data[i][j] / ( widthX * widthY );
00149   return v;
00150 }
00151 
00152 NTuple *
00153 Bins2DHist::
00154 createNTuple () const
00155 {
00156   unsigned int size = numberOfBins ( Axes::X ) * numberOfBins ( Axes::Y );
00157   NTuple * ntuple = prepareNTuple ( size );
00158 
00159   fillProjectedValues ( ntuple );
00160 
00161   return ntuple;
00162 }
00163 
00164 namespace dp = hippodraw::DataPoint3DTuple;
00165 
00166 void
00167 Bins2DHist::
00168 fillProjectedValues ( DataSource * ntuple ) const
00169 {
00170   ntuple -> clear ();
00171 
00172   double total = getNumberOfEntries ();
00173   double factor = m_is_scaling ? m_scale_factor / total : 1.0;
00174 
00175   vector < double > row ( dp::SIZE );
00176 
00177   double x = getLow ( Axes::X );
00178 
00179   unsigned int num_x = numberOfBins ( Axes::X );
00180   unsigned int num_y = numberOfBins ( Axes::Y );
00181 
00182   for ( unsigned int i = 0; i < num_x; i++ ) {
00183 
00184     double widthX = binWidthX ( i );
00185     x += 0.5 * widthX;
00186 
00187     double y = getLow ( Axes::Y );
00188 
00189     for ( unsigned int j = 0; j < num_y; j++ ) {
00190       double widthY = binWidthY ( j ); 
00191 
00192       y += 0.5 * widthY;
00193 
00194       double v = factor * ( m_data[i+1][j+1] / ( widthX * widthY ) );
00195       double xerr = widthX;
00196       double yerr = widthY;
00197       double verr = factor * ( sqrt ( m_variance[i][j] ) );
00198 
00199       row[dp::X] = x;
00200       row[dp::Y] = y;
00201       row[dp::Z] = v;
00202       row [dp::XERR] = xerr;
00203       row [dp::YERR] = yerr;
00204       row [dp::ZERR] = verr;
00205 
00206       ntuple -> addRow ( row );
00207 
00208       y += 0.5 * widthY;
00209     }
00210     x += 0.5 * widthX;
00211   }
00212 
00213   vector < unsigned int > shape ( 3 );
00214   shape[0] = num_x;
00215   shape[1] = num_y;
00216   shape[2] = dp::SIZE;
00217 
00218   ntuple -> setShape ( shape );
00219 }
00220 
00221 void
00222 Bins2DHist::
00223 fillDataSource ( DataSource * ntuple ) const
00224 {
00225   ntuple -> clear ();
00226 
00227   double total = getNumberOfEntries ();
00228   double factor = m_is_scaling ? m_scale_factor / total : 1.0;
00229 
00230   vector < double > row ( dp::SIZE );
00231 
00232   double x = getLow ( Axes::X );
00233 
00234   unsigned int num_x = numberOfBins ( Axes::X );
00235   unsigned int num_y = numberOfBins ( Axes::Y );
00236 
00237   for ( unsigned int i = 0; i < num_x; i++ ) {
00238 
00239     double widthX = binWidthX ( i );
00240     double half_widthX = 0.5 * widthX;
00241 //     x += 0.5 * widthX;
00242     x += half_widthX;
00243 
00244     double y = getLow ( Axes::Y );
00245 
00246     for ( unsigned int j = 0; j < num_y; j++ ) {
00247       double widthY = binWidthY ( j ); 
00248       double half_widthY = 0.5 * widthY;
00249 //       y += 0.5 * widthY;
00250       y += half_widthY;
00251 
00252       double v = factor * ( m_data[i+1][j+1] / ( widthX * widthY ) );
00253 //       double xerr = widthX;
00254 //       double yerr = widthY;
00255       double verr = factor * ( sqrt ( m_variance[i][j] ) );
00256 
00257       row[dp::X] = x;
00258       row[dp::Y] = y;
00259       row[dp::Z] = v;
00260 //       row [dp::XERR] = xerr;
00261 //       row [dp::YERR] = yerr;
00262       row [dp::XERR] = half_widthX;
00263       row [dp::YERR] = half_widthY;
00264       row [dp::ZERR] = verr;
00265 
00266       ntuple -> addRow ( row );
00267 
00268 //       y += 0.5 * widthY;
00269       y += half_widthY;
00270     }
00271 //     x += 0.5 * widthX;
00272     x += half_widthX;
00273   }
00274 
00275   vector < unsigned int > shape ( 3 );
00276   shape[0] = num_x;
00277   shape[1] = num_y;
00278   shape[2] = dp::SIZE;
00279 
00280   ntuple -> setShape ( shape );
00281 }
00282 
00283 void
00284 Bins2DHist::
00285 setBinContents ( const DataSource * ntuple )
00286 {
00287   unsigned int num_x = numberOfBins ( Axes::X );
00288   unsigned int num_y = numberOfBins ( Axes::Y );
00289   unsigned int r = 0;
00290   for ( unsigned int i = 0; i < num_x; i++ ) {
00291     double widthX = binWidthX ( i );
00292     for ( unsigned int j = 0; j < num_y; j++ ) {
00293       double widthY = binWidthY ( j );
00294       const vector < double > & row = ntuple -> getRow ( r++ );
00295       double value = row [ dp::Z ];
00296       double verr = row [ dp::ZERR ];
00297       m_data[i+1][j+1] = value * ( widthX * widthY );
00298       m_variance[i][j] = verr * verr;
00299     }
00300   }
00301 }

Generated for HippoDraw-1.14.8.5 by doxygen 1.4.3