StHbtAsciiReader.cxx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /***************************************************************************
  2. *
  3. * $Id:
  4. *
  5. * Author: Mike Lisa, Ohio State, lisa@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. * ASCII-based HBT microDSTs
  11. *
  12. ***************************************************************************
  13. *
  14. * $Log:
  15. **************************************************************************/
  16. #include <Stiostream.h>
  17. #include "Stiostream.h"
  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 "StHbtMaker/Reader/StHbtAsciiReader.h"
  24. #ifdef __ROOT__
  25. ClassImp(StHbtAsciiReader)
  26. #endif
  27. //_______________________________
  28. StHbtAsciiReader::StHbtAsciiReader() : mInputStream(0), mOutputStream(0){
  29. mFileName = "HbtAsciiFile"; // default name
  30. mReaderStatus = 0; // means "good"
  31. mStHbtEventVersion = mStHbtTrackVersion = mStHbtV0Version = 1;
  32. }
  33. //_______________________________
  34. StHbtAsciiReader::StHbtAsciiReader(char* file) : mInputStream(0), mOutputStream(0), mFileName(file)
  35. {
  36. mReaderStatus = 0; // means "good"
  37. mStHbtEventVersion = mStHbtTrackVersion = mStHbtV0Version = 1;
  38. }
  39. //_______________________________
  40. StHbtAsciiReader::~StHbtAsciiReader(){
  41. if (!mInputStream){
  42. delete mInputStream;
  43. mInputStream = 0;
  44. }
  45. if (!mOutputStream){
  46. delete mOutputStream;
  47. mOutputStream = 0;
  48. }
  49. }
  50. //_______________________________
  51. StHbtEvent* StHbtAsciiReader::ReturnHbtEvent(){
  52. if (!mInputStream){
  53. cout << "StHbtAsciiReader::ReturnHbtEvent() - there is no input stream!";
  54. mReaderStatus = 1; // 0 means "good"
  55. return (0);
  56. }
  57. if (!(*mInputStream)){
  58. cout << "StHbtAsciiReader::ReturnHbtEvent() - input stream in bad state!" << endl;
  59. cout << "State is " << mInputStream->rdstate() << endl;
  60. mReaderStatus = 1; // 0 means "good"
  61. return (0);
  62. }
  63. StHbtEvent* event = new StHbtEvent;
  64. (*mInputStream) >> (*event);
  65. if (!(mInputStream->good())){
  66. cout << "StHbtAsciiReader::ReturnHbtEvent() - input stream in bad state!" <<endl;
  67. cout << "State is " << mInputStream->rdstate() << endl;
  68. mReaderStatus = 1; // 0 means "good"
  69. return (0);
  70. //mInputStream->clear();
  71. }
  72. return event;
  73. }
  74. //_______________________________
  75. int StHbtAsciiReader::WriteHbtEvent(StHbtEvent* event){
  76. if (!mOutputStream){ // checks for existence of stream
  77. cout << "\n StHbtAsciiReader::WriteHbtEvent() - There is no Output Stream -- I am NOT writing !!! \n\n";
  78. return (1);
  79. }
  80. if (!(*mOutputStream)){ // checks for good state of stream
  81. cout << "\n StHbtAsciiReader::WriteHbtEvent() - Output Stream in bad state -- I am NOT writing !!! \n\n";
  82. return (2);
  83. }
  84. if ( !mEventCut || mEventCut->Pass(event) ) {
  85. cout << "StHbtAsciiReader: eventCut passed" << endl;
  86. StHbtEvent newEvent(*event, mTrackCut, mV0Cut);
  87. (*mOutputStream) << (newEvent);
  88. }
  89. return (0);
  90. }
  91. //_______________________________
  92. //StHbtString StHbtAsciiReader::Report(){
  93. // StHbtString temp = "\n This is StHbtAsciiReader calling the base class Report()";
  94. /// temp += this->Report();
  95. // return temp;
  96. //}
  97. //_______________________________
  98. int StHbtAsciiReader::Init(const char* ReadWrite, StHbtString& Message){
  99. cout << " *\n *\n *\n StHbtAsciiReader::Init() being called*\n *\n";
  100. mReaderStatus = 0; // means "good"
  101. // if ((ReadWrite=="r")|| (ReadWrite=="R")){ // this object will be a reader
  102. if (((*ReadWrite)=='r')|| ((*ReadWrite)=='R')){ // this object will be a reader
  103. mInputStream = new ifstream;
  104. mInputStream->open(mFileName);
  105. if (!(*mInputStream)){
  106. cout << "StHbtAsciiReader::Init() - Cannot open input file! " << endl;
  107. return (1);
  108. }
  109. cout << "StHbtAsciiReader::Init() - being configured as a Reader" << ReadWrite << endl;
  110. // extract Input reader Report...
  111. char temp[200] = "";
  112. string stemp;
  113. do {
  114. Message += temp;
  115. Message += "\n";
  116. mInputStream->getline(temp,200);
  117. stemp = temp;
  118. } while (stemp != "-*-*-*-* End of Input Reader Report");
  119. cout << "Here is the message that was at the beginning of the file...\n";
  120. cout << Message.c_str();
  121. (*mInputStream) >> mStHbtEventVersion >> mStHbtTrackVersion >> mStHbtV0Version;
  122. cout << " StHbtEventVersion=" << mStHbtEventVersion;
  123. cout << " StHbtTrackVersion=" << mStHbtTrackVersion;
  124. cout << " StHbtV0Version=" << mStHbtV0Version << endl;
  125. }
  126. else{ // this object will be a writer
  127. mOutputStream = new ofstream;
  128. // mOutputStream->open(mFileName,ios::noreplace); // that last bit means do NOT overwrite files...
  129. // (But then it doesn't work???)
  130. mOutputStream->open(mFileName);
  131. if (!(*mOutputStream)){
  132. cout << "StHbtAsciiReader::Init() - Cannot open output file! " << endl;
  133. return (1);
  134. }
  135. cout << "StHbtAsciiReader::Init() - being configured as a Writer" << ReadWrite << endl;
  136. (*mOutputStream) << Message.c_str();
  137. (*mOutputStream) << endl;
  138. (*mOutputStream) << "-*-*-*-* End of Input Reader Report" << endl; // write THIS out even if there is no report
  139. (*mOutputStream) << mStHbtEventVersion << " " << mStHbtTrackVersion << " " << mStHbtV0Version << endl;
  140. }
  141. return (0);
  142. }
  143. //_______________________________
  144. void StHbtAsciiReader::Finish(){
  145. if (mInputStream) mInputStream->close();
  146. if (mOutputStream) mOutputStream->close();
  147. }