00001
00012 #ifdef HAVE_CONFIG_H
00013 #include "config.h"
00014 #else
00015
00016 #ifdef _MSC_VER
00017
00018 #include "msdevstudio/MSconfig.h"
00019 #endif
00020
00021 #endif
00022
00023 #include "Map1Projector.h"
00024
00025 #include "axes/AxisModelBase.h"
00026
00027 #include "datasrcs/DataPointTuple.h"
00028 #include "datasrcs/NTuple.h"
00029
00030 #include <algorithm>
00031 #include <numeric>
00032
00033 #include <cfloat>
00034
00035 #include <cassert>
00036
00037 using namespace hippodraw;
00038
00039 #ifdef ITERATOR_MEMBER_DEFECT
00040 using namespace std;
00041 #else
00042 using std::accumulate;
00043 using std::find;
00044 using std::max;
00045 using std::min;
00046 using std::string;
00047 using std::vector;
00048 #endif
00049
00050 Map1Projector::Map1Projector ( )
00051 : NTupleProjector ( 2 ),
00052 m_x_label ( "Item" ),
00053 m_y_option ( "Y error (optional)" )
00054 {
00055 m_binding_options.push_back ( "Y" );
00056 m_min_bindings = 1;
00057 addPointReps();
00058 }
00059
00060 Map1Projector::Map1Projector ( const Map1Projector & projector )
00061 : NTupleProjector( projector ),
00062 m_x_label ( projector.m_x_label )
00063 {
00064 addPointReps();
00065 }
00066
00067 Map1Projector::~Map1Projector()
00068 {
00069 }
00070
00071 ProjectorBase * Map1Projector::clone()
00072 {
00073 return new Map1Projector( *this );
00074 }
00075
00076 bool
00077 Map1Projector::
00078 inRange ( int row ) const
00079 {
00080 bool yes = true;
00081 double x = row;
00082 const Range & x_range = m_x_axis->getRange ( false );
00083 if ( x_range.excludes ( x ) ) {
00084 yes = false;
00085 }
00086 else {
00087 const Range & y_range = m_y_axis->getRange ( false );
00088 if ( y_range.excludes ( m_ntuple ->
00089 valueAt ( row, m_columns[0] ) ) ) yes = false;
00090 }
00091
00092 return yes;
00093 }
00094
00095
00096 void Map1Projector::setYErrorOption ( bool enable )
00097 {
00098 const string name ( "Y error" );
00099 vector< string >:: iterator first
00100 = find ( m_binding_options.begin (),
00101 m_binding_options.end (),
00102 name );
00103 if ( first != m_binding_options.end () && !enable ) {
00104 m_binding_options.erase ( first );
00105 m_columns[1] = UINT_MAX;
00106 }
00107 else if ( enable ) {
00108 m_binding_options.push_back ( name );
00109 }
00110 }
00111
00112 void Map1Projector::changedNTuple()
00113 {
00114 unsigned int cols = m_ntuple->columns () - 1;
00115 if ( m_columns[0] > cols ) m_columns[0] = cols;
00116 if ( m_columns[1] > cols ) m_columns[1] = UINT_MAX;
00117 }
00118
00119 Range Map1Projector::valueRange () const
00120 {
00121 return dataRangeOn ( Axes::Y );
00122 }
00123
00124 Range
00125 Map1Projector::
00126 dataRangeOn ( hippodraw::Axes::Type axis ) const
00127 {
00128 assert ( axis == Axes::X || axis == Axes::Y );
00129
00130 if ( axis == Axes::X ) {
00131 double pos = getPosOn ( Axes::X );
00132 return Range ( 0.0, m_ntuple->rows (), pos );
00133 }
00134
00135 if ( m_columns[1] == UINT_MAX ) {
00136 return dataRange ( m_columns[0] );
00137 }
00138
00139 return dataRangeWithError ( m_columns[0], m_columns[1] );
00140 }
00141
00142 double
00143 Map1Projector::
00144 getPosOn ( hippodraw::Axes::Type axis ) const
00145 {
00146 assert ( axis == Axes::X || axis == Axes::Y );
00147
00148 if ( axis == Axes::X ) {
00149 return m_ntuple -> empty () ? DBL_MAX : 1.0;
00150 }
00151
00152 if ( m_columns[1] == UINT_MAX ) {
00153 return getPos ( m_columns[0] );
00154 }
00155
00156 return getPosWithError ( m_columns[0], m_columns[1] );
00157 }
00158
00159 const string & Map1Projector::getXLabel() const
00160 {
00161 return m_x_label;
00162 }
00163
00164 const string & Map1Projector::getYLabel ( bool ) const
00165 {
00166 return m_ntuple->getLabelAt( m_columns[0] );
00167 }
00168
00169 namespace dp = hippodraw::DataPoint2DTuple;
00170
00171
00172 double
00173 Map1Projector::
00174 getAverage ( hippodraw::Axes::Type axis ) const
00175 {
00176 Map1Projector * p = const_cast < Map1Projector * > ( this );
00177 p -> prepareValues ();
00178
00179 unsigned int col = 2;
00180 switch ( axis ) {
00181
00182 case Axes::X:
00183 col = dp::X;
00184 break;
00185
00186 case Axes::Y:
00187 col = dp::Y;
00188 break;
00189
00190 default:
00191 break;
00192 }
00193 assert ( col < 2 );
00194
00195 const DataSource * ntuple = getProjectedValues ();
00196 const vector < double > & data = ntuple -> getColumn ( col );
00197
00198 unsigned int size = ntuple -> rows ();
00199 double sum = 0.0;
00200 sum = accumulate ( data.begin(), data.end(), sum );
00201
00202 return sum / size;
00203 }
00204
00205 void Map1Projector::addPointReps()
00206 {
00207 m_pointreps.push_back ( "Symbol" );
00208 }
00209
00210 DataSource *
00211 Map1Projector::
00212 createNTuple () const
00213 {
00214 unsigned int columns = 4;
00215 NTuple * ntuple = new NTuple ( columns );
00216
00217 vector < string > labels;
00218 labels.push_back ( "X" );
00219 labels.push_back ( "Value" );
00220 labels.push_back ( dp::WIDTH );
00221 labels.push_back ( dp::ERROR );
00222
00223 ntuple->setLabels ( labels );
00224
00225 fillProjectedValues ( ntuple );
00226
00227 return ntuple;
00228 }
00229
00230 void
00231 Map1Projector::
00232 fillProjectedValues ( DataSource * ntuple, bool ) const
00233 {
00234 unsigned int y_col = m_columns[0];
00235 unsigned int y_err = m_columns[1];
00236 unsigned int size = m_ntuple -> rows ();
00237
00238 ntuple -> clear();
00239 ntuple -> reserve ( size );
00240
00241 vector < double > row ( dp::SIZE );
00242 for ( unsigned int i = 0; i < size; i++ ) {
00243 if ( acceptRow ( i ) == false ||
00244 inRange ( i ) == false ) continue;
00245 row[dp::X] = i;
00246 row[dp::Y] = m_ntuple -> valueAt ( i, y_col );
00247
00248 double ye
00249 = y_err < UINT_MAX ? (m_ntuple -> valueAt ( i, y_err ) ) : 0.0;
00250 row[dp::XERR] = 0.0;
00251 row[dp::YERR] = ye;
00252 ntuple -> addRow ( row );
00253 }
00254 }
00255
00256 void
00257 Map1Projector::
00258 fillDataSource ( DataSource * ntuple, bool ) const
00259 {
00260 unsigned int y_col = m_columns[0];
00261 unsigned int y_err = m_columns[1];
00262 unsigned int size = m_ntuple -> rows ();
00263
00264 ntuple -> clear();
00265 ntuple -> reserve ( size );
00266
00267 vector < double > row ( dp::SIZE );
00268 for ( unsigned int i = 0; i < size; i++ ) {
00269 if ( acceptRow ( i ) == false ||
00270 inRange ( i ) == false ) continue;
00271 row[dp::X] = i + 0.5;
00272 row[dp::Y] = m_ntuple -> valueAt ( i, y_col );
00273
00274 double ye
00275 = y_err < UINT_MAX ? (m_ntuple -> valueAt ( i, y_err ) ) : 0.0;
00276 row[dp::XERR] = 0.5;
00277 row[dp::YERR] = ye;
00278 ntuple -> addRow ( row );
00279 }
00280 }
00281
00282 void
00283 Map1Projector::
00284 prepareValues ()
00285 {
00286 if ( m_proj_values == 0 ) {
00287 m_proj_values = createNTuple ();
00288 } else {
00289 fillProjectedValues ( m_proj_values );
00290 }
00291
00292 setDirty ( false );
00293 }