00001 // 00002 // $Id: Base_Constrainer.h,v 1.1 2011/05/26 09:46:52 mseidel Exp $ 00003 // 00004 // File: hitfit/Base_Constrainer.h 00005 // Purpose: Abstract base for the chisq fitter classes. 00006 // This allows for different algorithms to be used. 00007 // Created: Jul, 2000, sss, based on run 1 mass analysis code. 00008 // 00009 // CMSSW File : interface/Base_Constrainer.h 00010 // Original Author : Scott Stuart Snyder <snyder@bnl.gov> for D0 00011 // Imported to CMSSW by Haryo Sumowidagdo <Suharyo.Sumowidagdo@cern.ch> 00012 00038 #ifndef HITFIT_BASE_CONSTRAINER_H 00039 #define HITFIT_BASE_CONSTRAINER_H 00040 00041 00042 #include "TopQuarkAnalysis/TopHitFit/interface/matutil.h" 00043 #include <iosfwd> 00044 00045 00046 namespace hitfit { 00047 00048 class Defaults; 00049 00050 00051 //************************************************************************* 00052 00053 00059 class Base_Constrainer_Args 00060 // 00061 // Purpose: Hold on to parameters for the Base_Constrainer class. 00062 // 00063 // Parameters: 00064 // bool test_gradient - If true, check the constraint gradient calculations 00065 // by also doing them numerically. 00066 // float test_step - When test_gradient is true, the step size to use 00067 // for numeric differentiation. 00068 // float test_eps - When test_gradient is true, the maximum relative 00069 // difference permitted between returned and 00070 // numerically calculated gradients. 00071 // 00072 { 00073 public: 00074 // Constructor. Initialize from a Defaults object. 00075 00085 Base_Constrainer_Args (const Defaults& defs); 00086 00087 // Retrieve parameter values. 00091 bool test_gradient () const; 00092 00096 double test_step () const; 00097 00101 double test_eps () const; 00102 00103 00104 private: 00105 00106 // Hold on to parameter values. 00107 00112 bool _test_gradient; 00113 00118 double _test_step; 00119 00124 double _test_eps; 00125 00126 }; 00127 00128 00129 //************************************************************************* 00130 00131 00140 class Constraint_Calculator 00141 // 00142 // Purpose: Abstract base class for evaluating constraints. 00143 // Derive from this and implement the eval() method. 00144 // 00145 { 00146 public: 00147 // Constructor, destructor. Pass in the number of constraints. 00148 00153 Constraint_Calculator (int nconstraints); 00154 00158 virtual ~Constraint_Calculator () {} 00159 00160 // Get back the number of constraints. 00164 int nconstraints () const; 00165 00166 // Evaluate constraints at the point described by X and Y (well-measured 00167 // and poorly-measured variables, respectively). The results should 00168 // be stored in F. BX and BY should be set to the gradients of F with 00169 // respect to X and Y, respectively. 00170 // 00171 // Return true if the point X, Y is accepted. 00172 // Return false if it is rejected (i.e., in an unphysical region). 00173 // The constraints need not be evaluated in that case. 00174 00205 virtual bool eval (const Column_Vector& x, 00206 const Column_Vector& y, 00207 Row_Vector& F, 00208 Matrix& Bx, 00209 Matrix& By) = 0; 00210 00211 00212 private: 00213 // The number of constraint functions. 00217 int _nconstraints; 00218 00219 }; 00220 00221 00222 //************************************************************************* 00223 00224 00229 class Base_Constrainer 00230 // 00231 // Purpose: Base class for chisq constrained fitter. 00232 // 00233 { 00234 public: 00235 // Constructor, destructor. 00236 // ARGS holds the parameter settings for this instance. 00237 00242 Base_Constrainer (const Base_Constrainer_Args& args); 00243 00247 virtual ~Base_Constrainer () {} 00248 00249 // Do the fit. 00250 // Call the number of well-measured variables Nw, the number of 00251 // poorly-measured variables Np, and the number of constraints Nc. 00252 // Inputs: 00253 // CONSTRAINT_CALCULATOR is the object that will be used to evaluate 00254 // the constraints. 00255 // XM(Nw) and YM(Np) are the measured values of the well- and 00256 // poorly-measured variables, respectively. 00257 // X(Nw) and Y(Np) are the starting values for the fit. 00258 // G_I(Nw,Nw) is the error matrix for the well-measured variables. 00259 // Y(Np,Np) is the inverse error matrix for the poorly-measured variables. 00260 // 00261 // Outputs: 00262 // X(Nw) and Y(Np) is the point at the minimum. 00263 // PULLX(Nw) and PULLY(Np) are the pull quantities. 00264 // Q(Nw,Nw), R(Np,Np), and S(Nw,Np) are the final error matrices 00265 // between all the variables. 00266 // 00267 // The return value is the final chisq. Returns a value < 0 if the 00268 // fit failed to converge. 00269 00321 virtual double fit (Constraint_Calculator& constraint_calculator, 00322 const Column_Vector& xm, 00323 Column_Vector& x, 00324 const Column_Vector& ym, 00325 Column_Vector& y, 00326 const Matrix& G_i, 00327 const Diagonal_Matrix& Y, 00328 Column_Vector& pullx, 00329 Column_Vector& pully, 00330 Matrix& Q, 00331 Matrix& R, 00332 Matrix& S) = 0; 00333 00334 // Print out any internal state to S. 00344 virtual std::ostream& print (std::ostream& s) const; 00345 00346 // Print out internal state to S. 00347 friend std::ostream& operator<< (std::ostream& s, const Base_Constrainer& f); 00348 00349 00350 private: 00351 // Parameter settings. 00352 00356 const Base_Constrainer_Args _args; 00357 00358 00359 protected: 00360 // Helper function to evaluate the constraints. 00361 // This takes care of checking what the user function returns against 00362 // numerical derivatives, if that was requested. 00363 00403 bool call_constraint_fcn (Constraint_Calculator& constraint_calculator, 00404 const Column_Vector& x, 00405 const Column_Vector& y, 00406 Row_Vector& F, 00407 Matrix& Bx, 00408 Matrix& By) const; 00409 }; 00410 00411 00412 } // namespace hitfit 00413 00414 00415 #endif // not HITFIT_BASE_CONSTRAINER_H 00416