Get mean, max, min y value for a specific range on x-axis for TGraphErrors

I want to extract the maximum y value for a specific range on x-axis
gr.GetMaximum() always give -1110
gr.GetYaxis().GetXmax(), gr.GetYaxis().GetXmin() seems to be works only for the whole range of x-axis.

func = TF1("func","[0]*x + [1]",0,5)
func.SetNpx(1000)
func.SetParameters(2,1)
func.SetLineColor(2)

for i in range(10):
    gr.SetPoint(gr.GetN(),i, func.Eval(i))

print(gr.GetMaximum())
print(gr.GetYaxis().GetXmax(), gr.GetYaxis().GetXmin())
# what if i want to find min,max of y-value for x in [3,7] ?

any suggestion ?


ROOT Version: 6.24 & 6.28.06
_Platform:(conda) Centos7 & MacOS ventura
Compiler: Not Provided


Hi @NgJunKai,

thank you for your question on the forum!

Could you please give us a minimal working reproducer of your programme, it is hard to tell what is wrong based on these two lines only.

Cheers,
Marta

updated my question.

Hi again @NgJunKai,

thank you for the reproducer.

What you are seeing for GetMaximum() is expected. It was already discussed in the forum post here: TGraph GetMaximum() GetMinimum().

The suggested solution is to use ROOT::TMath class, which would look like this if I indeed set the range to be between 3 and 7:

import ROOT 

func = ROOT.TF1("func","[0]*x + [1]")
func.SetNpx(1000)
func.SetParameters(2., 1.)

gr = ROOT.TGraph()

for i in range(3,8):
    gr.SetPoint(gr.GetN(), i, func.Eval(i))

print(ROOT.TMath.MaxElement(gr.GetN(),gr.GetY())) # gives 15 as expected
print(ROOT.TMath.MinElement(gr.GetN(),gr.GetY())) # gives 7 as expected

You can explore more of the ROOT::TMath class if you need other quantities, ROOT: TMath Namespace Reference and more generally the Math class is documented here ROOT: Math.

Cheers,
Marta

1 Like

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