00001 00012 #include "Fitter.h" 00013 00014 #include "StatedFCN.h" 00015 00016 #include <climits> 00017 #include <cstdlib> 00018 #include <stdexcept> 00019 00020 using std::string; 00021 using std::vector; 00022 00023 using namespace hippodraw; 00024 00025 Fitter:: 00026 Fitter ( const char * name ) 00027 : m_name ( name ), 00028 m_fcn ( 0 ), 00029 m_max_iterations ( 100 ) 00030 { 00031 } 00032 00033 Fitter:: 00034 Fitter ( const Fitter & fitter ) 00035 : m_name ( fitter.m_name ), 00036 m_max_iterations ( fitter.m_max_iterations ) 00037 { 00038 if ( fitter.m_fcn != 0 ) m_fcn = fitter.m_fcn -> clone (); 00039 } 00040 00041 Fitter:: 00042 ~Fitter () 00043 { 00044 if ( m_fcn != 0 ) delete m_fcn; 00045 } 00046 00047 void 00048 Fitter:: 00049 copyFrom ( const Fitter * fitter ) 00050 { 00051 m_fcn -> copyFrom ( fitter -> m_fcn ); 00052 } 00053 00054 const std::string & 00055 Fitter:: 00056 name () const 00057 { 00058 return m_name; 00059 } 00060 00061 void 00062 Fitter:: 00063 setFCN ( StatedFCN * fcn ) 00064 { 00065 if ( m_fcn != 0 ) delete m_fcn; 00066 00067 m_fcn = fcn; 00068 } 00069 00070 StatedFCN * 00071 Fitter:: 00072 getFCN () 00073 { 00074 return m_fcn; 00075 } 00076 00077 bool 00078 Fitter:: 00079 isCompatible ( const FunctionBase * function ) const 00080 { 00081 bool yes = false; 00082 if ( m_fcn != 0 ) { 00083 yes = m_fcn -> isCompatible ( function ); 00084 } 00085 return yes; 00086 } 00087 00088 void 00089 Fitter:: 00090 setFunction ( FunctionBase * function ) 00091 { 00092 if ( m_fcn != 0 ) { 00093 m_fcn -> setFunction ( function ); 00094 } 00095 } 00096 00097 void 00098 Fitter:: 00099 setDataSource ( const DataSource * source ) 00100 { 00101 if ( m_fcn != 0 ) { 00102 m_fcn -> setDataSource ( source ); 00103 m_fcn -> setUseErrors (); 00104 } 00105 } 00106 00107 void 00108 Fitter:: 00109 setUseErrors ( bool yes ) 00110 { 00111 if ( m_fcn != 0 ) { 00112 m_fcn -> setUseErrors ( yes ); 00113 } 00114 } 00115 00116 bool 00117 Fitter:: 00118 getUseErrors () const 00119 { 00120 bool yes = false; 00121 if ( m_fcn != 0 ) { 00122 yes = m_fcn -> getUseErrors (); 00123 } 00124 00125 return yes; 00126 } 00127 00128 bool 00129 Fitter:: 00130 needsIntegrated () const 00131 { 00132 bool yes = true; 00133 if ( m_fcn != 0 ) { 00134 yes = m_fcn -> needsIntegrated (); 00135 } 00136 return yes; 00137 } 00138 00139 void 00140 Fitter:: 00141 fillFreeParameters ( std::vector < double > & free_parms) const 00142 { 00143 return m_fcn -> fillFreeParameters ( free_parms ); 00144 } 00145 00146 void 00147 Fitter:: 00148 setFixedFlags ( const std::vector < int > & flags ) 00149 { 00150 m_fcn -> setFixedFlags ( flags ); 00151 } 00152 00153 const vector < int > & 00154 Fitter:: 00155 getFixedFlags ( ) const 00156 { 00157 return m_fcn -> getFixedFlags (); 00158 } 00159 00160 void 00161 Fitter:: 00162 setLimits ( unsigned int, double, double ) 00163 { 00164 string what ( "The " ); 00165 what += m_name; 00166 what += " minimizer does not support limits on parameters"; 00167 throw std::runtime_error ( what ); 00168 } 00169 00170 unsigned int 00171 Fitter:: 00172 getParameterIndex ( const std::string & name ) 00173 { 00174 unsigned int index = UINT_MAX; 00175 const vector < string > & names = m_fcn -> getParmNames (); 00176 unsigned int size = names.size(); 00177 for ( unsigned int i = 0; i < size; i++ ) { 00178 const string & pname = names [i]; 00179 if ( pname == name ) { 00180 index = i; 00181 break; 00182 } 00183 } 00184 if ( index == UINT_MAX ) { 00185 string what ( "No parameter named `" ); 00186 what += name; 00187 what += "' for this function."; 00188 throw std::runtime_error ( what ); 00189 } 00190 00191 return index; 00192 } 00193 00194 void 00195 Fitter:: 00196 setLimits ( const std::string & name, double lower, double upper ) 00197 { 00198 unsigned int index = getParameterIndex ( name ); 00199 setLimits ( index, lower, upper ); 00200 } 00201 00202 void 00203 Fitter:: 00204 setStepSize ( unsigned int, double ) 00205 { 00206 string what ( "This " ); 00207 what += m_name; 00208 what += " minimizer does not support setting step size."; 00209 throw std::runtime_error ( what ); 00210 } 00211 00212 void 00213 Fitter:: 00214 setStepSize ( const std::string & name, double size ) 00215 { 00216 unsigned int index = getParameterIndex ( name ); 00217 setStepSize ( index, size ); 00218 } 00219 00220 double 00221 Fitter::objectiveValue () const 00222 { 00223 return m_fcn -> objectiveValue (); 00224 } 00225 00226 int 00227 Fitter:: 00228 calcDegreesOfFreedom () const 00229 { 00230 return m_fcn -> degreesOfFreedom(); 00231 } 00232 00233 int 00234 Fitter:: 00235 calcCovariance ( std::vector < std::vector < double > > & ) 00236 { 00237 return EXIT_FAILURE; 00238 } 00239 00240 void 00241 Fitter:: 00242 setFitCut ( TupleCut * cut ) 00243 { 00244 m_fcn -> setFitCut ( cut ); 00245 } 00246 00247 void 00248 Fitter:: 00249 setFitRange ( bool yes ) 00250 { 00251 m_fcn -> setFitRange ( yes ); 00252 }