9 #include "StMuException.hh"
10 #include "StMuDebug.h"
11 #include "StMuChainMaker.h"
12 #include "StMuDbReader.h"
13 #include "StMuTimer.h"
22 #include "StMessMgr.h"
24 extern TSystem* gSystem;
26 string StMuChainMaker::mSQLConnection =
"";
37 mChain =
new TChain(mTreeName.c_str());
38 mChain->SetDirectory(0);
39 mDbReader = StMuDbReader::instance();
63 fileName = dir + fileName + extention;
76 pos = name.find_last_of(
"/");
77 if (pos!=string::npos ) name.erase(0, pos+1 );
78 pos = name.find_first_of(
".");
79 if (pos!=string::npos ) name.erase(pos,name.length() );
94 name.erase(name.find(base),base.length());
96 pos = name.find_last_of(
"/");
97 if (pos!=string::npos ) name.erase(pos, name.length());
98 if (name==
"/") name =
"";
128 mMaxFiles = maxFiles;
131 string dirFile = dir+file;
132 DEBUGVALUE1(dir.c_str());
133 DEBUGVALUE1(file.c_str());
134 DEBUGVALUE1(dirFile.c_str());
136 if (dirFile.find(
".lis")!=string::npos) fromList(dirFile);
137 else if (dirFile.find(
".files")!=string::npos) fromList(dirFile);
138 else if (dirFile.find(
".MuDst.root")!=string::npos) fromFile(dirFile);
139 else if (dirFile.rfind(
"/") == dirFile.length()-1 ) fromDir(dirFile);
141 FORCEDDEBUGMESSAGE(
"ATTENTION: don't know how to read input (you may have used a bogus constructor syntax)");
145 if ( mFileList.size() == 0 ) {
146 DEBUGMESSAGE(
"No files found");
152 DEBUGMESSAGE2(
"return");
158 void StMuChainMaker::subFilter(
string filter) {
163 while ( tmp.length() ) {
166 DEBUGVALUE3(string::npos);
167 pos = tmp.find_first_of(
":");
168 if ( pos==string::npos ) pos = tmp.length();
169 mSubFilters[n] = string( tmp.substr(0,pos) );
170 DEBUGVALUE3(mSubFilters[n]);
175 mSubFilters[n] = string(
"endOfFilters");
176 DEBUGMESSAGE2(
"return");
181 void StMuChainMaker::add( StMuStringLongPairVector fileList ) {
185 StMuStringLongPairVectorIterator iter;
186 for ( iter=fileList.begin(); iter!=fileList.end(); iter++) {
187 if (mFileCounter>=mMaxFiles)
break;
196 void StMuChainMaker::add( StMuStringLongPair filenameEvents) {
197 string file = filenameEvents.first;
198 int entries = filenameEvents.second;
200 string rootdTag =
"root://";
203 if ( file.find(rootdTag)==0 ) {
205 string machine(gSystem->HostName());
212 int pos = file.find(
"//",rootdTag.length());
213 string node = file.substr(rootdTag.length(),pos-rootdTag.length());
215 if ( node == machine ) {
216 LOG_INFO <<
" filename changed from " << file.c_str();
218 LOG_INFO <<
" to : " << file.c_str() << endm;
222 if (entries==0 || entries==TChain::kBigNumber) {
223 int tmp_entries = mDbReader->
entries(file.c_str());
224 if (tmp_entries != 0)
225 entries = tmp_entries;
227 entries = TChain::kBigNumber;
232 mChain->Add( file.c_str(), entries );
238 void StMuChainMaker::fromDir(
string dir) {
240 DEBUGVALUE2(gSystem);
242 void *pDir = gSystem->OpenDirectory(dir.c_str());
244 const char* fileName(0);
245 while((fileName = gSystem->GetDirEntry(pDir))){
247 string name(fileName);
248 if( strcmp(fileName,
".")==0 || strcmp(fileName,
"..")==0) good=
false;
249 if( strcmp(fileName,
".root")==0 || strcmp(fileName,
"..")==0) good=
false;
250 if ( name.find(
".MuDst.root")==string::npos ) good=
false;
251 if ( good && pass(name,mSubFilters) ) {
252 char* fullFile = gSystem->ConcatFileName(dir.c_str(),fileName);
254 const auto TChain_kBigNumber = TChain::kBigNumber;
255 mFileList.push_back( StMuStringLongPair( fullFile, TChain_kBigNumber ) );
263 #include "TSQLServer.h"
264 #include "TSQLResult.h"
268 TSQLServer* server = TSQLServer::Connect(mSQLConnection.c_str(),
"",
"");
269 if ( !server ) DEBUGMESSAGE(
"could not connect to server");
272 string machine(gSystem->Getenv(
"HOSTNAME"));
276 string files(
"(filename='dummy')");
277 ifstream in(list.c_str());
279 DEBUGMESSAGE(
"can not open file");
284 while ( in.getline(line,511) ) {
286 int split = full.rfind(
"/");
287 string name = full.substr(split+1);
288 string path = full.substr(0,split);
291 if (path.find(
"/star/data")==0) {
292 files +=
" || (filePath='" + path +
"'&&fileName='" + name +
"')";
295 files +=
" || (filePath='" + path +
"'&&fileName='" + name +
"'&&nodeName='" + machine +
"')";
306 string query =
"SELECT filePath,fileName,numEntries FROM FileData LEFT JOIN FileLocations USING (fileDataId) WHERE " + files;
307 DEBUGVALUE2(query.c_str());
308 TSQLResult* result = server->Query(query.c_str());
311 DEBUGVALUE(timer.elapsedTime());
312 DEBUGVALUE(result->GetRowCount());
318 while ( (row=result->Next()) ) {
319 file = row->GetField(0);
321 file += row->GetField(1);
322 entries = atoi(row->GetField(2));
323 mFileList.push_back( StMuStringLongPair(file, entries ));
328 DEBUGVALUE2(mFileCounter);
333 void StMuChainMaker::fromList(
string list) {
335 ifstream inputStream(list.c_str());
336 if (!(inputStream.good())) {
337 LOG_ERROR <<
"ERROR: Cannot open list file " << list << endm;
342 DEBUGVALUE(inputStream.good());
343 while (inputStream.good()) {
344 inputStream.getline(line,512);
346 if (inputStream.good()) {
347 Long_t numberOfEvents = TChain::kBigNumber;
348 int iret = sscanf(line,
"%s%i",name, &numberOfEvents);
350 if ( pass(name,mSubFilters) && ltest!=
"") {
351 mFileList.push_back( StMuStringLongPair( name, numberOfEvents) );
359 void StMuChainMaker::fromFile(
string file) {
361 DEBUGMESSAGE2(mTreeName.c_str());
362 const auto TChain_kBigNumber = TChain::kBigNumber;
363 mFileList.push_back( StMuStringLongPair( file, TChain_kBigNumber ) );
368 bool StMuChainMaker::pass(
string file,
string* filters) {
371 while (filters[n].find(
"endOfFilters")==string::npos && good) {
372 if (
StMuDebug::level()==3 ) printf(
"%s %s %d ",file.c_str(),filters[n].c_str(), file.find(filters[n])==string::npos);
373 if ( (file.find(filters[n])==string::npos) ) good=
false;
virtual ~StMuChainMaker()
void fromFileCatalog(string file)
int entries(const char *file)
scan internal data base for file, if found return number of entries, otherwise return 0; ...
TChain * make(string dir, string file, string filter, int maxFiles=10)
static int level()
returns debug level
string buildFileName(string dir, string fileName, string extention)