Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

DyHist1DProjector.cxx

Go to the documentation of this file.
00001 
00012 #ifdef HAVE_CONFIG_H
00013 #include "config.h"
00014 #else
00015 #ifdef _MSC_VER
00016 // Include max() and min() missing from MicroSoft Visual C++.
00017 #include "msdevstudio/MSconfig.h"
00018 #endif
00019 #endif
00020 
00021 #include "DyHist1DProjector.h"
00022 
00023 #include "axes/AxisModelBase.h"
00024 #include "binners/BinsBase.h"
00025 #include "datasrcs/DataPointTuple.h"
00026 #include "datasrcs/NTuple.h"
00027 
00028 #include <cassert>
00029 
00030 using namespace hippodraw;
00031 
00032 #ifdef ITERATOR_MEMBER_DEFECT
00033 using namespace std;
00034 #else
00035 using std::list;
00036 using std::max;
00037 using std::string;
00038 using std::vector;
00039 #endif
00040 
00041 DyHist1DProjector::DyHist1DProjector( )
00042   : Hist1DProjImp ( ),
00043     NTupleProjector ( 2 )
00044 {
00045   m_binding_options.push_back ( "X" );
00046   m_binding_options.push_back ( "Weight (optional)" );
00047   m_min_bindings = 1;
00048 }
00049 
00050 DyHist1DProjector::DyHist1DProjector ( const DyHist1DProjector & projector )
00051   : Hist1DProjImp ( projector ),
00052     NTupleProjector ( projector ),
00053     m_fixed ( projector.m_fixed )
00054 {
00055 }
00056 
00057 ProjectorBase * DyHist1DProjector::clone()
00058 {
00059   return new DyHist1DProjector( *this );
00060 }
00061 
00062 void DyHist1DProjector::changedNTuple()
00063 {
00064   unsigned int cols = m_ntuple->columns () - 1;
00065   if ( m_columns[0] > cols ) {
00066     m_columns[0] = cols;
00067   }
00068   if ( m_columns[1] > cols ) m_columns[1] = UINT_MAX;
00069 
00070   m_binner->setDirty ( );
00071 }
00072 
00076 void DyHist1DProjector::execute ()
00077 {
00078   if ( m_ntuple -> isNull () ) return;
00079 
00080   // Get the data and the optional weight column. 
00081   unsigned int column = m_columns[0];
00082   unsigned int weight = UINT_MAX;
00083   bool have_weight = m_columns[1] < UINT_MAX;
00084 
00085   if ( have_weight ) {
00086     weight = m_columns[1];
00087   }
00088 
00089   // Use integer indexing to ensure that it will take everything from the
00090   // same row, including the cut values.
00091   
00092   m_binner->reset ();
00093 
00094   unsigned int size = m_ntuple -> rows ();
00095   for ( unsigned int i = 0; i < size; i++ ) 
00096     {
00097       if ( acceptRow ( i ) == false ) continue;
00098       double x = m_ntuple -> valueAt ( i, column );
00099       double w = 1.0;
00100       if ( have_weight ) {
00101         w = m_ntuple -> valueAt ( i, weight );
00102       }
00103       m_binner->accumulate( x, w );
00104     }
00105 }
00106 
00107 double
00108 DyHist1DProjector::
00109 getPosOn ( hippodraw::Axes::Type axis ) const
00110 {
00111   assert ( axis == Axes::X || axis == Axes::Y );
00112 
00113   if ( axis == Axes::X ) {
00114     return getPos ( m_columns[0] );
00115   }
00116   // Y
00117 
00118   return getPosOnValue ();
00119 }
00120 
00121 Range
00122 DyHist1DProjector::
00123 dataRangeOn ( hippodraw::Axes::Type axis ) const
00124 {
00125   assert ( axis == Axes::X || axis == Axes::Y );
00126 
00127   if ( axis == Axes::X ) {
00128     return dataRange ( m_columns[0] );
00129   }
00130   // Y
00131   return dataRangeOnValue ();
00132 }
00133 
00134 const string & DyHist1DProjector::getYLabel ( bool density ) const
00135 {
00136   if ( density == false ) {
00137     bool scaling = m_y_axis->isScaling ();
00138     if ( scaling ) {
00139       return m_y_label_entries;
00140     }
00141   }
00142   // else
00143  return m_y_label_density;
00144 }
00145 
00146 namespace dp = hippodraw::DataPoint2DTuple;
00147 
00153 double
00154 DyHist1DProjector::
00155 getAverage ( hippodraw::Axes::Type axis ) const
00156 {
00157   assert ( axis == Axes::X || axis == Axes::Y );
00158 
00159   double sum = 0.0;
00160   double number = 0.0;
00161 
00162   string label = "";
00163 
00164   // Get the axis label.
00165   switch ( axis ) {
00166   case Axes::X:
00167     label = getXLabel();
00168     break;
00169   case Axes::Y:
00170     label = getYLabel();
00171     break;
00172   default:
00173     break;
00174   }
00175   
00176   // Get the NTuple.
00177   const DataSource * tuple = getNTuple();
00178   if ( tuple -> empty () || axis == Axes::Y ) {
00179 
00180     // The axis label is invalid.
00181     
00182     // This should not happen for DyHist1DProjector.
00183     if ( axis == Axes::X ) return 0.0;
00184     
00185     // Get the range.
00186     const Range & r = m_y_axis->getRange(false);
00187     
00188     double min = r.low()  * m_y_axis->getScaleFactor();
00189     double max = r.high() * m_y_axis->getScaleFactor();
00190 
00191     const vector < double > & values 
00192       = m_proj_values -> getColumn ( dp::Y );    
00193 
00194     for ( unsigned int i = 0; i < values.size(); i++ ) {
00195       double val = values[i];
00196       // Add value to sum if its within the range.
00197       if(val >= min && val <= max){    
00198         sum += val;
00199         number ++;
00200       }
00201     }
00202   }
00203   else {
00204 
00205     // The axis label is valid. Reimplementation from NTupleProjector.
00206     
00207     unsigned int col = tuple -> indexOf ( label );
00208     unsigned int size = tuple -> rows ();    
00209     const Range & range = getRange ( axis );
00210 
00211     for ( unsigned int i = 0; i < size; i++ ) {
00212       
00213       if(!acceptRow(i))continue;
00214       double value = tuple -> valueAt ( i, col );
00215       if ( range.includes ( value ) ) { 
00216         sum += value;
00217         number ++;
00218       }
00219       
00220     }
00221   
00222   }
00223   
00224   return (sum / number);
00225 
00226 }
00227 
00228 /* virtual */
00229 bool DyHist1DProjector::isAxisBinned ( const std::string & axis ) const
00230 {
00231   if ( axis == m_binding_options[0] ) {
00232     return true;
00233   }
00234   return false;
00235 }
00236 
00237 void
00238 DyHist1DProjector::
00239 setBinnerRange ( hippodraw::Axes::Type axis,
00240                  const Range & range,
00241                  bool const_width )
00242 {
00243   m_binner -> setRange ( axis, range, const_width );
00244   checkScaling ();
00245 
00246   setDirty ( true );
00247 }
00248 
00249 void
00250 DyHist1DProjector::
00251 update ( const Observable * object )
00252 {
00253   const DataSource * datasource 
00254     = dynamic_cast < const DataSource * > ( object );
00255 
00256   if ( datasource != 0 ) {
00257     NTupleProjector::update ( object );
00258   }
00259   else {
00260     BinningProjector::update ( object );
00261   }
00262 }
00263 
00264 void
00265 DyHist1DProjector::
00266 willDelete ( const Observable * object )
00267 {
00268   const DataSource * datasource 
00269     = dynamic_cast < const DataSource * > ( object );
00270 
00271   if ( datasource != 0 ) {
00272     NTupleProjector::willDelete ( object );
00273   }
00274   else {
00275     BinningProjector::willDelete ( object );
00276   }
00277 }

Generated for HippoDraw-1.14.8.5 by doxygen 1.4.3