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, 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 ///
10 /// \macro_code
11 ///
12 /// \author Rene Brun
14 void copytree2() {
16  gSystem->Load("$ROOTSYS/test/libEvent");
18  //Get old file, old tree and set top branch address
19  TFile *oldfile;
20  TString dir = "$ROOTSYS/test/Event.root";
21  gSystem->ExpandPathName(dir);
22  if (!gSystem->AccessPathName(dir))
23  {oldfile = new TFile("$ROOTSYS/test/Event.root");}
24  else {oldfile = new TFile("./Event.root");} TTree *oldtree = (TTree*)oldfile->Get("T");
25  Event *event = new Event();
26  oldtree->SetBranchAddress("event",&event);
27  oldtree->SetBranchStatus("*",0);
28  oldtree->SetBranchStatus("event",1);
29  oldtree->SetBranchStatus("fNtrack",1);
30  oldtree->SetBranchStatus("fNseg",1);
31  oldtree->SetBranchStatus("fH",1);
34  //Create a new file + a clone of old tree header. Do not copy events
35  TFile *newfile = new TFile("small.root","recreate");
36  TTree *newtree = oldtree->CloneTree(0);
38  //Divert branch fH to a separate file and copy all events
39  newtree->GetBranch("fH")->SetFile("small_fH.root");
40  newtree->CopyEntries(oldtree);
42  newtree->Print();
43  newfile->Write();
44  delete oldfile;
45  delete newfile;
46 }
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 Long64_t CopyEntries(TTree *tree, Long64_t nentries=-1, Option_t *option="")
Copy nentries from given tree to this tree.
Definition: TTree.cxx:3366
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7760
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
Definition: TTree.cxx:4869
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
virtual void SetFile(TFile *file=0)
Set file where this branch writes/reads its buffers.
Definition: TBranch.cxx:2242
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