MaCh3  2.5.1
Reference Guide
Typedefs | Functions
histutils.h File Reference

HW: Histogram utilities for converting ROOT histograms to numpy arrays for use in Python. More...

#include "TH1.h"
#include "TH2.h"
#include "TH2Poly.h"
#include <pybind11/numpy.h>
Include dependency graph for histutils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

using HistTuple = std::tuple< py::array_t< M3::float_t >, py::array_t< M3::float_t >, py::array_t< M3::float_t > >
 

Functions

void FillEdgesPointer (py::array_t< M3::float_t > edges_buf, TAxis *axis, int nbins)
 
HistTuple THNToNumpy (std::unique_ptr< TH1 > &hist)
 
py::tuple HistToNumpy (std::unique_ptr< TH1 > &hist)
 

Detailed Description

HW: Histogram utilities for converting ROOT histograms to numpy arrays for use in Python.

Author
Henry Wallace
Ewan Miller

Definition in file histutils.h.

Typedef Documentation

◆ HistTuple

using HistTuple = std::tuple<py::array_t<M3::float_t>, py::array_t<M3::float_t>, py::array_t<M3::float_t> >

Definition at line 16 of file histutils.h.

Function Documentation

◆ FillEdgesPointer()

void FillEdgesPointer ( py::array_t< M3::float_t edges_buf,
TAxis *  axis,
int  nbins 
)

Definition at line 18 of file histutils.h.

18  {
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);
22  }
23  edges_ptr[nbins] = axis->GetBinLowEdge(nbins + 1) + axis->GetBinWidth(nbins + 1);
24 }
double float_t
Definition: Core.h:37

◆ HistToNumpy()

py::tuple HistToNumpy ( std::unique_ptr< TH1 > &  hist)
inline

Definition at line 56 of file histutils.h.

57 {
58  if(dynamic_cast<TH2Poly*>(hist.get())){
59  throw std::runtime_error("TH2Poly is not supported for conversion to numpy arrays");
60  }
61 
62  return py::cast(THNToNumpy(hist));
63 }
HistTuple THNToNumpy(std::unique_ptr< TH1 > &hist)
Definition: histutils.h:26

◆ THNToNumpy()

HistTuple THNToNumpy ( std::unique_ptr< TH1 > &  hist)

Definition at line 26 of file histutils.h.

26  {
27  int nbinsX = hist->GetNbinsX();
28 
29 
30  py::array_t<M3::float_t> edgesX(nbinsX + 1);
31  FillEdgesPointer(edgesX, hist->GetXaxis(), nbinsX);
32 
33  py::array_t<M3::float_t> edgesY;
34 
35  // If we need to get the contents as a 2D array, we need to check if the histogram is actually 2D
36  int nbinsY = hist->GetNbinsY();
37  if(nbinsY > 1){
38  edgesY = py::array_t<M3::float_t>(nbinsY + 1);
39  FillEdgesPointer(edgesY, hist->GetYaxis(), nbinsY);
40  }
41 
42  // Now we fill the contents
43  py::array_t<M3::float_t> contents({nbinsY, nbinsX});
44  auto contents_buf = contents.request();
45  M3::float_t* contents_ptr = static_cast<M3::float_t*>(contents_buf.ptr);
46 
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);
50  }
51  }
52 
53  return std::make_tuple(contents, edgesX, edgesY);
54 }
void FillEdgesPointer(py::array_t< M3::float_t > edges_buf, TAxis *axis, int nbins)
Definition: histutils.h:18