How to write a copy of a root file but with data in one branch in one tree modified, in Python

Hi,

How do I copy all the contents of an input root file, write it to an output root file, but with the data within one branch, a jet collection called “AntiKt4EMPFlowJets”, of one tree, called “CollectionTree”, modified? I want the output file to be an exact copy of the input except with the vector of xAOD::Jet_v1 objects that is inside the branch to be modified with different momenta. I already have the new xAOD::Jet_v1 objects. Here is the code I was using to try to make the output file:

import ROOT

ROOT.xAOD.Init()

inputFileName = "samples/DAOD_PHYS.37228588._000046.pool.root.1"
outputFileName = "fakeSignalSample.root"
treeName = "CollectionTree"
jetCollectionName = "AntiKt4EMPFlowJets"

inputFile = ROOT.TFile.Open(inputFileName, "READ")
inputTree = inputFile.Get(treeName)
tree = ROOT.xAOD.MakeTransientTree(inputFile, treeName)

outputFile = ROOT.TFile.Open(outputFileName, "RECREATE")

# Copy over everything except for CollectionTree
inputFile.cd()
for key in ROOT.gDirectory.GetListOfKeys():
    obj = key.ReadObj()
    if obj.GetName() != treeName:
        print(f"Writing object {obj.GetName()}")
        outputFile.cd()
        obj.Write()

outputFile.cd()
outputTree = inputTree.CloneTree(0) # Clone structure of CollectionTree

outputJets = ROOT.std.vector(ROOT.TLorentzVector)()
outputTree.Branch(jetCollectionName, outputJets) # create branch for modified jets

# Loop over events
for entry in range(inputTree.GetEntries()):
    tree.GetEntry(entry)
    inputJets = getattr(tree, jetCollectionName)
    
    outputJets.clear()

   # some code here to get array of new momenta, jetMomentaCoplanar, not important
    
    for p4 in jetMomentaCoplanar:
        newJet = ROOT.TLorentzVector()
        newJet.SetPxPyPzE(p4.Px(), p4.Py(), p4.Pz(), p4.E())
        outputJets.push_back(newJet)
    
    outputTree.Fill() # Fill output tree with modified jet collection and all other branches
    
outputTree.Write()
outputFile.Close()
inputFile.Close()

ROOT.xAOD.ClearTransientTrees()

I omitted the part of the code that has to do with getting the new momenta, since it isn’t important. I’m just trying to figure out how to write the new file. This code makes a new file that appears to have the same contents as the input, but there are things that don’t work. If I try to run my analysis with the new file, I get this error:

Error R__unzip_header: error in header.  Values: 00
TBasket::ReadBasketBuf... ERROR   Inconsistency found in header (nin=0, nbuf=0)
TBasket::ReadBasketBuf... ERROR   fNbytes = 592470386, fKeylen = 28019, fObjlen = 1929913123, noutot = 0, nout=0, nin=0, nbuf=0
TBranch::GetBasket        ERROR   File: fakeSignalSample.root at byte:644015846980911224, branch:db_string, entry:0, badread=1, nerrors=1, basketnumber=0

I get these errors/warnings when running the script:

xAOD::Init                INFO    Environment initialised for data access
xAOD::MakeTransientTree   INFO    Created transient tree "CollectionTree" in ROOT's common memory
TBranch::WriteBasketImpl  ERROR   basket's WriteBuffer failed.
TBranch::WriteBasketImpl  ERROR   basket's WriteBuffer failed.
TBranch::WriteBasketImpl  ERROR   basket's WriteBuffer failed.
TBranch::WriteBasketImpl  ERROR   basket's WriteBuffer failed.
TTree::Bronch             WARNING Using split mode on a class: TLorentzVector with a custom Streamer
TStreamerInfo::WriteBu... ERROR   The element xAOD::AuxInfoBase::ILockable type 100 (BASE) is not supported yet

TStreamerInfo::WriteBu... ERROR   The element xAOD::AuxInfoBase::ILockable type 100 (BASE) is not supported yet

TStreamerInfo::WriteBu... ERROR   The element xAOD::AuxInfoBase::ILockable type 100 (BASE) is not supported yet

TStreamerInfo::WriteBu... ERROR   The element xAOD::AuxInfoBase::ILockable type 100 (BASE) is not supported yet

I feel like my task should be simple but I really have no idea. Any help would be appreciated, thank you.

Hi Luca,

Thanks for the post and welcome to the ROOT Community!

I suspect that here not only ROOT is involved, but to a great extent also the ATLAS setup. ROOT will support you, however I would like to ask whether you asked this question also in an ATLAS specific forum first, because I suspect a lot of domain specific knowledge will be needed here.

Cheers,
Danilo

Hi Danilo,

Thanks for your response. Is there an atlas specific section on the root forum or is there somewhere else I should go to ask this question?

Thanks,

Luca

Hi,

Sorry for the delay in answering: unfortunately I do not know what could be the right ATLAS forum.

Best,
D