RooFit: Cannot get the fit result from RooAbsPdf::fitTo ( )

Hello,

I’m having a problem getting the fit result from RooAbsPdf::fitTo().
The fit works if I do:

myRooFitResult = myRooAbsPdf -> fitTo (*myRooDataHist);

but it doesn’t return the fit result. From the RooAbsPdf class reference page and from the RooFit tutorial, I found that I need to add Save() as an argument to my fitTo( ) function.
but when I do:

myRooFitResult = myRooAbsPdf -> fitTo (*myRooDataHist, Save());

I get the follow error:

error: use of undeclared identifier ‘Save’

The root version I’m using is 6.24/00.

Did I make any mistake, or are there changes in RooFit that is not documented yet?

Here is the code that I use
fitter_RooFitQuestion.C (3.6 KB)

Thank you,
Hoa

I just want to update that I made a mistake in my code. I forget to add:

using namespace RooFit

So it didn’t work.

1 Like

Hi @LongHoa ,
is everything working correctly with the fix or do you still have issues?

Cheers,
Enrico

Hi Enrico,

I still have problem with the fit.
Let me describe my program a bit.
I’m trying to fit a target histogram by the sum of 2 other histogram which I call signal and background.
All three histograms are created by TH1::FillRandom()

hist_sig -> FillRandom ("gaus", nRandom);
hist_bkg -> FillRandom ("landau", nRandom);

float scale_target_sig = 1.5;
float scale_target_bkg = 1.1;
hist_target -> FillRandom ("gaus",   nRandom*scale_target_sig);
hist_target -> FillRandom ("landau", nRandom*scale_target_bkg);

The Target histogram is converted into RooDataHist while the signal and background are converted to RooHistPdf

RooRealVar *varX    = new RooRealVar ("varX",    "x-variable", -5, 10);
varX -> setBins (30);
RooDataHist *roohist_target = new RooDataHist ("roohist_target", "conversion of target", *varX, RooFit::Import(*hist_target));
RooDataHist *roohist_sig    = new RooDataHist ("roohist_sig",    "conversion of sig",    *varX, RooFit::Import(*hist_sig));
RooDataHist *roohist_bkg    = new RooDataHist ("roohist_bkg",    "conversion of bkg",    *varX, RooFit::Import(*hist_bkg));

RooHistPdf *template_sig = new RooHistPdf ("template_sig", "histogram-pdf for signal",     *varX, *roohist_sig);
RooHistPdf *template_bkg = new RooHistPdf ("template_bkg", "histogram-pdf for background", *varX, *roohist_bkg);

I tried two different ways to fits:
Model 1

RooRealVar *fracsig = new RooRealVar ("fracsig", "fraction of Sig", 15000, 0.0, 30000);
RooRealVar *fracbkg = new RooRealVar ("fracbkg", "fraction of Bkg", 10000, 0.0, 30000);
RooAddPdf *fitmodel = new RooAddPdf (
		"fitmodel", "",
		RooArgList(*template_sig, *template_bkg),
		RooArgList(*fracsig, *fracbkg));

fitmodel -> fitTo (*roohist_target);

Model 2

RooRealVar *fracsig = new RooRealVar ("fracsig", "fraction of Sig", 0.5, 0.1, 0.9);
RooAddPdf *fitmodel = new RooAddPdf (
		"fitmodel", "",
		RooArgList(*template_sig, *template_bkg),
		RooArgList(*fracsig));
fitmodel -> fitTo (*roohist_target);

Both models will fail if I set scale_target_sig (in the first code block) to be larger than 1.0.
The error RooFit returns is:

RooMinimizerFcn: Minimized function has error status.
Returning maximum FCN so far (-inf) to force MIGRAD to back out of this region. Error log follows.
Parameter values: fracsig=0.5
RooAddPdf::fitmodel[ fracsig * template_sig + [%] * template_bkg ]
getLogVal() top-level p.d.f evaluates to zero @ !refCoefNorm=(), !pdfs=(template_sig = 0/1,template_bkg = 0/1), !coefficients=(fracsig = 0.5)
RooNLLVar::nll_fitmodel_roohist_target[ paramSet=(fracsig) ]
function value is NAN @ paramSet=(fracsig = 0.5)

Furthermore, if I change the order of event generation, the scale_target_bkg will affect the results.

Here is the new version of my code, you can use it to reproduce the result:
fitter_RooFitQuestion.C (3.8 KB)

Thank you very much,
Hoa.

Thank you for the clarification. We need @moneta or @jonas here, let’s ping them.

1 Like

Hi @LongHoa,

you are on the right path!

The problem is that the pdf needs to be strictly positive everywhere you have a data point, otherwise the likelihood is zero either way and your fit doesn’t go anywhere (or equivalently, the log-likelihood is minus infinity like we see in the log you posted).

With the random numbers, your target template gets some entries in the third bin, with both the signal and background templates don’t. I don’t know if this is an acceptable solution in your usecase, but you can fix your problem by restricting the range of the RooFit variable to exclude this bin:

RooRealVar *varX = new RooRealVar("varX", "x-variable", -3.5, 10);
varX->setBins(27);

Alternatively, you could scale up the number of events to get all bins populated. But maybe you can’t do that, because the template is some toy replacement to data where you expect exactly that number of events.

Hope this helps!

Cheers,
Jonas

1 Like

Hi Jonas,

Thank you. Your explanation is very clear, the solution you suggested solve my problem.

Cheers,
Hoa.

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