{ gROOT->Reset(); Int_t Evnt; TFile *file_total = new TFile("total.root","RECREATE"); TFile *file_1 = new TFile("file1.root","READ"); TTree *tree_1 = (TTree*)file_1->Get("tree1")->Clone("tree_1"); tree_1->SetBranchAddress("Evnt",&Evnt); //Build the indexer according to Evnt branch tree_1->BuildIndex("Evnt"); TFile *file_2 = new TFile("file2.root","READ"); TTree *tree_2 = (TTree*)file_2->Get("tree2")->Clone("tree_2"); //Build the indexer according to Eventno branch // (should be same as Evnt for previous tree) tree_2->BuildIndex("Eventno"); file_total->cd(); TTree *tree_temp = tree_temp=tree_1->CopyTree(""); tree_temp->AddFriend(tree_2); // tree_temp->SetName("tree_temp"); //No entries are copied just the structure, correct? TTree *tree_total = tree_temp->CopyTree("","",0,0); // The number of Entries of this tree // is be the bigger of the two friends, // correct ? for(int i=0; i< tree_temp->GetEntries(); i++){ tree_1->GetEntry(i); tree_2->GetEntryWithIndex(Evnt); tree_total->Fill(); } tree_total->SetName("tree_total"); file_total->cd(); tree_total->Write(); }