# Get local maximum when using THStack

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 using `SetRange` and then `GetMaximum.` However I always get the global maximum. The following script mimics what I tried:

``````{
Int_t x1 = {1,3,5,7};
Int_t x2 = {2,4,6,8};
Int_t ya = {5,10,7,3};
Int_t yb = {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->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?

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

Try to play with the histogram that you can get using:

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

Thanks @Wile_E_Coyote, it works when calling the histogram:

``````{
Int_t x1 = {1,3,5,7};
Int_t x2 = {2,4,6,8};
Int_t ya = {5,10,7,3};
Int_t yb = {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->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?

@Wile_E_Coyote, I tried the same approach with a `TMultiGraph` Object without success (using `GetHistogram(), SetRangeUser, and then GetMaximum`). I saw this post, but the solution returns a global rather than a local maximum. Do you know how to achieve the same goal with `TMultiGraph`?

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:

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

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

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.