ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
exampleFit3D.C
Go to the documentation of this file.
1 // example of fitting a 3D function
2 // Typical multidimensional parametric regression where the predictor
3 // depends on 3 variables
4 //
5 // In the case of 1 or 2D one can use the TGraph classes
6 // but since no TGraph3D class exists this tutorial provide
7 // an example of fitting 3D points
8 //
9 // Author: L. Moneta Dec. 2010
10 
11 #include "TRandom2.h"
12 #include "TF3.h"
13 #include "TError.h"
14 #include "Fit/BinData.h"
15 #include "Fit/Fitter.h"
16 #include "Math/WrappedMultiTF1.h"
17 
18 void exampleFit3D() {
19 
20  const int n = 1000;
21  double x[n], y[n], z[n], v[n];
22  double ev = 0.1;
23 
24  // generate the data
25  TRandom2 r;
26  for (int i = 0; i < n; ++i) {
27  x[i] = r.Uniform(0,10);
28  y[i] = r.Uniform(0,10);
29  z[i] = r.Uniform(0,10);
30  v[i] = sin(x[i] ) + cos(y[i]) + z[i] + r.Gaus(0,ev);
31  }
32 
33  // create a 3d binned data structure
34  ROOT::Fit::BinData data(n,3);
35  double xx[3];
36  for(int i = 0; i < n; ++i) {
37  xx[0] = x[i];
38  xx[1] = y[i];
39  xx[2] = z[i];
40  // add the 3d-data coordinate, the predictor value (v[i]) and its errors
41  data.Add(xx, v[i], ev);
42  }
43 
44  TF3 * f3 = new TF3("f3","[0] * sin(x) + [1] * cos(y) + [2] * z",0,10,0,10,0,10);
45  f3->SetParameters(2,2,2);
46  ROOT::Fit::Fitter fitter;
47  // wrapped the TF1 in a IParamMultiFunction interface for teh Fitter class
49  fitter.SetFunction(wf);
50  //
51  bool ret = fitter.Fit(data);
52  if (ret) {
53  const ROOT::Fit::FitResult & res = fitter.Result();
54  // print result (should be around 1)
55  res.Print(std::cout);
56  // copy all fit result info (values, chi2, etc..) in TF3
57  f3->SetFitResult(res);
58  // test fit p-value (chi2 probability)
59  double prob = res.Prob();
60  if (prob < 1.E-2)
61  Error("exampleFit3D","Bad data fit - fit p-value is %f",prob);
62  else
63  std::cout << "Good fit : p-value = " << prob << std::endl;
64 
65  }
66  else
67  Error("exampleFit3D","3D fit failed");
68 }
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:432
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:235
Class to Wrap a ROOT Function class (like TF1) in a IParamMultiFunction interface of multi-dimensions...
Random number generator class based on the maximally quidistributed combined Tausworthe generator by ...
Definition: TRandom2.h:29
void f3()
Definition: na49.C:50
double cos(double)
Double_t x[n]
Definition: legend1.C:17
const FitResult & Result() const
get fit result
Definition: Fitter.h:354
double sin(double)
Float_t z[5]
Definition: Ifit.C:16
void Error(const char *location, const char *msgfmt,...)
bool Fit(const Data &data, const Function &func)
fit a data set using any generic model function If data set is binned a least square fit is performed...
Definition: Fitter.h:146
ROOT::R::TRInterface & r
Definition: Object.C:4
SVector< double, 2 > v
Definition: Dict.h:5
A 3-Dim function with parameters.
Definition: TF3.h:30
void exampleFit3D()
Definition: exampleFit3D.C:18
Fitter class, entry point for performing all type of fits.
Definition: Fitter.h:94
Double_t E()
Definition: TMath.h:54
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition: BinData.h:61
double Prob() const
p value of the fit (chi2 probability)
Definition: FitResult.cxx:377
void SetFunction(const IModelFunction &func, bool useGradient=false)
Set the fitted function (model function) from a parametric function interface.
Definition: Fitter.cxx:104
class containg the result of the fit and all the related information (fitted parameter values...
Definition: FitResult.h:52
void Add(double x, double y)
add one dim data with only coordinate and values
Definition: BinData.cxx:265
Double_t y[n]
Definition: legend1.C:17
void Print(std::ostream &os, bool covmat=false) const
print the result and optionaly covariance matrix and correlations
Definition: FitResult.cxx:446
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
virtual void SetFitResult(const ROOT::Fit::FitResult &result, const Int_t *indpar=0)
Set the result from the fit parameter values, errors, chi2, etc...
Definition: TF1.cxx:3038
const Int_t n
Definition: legend1.C:16