Is there some efficient way to convert a TH2D to a numpy array?
So far I’m doing something like this:
x_bins = hist.GetNbinsX()
y_bins = hist.GetNbinsY()
bins = np.zeros((x_bins,y_bins))
for y_bin in xrange(y_bins):
for x_bin in xrange(x_bins):
bins[x_bin,y_bin] = hist.GetBinContent(x_bin + 1,y_bin + 1)
But this is obviously suboptimal, and takes a while when reading out ~ 1M bins.
you can get a pointer to the array of bins using hist.GetArray().
Thanks, although it’s hard to know what to do with the result, for example:
Does anyone know if it’s possible to (quickly) parse this into an ndarray?
[quote=“dguest”]Thanks, although it’s hard to know what to do with the result, for example:[/quote]in C++, a pointer representing an array has no further info, so the default is to set the size of the returned array to MAXINT. However, in your own example above, you already have the actual number of bins (GetNbinsX(), GetNbinsY()), and hence the dimensions.
More details (and possible pitfalls) here: [url]Problem reading from buffer with numpy