Clone TTree, modify it, and write in a file

Dear all,
I am trying to copy a pre-existing tree from root file, add new branch to the tree, and save (only the new, modified, tree) to a new root file. What I observe is that in new root file are saved both the old and the new ttree. How can I avoid it?

This is the relevant part of the code that I am using:

f_in = TFile.Open('inputFile.root')
t_in = f_in.Get("mytree")
f_out = TFile('outFile.root', "recreate")
t_out = t_in.CloneTree()
modifyTree(t_out) #complicate function to add the new branch to t_out, looping on t_out and uses Fill().
f_out.cd()
t_out.Write()
 

Thank you in advance,
Valerio

What you are doing looks correct seems to me. May be @pcanal will know what’s wrong.

What I observe is that in new root file are saved both the old and the new ttree.

What do you mean here? i.e. what is command and output do you use to reach this conclusion? Also what is that actual content of modifyTree?

Sorry for the late reply. This is why I said there is a duplicate:

root [1] _file0->ls()
TFile**		DKK__B0_DpDs_KK0__B0_DpKK0__1M__1ab.root
 TFile*		DKK__B0_DpDs_KK0__B0_DpKK0__1M__1ab.root
  KEY: TTree	B0_DpKK0;21	 [current cycle]
  KEY: TTree	B0_DpKK0;20	 [backup cycle]

the “backup cycle” has no the extra branch that I am adding in modifyTree(), the “current cycle” instead is the properly modified tree.

The modifyTree is adding a branch to the tree with this approach (this is not the exact code):

def addWeightBranch(myTree) :
    weight = array('f', [0.])
    w_branch = myTree.Branch('weight',weight,'weight/F')
    for ev in myTree :
        weight[0] = ev.var1*ev.var2
        w_branch.Fill()

The “backup cycle” is a snapshot of the meta data of the TTree (i.e. the list of branches and the location in the file of their data baskets) that is kept as a ‘precaution’ when writing the newest version of the meta data (the “current cycle”). So unless something went ‘wrong’ during the writing, you can safely ignore the “backup cycle”. (There is also ways to not keep the “backup cycle” when requesting the writing the TTree). To re-iterate this only means that the meta data is (partially) duplicated AND none of the actual data is duplicated.

Ok, thank you very much for the clarification.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.