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 "datasrcs/DataSource.h" 00020 #include "datasrcs/TupleCut.h" 00021 00022 #include "functions/FunctionBase.h" 00023 00024 #include "minimizers/Fitter.h" 00025 #include "minimizers/NTupleFCN.h" 00026 #include "projectors/FunctionProjector.h" 00027 #include "reps/LinePointRep.h" 00028 00029 using std::string; 00030 using std::vector; 00031 00032 FunctionRep::FunctionRep ( const std::string & name, DataRep * rep ) 00033 { 00034 m_target = rep; 00035 m_target -> addObserver ( this ); 00036 00037 const DataSource * ntuple = rep -> getProjectedValues (); 00038 DataSource * nt = const_cast < DataSource * > ( ntuple ); 00039 nt -> addObserver ( this ); 00040 00041 m_name = "Function"; 00042 00043 ProjectorBase * target = rep -> getProjector (); 00044 m_projector = new FunctionProjector ( name, target ); 00045 m_rep = new LinePointRep(); 00046 // The above deleted in ~DataRep() 00047 00048 m_rep->setColor ( Color::red ); 00049 00050 if ( name == "Linear Sum" ){ 00051 m_rep->setSize ( 2 ); 00052 m_rep->setColor ( Color::blue ); 00053 } 00054 } 00055 00056 FunctionRep::FunctionRep ( FunctionBase * function, DataRep * rep ) 00057 { 00058 m_target = rep; 00059 m_target -> addObserver ( this ); 00060 00061 const DataSource * ntuple = rep -> getProjectedValues (); 00062 DataSource * nt = const_cast < DataSource * > ( ntuple ); 00063 nt -> addObserver ( this ); 00064 00065 m_name = "Function"; 00066 00067 ProjectorBase * target = rep -> getProjector (); 00068 m_projector = new FunctionProjector ( function, target ); 00069 00070 // The following is needed to ensure FunctionProjector::principleErrors() 00071 // knows the correct number of parameters for this function. 00072 dynamic_cast<FunctionProjector *>(m_projector)->saveParameters(); 00073 00074 m_rep = new LinePointRep(); 00075 // The above deleted in ~DataRep() 00076 00077 m_rep->setColor ( Color::red ); 00078 } 00079 00080 FunctionRep:: 00081 FunctionRep ( const FunctionRep & rep ) 00082 : DataRep ( rep ) 00083 { 00084 CutList_t::const_iterator first = rep.m_cut_list.begin (); 00085 while ( first != m_cut_list.end () ) { 00086 m_cut_list.push_back ( *first++ ); 00087 } 00088 } 00089 00090 FunctionRep:: 00091 ~FunctionRep () 00092 { 00093 if ( m_target != 0 ) { 00094 m_target -> removeObserver ( this ); 00095 00096 const DataSource * target = m_target -> getProjectedValues (); 00097 DataSource * nt = const_cast < DataSource * > ( target ); 00098 nt -> removeObserver ( this ); 00099 } 00100 } 00101 00102 DataRep * FunctionRep::clone () 00103 { 00104 return new FunctionRep ( *this ); 00105 } 00106 00107 bool FunctionRep::hasNTupleBindings () const 00108 { 00109 return false; 00110 } 00111 00112 void FunctionRep::initializeWith ( DataRep * rep ) 00113 { 00114 m_target = rep; 00115 ProjectorBase * projector = rep -> getProjector (); 00116 00117 FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector); 00118 00119 fp->initializeFunction ( projector ); 00120 } 00121 00122 FunctionBase * FunctionRep::getFunction () const 00123 { 00124 FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector); 00125 00126 return fp->function (); 00127 } 00128 00129 DataRep * FunctionRep::getTarget () const 00130 { 00131 return m_target; 00132 } 00133 00134 void FunctionRep::setTarget ( DataRep * target ) 00135 { 00136 m_target = target; 00137 } 00138 00139 bool FunctionRep::isComposite () const 00140 { 00141 FunctionBase * function = getFunction(); 00142 return function->isComposite (); 00143 } 00144 00145 void 00146 FunctionRep:: 00147 setFitter ( Fitter * fitter ) 00148 { 00149 FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector); 00150 assert ( fp ); 00151 00152 fp -> setFitter ( fitter ); 00153 } 00154 00155 Fitter * 00156 FunctionRep:: 00157 getFitter ( ) const 00158 { 00159 FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector); 00160 assert ( fp ); 00161 00162 return fp -> getFitter (); 00163 } 00164 00168 const string & 00169 FunctionRep:: 00170 getFitterName () const 00171 { 00172 Fitter * fitter = getFitter (); 00173 00174 return fitter -> name (); 00175 } 00176 00177 bool FunctionRep::fitFunction ( ) 00178 { 00179 00180 FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector); 00181 assert ( fp ); 00182 00183 bool ok = fp->fitFunction ( ); 00184 00185 notifyObservers (); 00186 00187 return ok; 00188 } 00189 00190 const vector< double > & FunctionRep::principleErrors() const 00191 { 00192 FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector); 00193 assert ( fp ); 00194 00195 return fp->principleErrors(); 00196 } 00197 00198 void FunctionRep::saveParameters ( ) 00199 { 00200 FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector); 00201 fp->saveParameters (); 00202 } 00203 00204 void FunctionRep::restoreParameters () 00205 { 00206 FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector); 00207 fp->restoreParameters (); 00208 notifyObservers (); 00209 } 00210 00211 void FunctionRep::setParameters ( const std::vector<double> & params ) 00212 { 00213 FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector); 00214 fp->setParameters (params); 00215 notifyObservers (); 00216 } 00217 00218 void 00219 FunctionRep:: 00220 setFixedFlags ( const std::vector < int > & flags ) 00221 { 00222 Fitter * fitter = getFitter (); 00223 fitter -> setFixedFlags ( flags ); 00224 } 00225 00226 const vector < string > & FunctionRep::parmNames () const 00227 { 00228 FunctionBase * function = getFunction (); 00229 00230 return function->parmNames (); 00231 } 00232 00233 const vector < double > & FunctionRep::parameters () const 00234 { 00235 FunctionBase * function = getFunction (); 00236 00237 return function->getParameters (); 00238 } 00239 00240 const vector < int > & 00241 FunctionRep:: 00242 getFixedFlags ( ) const 00243 { 00244 const Fitter * fitter = getFitter (); 00245 00246 return fitter -> getFixedFlags (); 00247 } 00248 00253 void 00254 FunctionRep:: 00255 update ( const Observable * observed ) 00256 { 00257 const DataSource * ntuple = dynamic_cast < const DataSource * > ( observed ); 00258 if ( ntuple == 0 ) return; 00259 00260 Fitter * fitter = getFitter (); 00261 if ( fitter != 0 ) { 00262 StatedFCN * fcn = fitter -> getFCN (); 00263 NTupleFCN * ntfcn = dynamic_cast < NTupleFCN * > ( fcn ); 00264 ntfcn -> setUseErrors (); 00265 } 00266 00267 DataRep::update ( observed ); 00268 } 00269 00270 void 00271 FunctionRep:: 00272 willDelete ( const Observable * observed ) 00273 { 00274 const DataRep * rep = dynamic_cast < const DataRep * > ( observed ); 00275 if ( rep != 0 ) { 00276 m_target = 0; 00277 const DataSource * projvalues = rep -> getProjectedValues (); 00278 DataSource * source = const_cast < DataSource * > ( projvalues ); 00279 source -> removeObserver ( this ); 00280 } 00281 } 00282 00283 bool 00284 FunctionRep:: 00285 isTargetable () const 00286 { 00287 return false; 00288 } 00289 00290 using namespace hippodraw; 00291 00292 bool 00293 FunctionRep:: 00294 hasAxis ( hippodraw::Axes::Type axis ) const 00295 { 00296 return axis == Axes::X || axis == Axes::Y; 00297 } 00298 00299 void 00300 FunctionRep:: 00301 setCutRange ( const Range & range ) 00302 { 00303 if ( m_cut_list.empty () ) { 00304 TupleCut cut; 00305 m_cut_list.push_back ( cut ); 00306 } 00307 00308 TupleCut & cut = m_cut_list.front(); // to be index later 00309 const ProjectorBase * target = m_target -> getProjector (); 00310 const string & label = target -> getXLabel (); 00311 cut.setLabel ( label ); 00312 cut.setRange ( range ); 00313 00314 FunctionProjector * projector 00315 = dynamic_cast < FunctionProjector * > ( m_projector ); 00316 assert ( projector != 0 ); 00317 00318 projector -> setFitCut ( & cut ); 00319 projector -> setFitRange ( true ); 00320 } 00321 00322 void 00323 FunctionRep:: 00324 setCutRange ( bool yes ) 00325 { 00326 FunctionProjector * projector 00327 = dynamic_cast < FunctionProjector * > ( m_projector ); 00328 assert ( projector != 0 ); 00329 00330 projector -> setFitRange ( yes ); 00331 }