Is there a way to create a TH1 with variable binning when knowing only the bin centers?
I have a file with (x,y) values
1.0 55
1.1 100
1.5 2
1.6 14
1.7 33
1.9 88
2.0 53
In a TGraph the 1st column would be the x-values and the 2nd the y-values.
However, I’d ike to have a histogram with the above data where the 1st column should be the bin center and the 2nd the bin content.
Since the binning isn’t linear, the only way I can think of is to use the TH1::TH1 (const char* name, const char* title, Int_t nbins, const Float_t* xbins) constuctor. But then xbins are the low edges of the bins.
Trying to calculate the low edges in a way that the first column values, would be the bin centre isn’t straight forward because after a point data will be excluded:
If you don’t want to see empty bins, with the variable bins you cannot generally keep the original bin centres (which by deinition means symmetrical low/high edges around those centres), as you alreay found out when reaching 1.6
I’m not sure there’s a proper option other than either using new bin centres (and therefore some of your original points will probably get binned together), or just living with the empty bins
If you define your own bin edges carefully (with the new bin centres different from your original centres, of course), then you could try hiding the axis labels and manually (TLatex or TText) printing your custom ‘bin labels’ at the correct positions, but then the bin edges will not be symmetrical around those labels, e.g.: |_1.0_|_1.1________|____1.5_|...
If you don’t care about the statistics or other info on the histogram, as workaround you could try to hide or disguise the empty bins, e.g. filling 1.2 and 1.3 with the same value as 1.1 (100), and 1.4 with the same as 1.5 (2), so that they may seem to be the same bin.
Hi,
maybe you could set the first low edge as center[0] - 0.1, the last edge as center[-1] + 0.1 (where with center[-1] I mean the last of the bin centers) and all other low edges as center[i] + center[i+1] / 2?
Also if you check the bins.txt file I uploaded it’s evident that this approach doesn’t work.
For instance the largest bin center is 100000 (line 700, 1st column) but the low bin edges exceed this number.