ROOT logo

From $ROOTSYS/tutorials/tree/copytree.C

void copytree() {
// Example of Root macro to copy a subset of a Tree to a new Tree
// The input file has been generated by the program in $ROOTSYS/test/Event
// with   Event 1000 1 1 1
//Author: Rene Brun
   
   gSystem->Load("$ROOTSYS/test/libEvent");

   //Get old file, old tree and set top branch address
   TFile *oldfile = new TFile("$ROOTSYS/test/Event.root");
   TTree *oldtree = (TTree*)oldfile->Get("T");
   Event *event   = new Event();
   oldtree->SetBranchAddress("event",&event);
   oldtree->SetBranchStatus("*",0);
   oldtree->SetBranchStatus("event",1);
   oldtree->SetBranchStatus("fNtrack",1);
   oldtree->SetBranchStatus("fNseg",1);
   oldtree->SetBranchStatus("fH",1);

   //Create a new file + a clone of old tree in new file
   TFile *newfile = new TFile("small.root","recreate");
   TTree *newtree = oldtree->CloneTree();

   newtree->Print();
   newfile->Write();
   delete oldfile;
   delete newfile;
}
 copytree.C:1
 copytree.C:2
 copytree.C:3
 copytree.C:4
 copytree.C:5
 copytree.C:6
 copytree.C:7
 copytree.C:8
 copytree.C:9
 copytree.C:10
 copytree.C:11
 copytree.C:12
 copytree.C:13
 copytree.C:14
 copytree.C:15
 copytree.C:16
 copytree.C:17
 copytree.C:18
 copytree.C:19
 copytree.C:20
 copytree.C:21
 copytree.C:22
 copytree.C:23
 copytree.C:24
 copytree.C:25
 copytree.C:26
 copytree.C:27
 copytree.C:28
 copytree.C:29