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