ROOT logo

From $ROOTSYS/tutorials/tree/copytree2.C

void copytree2() {
// Example of Root macro to copy a subset of a Tree to a new Tree
// One branch of the new Tree is written to a separate file
// 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 header. Do not copy events
   TFile *newfile = new TFile("small.root","recreate");
   TTree *newtree = oldtree->CloneTree(0);

   //Divert branch fH to a separate file and copy all events
   newtree->GetBranch("fH")->SetFile("small_fH.root");
   newtree->CopyEntries(oldtree);

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