Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #315 from scikit-hep/fseekfree
Browse files Browse the repository at this point in the history
Fixes fSeekFree
  • Loading branch information
jpivarski committed Aug 14, 2019
2 parents 6dcc045 + 471eb38 commit afb0364
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,3 +1027,59 @@ def test_ttree_empty_tbranch_title(tmp_path):

f = ROOT.TFile.Open(filename)
assert f.Get("t").GetBranch("intBranch").GetTitle() == "hi"

def test_hist_rewrite_root(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, "UPDATE")
t = ROOT.TObjString("Hello World")
t.Write()
f.Close()

f = ROOT.TFile.Open(filename)
assert f.Get("Hello World") == "Hello World"

def test_empty_ttree_rewrite_root(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, "UPDATE")
t = ROOT.TObjString("Hello World")
t.Write()
f.Close()

f = ROOT.TFile.Open(filename)
assert f.Get("Hello World") == "Hello World"

def test_string_rewrite_root(tmp_path):
filename = join(str(tmp_path), "example.root")

with uproot.recreate(filename, compression=None) 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"
1 change: 1 addition & 0 deletions uproot/write/TFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ def _expandfile(self, cursor):
freekey = uproot.write.TKey.TKey(b"TFile", self._filename, fObjlen=0, fSeekKey=cursor.index, fSeekPdir=self._fBEGIN)
freeseg = uproot.write.TFree.TFree(cursor.index + freekey.fNbytes)
freekey.fObjlen = freeseg.size()
freekey.fNbytes += freekey.fObjlen

freekey.write(freecursor, self._sink)
freeseg.write(freecursor, self._sink)
Expand Down

0 comments on commit afb0364

Please sign in to comment.