How can I resolve the 'Basket data should be added to all branches' exception when writing to a TTree with Uproot?

30 Views Asked by At

Reading from an ntuple and saving into a new ntuple, after many iterations over events, getting the following:

    utils.writeTree(outf, ltree, outtreename, brs)
  File "/srv01/agrp/roybr/zprimeplusxntr/UprootFramework/utils.py", line 273, in writeTree
    outf[treename].extend({ br[2] : events[br[0]][br[1]] for br in branches })
  File "/srv01/agrp/roybr/.local/lib/python3.9/site-packages/uproot3/write/objects/TTree.py", line 109, in extend
    raise Exception("Basket data should be added to all branches")
Exception: Basket data should be added to all branches

Unclear as to why this is happening. Any help would be highly appreciated!

I expected the code to run smoothly. Not clear why extending the tree and writing into it should create such an exception, especially after iterating over many events without special problems.

================================================= EDIT

Let me share a longer snippet of the lines prior to the one shown above, mentioned by the trace:

            elif "fakeWeight" in ltree.weight.fields and "em" not in lep:
                print ("extending tree for fakes")
                brs = brlist(lep, True)
                brs.extend([ ["weight", "fakeWeight"] ])
                brs.extend([ ["weight", "leptonSF_iso"] ])
                #utils.writeTree(outf, ltree, outtreename, brlist(lep))
                utils.writeTree(outf, ltree, outtreename, brs)
            else:
                #utils.writeTree(outf, ltree, outtreename, brlist(lep))
                utils.writeTree(outf, ltree, outtreename, brs)
1

There are 1 best solutions below

3
Jim Pivarski On

One thing that would be needed to diagnose this is the code that led to the error. While the end of the stack trace is useful, it's not enough.

The stack trace is showing two things, though. One is that this is in a library called UprootFramework, which seems to be built on top of Uproot. In principle, the error (whatever it is) could be a user mistake, it could be in the UprootFramework, or it could be in Uproot itself. (Or it could be in mismatched assumptions between two of these layers, which amounts to the same thing.)

The other thing that I noticed is that this is Uproot3, a legacy version of Uproot that was superseded by Uproot 4.0 in 2020 and then Uproot 5.0 in 2022. In particular, the file-writing code was thoroughly overhauled because it was experimental and buggy in Uproot3. This long thread chronicles the rewrite effort. If you go to the Uproot3 GitHub page, you'll see that it's archived and has warning messages about being deprecated. Even if there are bugs in file-writing in Uproot3, it would be counterproductive now to investigate them. This is a version 3 → 4+ update guide and this is how Uproot-writing works in version 4+.

Good luck!