go-hep.org/x/hep@v0.38.1/hbook/rootcnv/testdata/make-root-h2.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-h2.root", "RECREATE")
     9  for t in [
    10          (ROOT.TH2F, "h2f", (3,0,3, 3,0,3)),
    11          (ROOT.TH2D, "h2d", (3,0,3, 3,0,3)),
    12          (ROOT.TH2F, "h2f-var", ( 
    13              (3, carray("f", [0.0, 1.5, 2.0, 3.0])),
    14              (3, carray("f", [0.0, 1.5, 2.0, 3.0])))),
    15          (ROOT.TH2D, "h2d-var", (
    16              (3, carray("d", [0.0, 1.5, 2.0, 3.0])),
    17              (3, carray("d", [0.0, 1.5, 2.0, 3.0]))))
    18          ]:
    19      cls, name, args = t
    20      if len(args) == 6:
    21          h = cls(name, name, args[0], args[1], args[2], args[3], args[4], args[5])
    22      elif len(args) == 2:
    23          h = cls(name, name, args[0][0], args[0][1], args[1][0], args[1][1])
    24      else:
    25          raise ValueError("invalid number of arguments %d" % len(args))
    26      h.StatOverflows(True)
    27      h.Sumw2()
    28      with open("gauss-2d-data.dat") as ff:
    29          for l in ff.readlines():
    30              x, y, w = l.split()
    31              h.Fill(float(x),float(y),float(w))
    32              pass
    33          pass
    34      h.Fill(+5,+5,101) # NE 
    35      h.Fill(+0,+5,102) # N
    36      h.Fill(-5,+5,103) # NW
    37      h.Fill(-5,+0,104) # W
    38      h.Fill(-5,-5,105) # SW
    39      h.Fill(+0,-5,106) # S
    40      h.Fill(+5,-5,107) # SE
    41      h.Fill(+5,+0,108) # E
    42  
    43      histos.append(h)
    44      pass
    45  f.Write()
    46  f.Close()