Error in using ROOT::Fit::Fitter::LikelihoodFit

Hello,
I need to do an extended likelihood fit of a histogram. Since using

hist->Fit("my_func","L");

gives a likelihood fit, I need to use the ROOT::Fit classes.
I followed the guide: https://root.cern.ch/root/htmldoc/guides/users-guide/FittingHistograms.html#new-rootfit-classes
And here is my code:

void fit_eml (TH1 *generated, TF1* func){
  TH1* hist = (TH1*)generated->Clone("EML fit");
  hist->SetNameTitle("EML fit", "EML_fit");
  ROOT::Fit::DataOptions opt;
  ROOT::Fit::DataRange range(0,2);
  ROOT::Fit::BinData data(opt, range);
  ROOT::Fit::FillData(data, hist);

  ROOT::Math::WrappedMultiTF1 fitFunction(*func, func->GetNdim() );
  ROOT::Fit::Fitter fitter;
  fitter.SetFunction(fitFunction, false);
  ROOT::Math::MinimizerOptions::SetDefaultMinimizer("Minuit2");
  ROOT::Fit::Fitter::LikelihoodFit(data);

  TCanvas *c = new TCanvas("Fit EML", "Fit EML", 800, 400);
  gPad->SetLeftMargin(0.15);

  hist->Draw();
}

Trying to compile this code I get the error:
error: call to non-static member function without an object argument
ROOT::Fit::Fitter::LikelihoodFit(data);
-------------------------^----------------

Thank you for your answer, I don’t know what I’m doing wrong.
Riccardo

PS
Root version ROOT 6.14/06

Hi,
You need to do:

fitter.LikelihoodFit(data);

since LikelihoodFit is a non-static member function

Lorenzo

Hi,
Thank you for your answer, sadly now I’ve a new problem:

IncrementalExecutor::executeFunction: symbol '_ZN7vecCore4math3PowIdEET_RKS2_S4_' unresolved while linking [cling interface function]!
You are probably missing the definition of double vecCore::math::Pow<double>(double const&, double const&)
Maybe you need to load the corresponding shared library?
IncrementalExecutor::executeFunction: symbol '_ZN9__gnu_cxxL27__exchange_and_add_dispatchEPii' unresolved while linking [cling interface function]!
You are probably missing the definition of __gnu_cxx::__exchange_and_add_dispatch(int*, int)
Maybe you need to load the corresponding shared library?

Why is there this problem? What libraries do I need to load?
Thank you.
Riccardo

Hi
It seems you have a built of ROOT with VecCore. In that case you might need to add -lVecCore to the linker.

Lorenzo

Thank you!
One last question, how can I Plot the fit on the canvas with the histogram?
Here the last version of the code:

  TH1* hist = (TH1*)generated->Clone("EML fit");
  hist->SetNameTitle("EML fit", "EML_fit");
  TCanvas *c = new TCanvas("Fit EML", "Fit EML", 800, 400);
  gPad->SetLeftMargin(0.15);
  hist->Draw();
  ROOT::Fit::DataOptions opt;
  ROOT::Fit::DataRange range(0,2);
  static ROOT::Fit::BinData data(opt, range);
  ROOT::Fit::FillData(data, hist);

  ROOT::Math::WrappedMultiTF1 fitFunction(func, func.GetNdim() );
  ROOT::Fit::Fitter fitter;
  fitter.SetFunction(fitFunction, false);
  fitter.Config().SetMinimizer("Minuit2", "Migrad");
  fitter.Config().MinimizerOptions().SetErrorDef(0.5);
  fitter.LikelihoodFit(data);
  ROOT::Fit::FitResult fit_result = fitter.Result();
  fit_result.Print(cout);

Thank you!
Riccardo

Hi,
You can do as following:

func->SetParameters(fit_result.Parameters().data()); 
hist->Draw(); 
func->Draw("SAME"); 

Lorenzo

1 Like

Thank you Lorenzo! Thanks for your advices, they were very helpful.

Riccardo

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.