00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include "StTrgDatReader.h"
00042 #include "StArchInfo.h"
00043 #include "StMessMgr.h"
00044 #include <iostream>
00045 #include <assert.h>
00046 #include <regex.h>
00047
00048 namespace {
00049
00050 inline unsigned int swapI(unsigned int var){
00051 return
00052 (var & 0xff000000) >> 24 |
00053 (var & 0x00ff0000) >> 8 |
00054 (var & 0x0000ff00) << 8 |
00055 (var & 0x000000ff) << 24 ;
00056 }
00057 }
00058 const int StTrgDatReader::mLheader=8;
00059
00060 StTrgDatReader::StTrgDatReader():StStreamFile(), mLength(0),mVersion(0), mRunNumber(-1)
00061 , mData(0) , mAllocated(0)
00062 {
00063 Buffer(mLheader);
00064 }
00065
00066
00067 StTrgDatReader::StTrgDatReader(const char *fileName, ios_base::openmode mode)
00068 :StStreamFile(fileName,mode), mLength(0), mVersion(0),mRunNumber(-1), mData(0),mAllocated(0)
00069 {
00070 Buffer(mLheader);
00071 }
00072
00073
00074
00075 StTrgDatReader::~StTrgDatReader() { Buffer(0) ; }
00076
00077
00078 char *StTrgDatReader::Buffer(streamsize n)
00079 {
00080 if ( (n >mAllocated) || (n==0)) {
00081 mData = (char *)realloc(mData,n);
00082 assert(!n || (mData && "There is no memory to allocate the I/O buffer"));
00083 mAllocated=n;
00084 }
00085 return mData;
00086 }
00087
00088
00089 fstream &StTrgDatReader::Read(){
00090 if (is_open() ) {
00091 read(Buffer(mLheader), mLheader);
00092 if (good()) {
00093 mVersion = swapI(*(unsigned int*)mData);
00094 mLength = swapI(*(unsigned int*)(mData+4));
00095 assert( (mLength - mLheader > 0) && "The file is too short!!!");
00096 if(Debug() >0) {
00097 printf("Version = %x\nLength = %d; data=%p\n",Version(), Length(),mData);
00098 }
00099 read(Buffer(mLength)+mLheader, mLength - mLheader);
00100 }
00101 if (!good()) {
00102 mLength = 0;
00103 if (!eof()) Perror("*********** Attention DAT errors:");
00104 }
00105 }
00106 return stream();
00107 }
00108
00109 int StTrgDatReader::RunNumber() const {
00110 if (mRunNumber == -1) {
00111 string f = filename();
00112 regex_t rx;
00113 const char *pattern = "^.*run([0-9]+)\\..+\\.dat$";
00114 int rc;
00115 char buffer[100];
00116 if ((rc = regcomp(&rx, pattern, REG_EXTENDED))) {
00117 regerror(rc, &rx, buffer, 100);
00118 LOG_ERROR << "Can not extract the event number from the file name \'"
00119 << f << "\' because \'"
00120 << buffer << "\'"
00121 << endm;
00122 assert(0&&"Can not extract the event number from the file name");
00123 } else {
00124 regmatch_t matchptr[2];
00125 if ( !(regexec (&rx, f.c_str(), sizeof(matchptr)/sizeof(regmatch_t), matchptr, 0)) ) {
00126 ((StTrgDatReader*) this)->mRunNumber = atoi(f.substr(matchptr[1].rm_so,matchptr[1].rm_eo).c_str());
00127 }
00128 }
00129 }
00130 return mRunNumber;
00131 }
00132
00133
00134 int StTrgDatReader::RecordUnixTime() const
00135 {
00136
00137 return 2019686401;
00138 }
00139
00140
00141 int StTrgDatReader::Length() const { return mLength; }
00142
00143
00144 int StTrgDatReader::Version() const { return mVersion;}
00145
00146
00147 char* StTrgDatReader::Record() { return mData; }
00148
00149 bool StTrgDatReader::closeFileSignal()
00150 {
00151
00152 mRunNumber = -1;
00153 return true;
00154 }