diff --git a/src/uproot/writing/_cascadetree.py b/src/uproot/writing/_cascadetree.py index f88174df2..9ecd2e87f 100644 --- a/src/uproot/writing/_cascadetree.py +++ b/src/uproot/writing/_cascadetree.py @@ -348,10 +348,11 @@ def _branch_np( if letter is None: raise TypeError(f"cannot write NumPy dtype {branch_dtype} in TTree") - if branch_shape == (): - dims = "" - else: - dims = "".join("[" + str(x) + "]" for x in branch_shape) + dims = "" + if counter is not None: + dims = "[" + counter["fName"] + "]" + if len(branch_shape) > 0: + dims = dims + "".join("[" + str(x) + "]" for x in branch_shape) title = f"{branch_name}{dims}/{letter}" diff --git a/tests/test_1207_fix_title_of_TBranch_with_counter.py b/tests/test_1207_fix_title_of_TBranch_with_counter.py new file mode 100644 index 000000000..83e701adc --- /dev/null +++ b/tests/test_1207_fix_title_of_TBranch_with_counter.py @@ -0,0 +1,21 @@ +# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE + +import os + +import awkward as ak + +import uproot + + +def test(tmp_path): + filename = os.path.join(tmp_path, "file.root") + + with uproot.recreate(filename) as file: + file["tree"] = {"branch": ak.Array([[1.1, 2.2, 3.3], [], [4.4, 5.5]])} + + with uproot.open(filename) as file: + assert file["tree"]["branch"].title == "branch[nbranch]/D" + assert ( + file["tree"]["branch"].member("fLeaves")[0].member("fTitle") + == "branch[nbranch]" + )