Logo ROOT  
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),
108  fMethodCall(tvf.fMethodCall),
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 /// Initialize the cache array
274 /// npoints is the number of points to be stored (or already stored) in the cache
275 /// psize is the number of elements per point
276 ///
277 /// if (npoints*psize > fCacheSize) the existing cache is deleted
278 /// and a new array is created.
279 /// The function returns a pointer to the cache
280 
282 {
283  if (npoints*psize > fCacheSize) {
284  delete [] fCache;
285  fCacheSize = npoints*psize;
286  fCache = new Double_t[fCacheSize];
287  }
288  fNpoints = npoints;
289  fPointSize = psize;
290  return fCache;
291 }
292 
293 ////////////////////////////////////////////////////////////////////////////////
294 /// static: Set the maximum number of function calls for the minimization algorithm
295 /// For example for MIGRAD this is the maxcalls value passed as first argument
296 /// (see https://cern-tex.web.cern.ch/cern-tex/minuit/node18.html )
297 
299 {
301 }
302 
303 ////////////////////////////////////////////////////////////////////////////////
304 /// static: Set the Error Definition (default=1)
305 /// For Minuit this is the value passed with the "SET ERR" command
306 /// (see https://cern-tex.web.cern.ch/cern-tex/minuit/node18.html)
307 
309 {
310 // fgErrorDef = errdef;
312  if (!GetGlobalFitter()) return;
313  Double_t arglist[1];
314  arglist[0] = errdef;
315  GetGlobalFitter()->ExecuteCommand("SET ERRORDEF", arglist, 1);
316 }
317 
318 ////////////////////////////////////////////////////////////////////////////////
319 /// static: Set the tolerance used in the minimization algorithm
320 /// For example for MIGRAD this is tolerance value passed as second argument
321 /// (see https://cern-tex.web.cern.ch/cern-tex/minuit/node18.html )
322 
324 {
325  //fgPrecision = prec;
327 }
TVirtualFitter::SetMaxIterations
static void SetMaxIterations(Int_t niter=5000)
static: Set the maximum number of function calls for the minimization algorithm For example for MIGRA...
Definition: TVirtualFitter.cxx:298
TVirtualFitter::fObjectFit
TObject * fObjectFit
Definition: TVirtualFitter.h:43
TVirtualFitter::fCacheSize
Int_t fCacheSize
Definition: TVirtualFitter.h:41
TVirtualFitter::fMethodCall
TMethodCall * fMethodCall
Definition: TVirtualFitter.h:45
f
#define f(i)
Definition: RSha256.hxx:104
ROOT::Math::MinimizerOptions::SetDefaultMinimizer
static void SetDefaultMinimizer(const char *type, const char *algo=0)
Definition: MinimizerOptions.cxx:43
TVirtualFitter::operator=
TVirtualFitter & operator=(const TVirtualFitter &tvf)
assignment operator
Definition: TVirtualFitter.cxx:116
gEnv
R__EXTERN TEnv * gEnv
Definition: TEnv.h:171
TNamed::operator=
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:51
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TVirtualFitter::fYfirst
Int_t fYfirst
Definition: TVirtualFitter.h:35
TVirtualFitter::SetFitter
static void SetFitter(TVirtualFitter *fitter, Int_t maxpar=25)
Static function to set an alternative fitter.
Definition: TVirtualFitter.cxx:257
TVirtualFitter::fPointSize
Int_t fPointSize
Definition: TVirtualFitter.h:40
ROOT::Math::MinimizerOptions::DefaultMaxFunctionCalls
static int DefaultMaxFunctionCalls()
Definition: MinimizerOptions.cxx:86
TVirtualFitter::TVirtualFitter
TVirtualFitter()
Default constructor.
Definition: TVirtualFitter.cxx:72
Int_t
int Int_t
Definition: RtypesCore.h:45
TVirtualFitter::GetConfidenceIntervals
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
Definition: TVirtualFitter.cxx:185
TVirtualFitter::Fitter
static TVirtualFitter * Fitter(TObject *obj, Int_t maxpar=25)
Static function returning a pointer to the current fitter.
Definition: TVirtualFitter.cxx:159
TEnv::GetValue
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition: TEnv.cxx:491
TEnv.h
TString
Basic string class.
Definition: TString.h:136
TVirtualFitter.h
TVirtualFitter::~TVirtualFitter
virtual ~TVirtualFitter()
Cleanup virtual fitter.
Definition: TVirtualFitter.cxx:142
MinimizerOptions.h
TVirtualFitter
Abstract Base Class for Fitting.
Definition: TVirtualFitter.h:29
TVirtualFitter::fNpoints
Int_t fNpoints
Definition: TVirtualFitter.h:39
TROOT.h
TVirtualFitter::SetFCN
virtual void SetFCN(void(*fcn)(Int_t &, Double_t *, Double_t &f, Double_t *, Int_t))
To set the address of the minimization objective function called by the native compiler (see function...
Definition: TVirtualFitter.cxx:267
TVirtualFitter::fCache
Double_t * fCache
Definition: TVirtualFitter.h:42
TPluginHandler
Definition: TPluginManager.h:101
TVirtualFitter::fYlast
Int_t fYlast
Definition: TVirtualFitter.h:36
ROOT::Math::MinimizerOptions::DefaultMinimizerType
static const std::string & DefaultMinimizerType()
Definition: MinimizerOptions.cxx:92
h
#define h(i)
Definition: RSha256.hxx:106
ROOT::Math::MinimizerOptions::DefaultErrorDef
static double DefaultErrorDef()
Definition: MinimizerOptions.cxx:83
TNamed
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
TVirtualFitter::SetErrorDef
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...
Definition: TVirtualFitter.cxx:308
TVirtualFitter::SetPrecision
static void SetPrecision(Double_t prec=1e-6)
static: Set the tolerance used in the minimization algorithm For example for MIGRAD this is tolerance...
Definition: TVirtualFitter.cxx:323
ROOT::Math::MinimizerOptions::SetDefaultErrorDef
static void SetDefaultErrorDef(double up)
Definition: MinimizerOptions.cxx:48
TVirtualFitter::GetDefaultFitter
static const char * GetDefaultFitter()
static: return the name of the default fitter
Definition: TVirtualFitter.cxx:200
TVirtualFitter::fUserFunc
TObject * fUserFunc
Definition: TVirtualFitter.h:44
TVirtualFitter::GetMaxIterations
static Int_t GetMaxIterations()
static: Return the maximum number of iterations actually max number of function calls
Definition: TVirtualFitter.cxx:218
TVirtualFitter::GetErrorDef
static Double_t GetErrorDef()
static: Return the Error Definition
Definition: TVirtualFitter.cxx:227
TVirtualFitter::fXlast
Int_t fXlast
Definition: TVirtualFitter.h:34
TVirtualFitter::fFCN
void(* fFCN)(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag)
Definition: TVirtualFitter.h:46
ROOT::Math::MinimizerOptions::SetDefaultMaxFunctionCalls
static void SetDefaultMaxFunctionCalls(int maxcall)
Definition: MinimizerOptions.cxx:60
Double_t
double Double_t
Definition: RtypesCore.h:59
TVirtualFitter::GetPrecision
static Double_t GetPrecision()
static: Return the fit relative precision
Definition: TVirtualFitter.cxx:236
TPluginManager.h
ROOT::Math::MinimizerOptions::DefaultTolerance
static double DefaultTolerance()
Definition: MinimizerOptions.cxx:84
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
TVirtualFitter::fXfirst
Int_t fXfirst
Definition: TVirtualFitter.h:33
name
char name[80]
Definition: TGX11.cxx:110
TVirtualFitter::SetDefaultFitter
static void SetDefaultFitter(const char *name="")
static: set name of default fitter
Definition: TVirtualFitter.cxx:245
TVirtualFitter::GetFitter
static TVirtualFitter * GetFitter()
static: return the current Fitter
Definition: TVirtualFitter.cxx:209
ROOT::Math::MinimizerOptions::SetDefaultTolerance
static void SetDefaultTolerance(double tol)
Definition: MinimizerOptions.cxx:52
TVirtualFitter::SetCache
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...
Definition: TVirtualFitter.cxx:281
ThreadLocalStorage.h
TVirtualFitter::fZfirst
Int_t fZfirst
Definition: TVirtualFitter.h:37
TVirtualFitter::fOption
Foption_t fOption
Definition: TVirtualFitter.h:32
gROOT
#define gROOT
Definition: TROOT.h:406
int
TVirtualFitter::fZlast
Int_t fZlast
Definition: TVirtualFitter.h:38