FunctionProjector.cxx

Go to the documentation of this file.
00001 
00012 // For truncation warning
00013 #ifdef _MSC_VER
00014 #include "msdevstudio/MSconfig.h"
00015 #endif
00016 
00017 #include "FunctionProjector.h"
00018 
00019 #include "ProjectorHelper.h"
00020 
00021 #include "axes/AxisModelBase.h"
00022 #include "datasrcs/NTuple.h"
00023 #include "functions/FunctionBase.h"
00024 #include "minimizers/Fitter.h"
00025 
00026 #include <cmath>
00027 
00028 #include <cassert>
00029 
00030 using std::copy;
00031 using std::list;
00032 using std::string;
00033 using std::vector;
00034 
00035 using namespace hippodraw;
00036 
00037 string FunctionProjector::s_x_label ("X");
00038 
00039 FunctionProjector::
00040 FunctionProjector ( FunctionBase * function,
00041                     ProjectorBase * targetProjector )
00042    :  ProjectorBase (),
00043       m_target ( targetProjector ),
00044       m_fitter ( 0 )
00045 {
00046   m_function = function;
00047   assert ( m_function != 0 );
00048   m_z_label = function -> name ();
00049 }
00050 
00052 FunctionProjector::
00053 FunctionProjector ( const FunctionProjector & projector )
00054   : ProjectorBase ( projector )
00055 {
00056 }
00057 
00058 FunctionProjector::~FunctionProjector ()
00059 {
00060   delete m_function;
00061   delete m_fitter;
00062 }
00063 
00065 ProjectorBase * FunctionProjector::clone ()
00066 {
00067   return 0;
00068 }
00069 
00070 FunctionBase * FunctionProjector::function () const
00071 {
00072   return m_function;
00073 }
00074 
00075 bool FunctionProjector:: hasSavedParameters () const
00076 {
00077   return m_save_parms.empty() == false;
00078 }
00079 
00080 void FunctionProjector::saveParameters ()
00081 {
00082   assert ( m_function != 0 );
00083 
00084   m_save_parms = m_function->getParameters ();
00085   m_principleErrors.resize ( m_save_parms.size(), 0. );
00086 }
00087 
00088 void FunctionProjector::restoreParameters ()
00089 {
00090   assert ( m_function != 0 );
00091   assert ( ! m_save_parms.empty () );
00092 
00093   m_function->setParameters ( m_save_parms );
00094 
00095   setDirty ( true );
00096 }
00097 
00098 void
00099 FunctionProjector::
00100 setFitter ( Fitter * fitter )
00101 {
00102   m_fitter = fitter;
00103 }
00104 
00105 Fitter *
00106 FunctionProjector::
00107 getFitter ( ) const
00108 {
00109   return m_fitter;
00110 }
00111 
00112 void
00113 FunctionProjector::
00114 initializeFunction ( ProjectorBase * projector )
00115 {
00116   assert ( m_function != 0 );
00117 
00118   m_target = projector;
00119   if ( m_target != 0 ) {
00120     projector->prepareValues();
00121     ProjectorHelper helper ( projector -> getProjectedValues () );
00122     m_function->initialParameters ( & helper );
00123   }
00124 
00125   saveParameters ();
00126 }
00127 
00128 bool
00129 FunctionProjector::
00130 fitFunction ( )
00131 {
00132   assert ( m_function != 0 && m_target != 0 );
00133   assert ( m_fitter != 0 );
00134   m_target -> prepareValues ();
00135 
00136   if ( hasSavedParameters() == false ) {
00137     saveParameters ();
00138   }
00139   // Calculate the best fit
00140   bool ok = m_fitter->calcBestFit ();
00141   
00142   setDirty ( true );
00143   
00144   return ok;
00145 }
00146 
00147 const string & FunctionProjector::getTitle() const
00148 {
00149   return m_function->name ();
00150 } 
00151 
00152 void
00153 FunctionProjector::
00154 setRange ( hippodraw::Axes::Type, bool )
00155 {
00156   setDirty ( true );
00157 }
00158 
00159 const std::string & FunctionProjector::getXLabel () const
00160 {
00161   return s_x_label;
00162 }
00163 
00164 const string & FunctionProjector::getYLabel ( bool ) const
00165 {
00166   return m_function->name ();
00167 }
00168 
00170 Range FunctionProjector::valueRange() const
00171 {
00172   return Range ( 0, 0 );
00173 }
00174 
00175 int
00176 FunctionProjector::
00177 getNumberOfEntries () const
00178 {
00179   // A call to this function for a FunctionProjector is meaningless.
00180   // Thus it returns meaningless negative number
00181   return -1;
00182 }
00183 
00184 int
00185 FunctionProjector::
00186 getUnderflow () const
00187 {
00188   return -1;
00189 }
00190 
00191 int 
00192 FunctionProjector::
00193 getOverflow () const
00194 {
00195   return -1;
00196 }
00197 
00198 Range
00199 FunctionProjector::
00200 dataRangeOn ( hippodraw::Axes::Type axis ) const
00201 {
00202   AxisModelBase * model = 0;
00203   switch ( axis ) {
00204   case Axes::X :
00205     model = m_x_axis;
00206     break;
00207   case Axes::Y :
00208     model = m_y_axis;
00209     break;
00210   case Axes::Z :
00211     model = m_z_axis;
00212     break;
00213   default:
00214     assert ( false );
00215   }
00216 
00217   return model -> getRange ( false );
00218 }
00219 
00220 double FunctionProjector::objectiveValue () const
00221 {
00222   return m_fitter->objectiveValue ();
00223 }
00224 
00225 void
00226 FunctionProjector::
00227 calcPrincipleErrors() const
00228 {
00229   int num_parms = m_save_parms.size ();
00230   m_principleErrors.clear();
00231   m_principleErrors.resize( num_parms, 0.0 );
00232   if ( m_fitter != 0 ) {
00233     m_fitter -> calcCovariance ( m_covariance );
00234     int n = m_covariance.size();
00235     if ( n != 0 ) {
00236       const vector < int > & fixed = m_fitter -> getFixedFlags ();
00237       int ic = 0;
00238       for( int i = 0; i < num_parms; i++ ) {
00239         if ( fixed [ i ] == 0 ) {
00240           m_principleErrors[i] = sqrt( m_covariance[ic][ic] );
00241           ic++;
00242         }
00243         else {
00244           m_principleErrors[i] = 0.0;
00245         }
00246       }
00247     }
00248   }
00249 }
00250 
00251 const vector < double > & 
00252 FunctionProjector::
00253 principleErrors() const
00254 {
00255   return m_principleErrors;
00256 }
00257 
00258 void
00259 FunctionProjector::
00260 setPrincipleErrors ( std::vector < double > :: const_iterator begin,
00261                      std::vector < double > :: const_iterator end )
00262 {
00263   unsigned int size = std::distance ( begin, end );
00264   assert ( size == m_principleErrors.size () );
00265 
00266   copy ( begin, end, m_principleErrors.begin() );
00267 }
00268 
00269 int FunctionProjector::degreesOfFreedom () const
00270 {
00271   return m_fitter->calcDegreesOfFreedom ();
00272 }
00273 
00274 double
00275 FunctionProjector::
00276 getPosOn ( hippodraw::Axes::Type ) const
00277 {
00278   return 0.0;
00279 }
00280 
00281 const string & FunctionProjector::getZLabel() const
00282 {
00283   return m_z_label;
00284 }
00285 
00286 void FunctionProjector::addPointReps()
00287 {
00288 }
00289 
00290 bool
00291 FunctionProjector::
00292 isEmpty () const
00293 {
00294   return m_function == 0;
00295 }
00296 
00297 void FunctionProjector::setParameters ( const std::vector<double> & params )
00298 {
00299   assert ( m_function != 0 );
00300 
00301   std::vector<double> myParams = m_function->getParameters();
00302 
00303   if ( myParams.size() == params.size() ) {
00304      m_function->setParameters ( params );
00305      setDirty ( true );
00306   }
00307 }
00308 
00309 const vector < vector < double > > &
00310 FunctionProjector::
00311 covariance ( ) const
00312 {
00313   if ( m_covariance.empty () ) {
00314     m_fitter -> calcCovariance ( m_covariance );
00315   }
00316 
00317   return m_covariance;
00318 }
00319 
00320 DataSource *
00321 FunctionProjector::
00322 createNTuple () const
00323 {
00324   // does nothing
00325   return 0;
00326 }
00327 
00328 void
00329 FunctionProjector::
00330 fillProjectedValues ( DataSource *, bool ) const
00331 {
00332   // does nothing.
00333 }
00334 
00335 void
00336 FunctionProjector::
00337 setFitCut ( TupleCut * cut )
00338 {
00339   m_fitter -> setFitCut ( cut );
00340 }
00341 
00342 void
00343 FunctionProjector::
00344 setFitRange ( bool yes )
00345 {
00346   m_fitter -> setFitRange ( yes );
00347 }

Generated for HippoDraw Class Library by doxygen