#include "TROOT.h"
#include "TVirtualFitter.h"
#include "TPluginManager.h"
#include "TEnv.h"
#include "TInterpreter.h"
#include "Math/MinimizerOptions.h"
TVirtualFitter *TVirtualFitter::fgFitter = 0;
Int_t TVirtualFitter::fgMaxpar = 0;
TString TVirtualFitter::fgDefault = "";
ClassImp(TVirtualFitter)
#ifdef R__COMPLETE_MEM_TERMINATION
namespace {
struct TVirtualFitterCleanup {
~TVirtualFitterCleanup() {
delete TVirtualFitter::GetFitter();
}
};
TVirtualFitterCleanup cleanup;
}
#endif
TVirtualFitter::TVirtualFitter() :
fXfirst(0),
fXlast(0),
fYfirst(0),
fYlast(0),
fZfirst(0),
fZlast(0),
fNpoints(0),
fPointSize(0),
fCacheSize(0),
fCache(0),
fObjectFit(0),
fUserFunc(0),
fMethodCall(0),
fFCN(0)
{
}
TVirtualFitter::TVirtualFitter(const TVirtualFitter& tvf) :
TNamed(tvf),
fOption(tvf.fOption),
fXfirst(tvf.fXfirst),
fXlast(tvf.fXlast),
fYfirst(tvf.fYfirst),
fYlast(tvf.fYlast),
fZfirst(tvf.fZfirst),
fZlast(tvf.fZlast),
fNpoints(tvf.fNpoints),
fPointSize(tvf.fPointSize),
fCacheSize(tvf.fCacheSize),
fCache(tvf.fCache),
fObjectFit(tvf.fObjectFit),
fUserFunc(tvf.fUserFunc),
fMethodCall(tvf.fMethodCall),
fFCN(tvf.fFCN)
{
}
TVirtualFitter& TVirtualFitter::operator=(const TVirtualFitter& tvf)
{
if(this!=&tvf) {
TNamed::operator=(tvf);
fOption=tvf.fOption;
fXfirst=tvf.fXfirst;
fXlast=tvf.fXlast;
fYfirst=tvf.fYfirst;
fYlast=tvf.fYlast;
fZfirst=tvf.fZfirst;
fZlast=tvf.fZlast;
fNpoints=tvf.fNpoints;
fPointSize=tvf.fPointSize;
fCacheSize=tvf.fCacheSize;
fCache=tvf.fCache;
fObjectFit=tvf.fObjectFit;
fUserFunc=tvf.fUserFunc;
fMethodCall=tvf.fMethodCall;
fFCN=tvf.fFCN;
}
return *this;
}
TVirtualFitter::~TVirtualFitter()
{
delete fMethodCall;
delete [] fCache;
if ( fgFitter == this ) {
fgFitter = 0;
fgMaxpar = 0;
}
fMethodCall = 0;
fFCN = 0;
}
TVirtualFitter *TVirtualFitter::Fitter(TObject *obj, Int_t maxpar)
{
if (fgFitter && maxpar > fgMaxpar) {
delete fgFitter;
fgFitter = 0;
}
if (!fgFitter) {
TPluginHandler *h;
if (fgDefault.Length() == 0) fgDefault = gEnv->GetValue("Root.Fitter","Minuit");
if ((h = gROOT->GetPluginManager()->FindHandler("TVirtualFitter",fgDefault))) {
if (h->LoadPlugin() == -1)
return 0;
fgFitter = (TVirtualFitter*) h->ExecPlugin(1, maxpar);
fgMaxpar = maxpar;
}
}
if (fgFitter) fgFitter->SetObjectFit(obj);
return fgFitter;
}
void TVirtualFitter::GetConfidenceIntervals(Int_t , Int_t , const Double_t * , Double_t * , Double_t )
{
}
void TVirtualFitter::GetConfidenceIntervals(TObject * , Double_t )
{
}
const char *TVirtualFitter::GetDefaultFitter()
{
return ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str();
}
TVirtualFitter *TVirtualFitter::GetFitter()
{
return fgFitter;
}
Int_t TVirtualFitter::GetMaxIterations()
{
return ROOT::Math::MinimizerOptions::DefaultMaxFunctionCalls();
}
Double_t TVirtualFitter::GetErrorDef()
{
return ROOT::Math::MinimizerOptions::DefaultErrorDef();
}
Double_t TVirtualFitter::GetPrecision()
{
return ROOT::Math::MinimizerOptions::DefaultTolerance();
}
void TVirtualFitter::SetDefaultFitter(const char *name)
{
ROOT::Math::MinimizerOptions::SetDefaultMinimizer(name,"");
if (fgDefault == name) return;
delete fgFitter;
fgFitter = 0;
fgDefault = name;
}
void TVirtualFitter::SetFitter(TVirtualFitter *fitter, Int_t maxpar)
{
fgFitter = fitter;
fgMaxpar = maxpar;
}
void TVirtualFitter::SetFCN(void (*fcn)(Int_t &, Double_t *, Double_t &f, Double_t *, Int_t))
{
fFCN = fcn;
}
void InteractiveFCN(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag)
{
TMethodCall *m = TVirtualFitter::GetFitter()->GetMethodCall();
if (!m) return;
Long_t args[5];
args[0] = (Long_t)∦
args[1] = (Long_t)gin;
args[2] = (Long_t)&f;
args[3] = (Long_t)u;
args[4] = (Long_t)flag;
m->SetParamPtrs(args);
Double_t result;
m->Execute(result);
}
Double_t *TVirtualFitter::SetCache(Int_t npoints, Int_t psize)
{
if (npoints*psize > fCacheSize) {
delete [] fCache;
fCacheSize = npoints*psize;
fCache = new Double_t[fCacheSize];
}
fNpoints = npoints;
fPointSize = psize;
return fCache;
}
void TVirtualFitter::SetFCN(void *fcn)
{
if (!fcn) return;
const char *funcname = gCint->Getp2f2funcname(fcn);
if (funcname) {
delete fMethodCall;
fMethodCall = new TMethodCall();
fMethodCall->InitWithPrototype(funcname,"Int_t&,Double_t*,Double_t&,Double_t*,Int_t");
}
fFCN = InteractiveFCN;
}
void TVirtualFitter::SetMaxIterations(Int_t niter)
{
ROOT::Math::MinimizerOptions::SetDefaultMaxFunctionCalls(niter);
}
void TVirtualFitter::SetErrorDef(Double_t errdef)
{
ROOT::Math::MinimizerOptions::SetDefaultErrorDef(errdef);
if (!fgFitter) return;
Double_t arglist[1];
arglist[0] = errdef;
fgFitter->ExecuteCommand("SET ERRORDEF", arglist, 1);
}
void TVirtualFitter::SetPrecision(Double_t prec)
{
ROOT::Math::MinimizerOptions::SetDefaultTolerance(prec);
}