StHbtBinaryReader.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /***************************************************************************
  2. *
  3. * $Id:
  4. *
  5. * Author: Frank Laue, Ohio State, laue@mps.ohio-state.edu
  6. ***************************************************************************
  7. *
  8. * Description: part of STAR HBT Framework: StHbtMaker package
  9. * This is the HbtEventReader class to be used when reading/writing
  10. * BINARY-based HBT microDSTs
  11. * It is been developted out of a copy of the StHbtAsciiReader class
  12. * on 11/18/1999
  13. *
  14. ***************************************************************************
  15. *
  16. * $Log:
  17. **************************************************************************/
  18. #include "StHbtMaker/Infrastructure/StHbtEvent.hh"
  19. #include "StHbtMaker/Base/StHbtEventCut.h"
  20. #include "StHbtMaker/Base/StHbtTrackCut.h"
  21. #include "StHbtMaker/Base/StHbtV0Cut.h"
  22. #include "StHbtMaker/Base/StHbtKinkCut.h"
  23. #include "StFlowMaker/StFlowMaker.h"
  24. #include "StFlowMaker/StFlowEvent.h"
  25. #include "StFlowAnalysisMaker/StFlowAnalysisMaker.h"
  26. #include "StFlowMaker/StFlowSelection.h"
  27. #include "StHbtMaker/Infrastructure/StHbtIOBinary.hh"
  28. #include "StHbtMaker/Reader/StHbtBinaryReader.h"
  29. #ifdef __ROOT__
  30. ClassImp(StHbtBinaryReader)
  31. #endif
  32. //_______________________________
  33. StHbtBinaryReader::StHbtBinaryReader(const char* dir, const char* file, const char* appendix) {
  34. init(dir, file, appendix);
  35. #ifdef __ROOT__
  36. mIOMaker =0;
  37. #endif
  38. mFlowMaker = 0;
  39. mFlowAnalysisMaker = 0;
  40. }
  41. //_______________________________
  42. #ifdef __ROOT__
  43. StHbtBinaryReader::StHbtBinaryReader(StIOMaker* ioMaker, const char* dir, const char* file, const char* appendix) {
  44. init(dir, file, appendix);
  45. mRetrieve = 1;
  46. mIOMaker = ioMaker;
  47. if (mDebug) cout << " StHbtBinaryReader::StHbtBinaryReader() - mIOMaker : " << mIOMaker << endl;
  48. mFlowMaker = 0;
  49. mFlowAnalysisMaker = 0;
  50. }
  51. #endif
  52. //_______________________________
  53. void StHbtBinaryReader::init(const char* dir, const char* file, const char* appendix) {
  54. if (mDebug) cout << dir << " " << file << " " << appendix << endl;
  55. mDirName=dir;
  56. mFileName=file;
  57. mAppendix=appendix;
  58. if ( strstr(mFileName,".lis") ) {
  59. if (mDebug) cout << " AddFileList " << endl;
  60. AddFileList(file);
  61. }
  62. mReaderStatus = ioOK;
  63. mRetrieve = 0;
  64. mStHbtEventVersion = 2;
  65. mStHbtTrackVersion = 2,
  66. mStHbtV0Version = 3;
  67. mFlowMaker = 0;
  68. mFlowAnalysisMaker = 0;
  69. }
  70. //_______________________________
  71. StHbtBinaryReader::~StHbtBinaryReader(){
  72. if (mFileList) delete mFileList;
  73. }
  74. //_______________________________
  75. StHbtEvent* StHbtBinaryReader::ReturnHbtEvent(){
  76. if (mDebug>1) cout << " StHbtBinaryReader::ReturnHbtEvent() " << endl;
  77. StHbtEvent* event = new StHbtEvent;
  78. if (mReaderStatus == ioOK ) mReaderStatus = binaryIO->read(*event,mStHbtEventVersion,mStHbtTrackVersion,mStHbtV0Version);
  79. if (mReaderStatus != ioOK) {
  80. if (mDebug>1) cout << " StHbtBinaryReader::ReturnHbtEvent() - event read with status " << mReaderStatus << endl;
  81. if (mDebug>1) cout << " StHbtBinaryReader::ReturnHbtEvent() - fileName: " << mFileName << endl;
  82. }
  83. if (mReaderStatus == ioEOF || mReaderStatus == ioERR ) { // end of file reached
  84. if (mFileList) {
  85. while (mReaderStatus !=ioEOL && mReaderStatus !=ioOK ) {
  86. if (event) delete event; event = new StHbtEvent; // in case we read an incomplete event
  87. if ( binaryIO ) delete binaryIO; // this closes the file
  88. mReaderStatus = NextFile(); // write next file from list into mFileName
  89. if (mDebug>1) cout << mReaderStatus << endl;
  90. if (mReaderStatus == ioOK ) mReaderStatus = Init("r",mTheMessage); // instantiate new reader, open file mFileName
  91. if (mReaderStatus == ioOK ) mReaderStatus = binaryIO->read(*event,mStHbtEventVersion,mStHbtTrackVersion,mStHbtV0Version);
  92. }
  93. }
  94. }
  95. if (mReaderStatus != ioOK) {
  96. if (mDebug>1) cout << " StHbtBinaryReader::ReturnHbtEvent() - event read with status " << mReaderStatus << endl;
  97. if (mDebug>1) cout << " StHbtBinaryReader::ReturnHbtEvent() - fileName: " << mFileName << endl;
  98. if (event) delete event; event=0;// we do not return events when reader status is not ioOk
  99. return 0;
  100. }
  101. // parse event throu event cut
  102. if (mEventCut && event ) {
  103. if ( mEventCut->Pass(event)==0 ) {
  104. delete event;
  105. event=0;
  106. }
  107. }
  108. // Pass through track cut if there is one
  109. if( mTrackCut && event){
  110. StHbtTrackIterator pIter;
  111. StHbtTrack* pParticle;
  112. StHbtTrackCollection NewTrackCollection;
  113. for (pIter=event->TrackCollection()->begin();
  114. pIter!=event->TrackCollection()->end();pIter++){
  115. pParticle = *pIter;
  116. bool tmpPassParticle = mTrackCut->Pass(pParticle);
  117. if (tmpPassParticle){
  118. NewTrackCollection.push_back(pParticle);
  119. }
  120. else{
  121. delete *pIter;
  122. }
  123. }
  124. event->TrackCollection()->clear();
  125. for (pIter=NewTrackCollection.begin();
  126. pIter!=NewTrackCollection.end();pIter++){
  127. event->TrackCollection()->push_back(*pIter);
  128. }
  129. NewTrackCollection.clear();
  130. }
  131. //Pass through v0 cut if there is one
  132. if( mV0Cut && event){
  133. StHbtV0Iterator pIter;
  134. StHbtV0* pParticle;
  135. StHbtV0Collection NewV0Collection;
  136. for (pIter=event->V0Collection()->begin();
  137. pIter!=event->V0Collection()->end();pIter++){
  138. pParticle = *pIter;
  139. bool tmpPassParticle = mV0Cut->Pass(pParticle);
  140. if (tmpPassParticle){
  141. NewV0Collection.push_back(pParticle);
  142. }
  143. else{
  144. delete *pIter;
  145. }
  146. }
  147. event->V0Collection()->clear();
  148. for (pIter=NewV0Collection.begin();
  149. pIter!=NewV0Collection.end();pIter++){
  150. event->V0Collection()->push_back(*pIter);
  151. }
  152. NewV0Collection.clear();
  153. }
  154. // to get RP from FlowMaker
  155. if ( mFlowMaker && event ) {
  156. // mFlowMaker->FillFlowEvent(event);
  157. // // First get RP for whole event
  158. // mFlowMaker->FlowSelection()->SetSubevent(-1);
  159. // double reactionPlane = mFlowMaker->FlowEventPointer()->Psi(mFlowMaker->FlowSelection());
  160. // cout << "Reaction Plane " << reactionPlane << endl;
  161. // event->SetReactionPlane(reactionPlane);
  162. // // Sub event RPs
  163. // mFlowMaker->FlowSelection()->SetSubevent(0);
  164. // double RP1 = mFlowMaker->FlowEventPointer()->Psi(mFlowMaker->FlowSelection());
  165. // mFlowMaker->FlowSelection()->SetSubevent(1);
  166. // double RP2 = mFlowMaker->FlowEventPointer()->Psi(mFlowMaker->FlowSelection());
  167. // event->SetReactionPlaneSubEventDifference(RP1-RP2);
  168. // if (mFlowAnalysisMaker) mFlowAnalysisMaker->Make();
  169. }
  170. if (mDebug>1) {
  171. cout << " StHbtBinaryReader::ReturnHbtEvent() - current filename: " << mFileName << endl;
  172. cout << " StHbtBinaryReader::ReturnHbtEvent() - bytes read : " << binaryIO->bytesRead() << endl;
  173. if (event){
  174. cout << " StHbtBinaryReader::ReturnHbtEvent() - #tracks/#V0s : " << event->TrackCollection()->size() << "/";
  175. cout << event->V0Collection()->size() << endl;
  176. }
  177. else{
  178. cout << "StHbtBinaryReader::ReturnHbtEvent() - failed cut - no StHbtEvent returned" << endl;
  179. }
  180. }
  181. return event;
  182. }
  183. //_______________________________
  184. int StHbtBinaryReader::WriteHbtEvent(StHbtEvent* event){
  185. #ifdef __ROOT__
  186. if (mIOMaker && mRetrieve==1) {
  187. if ( strcmp(mCurrentFile.c_str(),mIOMaker->GetFile()) ) {
  188. if (mDebug) {
  189. cout << " StHbtBinaryReader::WriteHbtEvent(StHbtEvent* event) " << endl;
  190. cout << " current file : " << mCurrentFile.c_str() << endl;
  191. cout << " new file : " << mIOMaker->GetFile() << endl;
  192. }
  193. mCurrentFile = mIOMaker->GetFile();
  194. mFileName = (mCurrentFile).c_str();
  195. if (mDebug) cout << " open file : " << mFileName << endl;
  196. if ( binaryIO ) delete binaryIO; // this closes the file
  197. mReaderStatus = Init("w",mTheMessage); // instantiate new writer, open file <mFileName>
  198. }
  199. }
  200. #endif
  201. if (mReaderStatus == ioOK ) { // > means o.k (number of bytes)
  202. if (!mEventCut || mEventCut->Pass(event)) {
  203. if (mDebug>1) cout << " StHbtBinaryReader::WriteHbtEvent(StHbtEvent* event) - eventCut passed" << endl;
  204. // StHbtEvent* newEvent = new StHbtEvent(*event, mTrackCut, mV0Cut); // apply cuts while copying event
  205. // mReaderStatus = binaryIO->write(*newEvent,mStHbtEventVersion,mStHbtTrackVersion,mStHbtV0Version);
  206. // delete newEvent;
  207. StHbtEvent newEvent(*event, mTrackCut, mV0Cut); // apply cuts while copying event
  208. mReaderStatus = binaryIO->write(newEvent,mStHbtEventVersion,mStHbtTrackVersion,mStHbtV0Version);
  209. }
  210. }
  211. if (mReaderStatus != ioOK) { // > means o.k (number of bytes)
  212. if (mDebug) cout << " StHbtBinaryReader::WriteHbtEvent(StHbtEvent* event) - error# ";
  213. if (mDebug) cout << mReaderStatus << " while writing" << endl;
  214. }
  215. if (mDebug>1) cout << " StHbtBinaryReader::WriteHbtEvent(StHbtEvent* event) - bytes written : " << binaryIO->bytesWritten() << endl;
  216. return (mReaderStatus);
  217. }
  218. //_______________________________
  219. int StHbtBinaryReader::Init(const char* ReadWrite, StHbtString& Message){
  220. if (mDebug>1) cout << " StHbtBinaryReader::Init(const char* ReadWrite, StHbtString& Message) - being called with filename: ";
  221. cout << mFileName << endl;
  222. mReaderStatus = ioOK;
  223. if (((*ReadWrite)=='r')|| ((*ReadWrite)=='R')){ // this object will be a reader
  224. binaryIO = new StHbtIOBinary( mDirName,mFileName, mAppendix ,"r"); // create input object and open file
  225. if (mDebug>1) cout << " StHbtBinaryReader::Init() - inputStreamStatus = " << binaryIO->inputStreamStatus();
  226. if (mDebug>1) cout << " StHbtBinaryReader::Init() - now read message " << endl;
  227. mReaderStatus = binaryIO->readHeader(Message); // read file header
  228. if (mTheMessage!=Message) {
  229. mTheMessage = Message;
  230. cout << Message.c_str() << endl;
  231. }
  232. if (mDebug>1) cout << " mReaderStatus " << binaryIO->outputStreamStatus() << endl;
  233. if (mDebug>1) cout << " StHbtBinaryReader::Init() - now read versions " << endl;
  234. mReaderStatus = binaryIO->read(mStHbtEventVersion);
  235. if (mDebug>1) cout << " mReaderStatus " << binaryIO->outputStreamStatus();
  236. mReaderStatus = binaryIO->read(mStHbtTrackVersion);
  237. if (mDebug>1) cout << " mReaderStatus " << binaryIO->outputStreamStatus();
  238. mReaderStatus = binaryIO->read(mStHbtV0Version);
  239. if (mDebug>1) {
  240. cout << " mReaderStatus " << binaryIO->outputStreamStatus();
  241. cout << " StHbtEventVersion=" << mStHbtEventVersion;
  242. cout << " StHbtTrackVersion=" << mStHbtTrackVersion;
  243. cout << " StHbtV0Version=" << mStHbtV0Version << endl;
  244. }
  245. }
  246. else{ // this object will be a writer
  247. mTheMessage = Message;
  248. binaryIO = new StHbtIOBinary(mDirName, mFileName, mAppendix,"w"); // create output object and open file
  249. if (mDebug>1) cout << " mReaderStatus " << binaryIO->outputStreamStatus();
  250. mReaderStatus = binaryIO->writeHeader(Message); // output file header (Message);
  251. if (mDebug>1) cout << " mReaderStatus " << mReaderStatus << endl;
  252. mReaderStatus = binaryIO->write(mStHbtEventVersion);
  253. if (mDebug>1) cout << " mReaderStatus " << mReaderStatus << endl;
  254. mReaderStatus = binaryIO->write(mStHbtTrackVersion);
  255. if (mDebug>1) cout << " mReaderStatus " << mReaderStatus << endl;
  256. mReaderStatus = binaryIO->write(mStHbtV0Version);
  257. if (mDebug>1) {
  258. cout << " mReaderStatus " << mReaderStatus << endl;
  259. cout << " StHbtEventVersion=" << mStHbtEventVersion;
  260. cout << " StHbtTrackVersion=" << mStHbtTrackVersion;
  261. cout << " StHbtV0Version=" << mStHbtV0Version << endl;
  262. }
  263. }
  264. if (mDebug>1) cout << " StHbtBinaryReader::Init(const char* ReadWrite, StHbtString& Message) - mReaderStatus: " << mReaderStatus << endl;
  265. return mReaderStatus;
  266. }
  267. //_______________________________
  268. void StHbtBinaryReader::Finish(){
  269. }
  270. //_______________________________
  271. int StHbtBinaryReader::NextFile() {
  272. mFileName="\0";
  273. delete (mFileList->front()); // remove current file from list
  274. mFileList->pop_front(); // remove current file from list
  275. if ( mFileList->empty() ) return ioEOL;
  276. mFileName = mFileList->front()->c_str(); // get next file
  277. if (mDebug) cout << " StHbtBinaryReader::NextFile() - mFileName: " << mFileName << endl;
  278. return ioOK;
  279. }
  280. //_______________________________
  281. void StHbtBinaryReader::SetFileName(const char* file){mFileName=(char*)file;}
  282. void StHbtBinaryReader::SetDirName(const char* dir){mDirName=(char*)dir;}
  283. void StHbtBinaryReader::SetAppendix(const char* appendix){mAppendix=(char*)appendix;}
  284. //_______________________________
  285. void StHbtBinaryReader::AddFileList(const char* fileList) {
  286. if (mDebug>1) cout << " StHbtBinaryReader::AddFileList(char* fileList)"<< endl;
  287. if (!mFileList) mFileList = new fileCollection;
  288. ifstream* inputStream = new ifstream;
  289. inputStream->open(fileList);
  290. if (!(inputStream)){
  291. if (mDebug) cout << " StHbtBinaryReader::AddFileList(char* fileList) - Cannot open input file! " << endl;
  292. mReaderStatus = ioERROpen;
  293. return;
  294. }
  295. char* temp;
  296. if (mDebug>1) cout << " inputStream->good() : " << inputStream->good() << endl;
  297. for (;inputStream->good();) {
  298. temp = new char[200];
  299. inputStream->getline(temp,200);
  300. cout << temp << endl;
  301. StHbtString* newFile = new StHbtString(temp);
  302. if ( newFile->length()>0 ) {
  303. mFileList->push_back(newFile);
  304. if (mDebug>1) cout << " file " << newFile->c_str() << " added to file list " << endl;
  305. }
  306. }
  307. if (mDebug) cout << " StHbtBinaryReader::FillFileList(char* fileList) - " << mFileList->empty() << " files in list " << endl;
  308. if (!mFileList->empty())
  309. mFileName = mFileList->front()->c_str();
  310. }
  311. //__________________
  312. StHbtString StHbtBinaryReader::Report(){
  313. StHbtString temp = "\n This is the StHbtBinaryEventReader\n";
  314. temp += "---> EventCuts in Reader: ";
  315. if (mEventCut) {
  316. temp += mEventCut->Report();
  317. }
  318. else {
  319. temp += "NONE";
  320. }
  321. temp += "\n---> TrackCuts in Reader: ";
  322. if (mTrackCut) {
  323. temp += mTrackCut->Report();
  324. }
  325. else {
  326. temp += "NONE";
  327. }
  328. temp += "\n---> V0Cuts in Reader: ";
  329. if (mV0Cut) {
  330. temp += mV0Cut->Report();
  331. }
  332. else {
  333. temp += "NONE";
  334. }
  335. temp += "\n";
  336. return temp;
  337. }