00001
00012 #ifdef _MSC_VER
00013
00014 #include "msdevstudio/MSconfig.h"
00015 #endif
00016
00017 #include "ProfileProjector.h"
00018
00019 #include "axes/AxisModelBase.h"
00020
00021 #include "binners/BinsBase.h"
00022 #include "binners/BinsFactory.h"
00023 #include "binners/BinnerAxis.h"
00024 #include "binners/BinnerAxisFactory.h"
00025
00026 #include "datasrcs/DataPointTuple.h"
00027 #include "datasrcs/NTuple.h"
00028
00029 #include <algorithm>
00030
00031 #include <cassert>
00032
00033 using namespace hippodraw;
00034
00035 #include <stdio.h>
00036
00037 #ifdef ITERATOR_MEMBER_DEFECT
00038 using namespace std;
00039 #else
00040 using std::list;
00041 using std::max;
00042 using std::string;
00043 using std::vector;
00044 #endif
00045
00046 ProfileProjector::ProfileProjector( )
00047 : BinningProjector ( 1 ),
00048 NTupleProjector ( 3 )
00049 {
00050 m_binding_options.push_back ( "X" );
00051 m_binding_options.push_back ( "Y" );
00052 m_binding_options.push_back ( "Weight (optional)" );
00053 m_min_bindings = 2;
00054
00055 BinnerAxisFactory * binner_factory = BinnerAxisFactory::instance ();
00056 BinnerAxis * binner = binner_factory -> create ( "BinnerLinear" );
00057
00058 BinsFactory * factory = BinsFactory::instance();
00059 m_binner = factory -> create ( "Bins1DProfile" );
00060
00061 m_binner->setBinnerOn ( binner, Axes::X );
00062 addPointReps();
00063 }
00064
00069 ProfileProjector::
00070 ProfileProjector ( const ProfileProjector & projector )
00071 : ProjectorBase ( projector ),
00072 BinningProjector ( projector ),
00073 NTupleProjector ( projector )
00074 {
00075 addPointReps();
00076 }
00077
00078 ProjectorBase * ProfileProjector::clone()
00079 {
00080 return new ProfileProjector( *this );
00081 }
00082
00084 void ProfileProjector::changedNTuple()
00085 {
00086 unsigned int cols = m_ntuple->columns () - 1;
00087 if ( m_columns[0] > cols ) m_columns[0] = cols;
00088 if ( m_columns[1] > cols ) m_columns[1] = cols;
00089 if ( m_columns[2] > cols ) m_columns[2] = cols;
00090
00091 m_binner->setDirty ( );
00092 }
00093
00094 void ProfileProjector::execute ()
00095 {
00096 if ( m_ntuple->isNull () ) return;
00097
00098
00099 unsigned int & x_col = m_columns[0];
00100 unsigned int & y_col = m_columns[1];
00101 unsigned int & w_col = m_columns[2];
00102 unsigned int size = m_ntuple -> rows ();
00103
00104 bool have_weight = w_col < UINT_MAX;
00105
00106
00107
00108
00109 m_binner->reset ();
00110
00111 for ( unsigned int i = 0; i < size; i++ )
00112 {
00113 if ( acceptRow ( i, m_cut_list ) == false ) continue;
00114
00115 double x = m_ntuple -> valueAt ( i, x_col );
00116 double y = m_ntuple -> valueAt ( i, y_col );
00117 double w = 1.0;
00118 if ( have_weight) {
00119 w = m_ntuple -> valueAt ( i, w_col );
00120 }
00121 m_binner->accumulate( x, y, w );
00122 }
00123 }
00124
00125 Range ProfileProjector::valueRange () const
00126 {
00127 return dataRangeOn ( Axes::Y );
00128 }
00129
00130 namespace dp = hippodraw::DataPoint2DTuple;
00131
00132 Range
00133 ProfileProjector::
00134 dataRangeOn ( hippodraw::Axes::Type axis ) const
00135 {
00136 assert ( axis == Axes::X || axis == Axes::Y );
00137
00138 if ( axis == Axes::X ) {
00139 return dataRange ( m_columns[0] );
00140 }
00141
00142 ProfileProjector * p = const_cast<ProfileProjector *> ( this );
00143 p->prepareValues ();
00144
00145
00146 if ( m_proj_values -> empty () ) {
00147 return Range ( 0.0, 0.0 );
00148 }
00149 double max = DBL_MIN;
00150 double min = DBL_MAX;
00151
00152 const vector < double > & values = m_proj_values -> getColumn ( dp::Y );
00153 const vector < double > & errors = m_proj_values -> getColumn ( dp::YERR );
00154 for ( unsigned int i = 0; i < values.size(); i++ ) {
00155 double hi = values[i] + errors[i];
00156 double lo = values[i] - errors[i];
00157 max = std::max ( max, hi );
00158 min = std::min ( min, lo );
00159 }
00160
00161 return Range ( min, max );
00162 }
00163
00164 double
00165 ProfileProjector::
00166 getPosOn ( hippodraw::Axes::Type axis ) const
00167 {
00168 assert ( axis == Axes::X || axis == Axes::Y );
00169
00170 if ( axis == Axes::X ) {
00171 return getPos ( m_columns[0] );
00172 }
00173
00174
00175 if ( m_proj_values -> empty() ) {
00176 return DBL_MAX;
00177 }
00178
00179 double pos = DBL_MAX;
00180
00181 const vector < double > & values = m_proj_values -> getColumn ( dp::Y );
00182 const vector < double > & errors = m_proj_values -> getColumn ( dp::YERR );
00183 for ( unsigned int i = 0; i < values.size (); i++ ) {
00184 double lo = values[i] - errors[i];
00185 if ( lo > 0.0 &&
00186 lo < pos ) pos = lo;
00187 }
00188
00189 return pos;
00190 }
00191
00192
00193 bool ProfileProjector::isAxisBinned ( const std::string & axis ) const
00194 {
00195 if ( axis == m_binding_options[0] ) {
00196 return true;
00197 }
00198 return false;
00199 }
00200
00201 void ProfileProjector::addPointReps()
00202 {
00203 m_pointreps.push_back ( "Symbol" );
00204 }
00205
00206 void
00207 ProfileProjector::
00208 setRange ( hippodraw::Axes::Type axis, bool const_width )
00209 {
00210 assert ( m_binner );
00211 assert ( axis == Axes::X || axis == Axes::Y );
00212
00213 if ( axis == Axes::X ) {
00214 const Range & range = m_x_axis->getRange( false );
00215 if( m_x_axis->isLog() ) {
00216 if( range.low() < 0.0 ) return;
00217 m_x_axis->setRange ( range.low(), range.high(), getPosOn ( Axes::X ) );
00218 const Range & range2 = m_x_axis->getRange( false );
00219 setBinnerRange ( axis, range2, const_width );
00220 }
00221 else {
00222 setBinnerRange ( axis, range, const_width );
00223
00224 }
00225 }
00226 }
00227
00228 void
00229 ProfileProjector::
00230 setBinnerRange ( hippodraw::Axes::Type axis,
00231 const Range & range,
00232 bool const_width )
00233 {
00234 m_binner -> setRange ( axis, range, const_width );
00235 checkScaling ();
00236
00237 setDirty ( true );
00238 }
00239
00240 void
00241 ProfileProjector::
00242 update ( const Observable * object )
00243 {
00244 const DataSource * datasource
00245 = dynamic_cast < const DataSource * > ( object );
00246
00247 if ( datasource != 0 ) {
00248 NTupleProjector::update ( object );
00249 }
00250 else {
00251 BinningProjector::update ( object );
00252 }
00253 }
00254
00255 void
00256 ProfileProjector::
00257 willDelete ( const Observable * object )
00258 {
00259 const DataSource * datasource
00260 = dynamic_cast < const DataSource * > ( object );
00261
00262 if ( datasource != 0 ) {
00263 NTupleProjector::willDelete ( object );
00264 }
00265 else {
00266 BinningProjector::willDelete ( object );
00267 }
00268 }