Example of Root macro to copy a subset of a Tree to a new Tree, selecting entries.
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
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, April 17, 2024 at 11:23 AM.
.! $ROOTSYS/test/eventexe 1000 1 1 1
Arguments are defined.
Get old file, old tree and set top branch address
TString dir = "$ROOTSYS/test/Event.root";
gSystem->ExpandPathName(dir);
const auto filename = gSystem->AccessPathName(dir) ? "./Event.root" : "$ROOTSYS/test/Event.root";
TFile oldfile(filename);
TTree *oldtree;
oldfile.GetObject("T", oldtree);
const auto nentries = oldtree->GetEntries();
Event *event = nullptr;
oldtree->SetBranchAddress("event", &event);
Create a new file + a clone of old tree in new file
TFile newfile("small.root", "recreate");
auto newtree = oldtree->CloneTree(0);
for (auto i : ROOT::TSeqI(nentries)) {
oldtree->GetEntry(i);
if (event->GetNtrack() > 605)
newtree->Fill();
event->Clear();
}
newtree->Print();
newfile.Write();