Get values from bins and evaluate their mean


I have a TH2D with x and y, and what I need is to get the values for all the bins to calculate the mean at each bin. So I tried to cycle through the bins and get all the x and y values that are in that bin, but when I print e.g. xval[0], xval[1]…, they are all the same. :sweat:

Any suggestion?


Can you post a little root macro showing what you are doing to retrieve the bin contents ?

Yes, sure :slight_smile:

example.c (752 Bytes)

can you post plot.txt ?

plot_part.txt (45.0 KB)

This is only a fraction of the file, as the original one has 10^6 entries and is too big. :slight_smile:

Thanks. Here is a new version of your macro.
example.c (1.0 KB)

Good morning,

thanks for the help!! :smiley:
After running the macro, I modified some lines:
I had to change GetVal() for xntuple and yntuple, because for some reason in xntuple the values for log(y/x) were stored and in yntuple the values for x. And as yntuple is already log(y/x), yaxis = yntuple[b]. Anyhow, next I’ll try to add the binW parameter to get all the points in a certain bin, not only one.


Yes sure… I tried to do something … you previous macro could not work.


Am writing again, because I stumbled upon something I don’t understand. Will attach my macro…So now if I print vx[] & vy[], I get ALMOST what I want - in line 59 I say that I want to increment the steps on x axis by the “binW”, which is 4 (I start at 0.1, the next is 0.1+4=4.1 and so on). When I print vx[0], I would like to get the corresponding vy (which is log(y/x)). Or in other words, vx[0] should be an interval between 0.1 and 4.1 (vx should contain all the values in range 0.1-4.1 from the text file), but instead it only contains the first entry from the file, and x[0] is not in the first “bin”. How can I tell ROOT I want all the values from a certain range, not just one?


example.c (1.3 KB)

Have you solved your problem ? sorry for the late answer I was on holidays

No problem! Turned out there was a more straight forward way of solving my problem, so I’ve got it now. :slight_smile:

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