# Getting (x,y) for maximum value within specific range of graph

**URL:** <https://root-forum.cern.ch/t/getting-x-y-for-maximum-value-within-specific-range-of-graph/36926>\
**Category:** Newbie\
**Tags:** graphics\
**Created:** [November 22, 2019, 4:47pm UTC](https://root-forum.cern.ch/t/getting-x-y-for-maximum-value-within-specific-range-of-graph/36926 "2019-11-22T16:47:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![schniefen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@schniefen](https://root-forum.cern.ch/u/schniefen)\
**Post date:** [November 22, 2019, 4:47pm UTC](https://root-forum.cern.ch/t/getting-x-y-for-maximum-value-within-specific-range-of-graph/36926/1 "2019-11-22T16:47:25Z")

</div>

There have been similar questions on the forum, but mostly in connection with histograms. Consider the attached picture. How could one find the point corresponding to this local maximum (see arrow)? Thanks!

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 25, 2019, 8:46am UTC](https://root-forum.cern.ch/t/getting-x-y-for-maximum-value-within-specific-range-of-graph/36926/2 "2019-11-25T08:46:46Z")

</div>

Hi,

Are the point really strictly increasing? Are they ordered? If both answers are “yes” then I’d not even use `TGraph` but the C++ standard library: [`is_sorted_until`](https://en.cppreference.com/w/cpp/algorithm/is_sorted_until) is your friend!

If it’s not sorted, sort it first - but make sure to re-order both x and y values, not just the x values 🙂

Let me know if you need an example code snippet.

Axel.

---

<div class="post-metadata">

**Author:** ![OSchaile](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/o/e495f1/32.png) [@OSchaile](https://root-forum.cern.ch/u/OSchaile)\
**Post date:** [November 25, 2019, 8:46am UTC](https://root-forum.cern.ch/t/getting-x-y-for-maximum-value-within-specific-range-of-graph/36926/3 "2019-11-25T08:46:53Z")

</div>

Hi,  
a TGraph is essentially an unordered collection of pairs (x,y).  
So I’m afraid there is no other way but “brute force” looping on all points.  
Otto

---

<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:** [November 25, 2019, 8:48am UTC](https://root-forum.cern.ch/t/getting-x-y-for-maximum-value-within-specific-range-of-graph/36926/4 "2019-11-25T08:48:31Z")

</div>

[TGraph::Sort](https://root.cern.ch/doc/master/classTGraph.html#a6192cff25c7ab28933c91f2869020cce)

---

<div class="post-metadata">

**Author:** ![OSchaile](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/o/e495f1/32.png) [@OSchaile](https://root-forum.cern.ch/u/OSchaile)\
**Post date:** [November 25, 2019, 11:32am UTC](https://root-forum.cern.ch/t/getting-x-y-for-maximum-value-within-specific-range-of-graph/36926/5 "2019-11-25T11:32:28Z")

</div>

Hi,  
Axel and Wile are of course right, thats the elegant way.  
I myself as a simple minded guy would do it straightforward:

```auto
#include "TGraph.h"

int getmax(TGraph *gr, double from, double to)
{
   double maxval = -1e20;
   int maxind =-1;
   double *x = gr->GetX();
   double *y = gr->GetY();
   for (int i=0; i<gr->GetN(); i++) {
      if (x[i] < from)
         continue;
      if (x[i] > to) 
         continue; // replace by break if ordered
      if (y[i] > maxval) {
         maxval = y[i];
         maxind = i;
      }
   }
   if (maxind <0)
      printf("no max found\n");
   return maxind;
};

double aaa()
{
   double from = 5, to = 8.5;
   double x=0, y=0;
   auto gr = new TGraph(10);
   for (int i=0; i<10; i++){
      gr->SetPoint(i, i+2, (i%5)*i);
   }
   gr->Draw("a*");
   int mind = getmax(gr, from, to);
   if (mind >=0) {
      gr->GetPoint(mind, x,y);
      printf("Max %f at: %d\n", y, mind);
   }
   return y;
}

```

Otto
