Logo ROOT   6.14/05
Reference Guide
copytree.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// \notebook -nodraw
4 /// Copy a subset of a Tree to a new Tree
5 ///
6 /// The input file has been generated by the program in `$ROOTSYS/test/Event`
7 /// with `Event 1000 1 1 1`.
8 ///
9 /// \macro_code
10 ///
11 /// \author Rene Brun
12 
13 R__LOAD_LIBRARY($ROOTSYS/test/libEvent.so)
14 
15 void copytree() {
16  //Get old file, old tree and set top branch address
17  TFile *oldfile;
18  TString dir = "$ROOTSYS/test/Event.root";
19  gSystem->ExpandPathName(dir);
20  if (!gSystem->AccessPathName(dir))
21  {oldfile = new TFile("$ROOTSYS/test/Event.root");}
22  else {oldfile = new TFile("./Event.root");}
23  TTree *oldtree = (TTree*)oldfile->Get("T");
24  Event *event = new Event();
25  oldtree->SetBranchAddress("event",&event);
26  oldtree->SetBranchStatus("*",0);
27  oldtree->SetBranchStatus("event",1);
28  oldtree->SetBranchStatus("fNtrack",1);
29  oldtree->SetBranchStatus("fNseg",1);
30  oldtree->SetBranchStatus("fH",1);
31 
32  //Create a new file + a clone of old tree in new file
33  TFile *newfile = new TFile("small.root","recreate");
34  TTree *newtree = oldtree->CloneTree();
35 
36  newtree->Print();
37  newfile->Write();
38  delete oldfile;
39  delete newfile;
40 }
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1276
virtual void SetBranchStatus(const char *bname, Bool_t status=1, UInt_t *found=0)
Set branch status to Process or DoNotProcess.
Definition: TTree.cxx:8128
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6926
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:47
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: test.py:1
Basic string class.
Definition: TString.h:131
virtual TTree * CloneTree(Long64_t nentries=-1, Option_t *option="")
Create a clone of this tree and copy nentries.
Definition: TTree.cxx:2971
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7982
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
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:2336
#define R__LOAD_LIBRARY(LIBRARY)
Definition: Rtypes.h:467
typedef void((*Func_t)())
A TTree object has a header with a name and a title.
Definition: TTree.h:70
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1254