Problem with CloneTree GetEntry and Fill while working with multiple root files

Hello everyone, I am working with multiple homogeneous root file, and want to extract some specific events among them.

I am planning to copy the tree structure with CloneTree(0) with the first tree, and then iterate the following root files and entry id,then use GetEntry and Fill to populate the new tree.

But I met segment fault while the new tree changes to another root file.
It said that:

How could I solve this problem?
Thanks~

[quote]But I met segment fault while the new tree changes to another root file.[/quote]How did you change the TTree to another file? Did you use a TChain?

Cheers,
Philippe.

[quote=“pcanal”][quote]But I met segment fault while the new tree changes to another root file.[/quote]How did you change the TTree to another file? Did you use a TChain?

Cheers,
Philippe.[/quote]

Thanks for your reply.

No, I did not do anything. So I think Ttree could not adapt its address to a new tree. Would the TChain change the entry id of event?

The python code is below:

import ROOT
ROOT.gSystem.Load("libRootEvent.so")
old_file = ROOT.TFile('1.dst')
old_tree = old_file.Get("Event")
new_file = ROOT.TFile('test.dst',"RECREATE")
new_tree = old_tree.CloneTree(0)
#Get some entry from first file
old_tree.GetEntry(0)
new_tree.Fill()
new_tree.Write()
print new_tree.GetEntries()

#Change to another file
old_file2 = ROOT.TFile('2.dst')
old_tree2 = old_file2.Get("Event")
#Error occur here
#Need to get some specific id from the second tree.
old_tree2.GetEntry(1)
new_tree.Fill()

print new_tree.GetEntries()

new_tree.Write()

Using a TChain will solve your problem.

I am not sure from your description why the error occur. The behavior I expect from the code you shown is that the 2nd TTree::Fill actually copies the data still from the first input TTree.

The main problem is that you never tell the old_tree2 and the new_tree that they are supposed to be sharing the memory where the data is written.

You could do so by hand but the simpliest is to use a TChain.

old_ch = ROOT.TChain("Event")
old_ch.AddFile("1.dst")
old_ch.AddFile("2.dst")

new_file = ROOT.TFile('test.dst',"RECREATE")
new_tree = old_ch.CloneTree(0)
#Get some entry from first file
old_ch.GetEntry(0)
new_tree.Fill()
new_tree.Write()
print new_tree.GetEntries()

old_ch.GetEntry(number_of_element_in_first_tree + 1) # Usually you get here by iterating 
new_tree.Fill()
new_tree.Write()
print new_tree.GetEntries()

Cheers,
Philippe.

Thanks, the problem has been solved by using TChain.