Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_branch_basket_one_tleafb(tmp_path):
filename = join(str(tmp_path), "example.root")
b = newbranch("int8")
branchdict = {"int8Branch": b}
tree = newtree(branchdict)
a = numpy.array([1, 2, 3, 4, 5], dtype="int8")
with uproot.recreate(filename, compression=None) as f:
f["t"] = tree
f["t"]["int8Branch"].newbasket(a)
f = ROOT.TFile.Open(filename)
tree = f.Get("t")
treedata = tree.AsMatrix().astype("int8")
for i in range(5):
assert a[i] == treedata[i]
def test_one_branch_multi_basket(tmp_path):
filename = join(str(tmp_path), "example.root")
b = newbranch("int32")
branchdict = {"intBranch": b}
tree = newtree(branchdict)
a = numpy.array([1, 2, 3, 4, 5], dtype=">i4")
b = numpy.array([6, 7, 8, 9, 10], dtype=">i4")
with uproot.recreate(filename, compression=None) as f:
f["t"] = tree
f["t"]["intBranch"].newbasket(a)
f["t"]["intBranch"].newbasket(b)
f = ROOT.TFile.Open(filename)
tree = f.Get("t")
treedata = tree.AsMatrix().astype(">i4")
for i in range(5):
assert a[i] == treedata[i]
assert b[i] == treedata[i+5]
def test_ttree_empty_tbranch_multitree_uproot(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:
for i in range(10):
f["t"*(i+1)] = tree
f = uproot.open(filename)
for i in range(10):
assert f["t" * (i + 1)]["intBranch"]._classname == b"TBranch"
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"
def test_branch_basket_one_rewrite_root(tmp_path):
filename = join(str(tmp_path), "example.root")
b = newbranch("int32")
branchdict = {"intBranch": b}
tree = newtree(branchdict)
a = numpy.array([1, 2, 3, 4, 5]).astype("int32").newbyteorder(">")
with uproot.recreate(filename, compression=None) as f:
f["t"] = tree
f["t"]["intBranch"].newbasket(a)
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_many_basket(tmp_path):
filename = join(str(tmp_path), "example.root")
b = newbranch(">i4")
branchdict = {"intBranch": b}
tree = newtree(branchdict)
a = numpy.array([1], dtype=">i4")
with uproot.recreate(filename, compression=None) as f:
f["t"] = tree
for i in range(101):
f["t"]["intBranch"].newbasket(a)
f = ROOT.TFile.Open(filename)
tree = f.Get("t")
treedata = tree.AsMatrix().astype(">i4")
for i in range(101):
assert a[0] == treedata[i]
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_ttree_empty_tbranch_uproot(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 = uproot.open(filename)
assert f["t"]["intBranch"]._classname == b"TBranch"
def test_branch_basket_one_tleaff(tmp_path):
filename = join(str(tmp_path), "example.root")
b = newbranch("float32")
branchdict = {"floatBranch": b}
tree = newtree(branchdict)
a = numpy.array([1, 2, 3, 4, 5], dtype="float32")
with uproot.recreate(filename, compression=None) as f:
f["t"] = tree
f["t"]["floatBranch"].newbasket(a)
f = ROOT.TFile.Open(filename)
tree = f.Get("t")
treedata = tree.AsMatrix().astype("float32")
for i in range(5):
assert a[i] == treedata[i]
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