ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
copytree2.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 ///
4 /// Copy a subset of a Tree to a new Tree, one branch in a separate file
5 ///
6 /// One branch of the new Tree is written to a separate file
7 /// The input file has been generated by the program in $ROOTSYS/test/Event
8 /// with the command `Event 1000 1 1 1`
9 /// \macro_code
10 /// \author Rene Brun
11 
12 void copytree2() {
13 
14  gSystem->Load("$ROOTSYS/test/libEvent");
15 
16  //Get old file, old tree and set top branch address
17  TFile *oldfile = new TFile("$ROOTSYS/test/Event.root");
18  TTree *oldtree = (TTree*)oldfile->Get("T");
19  Event *event = new Event();
20  oldtree->SetBranchAddress("event",&event);
21  oldtree->SetBranchStatus("*",0);
22  oldtree->SetBranchStatus("event",1);
23  oldtree->SetBranchStatus("fNtrack",1);
24  oldtree->SetBranchStatus("fNseg",1);
25  oldtree->SetBranchStatus("fH",1);
26 
27 
28  //Create a new file + a clone of old tree header. Do not copy events
29  TFile *newfile = new TFile("small.root","recreate");
30  TTree *newtree = oldtree->CloneTree(0);
31 
32  //Divert branch fH to a separate file and copy all events
33  newtree->GetBranch("fH")->SetFile("small_fH.root");
34  newtree->CopyEntries(oldtree);
35 
36  newtree->Print();
37  newfile->Write();
38  delete oldfile;
39  delete newfile;
40 }
virtual void SetBranchStatus(const char *bname, Bool_t status=1, UInt_t *found=0)
Set branch status to Process or DoNotProcess.
Definition: TTree.cxx:7656
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1766
virtual TTree * CloneTree(Long64_t nentries=-1, Option_t *option="")
Create a clone of this tree and copy nentries.
Definition: TTree.cxx:2960
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6495
virtual Long64_t CopyEntries(TTree *tree, Long64_t nentries=-1, Option_t *option="")
Copy nentries from given tree to this tree.
Definition: TTree.cxx:3322
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7510
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
Definition: TTree.cxx:4803
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual Int_t Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0)
Write memory objects to this file.
Definition: TFile.cxx:2248
virtual void SetFile(TFile *file=0)
Set file where this branch writes/reads its buffers.
Definition: TBranch.cxx:2230
A TTree object has a header with a name and a title.
Definition: TTree.h:98