14def hist2array(hist, return_edges=False):
16 Function to convert a histogram into a numpy array and optionally also
17 return a list of the bin edges
19 hist: one-dimensional histogram
20 return_edges (bool): flag whether bin edges should be calculated
and returned
22 numpy array
and optionally a list of the bin edges
24 nbins = hist.GetNbinsX()
26 for i
in range(nbins):
27 arr[i] = hist.GetBinContent(i + 1)
30 edges.append(np.empty(nbins + 1))
31 hist.GetXaxis().GetLowEdge(edges[-1])
32 edges[-1][nbins] = hist.GetXaxis().GetBinUpEdge(nbins)
37def array2hist(array, hist):
39 Function to fill a histogram from an array
47 for i
in range(nbins):
48 hist.SetBinContent(i + 1, array[i])
50 for i
in range(hist.GetNbinsX()):
51 for j
in range(hist.GetNbinsY()):
52 hist.SetBinContent(i + 1, j + 1, array[i, j])