#include "DFitFunctor.h" ClassImp(DFitFunctor) DFitFunctor::DFitFunctor(string locFitType, vector locInitParams, int locFuncColor) : dFitType(locFitType), dInitParams(locInitParams), dFuncColor(locFuncColor), dNumXPoints(1000), dAddFitToFitObjectFlag(true), dFunc(NULL) { dParamNames.assign(dInitParams.size(), ""); } DFitFunctor::DFitFunctor(string locFitType, TF1* locFunc) : dFitType(locFitType), dInitParams(vector()), dFuncColor(kBlack), dNumXPoints(1000), dAddFitToFitObjectFlag(true), dFunc(locFunc) { dParamNames.assign(dInitParams.size(), ""); } DFitFunctor::~DFitFunctor(void) { delete dFunc; } size_t DFitFunctor::Get_NumParams(void) const { if(dFunc != NULL) return dFunc->GetNpar(); return dInitParams.size(); } string DFitFunctor::Get_FitType(void) const { return dFitType; } vector DFitFunctor::Get_InitParams(void) const { return dInitParams; } vector DFitFunctor::Get_ParamNames(void) const { return dParamNames; } void DFitFunctor::Calc_CutRange(double locSignalWidthParameter, double& locCutRangeMin, double& locCutRangeMax) const { //to suppress compiler warnings locCutRangeMin = 0.0*locSignalWidthParameter; locCutRangeMax = 0.0*locSignalWidthParameter; } void DFitFunctor::Calc_Integral(double& locIntegral, double& locIntegralError) const { double locRangeMin, locRangeMax; dFunc->GetRange(locRangeMin, locRangeMax); Calc_Integral(locRangeMin, locRangeMax, locIntegral, locIntegralError); } void DFitFunctor::Calc_Integral(double locRangeMin, double locRangeMax, double& locIntegral, double& locIntegralError) const { #if ROOT_VERSION_CODE < ROOT_VERSION(6,0,0) locIntegral = dFunc->Integral(locRangeMin, locRangeMax, dFunc->GetParameters()); #else locIntegral = dFunc->Integral(locRangeMin, locRangeMax); #endif if(dCovarianceMatrix.GetNcols() > 0) locIntegralError = dFunc->IntegralError(locRangeMin, locRangeMax, dFunc->GetParameters(), dCovarianceMatrix.GetMatrixArray()); else locIntegralError = locIntegral/sqrt(locIntegral); //Poisson } void DFitFunctor::Set_CovarianceMatrix(const TMatrixDSym& locFullCovarianceMatrix, unsigned int locParamIndex) { unsigned int locNumParams = Get_NumParams(); dCovarianceMatrix.ResizeTo(locNumParams, locNumParams); for(unsigned int loc_j = 0; loc_j < locNumParams; ++loc_j) { for(unsigned int loc_k = 0; loc_k < locNumParams; ++loc_k) dCovarianceMatrix(loc_j, loc_k) = locFullCovarianceMatrix(locParamIndex + loc_j, locParamIndex + loc_k); } } void DFitFunctor::Create_Function(string locFuncName, double locFitRangeMin, double locFitRangeMax) //for stand-alone usage { double* locParamArray = new double[dInitParams.size()]; for(size_t loc_i = 0; loc_i < dInitParams.size(); ++loc_i) locParamArray[loc_i] = dInitParams[loc_i]; Create_Function(locFuncName, locFitRangeMin, locFitRangeMax, locParamArray); } void DFitFunctor::Create_Function(string locFuncName, double locFitRangeMin, double locFitRangeMax, double* locParamArray, double* locParamErrorArray) { dFunc = new TF1(locFuncName.c_str(), this, locFitRangeMin, locFitRangeMax, Get_NumParams(), "DFitFunctor"); dFunc->SetTitle(dFitType.c_str()); for(size_t loc_i = 0; loc_i < dParamNames.size(); ++loc_i) { if(dParamNames[loc_i] != "") dFunc->SetParName(loc_i, dParamNames[loc_i].c_str()); } dFunc->SetParameters(locParamArray); if(locParamErrorArray != NULL) dFunc->SetParErrors(locParamErrorArray); set::iterator locFixedIterator = dFixedParams.begin(); for(; locFixedIterator != dFixedParams.end(); ++locFixedIterator) dFunc->FixParameter(*locFixedIterator, locParamArray[*locFixedIterator]); map >::iterator locIterator = dParamLimits.begin(); for(; locIterator != dParamLimits.end(); ++locIterator) dFunc->SetParLimits(locIterator->first, locIterator->second.first, locIterator->second.second); dFunc->SetNpx(dNumXPoints); dFunc->SetLineColor(dFuncColor); } string DFitFunctor::Get_ParamStream(void) const { ostringstream locOutputStream; for(size_t loc_i = 0; loc_i < dParamNames.size(); ++loc_i) { locOutputStream << dParamNames[loc_i]; if(loc_i != (dParamNames.size() - 1)) locOutputStream << ", "; } locOutputStream << ":" << endl; for(size_t loc_i = 0; loc_i < dInitParams.size(); ++loc_i) { if(dFunc != NULL) locOutputStream << dFunc->GetParameter(loc_i); else locOutputStream << dInitParams[loc_i]; if(loc_i != (dInitParams.size() - 1)) locOutputStream << ", "; } locOutputStream << endl; return locOutputStream.str(); } void DFitFunctor::Fix_Parameter(size_t locParamIndex) { dFixedParams.insert(locParamIndex); dParamLimits.erase(locParamIndex); //must, or else fit won't fix it! }