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] ?
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.