00001
00002
00003
00004
00005
00006
00007 #include <fstream>
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010
00011 #include "StMuException.hh"
00012 #include "StMuDebug.h"
00013 #include "StMuDbReader.h"
00014
00015 #include "StMaker.h"
00016 #include "StChain.h"
00017
00018 ClassImp(StMuDbReader)
00019
00020 StMuDbReader* StMuDbReader::_instance=0;
00021
00022
00023
00024
00025 StMuDbReader* StMuDbReader::instance() {
00026 DEBUGMESSAGE2("");
00027 if (_instance==0) _instance = new StMuDbReader();
00028 return _instance;
00029 }
00030
00031
00032
00033 StMuDbReader* StMuDbReader::Instance() {
00034 DEBUGMESSAGE2("");
00035 return instance();
00036 }
00037
00038
00039
00041 StMuDbReader:: StMuDbReader() {
00042 DEBUGMESSAGE2("");
00043 }
00044
00045
00046
00047 StMuDbReader::~StMuDbReader() {
00048 DEBUGMESSAGE2("");
00049 }
00050
00051
00052
00054 int StMuDbReader::addDb(const char* fileName) {
00055 DEBUGMESSAGE2("");
00056 char name[256];
00057 int numberOfEvents;
00058 char line[512];
00059
00060 ifstream in(fileName);
00061 if (!in) {
00062 DEBUGMESSAGE2("can not open file");
00063 DEBUGVALUE2(fileName);
00064 return mDb.size();
00065 }
00066 while ( in ) {
00067 in.getline(line,511);
00068 DEBUGVALUE3(line);
00069 int iret = sscanf(line,"%s%i",name, &numberOfEvents);
00070 if (iret==2) {
00071 pair<string,int> aPair(name,numberOfEvents);
00072 mDb.push_back( aPair );
00073 }
00074 }
00075 in.close();
00076 sortDb();
00077 return mDb.size();
00078 }
00079
00080
00081
00083 void StMuDbReader::showDb(){
00084 DEBUGMESSAGE2("");
00085 for (iter=mDb.begin(); iter!=mDb.end(); iter++) {
00086 cout << (*iter).first.c_str() << endl;
00087 }
00088 }
00089
00090
00091
00093 void StMuDbReader::sortDb(){
00094 DEBUGMESSAGE2("");
00095 list< pair<string,int> > tmpList;
00096 list< pair<string,int> >::iterator tmpIter;
00097 for (iter=mDb.begin(); iter!=mDb.end(); iter++) {
00098 tmpList.push_back( *iter );
00099 }
00100 tmpList.sort();
00101
00102 mDb.clear();
00103 for (tmpIter=tmpList.begin(); tmpIter!=tmpList.end(); tmpIter++) {
00104 mDb.push_back( *tmpIter );
00105 }
00106
00107 }
00108
00109
00110
00112 int StMuDbReader::entries(const char* file){
00113 string fileName(file);
00114 int lo=0;
00115 int hi=mDb.size();
00116 int pos=0;
00117 int oldPos=0;
00118 int entries=0;
00119
00120 while (lo<hi) {
00121 pos = (int) (lo+hi)/2;
00122
00123 if (oldPos==pos) break;
00124 if (fileName > mDb[pos].first) lo=pos;
00125 if (fileName < mDb[pos].first) hi=pos;
00126 if (fileName == mDb[pos].first) {
00127 entries = mDb[pos].second;
00128 lo=hi;
00129 }
00130 oldPos=pos;
00131 }
00132 return entries;
00133 }
00134
00135
00136
00137 #include "TFile.h"
00138 #include "TTree.h"
00140 int StMuDbReader::createDB(const char* dbName, const char* inputList) {
00141 DEBUGMESSAGE("");
00142
00144 addDb(dbName);
00145
00146
00147 ofstream dbFile(dbName, ios::app);
00148
00149
00150
00151 ifstream in(inputList);
00152 if (!in || !dbFile) {
00153 DEBUGVALUE2("could not open file");
00154 return 0;
00155 }
00156
00157 int count=0;
00158 char line[256];
00159 char fileName[256];
00160
00161 while ( in ) {
00162 in.getline(line,255);
00163 int iret = sscanf(line,"%s",fileName);
00164
00165 if (iret==1) {
00166 cout << fileName << " ";
00167
00168 if (entries(fileName)==0) {
00169 TFile f1(fileName);
00170 TTree *tree = dynamic_cast<TTree*>(f1.Get("MuDst"));
00171 if (tree) {
00172 Stat_t nentries = tree->GetEntries();
00173 dbFile << fileName << " " << nentries << endl;
00174 cout << " added with " << nentries << " entries " << endl;
00175 count++;
00176 }
00177 f1.Close();
00178 } else {
00179 cout << " already in data base " << endl;
00180 }
00181 }
00182 cout << endl;
00183 }
00184 in.close();
00185 dbFile.close();
00186 return count;
00187 }
00188
00190 inline int StMuDbReader::entriesDb() { return mDb.size(); }
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