1

I have a root file with one TTree and 57 branches. I would like to transform this to a root file with just 40 of those branches, with their names changed. I'm doing this with python.

What's the best way to go about this? Should I create a new root file using uproot.recreate? If so, how do I create a new root file with data from 40 of the old branches, with branch names changed?

pseyfert
  • 3,263
  • 3
  • 21
  • 47
Dizzy
  • 33
  • 3
  • That's a good question. In principle, this could be done in an extremely efficient manner, without copying any data, just making a new TTree metadata structure. However, neither Uproot nor ROOT does that. For the time being, I think the only possibility is to iterate over the old file and write to the new file (using Uproot or PyROOT). – Jim Pivarski Nov 16 '21 at 20:45
  • @JimPivarski Do you have an idea how I can write to the new file? Can I load each branch as a numpy array, then do newfile["new tree name"] = {"new branch name": array}? – Dizzy Nov 16 '21 at 23:03
  • See https://uproot.readthedocs.io/en/latest/basic.html#writing-ttrees-to-a-file The syntax you described would make a one-branch tree. You probably want to make an empty tree with `newfile.mktree("name", branch_dtypes)` and fill them in large batches with `newfile["name"].extend(branch_data)`. The large batches are produced by iterating over `uproot.iterate`. – Jim Pivarski Nov 17 '21 at 16:11

0 Answers0