44 #include "StTrgDatReader.h"
45 #include "StArchInfo.h"
46 #include "StMessMgr.h"
53 inline unsigned int swapI(
unsigned int var){
55 (var & 0xff000000) >> 24 |
56 (var & 0x00ff0000) >> 8 |
57 (var & 0x0000ff00) << 8 |
58 (var & 0x000000ff) << 24 ;
61 const int StTrgDatReader::mLheader=8;
63 StTrgDatReader::StTrgDatReader():
StStreamFile(), mLength(0),mVersion(0), mRunNumber(-1)
64 , mData(0) , mAllocated(0)
70 StTrgDatReader::StTrgDatReader(
const char *fileName, ios_base::openmode mode)
71 :
StStreamFile(fileName,mode), mLength(0), mVersion(0),mRunNumber(-1), mData(0),mAllocated(0)
78 StTrgDatReader::~StTrgDatReader() { Buffer(0) ; }
81 char *StTrgDatReader::Buffer(streamsize n)
83 if ( (n >mAllocated) || (n==0)) {
84 mData = (
char *)realloc(mData,n);
85 assert(!n || (mData &&
"There is no memory to allocate the I/O buffer"));
92 fstream &StTrgDatReader::Read(){
94 read(Buffer(mLheader), mLheader);
96 mVersion = swapI(*(
unsigned int*)mData);
97 mLength = swapI(*(
unsigned int*)(mData+4));
98 assert( (mLength - mLheader > 0) &&
"The file is too short!!!");
100 printf(
"Version = %x\nLength = %d; data=%p\n",Version(), Length(),mData);
102 read(Buffer(mLength)+mLheader, mLength - mLheader);
106 if (!eof()) Perror(
"*********** Attention DAT errors:");
112 int StTrgDatReader::RunNumber()
const {
113 if (mRunNumber == -1) {
114 TString ts(filename());
115 ts.ReplaceAll(
"_",
".");
116 Ssiz_t last=ts.Last(
'/');
117 if(last>0) {last++; ts.Remove(0,last);}
118 string f = ts.Data();
122 const char *pattern =
"^.*run([0-9]+)\\..+\\.dat$";
125 if ((rc = regcomp(&rx, pattern, REG_EXTENDED))) {
126 regerror(rc, &rx, buffer, 100);
127 LOG_ERROR <<
"Can not extract the event number from the file name \'"
128 << f <<
"\' because \'"
131 assert(0&&
"Can not extract the event number from the file name");
134 regmatch_t matchptr[2];
135 if ( !(regexec (&rx, f.c_str(),
sizeof(matchptr)/
sizeof(regmatch_t), matchptr, 0)) ) {
136 ((
StTrgDatReader*)
this)->mRunNumber = atoi(f.substr(matchptr[1].rm_so,matchptr[1].rm_eo).c_str());
146 int StTrgDatReader::RecordUnixTime()
const
153 int StTrgDatReader::Length()
const {
return mLength; }
156 int StTrgDatReader::Version()
const {
return mVersion;}
159 char* StTrgDatReader::Record() {
return mData; }
161 bool StTrgDatReader::closeFileSignal()