Writing a tchain to a file

Hi,

In my code I have this:

TChain * chain = new TChain("tree");
chain->Add("file1.root");
chain->Add("file2.root");
TTree * tree = chain;

How can I save tree to a file so that when I open the file and open a TBrowser I can view histograms of the variables in tree?

Thanks.

Andrew

Try “hadd -h”.

Hi,

OK, what I really want to do is this:

TChain * chain = new TChain("tree");
chain->Add("file1.root");
chain->Add("file2.root");
TTree * tree = chain;
//make some trees that are friends of tree

Then I would like to save tree and its friends to a file and draw histograms of their variables. How do you recommend doing that? When I save a tree to a file, do its friends automatically get saved as well?

Andrew

[quote] When I save a tree to a file, do its friends automatically get saved as well?[/quote]No.

[quote]Then I would like to save tree and its friends to a file and draw histograms of their variables[/quote]Why? Is there creating TChain objects as needed sufficient? If not, why?

The direct answer to you question that you should use for each chain (and each of the friend (chain)), CloneTree.
See $ROOTSYS/tutorials/tree/copytree* for some example.TChain * chain = new TChain("tree"); chain->Add("file1.root"); chain->Add("file2.root"); TFile *file = TFile::Open("output","RECREATE"); chain->CloneTree(-1,"fast"); file->Write(); delete file.

Cheers,
Philippe.

ok when I use clonetree, does the new tree have the same friends as the old tree? if not, is there some way to clone a tree so that the new tree has the same friends as the old tree?

Hi,

When using CloneTree, the resulting TTree will have the same friend as the first TTree (of the TChain). (No friend will be copied from the TChain object itself).

The genuine question is still:[quote] Why? Isn’t creating TChain objects as needed sufficient? If not, why?[/quote]It seems that you making the problem more complex for no real gain …

Cheers,
Philippe.