00001
00002
00003
00004
00005
00006
00007
00008 #include "StHbtMaker/Reader/StHbtTTreeReader.h"
00009
00010 #include "StChain.h"
00011 #include "TChain.h"
00012 #include "TFile.h"
00013 #include "TTree.h"
00014
00015 #include "StIOMaker/StIOMaker.h"
00016
00017
00018 #include "StEvent.h"
00019 #include "StEventTypes.h"
00020 #include <math.h>
00021 #include <string>
00022 #include <typeinfo>
00023
00024 #include "SystemOfUnits.h"
00025 #include "StHbtMaker/Infrastructure/StExceptions.hh"
00026 #include "StHbtMaker/Infrastructure/StHbtTrackCollection.hh"
00027 #include "StHbtMaker/Infrastructure/StHbtV0Collection.hh"
00028 #include "StHbtMaker/Infrastructure/StHbtXiCollection.hh"
00029 #include "StHbtMaker/Reader/StHbtGstarTxtReader.h"
00030 #include "StHbtMaker/Infrastructure/StHbtEvent.hh"
00031 #include "StHbtMaker/Base/StHbtEventCut.h"
00032 #include "StHbtMaker/Base/StHbtTrackCut.h"
00033 #include "StHbtMaker/Base/StHbtV0Cut.h"
00034 #include "StHbtMaker/Base/StHbtXiCut.h"
00035 #include "StHbtMaker/Base/StHbtKinkCut.h"
00036 #include "StStrangeMuDstMaker/StStrangeMuDstMaker.h"
00037 #include "StStrangeMuDstMaker/StV0MuDst.hh"
00038 #include "StStrangeMuDstMaker/StXiMuDst.hh"
00039 #include "StStrangeMuDstMaker/StKinkMuDst.hh"
00040
00041 #include "StHbtMaker/Infrastructure/StHbtTTreeEvent.h"
00042 #include "StarClassLibrary/StMemoryInfo.hh"
00043
00044 #ifdef __ROOT__
00045 ClassImp(StHbtTTreeReader)
00046 #endif
00047
00048 #if !(ST_NO_NAMESPACES)
00049 using namespace units;
00050 #endif
00051
00052
00053
00054 StHbtTTreeReader::StHbtTTreeReader(StHbtIOMode mode, StIOMaker* io,
00055 const char* dirName, const char* fileName, const char* extention,
00056 const char* filter, int maxFiles)
00057 : mIOMaker(io), mTrackType(primary), mIOMode(mode), mMaxFiles(maxFiles), mDebug(0), mCurrentFile(0),
00058 mTTree(0), split(99), comp(2), bufsize(256000/4) {
00059 if (mDebug) cout << "StHbtTTreeReader::StHbtTTreeReader(...)"<< endl;
00060
00061 mDir = string(dirName);
00062 mFile = string(fileName);
00063 mExtention = string(extention);
00064 mFilter = string(filter);
00065 mReaderStatus = 0;
00066 mHbtTTreeEvent = new StHbtTTreeEvent();
00067 if (mDebug) cout << "StHbtTTreeReader::StHbtTTreeReader(...) - leaving"<< endl;
00068 }
00069
00070 StHbtTTreeReader::~StHbtTTreeReader(){
00071 if (mIOMode==hbtWrite && mCurrentFile && mTTree) { mTTree->AutoSave(); mTTree=0;}
00072 if (mCurrentFile) { mCurrentFile->Close(); delete mCurrentFile; mCurrentFile = 0;}
00073 if (mEventCut) delete mEventCut;
00074 if (mTrackCut) delete mTrackCut;
00075 if (mV0Cut) delete mV0Cut;
00076 if (mXiCut) delete mXiCut;
00077 if (mKinkCut) delete mKinkCut;
00078
00079
00080 }
00081
00082 StHbtString StHbtTTreeReader::Report(){
00083 StHbtString temp = "\n This is the StHbtTTreeReader\n";
00084 char ccc[100];
00085 sprintf(ccc," Track type is %d\n",mTrackType);
00086 temp += ccc;
00087 temp += "---> EventCuts in Reader: ";
00088 if (mEventCut) {
00089 temp += mEventCut->Report();
00090 }
00091 else {
00092 temp += "NONE";
00093 }
00094 temp += "\n---> TrackCuts in Reader: ";
00095 if (mTrackCut) {
00096 temp += mTrackCut->Report();
00097 }
00098 else {
00099 temp += "NONE";
00100 }
00101 temp += "\n---> V0Cuts in Reader: ";
00102 if (mV0Cut) {
00103 temp += mV0Cut->Report();
00104 }
00105 else {
00106 temp += "NONE";
00107 }
00108 temp += "\n---> XiCuts in Reader: ";
00109 if (mXiCut) {
00110 temp += mXiCut->Report();
00111 }
00112 else {
00113 temp += "NONE";
00114 }
00115 temp += "\n---> KinkCuts in Reader: ";
00116 if (mKinkCut) {
00117 temp += mKinkCut->Report();
00118 }
00119 else {
00120 temp += "NONE";
00121 }
00122 temp += "\n";
00123 return temp;
00124 }
00125
00126 StHbtEvent* StHbtTTreeReader::ReturnHbtEvent(){
00127 if (mDebug) cout << "StHbtTTreeReader::ReturnHbtEvent()" << endl;
00128
00129 StHbtEvent* hbtEvent = 0;
00130
00131 try {
00132 if (mIOMode == hbtRead) hbtEvent = read();
00133 }
00134 catch(StExceptionEOF e) {
00135 e.print();
00136 mReaderStatus = 2;
00137 return 0;
00138 }
00139 catch(StException e) {
00140 e.print();
00141 mReaderStatus = 1;
00142 return 0;
00143 }
00144
00145 if (hbtEvent && mDebug>1) cout << *hbtEvent << endl;
00146 if (!hbtEvent) cout << "StHbtTTreeReader::ReturnHbtEvent() - no hbtEvent" << endl;
00147 if (mEventCut && hbtEvent ) {
00148 if ( mEventCut->Pass(hbtEvent)==0 ) {
00149 delete hbtEvent;
00150 hbtEvent=0;
00151 }
00152 }
00153
00154 return hbtEvent;
00155 }
00156
00157
00158 int StHbtTTreeReader::WriteHbtEvent(StHbtEvent* event){
00159 if (mDebug) cout << "StHbtTTreeReader::WriteHbtEvent()" << endl;
00160
00161 StHbtEvent* hbtEvent = 0;
00162
00163 try {
00164 if (mIOMode == hbtWrite) hbtEvent = write(event);
00165 }
00166 catch(StExceptionEOF e) {
00167 e.print();
00168 return 1;
00169 }
00170 catch(StException e) {
00171 e.print();
00172 return 0;
00173 }
00174
00175 if (hbtEvent && mDebug>1) cout << *hbtEvent << endl;
00176 if (!hbtEvent) cout << "StHbtTTreeReader::WriteHbtEvent() - no hbtEvent" << endl;
00177
00178 return 0;
00179 }
00180
00181
00182 StHbtEvent* StHbtTTreeReader::write(StHbtEvent* event){
00184
00186 if (!event){
00187 if (mDebug) cout << "StHbtTTreeReader::write() - No StHbtEvent!!! " << endl;
00188 return 0;
00189 }
00190
00191
00192 string ioMakerFileName;
00193 if (mIOMaker) {
00194 ioMakerFileName = string(mIOMaker->GetFile());
00195 }
00196 else {
00197 ioMakerFileName = mDir+mFile;
00198 }
00199
00200 if (!mCurrentFile) initWrite(ioMakerFileName,mExtention);
00201
00202 if (mCurrentFileName != ioMakerFileName) {
00203 uninitWrite();
00204 initWrite(ioMakerFileName,mExtention);
00205 }
00206
00207 if (!mCurrentFile) {
00208 if (mDebug) cout << "StHbtTTreeReader::write() - can not open file!!! " << endl;
00209 return 0;
00210 }
00211
00212 if (mEventCut->Pass(event)) {
00213 try{
00214 mHbtTTreeEvent->clear();
00215 mHbtTTreeEvent->fill(event, mTrackCut, mV0Cut, mXiCut, mKinkCut);
00216 if (mDebug) cout << "StHbtTTreeReader::write() - now fill the tree " << endl;
00217 mTTree->Fill();
00218 }
00219 catch(StException e) {
00220 mHbtTTreeEvent =0;
00221 e.print();
00222 }
00223 }
00224 if (mDebug) cout << "StHbtTTreeReader::write() - leaving " << endl;
00225 return event;
00226 }
00227
00228 int StHbtTTreeReader::initWrite(string ioMakerFileName, string extention){
00229 if (split) bufsize /= 4;
00230
00231 string fileName = ioMakerFileName;
00232 while ( fileName.find("/") != string::npos ) {
00233 string::size_type pos = fileName.find("/");
00234 fileName.erase(0, pos+1 );
00235 }
00236 fileName = mDir +fileName + extention;
00237 if (mDebug) cout << "StHbtTTreeReader::initWrite(...) - new fileName: " << fileName.c_str() << endl;
00238
00239 if (mDebug) cout << "StHbtTTreeReader::initWrite(...) - now create file " << endl;
00240 mCurrentFile = new TFile(fileName.c_str(),"RECREATE","hbtTTreeMuDst");
00241 if (!mCurrentFile) {
00242 cout << "StHbtTTreeReader::initWrite(...) - Warning: no file opened = " << endl;
00243 assert(0);
00244 }
00245
00246 mCurrentFile->SetCompressionLevel(comp);
00247
00248
00249 if (mDebug) cout << "StHbtTTreeReader::initWrite(...) - now create tree " << endl;
00250 mTTree = new TTree("Tree", "hbtTTree",split);
00251 if (!mTTree) {
00252 cout << "StHbtTTreeReader::initWrite(...) - Warning: No Tree" << endl;
00253 assert(0);
00254 }
00255
00256 mTTree->SetAutoSave(1000000);
00257
00258 if (mDebug) cout << "StHbtTTreeReader::initWrite(...) - now create branch " << endl;
00259 mTTree->Branch("mHbtTTreeEvent", "StHbtTTreeEvent", &mHbtTTreeEvent, bufsize, split);
00260 mCurrentFileName = ioMakerFileName;
00261 return 0;
00262 }
00263
00264 int StHbtTTreeReader::uninitWrite(){
00265 mTTree->AutoSave();
00266 mCurrentFile->Close();
00267 if (mCurrentFile) delete mCurrentFile;
00268 if (mTTree) delete mTTree;
00269 mTTree = 0;
00270 mCurrentFile = 0;
00271 return 0;
00272 }
00273
00274 StHbtEvent* StHbtTTreeReader::read(){
00275 if (!mTChain) {
00276 try {
00277 cout << initRead(mDir,mFile,mFilter,mExtention,mMaxFiles) << " files to analyse " << endl;
00278 }
00279 catch(StException e) {
00280 e.print();
00281 return 0;
00282 }
00283 }
00284
00285 unsigned int nEvents = (unsigned int)mTChain->GetEntries();
00286 if (!nEvents) throw StException("StHbtTTreeReader::read() - no events to read ");
00287 mHbtTTreeEvent->clear();
00288 int iBytes= mTChain->GetEntry(mEventIndex++);
00289 if (mDebug) cout << "StHbtTTreeReader::read() - bytes read :" << iBytes << endl;
00290 if (nEvents<mEventIndex) throw StExceptionEOF("StHbtTTreeReader::read()");
00291 if (!iBytes) throw StException("StHbtTTreeReader::read() - no event ");
00292 StHbtEvent* hbtEvent = new StHbtEvent(mHbtTTreeEvent);
00293
00294 return hbtEvent;
00295 }
00296
00297 int StHbtTTreeReader::initRead(string dir, string file, string filter, string extention, int mMaxFiles){
00298 mEventIndex =0;
00299 mTChain = new TChain("Tree","hbtTree");
00300 int nFiles =0;
00301 if (file!="") {
00302 if( strstr(file.c_str(),".lis") ) {
00303 try {
00304 nFiles = fillChain(mTChain, (dir+file).c_str(), mMaxFiles);
00305 }
00306 catch(StException e) {
00307 throw e;
00308 }
00309 }
00310 else {
00311 mTChain->Add((dir+file).c_str());
00312 nFiles++;
00313 }
00314 }
00315 else {
00316 try {
00317 nFiles = fillChain(mTChain,dir.c_str(), filter.c_str(), extention.c_str(), mMaxFiles);
00318 }
00319 catch(StException e) {
00320 throw e;
00321 }
00322 } mTChain->SetBranchAddress("mHbtTTreeEvent",&mHbtTTreeEvent);
00323 return nFiles;
00324 }
00325
00326 int StHbtTTreeReader::uninitRead(){
00327 if (mHbtTTreeEvent) delete mHbtTTreeEvent;
00328 if (mTChain) delete mTChain;
00329 mHbtTTreeEvent = 0;
00330 mTChain = 0;
00331 return 0;
00332 }
00333
00334 int StHbtTTreeReader::fillChain(TChain* chain, const char* fileList, const int maxFiles) {
00335 ifstream* inputStream = new ifstream;
00336 inputStream->open(fileList);
00337 if (!(inputStream)) throw StException("StHbtTTreeReader::fillChain(string dir) - can not open directory");
00338 char* temp;
00339 int count=0;
00340 if (mDebug>1) cout << " StHbtTTreeReader::fillChain(...)- inputStream->good() : " << inputStream->good() << endl;
00341 for (;inputStream->good();) {
00342 temp = new char[200];
00343 inputStream->getline(temp,200);
00344 if (mDebug) cout << temp << endl;
00345 chain->Add(temp);
00346 delete temp;
00347 ++count;
00348 if (count>maxFiles) break;
00349 }
00350 delete inputStream;
00351 if (mDebug) cout << "StHbtTTreeReader::(string dir)(string dir) - Added " << count << " files to the chain" << endl;
00352 return count;
00353 }
00354
00355 int StHbtTTreeReader::fillChain(TChain* chain, const char* dir, const char* filter, const char* extention, const int maxFiles) {
00356
00357 void *pDir = gSystem->OpenDirectory(dir);
00358 if(!pDir) throw StException("StHbtTTreeReader::fillChain(string dir) - can not open directory");
00359
00360 const char* fileName(0);
00361 int count(0);
00362 while((fileName = gSystem->GetDirEntry(pDir))){
00363 if(strcmp(fileName,".")==0 || strcmp(fileName,"..")==0) continue;
00364 if(strstr(fileName,filter) && strstr(fileName,extention)) {
00365 char* fullFile = gSystem->ConcatFileName(dir,fileName);
00366
00367 cout << "StHbtTTreeReader::fillChain(string dir) - Adding " << fullFile << " to the chain" << endl;
00368 chain->Add(fullFile);
00369 delete fullFile;
00370 ++count;
00371 if (count>maxFiles) break;
00372 }
00373 }
00374 cout << "StHbtTTreeReader::(string dir)(string dir) - Added " << count << " files to the chain" << endl;
00375 return count;
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425