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

Generated for HippoDraw Class Library by doxygen