Using TTree::MergeTrees() leads to segfault

This works:

TFile* f1 = new TFile("in1.root", "READ"); TFile* f2 = new TFile("in1.root", "READ"); TTree* t1 = (TTree*)f1->Get("tree1"); TTree* t2 = (TTree*)f2->Get("tree1"); TList* l = new TList(); l->Add(t1); l->Add(t2); TFile* f3 = new TFile("out.root", "RECREATE"); TTree* t3 = TTree::MergeTrees(l); t3->Write();

This works (file and tree names changed, rest as above):

TFile* f1 = new TFile("in2.root", "READ"); TFile* f2 = new TFile("in2.root", "READ"); TTree* t1 = (TTree*)f1->Get("tree2"); TTree* t2 = (TTree*)f2->Get("tree2"); TList* l = new TList(); l->Add(t1); l->Add(t2); TFile* f3 = new TFile("out.root", "RECREATE"); TTree* t3 = TTree::MergeTrees(l); t3->Write();

This segfaults (file and tree names changed, rest as above):

TFile* f1 = new TFile("in1.root", "READ"); TFile* f2 = new TFile("in2.root", "READ"); TTree* t1 = (TTree*)f1->Get("tree1"); TTree* t2 = (TTree*)f2->Get("tree2"); TList* l = new TList(); l->Add(t1); l->Add(t2); TFile* f3 = new TFile("out.root", "RECREATE"); TTree* t3 = TTree::MergeTrees(l); t3->Write();

The trees in both files have the same branches, since they are produced by the same code. In fact, if I rename tree1 to tree2, I can hadd the two files just fine, so I don’t see a reason why the code above would not work. What am I doing wrong?

Hi,

I can not reproduce the problem (with a random tree), to track it down I would need access to the two input file.

Alternatively, you could also use TChain and CloneTree.

TChain ch("tree1"); ch.AddFile("in1.root"); ch.AddFile("in2.root/tree2); TFile* f3 = new TFile("out.root", "RECREATE"); ch.CloneTree(-1,"fast"); f3->Write(); delete f3;

Cheers,
Philippe.

Thanks for the help.

When I tried it again this morning, I didn’t run into a segfault, but the output file was corrupted. I then added

delete f3; at the end and received back the segfault.

The TChain method works flawlessly though.