1

In uproot the example provided in the docs for writing a TH1 histogram to a root file works very well. Is it possible to do the same thing with TH2 as well? If yes, would it be possible to provide an example, and possibly include in the docs?

EDIT:

I have now made a similar class like in the readme section, but this time with TH2:

import types
import uproot
import uproot_methods.classes.TH2
import numpy as np

class MyTH2(uproot_methods.classes.TH2.Methods, list):
    def __init__(self, xlow, xhigh, xnbins, ylow, yhigh, ynbins, values, title=""):
        self._fXaxis = types.SimpleNamespace()
        self._fXaxis._fNbins = xnbins
        self._fXaxis._fXmin = xlow
        self._fXaxis._fXmax = xhigh
        self._fYaxis = types.SimpleNamespace()
        self._fYaxis._fNbins = ynbins
        self._fYaxis._fXmin = ylow
        self._fYaxis._fXmax = yhigh
        for x in values:
            self.append(float(x))
        self._fTitle = title
        self._classname = "TH2F"

th2 = MyTH2(-5, 5, 6, -8, 8, 4, np.random.rand(24), title='')
file = uproot.recreate("tmp_th2.root", compression=uproot.ZLIB(4))
file["th2"] = th2

still no success. Any ideas what I am doing wrong?

xaratustra
  • 647
  • 1
  • 14
  • 28
  • 1
    I don't remember if that feature exists. If you try it and it works, you can answer your own question (and I'll copy your solution into the docs). If the histogram is coming from NumPy, there's a `np.histogram2d` and a `np.histogramdd` function, which produce different tuple formats, but I think if one is covered, they both are. – Jim Pivarski May 12 '20 at 14:17
  • In this [doc-section](https://github.com/scikit-hep/uproot#writing-histograms), there is an example of using `uproot_methods.classes.TH1` inside a class created entirely in python. Meanwhile I see that there is also a `uproot_methods.classes.TH2` but there is no example how to use it. – xaratustra May 12 '20 at 16:19

0 Answers0