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

**URL:** <https://root-forum.cern.ch/t/tgraph-get-value-s-of-x-based-on-value-of-y/38289>\
**Category:** Newbie\
**Created:** [March 6, 2020, 11:03am UTC](https://root-forum.cern.ch/t/tgraph-get-value-s-of-x-based-on-value-of-y/38289 "2020-03-06T11:03:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilomi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ilomi/32/21077_2.png) [@ilomi](https://root-forum.cern.ch/u/ilomi)\
**Post date:** [March 6, 2020, 11:03am UTC](https://root-forum.cern.ch/t/tgraph-get-value-s-of-x-based-on-value-of-y/38289/1 "2020-03-06T11:03:11Z")

</div>

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

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 6, 2020, 12:24pm UTC](https://root-forum.cern.ch/t/tgraph-get-value-s-of-x-based-on-value-of-y/38289/2 "2020-03-06T12:24:03Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![ilomi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ilomi/32/21077_2.png) [@ilomi](https://root-forum.cern.ch/u/ilomi)\
**Post date:** [March 6, 2020, 12:41pm UTC](https://root-forum.cern.ch/t/tgraph-get-value-s-of-x-based-on-value-of-y/38289/3 "2020-03-06T12:41:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 6, 2020, 1:07pm UTC](https://root-forum.cern.ch/t/tgraph-get-value-s-of-x-based-on-value-of-y/38289/4 "2020-03-06T13:07:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![canigia](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/f4b2a3/32.png) [@canigia](https://root-forum.cern.ch/u/canigia)\
**Post date:** [May 20, 2020, 10:19am UTC](https://root-forum.cern.ch/t/tgraph-get-value-s-of-x-based-on-value-of-y/38289/5 "2020-05-20T10:19:51Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 20, 2020, 10:22am UTC](https://root-forum.cern.ch/t/tgraph-get-value-s-of-x-based-on-value-of-y/38289/6 "2020-05-20T10:22:54Z")

</div>

Note that this will work only if your graph represents some [strictly monotonic function](https://en.wikipedia.org/wiki/Monotonic_function).
