I have a python code which reads in a TTree and writes certain branches to a new TTree, while also adding some new branches. For these new branches, I read in a value and evaluate a RooAbsPdf at the position of that value. The RooAbsPdfs are read from a workspace. Now while trying to improve the speed of the tree writing, I noticed that the Roo part takes more than 90% of the total time.

For example for the met variable, before the loop, I do:

metPdf = w.pdf(“met_analyticalPDF_DA”) # In this case, it is a RooAddPdf
met_Edge = w.var(“met_Edge”) # the PDF depends on this variable

Then, in the loop, I do this:

met_Edge.setVal(met[0]) # met[0] is read from the original tree
obsMet = ROOT.RooArgSet(met_Edge) # build argset from the variable
data.metPdfVal = metPdf.getVal(obsMet) # evaluate the PDF to build a log-likelihood from it later

So my question is:
Can I do this in a faster way, i.e., is my way of evaluating the PDF slow? Or is there perhaps a way to convert the RooAbsPdf to something else that is faster to evaluate?

I don’t see an immediate problem, but please note that python loops can be expensive in general.

Two ideas for you to test/check:

Maybe pull making the RooArgSet obsMet out of the loop. It looks like unnecessary overhead to make a new one every time. Setting the value of the observable will also work if you don’t put it in an ArgSet every time. Passing the ArgSet is needed to convey over which variable the PDF needs to be integrated.

Could it be that the PDF cannot be integrated analytically? In this case, a numerical integral might be consuming the time.

Hi Stephan,
Thank you for your answer. Indeed, pulling the RooArgSet definition out of the loop made the execution speed much faster without changing results.

About the second part, I am not sure. I think it should be possible to analytically integrate most of the used functions (sum of two exponentials, polynomial). For Crystal Ball (which I also use) I am not sure how it is handled, I would assume that there is some Erf for the gaussian part? Would I get a message if the integration had to be done numerically or is there a different way to find out?

The Crystal ball shape indeed has an analytic integration function. I guess that you can already see from the improved speed that analytic integration is possible. If you didn’t touch any message settings, you should also see a message about numerical integrals.