How to use the uproot.recreate function in uproot

To help you get started, we’ve selected a few uproot examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scikit-hep / uproot / tests / test_write.py View on Github external
def test_th1(tmp_path):
    filename = join(str(tmp_path), "example.root")
    testfile = join(str(tmp_path), "test.root")

    f = ROOT.TFile.Open(testfile, "RECREATE")
    h = ROOT.TH1F("hvar", "title", 5, 1, 10)
    h.Sumw2()
    h.Fill(1.0, 3)
    h.Fill(2.0, 4)
    h.Write()
    f.Close()

    t = uproot.open(testfile)
    hist = t["hvar"]
    with uproot.recreate(filename, compression=None) as f:
        f["test"] = hist

    f = ROOT.TFile.Open(filename)
    h = f.Get("test")
    sums = [0.0, 25.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    bincontents = [7.0, 0.0, 0.0, 0.0, 0.0]
    assert list(h.GetSumw2()) == sums
    count = 0
    for x in range(1, 6):
        assert h.GetBinContent(x) == bincontents[count]
        count += 1
    assert h.GetNbinsX() == 5
    assert h.GetMean() == 1.5714285714285714
    assert h.GetRMS() == 0.4948716593053938
github scikit-hep / uproot / tests / test_write.py View on Github external
def test_issue340(tmp_path):
    filename = join(str(tmp_path), "example.root")

    a = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
    with uproot.recreate(filename) as f:
        f["t"] = uproot.newtree({"normal": numpy.float64})
        f["t"].extend({"normal": a})

    t = uproot.open(filename)["t"]
    for i in range(10):
        assert t["normal"].basket(0)[i] == a[i]
github scikit-hep / uproot / tests / test_write.py View on Github external
def test_string_rewrite_root_compress(tmp_path):
    filename = join(str(tmp_path), "example.root")

    with uproot.recreate(filename, compression=uproot.ZLIB(4)) as f:
        f["a"*5] = "a"*5

    f = ROOT.TFile.Open(filename, "UPDATE")
    t = ROOT.TObjString("Hello World")
    t.Write()
    f.Close()

    f = ROOT.TFile.Open(filename)
    assert f.Get("Hello World") == "Hello World"
github scikit-hep / uproot / tests / test_write.py View on Github external
def test_ttree_empty_tbranch_title(tmp_path):
    filename = join(str(tmp_path), "example.root")

    b = newbranch("int32", title="hi")
    branchdict = {"intBranch": b}
    tree = newtree(branchdict)
    with uproot.recreate(filename, compression=None) as f:
        f["t"] = tree

    f = ROOT.TFile.Open(filename)
    assert f.Get("t").GetBranch("intBranch").GetTitle() == "hi"
github scikit-hep / uproot / tests / test_write.py View on Github external
def test_rdf(tmp_path):
    filename = join(str(tmp_path), "example.root")

    b = newbranch("int32")
    branchdict = {"intBranch": b, "intBranch2": b}
    tree = newtree(branchdict)
    a = numpy.array([1, 2, 3, 4, 5], dtype=">i4")
    b = numpy.array([11, 12, 13, 14, 15], dtype=">i4")
    with uproot.recreate(filename, compression=None) as f:
        f["t"] = tree
        f["t"]["intBranch"].newbasket(a)
        f["t"]["intBranch2"].newbasket(b)

    f = ROOT.TFile.Open(filename)
    tree = f.Get("t")
    rdf = ROOT.RDataFrame(tree)
    for i in range(5):
        assert a[i] == rdf.AsNumpy()["intBranch"][i]
        assert b[i] == rdf.AsNumpy()["intBranch2"][i]
github scikit-hep / uproot / tests / test_write.py View on Github external
def test_ttree_empty_tbranch(tmp_path):
    filename = join(str(tmp_path), "example.root")

    b = newbranch("int32")
    branchdict = {"intBranch": b}
    tree = newtree(branchdict)
    with uproot.recreate(filename, compression=None) as f:
        f["t"] = tree

    f = ROOT.TFile.Open(filename)
    assert f.Get("t").GetBranch("intBranch").GetName() == "intBranch"
github scikit-hep / uproot / tests / test_write.py View on Github external
def test_th3_binlabel1(tmp_path):
    filename = join(str(tmp_path), "example.root")
    testfile = join(str(tmp_path), "test.root")

    f = ROOT.TFile.Open(testfile, "RECREATE")
    h = ROOT.TH3F("hvar", "title", 5, 1, 10, 6, 1, 20, 7, 1, 30)
    h.Fill(1.0, 5.0, 8.0, 3)
    h.GetXaxis().SetBinLabel(1, "Hi1")
    h.GetYaxis().SetBinLabel(2, "Hi2")
    h.GetZaxis().SetBinLabel(3, "Hi3")
    h.Write()
    f.Close()

    t = uproot.open(testfile)
    hist = t["hvar"]
    with uproot.recreate(filename, compression=None) as f:
        f["test"] = hist

    f = ROOT.TFile.Open(filename)
    h = f.Get("test")
    assert h.GetXaxis().GetBinLabel(1) == "Hi1"
    assert h.GetYaxis().GetBinLabel(2) == "Hi2"
    assert h.GetZaxis().GetBinLabel(3) == "Hi3"
github scikit-hep / uproot / tests / test_write.py View on Github external
def test_compressed_tprofile(tmp_path):
    filename = join(str(tmp_path), "example.root")
    testfile = join(str(tmp_path), "test.root")

    f = ROOT.TFile.Open(testfile, "RECREATE")
    h = ROOT.TProfile("hvar", "title", 5, 1, 10)
    h.Sumw2()
    h.Fill(1.0, 3)
    h.Fill(2.0, 4)
    h.Write()
    f.Close()

    t = uproot.open(testfile)
    hist = t["hvar"]
    with uproot.recreate(filename, compression=uproot.LZMA(5)) as f:
        f["test"] = hist

    f = ROOT.TFile.Open(filename)
    h = f.Get("test")
    sums = [0.0, 25.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    bincontents = [3.5, 0.0, 0.0, 0.0, 0.0]
    assert list(h.GetSumw2()) == sums
    assert h.GetMean() == 1.5
    assert h.GetRMS() == 0.5
    count = 0
    for x in range(1, 6):
        assert h.GetBinContent(x) == bincontents[count]
        count += 1
github scikit-hep / uproot / tests / test_write.py View on Github external
def test_ttree(tmp_path):
    filename = join(str(tmp_path), "example.root")

    tree = newtree()
    with uproot.recreate(filename, compression=None) as f:
        f["t"] = tree

    f = ROOT.TFile.Open(filename)
    assert f.GetKey("t").GetClassName() == "TTree"
github scikit-hep / pyhf / src / pyhf / writexml.py View on Github external
def writexml(spec, specdir, data_rootdir, resultprefix):
    global _ROOT_DATA_FILE

    shutil.copyfile(
        pkg_resources.resource_filename(__name__, 'schemas/HistFactorySchema.dtd'),
        os.path.join(os.path.dirname(specdir), 'HistFactorySchema.dtd'),
    )
    combination = ET.Element(
        "Combination", OutputFilePrefix=os.path.join('.', specdir, resultprefix)
    )

    with uproot.recreate(os.path.join(data_rootdir, 'data.root')) as _ROOT_DATA_FILE:
        for channelspec in spec['channels']:
            channelfilename = os.path.join(
                specdir, '{0:s}_{1:s}.xml'.format(resultprefix, channelspec['name'])
            )
            with open(channelfilename, 'w') as channelfile:
                channel = build_channel(spec, channelspec, spec.get('observations'))
                indent(channel)
                channelfile.write(
                    "\n\n"
                )
                channelfile.write(
                    ET.tostring(channel, encoding='utf-8').decode('utf-8')
                )

            inp = ET.Element("Input")
            inp.text = channelfilename