Logo ROOT   6.08/07
Reference Guide
copyFiles.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_io
3 /// \notebook -nodraw
4 /// Example of script showing how to copy all objects (including directories)
5 /// from a source file.
6 /// For each input file, a new directory is created in the current directory
7 /// with the name of the source file.
8 /// After the execution of:
9 /// ~~~{.bash}
10 /// root [0] .x copyFiles.C
11 /// ~~~
12 /// the file result.root will contain 4 subdirectories:
13 /// "tot100.root", "hsimple.root", "hs1.root","hs2.root"
14 ///
15 /// \macro_code
16 ///
17 /// \author Rene Brun
18 
19 #include "TROOT.h"
20 #include "TKey.h"
21 #include "TFile.h"
22 #include "TSystem.h"
23 #include "TTree.h"
24 
25 void CopyDir(TDirectory *source) {
26  //copy all objects and subdirs of directory source as a subdir of the current directory
27  source->ls();
28  TDirectory *savdir = gDirectory;
29  TDirectory *adir = savdir->mkdir(source->GetName());
30  adir->cd();
31  //loop on all entries of this directory
32  TKey *key;
33  TIter nextkey(source->GetListOfKeys());
34  while ((key = (TKey*)nextkey())) {
35  const char *classname = key->GetClassName();
36  TClass *cl = gROOT->GetClass(classname);
37  if (!cl) continue;
38  if (cl->InheritsFrom(TDirectory::Class())) {
39  source->cd(key->GetName());
40  TDirectory *subdir = gDirectory;
41  adir->cd();
42  CopyDir(subdir);
43  adir->cd();
44  } else if (cl->InheritsFrom(TTree::Class())) {
45  TTree *T = (TTree*)source->Get(key->GetName());
46  adir->cd();
47  TTree *newT = T->CloneTree(-1,"fast");
48  newT->Write();
49  } else {
50  source->cd();
51  TObject *obj = key->ReadObj();
52  adir->cd();
53  obj->Write();
54  delete obj;
55  }
56  }
57  adir->SaveSelf(kTRUE);
58  savdir->cd();
59 }
60 void CopyFile(const char *fname) {
61  //Copy all objects and subdirs of file fname as a subdir of the current directory
62  TDirectory *target = gDirectory;
63  TFile *f = TFile::Open(fname);
64  if (!f || f->IsZombie()) {
65  printf("Cannot copy file: %s\n",fname);
66  target->cd();
67  return;
68  }
69  target->cd();
70  CopyDir(f);
71  delete f;
72  target->cd();
73 }
74 void copyFiles() {
75  //prepare files to be copied
76  if(gSystem->AccessPathName("tot100.root")) {
77  gSystem->CopyFile("hsimple.root", "tot100.root");
78  gSystem->CopyFile("hsimple.root", "hs1.root");
79  gSystem->CopyFile("hsimple.root", "hs2.root");
80  }
81  //main function copying 4 files as subdirectories of a new file
82  TFile *f = new TFile("result.root","recreate");
83  CopyFile("tot100.root");
84  CopyFile("hsimple.root");
85  CopyFile("hs1.root");
86  CopyFile("hs2.root");
87  f->ls();
88  delete f;
89 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
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:830
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 TList * GetListOfKeys() const
Definition: TDirectory.h:158
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:729
double T(double x)
Definition: ChebyshevPol.h:34
virtual const char * GetClassName() const
Definition: TKey.h:77
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
#define gROOT
Definition: TROOT.h:364
virtual TDirectory * mkdir(const char *name, const char *title="")
Create a sub-directory and return a pointer to the created directory.
Definition: TDirectory.cxx:957
virtual TTree * CloneTree(Long64_t nentries=-1, Option_t *option="")
Create a clone of this tree and copy nentries.
Definition: TTree.cxx:3004
const char * Class
Definition: TXMLSetup.cxx:64
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:3907
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:9042
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
Definition: TClass.cxx:4610
virtual void SaveSelf(Bool_t=kFALSE)
Definition: TDirectory.h:189
double f(double x)
Bool_t IsZombie() const
Definition: TObject.h:120
Describe directory structure in memory.
Definition: TDirectory.h:44
Mother of all ROOT objects.
Definition: TObject.h:37
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:730
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
Definition: TDirectory.cxx:435
virtual void ls(Option_t *option="") const
List Directory contents.
virtual int CopyFile(const char *from, const char *to, Bool_t overwrite=kFALSE)
Copy a file.
Definition: TSystem.cxx:1311
A TTree object has a header with a name and a title.
Definition: TTree.h:98
#define gDirectory
Definition: TDirectory.h:221
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void ls(Option_t *option="") const
List file contents.
Definition: TFile.cxx:1367