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