Problem setting parameters with pyroot

Hi guys

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])
c.Draw() # Necessary to make the graphics show!

however only an empy Tcanvas appears, why?
Thank you all for your help

Hi @Adrianodelvincio!

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])

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.

I hope that helps!

1 Like

Hi jonas

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

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