Cannot get TF1 with Sqrt() to plot


ROOT Version: JupyROOT 6.22/06
Platform: macOS Ventura 13.2
Compiler: Jupyter Lab


I am making TF1 objects from functions in python 3.11.0. I have been unable to get a sqrt to work, either via Sqrt() or Pow(x,1/2). I can get Power to work for when it is not raised to 1/2. My code looks like the following:

def background(X, P):
    return ROOT.TMath.Power( P[1]*(P[0]-X[0]), 2 )
can = ROOT.TCanvas("can", "can")

show_background = ROOT.TF1("show_background", background, 0.15, 0.45, 2)
show_background.SetParameters( 0.37, 2)
show_background.DrawClone()

can.Draw()

However, when I try ROOT.TMath.Sqrt( P[1]*(P[0]-X[0]) ) or ROOT.TMath.Power( P[1]*(P[0]-X[0]), 1/2 ) in the function background I get an empty canvas. My end goal is to use the Sqrt() in fits, but I can’t even get it to plot properly. Is there something obvious that I am missing for getting Sqrt() to work? Even when I did get it to sometimes “plot” it was a straight line. I can provide the outputs that I see on my end as well, if this is an issue specific to my environment.

Try with the power “1” (see how your function becomes negative).

BTW. I think you cannot “clone” this type of function.

Hello Wile,

1.) Thank you for pointing that out re Clone

2.) Yes, I see that with a power of 1 it is just a line that goes negative y for positive x.

I have been trying to think of why this will not plot in root. Is this an issue because of the (P[0]-X[0]) term? I am trying to use that to get the model to terminate at some maximal x value P[0], hence P[0]-X[0]. I tried plotting ROOT.TMath.Power( P[0]*X[0], 1/2) and that does indeed seem to return a reasonable result. But now I am stuck with the issue of being unable to use (P[0]-X[0]).

Uncle Google → sqrt(-1)

Ah. I do feel silly now. I was able to plot it with some random values in Mathematica but I do forget that Mathematica != root…