go-hep.org/x/hep@v0.38.1/hbook/rootcnv/testdata/make-root-h1.py (about)

     1  # Copyright ©2017 The go-hep Authors.  All rights reserved.
     2  # Use of this source code is governed by a BSD-style
     3  # license that can be found in the LICENSE file.
     4  
     5  from array import array as carray
     6  import ROOT
     7  
     8  f = ROOT.TFile.Open("gauss-h1.root","RECREATE")
     9  histos = []
    10  for t in [
    11          (ROOT.TH1D, "h1d", (10,-4,4)),
    12          (ROOT.TH1F, "h1f", (10,-4,4)),
    13          (ROOT.TH1F, "h1d-var", (10, carray("d", [
    14              -4.0, -3.2, -2.4, -1.6, -0.8,  0,
    15              +0.8, +1.6, +2.4, +3.2, +4.0
    16          ]))),
    17          (ROOT.TH1F, "h1f-var", (10, carray("f", [
    18              -4.0, -3.2, -2.4, -1.6, -0.8,  0,
    19              +0.8, +1.6, +2.4, +3.2, +4.0
    20          ])))
    21          ]:
    22      cls, name, args = t
    23      if len(args) == 3:
    24          h = cls(name, name, args[0], args[1], args[2])
    25      elif len(args) == 2:
    26          h = cls(name, name, args[0], args[1])
    27      else:
    28          raise ValueError("invalid number of arguments %d" % len(args))
    29      h.StatOverflows(True)
    30      h.Sumw2()
    31      with open("gauss-1d-data.dat") as ff:
    32          for l in ff.readlines():
    33              x, w = l.split()
    34              h.Fill(float(x),float(w))
    35              pass
    36          pass
    37      histos.append(h)
    38      pass
    39  f.Write()
    40  f.Close()