# Get local maximum when using THStack

**URL:** <https://root-forum.cern.ch/t/get-local-maximum-when-using-thstack/27361>\
**Category:** ROOT\
**Tags:** graphics\
**Created:** [December 22, 2017, 1:58am UTC](https://root-forum.cern.ch/t/get-local-maximum-when-using-thstack/27361 "2017-12-22T01:58:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![amvargash](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/f07891/32.png) [@amvargash](https://root-forum.cern.ch/u/amvargash)\
**Post date:** [December 22, 2017, 1:58am UTC](https://root-forum.cern.ch/t/get-local-maximum-when-using-thstack/27361/1 "2017-12-22T01:58:09Z")

</div>

Hi everyone,

I was trying to get a local maximum value (Y Axis) when using `THStack,` using the approach suggested [in this really old post](https://root.cern.ch/root/roottalk/roottalk03/2977.html) using `SetRange` and then `GetMaximum.` However I always get the global maximum. The following script mimics what I tried:

```auto
{
  Int_t x1[4] = {1,3,5,7};
  Int_t x2[4] = {2,4,6,8};
  Int_t ya[4] = {5,10,7,3};
  Int_t yb[4] = {4,4,5,4};
  
  TH1I *h1 = new TH1I("h1","h1",8,0,8);
  TH1I *h2 = new TH1I("h2","h2",8,0,8);
  THStack *hs = new THStack();

  for (Int_t i =0; i < 4; i++){
    h1->Fill(x1[i],ya[i]);
    h2->Fill(x2[i],yb[i]);
  }
  h1->SetFillColor(1);
  h2->SetFillColor(2);
  hs->Add(h1,"HIST");
  hs->Add(h2,"HIST");
  hs->Draw();
  printf("GlobalMaximum: %f\n",hs->GetMaximum()); 
  hs->GetXaxis()->SetRange(1,4);
  printf("LocalMaximum: %f\n",hs->GetMaximum());
  hs->GetXaxis()->SetRange(5,8);
  printf("LocalMaximum: %f\n",hs->GetMaximum());
  hs->GetXaxis()->SetRange(1,8);
}

```

Do you have any suggestion on how to achieve this?

---

<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:** [December 22, 2017, 9:21am UTC](https://root-forum.cern.ch/t/get-local-maximum-when-using-thstack/27361/2 "2017-12-22T09:21:33Z")

</div>

This approach is implemented for TH1 and TH2 but does not seems to be in case of THStack.

---

<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:** [December 22, 2017, 9:47am UTC](https://root-forum.cern.ch/t/get-local-maximum-when-using-thstack/27361/3 "2017-12-22T09:47:22Z")

</div>

[Try to play with the histogram that you can get using:](https://root-forum.cern.ch/t/findfirstbinabove-for-thstack/15589/2)

```auto
TH1 *h = ((TH1*)(hs->GetStack()->Last())); // the "SUM"

```

---

<div class="post-metadata">

**Author:** ![amvargash](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/f07891/32.png) [@amvargash](https://root-forum.cern.ch/u/amvargash)\
**Post date:** [December 22, 2017, 12:22pm UTC](https://root-forum.cern.ch/t/get-local-maximum-when-using-thstack/27361/4 "2017-12-22T12:22:33Z")

</div>

Thanks @Wile_E_Coyote, it works when calling the histogram:

```auto
{
  Int_t x1[4] = {1,3,5,7};
  Int_t x2[4] = {2,4,6,8};
  Int_t ya[4] = {5,10,7,3};
  Int_t yb[4] = {4,4,5,4};
  
  TH1I *h1 = new TH1I("h1","h1",8,0,8);
  TH1I *h2 = new TH1I("h2","h2",8,0,8);
  THStack *hs = new THStack();

  for (Int_t i =0; i < 4; i++){
    h1->Fill(x1[i],ya[i]);
    h2->Fill(x2[i],yb[i]);
  }
  h1->SetFillColor(1);
  h2->SetFillColor(2);
  hs->Add(h1,"HIST");
  hs->Add(h2,"HIST");

  hs->Draw();
  
  printf("GlobalMaximum: %f\n",hs->GetMaximum());
  
  TH1 *h = ((TH1*)(hs->GetStack()->Last()));
  h->GetXaxis()->SetRangeUser(1,3);
  printf("LocalMaximum: %f\n",h->GetMaximum());
  
  h->GetXaxis()->SetRangeUser(3,5);
  printf("LocalMaximum: %f\n",h->GetMaximum());
  
  h->GetXaxis()->SetRangeUser(5,8);
  printf("LocalMaximum: %f\n",h->GetMaximum());
  
  h->GetXaxis()->SetRangeUser(1,8);
}

```

It works, but I’m no quite sure why the solution has to be, to some extent, cumbersome. It looks like the user has to know the internal behavior of `THStack` to get things done. In principle, a more natural way to get a maximum/mimimum value for some `TGraph*/TH*` would be setting the range as an argument to the `GetMaximum(fromxmin,toxmax)` method. @couet What do you think? Could this be implemented?

---

<div class="post-metadata">

**Author:** ![amvargash](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/f07891/32.png) [@amvargash](https://root-forum.cern.ch/u/amvargash)\
**Post date:** [December 22, 2017, 2:01pm UTC](https://root-forum.cern.ch/t/get-local-maximum-when-using-thstack/27361/5 "2017-12-22T14:01:31Z")

</div>

@Wile_E_Coyote, I tried the same approach with a `TMultiGraph` Object without success (using `GetHistogram(), SetRangeUser, and then GetMaximum`). I saw [this post](https://root-forum.cern.ch/t/tmultigraph-getmax-min-imum/26127), but the solution returns a global rather than a local maximum. Do you know how to achieve the same goal with `TMultiGraph`?

---

<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:** [December 22, 2017, 2:51pm UTC](https://root-forum.cern.ch/t/get-local-maximum-when-using-thstack/27361/6 "2017-12-22T14:51:12Z")

</div>

Note that in the case of a “THStack”, you are retrieving the sum of all histograms (so, another fully filled histogram).  
BTW. It may be safer to use:

```auto
h->GetBinContent(h->GetMaximumBin())

```

This is not the case for a “TMultiGraph”, so I guess you would need to manually scan all graphs (the “GetHistogram” retrieves an empty histogram which is only used to draw the axes).

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [January 5, 2018, 2:51pm UTC](https://root-forum.cern.ch/t/get-local-maximum-when-using-thstack/27361/7 "2018-01-05T14:51:13Z")

</div>

Closed on January 5, 2018, 2:51pm UTC

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

---

<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:** [January 15, 2018, 2:30pm UTC](https://root-forum.cern.ch/t/get-local-maximum-when-using-thstack/27361/8 "2018-01-15T14:30:48Z")

</div>

> [@amvargash](#):
>
> @couet What do you think? Could this be implemented?

I looked at it a bit more closely. The way it is done now prevents to do what you are asking.  
The only way is to set the range for each histograms.
