StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
IO.cxx
1 
2 #include "IO.h"
3 #include "TChain.h"
4 #include "TFile.h"
5 #include "TTree.h"
6 #include "TSystem.h"
7 
8 #include "Stiostream.h"
9 #include <fstream>
10 #include <stdio.h>
11 #include <stdlib.h>
12 
13 ClassImp(IO)
14 
15 IO::IO(const char* dir, const char* match, const char* ext)
16  : mNFile(0), mDir(dir), mMatch(match), mExt(ext) {
17 
18 }
19 
20 IO::~IO() {}
21 
22 void IO::chain(TChain* chain)
23 {
24 
25  void *pDir = gSystem->OpenDirectory(mDir.Data());
26  if(!pDir){
27  std::cerr << "##Cannot open directory " << mDir.Data() << endl;
28  std::cerr << "##Goodbye" << endl;
29  exit(1);
30  }
31 
32  cout << "\tUsing directory : " << mDir.Data() << endl;
33  cout << "\tMatch string : " << mMatch.Data() << endl;
34  cout << "\tMatch extension : " << mExt.Data() << endl;
35  if(mNFile) cout << "\tMaximum # files : " << mNFile << endl;
36 
37  //
38  // now find the files that end in the specified extension
39  // and match the specified string
40  //
41  const char* fileName(0);
42  Int_t count(0);
43 
44  while((fileName = gSystem->GetDirEntry(pDir))){
45  if(strcmp(fileName,".")==0 || strcmp(fileName,"..")==0) continue;
46 
47  if(strstr(fileName,mExt.Data()) && strstr(fileName,mMatch.Data())){ // found a match
48  char* fullFile = gSystem->ConcatFileName(mDir.Data(),fileName);
49 
50  // add it to the chain
51  // cout << "\tAdding " << fullFile << " to the chain" << endl;
52 
53  //Here implement appeal to event count to speed up chain read
54 
55  count++;
56 
57  int events = 0;
58 /*
59  // read number of events in file from db
60  events = entries(fullFile);
61 */
62  // if I can not read the number of events from db, open file and read number.
63  if (events==0) {
64  TFile *f1 = new TFile(fullFile);
65  TTree *tree = (TTree*)f1->Get("StHiMicroTree");
66  if (tree) events = (int)tree->GetEntries();
67  delete f1;
68  }
69  // add to chain if #events > 0
70  if (events) {
71  chain->Add(fullFile,events);
72  if (count%100==0) cout << "File # " << count << endl << "\tAdding " <<
73  fullFile << " with " << events << " events to the chain" << endl;
74  delete fullFile;
75  }
76 
77  if(mNFile && count > mNFile) break;
78 
79  }
80  }
81  cout << "Added " << count << " files to the chain" << endl;
82 
83 }
84 
85 void IO::createDb(const char *dbName)
86 {
87 
88  // open db to add entries
89  ofstream dbFile(dbName, std::ios::app);
90 
91  void *pDir = gSystem->OpenDirectory(mDir.Data());
92  if(!pDir){
93  std::cerr << "##Cannot open directory " << mDir.Data() << endl;
94  std::cerr << "##Goodbye" << endl;
95  exit(1);
96  }
97 
98  cout << "\tUsing directory : " << mDir.Data() << endl;
99  cout << "\tMatch string : " << mMatch.Data() << endl;
100  cout << "\tMatch extension : " << mExt.Data() << endl;
101 
102  //
103  // now find the files that end in the specified extension
104  //
105  const char* fileName(0);
106  Int_t count(0);
107 
108  while((fileName = gSystem->GetDirEntry(pDir))){
109  if(strcmp(fileName,".")==0 || strcmp(fileName,"..")==0) continue;
110 
111  if(strstr(fileName,mExt.Data()) && strstr(fileName,mMatch.Data())){ // found a match
112  char* fullFile = gSystem->ConcatFileName(mDir.Data(),fileName);
113 
114  count++;
115  int events = 0;
116  //Open file and read number.
117  if (events==0) {
118  TFile *f1 = new TFile(fullFile);
119  TTree *tree = (TTree*)f1->Get("StHiMicroTree");
120  if (tree) events = (int)tree->GetEntries();
121  delete f1;
122  }
123  if (events) {
124  dbFile << fullFile << " " << events << endl;
125  cout << "\tAdding " << fullFile << " with " << events << " events to the Database" << endl;
126  delete fullFile;
127  }
128 
129  }
130  }
131  dbFile.close();
132 
133 }
134 
135 /*
136 
137 //----------------------------------------------------------------
140 
141 int IO::addDb(const char* fileName) {
142 
143  char name[128];
144  string nameString;
145  int numberOfEvents;
146  char line[256];
147 
148  ifstream in(fileName);
149  if (!in) {
150  cout << "Cannot open file " << fileName << endl;
151  return mDb.size();
152  }
153  while ( in ) {
154  in.getline(line,255);
155  int iret = sscanf(line,"%s %i",name, &numberOfEvents);
156  nameString = name;
157  if (iret==2) {
158  pair<string,int> aPair(nameString,numberOfEvents);
159  mDb.push_back( aPair );
160  }
161  }
162  in.close();
163  sortDb();
164  return mDb.size();
165 }
166 
167 //-----------------------------------------------------
169 void IO::showDb(){
170  for (iter=mDb.begin(); iter!=mDb.end(); iter++) {
171  cout << (*iter).first.c_str() << endl;
172  }
173 }
174 
175 //-----------------------------------------------------------------------
177 void IO::sortDb(){
178 
179  list< pair<string,int> > tmpList;
180  list< pair<string,int> >::iterator tmpIter;
181  for (iter=mDb.begin(); iter!=mDb.end(); iter++) {
182  tmpList.push_back( *iter );
183  }
184  tmpList.sort();
185 
186  mDb.clear();
187  for (tmpIter=tmpList.begin(); tmpIter!=tmpList.end(); tmpIter++) {
188  mDb.push_back( *tmpIter );
189  }
190 
191 }
192 
193 //-----------------------------------------------------------------------
196 
197 int IO::entries(const char* file){
198  string fileName(file);
199  int lo=0;
200  int hi=mDb.size();
201  int pos=0;
202  int oldPos=0;
203  int entries=0;
204  //cout << lo << " " << pos << " " << hi << endl;
205  while (lo<hi) {
206  pos = (int) (lo+hi)/2;
207  //cout << lo << " " << pos << " " << hi << endl;
208  if (oldPos==pos) break;
209  if (fileName > mDb[pos].first) lo=pos;
210  if (fileName < mDb[pos].first) hi=pos;
211  if (fileName == mDb[pos].first) {
212  entries = mDb[pos].second;
213  lo=hi;
214  }
215  oldPos=pos;
216  }
217  return entries;
218 }
219 
220 //-----------------------------------------------------------------------
224 
225 int IO::createDb(const char* dbName, const char* inputList)
226 {
227 
229  addDb(dbName);
230  //showDb();
231  // open db to add entries
232  ofstream dbFile(dbName, std::ios::app);
233  // open inputList
234 
235  // check streams
236  ifstream in(inputList);
237 
238  if (!in || !dbFile) {
239  cout << "Could not open file" << endl;
240  return 0;
241  }
242 
243  int count=0;
244  char line[256];
245  char fileName[256];
246  // loop over input list
247  while ( in ) {
248  in.getline(line,255);
249  int iret = sscanf(line,"%s",fileName);
250  // if a filename is read
251  if (iret==1) {
252  cout << fileName << " ";
253  //check whether file is alread in db or not
254  if (entries(fileName)==0) {
255  TFile *f1 = new TFile(fileName);
256  TTree *tree = (TTree*)f1->Get("StHiMicroTree");
257  if (tree) {
258  Stat_t nentries = tree->GetEntries();
259  dbFile << fileName << " " << nentries << endl;
260  cout << " added with " << nentries << " entries " << endl;
261  count++;
262  }
263  delete f1;
264  delete tree;
265  } else {
266  cout << " already in data base " << endl;
267  }
268  }
269  cout << endl;
270  }
271  in.close();
272  dbFile.close();
273  return count;
274 }
275 
276 */
Definition: IO.h:20