How to properly redefine pdf in RooWorkspace?

ROOT version:6.24/06
Platform: lxplus7

Dear ROOT experts,

I encounter the following error when attempting to redefine existing pdfs in RooWorkspace.

The error message:

Error in <ROOT::Math::Fitter::SetFCN>: FCN function has zero parameters 
python3: /home/conda/feedstock_root/build_artifacts/root_base_1654795774398/work/root-source/math/mathcore/inc/Fit/Fitter.h:385: const ROOT::Fit::FitResul
t& ROOT::Fit::Fitter::Result() const: Assertion `fResult.get()' failed.
 *** Break *** abort

...(some segfault error messages)


Traceback (most recent call last):
  File "/afs/cern.ch/work/h/hawu/private/b2dmumu/demo/recreate.py", line 77, in <module>
    r = pdf.fitTo(data, ROOT.RooFit.Save(True))
  File "/cvmfs/lhcbdev.cern.ch/conda/envs/default/2022-06-15_17-18/linux-64/lib/python3.9/site-packages/ROOT/_pythonization/_roofit/_rooabspdf.py", line0, in fitTo
    return self._fitTo(*args, **kwargs)
TypeError: none of the 2 overloaded methods succeeded. Full details:
  RooFitResult* RooAbsPdf::fitTo(RooAbsData& data, const RooCmdArg& arg1 = RooCmdArg::none(), const RooCmdArg& arg2 = RooCmdArg::none(), const RooCmdArgarg3 = RooCmdArg::none(), const RooCmdArg& arg4 = RooCmdArg::none(), const RooCmdArg& arg5 = RooCmdArg::none(), const RooCmdArg& arg6 = RooCmdArg::none( const RooCmdArg& arg7 = RooCmdArg::none(), const RooCmdArg& arg8 = RooCmdArg::none()) =>
    AbortSignal: abort from C++; program state was reset
  RooFitResult* RooAbsPdf::fitTo(RooAbsData& data, const RooLinkedList& cmdList) =>
    TypeError: could not convert argument 2

I made the following minimal code example for you to reproduce the problem.

  • The create.py file
    1. create a new RooWorkspace
    2. import RooDataSet from RDataFrame
    3. define pdf model
    4. perform fitTo
    5. save everything to a .root file.
  • The recreate.py file
    1. import RooWorkspace from that saved .root file
    2. remove all vars, pdfs, formulas and objects
    3. define new pdf model
    4. perform fitTo → where the error is triggered

Hopefully with attached files, you can reproduce my error by running

python3 create.py # -> should run successfully
python3 recreate.py  # -> show the error above

PS. maybe redefining existing pdfs in RooWorkspace doesn’t require the removal of them in the first place. In RooWorkspace::factory, it seems to me that one can use the expression “EDIT:name” to redefine objects but with the given description in the webpage I don’t know how to do it.

create.py (1.1 KB)
recreate.py (1.6 KB)
demo.root (249.2 KB)

I’m sure @jonas can help you

Hi @Patrick_Wu,

the crash is because you forgot to define n2 in the recreate.py script. This line is missing:

ws.factory("n2[2]")

Furthermore, there are problems because the workspace is not fully cleared. There are still some internal caching sets left (you can see them with ws.Print("v")). You have to remove these too:

ws.removeSet("CACHE_CONSTR_OF_PDF_model_FOR_OBS_fit_var")
ws.removeSet("CACHE_PARAMS_OF_PDF_model_FOR_OBS_fit_var")

Of course, the user should not have to bother with the details of the caching, so it is a bug that these now invalid cached sets are still there. I have opened a PR to remove the cached sets when they get invalidated automatically:

I hope this helps, let me know if you have further questions!

Cheers,
Jonas

Silly me! Thanks for not only identifying the missing line but also offering extra explanation.