15 #include "Riostream.h"
24 void MergeFiles(
const Int_t nFiles=100,
const TString filelist=
"file.list",
const TString outputrootfiles =
"test.root") {
37 TString filename_output;
42 Target = TFile::Open(outputrootfiles,
"RECREATE");
43 FileList =
new TList();
49 if ((line + 1) > nFiles) {
50 cout <<
"WARNING: Whoah there, tiger! Only need " << nFiles <<
" files!\n"
51 <<
" Moving on to processing files."
63 Bool_t fOk = f.IsZombie();
64 cout<<
"file# "<<line<<
" "<< file_stm<<
" zombi= "<<fOk<<endl;
68 FileList->Add(TFile::Open(file_stm));
73 if (!files.good())
break;
86 TString
path( (
char*)strstr( target->GetPath(),
":" ) );
89 TFile *first_source = (TFile*)sourcelist->First();
90 first_source->cd(
path );
91 TDirectory *current_sourcedir = gDirectory;
93 Bool_t
status = TH1::AddDirectoryStatus();
94 TH1::AddDirectory(kFALSE);
97 TChain *globChain = 0;
98 TIter nextkey( current_sourcedir->GetListOfKeys() );
100 while ( (key = (TKey*)nextkey())) {
103 if (oldkey && !strcmp(oldkey->GetName(),key->GetName()))
continue;
106 first_source->cd(
path );
107 TObject *obj = key->ReadObj();
109 if ( obj->IsA()->InheritsFrom( TH1::Class() ) ) {
117 TFile *nextsource = (TFile*)sourcelist->After( first_source );
118 while ( nextsource ) {
121 nextsource->cd(
path );
122 TKey *key2 = (TKey*)gDirectory->GetListOfKeys()->FindObject(h1->GetName());
124 TH1 *
h2 = (TH1*)key2->ReadObj();
129 nextsource = (TFile*)sourcelist->After( nextsource );
132 else if ( obj->IsA()->InheritsFrom( TTree::Class() ) ) {
135 const char* obj_name= obj->GetName();
137 globChain =
new TChain(obj_name);
138 globChain->Add(first_source->GetName());
139 TFile *nextsource = (TFile*)sourcelist->After( first_source );
142 while ( nextsource ) {
144 globChain->Add(nextsource->GetName());
145 nextsource = (TFile*)sourcelist->After( nextsource );
148 }
else if ( obj->IsA()->InheritsFrom( TDirectory::Class() ) ) {
151 cout <<
"Found subdirectory " << obj->GetName() << endl;
155 TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() );
165 cout <<
"Unknown object type, name: "
166 << obj->GetName() <<
" title: " << obj->GetTitle() << endl;
177 if(obj->IsA()->InheritsFrom( TTree::Class() ))
178 globChain->Merge(target->GetFile(),0,
"keep");
180 obj->Write( key->GetName() );
186 target->SaveSelf(kTRUE);
187 TH1::AddDirectory(status);