uproot

Uproot: Add branch to existing TTree in root file


I am trying to use uproot to add a branch to an existing TTree that I have in a .root file. Is it possible to this with uproot?


Solution

  • No, this is not possible.

    The first difficulty is opening an arbitrary ROOT file and adding any content to it in a robust way (i.e. the result can be read back by ROOT and yet more can be added by ROOT, without segfaults). If you want the long story, it's in scikit-hep/uproot#381, but the short story is that if Uproot makes the file itself, Uproot controls the initial conditions and can put the file into a known good state. We (developers of Uproot) can know some good states without understanding 100% of the state space. But if we want to accept files from ROOT, modify them, and give them back to ROOT, then we have to understand all the possible states that its filesystem-inside-a-file defragmentation algorithm can get into before we can trust our modifications of those states to stay within the space of good states. Understanding that state space would require a lot of study because (1) it's a whole filesystem that can reclaim free space and everything, and (2) there isn't a specification for this aspect of the format. It's defined by what the C++ code does.

    The second difficulty is adding a TBranch to an existing TTree. That's not implemented, but much more manageable: we'd just make a new copy of the TTree metadata with the extra TBranch and link the new metadata to the old TBaskets (data). The main difficulty is adding any object to a preexisting ROOT file.