31 void MergeRootfile( TDirectory *target, TList *sourcelist );
 
   36    if(gSystem->AccessPathName(
"hsimple1.root")) {
 
   37      gSystem->CopyFile(
"hsimple.root", 
"hsimple1.root");
 
   38      gSystem->CopyFile(
"hsimple.root", 
"hsimple2.root");
 
   46    Target = TFile::Open( 
"result.root", 
"RECREATE" );
 
   48    FileList = 
new TList();
 
   49    FileList->Add( TFile::Open(
"hsimple1.root") );
 
   50    FileList->Add( TFile::Open(
"hsimple2.root") );
 
   52    MergeRootfile( Target, FileList );
 
   56 void MergeRootfile( TDirectory *target, TList *sourcelist ) {
 
   59    TString path( (
char*)strstr( target->GetPath(), 
":" ) );
 
   62    TFile *first_source = (TFile*)sourcelist->First();
 
   63    first_source->cd( path );
 
   64    TDirectory *current_sourcedir = gDirectory;
 
   66    Bool_t status = TH1::AddDirectoryStatus();
 
   67    TH1::AddDirectory(kFALSE);
 
   70    TChain *globChain = 0;
 
   71    TIter nextkey( current_sourcedir->GetListOfKeys() );
 
   73    while ( (key = (TKey*)nextkey())) {
 
   76       if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) 
continue;
 
   79       first_source->cd( path );
 
   80       TObject *obj = key->ReadObj();
 
   82       if ( obj->IsA()->InheritsFrom( TH1::Class() ) ) {
 
   90          TFile *nextsource = (TFile*)sourcelist->After( first_source );
 
   91          while ( nextsource ) {
 
   94             nextsource->cd( path );
 
   95             TKey *key2 = (TKey*)gDirectory->GetListOfKeys()->FindObject(h1->GetName());
 
   97                TH1 *h2 = (TH1*)key2->ReadObj();
 
  102             nextsource = (TFile*)sourcelist->After( nextsource );
 
  105       else if ( obj->IsA()->InheritsFrom( TTree::Class() ) ) {
 
  108          const char* obj_name= obj->GetName();
 
  110          globChain = 
new TChain(obj_name);
 
  111          globChain->Add(first_source->GetName());
 
  112          TFile *nextsource = (TFile*)sourcelist->After( first_source );
 
  115          while ( nextsource ) {
 
  117             globChain->Add(nextsource->GetName());
 
  118             nextsource = (TFile*)sourcelist->After( nextsource );
 
  121       } 
else if ( obj->IsA()->InheritsFrom( TDirectory::Class() ) ) {
 
  124          cout << 
"Found subdirectory " << obj->GetName() << endl;
 
  128          TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() );
 
  133          MergeRootfile( newdir, sourcelist );
 
  138          cout << 
"Unknown object type, name: " 
  139          << obj->GetName() << 
" title: " << obj->GetTitle() << endl;
 
  150          if(obj->IsA()->InheritsFrom( TTree::Class() ))
 
  151             globChain->Merge(target->GetFile(),0,
"keep");
 
  153             obj->Write( key->GetName() );
 
  159    target->SaveSelf(kTRUE);
 
  160    TH1::AddDirectory(status);