/*! \file Fitting.cxx * \brief subroutines for fitting functions to data. */ #include #include using namespace std; namespace Math { ///\todo must alter so doesn't hang that often but I have no idea what could be taking so long, what hangs Double_t FitNonLinLS(const math_function fitfunc, const math_function *difffuncs, const int nparams, Double_t *params, GMatrix &covar, const int npoints, const Double_t x[], const Double_t y[], GMatrix *W, Double_t error, Double_t cl, int *fixparam, int binned, int maxiit, int iestimateerror, bool icallgsl) { Double_t chi2; #ifdef HAVE_GSL22 if (icallgsl) { chi2 = FitNonLinLSWithGSL(fitfunc, difffuncs, nparams, params, covar, npoints, x, y, W, error, cl, fixparam, binned, maxiit, iestimateerror); } else { chi2 = FitNonLinLSNoGSL(fitfunc, difffuncs, nparams, params, covar, npoints, x, y, W, error, cl, fixparam, binned, maxiit, iestimateerror); } #else chi2 = FitNonLinLSNoGSL(fitfunc, difffuncs, nparams, params, covar, npoints, x, y, W, error, cl, fixparam, binned, maxiit, iestimateerror); #endif return chi2; } Double_t FitNonLinLSNoGSL(const math_function fitfunc, const math_function *difffuncs, const int nparams, Double_t *params, GMatrix &covar, const int npoints, const Double_t x[], const Double_t y[], GMatrix *W, Double_t error, Double_t cl, int *fixparam, int binned, int maxiit, int iestimateerror) { int iit=0,iflag,npar=0; int parlist[nparams];//store list of all unfixed parameters //first check if some parameters are held fixed if (fixparam!=NULL) { for (int i=0;ioldchi2) { lambda*=10.0; for (int j=0;j0.1) {return -1;} }while (iflag==0&&iit<=maxiit); //if a fit was found meeting the expect variation requirements and is a local minimum (code does not guarantee finding global minimum) //then do one last fit with lambda =0; There appears to be an issue with whether lambda must be zero for appropriate fit. //and determine covariance matrix for parameters. //An estimate of the covariance matrix is the inverse of (JT*W*J) which should give you 1/sigma^2df/dPi*df/dPj //more accurate estimate requires determining the hessian matrix and inverting. Must make more robust call when determining covariance matrix //the parameters //if iterations have not converged print warning if (iit>maxiit) return -1; oldchi2=chi2; if (iestimateerror==1) { for (int i=0;ioldchi2) { for (int j=0;j data; fitdata.y = y_gsl -> data; fitdata.npar = npar; fitdata.iparindex = &parlist[0]; fitdata.nallparams = nparams; fitdata.allparams = allparams_gsl->data; //parameters related to gsl fitting, set to default gsl_multifit_nlinear_parameters gsl_fitting_params = gsl_multifit_nlinear_default_parameters(); //set workspace gsl_multifit_nlinear_workspace *workspace_gsl = gsl_multifit_nlinear_alloc(T_gsl, &gsl_fitting_params, npoints, npar); gsl_vector *res_gsl = gsl_multifit_nlinear_residual(workspace_gsl); gsl_vector *curparam_gsl = gsl_multifit_nlinear_position(workspace_gsl); for (auto i=0;i::infinity(); } // store cond(J(x)) //gsl_multifit_nlinear_rcond(&rcond, work); //store results for (int i=0;i