Logo ROOT   6.08/07
Reference Guide
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
13 void copytree() {
14  gSystem->Load("$ROOTSYS/test/libEvent");
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);
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();
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:1266
virtual void SetBranchStatus(const char *bname, Bool_t status=1, UInt_t *found=0)
Set branch status to Process or DoNotProcess.
Definition: TTree.cxx:7906
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6730
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
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:1819
Basic string class.
Definition: TString.h:137
virtual TTree * CloneTree(Long64_t nentries=-1, Option_t *option="")
Create a clone of this tree and copy nentries.
Definition: TTree.cxx:3004
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7760
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
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:2268
A TTree object has a header with a name and a title.
Definition: TTree.h:98
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1244