pyROOT: problem calculating IntegralError

Hi ROOTers,

I’m having a problem calculating IntegralError in pyROOT – the calculation completes without throwing an error, but the value returned is nonsensical or changes on subsequent calculation.

I’m using ROOT v 5.34.32 and python 2.7.10 on mac os 10.10.5

I attach a sample code so you can see what I mean:

import ROOT

gRandom = ROOT.TRandom3()

h = ROOT.TH1D('h','random',100,0.0,100.0)
for i in range(10000):

f = ROOT.TF1('fit_%s'%(h.GetName()),'gaus',40.0,50.0)

r = h.Fit(f,"RNS")

print f.IntegralError(40.0,50.0,r.GetParams(),r.GetCovarianceMatrix().GetMatrixArray())

Thanks for your input!

The problem is in the Python garbage collection and C++ returning an address of a member of a object returned by value. The function r.GetCovarianceMatrix() returns a TMatrixTSym by value and GetMatrixArray() returns a pointer to the internal data of the matrix. Depending when the TMatrixTSym goes out of scope, the pointer will be invalid. This is exactly what happens in Python. The object returned gets deleted while you still have a reference to it. To avoid it you can keep a reference to the retuned TMatrixTSym object while you still use it. Something like this:

cov =  r.GetCovarianceMatrix()
print f.IntegralError(40.0,50.0,r.GetParams(), cov.GetMatrixArray())
1 Like

Thanks @mato! That’s done it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.