Get x value for linear fit

Hello,

I am using the linear fit:

TF1*fit= new TF1(“fit”,“pol1”,0,1);

I can read out the parameter (slope and y-axis)
with
fit->GetParameter(0);
fit->GetParameter(1);

Now, I want to know the value where the fit crosses the x-axis (that means the value where y=0)
How can I read out this value?

Thanks!
Gordon

Hi,

For a polynomial you can solve the equation y = p1*x + p0 = 0
So, x = -p0/p1

For a generic TF1 you can also call the method TF1::GetX

root.cern.ch/root/html/TF1.html#TF1:GetX

Lorenzo

Hi Gordon,

In addition to what Lorenzo pointed out, it’s important to note that for arbitrary functions, there can be many roots (i.e., many values fox X where F(x) = 0). This means you want to use TF1::GetX() carefully. :smiley:

Cheers,
Charles

Hi Charles and Lorenzo,

thanks for answer!

Gordon

Hello, I have a similar question. I have a histogram with a cluster of data points. How would I extract the x and y values in order to get an equation for a line y = mx + b ?? Thanks in advance

1 Like

Hi,

I am not sure I have understood your question. You have an histogram and what do you want to do ? A fit to a y = a* x + b function and extract the a and b parameters ?

Or do you want the y (bin contents) and x ( bin center) values of the histograms ?

Lorenzo

Hi Lorenzo,

What I meant to say is, I have a 1D histogram, I want to fit 2 lines with eqn y = mx + b over 2 different y-ranges (vertical axis).

i.e If I have 2 points from y = 0 to y = 100, I want to fit a line through those points. And if I have 2 points from y = 200 to y = 300, I want to fit another through those points as well.

I cannot think of another way to do this as TF1 only takes in an x-range (horizontal axis)?

Hi,

You should get the x range corresponding to your points, or extract your points and make a separate object (e.g. a Graph) and fit that one

Lorenzo

Ok, I guess the better question is, how can I extract the x value from the data point, given that I know what the y-value is?

Hi,
If you know the y value exactly, you can loop on the bins to find it. Otherwise you can try to use something like
TH1::FindFirstBinAbove or TH1::FindLastBinAbove

Lorenzo