RooFit Seg Fault with vectors of RooHistPdfs

Hello,

I have been writing a class to take any number of template histograms and fit them to a data histogram. To this end I would like to pass a vector of TH1s and create a vector of RooHistPdfs with which I can construct a RooAddPdf model.
When I loop over the input vector of TH1s and create the RooDataHists, RooRealVars and RooHistPdfs I find that I get a seg fault. This can be avoided by moving the RooHistPdf section to a separate but identical loop.

I have attached a simple macro to demonstrate this behaviour. It will currently seg fault on running but if you un-comment lines 59-64 and put the RooHistPdfs into a separate loop it runs as expected.
I am using ROOT 5.34/17 but have reproduced this issue using ROOT 6.00/00 as well.

Can you please explain this behaviour to me and why I am unable to create all the RooFit objects in a single loop?

Many thanks,
Jim
badLoop.C (2.79 KB)

Hi,

I don’t have a crash on my machine, but looking at your code I see a potential problem. You are inserting pdf’s object in a std::vector by value. This is can be problematic because you risk to create temporary objects.
In principle it should work, but in practice it might not due to some complications by calling copy constructors and/or assignment operators.
I would recommend you to change the program to store pointers in the std::vector, as you are already doing for the histogram

Best Regards

Lorenzo

Hi Lorenzo,

Many thanks for your reply. You are correct that my issue was the RooDataHists were temporaries and adding using the vectors of pointers solves the problem.

My code now contains:

RooDataHist* temp_RooDataHist = new RooDataHist( ... );
mc_RooDataHists.push_back( temp_RooDataHist );

Thanks again,
Jim