Logo ROOT   6.12/07
Reference Guide
copytree2.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, 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
13 
14 R__LOAD_LIBRARY($ROOTSYS/test/libEvent.so)
15 
16 void copytree2() {
17 
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);
32 
33 
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);
37 
38  //Divert branch fH to a separate file and copy all events
39  newtree->GetBranch("fH")->SetFile("small_fH.root");
40  newtree->CopyEntries(oldtree);
41 
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: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:8044
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6864
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: test.py:1
Basic string class.
Definition: TString.h:125
virtual TTree * CloneTree(Long64_t nentries=-1, Option_t *option="")
Create a clone of this tree and copy nentries.
Definition: TTree.cxx:2961
virtual Long64_t CopyEntries(TTree *tree, Long64_t nentries=-1, Option_t *option="")
Copy nentries from given tree to this tree.
Definition: TTree.cxx:3343
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7898
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
Definition: TTree.cxx:4985
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:2313
#define R__LOAD_LIBRARY(LIBRARY)
Definition: Rtypes.h:470
typedef void((*Func_t)())
virtual void SetFile(TFile *file=0)
Set file where this branch writes/reads its buffers.
Definition: TBranch.cxx:2421
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