# Gettin correct values of histograms

I wrote a code that outputs a list of values corresponding to the maxima of histograms for a separate branch for all files from the folder. But I noticed that not all values are correct. As an example, I attach two histograms. For a wide one, the correct value is obtained. For a narrow one, no. How to solve this problem?

``````def ch7_939(y):
c = ROOT.TCanvas()
myfile = ROOT.TFile.Open("/home/erg/2022.12/2022.12.01/"+y)
h = ROOT.TH1D("h", "histogram with channel 7", 100, 0., 0.)
tree.Draw("Channel7 >> h")
return h.GetMaximum(maxval=100000.0)

second = []
chan7_939 =[]
m = os.listdir("/home/erg/2022.12/2022.12.01")
for i in range(len(m)):
second.append(m[i])
second.sort()
print(second)
for j in range(len(m)):
chan7_939.append(ch7_939(second[j]))
chan7_939.sort()
print(chan7_939)
``````

_ROOT Version: 6.26/10
_Platform: Ubuntu
_Compiler: pycharm community

Well, I guess you get the “x” around 15915 for the first and 15910.5 for the second histogram (btw. remove “`maxval=100000.0`” from your function).

I mean maximum of Y axis.

I guess you get the “y” around 9800 for the first and around 1540 for the second.

Maybe you want to use:
`h = ROOT.TH1D("h", "histogram with channel 7", 10000, 10000., 20000.) # fix bin size = 1`

But code output 1541 and 1533
I guess it relate with the line h = ROOT.TH1D(“h”, “histogram with channel 7”, 100, 0., 0.)

Well, after the “`Draw`”, try to add: `ROOT.gPad.Update()`

Unfortunately, it doesn’t help.

Well, after the “`Draw`”, try to add: `h.BufferEmpty(1) # 1 or -1 ?`

It is also useless.

It seems we need a “reproducer” (i.e., the macro plus data files).

Dec01031235.root (120.3 KB)
Dec01031201.root (118.1 KB)

The problem is that your `"h"` histogram with automatic bins uses the default “buffer size” of `1000` (and then the automatically computed x-axis limits may be “narrow” when the first `1000` entries are near the maximum).

``````import ROOT
f = ROOT.TFile("Dec01031235.root")
ROOT.gROOT.cd()
# ROOT.TH1.SetDefaultBufferSize(100000) # either before creating any histogram
h = ROOT.TH1D("h", "histogram with channel 7", 100, 0., 0.)
# h.SetBuffer(100000) # or after creating this histogram
t.Draw("Channel7 >> h")
``````

It didn’t work.

Did you try to uncomment one of the additional lines?

Which lines exactly do you mean?

One of these which begin with a “`#`” in the small test macro in my previous post, of course.

I did not uncomment it.

Is there a way to set borders automatically?

I’d say, in order to have meaningful results, you should create a histogram with a “fix bin size” = 1, e.g.:

Unsuccessfully