00001 void copytree3() { 00002 // Example of Root macro to copy a subset of a Tree to a new Tree 00003 // Only selected entries are copied to the new Tree. 00004 // The input file has been generated by the program in $ROOTSYS/test/Event 00005 // with Event 1000 1 99 1 00006 //Author: Rene Brun 00007 00008 gSystem->Load("$ROOTSYS/test/libEvent"); 00009 00010 //Get old file, old tree and set top branch address 00011 TFile *oldfile = new TFile("$ROOTSYS/test/Event.root"); 00012 TTree *oldtree = (TTree*)oldfile->Get("T"); 00013 Int_t nentries = (Int_t)oldtree->GetEntries(); 00014 Event *event = 0; 00015 oldtree->SetBranchAddress("event",&event); 00016 00017 //Create a new file + a clone of old tree in new file 00018 TFile *newfile = new TFile("small.root","recreate"); 00019 TTree *newtree = oldtree->CloneTree(0); 00020 00021 for (Int_t i=0;i<nentries; i++) { 00022 oldtree->GetEntry(i); 00023 if (event->GetNtrack() > 605) newtree->Fill(); 00024 event->Clear(); 00025 } 00026 newtree->Print(); 00027 newtree->AutoSave(); 00028 delete oldfile; 00029 delete newfile; 00030 }