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
Author: Lorenzo Moneta
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Monday, March 27, 2023 at 09:47 AM.
const int n = 1000;
double x[n], y[n], z[n], v[n];
double ev = 0.1;
generate the data
TRandom2 r;
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
ROOT::Fit::BinData data(n,3);
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);
f3->SetParameters(2,2,2);
ROOT::Fit::Fitter fitter;
input_line_53:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration ROOT::Fit::BinData data(n,3); ^
wrapped the TF1 in a IParamMultiFunction interface for the Fitter class
ROOT::Math::WrappedMultiTF1 wf(*f3,3);
fitter.SetFunction(wf);
bool ret = fitter.Fit(data);
if (ret) {
const ROOT::Fit::FitResult & res = fitter.Result();
// print result (should be around 1)
res.Print(std::cout);
// copy all fit result info (values, chi2, etc..) in TF3
f3->SetFitResult(res);
// 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);
else
std::cout << "Good fit : p-value = " << prob << std::endl;
}
else
Error("exampleFit3D","3D fit failed");
input_line_58:2:24: error: reference to 'data' is ambiguous bool ret = fitter.Fit(data); ^ input_line_53:2:21: note: candidate found by name lookup is 'data' ROOT::Fit::BinData data(n,3); ^ /usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data' data(initializer_list<_Tp> __il) noexcept ^ /usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data' data(_Container& __cont) noexcept(noexcept(__cont.data())) ^ /usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data' data(const _Container& __cont) noexcept(noexcept(__cont.data())) ^ /usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data' data(_Tp (&__array)[_Nm]) noexcept ^