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