#include "Fit/BinData.h" #include "Fit/Fitter.h" #include "Math/WrappedMultiTF1.h" #include "TF1.h" #include "TRandom.h" using namespace ROOT::Fit; using namespace ROOT::Math; void fitArray() { // generate first data arrays const int n = 100; double x[n]; double y[n]; double erry[n]; // error in y double A = 2; double B = 3; double E = 0.1; for (int i = 0; i < n; ++i) { x[i] = static_cast (i); // generate y as a linear funxtion of x plus a random gaussian term y[i] = A * x[i] + B + gRandom->Gaus(0, E); erry[i] = E; } // now fit the data TF1 * f1 = new TF1("f1","pol1"); // create fit data class double *errx = 0; //assume no error in x ROOT::Fit::BinData data(n,x,y, errx, erry); // create function class ROOT::Math::WrappedMultiTF1 wf(*f1); // create fitter class and fit ROOT::Fit::Fitter fitter; if (!fitter.Fit(data, wf) ) std::cerr << "Error Fitting " << std::endl; else fitter.Result().Print(std::cout); }