histogram_utils.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. '''
  2. histogram_utils.py
  3. The functions in this module use a representation of a histogram that is a
  4. tuple containing an arr of N bin values, an array of N bin errors(symmetric)
  5. and an array of N+1 bin edges(N lower edges + 1 upper edge).
  6. For 2d histograms, It is similar, but the arrays are two dimensional and
  7. there are separate arrays for x-edges and y-edges.
  8. '''
  9. import numpy as np
  10. from scipy.optimize import curve_fit
  11. def hist(th1):
  12. nbins = th1.GetNbinsX()
  13. edges = np.zeros(nbins+1, np.float32)
  14. values = np.zeros(nbins, np.float32)
  15. errors = np.zeros(nbins, np.float32)
  16. for i in range(nbins):
  17. edges[i] = th1.GetXaxis().GetBinLowEdge(i+1)
  18. values[i] = th1.GetBinContent(i+1)
  19. errors[i] = th1.GetBinError(i+1)
  20. edges[nbins] = th1.GetXaxis().GetBinUpEdge(nbins)
  21. return values, errors, edges
  22. def hist_bin_centers(h):
  23. _, _, edges = h
  24. return (edges[:-1] + edges[1:])/2.0
  25. def hist2d(th2, include_errors=False):
  26. """ Converts TH2 object to something amenable to
  27. plotting w/ matplotlab's pcolormesh.
  28. """
  29. import numpy as np
  30. nbins_x = th2.GetNbinsX()
  31. nbins_y = th2.GetNbinsY()
  32. xs = np.zeros((nbins_y+1, nbins_x+1), np.float32)
  33. ys = np.zeros((nbins_y+1, nbins_x+1), np.float32)
  34. values = np.zeros((nbins_y, nbins_x), np.float32)
  35. errors = np.zeros((nbins_y, nbins_x), np.float32)
  36. for i in range(nbins_x):
  37. for j in range(nbins_y):
  38. xs[j][i] = th2.GetXaxis().GetBinLowEdge(i+1)
  39. ys[j][i] = th2.GetYaxis().GetBinLowEdge(j+1)
  40. values[j][i] = th2.GetBinContent(i+1, j+1)
  41. errors[j][i] = th2.GetBinError(i+1, j+1)
  42. xs[nbins_y][i] = th2.GetXaxis().GetBinUpEdge(i+1)
  43. ys[nbins_y][i] = th2.GetYaxis().GetBinUpEdge(nbins_y+1)
  44. for j in range(nbins_y+1):
  45. xs[j][nbins_x] = th2.GetXaxis().GetBinUpEdge(nbins_x+1)
  46. ys[j][nbins_x] = th2.GetYaxis().GetBinUpEdge(j+1)
  47. return values, errors, xs, ys
  48. def hist_slice(hist, range_):
  49. values, errors, edges = hist
  50. lim_low, lim_high = range_
  51. slice_ = np.logical_and(edges[:-1] > lim_low, edges[1:] < lim_high)
  52. last = len(slice_) - np.argmax(slice_[::-1])
  53. return (values[slice_],
  54. errors[slice_],
  55. np.concatenate([edges[:-1][slice_], [edges[last]]]))
  56. def hist_normalize(h, norm):
  57. values, errors, edges = h
  58. scale = norm/np.sum(values)
  59. return values*scale, errors*scale, edges
  60. # def hist_slice2d(h, range_):
  61. # values, errors, xs, ys = h
  62. # last = len(slice_) - np.argmax(slice_[::-1])
  63. # (xlim_low, xlim_high), (ylim_low, ylim_high) = range_
  64. # slice_ = np.logical_and(xs[:-1, :-1] > xlim_low, xs[1:, 1:] < xlim_high,
  65. # ys[:-1, :-1] > ylim_low, ys[1:, 1:] < ylim_high)
  66. # last = len(slice_) - np.argmax(slice_[::-1])
  67. # return (values[slice_],
  68. # errors[slice_],
  69. # np.concatenate([edges[:-1][slice_], [edges[last]]]))
  70. def hist_fit(h, f, p0=None):
  71. values, errors, edges = h
  72. xs = hist_bin_centers(h)
  73. # popt, pcov = curve_fit(f, xs, values, p0=p0, sigma=errors)
  74. popt, pcov = curve_fit(f, xs, values, p0=p0)
  75. return popt, pcov
  76. def hist_rebin(hist, range_, nbins):
  77. raise NotImplementedError()