00001 void copytree2() { 00002 // Example of Root macro to copy a subset of a Tree to a new Tree 00003 // One branch of the new Tree is written to a separate file 00004 // The input file has been generated by the program in $ROOTSYS/test/Event 00005 // with Event 1000 1 1 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 Event *event = new Event(); 00014 oldtree->SetBranchAddress("event",&event); 00015 oldtree->SetBranchStatus("*",0); 00016 oldtree->SetBranchStatus("event",1); 00017 oldtree->SetBranchStatus("fNtrack",1); 00018 oldtree->SetBranchStatus("fNseg",1); 00019 oldtree->SetBranchStatus("fH",1); 00020 00021 00022 //Create a new file + a clone of old tree header. Do not copy events 00023 TFile *newfile = new TFile("small.root","recreate"); 00024 TTree *newtree = oldtree->CloneTree(0); 00025 00026 //Divert branch fH to a separate file and copy all events 00027 newtree->GetBranch("fH")->SetFile("small_fH.root"); 00028 newtree->CopyEntries(oldtree); 00029 00030 newtree->Print(); 00031 newfile->Write(); 00032 delete oldfile; 00033 delete newfile; 00034 }