How to call operator int () from Python?

Dear all,

I am trying to access the fStatus member of a TFitResultPointer from Python. In C++, one can access it by casting the TFitResultPointer to int, but I could not figure out how to do this from Python ( int(fitResult) did not seem to work, neither did fitResult.fStatus [fStatus is private] ).

Unfortunately, there is no “regular” member function in TFitResultPointer to access fStatus either…

Thanks for any help,

Norbert

Norbert,

which version are we talking? Currently, operator int() is mapped to python’s int, which should do the trick. Otherwise I’m thinking that calling ptr.Get().Status() should yield the same?

Cheers,
Wim

Dear Wim,

thanks for your reply!

I am talking about ROOT v5.26 and Python v2.6.5:

braun@groofy:~$ root-config --version
5.26/00
braun@groofy:~$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
>>> import array
>>> x = array.array("d", (1,2))
>>> y = array.array("d", (2,4))
>>> gr = ROOT.TGraph(2, x, y)
>>> res = gr.Fit("pol1", "S")

****************************************
Minimizer is Linear
Chi2                      =	8.53931e-25
NDf                       =	0
p0                        =	1.18071e-12  	+/-	2.23607     
p1                        =	2            	+/-	1.41421     
>>> int(res)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string or a number, not 'TFitResultPtr'
>>> res.__int__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'TFitResult' object has no attribute '__int__'
>>> res.Get().Status()
0
>>> 

The ptr.Get().Status() workaround works as long as I pass the “S” argument to the Fit() function. However, the rest of my program (not the example above) extracts the fit result from the function object, so this seems kind of unnecessary.

Best regards,

Norbert

Norbert,

just checked also and the operator int to int mapping wasn’t in v5.26 yet. Is in trunk, though.

Not sure whether I understand your other comment, does the workaround solve the problem or not (i.e. would a backport of the mapping to v5.26 be needed)?

Cheers,
Wim

Dear Wim,

the script I am trying to write is supposed to run with older versions of ROOT as well as with newer ones. Thus, I now have to test whether the result returned by the Fit() function is of type int or of type ROOT.TFitResultPtr, and act accordingly. Had the mapping already been in place, I could have handled both cases by writing int(result).

However, this is not a serious problem, so the workaround will do for me. In any case, there are now version of ROOT “in the wild” where the workaround would be needed.

Best regards,

Norbert