00001
00002
00003
00004
00005
00006
00007 #include <fstream>
00008
00009 #include "StMuException.hh"
00010 #include "StMuDebug.h"
00011 #include "StMuChainMaker.h"
00012 #include "StMuDbReader.h"
00013 #include "StMuTimer.h"
00014
00015 #include "StMaker.h"
00016 #include "StChain.h"
00017
00018 #include "TFile.h"
00019 #include "TTree.h"
00020 #include "TChain.h"
00021 #include "TSystem.h"
00022 #include "StMessMgr.h"
00023
00024 extern TSystem* gSystem;
00025
00026 string StMuChainMaker::mSQLConnection ="";
00027
00028 ClassImp(StMuChainMaker)
00029
00030
00031
00035 StMuChainMaker::StMuChainMaker(const char* name) : mTreeName(name) {
00036 DEBUGMESSAGE2("");
00037 mChain = new TChain(mTreeName.c_str());
00038 mChain->SetDirectory(0);
00039 mDbReader = StMuDbReader::instance();
00040 mFileCounter=0;
00041
00042 }
00043
00044
00045
00050 StMuChainMaker::~StMuChainMaker() {
00051 DEBUGMESSAGE2("");
00052
00053 }
00054
00055
00056
00061 string StMuChainMaker::buildFileName(string dir, string fileName, string extention){
00062 DEBUGMESSAGE2("");
00063 fileName = dir + fileName + extention;
00064 return fileName;
00065 }
00066
00067
00068
00073 string StMuChainMaker::basename(string s){
00074 string name(s);
00075 size_t pos;
00076 pos = name.find_last_of("/");
00077 if (pos!=string::npos ) name.erase(0, pos+1 );
00078 pos = name.find_first_of(".");
00079 if (pos!=string::npos ) name.erase(pos,name.length() );
00080 DEBUGVALUE2(name);
00081 return name;
00082 }
00083
00084
00085
00091 string StMuChainMaker::dirname(string s){
00092 string name(s);
00093 string base(basename(s));
00094 name.erase(name.find(base),base.length());
00095 size_t pos;
00096 pos = name.find_last_of("/");
00097 if (pos!=string::npos ) name.erase(pos, name.length());
00098 if (name=="/") name = "";
00099 DEBUGVALUE2(name);
00100 return name;
00101 }
00102
00103
00104
00126 TChain* StMuChainMaker::make(string dir, string file, string filter, int maxFiles) {
00127 DEBUGMESSAGE1("");
00128 mMaxFiles = maxFiles;
00129 subFilter(filter);
00130
00131 string dirFile = dir+file;
00132 DEBUGVALUE1(dir.c_str());
00133 DEBUGVALUE1(file.c_str());
00134 DEBUGVALUE1(dirFile.c_str());
00135
00136 if (dirFile.find(".lis")!=string::npos) fromList(dirFile);
00137 else if (dirFile.find(".files")!=string::npos) fromList(dirFile);
00138 else if (dirFile.find(".MuDst.root")!=string::npos) fromFile(dirFile);
00139 else if (dirFile.rfind("/") == dirFile.length()-1 ) fromDir(dirFile);
00140 else {
00141 FORCEDDEBUGMESSAGE("ATTENTION: don't know how to read input (you may have used a bogus constructor syntax)");
00142 return NULL;
00143 }
00144
00145 if ( mFileList.size() == 0 ) {
00146 DEBUGMESSAGE("No files found");
00147 return 0;
00148 }
00149
00150 add( mFileList );
00151
00152 DEBUGMESSAGE2("return");
00153 return mChain;
00154 }
00155
00156
00157
00158 void StMuChainMaker::subFilter(string filter) {
00159 DEBUGMESSAGE2("");
00160 string tmp(filter);
00161 int n=0;
00162 size_t pos=0;
00163 while ( tmp.length() ) {
00164 DEBUGMESSAGE3("");
00165 DEBUGVALUE3(tmp);
00166 DEBUGVALUE3(string::npos);
00167 pos = tmp.find_first_of(":");
00168 if ( pos==string::npos ) pos = tmp.length();
00169 mSubFilters[n] = string( tmp.substr(0,pos) );
00170 DEBUGVALUE3(mSubFilters[n]);
00171 tmp.erase(0,pos+1);
00172 DEBUGVALUE3(tmp);
00173 n++;
00174 }
00175 mSubFilters[n] = string("endOfFilters");
00176 DEBUGMESSAGE2("return");
00177 }
00178
00179
00180
00181 void StMuChainMaker::add( StMuStringIntPairVector fileList ) {
00182 DEBUGMESSAGE3("");
00183
00184
00185 StMuStringIntPairVectorIterator iter;
00186 for ( iter=fileList.begin(); iter!=fileList.end(); iter++) {
00187 if (mFileCounter>=mMaxFiles) break;
00188 add( *iter );
00189 }
00190 }
00191
00192
00193
00194
00196 void StMuChainMaker::add( StMuStringIntPair filenameEvents) {
00197 string file = filenameEvents.first;
00198 int entries = filenameEvents.second;
00199
00200 string rootdTag = "root://";
00201
00202
00203 if ( file.find(rootdTag)==0 ) {
00204
00205 string machine(gSystem->HostName());
00206
00207
00208
00209
00210
00211
00212 int pos = file.find("//",rootdTag.length());
00213 string node = file.substr(rootdTag.length(),pos-rootdTag.length());
00214
00215 if ( node == machine ) {
00216 LOG_INFO << " filename changed from " << file.c_str();
00217 file.erase(0,pos+1);
00218 LOG_INFO << " to : " << file.c_str() << endm;
00219 }
00220 }
00221
00222 if (entries==0 || entries==TChain::kBigNumber) {
00223 int tmp_entries = mDbReader->entries(file.c_str());
00224 if (tmp_entries != 0)
00225 entries = tmp_entries;
00226 else
00227 entries = TChain::kBigNumber;
00228 }
00229
00230
00231
00232 mChain->Add( file.c_str(), entries );
00233 mFileCounter++;
00234 }
00235
00236
00237
00238 void StMuChainMaker::fromDir(string dir) {
00239 DEBUGMESSAGE2("");
00240 DEBUGVALUE2(gSystem);
00241
00242 void *pDir = gSystem->OpenDirectory(dir.c_str());
00243
00244 const char* fileName(0);
00245 while((fileName = gSystem->GetDirEntry(pDir))){
00246 bool good = true;
00247 string name(fileName);
00248 if( strcmp(fileName,".")==0 || strcmp(fileName,"..")==0) good=false;
00249 if( strcmp(fileName,".root")==0 || strcmp(fileName,"..")==0) good=false;
00250 if ( name.find(".MuDst.root")==string::npos ) good=false;
00251 if ( good && pass(name,mSubFilters) ) {
00252 char* fullFile = gSystem->ConcatFileName(dir.c_str(),fileName);
00253
00254 mFileList.push_back( StMuStringIntPair( fullFile, TChain::kBigNumber ) );
00255 delete []fullFile;
00256 }
00257 }
00258 }
00259
00260
00261
00262 #include "TSQLServer.h"
00263 #include "TSQLResult.h"
00264 #include "TSQLRow.h"
00265 void StMuChainMaker::fromFileCatalog(string list) {
00266 DEBUGMESSAGE2("");
00267 TSQLServer* server = TSQLServer::Connect(mSQLConnection.c_str(),"","");
00268 if ( !server ) DEBUGMESSAGE("could not connect to server");
00269
00271 string machine(gSystem->Getenv("HOSTNAME"));
00272
00273
00275 string files("(filename='dummy')");
00276 ifstream in(list.c_str());
00277 if (!in) {
00278 DEBUGMESSAGE("can not open file");
00279 DEBUGVALUE(list);
00280 return;
00281 }
00282 char line[512];
00283 while ( in.getline(line,511) ) {
00284 string full(line);
00285 int split = full.rfind("/");
00286 string name = full.substr(split+1);
00287 string path = full.substr(0,split);
00288 DEBUGVALUE(path);
00289 DEBUGVALUE(name);
00290 if (path.find("/star/data")==0) {
00291 files += " || (filePath='" + path + "'&&fileName='" + name +"')";
00292 }
00293 else {
00294 files += " || (filePath='" + path + "'&&fileName='" + name + "'&&nodeName='" + machine + "')";
00295 }
00296 }
00297 in.close();
00298
00299 DEBUGVALUE(files);
00300
00301
00302 StMuTimer timer;
00303 timer.stop();
00304 timer.reset();
00305 string query = "SELECT filePath,fileName,numEntries FROM FileData LEFT JOIN FileLocations USING (fileDataId) WHERE " + files;
00306 DEBUGVALUE2(query.c_str());
00307 TSQLResult* result = server->Query(query.c_str());
00308 timer.stop();
00309
00310 DEBUGVALUE(timer.elapsedTime());
00311 DEBUGVALUE(result->GetRowCount());
00312
00313
00314 TSQLRow* row;
00315 string file;
00316 int entries;
00317 while ( (row=result->Next()) ) {
00318 file = row->GetField(0);
00319 file += +"/";
00320 file += row->GetField(1);
00321 entries = atoi(row->GetField(2));
00322 mFileList.push_back( StMuStringIntPair(file, entries ));
00323 }
00324
00325 server->Close();
00326 delete server;
00327 DEBUGVALUE2(mFileCounter);
00328 }
00329
00330
00331
00332 void StMuChainMaker::fromList(string list) {
00333 DEBUGMESSAGE2("");
00334 ifstream inputStream(list.c_str());
00335 if (!(inputStream.good())) {
00336 LOG_ERROR << "ERROR: Cannot open list file " << list << endm;
00337 }
00338 char line[512];
00339 char name[500];
00340 string ltest;
00341 DEBUGVALUE(inputStream.good());
00342 while (inputStream.good()) {
00343 inputStream.getline(line,512);
00344 string ltest(line);
00345 if (inputStream.good()) {
00346 int numberOfEvents = TChain::kBigNumber;
00347 int iret = sscanf(line,"%s%i",name, &numberOfEvents);
00348 if(iret) {}
00349 if ( pass(name,mSubFilters) && ltest!="") {
00350 mFileList.push_back( StMuStringIntPair( name, numberOfEvents) );
00351 }
00352 }
00353 }
00354 }
00355
00356
00357
00358 void StMuChainMaker::fromFile(string file) {
00359 DEBUGMESSAGE2("");
00360 DEBUGMESSAGE2(mTreeName.c_str());
00361 mFileList.push_back( StMuStringIntPair( file, TChain::kBigNumber ) );
00362 }
00363
00364
00365
00366 bool StMuChainMaker::pass(string file, string* filters) {
00367 bool good = true;
00368 int n=0;
00369 while (filters[n].find("endOfFilters")==string::npos && good) {
00370 if ( StMuDebug::level()==3 ) printf("%s %s %d ",file.c_str(),filters[n].c_str(), file.find(filters[n])==string::npos);
00371 if ( (file.find(filters[n])==string::npos) ) good=false;
00372 if ( StMuDebug::level()==3 ) printf("good= %d \n",good);
00373 n++;
00374 }
00375 DEBUGVALUE3(good);
00376 return good;
00377 }
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512