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

Generated for HippoDraw Class Library by doxygen