Logo ROOT  
Reference Guide
exampleFit3D.C File Reference

Detailed Description

View in nbviewer Open in SWAN example of fitting a 3D function Typical multidimensional parametric regression where the predictor depends on 3 variables

In the case of 1 or 2D one can use the TGraph classes but since no TGraph3D class exists this tutorial provide an example of fitting 3D points

Minimizer is Minuit / Migrad
Chi2 = 993.065
NDf = 997
Edm = 1.2693e-19
NCalls = 43
p0 = 0.993546 +/- 0.00475134
p1 = 0.99397 +/- 0.00434249
p2 = 0.997895 +/- 0.000545663
Good fit : p-value = 0.529221
#include "TRandom2.h"
#include "TF3.h"
#include "TError.h"
#include "Fit/BinData.h"
#include "Fit/Fitter.h"
void exampleFit3D() {
const int n = 1000;
double x[n], y[n], z[n], v[n];
double ev = 0.1;
// generate the data
for (int i = 0; i < n; ++i) {
x[i] = r.Uniform(0,10);
y[i] = r.Uniform(0,10);
z[i] = r.Uniform(0,10);
v[i] = sin(x[i] ) + cos(y[i]) + z[i] + r.Gaus(0,ev);
// create a 3d binned data structure
double xx[3];
for(int i = 0; i < n; ++i) {
xx[0] = x[i];
xx[1] = y[i];
xx[2] = z[i];
// add the 3d-data coordinate, the predictor value (v[i]) and its errors
data.Add(xx, v[i], ev);
TF3 * f3 = new TF3("f3","[0] * sin(x) + [1] * cos(y) + [2] * z",0,10,0,10,0,10);
// wrapped the TF1 in a IParamMultiFunction interface for the Fitter class
bool ret = fitter.Fit(data);
if (ret) {
const ROOT::Fit::FitResult & res = fitter.Result();
// print result (should be around 1)
// copy all fit result info (values, chi2, etc..) in TF3
// test fit p-value (chi2 probability)
double prob = res.Prob();
if (prob < 1.E-2)
Error("exampleFit3D","Bad data fit - fit p-value is %f",prob);
std::cout << "Good fit : p-value = " << prob << std::endl;
Error("exampleFit3D","3D fit failed");
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition: BinData.h:52
class containing the result of the fit and all the related information (fitted parameter values,...
Definition: FitResult.h:47
void Print(std::ostream &os, bool covmat=false) const
print the result and optionally covariance matrix and correlations
Definition: FitResult.cxx:433
double Prob() const
p value of the fit (chi2 probability)
Definition: FitResult.cxx:364
Fitter class, entry point for performing all type of fits.
Definition: Fitter.h:77
const FitResult & Result() const
get fit result
Definition: Fitter.h:382
bool Fit(const Data &data, const Function &func, const ROOT::EExecutionPolicy &executionPolicy=ROOT::EExecutionPolicy::kSequential)
fit a data set using any generic model function If data set is binned a least square fit is performed...
Definition: Fitter.h:139
void SetFunction(const IModelFunction &func, bool useGradient=false)
Set the fitted function (model function) from a parametric function interface.
Definition: Fitter.cxx:59
Class to Wrap a ROOT Function class (like TF1) in a IParamMultiFunction interface of multi-dimensions...
virtual void SetFitResult(const ROOT::Fit::FitResult &result, const Int_t *indpar=0)
Set the result from the fit parameter values, errors, chi2, etc... Optionally a pointer to a vector (...
Definition: TF1.cxx:3403
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:649
A 3-Dim function with parameters.
Definition: TF3.h:28
Random number generator class based on the maximally quidistributed combined Tausworthe generator by ...
Definition: TRandom2.h:27
RVec< PromoteType< T > > cos(const RVec< T > &v)
Definition: RVec.hxx:1776
RVec< PromoteType< T > > sin(const RVec< T > &v)
Definition: RVec.hxx:1775
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
Lorenzo Moneta

Definition in file exampleFit3D.C.