ROOT logo
// Author: Rene Brun

#include "TROOT.h"
#include "TKey.h"
#include "TFile.h"
#include "TSystem.h"
#include "TTree.h"

  //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 execution of:
  // root > .x copyFiles.C
  //the file result.root will contain 4 subdirectories:
  // "tot100.root", "hsimple.root", "hs1.root","hs2.root"
      
void CopyDir(TDirectory *source) {
   //copy all objects and subdirs of directory source as a subdir of the current directory   
   source->ls();
   TDirectory *savdir = gDirectory;
   TDirectory *adir = savdir->mkdir(source->GetName());
   adir->cd();
   //loop on all entries of this directory
   TKey *key;
   TIter nextkey(source->GetListOfKeys());
   while ((key = (TKey*)nextkey())) {
      const char *classname = key->GetClassName();
      TClass *cl = gROOT->GetClass(classname);
      if (!cl) continue;
      if (cl->InheritsFrom("TDirectory")) {
         source->cd(key->GetName());
         TDirectory *subdir = gDirectory;
         adir->cd();
         CopyDir(subdir);
         adir->cd();
      } else if (cl->InheritsFrom("TTree")) {
         TTree *T = (TTree*)source->Get(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
   TDirectory *target = gDirectory;
   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() {
   //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;
}
 copyFiles.C:1
 copyFiles.C:2
 copyFiles.C:3
 copyFiles.C:4
 copyFiles.C:5
 copyFiles.C:6
 copyFiles.C:7
 copyFiles.C:8
 copyFiles.C:9
 copyFiles.C:10
 copyFiles.C:11
 copyFiles.C:12
 copyFiles.C:13
 copyFiles.C:14
 copyFiles.C:15
 copyFiles.C:16
 copyFiles.C:17
 copyFiles.C:18
 copyFiles.C:19
 copyFiles.C:20
 copyFiles.C:21
 copyFiles.C:22
 copyFiles.C:23
 copyFiles.C:24
 copyFiles.C:25
 copyFiles.C:26
 copyFiles.C:27
 copyFiles.C:28
 copyFiles.C:29
 copyFiles.C:30
 copyFiles.C:31
 copyFiles.C:32
 copyFiles.C:33
 copyFiles.C:34
 copyFiles.C:35
 copyFiles.C:36
 copyFiles.C:37
 copyFiles.C:38
 copyFiles.C:39
 copyFiles.C:40
 copyFiles.C:41
 copyFiles.C:42
 copyFiles.C:43
 copyFiles.C:44
 copyFiles.C:45
 copyFiles.C:46
 copyFiles.C:47
 copyFiles.C:48
 copyFiles.C:49
 copyFiles.C:50
 copyFiles.C:51
 copyFiles.C:52
 copyFiles.C:53
 copyFiles.C:54
 copyFiles.C:55
 copyFiles.C:56
 copyFiles.C:57
 copyFiles.C:58
 copyFiles.C:59
 copyFiles.C:60
 copyFiles.C:61
 copyFiles.C:62
 copyFiles.C:63
 copyFiles.C:64
 copyFiles.C:65
 copyFiles.C:66
 copyFiles.C:67
 copyFiles.C:68
 copyFiles.C:69
 copyFiles.C:70
 copyFiles.C:71
 copyFiles.C:72
 copyFiles.C:73
 copyFiles.C:74
 copyFiles.C:75
 copyFiles.C:76
 copyFiles.C:77