7 #include <pybind11/numpy.h>
14 namespace py = pybind11;
16 using HistTuple = std::tuple<py::array_t<M3::float_t>, py::array_t<M3::float_t>, py::array_t<M3::float_t>>;
19 auto edges_ptr =
static_cast<M3::float_t*
>(edges_buf.request().ptr);
20 for (
int i = 0; i <= nbins; ++i) {
21 edges_ptr[i] = axis->GetBinLowEdge(i + 1);
23 edges_ptr[nbins] = axis->GetBinLowEdge(nbins + 1) + axis->GetBinWidth(nbins + 1);
27 int nbinsX = hist->GetNbinsX();
30 py::array_t<M3::float_t> edgesX(nbinsX + 1);
33 py::array_t<M3::float_t> edgesY;
36 int nbinsY = hist->GetNbinsY();
38 edgesY = py::array_t<M3::float_t>(nbinsY + 1);
43 py::array_t<M3::float_t> contents({nbinsY, nbinsX});
44 auto contents_buf = contents.request();
47 for(
int iy=0; iy<nbinsY; ++iy){
48 for(
int ix=0; ix<nbinsX; ++ix){
49 contents_ptr[iy * nbinsX + ix] = hist->GetBinContent(ix + 1, iy + 1);
53 return std::make_tuple(contents, edgesX, edgesY);
58 if(
dynamic_cast<TH2Poly*
>(hist.get())){
59 throw std::runtime_error(
"TH2Poly is not supported for conversion to numpy arrays");
HistTuple THNToNumpy(std::unique_ptr< TH1 > &hist)
std::tuple< py::array_t< M3::float_t >, py::array_t< M3::float_t >, py::array_t< M3::float_t > > HistTuple
py::tuple HistToNumpy(std::unique_ptr< TH1 > &hist)
void FillEdgesPointer(py::array_t< M3::float_t > edges_buf, TAxis *axis, int nbins)