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

Bins1DProfile.cxx

Go to the documentation of this file.
00001 
00012 #ifdef HAVE_CONFIG_H
00013 #include "config.h"
00014 #else
00015 #ifdef _MSC_VER
00016 #include "msdevstudio/MSconfig.h"
00017 #endif
00018 #endif
00019 
00020 #include "binners/Bins1DProfile.h"
00021 
00022 #include "datasrcs/DataPointTuple.h"
00023 #include "datasrcs/NTuple.h"
00024 
00025 #include <algorithm>
00026 #include <numeric>
00027 
00028 #include <cmath>
00029 
00030 #include <cassert>
00031 
00032 using namespace hippodraw;
00033 
00034 #ifdef ITERATOR_MEMBER_DEFECT
00035 using namespace std;
00036 #else
00037 using std::fill;
00038 using std::list;
00039 using std::sqrt;
00040 using std::string;
00041 using std::vector;
00042 using std::min_element;
00043 using std::max_element;
00044 #endif
00045 
00046 Bins1DProfile::Bins1DProfile ( )
00047   : Bins1DBase ( "Bins1DProfile" )
00048 {
00049 }
00050 
00051 Bins1DProfile::Bins1DProfile ( const Bins1DProfile & binner )
00052   : Bins1DBase ( binner ),
00053     m_sum( binner.m_sum ), 
00054     m_sumsq( binner.m_sumsq ),
00055     m_num( binner.m_num )
00056 {
00057 }
00058 
00059 BinsBase *
00060 Bins1DProfile::clone () const
00061 {
00062   return new Bins1DProfile(*this);
00063 }
00064 
00065 Bins1DProfile::~Bins1DProfile ()
00066 {
00067 }
00068 
00069 double Bins1DProfile::minBin()
00070 {
00071   return *min_element ( m_sum.begin() + 1, m_sum.end ( ) - 1 );
00072 }
00073 
00074 double Bins1DProfile::maxBin()
00075 {
00076   return *max_element ( m_sum.begin () + 1, m_sum.end () - 1 );
00077 }
00078 
00079 void Bins1DProfile::resize ( int number )
00080 {
00081   m_sum.resize( number + 2 );
00082   m_num.resize( number + 2 );
00083   m_sumsq.resize( number + 2 );
00084 
00085   reset();
00086 
00087   m_values_dirty = true;
00088 }
00089 
00090 void Bins1DProfile::reset()
00091 {
00092   fill( m_sum.begin(), m_sum.end(), 0.0 );
00093   fill( m_num.begin(), m_num.end(), 0 ); // This is an int array
00094   fill( m_sumsq.begin(), m_sumsq.end(), 0.0 );
00095 
00096   m_empty = true;
00097 }
00098 
00099 int Bins1DProfile::getNumberOfEntries () const
00100 {
00101   return static_cast < int > ( std::accumulate ( m_num.begin() + 1, 
00102                                                  m_num.end() - 2, 0.0 ) );
00103 }
00104 
00105 int Bins1DProfile::getNumberOfEntries ( int i ) const
00106 {
00107   return *( m_num.begin() + i + 1 );
00108 }
00109 
00110 void Bins1DProfile::accumulate( double x, double y, double, double )
00111 {
00112   int i = binNumber ( x );
00113 
00114   m_sum[i] += y;
00115   m_sumsq[i] += y * y;
00116   m_num[i] ++;
00117 
00118   m_empty = false;
00119 }
00120 
00121 
00122 NTuple *
00123 Bins1DProfile::
00124 createNTuple () const
00125 {
00126   unsigned int size = m_sum.size ();
00127   NTuple * ntuple = prepareNTuple ( size );
00128 
00129   fillProjectedValues ( ntuple );
00130 
00131   return ntuple;
00132 }
00133 
00134 namespace dp = hippodraw::DataPoint2DTuple;
00135 
00138 void
00139 Bins1DProfile::
00140 fillProjectedValues ( DataSource * ntuple ) const
00141 {
00142   ntuple -> clear();
00143   vector < double > row ( dp::SIZE );
00144 
00145 //   double total = getNumberOfEntries ();
00146 //   double factor = m_is_scaling ? m_scale_factor / total : 1.0;
00147 
00148   double width;
00149   double x = getLow ( Axes::X );
00150   int x_number = numberOfBins ( Axes::X );
00151   for ( int i = 0; i < x_number; i++ ) {
00152     width = binWidth ( i );
00153     x += 0.5 * width;
00154     double y = 0;
00155     double num = m_num [ i+1 ];
00156     if ( num <= 0 ) {
00157       x += 0.5 * width;
00158       continue;
00159     }
00160     if ( m_sum[i+1] != 0 ) {
00161       y = m_sum[i+1] / m_num[i+1];
00162     }
00163     else y = 0.;
00164 
00165     double xerr = width;
00166     double yerr = 0.0;
00167 
00168     if ( m_num[i+1] > 1. ) {
00169       double num = m_num[i+1];
00170 //       yerr = sqrt ( ( m_sumsq[i+1] / ( num - 1. )  - y * y ) 
00171 //                  / ( factor * num ) ) ;
00172 // Restore profile error bars as the RMS of the distribution.
00173       yerr = sqrt ( ( m_sumsq[i+1] / ( num - 1. )  - y * y ) );
00174     }
00175     else {
00176       yerr = 0.0;
00177     }
00178 
00179     row[dp::X] = x;
00180     row[dp::Y] = y;
00181     row[dp::XERR] = xerr;
00182     row[dp::YERR] = yerr;
00183 
00184     ntuple -> addRow ( row );
00185 
00186     x += 0.5 * width;
00187   }
00188 }
00189 
00190 void
00191 Bins1DProfile::
00192 fillDataSource ( DataSource * ntuple ) const
00193 {
00194   ntuple -> clear();
00195   vector < double > row ( dp::SIZE );
00196 
00197 //   double total = getNumberOfEntries ();
00198 //   double factor = m_is_scaling ? m_scale_factor / total : 1.0;
00199 
00200   double width;
00201   double x = getLow ( Axes::X );
00202   int x_number = numberOfBins ( Axes::X );
00203   for ( int i = 0; i < x_number; i++ ) {
00204     width = binWidth ( i );
00205     double half_width = 0.5 * width;
00206 //     x += 0.5 * width;
00207     x += half_width;
00208     double y = 0;
00209     double num = m_num [ i+1 ];
00210     if ( num <= 0 ) {
00211 //       x += 0.5 * width;
00212       x += half_width;
00213       continue;
00214     }
00215     if ( m_sum[i+1] != 0 ) {
00216       y = m_sum[i+1] / m_num[i+1];
00217     }
00218     else y = 0.;
00219 
00220 //     double xerr = width;
00221     double yerr = 0.0;
00222 
00223     if ( m_num[i+1] > 1. ) {
00224       double num = m_num[i+1];
00225 //       yerr = sqrt ( ( m_sumsq[i+1] / ( num - 1. )  - y * y ) 
00226 //                  / ( factor * num ) ) ;
00227 // Restore profile error bars as the RMS of the distribution.
00228       yerr = sqrt ( ( m_sumsq[i+1] / ( num - 1. )  - y * y ) );
00229     }
00230     else {
00231       yerr = 0.0;
00232     }
00233 
00234     row[dp::X] = x;
00235     row[dp::Y] = y;
00236 //     row[dp::XERR] = xerr;
00237     row[dp::XERR] = half_width;
00238     row[dp::YERR] = yerr;
00239 
00240     ntuple -> addRow ( row );
00241 
00242 //     x += 0.5 * width;
00243     x += half_width;
00244   }
00245 }
00246 
00250 void
00251 Bins1DProfile::
00252 setBinContents ( const DataSource * ntuple )
00253 {
00254 }

Generated for HippoDraw-1.14.8.5 by doxygen 1.4.3