FunctionRep.cxx

Go to the documentation of this file.
00001 
00012 // for truncation warning in debug mode
00013 #ifdef _MSC_VER
00014 #include "msdevstudio/MSconfig.h"
00015 #endif
00016 
00017 #include "FunctionRep.h"
00018 
00019 #include "FunctionParameter.h"
00020 
00021 #include "datasrcs/DataSource.h"
00022 #include "datasrcs/TupleCut.h"
00023 
00024 #include "functions/FunctionBase.h"
00025 #include "minimizers/Fitter.h"
00026 #include "projectors/FunctionProjector.h"
00027 #include "reps/CutRangeRep.h"
00028 
00029 #include <cassert>
00030 
00031 using std::string;
00032 using std::vector;
00033 
00034 using namespace hippodraw;
00035 
00036 FunctionRep::
00037 FunctionRep ( FunctionBase * function,DataRep * rep )
00038   : m_in_composite ( false )
00039 {
00040   m_name = "Function";
00041   m_target = rep;
00042 
00043   ProjectorBase * target = 0;
00044 
00045   if ( m_target != 0 ) {
00046     m_target -> addObserver ( this );
00047 
00048     const DataSource * ntuple = rep -> getProjectedValues ();
00049     DataSource * nt = const_cast < DataSource * > ( ntuple );
00050     nt -> addObserver ( this );
00051     target = rep -> getProjector ();
00052   }
00053 
00054   m_projector = new FunctionProjector ( function, target );
00055 
00056   // The following is needed to ensure FunctionProjector::principleErrors()
00057   // knows the correct number of parameters for this function.
00058   dynamic_cast<FunctionProjector *>(m_projector)->saveParameters();
00059 
00060 }
00061 
00062 FunctionRep::
00063 FunctionRep ( const FunctionRep & rep )
00064   : DataRep ( rep )
00065 {
00066   m_cut_rep = rep.m_cut_rep -> clone ();
00067 }
00068 
00069 FunctionRep::
00070 ~FunctionRep ()
00071 {
00072   if ( m_target != 0 ) {
00073     m_target -> removeObserver ( this );
00074 
00075     const DataSource * target = m_target -> getProjectedValues ();
00076     DataSource * nt = const_cast < DataSource * > ( target );
00077     nt -> removeObserver ( this );
00078   }
00079 }
00080 
00081 DataRep * FunctionRep::clone ()
00082 {
00083   return new FunctionRep ( *this );
00084 }
00085 
00086 bool FunctionRep::hasNTupleBindings () const
00087 {
00088   return false;
00089 }
00090 
00091 void FunctionRep::initializeWith ( DataRep * rep )
00092 {
00093   m_target = rep;
00094   ProjectorBase * projector = rep -> getProjector ();
00095 
00096   FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
00097 
00098   fp->initializeFunction ( projector );
00099 }
00100 
00101 FunctionBase * FunctionRep::getFunction () const
00102 {
00103   FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
00104 
00105   return fp->function ();
00106 }
00107 
00108 DataRep * FunctionRep::getTarget () const
00109 {
00110   return m_target;
00111 }
00112 
00113 void FunctionRep::setTarget ( DataRep * target )
00114 {
00115   m_target = target;
00116 }
00117 
00118 bool FunctionRep::isComposite () const
00119 {
00120   FunctionBase * function = getFunction();
00121   return function->isComposite ();
00122 }
00123 
00124 bool
00125 FunctionRep::
00126 isInComposite () const
00127 {
00128   return m_in_composite;
00129 }
00130 
00131 void
00132 FunctionRep::
00133 setInComposite ( bool yes )
00134 {
00135   m_in_composite = yes;
00136 }
00137 
00138 bool
00139 FunctionRep::
00140 setFitter ( Fitter * fitter )
00141 {
00142   if ( fitter -> needsIntegrated () == true ) {
00143     bool yes = m_target -> isAxisBinned ( Axes::X );
00144     if ( ! yes ) return false;
00145   }
00146 
00147   FunctionBase * function = getFunction ();
00148   fitter -> setFunction ( function );
00149 
00150   const DataSource * target = m_target -> getProjectedValues ();
00151   fitter -> setDataSource ( target );
00152 
00153   FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
00154   assert ( fp );
00155 
00156   fp -> setFitter ( fitter );
00157 
00158   return true;
00159 }
00160 
00161 Fitter *
00162 FunctionRep::
00163 getFitter ( ) const
00164 {
00165   FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
00166   assert ( fp );
00167 
00168   return fp -> getFitter ();
00169 }
00170 
00171 const string &
00172 FunctionRep::
00173 getFitterName () const
00174 {
00175   Fitter * fitter = getFitter ();
00176 
00177   return fitter -> name ();
00178 }
00179 
00180 bool FunctionRep::fitFunction ( )
00181 {
00182 
00183   FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
00184   assert ( fp );
00185 
00186   bool ok = fp->fitFunction ( );
00187   
00188   fp -> calcPrincipleErrors ();
00189   notifyObservers ();
00190 
00191   return ok;
00192 }
00193 
00194 double
00195 FunctionRep::
00196 objectiveValue () const
00197 {
00198   FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
00199 
00200   return fp -> objectiveValue ();
00201 }
00202 
00203 const vector < vector < double > > &
00204 FunctionRep::
00205 covarianceMatrix () const
00206 {
00207   FunctionProjector * projector 
00208     = dynamic_cast < FunctionProjector * > ( m_projector );
00209 
00210   return projector -> covariance ();
00211 }
00212 
00213 int
00214 FunctionRep::
00215 degreesOfFreedom () const
00216 {
00217   FunctionProjector * projector 
00218     = dynamic_cast < FunctionProjector * > ( m_projector );
00219 
00220   return projector -> degreesOfFreedom ();
00221 }
00222 
00223 const vector< double > &
00224 FunctionRep::
00225 principleErrors() const
00226 {
00227   FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
00228   assert ( fp );
00229   
00230   return fp->principleErrors();
00231 }
00232 
00233 void FunctionRep::saveParameters ( )
00234 {
00235   FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
00236   fp->saveParameters ();
00237 }
00238 
00239 void
00240 FunctionRep::
00241 setPrincipleErrors ( std::vector < double >::const_iterator begin, 
00242                      std::vector < double >::const_iterator end )
00243 {
00244   FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
00245 
00246   fp -> setPrincipleErrors ( begin, end );
00247 }
00248 
00249 void FunctionRep::restoreParameters ()
00250 {
00251   FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
00252   fp->restoreParameters ();
00253   notifyObservers ();
00254 }
00255 
00256 void FunctionRep::setParameters ( const std::vector<double> & params )
00257 {
00258   FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
00259   fp->setParameters (params);
00260   notifyObservers ();
00261 }
00262 
00263 void
00264 FunctionRep::
00265 setFixedFlags ( const std::vector < int > & flags )
00266 {
00267   Fitter * fitter = getFitter ();
00268   fitter -> setFixedFlags ( flags );
00269 }
00270 
00271 const vector < string > & FunctionRep::parmNames () const
00272 {
00273   FunctionBase * function = getFunction ();
00274 
00275   return function->parmNames ();
00276 }
00277 
00278 const vector < double > & FunctionRep::parameters () const
00279 {
00280   FunctionBase * function = getFunction ();
00281 
00282   return function->getParameters ();
00283 }
00284 
00285 const vector < int > &
00286 FunctionRep::
00287 getFixedFlags ( ) const
00288 {
00289   const Fitter * fitter = getFitter ();
00290 
00291   return fitter -> getFixedFlags ();
00292 }
00293 
00294 void
00295 FunctionRep::
00296 setIgnoreError(bool ignore)
00297 { 
00298 
00299   Fitter * fitter = getFitter ();
00300   if ( fitter != 0 ) {
00301     fitter -> setUseErrors ( ! ignore );
00302   }
00303 }
00304 
00305 void
00306 FunctionRep::
00307 willDelete ( const Observable * observed )
00308 {
00309   const DataRep * rep = dynamic_cast < const DataRep * > ( observed );
00310   if ( rep != 0 ) {
00311     m_target = 0;
00312     const DataSource * projvalues = rep -> getProjectedValues ();
00313     DataSource * source = const_cast < DataSource * > ( projvalues );
00314     source -> removeObserver ( this );
00315   }
00316 }
00317 
00318 bool
00319 FunctionRep::
00320 getIgnoreErrors() const
00321 {
00322   bool yes = false;
00323   Fitter * fitter = getFitter ();
00324   if (fitter !=0) {
00325     yes = ! fitter -> getUseErrors ();
00326   }
00327 
00328   return yes;
00329 }
00330 
00331 bool
00332 FunctionRep::
00333 isTargetable () const
00334 {
00335   return false;
00336 }
00337 
00338 void
00339 FunctionRep::
00340 setCutRange ( const Range & range )
00341 {
00342   if ( m_cut_rep == 0 ) {
00343     m_cut_rep = new CutRangeRep ();
00344     TupleCut cut;
00345     m_cut_rep -> addCut ( cut );
00346   }
00347 
00348   const vector < TupleCut > & cut_list = m_cut_rep -> getCuts ();
00349   const TupleCut & c = cut_list.front(); // to be index later
00350   TupleCut & cut = const_cast < TupleCut &> ( c );
00351 
00352   const ProjectorBase * target = m_target -> getProjector ();
00353   const string & label = target -> getXLabel ();
00354   cut.setLabel ( label );
00355   cut.setRange ( range );
00356 
00357   FunctionProjector * projector 
00358     = dynamic_cast < FunctionProjector * > ( m_projector );
00359   assert ( projector != 0 );
00360 
00361   projector -> setFitCut ( & cut );
00362   projector -> setFitRange ( true );
00363 }
00364 
00367 void
00368 FunctionRep::
00369 setCutRange ( bool yes )
00370 {
00371   FunctionProjector * projector 
00372     = dynamic_cast < FunctionProjector * > ( m_projector );
00373   assert ( projector != 0 );
00374 
00375   projector -> setFitRange ( yes );
00376 }
00377 
00378 void
00379 FunctionRep::
00380 setTupleCut ()
00381 {
00382   bool yes = m_target -> hasCut ();
00383   if ( yes ) {
00384     const vector < TupleCut > & cut_list = m_target -> getCuts ();
00385 
00386     const TupleCut & temp = cut_list.front ();
00387     TupleCut & cut = const_cast < TupleCut & > ( temp );
00388 
00389     FunctionProjector * projector 
00390       = dynamic_cast < FunctionProjector * > ( m_projector );
00391     assert ( projector != 0 );
00392 
00393     projector -> setFitCut ( & cut );
00394     projector -> setFitRange ( true );
00395   }
00396 }
00397 
00398 void
00399 FunctionRep::
00400 removeCut ()
00401 {
00402   FunctionProjector * projector 
00403     = dynamic_cast < FunctionProjector * > ( m_projector );
00404   projector -> setFitCut ( 0 );
00405   projector -> setFitRange ( false );
00406 
00407   DataRep::removeCut (); // If this object owns it
00408   m_target -> removeCut (); // if target owns it.
00409   m_target -> setDirty ( true );
00410 }
00411 
00412 bool
00413 FunctionRep::
00414 hasZoomY () const
00415 {
00416   return true;
00417 }
00418 
00419 void
00420 FunctionRep::
00421 fillFunctionParameters( std::vector<FunctionParameter> & function_parms) const
00422 {
00423   function_parms.clear ();
00424   const vector < string > & parm_names = parmNames ();
00425   const vector < double > & values = parameters ();
00426   const vector < double > & errors = principleErrors ();
00427   const vector < int > & flags = getFixedFlags ();
00428 
00429   unsigned int count = parm_names.size ();
00430   for ( unsigned int i = 0; i < count; i++ ) {
00431     FunctionParameter fp ( parm_names[i],
00432                            values[i],
00433                            errors[i],
00434                            flags[i] != 0,
00435                            0., // lower limit
00436                            0., // upper limit
00437                            false, // has lower
00438                            false ); // has upper
00439     function_parms.push_back ( fp );
00440   }
00441 }
00442 
00443 void
00444 FunctionRep::
00445 addToComposite ( FunctionRep * )
00446 {
00447   // does nothing
00448 }
00449 
00450 void
00451 FunctionRep::
00452 removeFromComposite ( FunctionRep * )
00453 {
00454   // does nothing
00455 }
00456 
00457 const string &
00458 FunctionRep::
00459 functionName () const
00460 {
00461   FunctionBase * function = getFunction ();
00462 
00463   return function -> name ();
00464 }

Generated for HippoDraw Class Library by doxygen