I’m trying to define a function with parameters with pyroot in jupyter-lab.
import ROOT
from ROOT import TMath
from array import array
%jsroot on
c = ROOT.TCanvas()
f1 = ROOT.TF1("func1", "x/[0]**2 * TMath::Exp(-(x**2)/(2*[0]**2))", 0, 10)
par = array('d', 1*[1])
f1.SetParameters(par)
c.Draw() # Necessary to make the graphics show!
f1.Draw()
however only an empy Tcanvas appears, why?
Thank you all for your help
The formula passed to the TF1 needs to be a valid C++ expression. You are trying to us the ** operator, which is a thing in Python but not in C++.
So if you to this, it will work:
c = ROOT.TCanvas()
f1 = ROOT.TF1("func1", "x/([0]*[0]) * exp(-(x*x)/(2.*[0]*[0]))", 0, 10)
par = array('d', 1*[1])
f1.SetParameters(par)
f1.Draw()
c.Draw()
By the way, better don’t try to generally replace x**y with std::pow, since the latter can result in a significant performance overhead.
Aftually it’s not great that PyROOT is silently taking these ** operators without warning you that they won’t work. Maybe you can open an improvement suggestion ticket on GitHub, which suggests to emit a warnings in such cases.
Thank you for your advice. If I replace TMath::Exp with “exp” the code is working, even if I use the ** operator. My problem seems connected to TMath, so I need to fix that
Anyway, I have replaced all the ** with the * operator