ROOT logo

From $ROOTSYS/tutorials/tree/copytree3.C

void copytree3() {
// Example of Root macro to copy a subset of a Tree to a new Tree
// Only selected entries are copied to the new Tree.
// The input file has been generated by the program in $ROOTSYS/test/Event
// with   Event 1000 1 99 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");
   Long64_t nentries = oldtree->GetEntries();
   Event *event   = 0;
   oldtree->SetBranchAddress("event",&event);

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

   for (Long64_t i=0;i<nentries; i++) {
      oldtree->GetEntry(i);
      if (event->GetNtrack() > 605) newtree->Fill();
      event->Clear();
   }
   newtree->Print();
   newtree->AutoSave();
   delete oldfile;
   delete newfile;
}
 copytree3.C:1
 copytree3.C:2
 copytree3.C:3
 copytree3.C:4
 copytree3.C:5
 copytree3.C:6
 copytree3.C:7
 copytree3.C:8
 copytree3.C:9
 copytree3.C:10
 copytree3.C:11
 copytree3.C:12
 copytree3.C:13
 copytree3.C:14
 copytree3.C:15
 copytree3.C:16
 copytree3.C:17
 copytree3.C:18
 copytree3.C:19
 copytree3.C:20
 copytree3.C:21
 copytree3.C:22
 copytree3.C:23
 copytree3.C:24
 copytree3.C:25
 copytree3.C:26
 copytree3.C:27
 copytree3.C:28
 copytree3.C:29
 copytree3.C:30
 copytree3.C:31