RooFit: Extract the final fit function with RooSimultaneous

I’m fitting a data set with a RooSimultaneous model. The fit proceeds smoothly and I obtain reasonable results. I plot the fit results on a RooPlot against the data, and it looks good. Then, I would like to save that final fit function (with final fit values) as a histogram, and finally, draw toy Monte Carlo events from the final fit function. The code looks something like this:

//Define the necessary objects
RooSimultaneous* modelPDF = …;
RooPlot* frameEnergy = …;
RooDataSet *data = …;
RooRealVar energy(…);
RooCategory ChDs(…);

//Operations on all the above variables, leading up to a fit
modelPDF->fitTo(*data, Minos(kTRUE), Extended());

//Plot the results of the fit
modelPDF->plotOn(frameEnergy, ProjWData(*data), LineColor(kRed));

//Create a histogram from the model PDF

//Create a toy MC study to draw 20000 events from the fit function with final values
RooMCStudy mgr(*modelPDF, *modelPDF, RooArgList(ChDs, energy), “”, “v”);
mgr.generate(1, 20000, 1,(dirName+"/toyMC/toy_%04d.txt").c_str());

The situation is very peculiar when I compare the three plots, as they do not line up with each other. There is a normalization mis-match, but that is fixed by renormalizing to a baseline value.
The really puzzling thing is there is a clear peak in the model, and those peaks do not match in the three plots. Furthermore, there is a secondary peak that matches between the “plotOn” version and the “createHistogram” version, but is completely absent in the toy Monte Carlo [it may be useful to say that the secondary peak is negative].

I suspect that the cause for this discrepancy is connected with the fact that in plotOn, I have a projection onto the data set ProjWData, which is absent in the other two cases. However, I have not found a way to do this projection in createHistogram or in the toy MC study.
Is there a way to extract a “final fit function” from the RooSimultaneous object.

I should also point out that there is a RooWorkspace associated as well, but when I tried extracting the PDF from there, it exhibited similar problems.


Can you please post the RooWorkspace with your model and a running script so I can investigate ?

Thank you