ROOT  6.06/09
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 "TInterpreter.h"
23 #include "Math/MinimizerOptions.h"
24 #include "ThreadLocalStorage.h"
25 
26 
27 // Implement a thread local static member as a replacement
28 // for TVirtualFitter::fgFitter
29 namespace {
30  struct FitterGlobals {
31  FitterGlobals() : fFitter(nullptr),fMaxPar(0) {}
32 
33  TVirtualFitter *fFitter;
34  Int_t fMaxPar;
35  TString fDefault;
36  };
37  static FitterGlobals &GetGlobals() {
38  TTHREAD_TLS_DECL(FitterGlobals,globals);
39  return globals;
40  }
41  static TVirtualFitter *&GetGlobalFitter() {
42  return GetGlobals().fFitter;
43  }
44  static Int_t &GetGlobalMaxPar() {
45  return GetGlobals().fMaxPar;
46  }
47  static TString &GetGlobalDefault() {
48  return GetGlobals().fDefault;
49  }
50 }
51 //Int_t TVirtualFitter::fgMaxpar = 0;
52 // Int_t TVirtualFitter::fgMaxiter = 5000;
53 // Double_t TVirtualFitter::fgPrecision = 1e-6;
54 // Double_t TVirtualFitter::fgErrorDef = 1;
55 //TString TVirtualFitter::fgDefault = "";
56 
58 
59 #ifdef R__COMPLETE_MEM_TERMINATION
60 namespace {
61  struct TVirtualFitterCleanup {
62  ~TVirtualFitterCleanup() {
64  }
65  };
66  TVirtualFitterCleanup cleanup;
67 }
68 #endif
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// Default constructor.
72 
74  fXfirst(0),
75  fXlast(0),
76  fYfirst(0),
77  fYlast(0),
78  fZfirst(0),
79  fZlast(0),
80  fNpoints(0),
81  fPointSize(0),
82  fCacheSize(0),
83  fCache(0),
84  fObjectFit(0),
85  fUserFunc(0),
86  fMethodCall(0),
87  fFCN(0)
88 {
89 }
90 
91 ////////////////////////////////////////////////////////////////////////////////
92 ///copy constructor
93 
95  TNamed(tvf),
96  fOption(tvf.fOption),
97  fXfirst(tvf.fXfirst),
98  fXlast(tvf.fXlast),
99  fYfirst(tvf.fYfirst),
100  fYlast(tvf.fYlast),
101  fZfirst(tvf.fZfirst),
102  fZlast(tvf.fZlast),
103  fNpoints(tvf.fNpoints),
104  fPointSize(tvf.fPointSize),
105  fCacheSize(tvf.fCacheSize),
106  fCache(tvf.fCache),
107  fObjectFit(tvf.fObjectFit),
108  fUserFunc(tvf.fUserFunc),
109  fMethodCall(tvf.fMethodCall),
110  fFCN(tvf.fFCN)
111 {
112 }
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 ///assignment operator
116 
118 {
119  if(this!=&tvf) {
120  TNamed::operator=(tvf);
121  fOption=tvf.fOption;
122  fXfirst=tvf.fXfirst;
123  fXlast=tvf.fXlast;
124  fYfirst=tvf.fYfirst;
125  fYlast=tvf.fYlast;
126  fZfirst=tvf.fZfirst;
127  fZlast=tvf.fZlast;
128  fNpoints=tvf.fNpoints;
131  fCache=tvf.fCache;
133  fUserFunc=tvf.fUserFunc;
135  fFCN=tvf.fFCN;
136  }
137  return *this;
138 }
139 
140 ////////////////////////////////////////////////////////////////////////////////
141 /// Cleanup virtual fitter.
142 
144 {
145  delete fMethodCall;
146  delete [] fCache;
147  if ( GetGlobalFitter() == this ) {
148  GetGlobalFitter() = 0;
149  GetGlobalMaxPar() = 0;
150  }
151  fMethodCall = 0;
152  fFCN = 0;
153 }
154 
155 ////////////////////////////////////////////////////////////////////////////////
156 /// Static function returning a pointer to the current fitter.
157 /// If the fitter does not exist, the default TFitter is created.
158 /// Don't delete the returned fitter object, it will be re-used.
159 
161 {
162  if (GetGlobalFitter() && maxpar > GetGlobalMaxPar()) {
163  delete GetGlobalFitter();
164  GetGlobalFitter() = 0;
165  }
166 
167  if (!GetGlobalFitter()) {
168  TPluginHandler *h;
169  if (GetGlobalDefault().Length() == 0) GetGlobalDefault() = gEnv->GetValue("Root.Fitter","Minuit");
170  if ((h = gROOT->GetPluginManager()->FindHandler("TVirtualFitter",GetGlobalDefault()))) {
171  if (h->LoadPlugin() == -1)
172  return 0;
173  GetGlobalFitter() = (TVirtualFitter*) h->ExecPlugin(1, maxpar);
174  GetGlobalMaxPar() = maxpar;
175  }
176  }
177 
178  if (GetGlobalFitter()) GetGlobalFitter()->SetObjectFit(obj);
179  return GetGlobalFitter();
180 }
181 
182 ////////////////////////////////////////////////////////////////////////////////
183 ///return confidence intervals in array x of dimension ndim
184 ///implemented in TFitter and TLinearFitter
185 
186 void TVirtualFitter::GetConfidenceIntervals(Int_t /*n*/, Int_t /*ndim*/, const Double_t * /*x*/, Double_t * /*ci*/, Double_t /*cl*/)
187 {
188 }
189 
190 ////////////////////////////////////////////////////////////////////////////////
191 ///return confidence intervals in TObject obj
192 ///implemented in TFitter and TLinearFitter
193 
195 {
196 }
197 
198 ////////////////////////////////////////////////////////////////////////////////
199 /// static: return the name of the default fitter
200 
202 {
203  //return GetGlobalDefault().Data();
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// static: return the current Fitter
209 
211 {
212  return GetGlobalFitter();
213 }
214 
215 ////////////////////////////////////////////////////////////////////////////////
216 /// static: Return the maximum number of iterations
217 /// actually max number of function calls
218 
220 {
221  //return fgMaxiter;
223 }
224 
225 ////////////////////////////////////////////////////////////////////////////////
226 /// static: Return the Error Definition
227 
229 {
230 // return fgErrorDef;
232 }
233 
234 ////////////////////////////////////////////////////////////////////////////////
235 /// static: Return the fit relative precision
236 
238 {
239  //return fgPrecision;
241 }
242 
243 ////////////////////////////////////////////////////////////////////////////////
244 /// static: set name of default fitter
245 
247 {
249  if (GetGlobalDefault() == name) return;
250  delete GetGlobalFitter();
251  GetGlobalFitter() = 0;
252  GetGlobalDefault() = name;
253 }
254 
255 ////////////////////////////////////////////////////////////////////////////////
256 /// Static function to set an alternative fitter
257 
259 {
260  GetGlobalFitter() = fitter;
261  GetGlobalMaxPar() = maxpar;
262 }
263 
264 ////////////////////////////////////////////////////////////////////////////////
265 /// To set the address of the minimization objective function
266 /// called by the native compiler (see function below when called by CINT)
267 
269 {
270  fFCN = fcn;
271 }
272 
273 ////////////////////////////////////////////////////////////////////////////////
274 /// Static function called when SetFCN is called in interactive mode
275 
276 void InteractiveFCN(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag)
277 {
279  if (!m) return;
280 
281  Long_t args[5];
282  args[0] = (Long_t)∦
283  args[1] = (Long_t)gin;
284  args[2] = (Long_t)&f;
285  args[3] = (Long_t)u;
286  args[4] = (Long_t)flag;
287  m->SetParamPtrs(args);
289  m->Execute(result);
290 }
291 
292 ////////////////////////////////////////////////////////////////////////////////
293 /// Initialize the cache array
294 /// npoints is the number of points to be stored (or already stored) in the cache
295 /// psize is the number of elements per point
296 ///
297 /// if (npoints*psize > fCacheSize) the existing cache is deleted
298 /// and a new array is created.
299 /// The function returns a pointer to the cache
300 
302 {
303  if (npoints*psize > fCacheSize) {
304  delete [] fCache;
305  fCacheSize = npoints*psize;
306  fCache = new Double_t[fCacheSize];
307  }
308  fNpoints = npoints;
309  fPointSize = psize;
310  return fCache;
311 }
312 
313 ////////////////////////////////////////////////////////////////////////////////
314 /// To set the address of the minimization objective function
315 ///
316 /// this function is called by CINT instead of the function above
317 
318 void TVirtualFitter::SetFCN(void *fcn)
319 {
320  if (!fcn) return;
321 
322  const char *funcname = gCling->Getp2f2funcname(fcn);
323  if (funcname) {
324  delete fMethodCall;
325  fMethodCall = new TMethodCall();
326  fMethodCall->InitWithPrototype(funcname,"Int_t&,Double_t*,Double_t&,Double_t*,Int_t");
327  }
329 }
330 
331 ////////////////////////////////////////////////////////////////////////////////
332 /// static: Set the maximum number of function calls for the minimization algorithm
333 /// For example for MIGRAD this is the maxcalls value passed as first argument
334 /// (see http://wwwasdoc.web.cern.ch/wwwasdoc/minuit/node18.html )
335 
337 {
339 }
340 
341 ////////////////////////////////////////////////////////////////////////////////
342 /// static: Set the Error Definition (default=1)
343 /// For Minuit this is the value passed with the "SET ERR" command
344 /// (see http://wwwasdoc.web.cern.ch/wwwasdoc/minuit/node18.html)
345 
347 {
348 // fgErrorDef = errdef;
350  if (!GetGlobalFitter()) return;
351  Double_t arglist[1];
352  arglist[0] = errdef;
353  GetGlobalFitter()->ExecuteCommand("SET ERRORDEF", arglist, 1);
354 }
355 
356 ////////////////////////////////////////////////////////////////////////////////
357 /// static: Set the tolerance used in the minimization algorithm
358 /// For example for MIGRAD this is tolerance value passed as second argument
359 /// (see http://wwwasdoc.web.cern.ch/wwwasdoc/minuit/node18.html )
360 
362 {
363  //fgPrecision = prec;
365 }
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
ClassImp(TVirtualFitter) 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:340
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 void SetFCN(void *fcn)
To set the address of the minimization objective function.
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...
Long_t ExecPlugin(int nargs, const T &...params)
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)
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:40
static const std::string & DefaultMinimizerType()
virtual Int_t GetValue(const char *name, Int_t dflt)
Returns the integer value for a resource.
Definition: TEnv.cxx:494
TMarker * m
Definition: textangle.C:8
static TVirtualFitter * GetFitter()
static: return the current Fitter
void InitWithPrototype(TClass *cl, const char *method, const char *proto, Bool_t objectIsConst=kFALSE, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch)
Initialize the method invocation environment.
long Long_t
Definition: RtypesCore.h:50
def cleanup()
Definition: ROOT.py:579
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...
#define name(a, b)
Definition: linkTestLib0.cpp:5
Abstract Base Class for Fitting.
Mother of all ROOT objects.
Definition: TObject.h:58
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
TMethodCall * GetMethodCall() const
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.
virtual const char * Getp2f2funcname(void *) const
Definition: TInterpreter.h:213
double result[121]
R__EXTERN TInterpreter * gCling
Definition: TInterpreter.h:504
static void SetDefaultMinimizer(const char *type, const char *algo=0)
TObject * obj
TMethodCall * fMethodCall