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