238 #include <sys/types.h>
239 #include <sys/stat.h>
240 #include <sys/mman.h>
246 #include "EventReader.hh"
249 #include "StMessMgr.h"
252 using namespace OLDEVP;
254 void EventInfo::printEventInfo(FILE * fd)
256 static const char *detnams[] =
257 {
"TPC ",
"SVT ",
"TOF ",
"BTOW",
"FPD ",
"FTPC",
"EXT ",
"RICH",
"TRG ",
"L3 "
258 ,
"SC ",
"EXT2",
"PMD ",
"SSD ",
"ETOW",
"DAQ ",
"FP2 ",
"PP ",
"BSMD",
"ESMD"
263 sprintf(ts,
"%s",ctime((
const time_t *)&UnixTime)) ;
265 LOG_INFO<<
"=============== Event # "<<EventSeqNo<<
" ============="<<endm;
266 LOG_INFO<<
"Ev len (wds) "<<EventLength<<endm;
267 LOG_INFO<<
"Creation Time: "<<ts<<endm;
268 LOG_INFO<<
"Trigger word "<< std::hex << (
void *)TrigWord<<
269 "\t\tTrigger Input word "<< std::hex << (
void *)TrigInputWord<<endm;
270 LOG_INFO<<"Token: "<<Token<<endm;
271 LOG_INFO<<"Detectors present: ";
272 unsigned const
char* p=0;
int i=0;
273 for (p=&TPCPresent,i=0; p<=&EMCPresent;p++,i++) {
275 LOG_INFO<<detnams[i]<<
" ";
279 LOG_INFO<<
"==========================================="<<endm;
281 EventReader *getEventReader(
int fd,
long offset,
int MMap)
285 er->InitEventReader(fd, offset, MMap);
288 LOG_ERROR << er->errstr().c_str() << endm;
289 LOG_ERROR << (er->err_string[er->errorNo()-1]) << endm;
295 er->InitEventReader(fd, offset);
298 LOG_ERROR << er->errstr().c_str() << endm;
299 LOG_ERROR << (er->err_string[er->errorNo()-1]) << endm;
309 EventReader *getEventReader(
int fd,
long offset,
const char *logfile,
int MMap)
313 er->InitEventReader(fd, offset, MMap);
316 er->InitEventReader(fd, offset);
320 LOG_ERROR << er->errstr().c_str() << endm;
321 LOG_ERROR << (er->err_string[er->errorNo()-1]) << endm;
325 er->printEventInfo(er->logfd);
332 er->InitEventReader(event);
335 LOG_ERROR << er->errstr().c_str() << endm;
336 LOG_ERROR << (er->err_string[er->errorNo()-1]) << endm;
344 EventReader::EventReader()
350 memset(errstr0,
'\0',
sizeof(errstr0));
353 next_event_offset = -1;
357 EventReader::EventReader(
const char *logfile)
363 memset(errstr0,
'\0',
sizeof(errstr0));
366 next_event_offset = -1;
368 logfd = fopen(logfile,
"a");
370 LOG_ERROR<<
"EventReader::EventReader() logfile failure"<<endm;
371 LOG_ERROR << strerror(errno) <<
": " << logfile<<
" !!!!!!!"<<endm;;
374 LOG_INFO<<
"opening logfile..."<<endm;
378 void EventReader::InitEventReader(
int fdes,
long offset,
int MMap)
380 #define MX_MAP_SIZE 0x20000000
385 LOG_INFO<<
"Initializing EventReader with a MAPPED file" << endm;
389 strcpy(err_string[0],
"ERROR: FILE");
390 strcpy(err_string[1],
"ERROR: CRC");
391 strcpy(err_string[2],
"ERROR: SWAP");
392 strcpy(err_string[3],
"ERROR: BANK");
393 strcpy(err_string[4],
"ERROR: MEM");
394 strcpy(err_string[5],
"ERROR: NOT DATA BANK");
395 strcpy(err_string[6],
"ERROR: BAD ARG");
396 strcpy(err_string[7],
"ERROR: ENDR ENCOUNTERED");
397 strcpy(err_string[8],
"ERROR: BAD HEADER");
398 strcpy(err_string[9],
"INFO: MISSING BANK");
399 strcpy(err_string[10],
"INFO: END OF FILE");
405 if (fstat(fd,&buf)<0){
406 LOG_ERROR <<
"DaqOpenTag"<< strerror(errno) <<endm;
409 FileLength = buf.st_size;
411 next_event_offset = 0;
415 long pagesize = sysconf(_SC_PAGESIZE);
417 LOG_INFO<<
"pagesize = "<<(int)pagesize<<endm;;
419 int mmap_offset = (offset/pagesize)*pagesize;
421 if(mmap_offset < 0) ERROR(ERR_FILE);
423 int mapsize = buf.st_size - offset + pagesize;
427 LOG_ERROR<<
"end of file encountered"<<endm;
429 ERROR(INFO_END_FILE) ;
431 if (mapsize>MX_MAP_SIZE) mapsize = MX_MAP_SIZE;
432 event_size = mapsize ;
436 if((MMAPP = (
char *)mmap(0, mapsize, PROT_READ | PROT_WRITE,
437 MAP_PRIVATE, fd, mmap_offset)) == (caddr_t) -1) {
438 LOG_ERROR<<strerror(errno)<<
"mapping file request "
439 <<std::hex<<(
void*)mapsize<<
" bytes"<<endm;
444 DATAP = MMAPP + (offset-mmap_offset);
446 if (offset>=buf.st_size) {
450 while (strncmp(
DATAP,
"LRHD", 4) == 0) {
452 if (!memcpy(&lr,
DATAP,
sizeof(lr))) {
453 LOG_ERROR<< strerror(errno)<<
": error in memcpy"<<endm;
456 if (!lr.test_CRC()) ERROR(ERR_CRC);
458 if (lr.swap() < 0) ERROR(ERR_SWAP);
464 strncpy(lcopy,lr.RecordType,8);
467 LOG_INFO<<
"lr.RecordType: "<<lcopy<<endm;
470 if(strncmp(lr.RecordType,
"DATA", 4) != 0) {
472 next_event_offset += 4 * lr.RecordLength;
474 LOG_INFO<<
"....skipping "<<(
unsigned int)next_event_offset<<
" bytes"<<endm;
476 DATAP += next_event_offset;
483 runnum = lr.header.RunNumber;
486 next_event_offset +=
sizeof(lr);
493 if(strncmp(
DATAP,
"DATAP", 5) != 0)
496 LOG_ERROR<<
"failed to find DATAP at offset 0x"<<(
unsigned int)next_event_offset<<endm;
505 if (!datap->test_CRC()) ERROR(ERR_CRC);
506 if (datap->swap() < 0) ERROR(ERR_SWAP);
508 if (offset + 4*datap->EventLength > buf.st_size) {
509 LOG_ERROR<<
"event #"<<datap->EventNumber<<
" continues beyond file boundary"<<endm;
514 next_event_offset += 4*datap->EventLength;
515 next_event_offset += offset;
520 void EventReader::InitEventReader(
int fdes,
long offset)
522 long c_offset = offset;
524 LOG_INFO<<
"Initializing EventReader with a file" << endm;
528 strcpy(err_string[0],
"ERROR: FILE");
529 strcpy(err_string[1],
"ERROR: CRC");
530 strcpy(err_string[2],
"ERROR: SWAP");
531 strcpy(err_string[3],
"ERROR: BANK");
532 strcpy(err_string[4],
"ERROR: MEM");
533 strcpy(err_string[5],
"ERROR: NOT DATA BANK");
534 strcpy(err_string[6],
"ERROR: BAD ARG");
535 strcpy(err_string[7],
"ERROR: ENDR ENCOUNTERED");
536 strcpy(err_string[8],
"ERROR: BAD HEADER");
537 strcpy(err_string[9],
"INFO: MISSING BANK");
538 strcpy(err_string[10],
"INFO: END OF FILE");
542 int DATAPEVENTLENGTH=0;
545 memset(bank,
'\0',
sizeof(bank));
547 int ret = lseek(fd, offset, SEEK_SET);
548 if(ret < 0) ERROR(ERR_FILE);
550 ret = read(fd,bank,8);
551 if(ret < 0) ERROR(ERR_FILE);
553 ret = lseek(fd, offset, SEEK_SET);
554 if(ret < 0) ERROR(ERR_FILE);
559 while (strncmp(bank,
"LRHD", 4) == 0) {
562 ret = read(fd,&lr,
sizeof(lr));
563 if(ret < 0) ERROR(ERR_FILE);
564 c_offset +=
sizeof(lr);
571 if (!lr.test_CRC()) ERROR(ERR_CRC);
572 if (lr.swap() < 0) ERROR(ERR_SWAP);
577 DATAPEVENTLENGTH = lr.RecordLength -
sizeof(lr)/4;
579 runnum = lr.header.RunNumber;
588 strncpy(lcopy,lr.RecordType,8);
591 LOG_INFO<<
"lr.RecordType: "<<lcopy<<endm;
599 if(strncmp(lr.RecordType,
"DATA", 4) != 0) {
601 offset = 4*lr.RecordLength-
sizeof(lr);
604 LOG_INFO<<
"....skipping "<<offset<<
" bytes"<<endm;
606 lseek(fd,offset,SEEK_CUR);
609 ret = read(fd,bank,8);
610 lseek(fd,-8,SEEK_CUR);
612 if(ret < 0) ERROR(ERR_FILE);
614 if(ret == 0) ERROR(INFO_END_FILE);
619 ret = read(fd,bank,8);
620 lseek(fd,-8,SEEK_CUR);
627 memset(bank,
'\0',
sizeof(bank));
628 ret = read(fd,bank,8);
629 if(ret < 0) ERROR(ERR_FILE);
632 ret = lseek(fd,-8,SEEK_CUR);
634 offset +=
sizeof(lr);
637 if(strncmp(bank,
"DATAP", 5) != 0)
645 if (datap.header.swap() < 0) ERROR(ERR_SWAP);
647 int len = 4*datap.header.BankLength;
652 if(ret < 0) ERROR(ERR_FILE);
653 ret = read(fd,&datap,len);
654 if(ret < 0) ERROR(ERR_FILE);
655 ret = lseek(fd,-len,SEEK_CUR);
656 if(ret < 0) ERROR(ERR_FILE);
659 if (!datap.test_CRC()) ERROR(ERR_CRC);
660 if (datap.swap() < 0) ERROR(ERR_SWAP);
662 if(datap.EventLength < DATAPEVENTLENGTH)
663 datap.EventLength = DATAPEVENTLENGTH;
665 next_event_offset = c_offset + datap.EventLength * 4;
671 if(fstat(fd, &statbuf) < 0) ERROR(ERR_FILE);
676 DATAP = (
char *)malloc(datap.EventLength * 4);
677 if(!
DATAP) ERROR(ERR_MEM);
679 ret = read(fd,
DATAP, datap.EventLength*4);
680 if(ret < 0) ERROR(ERR_FILE);
686 datap.EventLength = DATAPEVENTLENGTH;
688 event_size = datap.EventLength;
692 void EventReader::InitEventReader(
void *event)
695 LOG_INFO <<
"Creating EventReader with a pointer" << endm;
698 if(strncmp((
char *)event,
"LRHD",4) == 0)
703 if (!lr->test_CRC()) ERROR(ERR_CRC);
704 if (lr->swap() < 0) ERROR(ERR_SWAP);
708 runnum = lr->header.RunNumber;
713 if(strncmp(lr->RecordType,
"DATA", 4) != 0) ERROR(ERR_NOT_DATA_BANK);
719 if(strncmp((
char *)event,
"DATAP", 5) != 0) ERROR(ERR_BANK);
723 if (!datap->test_CRC()) ERROR(ERR_CRC);
724 if (datap->swap() < 0) ERROR(ERR_SWAP);
725 datap->header.CRC = 0;
728 DATAP = (
char *)datap;
729 event_size = datap->EventLength * 4;
732 EventReader::~EventReader()
740 else if(MMAPP != NULL)
743 munmap(MMAPP,event_size);
750 if (logfd!=NULL) fclose(logfd);
753 int EventReader::system_present(
Bank_DATAP *datap,
int sys)
759 pointer = &datap->EXTY_ID;
760 if((pointer->offset == 0) || (pointer->length == 0)) {
767 pointer = &datapx->EXT_DET[sys-10];
768 if((pointer->offset == 0) || (pointer->length == 0)) {
773 pointer = &datap->TPC;
775 if((pointer->offset == 0) || (pointer->length == 0)) {
812 ei.Token = dp->header.Token;
813 ei.EventLength = dp->EventLength;
814 ei.UnixTime = dp->Time;
815 ei.EventSeqNo = dp->EventNumber;
816 ei.TrigWord = dp->TriggerWord;
817 ei.TrigInputWord = dp->TriggerInWord;
818 int detpre = dp->DetectorPresence;
821 LOG_INFO<<
"EventReader::getEventInfo detector presence = "<<detpre<<endm;
824 for (
unsigned char *p = &ei.TPCPresent; p<=&ei.ESMDPresent;p++) {
826 if(system_present(dp, sys) ||
827 (ei.UnixTime < 1197676800 && (detpre & (1<<sys)))
831 ei.EMCPresent = (ei.BTOWPresent|ei.ETOWPresent|ei.BSMDPresent|ei.ESMDPresent|ei.TRGPresent);
833 LOG_ERROR <<
"EventReader::getEventInfo: No DATAP exists" << endm;
838 void EventReader::printEventInfo(FILE * fd)
841 ei.printEventInfo(fd);
844 long EventReader::NextEventOffset()
846 return next_event_offset;
849 int EventReader::MemUsed()
854 void EventReader::setVerbose(
int v)
859 char * EventReader::findBank(
const char *bankid)
864 LOG_ERROR<<
"DATAP not found: "<<__FILE__<<
" "<<__LINE__<<endm;
867 if (!pBankDATAP->test_CRC()) {
868 LOG_ERROR<<
"CRC error in DATAP: "<<__FILE__<<
" "<<__LINE__<<endm;
871 if (pBankDATAP->swap() < 0){
872 LOG_ERROR<<
"swap error in DATAP: "<<__FILE__<<
" "<<__LINE__<<endm;
875 pBankDATAP->header.CRC = 0;
886 Pointer *ptr = &pBankDATAP->TPC;
893 for (i=0; i<len; i++, ptr++) {
894 if (ptr->length==0)
continue;
895 if ((
unsigned int)ptr->length== 0xfeedf00d)
continue;
896 pBank = (
Bank_Header *)(((INT32 *)pBankDATAP)+ (ptr->offset));
900 if(!strncmp(
"DATAPX",pBank->BankType,6)){
901 pBankDATAPX = (
Bank_DATAPX *)(((INT32 *)pBankDATAP)+ (ptr->offset));
903 Pointer *ptr1 = &pBankDATAPX->EXT_DET[0];
904 for(j=0; j < ext_len; j++, ptr1++){
905 if (ptr1->length==0)
continue;
906 pBank = (
Bank_Header *)(((INT32 *)pBankDATAPX)+ ptr1->offset);
907 if(!strncmp(bankid,pBank->BankType,4))
break;
911 if(!strncmp(bankid,pBank->BankType,4))
break;
913 if (i==len)
return NULL;
914 if (!pBank)
return NULL;
916 if(strncmp(pBank->BankType,bankid,4)) {
917 LOG_ERROR<<
"detector "<<bankid<<
" not found in DATAP"<<endm;
920 return (
char *)pBank;
924 void EventReader::fprintError(
int err,
const char *file,
int line,
const char *userstring)
926 if (logfd==NULL)
return;
927 if (err<0 || err>MX_MESSAGE)
return;
928 fprintf(logfd,
"%s %s::%d %s\n",err_string[err-1],file,line,userstring);
935 char EventReader::eventIsCorrupted(
int herbFd,
long offset) {
938 strcpy(mLastBank,
"???"); mWordIndex=-1;
939 returnValue = BankOrItsDescendentsIsBad(herbFd,offset);
941 PP
"StDaqLib/EventReader::eventIsCorrupted: bank pointed to by data word %d of bank %s.\n",mWordIndex+1,mLastBank);
945 char *EventReader::ConvertToString(
unsigned long *input) {
950 while(isupper(*cc)||isdigit(*cc)||*cc==
'_') *(dd++)=*(cc++); *dd=0;
951 if(strlen(rv)>8) rv[8]=0;
954 void EventReader::WhereAreThePointers(
int *beg,
int *end,
char *xx) {
955 *beg=-123; *end=-123;
956 assert(strcmp(xx,
"TPCMZP"));
961 if(xx[0]==
'F'&&xx[1]==
'T'&&xx[2]==
'P') { *beg=0; *end= 0;
return; }
963 if(!strcmp(xx,
"DATAP")) { *beg=7; *end=26; }
964 if(!strcmp(xx,
"RICP")) { *beg=1; *end=36; }
965 if(!strcmp(xx,
"RICCRAMP")) { *beg=1; *end=16; }
966 if(!strcmp(xx,
"TRGP")) { *beg=1; *end= 2; }
967 if(!strcmp(xx,
"TPCP")) { *beg=1; *end=48; }
968 if(!strcmp(xx,
"TPCSECP")) { *beg=1; *end=24; }
969 if(!strcmp(xx,
"TPCRBP")) { *beg=1; *end= 6; }
970 if(!strcmp(xx,
"L3_SECTP")) { *beg=9; *end=14; }
971 if(!strcmp(xx,
"L3_SECP")) { *beg=6; *end=11; }
972 if(!strcmp(xx,
"L3_P")) { *beg=0; *end= 0;
return; }
973 if(!strcmp(xx,
"SVTP")) { *beg=1; *end= 8; }
974 if(!strcmp(xx,
"SVTSECP")) { *beg=1; *end=24; }
975 if(!strcmp(xx,
"SVTRBP")) { *beg=1; *end= 6; }
976 assert(strcmp(xx,
"FTPMZP"));
977 if(!strcmp(xx,
"FTPP")) { *beg=1; *end=4; }
978 if(!strcmp(xx,
"FTPSECP")) { *beg=1; *end= 24; }
979 if(!strcmp(xx,
"FTPRBP")) { *beg=1; *end=6; }
981 if(!strcmp(xx,
"FPDP")) { *beg=0; *end=0; }
983 if((*beg)<0||(*end)<0) {
985 *beg=0; *end=0;
return;
987 if(*beg>=*end) assert(0);
988 if((*end-*beg)%2!=1) assert(0);
990 void EventReader::Swap4(
unsigned long *
data) {
993 temp[0]=hh[3]; temp[1]=hh[2]; temp[2]=hh[1]; temp[3]=hh[0];
994 *data=*((
unsigned long*)temp);
999 char EventReader::BankOrItsDescendentsIsBad(
int herbFd,
long currentOffset) {
1000 unsigned long header[10],
data[DATA];
1001 char doTheByteSwap=FALSE,lastletter,bankname[25];
1002 int i,beg,end,numberOfDataWords,bytesRead;
1006 if(lseek(herbFd,currentOffset,SEEK_SET)!=currentOffset)
return TRUE;
1008 bytesRead=read(herbFd,header,10*
sizeof(
unsigned long));
1010 if(bytesRead!=10*
sizeof(
unsigned long))
return TRUE;
1013 strcpy(bankname,ConvertToString(header));
1015 if(strlen(bankname)<3)
return TRUE;
1016 lastletter=bankname[strlen(bankname)-1];
1018 if(header[5]==0x01020304) doTheByteSwap=TRUE;
else if(header[5]!=0x04030201)
return TRUE;
1022 if(lastletter!=
'P')
return FALSE;
1024 if(doTheByteSwap)
for(i=0;i<10;i++) Swap4(header+i);
1025 assert(header[5]==0x04030201);
1027 numberOfDataWords=header[2]-10;
1028 if(numberOfDataWords>DATA) { LOG_INFO<<numberOfDataWords<<
" "<<DATA<<
", bankname="<<bankname<<endm; }
1029 assert(numberOfDataWords<=DATA);
1030 if(!strcmp(bankname,
"TPCMZP")) { beg=0; end=numberOfDataWords-1; }
1031 else if(!strcmp(bankname,
"EMCP")) { beg=0; end=0; }
1032 else if(!strcmp(bankname,
"PMDP")) { beg=0; end=0; }
1033 else WhereAreThePointers(&beg,&end,bankname);
1034 if(end>=numberOfDataWords&&numberOfDataWords>0) {
1035 PP
"end=%d, numberOfDataWords=%d, bankname=%s.\n",end,numberOfDataWords,bankname);
1036 assert(end<numberOfDataWords);
1039 bytesRead=read(herbFd,data,numberOfDataWords*
sizeof(
unsigned long));
1040 if(bytesRead!=
int(numberOfDataWords*
sizeof(
unsigned long)))
return TRUE;
1041 if(doTheByteSwap)
for(i=0;i<numberOfDataWords;i++) Swap4(data+i);
1043 if(!strcmp(bankname,
"TPCMZP")||!strcmp(bankname,
"FTPMZP")) {
1044 if(data[5]!=0&&(data[1]==0||data[3]==0))
return TRUE;
1047 for(i=beg;i<end;i+=2) {
1048 if(data[i+1]==0)
continue;
1050 PP
"Bank '%s' (at offset %ld bytes in the .daq file) points to itself (data word %d counting from 1).\n",
1051 bankname,currentOffset,i+1);
1054 strncpy(mLastBank,bankname,25);
1056 if(BankOrItsDescendentsIsBad(herbFd,4*data[i]+currentOffset))
return TRUE;