8 #include "StHbtMaker/Infrastructure/StHbtIOBinary.hh"
11 StHbtIOBinary::StHbtIOBinary(
const char* dirName,
const char* fileName,
const char* appendix,
const char* readWrite)
12 : mDebug(0), mOStream(0), mIStream(0) {
16 if (((*readWrite)==
'w')|| ((*readWrite)==
'W')){
17 mOStream =
new ofstream( parseDirFile(dirName, fileName, appendix) );
19 cout <<
"StHbtIOBinary::Init() - Cannot open output file! " << endl;
22 cout <<
"StHbtIOBinary::Init() - being configured as a Writer " << readWrite << endl;
25 mIStream =
new ifstream( parseDirFile( dirName, fileName, appendix) );
27 cout <<
"StHbtIOBinary::Init() - Cannot open input file! " << endl;
30 cout << fileName <<
" open " << endl;
31 cout <<
"StHbtIOBinary::Init() - being configured as a Reader " << readWrite << endl;
35 StHbtIOBinary::~StHbtIOBinary() {
36 cout <<
" StHbtIOBinary::~StHbtIOBinary() " << endl;
47 int StHbtIOBinary::readHeader(StHbtString& header){
48 return readString( header );
51 int StHbtIOBinary::writeHeader(
const StHbtString& header){
52 return writeString( header );
55 int StHbtIOBinary::bytesWritten() {
return byteCounterTotal; }
56 int StHbtIOBinary::bytesRead() {
return byteCounterTotal; }
59 const char* StHbtIOBinary::parseDirFile(
const char* dir,
const char* file,
const char* appendix) {
60 if (dir) cout <<
" StHbtIOBinary::parseDirFile() - dir " << dir << endl;
61 if (file) cout <<
" StHbtIOBinary::parseDirFile() - file " << file << endl;
62 if (appendix) cout <<
" StHbtIOBinary::parseDirFile() - appendix " << appendix << endl;
64 if (!dir || !appendix)
return file;
65 StHbtString theDir = (
char*)dir;
66 StHbtString theFile = (
char*)file;
67 StHbtString theAppendix = (
char*)appendix;
68 while ( theFile.find(
"/") != string::npos ) {
70 cout << theFile.c_str() <<
" ";
72 string::size_type pos = theFile.find(
"/");
76 theFile.erase(0, pos+1 );
78 cout << theFile.c_str() << endl;
81 if(mDebug) cout <<
" StHbtIOBinary::parseDirFile() ---" << (theDir+theFile+theAppendix).c_str() <<
"---" << endl;
82 return (theDir+theFile+theAppendix).c_str();
85 #include "StHbtMaker/Infrastructure/StHbtTypes.hh"
86 #include "SystemOfUnits.h"
88 int StHbtIOBinary::readTrackList(
StHbtEvent& ev,
unsigned short trVersion){
90 ev.TrackCollection()->clear();
91 colSizeType NtracksInCollection;
92 iret = read(NtracksInCollection);
93 if(mDebug) cout <<
" reading " << NtracksInCollection <<
" tracks " << endl;
94 if (NtracksInCollection > 1e6) {
95 for (
int i=0; i<10; i++) {
96 cout <<
" StHbtIOBinaryReader::readTrackList(...) - unreasonable number of tracks, returning ioERR " << endl;
102 for (colSizeType itrk=0; itrk <NtracksInCollection; itrk++){
104 iret = read( *trk, trVersion);
105 ev.TrackCollection()->push_back(trk);
107 cout <<
" track read " << *trk << endl;
108 cout <<
" " << itrk <<
"/" << NtracksInCollection;
109 cout <<
" track pushed " << endl;
115 int StHbtIOBinary::writeTrackList(
const StHbtEvent& ev,
unsigned short trVersion){
117 colSizeType colSize = (colSizeType) ev.TrackCollection()->size();
118 iret = write( colSize );
119 for (StHbtTrackIterator iter=ev.TrackCollection()->begin(); iter != ev.TrackCollection()->end(); iter++){
120 iret = write( **iter, trVersion);
125 int StHbtIOBinary::readV0List(
StHbtEvent& ev,
unsigned short v0Version){
127 ev.V0Collection()->clear();
128 colSizeType NV0sInCollection;
129 iret = read( NV0sInCollection);
130 if (mDebug) cout <<
" reading " << NV0sInCollection <<
" V0s " << endl;
131 if ( !(mIStream->good()) ) {
132 cout <<
"StHbtEvent input operator finds stream in bad state ! " << endl;
135 if (NV0sInCollection > 1e6) {
136 for (
int i=0; i<10; i++) {
137 cout <<
" StHbtIOBinaryReader::readV0List(...) - unreasonable number of V0s, returning ioERR " << endl;
141 for (
unsigned int iv0=0; iv0<NV0sInCollection; iv0++){
143 iret = read( *v0, v0Version);
144 ev.V0Collection()->push_back(v0);
148 int StHbtIOBinary::writeV0List(
const StHbtEvent& ev,
unsigned short v0Version){
150 colSizeType colSize = (colSizeType) ev.V0Collection()->size();
151 iret = write( colSize );
152 for (StHbtV0Iterator iterv0=ev.V0Collection()->begin(); iterv0 != ev.V0Collection()->end(); iterv0++){
153 iret = write( **iterv0, v0Version);
160 int StHbtIOBinary::writeString(
const StHbtString& Message){
161 *mOStream << Message.c_str();
163 *mOStream <<
"-*-*-*-* End of Input Reader Report" << endl;
164 byteCounterEvent += Message.size();
165 return (!mOStream->good());
167 int StHbtIOBinary::readString(StHbtString& Message){
174 mIStream->getline(temp,200);
177 }
while (stemp !=
"-*-*-*-* End of Input Reader Report" && mIStream->good() );
178 cout <<
"Here is the message that was at the beginning of the file...\n";
179 cout << Message.c_str();
180 byteCounterEvent += Message.size();
181 return (!mIStream->good());
185 int StHbtIOBinary::read(
StHbtEvent& ev,
unsigned short evVersion,
unsigned short trVersion,
unsigned short v0Version){
186 if (mDebug) cout <<
" StHbtIOBinary::read(StHbtEvent& ev, ...) - Versions: " << evVersion <<
" " << trVersion <<
" " << v0Version << endl;
188 byteCounterEvent = 0;
189 switch ( (
int)evVersion ) {
190 case 0: iret = read_V0( ev, trVersion, v0Version);
break;
191 case 1: iret = read_V1( ev, trVersion, v0Version);
break;
192 case 2: iret = read_V2( ev, trVersion, v0Version);
break;
195 cout <<
" StHbtIOBinary::read(StHbtEvent& ev, ...) - can not read this event version " << endl;
198 byteCounterTotal += byteCounterEvent;
199 if (mDebug) cout <<
" StHbtIOBinary::read(StHbtEvent& ev, ...) - byteCounterTotal= " << byteCounterTotal <<
" byteCounterEvent = " << byteCounterEvent << endl;
204 int StHbtIOBinary::write(
const StHbtEvent& ev,
unsigned short evVersion,
unsigned short trVersion,
unsigned short v0Version){
205 if (mDebug) cout <<
" StHbtIOBinary::write(StHbtEvent& ev, ...) - Versions: " << evVersion <<
" " << trVersion <<
" " << v0Version << endl;
207 byteCounterEvent = 0;
208 switch ( (
int)evVersion ) {
209 case 0: iret = write_V0( ev, trVersion, v0Version);
break;
210 case 1: iret = write_V1( ev, trVersion, v0Version);
break;
211 case 2: iret = write_V2( ev, trVersion, v0Version);
break;
214 cout <<
" StHbtIOBinary::write(StHbtEvent& ev, ...) - can not write this event version " << endl;
217 byteCounterTotal += byteCounterEvent;
218 if (mDebug) cout <<
" StHbtIOBinary::write(StHbtEvent& ev, ...) - byteCounterTotal= " << byteCounterTotal <<
" byteCounterEvent = " << byteCounterEvent << endl;
223 int StHbtIOBinary::read(
StHbtTrack& x,
unsigned short version){
224 if (mDebug>1) cout <<
" StHbtIOBinary::readTrack() - Version : " << version << endl;
226 switch ( (
int)version ) {
228 case 1: iret = read_V1( x );
break;
229 case 2: iret = read_V2( x );
break;
231 cout <<
" can not write this track version " << endl;
239 int StHbtIOBinary::write(
const StHbtTrack& x,
unsigned short version){
240 if (mDebug>1) cout <<
" StHbtIOBinary::writeTrack() - Version : " << version << endl;
242 switch ( (
int)version ) {
244 case 1: iret = write_V1( x );
break;
245 case 2: iret = write_V2( x );
break;
247 cout <<
" can not write this track version " << endl;
254 int StHbtIOBinary::read(
StHbtV0& x,
unsigned short version){
255 if (mDebug>1) cout <<
" StHbtIOBinary::readV0(...) - Version : " << version << endl;
257 switch ( (
int)version ) {
259 case 1: iret = read_V1( x );
break;
260 case 2: iret = read_V2( x);
break;
261 case 3: iret = read_V3( x);
break;
263 cout <<
" can not read this V0 version " << endl;
271 int StHbtIOBinary::write(
const StHbtV0& x,
unsigned short version){
272 if (mDebug>0) cout <<
" StHbtIOBinary::writeV0(...) - Version : " << version << endl;
274 switch ( (
int)version ) {
276 case 1: iret = write_V1( x );
break;
277 case 2: iret = write_V2( x );
break;
278 case 3: iret = write_V3( x );
break;
280 cout <<
" can not write this V0 version " << endl;
292 int StHbtIOBinary::read_V0(
StHbtEvent& ev,
unsigned short trVersion,
unsigned short v0Version ) {
293 if (mDebug) cout <<
" StHbtIOBinary::read_V0(StHbtEvent& event, unsigned short trVersion, unsigned short v0Version )" << endl;
296 iret = read( ev.mEventNumber);
297 if (mIStream->eof()) {
298 cout <<
"Hit end of file " << endl;
301 iret = read( ev.mCtbMultiplicity );
302 iret = read( ev.mZdcAdc[0]);
303 iret = read( ev.mZdcAdc[1]);
304 iret = read( ev.mTpcNhits);
305 iret = read( ev.mNumberOfTracks);
306 iret = read( ev.mNumberOfGoodTracks);
307 iret = read( ev.mReactionPlane[0]);
308 iret = read( ev.mReactionPlane[1]);
309 iret = read( idummy );
310 iret = read( idummy );
311 iret = read( idummy );
312 iret = read( ev.mPrimVertPos);
317 if (!(mIStream->good())){
318 cout <<
"StHbtEvent input operator finds stream in bad state ! " << endl;
322 iret = readTrackList(ev,trVersion);
if (iret!=ioOK)
return iret;
323 iret = readV0List(ev,v0Version);
if (iret!=ioOK)
return iret;
326 int StHbtIOBinary::write_V0(
const StHbtEvent& ev,
unsigned short trVersion,
unsigned short v0Version){
327 if (mDebug) cout <<
" StHbtIOBinary::write_V0(const StHbtEvent& ev, unsigned short trVersion, unsigned short v0Version) " << endl;
331 iret = write( ev.mEventNumber );
332 iret = write( ev.mCtbMultiplicity );
333 iret = write( ev.mZdcAdc[0] );
334 iret = write( ev.mZdcAdc[1] );
335 iret = write( ev.mTpcNhits );
336 iret = write( ev.mNumberOfTracks );
337 iret = write( ev.mNumberOfGoodTracks );
338 iret = write( ev.mReactionPlane[0] );
339 iret = write( ev.mReactionPlane[1] );
340 iret = write( idummy );
341 iret = write( idummy );
342 iret = write( idummy );
343 iret = write( ev.mPrimVertPos );
345 iret = writeTrackList(ev,trVersion);
346 iret = writeV0List(ev,v0Version);
349 int StHbtIOBinary::read_V1(
StHbtEvent& ev,
unsigned short trVersion,
unsigned short v0Version ) {
350 if (mDebug) cout <<
" StHbtIOBinary::read_V1(StHbtEvent& event, unsigned short trVersion, unsigned short v0Version )" << endl;
352 iret = read( ev.mEventNumber);
353 if (mIStream->eof()) {
354 cout <<
" StHbtIOBinary::read_V1(...) - Hit end of file " << endl;
357 iret = read( ev.mCtbMultiplicity );
358 iret = read( ev.mZdcAdc[0]);
359 iret = read( ev.mZdcAdc[1]);
360 iret = read( ev.mTpcNhits);
361 iret = read( ev.mNumberOfTracks);
362 iret = read( ev.mNumberOfGoodTracks);
363 iret = read( ev.mReactionPlane[0]);
364 iret = read( ev.mReactionPlane[1]);
365 iret = read( ev.mPrimVertPos);
370 if (!(mIStream->good())){
371 cout <<
" StHbtIOBinary::read_V1(...) - StHbtEvent input operator finds stream in bad state ! " << endl;
375 iret = readTrackList(ev,trVersion);
if (iret!=ioOK)
return iret;
376 iret = readV0List(ev,v0Version);
if (iret!=ioOK)
return iret;
379 int StHbtIOBinary::write_V1(
const StHbtEvent& ev,
unsigned short trVersion,
unsigned short v0Version){
380 if (mDebug) cout <<
" StHbtIOBinary::write_V1(const StHbtEvent& ev, unsigned short trVersion, unsigned short v0Version) " << endl;
383 iret = write( ev.mEventNumber );
384 iret = write( ev.mCtbMultiplicity );
385 iret = write( ev.mZdcAdc[0] );
386 iret = write( ev.mZdcAdc[1] );
387 iret = write( ev.mTpcNhits );
388 iret = write( ev.mNumberOfTracks );
389 iret = write( ev.mNumberOfGoodTracks );
390 iret = write( ev.mReactionPlane[0] );
391 iret = write( ev.mReactionPlane[1] );
392 iret = write( ev.mPrimVertPos );
394 iret = writeTrackList(ev,trVersion);
395 iret = writeV0List(ev,v0Version);
404 int StHbtIOBinary::read_V2(
StHbtEvent& ev,
unsigned short trVersion,
unsigned short v0Version ) {
405 if (mDebug) cout <<
" StHbtIOBinary::read_V2(StHbtEvent& event, unsigned short trVersion, unsigned short v0Version )" << endl;
407 iret = read( ev.mEventNumber);
408 if (mIStream->eof()) {
409 cout <<
"Hit end of file " << endl;
412 iret = read( ev.mCtbMultiplicity );
413 iret = read( ev.mZdcAdc[0]);
414 iret = read( ev.mZdcAdc[1]);
415 iret = read( ev.mTpcNhits);
416 iret = read( ev.mNumberOfTracks);
417 iret = read( ev.mNumberOfGoodTracks);
418 iret = read( ev.mUncorrectedNumberOfPositivePrimaries);
419 iret = read( ev.mUncorrectedNumberOfNegativePrimaries);
420 iret = read( ev.mReactionPlane[0]);
421 iret = read( ev.mReactionPlane[1]);
422 iret = read( ev.mPrimVertPos);
427 if (!(mIStream->good())){
428 cout <<
"StHbtEvent input operator finds stream in bad state ! " << endl;
432 iret = readTrackList(ev,trVersion);
if (iret!=ioOK)
return iret;
433 iret = readV0List(ev,v0Version);
if (iret!=ioOK)
return iret;
436 int StHbtIOBinary::write_V2(
const StHbtEvent& ev,
unsigned short trVersion,
unsigned short v0Version){
437 if (mDebug) cout <<
" StHbtIOBinary::write_V2(const StHbtEvent& ev, unsigned short trVersion, unsigned short v0Version)" << endl;
440 iret = write( ev.mEventNumber );
441 iret = write( ev.mCtbMultiplicity );
442 iret = write( ev.mZdcAdc[0] );
443 iret = write( ev.mZdcAdc[1] );
444 iret = write( ev.mTpcNhits );
445 iret = write( ev.mNumberOfTracks );
446 iret = write( ev.mNumberOfGoodTracks );
447 iret = write( ev.mUncorrectedNumberOfPositivePrimaries);
448 iret = write( ev.mUncorrectedNumberOfNegativePrimaries);
449 iret = write( ev.mReactionPlane[0] );
450 iret = write( ev.mReactionPlane[1] );
451 iret = write( ev.mPrimVertPos );
453 iret = writeTrackList(ev,trVersion);
454 iret = writeV0List(ev,v0Version);
462 if (mDebug>1) cout <<
" StHbtIOBinary::read_V1(StHbtTrack&) " << endl;
465 int StHbtIOBinary::write_V1(
const StHbtTrack& x){
466 if (mDebug>1) cout <<
" StHbtIOBinary::write_V1(const StHbtTrack&)" << endl;
475 iret = read( x.mCharge );
476 iret = read( x.mNHits );
477 iret = read( x.mNHitsPoss );
478 iret = read( x.mNSigmaElectron );
479 iret = read( x.mNSigmaPion );
480 iret = read( x.mNSigmaKaon );
481 iret = read( x.mNSigmaProton );
482 iret = read( x.mdEdx );
483 iret = read( x.mDCAxy );
484 iret = read( x.mDCAz );
485 iret = read( x.mChiSqXY );
486 iret = read( x.mChiSqZ );
487 iret = read( x.mMap[0] );
488 iret = read( x.mMap[1] );
489 iret = read( x.mTrackId );
490 iret = read( x.mPt );
492 iret = read( x.mHelix );
495 int StHbtIOBinary::write_V2(
const StHbtTrack& x) {
497 iret = write( x.mCharge );
498 iret = write( x.mNHits );
499 iret = write( x.mNHitsPoss );
500 iret = write( x.mNSigmaElectron );
501 iret = write( x.mNSigmaPion );
502 iret = write( x.mNSigmaKaon );
503 iret = write( x.mNSigmaProton );
504 iret = write( x.mdEdx );
505 iret = write( x.mDCAxy );
506 iret = write( x.mDCAz );
507 iret = write( x.mChiSqXY );
508 iret = write( x.mChiSqZ );
509 iret = write( x.mMap[0] );
510 iret = write( x.mMap[1] );
511 iret = write( x.mTrackId );
512 iret = write( x.mPt );
513 iret = write( x.mP );
514 iret = write( x.mHelix );
521 int StHbtIOBinary::read_V1(
StHbtV0& x) {
522 if (mDebug>1) cout <<
" StHbtIOBinary::read_V1(StHbtV0&) " << endl;
525 int StHbtIOBinary::write_V1(
const StHbtV0& x){
526 if (mDebug>1) cout <<
" StHbtIOBinary::write_V1(const StHbtV0&) " << endl;
533 int StHbtIOBinary::read_V2(
StHbtV0& x) {
535 iret = read( x.mDecayLengthV0 );
536 iret = read( x.mDecayVertexV0 );
537 iret = read( x.mDcaV0Daughters );
538 iret = read( x.mDcaV0ToPrimVertex );
539 iret = read( x.mDcaPosToPrimVertex );
540 iret = read( x.mDcaNegToPrimVertex );
541 iret = read( x.mMomPos );
542 iret = read( x.mMomNeg );
543 iret = read( x.mTpcHitsPos );
544 iret = read( x.mTpcHitsNeg );
545 iret = read( x.mRapLambda );
546 iret = read( x.mRapK0Short );
547 iret = read( x.mCTauLambda );
548 iret = read( x.mCTauK0Short );
549 iret = read( x.mKeyPos );
550 iret = read( x.mKeyNeg );
551 iret = read( x.mTrackTopologyMapPos[0] );
552 iret = read( x.mTrackTopologyMapPos[1] );
553 iret = read( x.mTrackTopologyMapNeg[0] );
554 iret = read( x.mTrackTopologyMapNeg[1] );
559 int StHbtIOBinary::write_V2(
const StHbtV0& x) {
561 iret = write( x.mDecayLengthV0 );
562 iret = write( x.mDecayVertexV0 );
563 iret = write( x.mDcaV0Daughters );
564 iret = write( x.mDcaV0ToPrimVertex );
565 iret = write( x.mDcaPosToPrimVertex );
566 iret = write( x.mDcaNegToPrimVertex );
567 iret = write( x.mMomPos );
568 iret = write( x.mMomNeg );
569 iret = write( x.mTpcHitsPos );
570 iret = write( x.mTpcHitsNeg );
571 iret = write( x.mRapLambda );
572 iret = write( x.mRapK0Short );
573 iret = write( x.mCTauLambda );
574 iret = write( x.mCTauK0Short );
575 iret = write( x.mKeyPos );
576 iret = write( x.mKeyNeg );
577 iret = write( x.mTrackTopologyMapPos[0] );
578 iret = write( x.mTrackTopologyMapPos[1] );
579 iret = write( x.mTrackTopologyMapNeg[0] );
580 iret = write( x.mTrackTopologyMapNeg[1] );
588 int StHbtIOBinary::read_V3(
StHbtV0& x) {
590 iret = read( x.mDecayLengthV0 );
591 iret = read( x.mDecayVertexV0 );
592 iret = read( x.mDcaV0Daughters );
593 iret = read( x.mDcaV0ToPrimVertex );
594 iret = read( x.mDcaPosToPrimVertex );
595 iret = read( x.mDcaNegToPrimVertex );
596 iret = read( x.mMomPos );
597 iret = read( x.mMomNeg );
598 iret = read( x.mTpcHitsPos );
599 iret = read( x.mTpcHitsNeg );
600 iret = read( x.mRapLambda );
601 iret = read( x.mRapK0Short );
602 iret = read( x.mCTauLambda );
603 iret = read( x.mCTauK0Short );
604 iret = read( x.mKeyPos );
605 iret = read( x.mKeyNeg );
606 iret = read( x.mTrackTopologyMapPos[0] );
607 iret = read( x.mTrackTopologyMapPos[1] );
608 iret = read( x.mTrackTopologyMapNeg[0] );
609 iret = read( x.mTrackTopologyMapNeg[1] );
610 iret = read( x.mDedxPos );
611 iret = read( x.mDedxNeg );
615 int StHbtIOBinary::write_V3(
const StHbtV0& x) {
617 iret = write( x.mDecayLengthV0 );
618 iret = write( x.mDecayVertexV0 );
619 iret = write( x.mDcaV0Daughters );
620 iret = write( x.mDcaV0ToPrimVertex );
621 iret = write( x.mDcaPosToPrimVertex );
622 iret = write( x.mDcaNegToPrimVertex );
623 iret = write( x.mMomPos );
624 iret = write( x.mMomNeg );
625 iret = write( x.mTpcHitsPos );
626 iret = write( x.mTpcHitsNeg );
627 iret = write( x.mRapLambda );
628 iret = write( x.mRapK0Short );
629 iret = write( x.mCTauLambda );
630 iret = write( x.mCTauK0Short );
631 iret = write( x.mKeyPos );
632 iret = write( x.mKeyNeg );
633 iret = write( x.mTrackTopologyMapPos[0] );
634 iret = write( x.mTrackTopologyMapPos[1] );
635 iret = write( x.mTrackTopologyMapNeg[0] );
636 iret = write( x.mTrackTopologyMapNeg[1] );
637 iret = write( x.mDedxPos );
638 iret = write( x.mDedxNeg );