111 #include <sys/types.h>
112 #include <sys/stat.h>
113 #include <sys/mman.h>
119 #include "RichEventReader.hh"
120 #include "RICH_Reader.hh"
122 using namespace OLDEVP;
128 er->InitEventReader(fd, offset, MMap);
131 cout << er->errstr().c_str() << endl;
132 cout << (er->err_string[er->errorNo()-1]) << endl;
138 er->InitEventReader(fd, offset);
141 cout << er->errstr().c_str() << endl;
142 cout << (er->err_string[er->errorNo()-1]) << endl;
153 void RichEventReader::InitEventReader(
int fdes,
long offset,
int MMap)
156 #define MX_MAP_SIZE 32768*4
163 strcpy(err_string[0],
"ERROR: FILE");
164 strcpy(err_string[1],
"ERROR: CRC");
165 strcpy(err_string[2],
"ERROR: SWAP");
166 strcpy(err_string[3],
"ERROR: BANK");
167 strcpy(err_string[4],
"ERROR: MEM");
168 strcpy(err_string[5],
"ERROR: NOT DATA BANK");
169 strcpy(err_string[6],
"ERROR: BAD ARG");
170 strcpy(err_string[7],
"ERROR: ENDR ENCOUNTERED");
171 strcpy(err_string[8],
"ERROR: BAD HEADER");
172 strcpy(err_string[9],
"INFO: MISSING BANK");
173 strcpy(err_string[10],
"INFO: END OF FILE");
178 next_event_offset = 0;
179 if (fstat(fd,&buf)<0){
180 perror(
"error in DaqOpenTag");
182 next_event_offset = -1;
184 FileLength = buf.st_size;
189 long pagesize = sysconf(_SC_PAGESIZE);
190 int mmap_offset = (offset/pagesize)*pagesize;
192 if(mmap_offset < 0) ERROR(ERR_FILE);
194 int mapsize = buf.st_size - offset + pagesize;
197 if (verbose) printf(
"end of file encountered\n");
198 ERROR(INFO_END_FILE) ;
199 next_event_offset= -1;
202 if (mapsize>MX_MAP_SIZE) mapsize = MX_MAP_SIZE;
203 event_size = mapsize ;
207 if((MMAPP = (
char *)mmap(0, mapsize, PROT_READ | PROT_WRITE,
208 MAP_PRIVATE, fd, mmap_offset)) == (caddr_t) -1) {
210 sprintf(myerr,
"mapping file request 0x%x bytes 0x%x",mapsize,mmap_offset);
213 next_event_offset=-1;
218 DATAP = MMAPP + (offset-mmap_offset);
220 if (offset>=buf.st_size) {
222 next_event_offset = -1;
225 if (strncmp(
DATAP,
"RICP", 4) == 0) {
227 if (!memcpy(&lr,
DATAP,
sizeof(lr))) perror(
"error in memcpy");
229 if (!lr.test_CRC()) ERROR(ERR_CRC);
231 if (lr.swap() < 0) ERROR(ERR_SWAP);
237 strncpy(lcopy,lr.header.BankType,8);
241 if(strncmp(lr.header.BankType,
"RICP", 4) != 0) {
242 perror(
"Have problem will the file\n");
243 next_event_offset = -1;
250 next_event_offset = 4 * lr.Reserved[0].off+offset;
252 if (lr.header.Token==0){
253 next_event_offset = -1;
254 printf(
"hack: do not know how to deal with token==0\n");
261 printf(
"======= Event number: %d with size %d============\n",lr.header.Token,lr.Reserved[0].off);
264 RichEventReader::RichEventReader()
270 memset(errstr0,
'\0',
sizeof(errstr0));
273 next_event_offset = -1;
279 void RichEventReader::InitEventReader(
int fdes,
long offset)
281 long c_offset = offset;
282 next_event_offset = -1;
284 if (verbose) cout <<
"Initializing RichEventReader with a file" << endl;
287 strcpy(err_string[0],
"ERROR: FILE");
288 strcpy(err_string[1],
"ERROR: CRC");
289 strcpy(err_string[2],
"ERROR: SWAP");
290 strcpy(err_string[3],
"ERROR: BANK");
291 strcpy(err_string[4],
"ERROR: MEM");
292 strcpy(err_string[5],
"ERROR: NOT DATA BANK");
293 strcpy(err_string[6],
"ERROR: BAD ARG");
294 strcpy(err_string[7],
"ERROR: ENDR ENCOUNTERED");
295 strcpy(err_string[8],
"ERROR: BAD HEADER");
296 strcpy(err_string[9],
"INFO: MISSING BANK");
297 strcpy(err_string[10],
"INFO: END OF FILE");
301 int DATAPEVENTLENGTH=0;
304 memset(bank,
'\0',
sizeof(bank));
306 int ret = lseek(fd, offset, SEEK_SET);
308 next_event_offset = -1;
312 ret = read(fd,bank,8);
314 next_event_offset = -1;
318 ret = lseek(fd, offset, SEEK_SET);
319 if(ret < 0) ERROR(ERR_FILE);
324 if (strncmp(bank,
"RICP", 4) == 0) {
326 ret = read(fd,&lr,
sizeof(lr));
327 if(ret < 0) ERROR(ERR_FILE);
328 ret = lseek(fd, offset, SEEK_SET);
329 if(ret < 0) ERROR(ERR_FILE);
330 c_offset +=
sizeof(lr);
331 if (!lr.test_CRC()) ERROR(ERR_CRC);
332 if (lr.swap() < 0) ERROR(ERR_SWAP);
337 DATAPEVENTLENGTH = lr.Reserved[0].off;
340 next_event_offset = offset+lr.Reserved[0].off * 4;
345 if(fstat(fd, &statbuf) < 0) ERROR(ERR_FILE);
347 DATAP = (
char *)malloc(DATAPEVENTLENGTH * 4);
348 if(!
DATAP) ERROR(ERR_MEM);
350 ret = read(fd,
DATAP, DATAPEVENTLENGTH*4);
351 if(ret < 0) ERROR(ERR_FILE);
356 event_size = DATAPEVENTLENGTH;
359 if (lr.header.Token==0){
360 next_event_offset = -1;
361 printf(
"hack: do not know how to deal with token==0\n");
365 printf(
"======= Event number: %d size %d============\n",lr.header.Token, DATAPEVENTLENGTH);
369 RichEventReader::~RichEventReader()
377 else if(MMAPP != NULL)
381 munmap(MMAPP,event_size);
388 if (logfd!=NULL) fclose(logfd);
391 EventInfo RichEventReader::getEventInfo()
396 ei.EventLength = dp->Reserved[0].off;
397 ei.EventSeqNo = dp->header.Token;
398 ei.RICHPresent = (0x80);
402 void RichEventReader::printEventInfo(FILE * fd)
408 long RichEventReader::NextEventOffset()
410 return next_event_offset;
413 int RichEventReader::MemUsed()
418 void RichEventReader::setVerbose(
int v)
423 char * RichEventReader::findBank(
const char *bankid)
427 return (
char *)pBankDATAP;
431 void RichEventReader::fprintError(
int err,
char *file,
int line,
char *userstring)
433 if (logfd==NULL)
return;
434 if (err<0 || err>MX_MESSAGE)
return;
435 fprintf(logfd,
"%s %s::%d %s\n",err_string[err-1],file,line,userstring);