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