Strange behavior with TF1 and SetParameters

Dear all,

After a moment to find from were this problem was coming, I realized that applying SetParameters(…) or SetParameter(…) on a TF1 reset the properties of the axis that have been previously defined. This is due to, from what I have understood to the Update() that is done after the parameters definition.

As a small example:

root [0] TF1 *f = new TF1("f","gaus",-10,10);
root [1] f->GetXaxis()->SetTitle("axis name")
root [2] f->GetXaxis()->GetTitle()
(const char *) "axis name"
root [3] f->SetParameters(0,1)
root [4] f->GetXaxis()->GetTitle()
(const char *) ""

I don’t know if this behaviour is really wanted but in some cases it is really annoying.


Jérémie Dudouet

Dear @dudouet ,

This looks like a bug to me, but I will ask @couet to comment further.


Yes Update() is the problem. It delete the underlying histogram, but the axis titles are hold by this histogram, so they are removed. I am not sure it can be easily fixed. I will investigate a bit more.

Indeed the underlaying histogram must be deleted because the function changes after SetParameters. As the titles are stored in the TAxis belonging to the underlaying histogram, they also disappear.

In the user’s code the simple workaround is of course to set the axis titles after setting the parameters, just before drawing.

I am not sure we can safely re-create a new histogram just after the deletion of the existing one in Update() ? I guess @moneta will know better. If that’s possible I can make a PR for that.

EDIT: right now Update() blindly deletes the existing histogram, without re-creating it.

Thanks for your answer. Indeed, setting the titles after SetParameters is the solution I found. But for example in my case, I have a graphical interface that allows to modify the parameters of a function and in real time seeing the function modification. That means that each time the user change something, the axis needs to be set again which is not ideal.

Yes I perfectly understand that in some cases this workaround is unpractical. Let see what @moneta will say about “re-creating the underlying histogram in Update()”, if that’s possible we will be able to copy the histogram’s axis title (and my be some other attributes) to the newly created histogram before deleting the old one.

1 Like

I think we can improve the situation here. TF1::Update is called when setting new parameters, changing the number of sampling point or changing the range. In the first case we don’t need to delete the histogram, will be enough to Reset and re-fill it again with the new function content.
For the other cases, we probably need to delete but we could improve by re-creating preserving the axis attributes and possibly also some other attributes.


1 Like

May me we can have only one case. It looks like re-creating the histogram in Update(0 will work in both cases. The question was: is it safe or not. It seems you agree it is.

Yes, this should work too, but then you need to re-use any existing attribute

1 Like

Thanks, I will investigate.

Github issue and PR here: Strange behavior with TF1 and SetParameters · Issue #14385 · root-project/root · GitHub

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