17 #include "Riostream.h"
26 void MergeFiles(
const Int_t nFiles=100,
const TString filelist=
"file.list",
const TString outputrootfiles =
"test.root") {
39 TString filename_output;
44 Target = TFile::Open(outputrootfiles,
"RECREATE");
45 FileList =
new TList();
51 if ((line + 1) > nFiles) {
52 cout <<
"WARNING: Whoah there, tiger! Only need " << nFiles <<
" files!\n"
53 <<
" Moving on to processing files."
65 Bool_t fOk = f.IsZombie();
66 cout<<
"file# "<<line<<
" "<< file_stm<<
" zombi= "<<fOk<<endl;
70 FileList->Add(TFile::Open(file_stm));
75 if (!files.good())
break;
88 TString
path( (
char*)strstr( target->GetPath(),
":" ) );
91 TFile *first_source = (TFile*)sourcelist->First();
92 first_source->cd(
path );
93 TDirectory *current_sourcedir = gDirectory;
95 Bool_t
status = TH1::AddDirectoryStatus();
96 TH1::AddDirectory(kFALSE);
99 TChain *globChain = 0;
100 TIter nextkey( current_sourcedir->GetListOfKeys() );
101 TKey *key, *oldkey=0;
102 while ( (key = (TKey*)nextkey())) {
105 if (oldkey && !strcmp(oldkey->GetName(),key->GetName()))
continue;
108 first_source->cd(
path );
109 TObject *obj = key->ReadObj();
111 if ( obj->IsA()->InheritsFrom( TH1::Class() ) ) {
119 TFile *nextsource = (TFile*)sourcelist->After( first_source );
120 while ( nextsource ) {
123 nextsource->cd(
path );
124 TKey *key2 = (TKey*)gDirectory->GetListOfKeys()->FindObject(h1->GetName());
126 TH1 *
h2 = (TH1*)key2->ReadObj();
131 nextsource = (TFile*)sourcelist->After( nextsource );
134 else if ( obj->IsA()->InheritsFrom( TTree::Class() ) ) {
137 const char* obj_name= obj->GetName();
139 globChain =
new TChain(obj_name);
140 globChain->Add(first_source->GetName());
141 TFile *nextsource = (TFile*)sourcelist->After( first_source );
144 while ( nextsource ) {
146 globChain->Add(nextsource->GetName());
147 nextsource = (TFile*)sourcelist->After( nextsource );
150 }
else if ( obj->IsA()->InheritsFrom( TDirectory::Class() ) ) {
153 cout <<
"Found subdirectory " << obj->GetName() << endl;
157 TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() );
167 cout <<
"Unknown object type, name: "
168 << obj->GetName() <<
" title: " << obj->GetTitle() << endl;
179 if(obj->IsA()->InheritsFrom( TTree::Class() ))
180 globChain->Merge(target->GetFile(),0,
"keep");
182 obj->Write( key->GetName() );
188 target->SaveSelf(kTRUE);
189 TH1::AddDirectory(status);