00001
00002 #include "IO.h"
00003 #include "TChain.h"
00004 #include "TFile.h"
00005 #include "TTree.h"
00006 #include "TSystem.h"
00007
00008 #include "Stiostream.h"
00009 #include <fstream>
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012
00013 ClassImp(IO)
00014
00015 IO::IO(const char* dir, const char* match, const char* ext)
00016 : mNFile(0), mDir(dir), mMatch(match), mExt(ext) {
00017
00018 }
00019
00020 IO::~IO() {}
00021
00022 void IO::chain(TChain* chain)
00023 {
00024
00025 void *pDir = gSystem->OpenDirectory(mDir.Data());
00026 if(!pDir){
00027 cerr << "##Cannot open directory " << mDir.Data() << endl;
00028 cerr << "##Goodbye" << endl;
00029 exit(1);
00030 }
00031
00032 cout << "\tUsing directory : " << mDir.Data() << endl;
00033 cout << "\tMatch string : " << mMatch.Data() << endl;
00034 cout << "\tMatch extension : " << mExt.Data() << endl;
00035 if(mNFile) cout << "\tMaximum # files : " << mNFile << endl;
00036
00037
00038
00039
00040
00041 const char* fileName(0);
00042 Int_t count(0);
00043
00044 while((fileName = gSystem->GetDirEntry(pDir))){
00045 if(strcmp(fileName,".")==0 || strcmp(fileName,"..")==0) continue;
00046
00047 if(strstr(fileName,mExt.Data()) && strstr(fileName,mMatch.Data())){
00048 char* fullFile = gSystem->ConcatFileName(mDir.Data(),fileName);
00049
00050
00051
00052
00053
00054
00055 count++;
00056
00057 int events = 0;
00058
00059
00060
00061
00062
00063 if (events==0) {
00064 TFile *f1 = new TFile(fullFile);
00065 TTree *tree = (TTree*)f1->Get("StHiMicroTree");
00066 if (tree) events = (int)tree->GetEntries();
00067 delete f1;
00068 }
00069
00070 if (events) {
00071 chain->Add(fullFile,events);
00072 if (count%100==0) cout << "File # " << count << endl << "\tAdding " <<
00073 fullFile << " with " << events << " events to the chain" << endl;
00074 delete fullFile;
00075 }
00076
00077 if(mNFile && count > mNFile) break;
00078
00079 }
00080 }
00081 cout << "Added " << count << " files to the chain" << endl;
00082
00083 }
00084
00085 void IO::createDb(const char *dbName)
00086 {
00087
00088
00089 ofstream dbFile(dbName, ios::app);
00090
00091 void *pDir = gSystem->OpenDirectory(mDir.Data());
00092 if(!pDir){
00093 cerr << "##Cannot open directory " << mDir.Data() << endl;
00094 cerr << "##Goodbye" << endl;
00095 exit(1);
00096 }
00097
00098 cout << "\tUsing directory : " << mDir.Data() << endl;
00099 cout << "\tMatch string : " << mMatch.Data() << endl;
00100 cout << "\tMatch extension : " << mExt.Data() << endl;
00101
00102
00103
00104
00105 const char* fileName(0);
00106 Int_t count(0);
00107
00108 while((fileName = gSystem->GetDirEntry(pDir))){
00109 if(strcmp(fileName,".")==0 || strcmp(fileName,"..")==0) continue;
00110
00111 if(strstr(fileName,mExt.Data()) && strstr(fileName,mMatch.Data())){
00112 char* fullFile = gSystem->ConcatFileName(mDir.Data(),fileName);
00113
00114 count++;
00115 int events = 0;
00116
00117 if (events==0) {
00118 TFile *f1 = new TFile(fullFile);
00119 TTree *tree = (TTree*)f1->Get("StHiMicroTree");
00120 if (tree) events = (int)tree->GetEntries();
00121 delete f1;
00122 }
00123 if (events) {
00124 dbFile << fullFile << " " << events << endl;
00125 cout << "\tAdding " << fullFile << " with " << events << " events to the Database" << endl;
00126 delete fullFile;
00127 }
00128
00129 }
00130 }
00131 dbFile.close();
00132
00133 }
00134
00135
00136
00137
00140
00141
00142
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
00169
00170
00171
00172
00173
00174
00175
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
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
00224
00225
00226
00227
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
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276