00001
00002
00003
00004
00005 #include <string.h>
00006 #include "TChain.h"
00007 #include "TFile.h"
00008 #include "TH1.h"
00009 #include "TTree.h"
00010 #include "TKey.h"
00011 #include "Riostream.h"
00012
00013 TList *FileList;
00014 TFile *Target;
00015
00016
00017 int isAPS2010pol = 0;
00018
00019
00020
00021
00022 void MergeRootfile( TDirectory *target, TList *sourcelist );
00023
00024
00025 void haddWana(char *set = "sumFeb21_2011", TString iPath = "./")
00026 {
00027 TString out = iPath;
00028
00029 if (strstr("sumFeb21_2011", set) > 0) {
00030 char *runL = " cutsOnly_Feb21 raw_sumFeb21";
00031 }
00032 else if (strstr("setXX", set) > 0) {
00033 char *runL = "HHHHH ";
00034 }
00035 else {
00036 printf(" hadd: set=%s= NOT found, quit\n", set);
00037 return;
00038 }
00039 printf(" hadd: set=%s= path=%s= ...\n", set, iPath.Data());
00040 out += set;
00041 Target = TFile::Open( out + ".wana.hist.root", "RECREATE" );
00042 FileList = new TList();
00043 printf("sum Output '%s' \n", Target->GetName());
00044
00045 char *run = strtok(runL, " ");
00046 int i = 1;
00047 do {
00048 printf("add run %d '%s' \n", i++, run);
00049 if (isAPS2010pol && strstr(set, "run9setP") > 0 ) {
00050 int irun = atoi(run + 1);
00051 if (irun >= 10083013 && irun <= 10083058) {
00052 printf("\tdrop %s from F10415\n", run);
00053 continue;
00054 }
00055 if (irun >= 10098015 && irun <= 10098015) {
00056 printf("\tdrop %s from F10508\n", run);
00057 continue;
00058 }
00059 }
00060 TString fullName = iPath + run + ".wana.hist.root";
00061 FileList->Add( TFile::Open(fullName));
00062 }
00063 while (run = strtok(0, " "));
00064
00065 MergeRootfile( Target, FileList );
00066 printf("finished Output '%s' \n", Target->GetName());
00067
00068 }
00069
00070 void MergeRootfile( TDirectory *target, TList *sourcelist )
00071 {
00072
00073 cout << "Target path: " << target->GetPath() << endl;
00074 TString path( (char*)strstr( target->GetPath(), ":" ) );
00075 path.Remove( 0, 2 );
00076
00077 TFile *first_source = (TFile*)sourcelist->First();
00078 first_source->cd( path );
00079 TDirectory *current_sourcedir = gDirectory;
00080
00081 int nh = 0;
00082
00083 TChain *globChain = 0;
00084 TIter nextkey( current_sourcedir->GetListOfKeys() );
00085 TKey *key;
00086 while ( (key = (TKey*)nextkey())) {
00087 const char *name = key->GetName();
00088 nh++;
00089 if (nh % 100 == 0) printf("nh=%d addingX %s\n", nh, name);
00090
00091
00092 first_source->cd( path );
00093 TObject *obj = key->ReadObj();
00094
00095 if ( obj->IsA()->InheritsFrom( "TH1" ) ) {
00096
00097
00098
00099 TH1 *h1 = (TH1*)obj;
00100
00101
00102
00103 TFile *nextsource = (TFile*)sourcelist->After( first_source );
00104 while ( nextsource ) {
00105
00106
00107 nextsource->cd( path );
00108 TH1 *h2 = (TH1*)gDirectory->Get( h1->GetName() );
00109 if ( h2 ) {
00110 h1->Add( h2 );
00111 delete h2;
00112
00113 }
00114
00115 nextsource = (TFile*)sourcelist->After( nextsource );
00116 }
00117 }
00118 else if ( obj->IsA()->InheritsFrom( "TTree" ) ) {
00119
00120
00121 const char* obj_name = obj->GetName();
00122
00123 globChain = new TChain(obj_name);
00124 globChain->Add(first_source->GetName());
00125 TFile *nextsource = (TFile*)sourcelist->After( first_source );
00126
00127
00128 while ( nextsource ) {
00129
00130 globChain->Add(nextsource->GetName());
00131 nextsource = (TFile*)sourcelist->After( nextsource );
00132 }
00133
00134 }
00135 else if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) {
00136
00137
00138 cout << "Found subdirectory " << obj->GetName() << endl;
00139
00140
00141 target->cd();
00142 TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() );
00143
00144
00145
00146
00147 MergeRootfile( core, newdir, sourcelist );
00148
00149 }
00150 else {
00151
00152
00153 cout << "Unknown object type, name: "
00154 << obj->GetName() << " title: " << obj->GetTitle() << endl;
00155 }
00156
00157
00158
00159
00160
00161 if ( obj ) {
00162 target->cd();
00163
00165 if (obj->IsA()->InheritsFrom( "TTree" ))
00166 globChain->Write( key->GetName() );
00167 else
00168 obj->Write( key->GetName() );
00169 }
00170
00171 }
00172
00173
00174 target->Write();
00175
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265