TGraph - Get Value(s) of X, based on value of Y

In TGraph:
Is there some function, which gives value(s) of x, if you know value of y?

P.S.
I can use for cycle and get some (approximate) result with Eval() Function.
However, the result isn’t very precise - I cant use infinitely small step in for cycle…

ROOT Version: 6.18/04
Platform: Ubuntu 18.04.4 LTS
Compiler: VSCode


This inverse of Eval is a bit tricky seems to me. What happens if the y value you give is not (linearly speaking) on the graph ?

Good point.
So if there is no straight forward way, I can do the specific scenario on my own,
thanks :slight_smile:

Yes there is no predefined function in TGraph to do that. The best is to make a small piece of code, which, in addition, can be adapted to your specific case.

I had the same problem, and what reasonably seems to work is to inverse the TGraph and then use Eval, like

TGraph *g = ...
TGraph invg(g->GetN(), g->GetY(), g->GetX());
double x = invg.Eval(y);

At least this makes use of the TSpline approximation. I don’t know what happens for non-monotonuos graphs though.

Note that this will work only if your graph represents some strictly monotonic function.