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