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