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
00009 TList *FileList;
00010 TFile *Target;
00011
00012 void MergeRootfile( TDirectory *target, TList *sourcelist );
00013
00014 void hadd(int flag=0) {
00015 TString iPath="out1/";
00016 TString out=iPath;
00017 out+="/sumP";
00018
00019 Target = TFile::Open( out+".hist.root", "RECREATE" );
00020
00021
00022
00023
00024
00025 char *runL="R7098001 R7098024 R7098038 R7098065 R7098082 R7099025 R7099047 R7100072 R7098002 R7098025 R7098039 R7098066 R7098083 R7099026 R7100052 R7100075 ";
00026
00027 FileList = new TList();
00028
00029 char *run=strtok(runL," ");
00030 int i=0;
00031 do {
00032 printf("add run %d '%s' \n",i++,run);
00033 TString fullName=iPath+run+".hist.root";
00034 FileList->Add( TFile::Open(fullName));
00035 } while(run=strtok(0," "));
00036
00037 MergeRootfile( Target, FileList );
00038
00039 }
00040
00041 void MergeRootfile( TDirectory *target, TList *sourcelist ) {
00042
00043
00044 TString path( (char*)strstr( target->GetPath(), ":" ) );
00045 path.Remove( 0, 2 );
00046
00047 TFile *first_source = (TFile*)sourcelist->First();
00048 first_source->cd( path );
00049 TDirectory *current_sourcedir = gDirectory;
00050
00051 int nh=0;
00052
00053 TChain *globChain = 0;
00054 TIter nextkey( current_sourcedir->GetListOfKeys() );
00055 TKey *key;
00056 while ( (key = (TKey*)nextkey())) {
00057 const char *name=key->GetName();
00058 nh++;
00059 if(nh%10==0) printf("nh=%d addingX %s\n",nh,name);
00060
00061
00062 first_source->cd( path );
00063 TObject *obj = key->ReadObj();
00064
00065 if ( obj->IsA()->InheritsFrom( "TH1" ) ) {
00066
00067
00068
00069 TH1 *h1 = (TH1*)obj;
00070
00071
00072
00073 TFile *nextsource = (TFile*)sourcelist->After( first_source );
00074 while ( nextsource ) {
00075
00076
00077 nextsource->cd( path );
00078 TH1 *h2 = (TH1*)gDirectory->Get( h1->GetName() );
00079 if ( h2 ) {
00080 h1->Add( h2 );
00081 delete h2;
00082
00083 }
00084
00085 nextsource = (TFile*)sourcelist->After( nextsource );
00086 }
00087 }
00088 else if ( obj->IsA()->InheritsFrom( "TTree" ) ) {
00089
00090
00091 const char* obj_name= obj->GetName();
00092
00093 globChain = new TChain(obj_name);
00094 globChain->Add(first_source->GetName());
00095 TFile *nextsource = (TFile*)sourcelist->After( first_source );
00096
00097
00098 while ( nextsource ) {
00099
00100 globChain->Add(nextsource->GetName());
00101 nextsource = (TFile*)sourcelist->After( nextsource );
00102 }
00103
00104 } else if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) {
00105
00106
00107 cout << "Found subdirectory " << obj->GetName() << endl;
00108
00109
00110 target->cd();
00111 TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() );
00112
00113
00114
00115
00116 MergeRootfile( core,newdir, sourcelist );
00117
00118 } else {
00119
00120
00121 cout << "Unknown object type, name: "
00122 << obj->GetName() << " title: " << obj->GetTitle() << endl;
00123 }
00124
00125
00126
00127
00128
00129 if ( obj ) {
00130 target->cd();
00131
00133 if(obj->IsA()->InheritsFrom( "TTree" ))
00134 globChain->Write( key->GetName() );
00135 else
00136 obj->Write( key->GetName() );
00137 }
00138
00139 }
00140
00141
00142 target->Write();
00143
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170