Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
tree112_copy.C File Reference

Detailed Description

View in nbviewer Open in SWAN
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

#ifdef ACTUAL_RUN // -------- Second pass: dictionary already built --------
#include "./Event.h" // now safe to include Event, its dictionary is loaded
void run()
{
TFile oldfile("tree108.root");
TTree *oldtree;
oldfile.GetObject("t4", oldtree);
const auto nentries = oldtree->GetEntries();
Event *event = nullptr;
oldtree->SetBranchAddress("event_split", &event);
// Create a new file + a clone of old tree in new file
TFile newfile("tree112.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();
}
#else // -------- First pass: build dictionary + rerun macro --------
void tree112_copy()
{
TString tutdir = gROOT->GetTutorialDir();
gROOT->ProcessLine(".L " + tutdir + "/io/tree/Event.cxx+");
gROOT->ProcessLine("#define ACTUAL_RUN yes");
gROOT->ProcessLine("#include \"" __FILE__ "\"");
gROOT->ProcessLine("run()");
}
#endif
int nentries
#define gROOT
Definition TROOT.h:417
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
Basic string class.
Definition TString.h:138
A TTree represents a columnar dataset.
Definition TTree.h:89
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr, TClass *realClass, EDataType datatype, bool isptr, bool suppressMissingBranchError)
Definition TTree.cxx:8675
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition TTree.cxx:5718
void Print(Option_t *option="") const override
Print a summary of the tree contents.
Definition TTree.cxx:7414
virtual Long64_t GetEntries() const
Definition TTree.h:510
virtual TTree * CloneTree(Long64_t nentries=-1, Option_t *option="")
Create a clone of this tree and copy nentries.
Definition TTree.cxx:3173
TSeq< int > TSeqI
Definition TSeq.hxx:203
Author
Rene Brun

Definition in file tree112_copy.C.