StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StMuDbReader.cxx
1 /***************************************************************************
2  *
3  * $Id: StMuDbReader.cxx,v 1.5 2016/05/04 19:08:27 smirnovd Exp $
4  * Author: Frank Laue, BNL, laue@bnl.gov
5  *
6  **************************************************************************/
7 #include <fstream>
8 #include <stdio.h>
9 #include <stdlib.h>
10 
11 #include "StMuException.hh"
12 #include "StMuDebug.h"
13 #include "StMuDbReader.h"
14 
15 #include "StMaker.h"
16 #include "StChain.h"
17 
18 ClassImp(StMuDbReader)
19 
20 StMuDbReader* StMuDbReader::_instance=0;
21 
22 //-----------------------------------------------------------------------
23 //-----------------------------------------------------------------------
24 //-----------------------------------------------------------------------
25 StMuDbReader* StMuDbReader::instance() {
26  DEBUGMESSAGE2("");
27  if (_instance==0) _instance = new StMuDbReader();
28  return _instance;
29 }
30 //-----------------------------------------------------------------------
31 //-----------------------------------------------------------------------
32 //-----------------------------------------------------------------------
33 StMuDbReader* StMuDbReader::Instance() {
34  DEBUGMESSAGE2("");
35  return instance();
36 }
37 //-----------------------------------------------------------------------
38 //-----------------------------------------------------------------------
39 //-----------------------------------------------------------------------
42  DEBUGMESSAGE2("");
43 }
44 //-----------------------------------------------------------------------
45 //-----------------------------------------------------------------------
46 //-----------------------------------------------------------------------
47 StMuDbReader::~StMuDbReader() {
48  DEBUGMESSAGE2("");
49 }
50 //-----------------------------------------------------------------------
51 //-----------------------------------------------------------------------
52 //-----------------------------------------------------------------------
54 int StMuDbReader::addDb(const char* fileName) {
55  DEBUGMESSAGE2("");
56  char name[256];
57  int numberOfEvents;
58  char line[512];
59 
60  ifstream in(fileName);
61  if (!in) {
62  DEBUGMESSAGE2("can not open file");
63  DEBUGVALUE2(fileName);
64  return mDb.size();
65  }
66  while ( in ) {
67  in.getline(line,511);
68  DEBUGVALUE3(line);
69  int iret = sscanf(line,"%s%i",name, &numberOfEvents);
70  if (iret==2) {
71  pair<string,int> aPair(name,numberOfEvents);
72  mDb.push_back( aPair );
73  }
74  }
75  in.close();
76  sortDb();
77  return mDb.size();
78 }
79 //-----------------------------------------------------------------------
80 //-----------------------------------------------------------------------
81 //-----------------------------------------------------------------------
84  DEBUGMESSAGE2("");
85  for (iter=mDb.begin(); iter!=mDb.end(); iter++) {
86  cout << (*iter).first.c_str() << endl;
87  }
88 }
89 //-----------------------------------------------------------------------
90 //-----------------------------------------------------------------------
91 //-----------------------------------------------------------------------
94  DEBUGMESSAGE2("");
95  list< pair<string,int> > tmpList;
96  list< pair<string,int> >::iterator tmpIter;
97  for (iter=mDb.begin(); iter!=mDb.end(); iter++) {
98  tmpList.push_back( *iter );
99  }
100  tmpList.sort();
101 
102  mDb.clear();
103  for (tmpIter=tmpList.begin(); tmpIter!=tmpList.end(); tmpIter++) {
104  mDb.push_back( *tmpIter );
105  }
106 
107 }
108 //-----------------------------------------------------------------------
109 //-----------------------------------------------------------------------
110 //-----------------------------------------------------------------------
112 int StMuDbReader::entries(const char* file){
113  string fileName(file);
114  int lo=0;
115  int hi=mDb.size();
116  int pos=0;
117  int oldPos=0;
118  int entries=0;
119  //cout << lo << " " << pos << " " << hi << endl;
120  while (lo<hi) {
121  pos = (int) (lo+hi)/2;
122  //cout << lo << " " << pos << " " << hi << endl;
123  if (oldPos==pos) break;
124  if (fileName > mDb[pos].first) lo=pos;
125  if (fileName < mDb[pos].first) hi=pos;
126  if (fileName == mDb[pos].first) {
127  entries = mDb[pos].second;
128  lo=hi;
129  }
130  oldPos=pos;
131  }
132  return entries;
133 }
134 //-----------------------------------------------------------------------
135 //-----------------------------------------------------------------------
136 //-----------------------------------------------------------------------
137 #include "TFile.h"
138 #include "TTree.h"
140 int StMuDbReader::createDB(const char* dbName, const char* inputList) {
141  DEBUGMESSAGE("");
142 
144  addDb(dbName);
145  //showDb();
146  // open db to add entries
147  ofstream dbFile(dbName, ios::app);
148  // open inputList
149 
150  // check streams
151  ifstream in(inputList);
152  if (!in || !dbFile) {
153  DEBUGVALUE2("could not open file");
154  return 0;
155  }
156 
157  int count=0;
158  char line[256];
159  char fileName[256];
160  // loop over input list
161  while ( in ) {
162  in.getline(line,255);
163  int iret = sscanf(line,"%s",fileName);
164  // if a filename is read
165  if (iret==1) {
166  cout << fileName << " ";
167  //check whether file is alread in db or not
168  if (entries(fileName)==0) {
169  TFile f1(fileName);
170  TTree *tree = dynamic_cast<TTree*>(f1.Get("MuDst"));
171  if (tree) {
172  Stat_t nentries = tree->GetEntries();
173  dbFile << fileName << " " << nentries << endl;
174  cout << " added with " << nentries << " entries " << endl;
175  count++;
176  }
177  f1.Close();
178  } else {
179  cout << " already in data base " << endl;
180  }
181  }
182  cout << endl;
183  }
184  in.close();
185  dbFile.close();
186  return count;
187 }
188 
189 /***************************************************************************
190  *
191  * $Log: StMuDbReader.cxx,v $
192  * Revision 1.5 2016/05/04 19:08:27 smirnovd
193  * StMuDbReader: Moved inlined method in the header
194  *
195  * Inlined methods need to be defined in the header to avoid unresolved external
196  * errors from the linker
197  *
198  * Revision 1.4 2003/04/15 16:57:42 laue
199  * Minor changes to be able to filter MuDst.root files and an example
200  * how to do this. The StMuDstFilterMaker is just an example, it has to be
201  * customized (spoilers, chrome weels, etc.) by the user.
202  *
203  * Revision 1.3 2002/10/03 19:27:13 laue
204  * maximum filename length increased (256 characters)
205  *
206  * Revision 1.2 2002/05/04 23:56:29 laue
207  * some documentation added
208  *
209  * Revision 1.1 2002/04/11 14:19:30 laue
210  * - update for RH 7.2
211  * - decrease default arrays sizes
212  * - add data base readerfor number of events in a file
213  *
214  * Revision 1.1 2002/04/01 22:42:29 laue
215  * improved chain filter options
216  *
217  *
218  **************************************************************************/
219 
220 
221 
222 
223 
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
int addDb(const char *dbFile)
add entries in dbFile to internal data base ( mDb ), will call sortDb(), returns number of entries in...
int entries(const char *file)
scan internal data base for file, if found return number of entries, otherwise return 0; ...
StMuDbReader()
Attention: constructor not public, this is a singleton.
void showDb()
show all entries in internal data base
void sortDb()
sort all entries in internal data base according to file name
vector< pair< string, int > > mDb
the internal database, a vector containing pairs of file names and number of events */ ...
Definition: StMuDbReader.h:37
int createDB(const char *dbFile, const char *inputList)
scan the files in inputList for the number of events add add them to the dbFile file. Create dbFile file if not existent returns number of entries in mDb