PyROOT using TMath function in TF1 object

I want to draw a Bessel-function of zeroth order.
Therefore I used

#!/usr/bin python2

from ROOT import *

func=TF1("func","TMath.BesselJ0(x)")

This yields the error

Error in <TFormula::Compile>:  Bad numerical expression : "TMath.BesselJ0(x)"
Error in <TF1::TF1>: function: func/TMath.BesselJ0(x) has 0 parameters instead of 1

I don’t know how to solve this. Where is the problem with TMath functions in TF1?

The 2nd parameter of the TF1 constructor is a text string parsed as a TFormla. So it should obey the C++ syntax … try:

"TMath::BesselJ0(x)"
1 Like

Thanks for the fast answer, it worked!

I tried this before, but just in a print command and obviously it didn’t work.

Yes because in the print command you should obey the Python syntax. But there, on the python side, the 2nd parameter of TF1 is just a character string which is passed to TFormula. And that’s TFormula which interpret it … and it follows the C++ syntax.

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