Good morning
I’m using RooFit to fit a pdf with several observables, with one of them being a RooCategory, with possible values (-1,0,1).
Part of the final pdf is a RooRealSumPdf, with some of the coefficients changing sign depending on the RooCategory. This was implemented by multiplying the coefficients by the category value inside a RooFormulaVar.
The fit runs fine, but when trying to plot it crashes with a cryptic backtrace (segfault inside ostream::sentry…). Investigating the crash it appear that the plotting code of RooFormulaVar, when searching for a proper sampling, assumes that every variable inside the function is a RooAbsReal and static_casts to that, which breaks in the case of my coefficient.
What is the proper way to do this? The simple solution for now is lie about the continuity of the discrete variable and make it a RooRealVar, which is something I can still do at this point of the analysis, but as we add new features in the fit I will need the discrete variable to be actually discrete.
Additionally, would it be possible to add a diagnostic during the construction of the formulavar? Since the fit was running fine and giving correct results I didn’t think the error was on the coefficients at the beginning, I had to inspect the RooFormulaVar source code to understand that placing a RooCategory inside was not supported.
It sounds like your use case hadn’t been foreseen when the class was designed. Could you point me to the location of the crash, maybe even include a 5 line demo?
I assume that the proper way is to dynamic_cast in RooFormulaVar::plotSamplingHint(). I changed that already locally, but I don’t have a model to test if the plotting works now.
On my computer when inside plotSamplingHint, when forwarding the call to the subvariables (line 230 of RooFormulaVar.cxx on the master), the function calls instead RooAbsArg::printArgs (due to the corrupted vtable probably). Since there is no actual ostream object the first string printed crashes the macro.
Another fix might be doing something similar to RooProduct, which keeps separate collections for real and category values (something that sadly I noticed only just now, it’s a good workaround).