ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
copyFiles.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_io
3 /// Example of script showing how to copy all objects (including directories)
4 /// from a source file.
5 /// For each input file, a new directory is created in the current directory
6 /// with the name of the source file.
7 /// After the execution of:
8 /// ~~~{.bash}
9 /// root [0] .x copyFiles.C
10 /// ~~~
11 /// the file result.root will contain 4 subdirectories:
12 /// "tot100.root", "hsimple.root", "hs1.root","hs2.root"
13 ///
14 /// \macro_code
15 ///
16 /// \author Rene Brun
17 
18 #include "TROOT.h"
19 #include "TKey.h"
20 #include "TFile.h"
21 #include "TSystem.h"
22 #include "TTree.h"
23 
24 void CopyDir(TDirectory *source) {
25  //copy all objects and subdirs of directory source as a subdir of the current directory
26  source->ls();
27  TDirectory *savdir = gDirectory;
28  TDirectory *adir = savdir->mkdir(source->GetName());
29  adir->cd();
30  //loop on all entries of this directory
31  TKey *key;
32  TIter nextkey(source->GetListOfKeys());
33  while ((key = (TKey*)nextkey())) {
34  const char *classname = key->GetClassName();
35  TClass *cl = gROOT->GetClass(classname);
36  if (!cl) continue;
37  if (cl->InheritsFrom(TDirectory::Class())) {
38  source->cd(key->GetName());
39  TDirectory *subdir = gDirectory;
40  adir->cd();
41  CopyDir(subdir);
42  adir->cd();
43  } else if (cl->InheritsFrom(TTree::Class())) {
44  TTree *T = (TTree*)source->Get(key->GetName());
45  adir->cd();
46  TTree *newT = T->CloneTree(-1,"fast");
47  newT->Write();
48  } else {
49  source->cd();
50  TObject *obj = key->ReadObj();
51  adir->cd();
52  obj->Write();
53  delete obj;
54  }
55  }
56  adir->SaveSelf(kTRUE);
57  savdir->cd();
58 }
59 void CopyFile(const char *fname) {
60  //Copy all objects and subdirs of file fname as a subdir of the current directory
61  TDirectory *target = gDirectory;
62  TFile *f = TFile::Open(fname);
63  if (!f || f->IsZombie()) {
64  printf("Cannot copy file: %s\n",fname);
65  target->cd();
66  return;
67  }
68  target->cd();
69  CopyDir(f);
70  delete f;
71  target->cd();
72 }
73 void copyFiles() {
74  //prepare files to be copied
75  if(gSystem->AccessPathName("tot100.root")) {
76  gSystem->CopyFile("hsimple.root", "tot100.root");
77  gSystem->CopyFile("hsimple.root", "hs1.root");
78  gSystem->CopyFile("hsimple.root", "hs2.root");
79  }
80  //main function copying 4 files as subdirectories of a new file
81  TFile *f = new TFile("result.root","recreate");
82  CopyFile("tot100.root");
83  CopyFile("hsimple.root");
84  CopyFile("hs1.root");
85  CopyFile("hs2.root");
86  f->ls();
87  delete f;
88 }
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:823
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:1213
virtual void ls(Option_t *option="") const
List file contents.
Definition: TFile.cxx:1361
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:158
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:727
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
#define gROOT
Definition: TROOT.h:344
Bool_t IsZombie() const
Definition: TObject.h:141
virtual const char * GetClassName() const
Definition: TKey.h:77
virtual TDirectory * mkdir(const char *name, const char *title="")
Create a sub-directory and return a pointer to the created directory.
Definition: TDirectory.cxx:955
virtual TTree * CloneTree(Long64_t nentries=-1, Option_t *option="")
Create a clone of this tree and copy nentries.
Definition: TTree.cxx:2960
TFile * f
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3851
virtual void ls(Option_t *option="") const
List Directory contents.
Definition: TDirectory.cxx:998
void Class()
Definition: Class.C:29
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:30
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TTree.cxx:8780
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
virtual void SaveSelf(Bool_t=kFALSE)
Definition: TDirectory.h:189
Describe directory structure in memory.
Definition: TDirectory.h:44
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
Mother of all ROOT objects.
Definition: TObject.h:58
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:727
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
Definition: TDirectory.cxx:433
virtual int CopyFile(const char *from, const char *to, Bool_t overwrite=kFALSE)
Copy a file.
Definition: TSystem.cxx:1258
A TTree object has a header with a name and a title.
Definition: TTree.h:98
#define gDirectory
Definition: TDirectory.h:221
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
Definition: TClass.cxx:4498
const Bool_t kTRUE
Definition: Rtypes.h:91
TObject * obj