Logo ROOT  
Reference Guide
copyFiles.C File Reference

Detailed Description

View in nbviewer Open in SWAN Example of script showing how to copy all objects (including directories) from a source file. For each input file, a new directory is created in the current directory with the name of the source file. After the execution of:

root [0] .x copyFiles.C

the file result.root will contain 4 subdirectories: "tot100.root", "hsimple.root", "hs1.root","hs2.root"

#include "TROOT.h"
#include "TKey.h"
#include "TFile.h"
#include "TSystem.h"
#include "TTree.h"
void CopyDir(TDirectory *source) {
//copy all objects and subdirs of directory source as a subdir of the current directory
source->ls();
TDirectory *adir = savdir->mkdir(source->GetName());
adir->cd();
//loop on all entries of this directory
TKey *key;
//Loop in reverse order to make sure that the order of cycles is
//preserved.
TIter nextkey(source->GetListOfKeys(),kIterBackward);
while ((key = (TKey*)nextkey())) {
const char *classname = key->GetClassName();
TClass *cl = gROOT->GetClass(classname);
if (!cl) continue;
source->cd(key->GetName());
adir->cd();
CopyDir(subdir);
adir->cd();
} else if (cl->InheritsFrom(TTree::Class())) {
TTree *T = (TTree*)source->Get(key->GetName());
// Avoid writing the data of a TTree more than once.
// Note this assume that older cycles are (as expected) older
// snapshots of the TTree meta data.
if (!adir->FindObject(key->GetName())) {
adir->cd();
TTree *newT = T->CloneTree(-1,"fast");
newT->Write();
}
} else {
source->cd();
TObject *obj = key->ReadObj();
adir->cd();
obj->Write();
delete obj;
}
}
adir->SaveSelf(kTRUE);
savdir->cd();
}
void CopyFile(const char *fname) {
//Copy all objects and subdirs of file fname as a subdir of the current directory
TFile *f = TFile::Open(fname);
if (!f || f->IsZombie()) {
printf("Cannot copy file: %s\n",fname);
target->cd();
return;
}
target->cd();
CopyDir(f);
delete f;
target->cd();
}
void copyFiles() {
//prepare files to be copied
if(gSystem->AccessPathName("tot100.root")) {
gSystem->CopyFile("hsimple.root", "tot100.root");
gSystem->CopyFile("hsimple.root", "hs1.root");
gSystem->CopyFile("hsimple.root", "hs2.root");
}
//main function copying 4 files as subdirectories of a new file
TFile *f = new TFile("result.root","recreate");
CopyFile("tot100.root");
CopyFile("hsimple.root");
CopyFile("hs1.root");
CopyFile("hs2.root");
f->ls();
delete f;
}
Author
Rene Brun

Definition in file copyFiles.C.

kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TDirectory::mkdir
virtual TDirectory * mkdir(const char *name, const char *title="", Bool_t returnExistingDirectory=kFALSE)
Create a sub-directory "a" or a hierarchy of sub-directories "a/b/c/...".
Definition: TDirectory.cxx:1042
TDirectory::GetListOfKeys
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:168
f
#define f(i)
Definition: RSha256.hxx:122
TTree
Definition: TTree.h:79
TFile::Open
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3946
TDirectory::cd
virtual Bool_t cd(const char *path=nullptr)
Change current directory to "this" directory.
Definition: TDirectory.cxx:498
TTree.h
TSystem::AccessPathName
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:1294
TFile.h
TDirectory::Get
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:806
TSystem::CopyFile
virtual int CopyFile(const char *from, const char *to, Bool_t overwrite=kFALSE)
Copy a file.
Definition: TSystem.cxx:1339
TTree::Write
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:9563
kIterBackward
const Bool_t kIterBackward
Definition: TCollection.h:41
TROOT.h
TClass::InheritsFrom
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
Definition: TClass.cxx:4777
TSystem.h
TKey::GetClassName
virtual const char * GetClassName() const
Definition: TKey.h:76
gDirectory
#define gDirectory
Definition: TDirectory.h:236
TDirectory::SaveSelf
virtual void SaveSelf(Bool_t=kFALSE)
Definition: TDirectory.h:199
TFile
Definition: TFile.h:54
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
TDirectory::ls
void ls(Option_t *option="") const override
List Directory contents.
Definition: TDirectory.cxx:1087
TKey
Definition: TKey.h:28
TKey::ReadObj
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:750
TClass
Definition: TClass.h:80
TObject::Write
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:795
TObject
Definition: TObject.h:37
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:52
TDirectory
Definition: TDirectory.h:40
TIter
Definition: TCollection.h:233
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
TKey.h
Class
void Class()
Definition: Class.C:29
TDirectory::FindObject
TObject * FindObject(const char *name) const override
Find object by name in the list of memory objects.
Definition: TDirectory.cxx:724
gROOT
#define gROOT
Definition: TROOT.h:406