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

Generated for HippoDraw Class Library by doxygen