Adding many Tree's to a TFile

Wonder if someone could tell me what I am doing wrong.

I have 2 trees A and B. Each have the same structure but contain different instances of objects and have different number of entries. At present each tree is in its own TFile and I would like to put them in the same TFile to ease data access.

I tried the following but got an error.

[code]TFile hfile(“ALL.root”,“RECREATE”,“Data”);

TFile *f1 = new TFile(“A.root”);
TFile *f2 = new TFile(“B.root”);

TTree A = (TTree)f1->Get(“A”);
TTree B = (TTree)f2->Get(“B”);

hfile.WriteTObject(A);
hfile.WriteTObject(B);[/code]

When I run this the resulting hfile is much smaller than the size ot the individual files. When I open hfile in root “A” seems OK but when I try to access “B” (ie B->Draw(“i”)) I get

[quote]: created default TCanvas with name c1
Exception: bad allocation
Error: Symbol G__exception is not defined in current scope (tmpfile)(1)
Error: type G__exception not defined FILE:(tmpfile) LINE:1
(void)0
*** Interpreter error recovered ***[/quote]

Replace your code with:

[code] TFile *f1 = new TFile(“A.root”);
TFile *f2 = new TFile(“B.root”);
TTree A = (TTree)f1->Get(“A”);
TTree B = (TTree)f2->Get(“B”);
TFile hfile(“ALL.root”,“RECREATE”,“Data”);

TTree *AC = A->CloneTree();
AC->Write();
TTree *BC = B->CloneTree();
BC->Write();[/code]

Rene

Thank you. That worked.

-Sanjeev