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

Fix issue340 #343

Merged
merged 4 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -1546,18 +1546,38 @@ def test_tree_branch_compression(tmp_path):
def test_branch_compression_interface1(tmp_path):
filename = join(str(tmp_path), "example.root")

b = newbranch(">i8")
branchdict = {"intBranch": b}
tree = newtree(branchdict)
a = numpy.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], dtype=">i8")
with uproot.recreate(filename, compression=uproot.ZLIB(4)) as f:
f["t"] = tree
f["t"]["intBranch"].newbasket(a)

f = ROOT.TFile.Open(filename)
tree = f.Get("t")
treedata = tree.AsMatrix().astype(">i8")
for i in range(15):
assert a[i] == treedata[i]
branch = tree.GetBranch("intBranch")
assert branch.GetCompressionAlgorithm() == 1
assert branch.GetCompressionLevel() == 4

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

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

f = ROOT.TFile.Open(filename)
tree = f.Get("t")
treedata = tree.AsMatrix().astype(">i4")
for i in range(5):
for i in range(15):
assert a[i] == treedata[i]
branch = tree.GetBranch("intBranch")
assert branch.GetCompressionAlgorithm() == 1
Expand All @@ -1566,18 +1586,18 @@ def test_branch_compression_interface1(tmp_path):
def test_branch_compression_interface2(tmp_path):
filename = join(str(tmp_path), "example.root")

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

f = ROOT.TFile.Open(filename)
tree = f.Get("t")
treedata = tree.AsMatrix().astype(">i4")
for i in range(5):
treedata = tree.AsMatrix().astype(">i8")
for i in range(15):
assert a[i] == treedata[i]
branch = tree.GetBranch("intBranch")
assert branch.GetCompressionAlgorithm() == 1
Expand All @@ -1586,18 +1606,18 @@ def test_branch_compression_interface2(tmp_path):
def test_branch_compression_interface3(tmp_path):
filename = join(str(tmp_path), "example.root")

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

f = ROOT.TFile.Open(filename)
tree = f.Get("t")
treedata = tree.AsMatrix().astype(">i4")
for i in range(5):
treedata = tree.AsMatrix().astype(">i8")
for i in range(15):
assert a[i] == treedata[i]
branch = tree.GetBranch("intBranch")
assert branch.GetCompressionAlgorithm() == 1
Expand Down Expand Up @@ -1855,3 +1875,15 @@ def test_ttree_extend_flush_false_diff_flush(tmp_path):
assert f["t"]["intBranch"].numbaskets == 0
f["t"].extend({"intBranch": numpy.array([2, 3])}, flush=False)
assert f["t"]["intBranch"].numbaskets == 2

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]
2 changes: 1 addition & 1 deletion uproot/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import re

__version__ = "3.10.0"
__version__ = "3.10.1"
version = __version__
version_info = tuple(re.split(r"[-\.]", __version__))

Expand Down
7 changes: 5 additions & 2 deletions uproot/write/TKey.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def __init__(self, fName, fNevBuf, fNevBufSize, fObjlen=0, fSeekKey=100, fSeekPd
def fKeylen(self):
return self._format1.size + uproot.write.sink.cursor.Cursor.length_strings([self.fClassName, self.fName, self.fTitle]) + self._format_basketkey.size + 1

@property
def fLast(self):
return self.fKeylen + self.fObjlen

def update(self):
self.cursor.update_fields(self.sink, self._format1, self.fNbytes, self._version, self.fObjlen, self.fDatime, self.fKeylen, self.fCycle, self.fSeekKey, self.fSeekPdir)

Expand All @@ -44,8 +48,7 @@ def write(self, cursor, sink):
cursor.write_string(sink, self.fTitle)

basketversion = 3
fLast = self.fNbytes
cursor.write_fields(sink, self._format_basketkey, basketversion, self.fBufferSize, self.fNevBufSize, self.fNevBuf, fLast)
cursor.write_fields(sink, self._format_basketkey, basketversion, self.fBufferSize, self.fNevBufSize, self.fNevBuf, self.fLast)
cursor.write_data(sink, b"\x00")

_version = 1004
Expand Down
6 changes: 3 additions & 3 deletions uproot/write/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def write(context, cursor, givenbytes, compression, key, keycursor):
import zlib
after_compressed = zlib.compress(givenbytes, level)
compressedbytes = len(after_compressed)
if compressedbytes < uncompressedbytes:
if (compressedbytes + 9) < uncompressedbytes:
c1 = (compressedbytes >> 0) & 0xff
c2 = (compressedbytes >> 8) & 0xff
c3 = (compressedbytes >> 16) & 0xff
Expand Down Expand Up @@ -108,7 +108,7 @@ def write(context, cursor, givenbytes, compression, key, keycursor):
after_compressed = lz4.block.compress(givenbytes, store_size=False)
compressedbytes = len(after_compressed) + 8
checksum = xxhash.xxh64(after_compressed).digest()
if compressedbytes < uncompressedbytes:
if (compressedbytes + 9) < uncompressedbytes:
c1 = (compressedbytes >> 0) & 0xff
c2 = (compressedbytes >> 8) & 0xff
c3 = (compressedbytes >> 16) & 0xff
Expand Down Expand Up @@ -137,7 +137,7 @@ def write(context, cursor, givenbytes, compression, key, keycursor):
"Install lzma package with:\n pip install backports.lzma\nor\n conda install -c conda-forge backports.lzma\n(or just use Python >= 3.3).")
after_compressed = lzma.compress(givenbytes, preset=level)
compressedbytes = len(after_compressed)
if compressedbytes < uncompressedbytes:
if (compressedbytes + 9) < uncompressedbytes:
c1 = (compressedbytes >> 0) & 0xff
c2 = (compressedbytes >> 8) & 0xff
c3 = (compressedbytes >> 16) & 0xff
Expand Down