StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StHbtTTreeReader.cxx
1 /***************************************************************************
2  *
3  * $Id: StHbtTTreeReader.cxx,v 1.8 2003/09/02 17:58:34 perev Exp $
4  *
5  * Author: Frank Laue, BNL, laue@bnl.gov
6  ***************************************************************************/
7 
8 #include "StHbtMaker/Reader/StHbtTTreeReader.h"
9 
10 #include "StChain.h"
11 #include "TChain.h"
12 #include "TFile.h"
13 #include "TTree.h"
14 
15 #include "StIOMaker/StIOMaker.h"
16 
17 
18 #include "StEvent.h"
19 #include "StEventTypes.h"
20 #include <math.h>
21 #include <string>
22 #include <typeinfo>
23 
24 #include "SystemOfUnits.h" // has "tesla" in it
25 #include "StHbtMaker/Infrastructure/StExceptions.hh"
26 #include "StHbtMaker/Infrastructure/StHbtTrackCollection.hh"
27 #include "StHbtMaker/Infrastructure/StHbtV0Collection.hh"
28 #include "StHbtMaker/Infrastructure/StHbtXiCollection.hh"
29 #include "StHbtMaker/Reader/StHbtGstarTxtReader.h"
30 #include "StHbtMaker/Infrastructure/StHbtEvent.hh"
31 #include "StHbtMaker/Base/StHbtEventCut.h"
32 #include "StHbtMaker/Base/StHbtTrackCut.h"
33 #include "StHbtMaker/Base/StHbtV0Cut.h"
34 #include "StHbtMaker/Base/StHbtXiCut.h"
35 #include "StHbtMaker/Base/StHbtKinkCut.h"
37 #include "StStrangeMuDstMaker/StV0MuDst.hh"
38 #include "StStrangeMuDstMaker/StXiMuDst.hh"
39 #include "StStrangeMuDstMaker/StKinkMuDst.hh"
40 
41 #include "StHbtMaker/Infrastructure/StHbtTTreeEvent.h"
42 #include "StarClassLibrary/StMemoryInfo.hh"
43 
44 #ifdef __ROOT__
45 ClassImp(StHbtTTreeReader)
46 #endif
47 
48 #if !(ST_NO_NAMESPACES)
49  using namespace units;
50 #endif
51 
52 
53 //__________________
54 StHbtTTreeReader::StHbtTTreeReader(StHbtIOMode mode, StIOMaker* io,
55  const char* dirName, const char* fileName, const char* extention,
56  const char* filter, int maxFiles)
57  : mIOMaker(io), mTrackType(primary), mIOMode(mode), mMaxFiles(maxFiles), mDebug(0), mCurrentFile(0),
58  mTTree(0), split(99), comp(2), bufsize(256000/4) {
59  if (mDebug) cout << "StHbtTTreeReader::StHbtTTreeReader(...)"<< endl;
60 
61  mDir = string(dirName);
62  mFile = string(fileName);
63  mExtention = string(extention);
64  mFilter = string(filter);
65  mReaderStatus = 0; // "good"
66  mHbtTTreeEvent = new StHbtTTreeEvent();
67  if (mDebug) cout << "StHbtTTreeReader::StHbtTTreeReader(...) - leaving"<< endl;
68 }
69 //__________________
70 StHbtTTreeReader::~StHbtTTreeReader(){
71  if (mIOMode==hbtWrite && mCurrentFile && mTTree) { mTTree->AutoSave(); mTTree=0;}
72  if (mCurrentFile) { mCurrentFile->Close(); delete mCurrentFile; mCurrentFile = 0;}
73  if (mEventCut) delete mEventCut;
74  if (mTrackCut) delete mTrackCut;
75  if (mV0Cut) delete mV0Cut;
76  if (mXiCut) delete mXiCut;
77  if (mKinkCut) delete mKinkCut;
78 
79 
80 }
81 //__________________
82 StHbtString StHbtTTreeReader::Report(){
83  StHbtString temp = "\n This is the StHbtTTreeReader\n";
84  char ccc[100];
85  sprintf(ccc," Track type is %d\n",mTrackType);
86  temp += ccc;
87  temp += "---> EventCuts in Reader: ";
88  if (mEventCut) {
89  temp += mEventCut->Report();
90  }
91  else {
92  temp += "NONE";
93  }
94  temp += "\n---> TrackCuts in Reader: ";
95  if (mTrackCut) {
96  temp += mTrackCut->Report();
97  }
98  else {
99  temp += "NONE";
100  }
101  temp += "\n---> V0Cuts in Reader: ";
102  if (mV0Cut) {
103  temp += mV0Cut->Report();
104  }
105  else {
106  temp += "NONE";
107  }
108  temp += "\n---> XiCuts in Reader: ";
109  if (mXiCut) {
110  temp += mXiCut->Report();
111  }
112  else {
113  temp += "NONE";
114  }
115  temp += "\n---> KinkCuts in Reader: ";
116  if (mKinkCut) {
117  temp += mKinkCut->Report();
118  }
119  else {
120  temp += "NONE";
121  }
122  temp += "\n";
123  return temp;
124 }
125 //__________________
126 StHbtEvent* StHbtTTreeReader::ReturnHbtEvent(){
127  if (mDebug) cout << "StHbtTTreeReader::ReturnHbtEvent()" << endl;
128 
129  StHbtEvent* hbtEvent = 0;
130 
131  try {
132  if (mIOMode == hbtRead) hbtEvent = read();
133  }
134  catch(StExceptionEOF e) {
135  e.print();
136  mReaderStatus = 2;
137  return 0;
138  }
139  catch(StException e) {
140  e.print();
141  mReaderStatus = 1;
142  return 0;
143  }
144 
145  if (hbtEvent && mDebug>1) cout << *hbtEvent << endl;
146  if (!hbtEvent) cout << "StHbtTTreeReader::ReturnHbtEvent() - no hbtEvent" << endl;
147  if (mEventCut && hbtEvent ) {
148  if ( mEventCut->Pass(hbtEvent)==0 ) {
149  delete hbtEvent;
150  hbtEvent=0;
151  }
152  }
153 
154  return hbtEvent;
155 }
156 
157 //__________________
158 int StHbtTTreeReader::WriteHbtEvent(StHbtEvent* event){
159  if (mDebug) cout << "StHbtTTreeReader::WriteHbtEvent()" << endl;
160 
161  StHbtEvent* hbtEvent = 0;
162 
163  try {
164  if (mIOMode == hbtWrite) hbtEvent = write(event);
165  }
166  catch(StExceptionEOF e) {
167  e.print();
168  return 1;
169  }
170  catch(StException e) {
171  e.print();
172  return 0;
173  }
174 
175  if (hbtEvent && mDebug>1) cout << *hbtEvent << endl;
176  if (!hbtEvent) cout << "StHbtTTreeReader::WriteHbtEvent() - no hbtEvent" << endl;
177 
178  return 0;
179 }
180 
181 //__________________
182 StHbtEvent* StHbtTTreeReader::write(StHbtEvent* event){
184  // get StEvent //
186  if (!event){
187  if (mDebug) cout << "StHbtTTreeReader::write() - No StHbtEvent!!! " << endl;
188  return 0;
189  }
190 
191 
192  string ioMakerFileName;
193  if (mIOMaker) { // if the ioMaker is specified, we take the output filename from the ioMaker
194  ioMakerFileName = string(mIOMaker->GetFile());
195  }
196  else { // if not then construct the filename
197  ioMakerFileName = mDir+mFile;
198  }
199 
200  if (!mCurrentFile) initWrite(ioMakerFileName,mExtention); // if no file open, open the file
201 
202  if (mCurrentFileName != ioMakerFileName) { // if wrong file open, close file and open new file
203  uninitWrite();
204  initWrite(ioMakerFileName,mExtention);
205  }
206 
207  if (!mCurrentFile) {
208  if (mDebug) cout << "StHbtTTreeReader::write() - can not open file!!! " << endl;
209  return 0;
210  }
211 
212  if (mEventCut->Pass(event)) {
213  try{
214  mHbtTTreeEvent->clear();
215  mHbtTTreeEvent->fill(event, mTrackCut, mV0Cut, mXiCut, mKinkCut);
216  if (mDebug) cout << "StHbtTTreeReader::write() - now fill the tree " << endl;
217  mTTree->Fill();
218  }
219  catch(StException e) {
220  mHbtTTreeEvent =0;
221  e.print();
222  }
223  }
224  if (mDebug) cout << "StHbtTTreeReader::write() - leaving " << endl;
225  return event;
226 }
227 
228 int StHbtTTreeReader::initWrite(string ioMakerFileName, string extention){
229  if (split) bufsize /= 4;
230 
231  string fileName = ioMakerFileName;
232  while ( fileName.find("/") != string::npos ) {
233  string::size_type pos = fileName.find("/");
234  fileName.erase(0, pos+1 );
235  }
236  fileName = mDir +fileName + extention;
237  if (mDebug) cout << "StHbtTTreeReader::initWrite(...) - new fileName: " << fileName.c_str() << endl;
238  // creat a Picoevent and and output file
239  if (mDebug) cout << "StHbtTTreeReader::initWrite(...) - now create file " << endl;
240  mCurrentFile = new TFile(fileName.c_str(),"RECREATE","hbtTTreeMuDst");
241  if (!mCurrentFile) {
242  cout << "StHbtTTreeReader::initWrite(...) - Warning: no file opened = " << endl;
243  assert(0);
244  }
245  //mCurrentFile->SetFormat(1);
246  mCurrentFile->SetCompressionLevel(comp);
247 
248  // Create a ROOT Tree and one superbranch
249  if (mDebug) cout << "StHbtTTreeReader::initWrite(...) - now create tree " << endl;
250  mTTree = new TTree("Tree", "hbtTTree",split);
251  if (!mTTree) {
252  cout << "StHbtTTreeReader::initWrite(...) - Warning: No Tree" << endl;
253  assert(0);
254  }
255 
256  mTTree->SetAutoSave(1000000); // autosave when 1 Mbyte written
257  //mTTree->SetAutoSave(1000); // autosave when 1 Mbyte written
258  if (mDebug) cout << "StHbtTTreeReader::initWrite(...) - now create branch " << endl;
259  mTTree->Branch("mHbtTTreeEvent", "StHbtTTreeEvent", &mHbtTTreeEvent, bufsize, split);
260  mCurrentFileName = ioMakerFileName;
261  return 0;
262 }
263 
264 int StHbtTTreeReader::uninitWrite(){
265  mTTree->AutoSave();
266  mCurrentFile->Close();
267  if (mCurrentFile) delete mCurrentFile;
268  if (mTTree) delete mTTree;
269  mTTree = 0;
270  mCurrentFile = 0;
271  return 0;
272 }
273 
274 StHbtEvent* StHbtTTreeReader::read(){
275  if (!mTChain) {
276  try {
277  cout << initRead(mDir,mFile,mFilter,mExtention,mMaxFiles) << " files to analyse " << endl;
278  }
279  catch(StException e) {
280  e.print();
281  return 0;
282  }
283  }
284 
285  unsigned int nEvents = (unsigned int)mTChain->GetEntries();
286  if (!nEvents) throw StException("StHbtTTreeReader::read() - no events to read ");
287  mHbtTTreeEvent->clear();
288  int iBytes= mTChain->GetEntry(mEventIndex++);
289  if (mDebug) cout << "StHbtTTreeReader::read() - bytes read :" << iBytes << endl;
290  if (nEvents<mEventIndex) throw StExceptionEOF("StHbtTTreeReader::read()");
291  if (!iBytes) throw StException("StHbtTTreeReader::read() - no event ");
292  StHbtEvent* hbtEvent = new StHbtEvent(mHbtTTreeEvent); // ok, here we got event read from the file
293  // now we should use the copy constructor for StHbtEvent in order to appy the cuts
294  return hbtEvent;
295 }
296 
297 int StHbtTTreeReader::initRead(string dir, string file, string filter, string extention, int mMaxFiles){
298  mEventIndex =0;
299  mTChain = new TChain("Tree","hbtTree");
300  int nFiles =0;
301  if (file!="") { // if a filename was given
302  if( strstr(file.c_str(),".lis") ) { // if a file list is specified
303  try {
304  nFiles = fillChain(mTChain, (dir+file).c_str(), mMaxFiles);
305  }
306  catch(StException e) {
307  throw e;
308  }
309  }
310  else { // a single file was specified
311  mTChain->Add((dir+file).c_str());
312  nFiles++;
313  }
314  }
315  else {
316  try {
317  nFiles = fillChain(mTChain,dir.c_str(), filter.c_str(), extention.c_str(), mMaxFiles);
318  }
319  catch(StException e) {
320  throw e;
321  }
322  } mTChain->SetBranchAddress("mHbtTTreeEvent",&mHbtTTreeEvent); // set the address where to read the event object
323  return nFiles;
324 }
325 
326 int StHbtTTreeReader::uninitRead(){
327  if (mHbtTTreeEvent) delete mHbtTTreeEvent;
328  if (mTChain) delete mTChain;
329  mHbtTTreeEvent = 0;
330  mTChain = 0;
331  return 0;
332 }
333 
334 int StHbtTTreeReader::fillChain(TChain* chain, const char* fileList, const int maxFiles) {
335  ifstream* inputStream = new ifstream;
336  inputStream->open(fileList);
337  if (!(inputStream)) throw StException("StHbtTTreeReader::fillChain(string dir) - can not open directory");
338  char* temp;
339  int count=0;
340  if (mDebug>1) cout << " StHbtTTreeReader::fillChain(...)- inputStream->good() : " << inputStream->good() << endl;
341  for (;inputStream->good();) {
342  temp = new char[200];
343  inputStream->getline(temp,200);
344  if (mDebug) cout << temp << endl;
345  chain->Add(temp);
346  delete temp;
347  ++count;
348  if (count>maxFiles) break;
349  }
350  delete inputStream;
351  if (mDebug) cout << "StHbtTTreeReader::(string dir)(string dir) - Added " << count << " files to the chain" << endl;
352  return count;
353 }
354 
355 int StHbtTTreeReader::fillChain(TChain* chain, const char* dir, const char* filter, const char* extention, const int maxFiles) {
356  // read directory
357  void *pDir = gSystem->OpenDirectory(dir);
358  if(!pDir) throw StException("StHbtTTreeReader::fillChain(string dir) - can not open directory");
359  // now find the files that end in the specified searchString
360  const char* fileName(0);
361  int count(0);
362  while((fileName = gSystem->GetDirEntry(pDir))){
363  if(strcmp(fileName,".")==0 || strcmp(fileName,"..")==0) continue;
364  if(strstr(fileName,filter) && strstr(fileName,extention)) { // found a match
365  char* fullFile = gSystem->ConcatFileName(dir,fileName);
366  // add it to the chain
367  cout << "StHbtTTreeReader::fillChain(string dir) - Adding " << fullFile << " to the chain" << endl;
368  chain->Add(fullFile);
369  delete fullFile;
370  ++count;
371  if (count>maxFiles) break;
372  }
373  }
374  cout << "StHbtTTreeReader::(string dir)(string dir) - Added " << count << " files to the chain" << endl;
375  return count;
376 }
377 
378 
379 /***************************************************************************
380  *
381  * $Log: StHbtTTreeReader.cxx,v $
382  * Revision 1.8 2003/09/02 17:58:34 perev
383  * gcc 3.2 updates + WarnOff
384  *
385  * Revision 1.7 2003/05/16 21:30:18 magestro
386  * Removed obsolete include file
387  *
388  * Revision 1.6 2003/05/07 20:05:25 magestro
389  * Removed StFlowTagMaker.h include
390  *
391  * Revision 1.5 2001/12/05 14:42:17 laue
392  * updated for trigger(action)word and l3TriggerAlgorithm
393  *
394  * Revision 1.1 2001/06/21 19:18:42 laue
395  * Modified Files: (to match the changed base classes)
396  * StHbtAsciiReader.cxx StHbtAsciiReader.h
397  * StHbtAssociationReader.cxx StHbtAssociationReader.h
398  * StHbtBinaryReader.cxx StHbtBinaryReader.h
399  * StHbtGstarTxtReader.cxx StHbtGstarTxtReader.h
400  * StHbtStrangeMuDstEventReader.cxx
401  * StHbtStrangeMuDstEventReader.h StStandardHbtEventReader.cxx
402  * Added Files: new reader
403  * StHbtTTreeReader.cxx StHbtTTreeReader.h
404  *
405  *
406  **************************************************************************/
407 
408 
409 
410 
411 
412 
413 
414 
415 
416 
417 
418 
419 
420 
421 
422 
423 
424 
425