Fitting of a TGraph2D with a 3D straight line.
run this macro by doing:
Processing /builddir/build/BUILD/root-6.10.00/tutorials/fit/line3Dfit.C...
Total Initial distance square = 8.65172e+07
Total final distance square 19988
****************************************
MinFCN = 19988
NDf = 0
Edm = 6.31218e-10
NCalls = 257
Par_0 = 10.3552 +/- 0.394538
Par_1 = 19.9602 +/- 0.0689481
Par_2 = 0.999198 +/- 0.0444209
Par_3 = 2.00379 +/- 0.00780945
(int) 0
using namespace ROOT::Math;
void line(
double t,
const double *p,
double &x,
double &y,
double &z) {
x = p[0] + p[1]*t;
y = p[2] + p[3]*t;
z = t;
}
bool first = true;
struct SumDistance2 {
SumDistance2(
TGraph2D *g) : fGraph(g) {}
double distance2(double x,double y,double z, const double *p) {
return d2;
}
assert(fGraph != 0);
double * x = fGraph->GetX();
double * y = fGraph->GetY();
double * z = fGraph->GetZ();
int npoints = fGraph->GetN();
double sum = 0;
for (int i = 0; i < npoints; ++i) {
double d = distance2(x[i],y[i],z[i],par);
sum += d;
}
if (first) {
std::cout << "Total Initial distance square = " << sum << std::endl;
}
first = false;
}
};
{
double p0[4] = {10,20,1,2};
double err = 1;
}
SumDistance2 sdist(gr);
double pStart[4] = {1,1,1,1};
if (!ok) {
Error(
"line3Dfit",
"Line3D Fit failed");
return 1;
}
std::cout <<
"Total final distance square " << result.
MinFcnValue() << std::endl;
int n = 1000;
double t0 = 0;
double dt = 10;
for (
int i = 0; i <
n;++i) {
}
for (
int i = 0; i <
n;++i) {
}
return 0;
}
return line3Dfit();
}
- Author
- Lorenzo Moneta
Definition in file line3Dfit.C.