8 #include "StHbtMaker/Reader/StHbtTTreeReader.h"
15 #include "StIOMaker/StIOMaker.h"
19 #include "StEventTypes.h"
24 #include "SystemOfUnits.h"
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"
41 #include "StHbtMaker/Infrastructure/StHbtTTreeEvent.h"
42 #include "StarClassLibrary/StMemoryInfo.hh"
48 #if !(ST_NO_NAMESPACES)
49 using namespace units;
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;
61 mDir = string(dirName);
62 mFile = string(fileName);
63 mExtention = string(extention);
64 mFilter = string(filter);
67 if (mDebug) cout <<
"StHbtTTreeReader::StHbtTTreeReader(...) - leaving"<< endl;
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;
82 StHbtString StHbtTTreeReader::Report(){
83 StHbtString temp =
"\n This is the StHbtTTreeReader\n";
85 sprintf(ccc,
" Track type is %d\n",mTrackType);
87 temp +=
"---> EventCuts in Reader: ";
89 temp += mEventCut->Report();
94 temp +=
"\n---> TrackCuts in Reader: ";
96 temp += mTrackCut->Report();
101 temp +=
"\n---> V0Cuts in Reader: ";
103 temp += mV0Cut->Report();
108 temp +=
"\n---> XiCuts in Reader: ";
110 temp += mXiCut->Report();
115 temp +=
"\n---> KinkCuts in Reader: ";
117 temp += mKinkCut->Report();
126 StHbtEvent* StHbtTTreeReader::ReturnHbtEvent(){
127 if (mDebug) cout <<
"StHbtTTreeReader::ReturnHbtEvent()" << endl;
132 if (mIOMode == hbtRead) hbtEvent = read();
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 ) {
158 int StHbtTTreeReader::WriteHbtEvent(
StHbtEvent* event){
159 if (mDebug) cout <<
"StHbtTTreeReader::WriteHbtEvent()" << endl;
164 if (mIOMode == hbtWrite) hbtEvent = write(event);
175 if (hbtEvent && mDebug>1) cout << *hbtEvent << endl;
176 if (!hbtEvent) cout <<
"StHbtTTreeReader::WriteHbtEvent() - no hbtEvent" << endl;
187 if (mDebug) cout <<
"StHbtTTreeReader::write() - No StHbtEvent!!! " << endl;
192 string ioMakerFileName;
194 ioMakerFileName = string(mIOMaker->GetFile());
197 ioMakerFileName = mDir+mFile;
200 if (!mCurrentFile) initWrite(ioMakerFileName,mExtention);
202 if (mCurrentFileName != ioMakerFileName) {
204 initWrite(ioMakerFileName,mExtention);
208 if (mDebug) cout <<
"StHbtTTreeReader::write() - can not open file!!! " << endl;
212 if (mEventCut->Pass(event)) {
214 mHbtTTreeEvent->clear();
215 mHbtTTreeEvent->fill(event, mTrackCut, mV0Cut, mXiCut, mKinkCut);
216 if (mDebug) cout <<
"StHbtTTreeReader::write() - now fill the tree " << endl;
224 if (mDebug) cout <<
"StHbtTTreeReader::write() - leaving " << endl;
228 int StHbtTTreeReader::initWrite(
string ioMakerFileName,
string extention){
229 if (split) bufsize /= 4;
231 string fileName = ioMakerFileName;
232 while ( fileName.find(
"/") != string::npos ) {
233 string::size_type pos = fileName.find(
"/");
234 fileName.erase(0, pos+1 );
236 fileName = mDir +fileName + extention;
237 if (mDebug) cout <<
"StHbtTTreeReader::initWrite(...) - new fileName: " << fileName.c_str() << endl;
239 if (mDebug) cout <<
"StHbtTTreeReader::initWrite(...) - now create file " << endl;
240 mCurrentFile =
new TFile(fileName.c_str(),
"RECREATE",
"hbtTTreeMuDst");
242 cout <<
"StHbtTTreeReader::initWrite(...) - Warning: no file opened = " << endl;
246 mCurrentFile->SetCompressionLevel(comp);
249 if (mDebug) cout <<
"StHbtTTreeReader::initWrite(...) - now create tree " << endl;
250 mTTree =
new TTree(
"Tree",
"hbtTTree",split);
252 cout <<
"StHbtTTreeReader::initWrite(...) - Warning: No Tree" << endl;
256 mTTree->SetAutoSave(1000000);
258 if (mDebug) cout <<
"StHbtTTreeReader::initWrite(...) - now create branch " << endl;
259 mTTree->Branch(
"mHbtTTreeEvent",
"StHbtTTreeEvent", &mHbtTTreeEvent, bufsize, split);
260 mCurrentFileName = ioMakerFileName;
264 int StHbtTTreeReader::uninitWrite(){
266 mCurrentFile->Close();
267 if (mCurrentFile)
delete mCurrentFile;
268 if (mTTree)
delete mTTree;
277 cout << initRead(mDir,mFile,mFilter,mExtention,mMaxFiles) <<
" files to analyse " << endl;
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 ");
297 int StHbtTTreeReader::initRead(
string dir,
string file,
string filter,
string extention,
int mMaxFiles){
299 mTChain =
new TChain(
"Tree",
"hbtTree");
302 if( strstr(file.c_str(),
".lis") ) {
304 nFiles = fillChain(mTChain, (dir+file).c_str(), mMaxFiles);
311 mTChain->Add((dir+file).c_str());
317 nFiles = fillChain(mTChain,dir.c_str(), filter.c_str(), extention.c_str(), mMaxFiles);
322 } mTChain->SetBranchAddress(
"mHbtTTreeEvent",&mHbtTTreeEvent);
326 int StHbtTTreeReader::uninitRead(){
327 if (mHbtTTreeEvent)
delete mHbtTTreeEvent;
328 if (mTChain)
delete mTChain;
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");
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;
348 if (count>maxFiles)
break;
351 if (mDebug) cout <<
"StHbtTTreeReader::(string dir)(string dir) - Added " << count <<
" files to the chain" << endl;
355 int StHbtTTreeReader::fillChain(TChain* chain,
const char* dir,
const char* filter,
const char* extention,
const int maxFiles) {
357 void *pDir = gSystem->OpenDirectory(dir);
358 if(!pDir)
throw StException(
"StHbtTTreeReader::fillChain(string dir) - can not open directory");
360 const char* fileName(0);
362 while((fileName = gSystem->GetDirEntry(pDir))){
363 if(strcmp(fileName,
".")==0 || strcmp(fileName,
"..")==0)
continue;
364 if(strstr(fileName,filter) && strstr(fileName,extention)) {
365 char* fullFile = gSystem->ConcatFileName(dir,fileName);
367 cout <<
"StHbtTTreeReader::fillChain(string dir) - Adding " << fullFile <<
" to the chain" << endl;
368 chain->Add(fullFile);
371 if (count>maxFiles)
break;
374 cout <<
"StHbtTTreeReader::(string dir)(string dir) - Added " << count <<
" files to the chain" << endl;