Logo ROOT   6.08/07
Reference Guide
TVirtualFitter.cxx
Go to the documentation of this file.
1 // @(#)root/hist:$Id$
2 // Author: Rene Brun 31/08/99
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 
13 /** \class TVirtualFitter
14  \ingroup Hist
15  Abstract Base Class for Fitting
16 */
17 
18 #include "TROOT.h"
19 #include "TVirtualFitter.h"
20 #include "TPluginManager.h"
21 #include "TEnv.h"
22 #include "Math/MinimizerOptions.h"
23 #include "ThreadLocalStorage.h"
24 
25 
26 // Implement a thread local static member as a replacement
27 // for TVirtualFitter::fgFitter
28 namespace {
29  struct FitterGlobals {
30  FitterGlobals() : fFitter(nullptr),fMaxPar(0) {}
31 
32  TVirtualFitter *fFitter;
33  Int_t fMaxPar;
34  TString fDefault;
35  };
36  static FitterGlobals &GetGlobals() {
37  TTHREAD_TLS_DECL(FitterGlobals,globals);
38  return globals;
39  }
40  static TVirtualFitter *&GetGlobalFitter() {
41  return GetGlobals().fFitter;
42  }
43  static Int_t &GetGlobalMaxPar() {
44  return GetGlobals().fMaxPar;
45  }
46  static TString &GetGlobalDefault() {
47  return GetGlobals().fDefault;
48  }
49 }
50 //Int_t TVirtualFitter::fgMaxpar = 0;
51 // Int_t TVirtualFitter::fgMaxiter = 5000;
52 // Double_t TVirtualFitter::fgPrecision = 1e-6;
53 // Double_t TVirtualFitter::fgErrorDef = 1;
54 //TString TVirtualFitter::fgDefault = "";
55 
57 
58 #ifdef R__COMPLETE_MEM_TERMINATION
59 namespace {
60  struct TVirtualFitterCleanup {
61  ~TVirtualFitterCleanup() {
63  }
64  };
65  TVirtualFitterCleanup cleanup;
66 }
67 #endif
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// Default constructor.
71 
73  fXfirst(0),
74  fXlast(0),
75  fYfirst(0),
76  fYlast(0),
77  fZfirst(0),
78  fZlast(0),
79  fNpoints(0),
80  fPointSize(0),
81  fCacheSize(0),
82  fCache(0),
83  fObjectFit(0),
84  fUserFunc(0),
85  fMethodCall(0),
86  fFCN(0)
87 {
88 }
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 ///copy constructor
92 
94  TNamed(tvf),
95  fOption(tvf.fOption),
96  fXfirst(tvf.fXfirst),
97  fXlast(tvf.fXlast),
98  fYfirst(tvf.fYfirst),
99  fYlast(tvf.fYlast),
100  fZfirst(tvf.fZfirst),
101  fZlast(tvf.fZlast),
102  fNpoints(tvf.fNpoints),
103  fPointSize(tvf.fPointSize),
104  fCacheSize(tvf.fCacheSize),
105  fCache(tvf.fCache),
106  fObjectFit(tvf.fObjectFit),
107  fUserFunc(tvf.fUserFunc),
109  fFCN(tvf.fFCN)
110 {
111 }
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 ///assignment operator
115 
117 {
118  if(this!=&tvf) {
119  TNamed::operator=(tvf);
120  fOption=tvf.fOption;
121  fXfirst=tvf.fXfirst;
122  fXlast=tvf.fXlast;
123  fYfirst=tvf.fYfirst;
124  fYlast=tvf.fYlast;
125  fZfirst=tvf.fZfirst;
126  fZlast=tvf.fZlast;
127  fNpoints=tvf.fNpoints;
130  fCache=tvf.fCache;
132  fUserFunc=tvf.fUserFunc;
134  fFCN=tvf.fFCN;
135  }
136  return *this;
137 }
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// Cleanup virtual fitter.
141 
143 {
144  delete fMethodCall;
145  delete [] fCache;
146  if ( GetGlobalFitter() == this ) {
147  GetGlobalFitter() = 0;
148  GetGlobalMaxPar() = 0;
149  }
150  fMethodCall = 0;
151  fFCN = 0;
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////////
155 /// Static function returning a pointer to the current fitter.
156 /// If the fitter does not exist, the default TFitter is created.
157 /// Don't delete the returned fitter object, it will be re-used.
158 
160 {
161  if (GetGlobalFitter() && maxpar > GetGlobalMaxPar()) {
162  delete GetGlobalFitter();
163  GetGlobalFitter() = 0;
164  }
165 
166  if (!GetGlobalFitter()) {
167  TPluginHandler *h;
168  if (GetGlobalDefault().Length() == 0) GetGlobalDefault() = gEnv->GetValue("Root.Fitter","Minuit");
169  if ((h = gROOT->GetPluginManager()->FindHandler("TVirtualFitter",GetGlobalDefault()))) {
170  if (h->LoadPlugin() == -1)
171  return 0;
172  GetGlobalFitter() = (TVirtualFitter*) h->ExecPlugin(1, maxpar);
173  GetGlobalMaxPar() = maxpar;
174  }
175  }
176 
177  if (GetGlobalFitter()) GetGlobalFitter()->SetObjectFit(obj);
178  return GetGlobalFitter();
179 }
180 
181 ////////////////////////////////////////////////////////////////////////////////
182 ///return confidence intervals in array x of dimension ndim
183 ///implemented in TFitter and TLinearFitter
184 
185 void TVirtualFitter::GetConfidenceIntervals(Int_t /*n*/, Int_t /*ndim*/, const Double_t * /*x*/, Double_t * /*ci*/, Double_t /*cl*/)
186 {
187 }
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 ///return confidence intervals in TObject obj
191 ///implemented in TFitter and TLinearFitter
192 
194 {
195 }
196 
197 ////////////////////////////////////////////////////////////////////////////////
198 /// static: return the name of the default fitter
199 
201 {
202  //return GetGlobalDefault().Data();
204 }
205 
206 ////////////////////////////////////////////////////////////////////////////////
207 /// static: return the current Fitter
208 
210 {
211  return GetGlobalFitter();
212 }
213 
214 ////////////////////////////////////////////////////////////////////////////////
215 /// static: Return the maximum number of iterations
216 /// actually max number of function calls
217 
219 {
220  //return fgMaxiter;
222 }
223 
224 ////////////////////////////////////////////////////////////////////////////////
225 /// static: Return the Error Definition
226 
228 {
229 // return fgErrorDef;
231 }
232 
233 ////////////////////////////////////////////////////////////////////////////////
234 /// static: Return the fit relative precision
235 
237 {
238  //return fgPrecision;
240 }
241 
242 ////////////////////////////////////////////////////////////////////////////////
243 /// static: set name of default fitter
244 
246 {
248  if (GetGlobalDefault() == name) return;
249  delete GetGlobalFitter();
250  GetGlobalFitter() = 0;
251  GetGlobalDefault() = name;
252 }
253 
254 ////////////////////////////////////////////////////////////////////////////////
255 /// Static function to set an alternative fitter
256 
258 {
259  GetGlobalFitter() = fitter;
260  GetGlobalMaxPar() = maxpar;
261 }
262 
263 ////////////////////////////////////////////////////////////////////////////////
264 /// To set the address of the minimization objective function
265 /// called by the native compiler (see function below when called by CINT)
266 
268 {
269  fFCN = fcn;
270 }
271 
272 ////////////////////////////////////////////////////////////////////////////////
273 /// Static function called when SetFCN is called in interactive mode
274 
275 void InteractiveFCN(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag)
276 {
278  if (!m) return;
279 
280  Long_t args[5];
281  args[0] = (Long_t)∦
282  args[1] = (Long_t)gin;
283  args[2] = (Long_t)&f;
284  args[3] = (Long_t)u;
285  args[4] = (Long_t)flag;
286  m->SetParamPtrs(args);
288  m->Execute(result);
289 }
290 
291 ////////////////////////////////////////////////////////////////////////////////
292 /// Initialize the cache array
293 /// npoints is the number of points to be stored (or already stored) in the cache
294 /// psize is the number of elements per point
295 ///
296 /// if (npoints*psize > fCacheSize) the existing cache is deleted
297 /// and a new array is created.
298 /// The function returns a pointer to the cache
299 
301 {
302  if (npoints*psize > fCacheSize) {
303  delete [] fCache;
304  fCacheSize = npoints*psize;
305  fCache = new Double_t[fCacheSize];
306  }
307  fNpoints = npoints;
308  fPointSize = psize;
309  return fCache;
310 }
311 
312 ////////////////////////////////////////////////////////////////////////////////
313 /// To set the address of the minimization objective function
314 ///
315 /// this function is called by CINT instead of the function above
316 
317 void TVirtualFitter::SetFCN(void *fcn)
318 {
319  if (!fcn) return;
320 
321  Error("SetFCN", "Not used anymore.");
322 
324 }
325 
326 ////////////////////////////////////////////////////////////////////////////////
327 /// static: Set the maximum number of function calls for the minimization algorithm
328 /// For example for MIGRAD this is the maxcalls value passed as first argument
329 /// (see https://cern-tex.web.cern.ch/cern-tex/minuit/node18.html )
330 
332 {
334 }
335 
336 ////////////////////////////////////////////////////////////////////////////////
337 /// static: Set the Error Definition (default=1)
338 /// For Minuit this is the value passed with the "SET ERR" command
339 /// (see https://cern-tex.web.cern.ch/cern-tex/minuit/node18.html)
340 
342 {
343 // fgErrorDef = errdef;
345  if (!GetGlobalFitter()) return;
346  Double_t arglist[1];
347  arglist[0] = errdef;
348  GetGlobalFitter()->ExecuteCommand("SET ERRORDEF", arglist, 1);
349 }
350 
351 ////////////////////////////////////////////////////////////////////////////////
352 /// static: Set the tolerance used in the minimization algorithm
353 /// For example for MIGRAD this is tolerance value passed as second argument
354 /// (see https://cern-tex.web.cern.ch/cern-tex/minuit/node18.html )
355 
357 {
358  //fgPrecision = prec;
360 }
static Double_t GetErrorDef()
static: Return the Error Definition
static void SetErrorDef(Double_t errdef=1)
static: Set the Error Definition (default=1) For Minuit this is the value passed with the "SET ERR" c...
Double_t * fCache
static Double_t GetPrecision()
static: Return the fit relative precision
TVirtualFitter()
Default constructor.
virtual void GetConfidenceIntervals(Int_t n, Int_t ndim, const Double_t *x, Double_t *ci, Double_t cl=0.95)
return confidence intervals in array x of dimension ndim implemented in TFitter and TLinearFitter ...
static Int_t GetMaxIterations()
static: Return the maximum number of iterations actually max number of function calls ...
TH1 * h
Definition: legend2.C:5
static void SetDefaultFitter(const char *name="")
static: set name of default fitter
#define gROOT
Definition: TROOT.h:364
Int_t LoadPlugin()
Load the plugin library for this handler.
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
virtual Double_t * SetCache(Int_t npoints, Int_t psize)
Initialize the cache array npoints is the number of points to be stored (or already stored) in the ca...
TMethodCall * GetMethodCall() const
static const char * GetDefaultFitter()
static: return the name of the default fitter
void SetParamPtrs(void *paramArr, Int_t nparam=-1)
ParamArr is an array containing the function argument values.
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
TVirtualFitter & operator=(const TVirtualFitter &tvf)
assignment operator
TObject * fObjectFit
static void SetMaxIterations(Int_t niter=5000)
static: Set the maximum number of function calls for the minimization algorithm For example for MIGRA...
Foption_t fOption
static void SetDefaultErrorDef(double up)
virtual void SetFCN(void *fcn) R__DEPRECATED(6
To set the address of the minimization objective function.
Method or function calling interface.
Definition: TMethodCall.h:41
static void SetFitter(TVirtualFitter *fitter, Int_t maxpar=25)
Static function to set an alternative fitter.
TObject * fUserFunc
virtual ~TVirtualFitter()
Cleanup virtual fitter.
static void SetDefaultMaxFunctionCalls(int maxcall)
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:42
static const std::string & DefaultMinimizerType()
Long_t ExecPlugin(int nargs, const T &... params)
virtual Int_t GetValue(const char *name, Int_t dflt)
Returns the integer value for a resource.
Definition: TEnv.cxx:496
TMarker * m
Definition: textangle.C:8
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:925
static TVirtualFitter * GetFitter()
static: return the current Fitter
long Long_t
Definition: RtypesCore.h:50
#define ClassImp(name)
Definition: Rtypes.h:279
double f(double x)
static void SetDefaultTolerance(double tol)
double Double_t
Definition: RtypesCore.h:55
R__EXTERN TEnv * gEnv
Definition: TEnv.h:174
static void SetPrecision(Double_t prec=1e-6)
static: Set the tolerance used in the minimization algorithm For example for MIGRAD this is tolerance...
Abstract Base Class for Fitting.
Mother of all ROOT objects.
Definition: TObject.h:37
static TVirtualFitter * Fitter(TObject *obj, Int_t maxpar=25)
Static function returning a pointer to the current fitter.
#define nullptr
Definition: Rtypes.h:87
void Execute(const char *, const char *, int *=0)
Execute method on this object with the given parameter string, e.g.
Definition: TMethodCall.h:68
void(* fFCN)(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag)
void InteractiveFCN(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag)
Static function called when SetFCN is called in interactive mode.
double result[121]
static void SetDefaultMinimizer(const char *type, const char *algo=0)
char name[80]
Definition: TGX11.cxx:109
TMethodCall * fMethodCall