00001 #include <string.h>
00002 #include "TChain.h"
00003 #include "TFile.h"
00004 #include "TH1.h"
00005 #include "TTree.h"
00006 #include "TKey.h"
00007 #include "Riostream.h"
00008 #include "TFolder.h"
00009 TList *FileList;
00010 TFile *Target;
00011 void MergeRootfile( TDirectory *target, TList *sourcelist );
00012
00013 int main(int argc, char* argv[]) {
00014
00015
00016 std::cout<<"argv[0]:"<<argv[0]<<std::endl;
00017 std::cout<<"argv[1]:"<<argv[1]<<std::endl;
00018 std::cout<<"argv[2]:"<<argv[2]<<std::endl;
00019 std::cout<<"argv[3]:"<<argv[3]<<std::endl;
00020 Target = TFile::Open(argv[1], "RECREATE" );
00021 FileList = new TList();
00022
00023 for(int i=0;i<10000;i++){
00024 std::cout<<"argv[i+2]=="<<argv[i+2]<<std::endl;
00025 if(string(argv[i+2])=="end") break;
00026 FileList->Add( TFile::Open(argv[i+2]) );
00027 }
00028
00029 MergeRootfile( Target, FileList );
00030
00031 }
00032 void MergeRootfile( TDirectory *target, TList *sourcelist ) {
00033
00034
00035 TString path( (char*)strstr( target->GetPath(), ":" ) );
00036 path.Remove( 0, 2 );
00037
00038 TFile *first_source = (TFile*)sourcelist->First();
00039 first_source->cd( path );
00040 TDirectory *current_sourcedir = gDirectory;
00041
00042 Bool_t status = TH1::AddDirectoryStatus();
00043 TH1::AddDirectory(kFALSE);
00044
00045
00046 TChain *globChain = 0;
00047 TIter nextkey( current_sourcedir->GetListOfKeys() );
00048 TKey *key, *oldkey=0;
00049 while ( (key = (TKey*)nextkey())) {
00050
00051
00052 if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) continue;
00053
00054
00055 first_source->cd( path );
00056 TObject *obj = key->ReadObj();
00057
00058 TFolder* add = dynamic_cast<TFolder*>(obj);
00059 TCollection *hh = add->GetListOfFolders();
00060 TFolder* add1=new TFolder(key->GetName(), key->GetTitle());
00061
00062
00063 TFile *nextsource = (TFile*)sourcelist->After( first_source );
00064 while ( nextsource ) {
00065 TIter next(hh);
00066 nextsource->cd( path );
00067 std::cout<<"nextsource->Name()=="<<std::endl;
00068 nextsource->Print();
00069 std::cout<<"firstsource->Name()=="<<std::endl;
00070 first_source->Print();
00071 TFolder* kk=(TFolder *)nextsource->Get(key->GetName());
00072 std::cout<<"key->GetName()=="<<key->GetName()<<std::endl;
00073 first_source->cd( path );
00074 while ((obj = next())){
00075 TH1 *h1 = (TH1*)obj;
00076 std::cout<<"obj->getName()=="<<obj->GetName()<<std::endl;
00077 add1->Add(h1);
00078 nextsource->cd( path );
00079 TH1 *h2 = (TH1*)kk->FindObjectAny(h1->GetName());
00080 if(h2){
00081 h1->Add( h2 );
00082 std::cout<<"h1->GetEntries()"<<h1->GetEntries()<<std::endl;
00083 }
00084 }
00085 nextsource = (TFile*)sourcelist->After( nextsource );
00086 }
00087 target->cd();
00088
00089 add1->Write();
00090 delete add1;
00091 delete hh;
00092 }
00093 target->SaveSelf(kTRUE);
00094 }
00095