How to copy some events from different trees to a new tree?

Hello,
I have two old trees, both trees have same format. Because the varaibles in the tree are too complicate, also it will change at any moment. So I would not like to put the variables name in my code.
Now what I want to do is random sample both old tree, get a event from both old tree( I mean that some time I get a event from tree A, some time from tree B), and copy this events to an new tree. Are there easy way to do it?

Next is my code to do this;

getVariableSpecs(fCurrentTree[index]); //get the branch name and title from the old tree, and put them in the vector bnamesand btitles;
fOutputTree= new TTree(“h3333”," mc data"); //defind the new tree;
for (int i=0; i<bnames.size(); i++) { //defind the branchs
fOutputTree->Branch(bnames[i].c_str(), NULL ,btitles[i].c_str());
}

for(int ievent=0; i<…)

for(int i=0; i<bnames.size(); i++) {
TBranch *b = fCurrentTree[index]->GetBranch(bnames[i].c_str()); //get the branch;
fOutputTree->SetBranchAddress(bnames[i].c_str(),b->GetAddress());
}
fCurrentTree[index]->GetEntry(ievent);
fOutputTree->Fill();
}
When I write out the new tree, I found the varaibles value are 0, also
I checked for the cod , it see that b->GetAddress() will return zero.

any one know what Is wrong?
thanks a lot
Haiping

To copy the address from one tree to the other, you can simply call:
Code:
orig->CopyAddresses(copy);
where orig is the tree with the addresses that you want to copy.

Cheers,
Philippe

Hi,

 Thank you for reply quickly.
But my gold is not to copy one tree address to another tree. I want to take some events randomly from two old tree  to an new tree.

Best regards
—Haiping

[quote] But my gold is not to copy one tree address to another tree. I want to take some events randomly from two old tree to an new tree. [/quote]Yes … and copying the addresses being looked at is the mean to that end … your own code contains:fOutputTree->SetBranchAddress(bnames[i].c_str(),b->GetAddress()); and you requested: [quote]Because the varaibles in the tree are too complicate, also it will change at any moment. So I would not like to put the variables name in my code. [/quote]So what you are looking for is something like:TFile *fone = new TFile("one.root"); TTree *one; f->GetObjet("one",one); TFile *ftwo = new TFile("two.root"); TTree *two; f->GetObjet("two",two); ... TFile *f = new TFile("output.root","RECREATE"); TTree *output; if (tree_are_really_indentical_in_structure) { output = one->CloneTree(0); one->CopyAddresses(two); } else { output = new TTree("h3333"," mc data"); //defind the new tree; for (int i=0; i<bnames.size(); i++) { //defind the branchs output->Branch(bnames[i].c_str(), NULL ,btitles[i].c_str()); } one->CopyAddresses(two); one->CopyAddresses(output); } // Now one, two and output will ALL read and write their data from/to the same place. So the following works for(int i=0; i < number_of_event_we_want; ++i) { if (in_some_case_we_pick_from_one) { one->GetEntry(entry_in_one); } else { // we pick from two two->GetEntry(entry_in_two); } output->Fill(); } f->Write();

Cheers,
Philippe

We have a standard tutorial to do this job. See
$ROOTSYS/tutorials/tree/copytree3.C

Rene