Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
25void 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 //Loop in reverse order to make sure that the order of cycles is
34 //preserved.
35 TIter nextkey(source->GetListOfKeys(),kIterBackward);
36 while ((key = (TKey*)nextkey())) {
37 const char *classname = key->GetClassName();
38 TClass *cl = gROOT->GetClass(classname);
39 if (!cl) continue;
41 source->cd(key->GetName());
42 TDirectory *subdir = gDirectory;
43 adir->cd();
44 CopyDir(subdir);
45 adir->cd();
46 } else if (cl->InheritsFrom(TTree::Class())) {
47 TTree *T = (TTree*)source->Get(key->GetName());
48 // Avoid writing the data of a TTree more than once.
49 // Note this assume that older cycles are (as expected) older
50 // snapshots of the TTree meta data.
51 if (!adir->FindObject(key->GetName())) {
52 adir->cd();
53 TTree *newT = T->CloneTree(-1,"fast");
54 newT->Write();
55 }
56 } else {
57 source->cd();
58 TObject *obj = key->ReadObj();
59 adir->cd();
60 obj->Write();
61 delete obj;
62 }
63 }
64 adir->SaveSelf(kTRUE);
65 savdir->cd();
66}
67void CopyFile(const char *fname) {
68 //Copy all objects and subdirs of file fname as a subdir of the current directory
70 TFile *f = TFile::Open(fname);
71 if (!f || f->IsZombie()) {
72 printf("Cannot copy file: %s\n",fname);
73 target->cd();
74 return;
75 }
76 target->cd();
77 CopyDir(f);
78 delete f;
79 target->cd();
80}
81void copyFiles() {
82 //prepare files to be copied
83 if(gSystem->AccessPathName("tot100.root")) {
84 gSystem->CopyFile("hsimple.root", "tot100.root");
85 gSystem->CopyFile("hsimple.root", "hs1.root");
86 gSystem->CopyFile("hsimple.root", "hs2.root");
87 }
88 //main function copying 4 files as subdirectories of a new file
89 TFile *f = new TFile("result.root","recreate");
90 CopyFile("tot100.root");
91 CopyFile("hsimple.root");
92 CopyFile("hs1.root");
93 CopyFile("hs2.root");
94 f->ls();
95 delete f;
96}
#define f(i)
Definition RSha256.hxx:104
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
const Bool_t kIterBackward
Definition TCollection.h:43
#define gDirectory
Definition TDirectory.h:384
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t target
#define gROOT
Definition TROOT.h:406
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
Bool_t InheritsFrom(const char *cl) const override
Return kTRUE if this class inherits from a class with name "classname".
Definition TClass.cxx:4874
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:2968
Describe directory structure in memory.
Definition TDirectory.h:45
static TClass * Class()
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
void ls(Option_t *option="") const override
List Directory contents.
TObject * FindObject(const char *name) const override
Find object by name in the list of memory objects.
virtual Bool_t cd()
Change current directory to "this" directory.
virtual void SaveSelf(Bool_t=kFALSE)
Definition TDirectory.h:255
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/...".
virtual TList * GetListOfKeys() const
Definition TDirectory.h:223
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
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:4082
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
virtual const char * GetClassName() const
Definition TKey.h:75
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition TKey.cxx:758
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Mother of all ROOT objects.
Definition TObject.h:41
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition TObject.cxx:880
virtual int CopyFile(const char *from, const char *to, Bool_t overwrite=kFALSE)
Copy a file.
Definition TSystem.cxx:1341
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:1296
A TTree represents a columnar dataset.
Definition TTree.h:79
Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) override
Write this object to the current directory.
Definition TTree.cxx:9753
static TClass * Class()
double T(double x)