TVirtualFitter.cxx

Go to the documentation of this file.
00001 // @(#)root/mathcore:$Id: TVirtualFitter.cxx 37531 2010-12-10 20:38:06Z pcanal $
00002 // Author: Rene Brun   31/08/99
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 
00013 //______________________________________________________________________________
00014 //
00015 //     Abstract Base Class for Fitting
00016 
00017 #include "TROOT.h"
00018 #include "TVirtualFitter.h"
00019 #include "TPluginManager.h"
00020 #include "TEnv.h"
00021 #include "TInterpreter.h"
00022 #include "Math/MinimizerOptions.h"
00023 
00024 
00025 TVirtualFitter *TVirtualFitter::fgFitter    = 0;
00026 Int_t           TVirtualFitter::fgMaxpar    = 0;
00027 // Int_t           TVirtualFitter::fgMaxiter   = 5000;
00028 // Double_t        TVirtualFitter::fgPrecision = 1e-6;
00029 // Double_t        TVirtualFitter::fgErrorDef  = 1;
00030 TString         TVirtualFitter::fgDefault   = "";
00031 
00032 ClassImp(TVirtualFitter)
00033 
00034 #ifdef R__COMPLETE_MEM_TERMINATION
00035 namespace {
00036    struct TVirtualFitterCleanup {
00037       ~TVirtualFitterCleanup() {
00038          delete TVirtualFitter::GetFitter();
00039       }
00040    };
00041    TVirtualFitterCleanup cleanup;
00042 }
00043 #endif
00044 
00045 //______________________________________________________________________________
00046 TVirtualFitter::TVirtualFitter() : 
00047    fXfirst(0), 
00048    fXlast(0), 
00049    fYfirst(0), 
00050    fYlast(0), 
00051    fZfirst(0), 
00052    fZlast(0), 
00053    fNpoints(0),
00054    fPointSize(0),
00055    fCacheSize(0),
00056    fCache(0),
00057    fObjectFit(0),
00058    fUserFunc(0),
00059    fMethodCall(0),
00060    fFCN(0)
00061 {
00062    // Default constructor.
00063 }
00064 
00065 //______________________________________________________________________________
00066 TVirtualFitter::TVirtualFitter(const TVirtualFitter& tvf) :
00067   TNamed(tvf),
00068   fOption(tvf.fOption),
00069   fXfirst(tvf.fXfirst),
00070   fXlast(tvf.fXlast),
00071   fYfirst(tvf.fYfirst),
00072   fYlast(tvf.fYlast),
00073   fZfirst(tvf.fZfirst),
00074   fZlast(tvf.fZlast),
00075   fNpoints(tvf.fNpoints),
00076   fPointSize(tvf.fPointSize),
00077   fCacheSize(tvf.fCacheSize),
00078   fCache(tvf.fCache),
00079   fObjectFit(tvf.fObjectFit),
00080   fUserFunc(tvf.fUserFunc),
00081   fMethodCall(tvf.fMethodCall),
00082   fFCN(tvf.fFCN)
00083 {
00084    //copy constructor
00085 }
00086 
00087 //______________________________________________________________________________
00088 TVirtualFitter& TVirtualFitter::operator=(const TVirtualFitter& tvf)
00089 {
00090    //assignment operator
00091    if(this!=&tvf) {
00092       TNamed::operator=(tvf);
00093       fOption=tvf.fOption;
00094       fXfirst=tvf.fXfirst;
00095       fXlast=tvf.fXlast;
00096       fYfirst=tvf.fYfirst;
00097       fYlast=tvf.fYlast;
00098       fZfirst=tvf.fZfirst;
00099       fZlast=tvf.fZlast;
00100       fNpoints=tvf.fNpoints;
00101       fPointSize=tvf.fPointSize;
00102       fCacheSize=tvf.fCacheSize;
00103       fCache=tvf.fCache;
00104       fObjectFit=tvf.fObjectFit;
00105       fUserFunc=tvf.fUserFunc;
00106       fMethodCall=tvf.fMethodCall;
00107       fFCN=tvf.fFCN;
00108    }
00109    return *this;
00110 }
00111 
00112 //______________________________________________________________________________
00113 TVirtualFitter::~TVirtualFitter()
00114 {
00115    // Cleanup virtual fitter.
00116 
00117    delete fMethodCall;
00118    delete [] fCache;
00119    if ( fgFitter == this ) { 
00120       fgFitter    = 0;
00121       fgMaxpar    = 0;
00122    }
00123    fMethodCall = 0;
00124    fFCN        = 0;
00125 }
00126 
00127 //______________________________________________________________________________
00128 TVirtualFitter *TVirtualFitter::Fitter(TObject *obj, Int_t maxpar)
00129 {
00130    // Static function returning a pointer to the current fitter.
00131    // If the fitter does not exist, the default TFitter is created.
00132    // Don't delete the returned fitter object, it will be re-used.
00133 
00134    if (fgFitter && maxpar > fgMaxpar) {
00135       delete fgFitter;
00136       fgFitter = 0;
00137    }
00138 
00139    if (!fgFitter) {
00140       TPluginHandler *h;
00141       if (fgDefault.Length() == 0) fgDefault = gEnv->GetValue("Root.Fitter","Minuit");
00142       if ((h = gROOT->GetPluginManager()->FindHandler("TVirtualFitter",fgDefault))) {
00143          if (h->LoadPlugin() == -1)
00144             return 0;
00145          fgFitter = (TVirtualFitter*) h->ExecPlugin(1, maxpar);
00146          fgMaxpar = maxpar;
00147       }
00148    }
00149 
00150    if (fgFitter) fgFitter->SetObjectFit(obj);
00151    return fgFitter;
00152 }
00153 
00154 //______________________________________________________________________________
00155 void  TVirtualFitter::GetConfidenceIntervals(Int_t /*n*/, Int_t /*ndim*/, const Double_t * /*x*/, Double_t * /*ci*/, Double_t /*cl*/)
00156 {
00157    //return confidence intervals in array x of dimension ndim
00158    //implemented in TFitter and TLinearFitter
00159 }
00160 
00161 //______________________________________________________________________________
00162 void  TVirtualFitter::GetConfidenceIntervals(TObject * /*obj*/, Double_t /*cl*/)
00163 {
00164    //return confidence intervals in TObject obj
00165    //implemented in TFitter and TLinearFitter
00166 }
00167 
00168 //______________________________________________________________________________
00169 const char *TVirtualFitter::GetDefaultFitter()
00170 {
00171    // static: return the name of the default fitter
00172 
00173    //return fgDefault.Data();
00174    return ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str();
00175 }
00176 
00177 //______________________________________________________________________________
00178 TVirtualFitter *TVirtualFitter::GetFitter()
00179 {
00180    // static: return the current Fitter
00181    return fgFitter;
00182 }
00183 
00184 //______________________________________________________________________________
00185 Int_t TVirtualFitter::GetMaxIterations()
00186 {
00187    // static: Return the maximum number of iterations
00188    // actually max number of function calls
00189 
00190    //return fgMaxiter;
00191    return ROOT::Math::MinimizerOptions::DefaultMaxFunctionCalls();
00192 }
00193 
00194 //______________________________________________________________________________
00195 Double_t TVirtualFitter::GetErrorDef()
00196 {
00197    // static: Return the Error Definition
00198 
00199 //   return fgErrorDef;
00200    return ROOT::Math::MinimizerOptions::DefaultErrorDef();
00201 }
00202 
00203 //______________________________________________________________________________
00204 Double_t TVirtualFitter::GetPrecision()
00205 {
00206    // static: Return the fit relative precision
00207 
00208    //return fgPrecision;
00209    return ROOT::Math::MinimizerOptions::DefaultTolerance();
00210 }
00211 
00212 //______________________________________________________________________________
00213 void TVirtualFitter::SetDefaultFitter(const char *name)
00214 {
00215    // static: set name of default fitter
00216 
00217    ROOT::Math::MinimizerOptions::SetDefaultMinimizer(name,"");
00218    if (fgDefault == name) return;
00219    delete fgFitter;
00220    fgFitter = 0;
00221    fgDefault = name;
00222 }
00223 
00224 //______________________________________________________________________________
00225 void TVirtualFitter::SetFitter(TVirtualFitter *fitter, Int_t maxpar)
00226 {
00227    // Static function to set an alternative fitter
00228 
00229    fgFitter = fitter;
00230    fgMaxpar = maxpar;
00231 }
00232 
00233 //______________________________________________________________________________
00234 void TVirtualFitter::SetFCN(void (*fcn)(Int_t &, Double_t *, Double_t &f, Double_t *, Int_t))
00235 {
00236    // To set the address of the minimization objective function
00237    // called by the native compiler (see function below when called by CINT)
00238 
00239    fFCN = fcn;
00240 }
00241 
00242 //______________________________________________________________________________
00243 void InteractiveFCN(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag)
00244 {
00245    // Static function called when SetFCN is called in interactive mode
00246 
00247    TMethodCall *m = TVirtualFitter::GetFitter()->GetMethodCall();
00248    if (!m) return;
00249 
00250    Long_t args[5];
00251    args[0] = (Long_t)∦
00252    args[1] = (Long_t)gin;
00253    args[2] = (Long_t)&f;
00254    args[3] = (Long_t)u;
00255    args[4] = (Long_t)flag;
00256    m->SetParamPtrs(args);
00257    Double_t result;
00258    m->Execute(result);
00259 }
00260 
00261 //______________________________________________________________________________
00262 Double_t *TVirtualFitter::SetCache(Int_t npoints, Int_t psize)
00263 {
00264    // Initialize the cache array
00265    // npoints is the number of points to be stored (or already stored) in the cache
00266    // psize is the number of elements per point
00267    //
00268    // if (npoints*psize > fCacheSize) the existing cache is deleted
00269    // and a new array is created.
00270    // The function returns a pointer to the cache
00271 
00272    if (npoints*psize > fCacheSize) {
00273       delete [] fCache;
00274       fCacheSize = npoints*psize;
00275       fCache = new Double_t[fCacheSize];
00276    }
00277    fNpoints = npoints;
00278    fPointSize = psize;
00279    return fCache;
00280 }
00281 
00282 //______________________________________________________________________________
00283 void TVirtualFitter::SetFCN(void *fcn)
00284 {
00285    //  To set the address of the minimization objective function
00286    //
00287    //     this function is called by CINT instead of the function above
00288 
00289    if (!fcn) return;
00290 
00291    const char *funcname = gCint->Getp2f2funcname(fcn);
00292    if (funcname) {
00293       delete fMethodCall;
00294       fMethodCall = new TMethodCall();
00295       fMethodCall->InitWithPrototype(funcname,"Int_t&,Double_t*,Double_t&,Double_t*,Int_t");
00296    }
00297    fFCN = InteractiveFCN;
00298 }
00299 
00300 //______________________________________________________________________________
00301 void TVirtualFitter::SetMaxIterations(Int_t niter)
00302 {
00303    // static: Set the maximum number of function calls for the minimization algorithm
00304    // For example for MIGRAD this is the maxcalls value passed as first argument
00305    // (see http://wwwasdoc.web.cern.ch/wwwasdoc/minuit/node18.html ) 
00306 
00307    ROOT::Math::MinimizerOptions::SetDefaultMaxFunctionCalls(niter);
00308 }
00309 
00310 //______________________________________________________________________________
00311 void TVirtualFitter::SetErrorDef(Double_t errdef)
00312 {
00313    // static: Set the Error Definition (default=1)
00314    // For Minuit this is the value passed with the "SET ERR" command
00315    // (see http://wwwasdoc.web.cern.ch/wwwasdoc/minuit/node18.html)
00316 
00317 //    fgErrorDef = errdef;
00318    ROOT::Math::MinimizerOptions::SetDefaultErrorDef(errdef);
00319    if (!fgFitter) return;
00320    Double_t arglist[1];
00321    arglist[0] = errdef;
00322    fgFitter->ExecuteCommand("SET ERRORDEF", arglist, 1);
00323 }
00324 
00325 //______________________________________________________________________________
00326 void TVirtualFitter::SetPrecision(Double_t prec)
00327 {
00328    // static: Set the tolerance used in the minimization algorithm
00329    // For example for MIGRAD this is tolerance value passed as second argument
00330    // (see http://wwwasdoc.web.cern.ch/wwwasdoc/minuit/node18.html ) 
00331   
00332    //fgPrecision = prec;
00333    ROOT::Math::MinimizerOptions::SetDefaultTolerance(prec);
00334 }

Generated on Tue Jul 5 14:34:28 2011 for ROOT_528-00b_version by  doxygen 1.5.1