So, I just introduced Roofit into my analysis code and the fitting is going well. I have a class which has a fitting method that is called from another class method. The code works perfectly fine if I don’t try to plot the the RooPlots. If I do use RooPlot, the plots are created and stored in the root file, but at the very end I get a core dump. I could go on my merry way and keep deleting core dumps, but that’s kindof silly.
Solution:
Not only does one need to delete the frame, but a write command is also required.
So, by example
RooPlot *frame;
Data.plotOn(frame);
frame->Write();
delete frame;
I’ve tried deleting the frame after the plot is done, but then I get nothing in my root file afterwards (no core dump though). Are there issues with RooPLot running in batch mode (compiled code). Here’s the core dump message:
Error in TUnixSystem::DynamicPathName: libMinuit.so does not exist in
Error in TUnixSystem::DynamicPathName: libMinuit.so does not exist in
Error in TCint::AutoLoad: failure loading library libRooFit.so for class RooPrintable
Segmentation fault (core dumped)
Here’s the fitting method:
void PidTable::FitHisto(TH1D* hist, Double_t &sig, Double_t &err)
{
double m1a, mi, mf, w1a, wi, wf, w2a;
m1a = 1.8694; mi = 1.855; mf = 1.875;
w1a = 0.004; wi = 0.002; wf = 0.025; w2a = 0.01;
//Build RooFit model
RooRealVar CandidateMass(“CandidateMass”,"ParentMass ", ParentMass, Hist_lower, Hist_upper, “GeV/c^2”);
RooRealVar mean1(“mean1”, “mean1”, m1a, mi, mf);
RooRealVar width1(“width1”, “width1”, w1a, wi, wf);
RooRealVar width2(“width2”,“width2”, w2a, wi, wf);
// RooRealVar m(“m”, “slope”, 0.0, -100.0, 100.0);
// RooRealVar o(“o”, “offset”, 2.0, -100.0, 10000.0);
RooRealVar m(“m”, “Exponential Parameter”, -60.0, 40.0 );
RooRealVar frac(“frac”, “fraction of gaussian”, 0.1, 0.0, 1.0);
RooRealVar N0(“N0”, “# signal events”, 100.0, 0.0, 80000.0);
RooRealVar nBkg(“nBkg”, “# Background events”, 10.0, 0.0, 20000.0);
//RooPolynomial bg(“bg”, “Background shape”, CandidateMass);
RooExponential bg(“bg”, “Background shape”, CandidateMass, m);
RooGaussian gauss1(“gauss1”, “Signal 1”, CandidateMass, mean1, width1);
RooGaussian gauss2(“gauss2”, “Signal 2”, CandidateMass , mean1, width2);
RooAddPdf signal(“signal”,“Signal”, RooArgList(gauss1,gauss2), RooArgList(frac));
RooAddPdf both(“both”,“Signal+Background”, RooArgList(signal, bg), RooArgList(N0, nBkg));
RooDataHist H(“H”, “H”, RooArgList(CandidateMass), hist);
both.fitTo(H, “me”);
double N = N0.getVal(),
f = frac.getVal(),
m1 = mean1.getVal(),
w1 = width1.getVal(),
m2 = mean1.getVal(),
w2 = width2.getVal(),
n = nBkg.getVal(),
p = m.getVal(),
Nerr = N0.getError(),
ferr = frac.getError(),
m1err = mean1.getError(),
w1err = width1.getError(),
m2err = mean1.getError(),
w2err = width2.getError(),
nerr = nBkg.getError(),
Mean = fm1 + (1-f)m2,
errMean = sqrt((m1-m2)ferr(m1-m2)ferr + fm1errfm1err +
(1-f)m2err(1-f)m2err),
Sigma = sqrt(f(w1)(w1) + (1-f)(w2)(w2) + f(1-f)(m1-m2)(m1-m2)),
errSigma = (1/2Sigma) * sqrt((w1w1-w2w2+(1-2f)(m1-m2)(m1-m2))(ferr)(ferr) +
4*(fw1w1err)(fw1w1err) + 4(fw2w2err)(fw2w2err) +
4(f*(1-f)(m1-m2))(f*(1-f)(m1-m2))((m1err)(m1err)+(m2err)(m2err)));
RooPlot *bframe = CandidateMass.frame(Hist_lower, Hist_upper, N_HistBins);
H.plotOn(bframe);
both.plotOn(bframe,Components(RooArgList(signal, bg)), LineColor(kRed), LineStyle(kDashed));
bframe->SetTitle( hist->GetTitle());
bframe->Draw();
TPaveText *box = new TPaveText(0.12,0.65,.40,.90,“TRNDC”);
box->SetTextFont(12); box->Draw();
char title[100];
sprintf(title, “#chi^{2}/#nu = %.2f”, bframe->chiSquare());
box->AddText(title);
sprintf(title, “Mean=%.4f”, Mean);
box->AddText(title);
sprintf(title, “RMS=%.4f”, Sigma);
box->AddText(title);
sprintf(title, "Signal = %.0f #pm %.0f ", N, Nerr );
box->AddText(title);
sprintf(title, "Background = %.0f #pm %.0f ", n, nerr );
box->AddText(title);
sig = N;
err = Nerr;
//chi2 = bframe->chiSquare();
//delete bframe;
}