Changeset 11689 for trunk/FACT++/src/EventBuilder.c
- Timestamp:
- 07/28/11 14:33:18 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/EventBuilder.c
r11603 r11689 1 1 2 // #define nanosleep(x,y)2 // // // #define EVTDEBUG 3 3 4 4 … … 34 34 #define MIN_LEN 32 // min #bytes needed to interpret FADheader 35 35 #define MAX_LEN 256*1024 // size of read-buffer per socket 36 36 37 //#define nanosleep(x,y) 38 37 39 extern FileHandle_t runOpen(uint32_t irun, RUN_HEAD *runhd, size_t len ) ; 38 40 extern int runWrite(FileHandle_t fileHd , EVENT *event, size_t len ) ; … … 61 63 int g_runStat ; 62 64 int g_reset ; 65 int g_useFTM ; 66 63 67 int gi_reset, gi_resetR, gi_resetS, gi_resetW, gi_resetX ; 64 68 size_t g_maxMem ; //maximum memory allowed for buffer … … 138 142 int runID ; // run " 139 143 int ftmID ; // event ID from FTM 140 uint fadLen ; // FADlength of event currently read144 uint fadLen ; // FADlength of event currently read 141 145 int fadVers ; // Version of FAD 146 int ftmTyp ; // trigger type 142 147 int board ; // boardID (softwareID: 0..40 ) 143 148 int Port ; 144 149 150 CNV_FACT *rBuf ; 151 152 #ifdef EVTDEBUG 145 153 CNV_FACT *xBuf ; //a copy of rBuf (temporary for debuging) 146 CNV_FACT *rBuf ; 147 154 #endif 148 155 149 156 } READ_STRUCT ; … … 202 209 if (flag < 0) { 203 210 free(rd->rBuf) ; //and never open again 211 #ifdef EVTDEBUG 204 212 free(rd->xBuf) ; //and never open again 213 #endif 205 214 rd->rBuf = NULL ; 206 215 rd->sockStat = 99 ; … … 215 224 rd->SockAddr.sin_addr = sockAddr->sin_addr ; 216 225 226 #ifdef EVTDEBUG 217 227 rd->xBuf = malloc(sizeof(CNV_FACT) ) ; 228 #endif 218 229 rd->rBuf = malloc(sizeof(CNV_FACT) ) ; 219 230 if ( rd->rBuf == NULL ) { … … 275 286 276 287 277 int mBufEvt( int evID, uint runID, int nRoi[8], int sk, int fadlen) { 288 int mBufEvt( int evID, uint runID, int nRoi[], int sk, 289 int fadlen, int trgTyp, int trgNum, int fadNum) { 278 290 // generate a new Event into mBuffer: 279 291 // make sure only complete Event are possible, so 'free' will always work … … 337 349 return -8201 ; 338 350 } 351 352 // count for inconsistencies 353 354 if ( mBuffer[i].trgNum != trgNum ) mBuffer[i].Errors++ ; 355 if ( mBuffer[i].fadNum != fadNum ) mBuffer[i].Errors+=100 ; 356 if ( mBuffer[i].trgTyp != trgTyp ) mBuffer[i].Errors+=10000 ; 357 339 358 //everything seems fine so far ==> use this slot .... 340 359 return i ; … … 460 479 mBuffer[i].nRoi = nRoi[0] ; 461 480 mBuffer[i].nRoiTM = nRoi[8] ; 462 mBuffer[i].evNum = evID 481 mBuffer[i].evNum = evID ; 463 482 mBuffer[i].runNum = runID ; 483 mBuffer[i].fadNum = fadNum; 484 mBuffer[i].trgNum = trgNum; 485 mBuffer[i].trgTyp = trgTyp ; 464 486 mBuffer[i].evtLen = needmem ; 487 mBuffer[i].Errors = 0 ; 465 488 466 489 gj.usdMem += needmem + headmem; … … 577 600 int32_t jrd ; 578 601 uint gi_SecTime ; //time in seconds 579 int boardId, roi[ 8],drs,px,src,pixS,pixH,pixC,pixR,tmS ;602 int boardId, roi[9],drs,px,src,pixS,pixH,pixC,pixR,tmS ; 580 603 581 604 int goodhed = 0 ; … … 758 781 if (jrd >0 ) { 759 782 debugStream(i,&rd[i].rBuf->B[ rd[i].bufPos],jrd) ; 783 #ifdef EVTDEBUG 760 784 memcpy(&rd[i].xBuf->B[ rd[i].bufPos], &rd[i].rBuf->B[ rd[i].bufPos], jrd) ; 761 snprintf(str,MXSTR,"read sock %3d bytes %5d len %5d first %d %d",i,jrd,rd[i].bufLen, 762 rd[i].rBuf->B[ rd[i].bufPos ], 763 rd[i].rBuf->B[ rd[i].bufPos +1] ); 764 factOut(kDebug,301, str ) ; 785 snprintf(str,MXSTR,"read sock %3d bytes %5d len %5d first %d %d",i,jrd,rd[i].bufLen, 786 rd[i].rBuf->B[ rd[i].bufPos ], 787 rd[i].rBuf->B[ rd[i].bufPos +1] ); 788 factOut(kDebug,301, str ) ; 789 #endif 765 790 } 766 791 … … 796 821 if ( rd[i].bufTyp <0 ) { // we are skipping this board ... 797 822 // just do nothing 823 #ifdef EVTDEBUG 824 snprintf(str,MXSTR,"skipping %d bytes on socket %d",jrd,i) ; 825 factOut(kInfo,301, str ) ; 826 #endif 798 827 799 828 } else if ( rd[i].bufTyp >0 ) { // we are reading data ... … … 814 843 goto EndBuf ; 815 844 816 // } else { 817 // snprintf(str,MXSTR,"good end of buffer found sock %3d len %5d %d %d : %d %d - %d %d : %d %d", 818 // i,rd[i].fadLen, 819 // rd[i].rBuf->B[ 0 ], rd[i].rBuf->B[ 1 ], start.B[1],start.B[0], 820 // rd[i].rBuf->B[ rd[i].bufPos-2], rd[i].rBuf->B[ rd[i].bufPos-1], stop.B[1], stop.B[0]); 821 // factOut(kDebug,301, str ) ; 845 #ifdef EVTDEBUG 846 } else { 847 snprintf(str,MXSTR,"good end of buffer found sock %3d len %5d %d %d : %d %d - %d %d : %d %d", 848 i,rd[i].fadLen, 849 rd[i].rBuf->B[ 0 ], rd[i].rBuf->B[ 1 ], start.B[1],start.B[0], 850 rd[i].rBuf->B[ rd[i].bufPos-2], rd[i].rBuf->B[ rd[i].bufPos-1], stop.B[1], stop.B[0]); 851 factOut(kDebug,301, str ) ; 852 #endif 822 853 } 823 854 … … 831 862 } 832 863 //get index into mBuffer for this event (create if needed) 833 evID = mBufEvt( rd[i].evtID, rd[i].runID, roi, i, rd[i].fadLen ) ; 864 865 int actid; 866 if (g_useFTM >0) actid = rd[i].evtID ; 867 else actid = rd[i].ftmID ; 868 869 evID = mBufEvt( rd[i].evtID, rd[i].runID, roi, i, 870 rd[i].fadLen, rd[i].ftmTyp, rd[i].ftmID, rd[i].evtID ) ; 834 871 835 872 if (evID <-1000) { … … 837 874 } 838 875 if (evID < 0) { //no space left, retry later 839 840 if ( rd[i].bufLen != 0) { 841 snprintf(str,MXSTR,"something screwed up"); 842 factOut(kFatal, 1, str ) ; 843 } 876 #ifdef EVTDEBUG 877 if ( rd[i].bufLen != 0) { 878 snprintf(str,MXSTR,"something screwed up"); 879 factOut(kFatal, 1, str ) ; 880 } 881 #endif 844 882 xwait.tv_sec = 0; 845 883 xwait.tv_nsec= 10000000 ; // sleep for ~10 msec … … 851 889 //we have a valid entry in mBuffer[]; fill it 852 890 853 int xchk = memcmp(&rd[i].xBuf->B[0], &rd[i].rBuf->B[0], rd[i].fadLen ) ; 854 if (xchk != 0) { 855 snprintf(str,MXSTR,"ERROR OVERWRITE %d %d on port %d",xchk,rd[i].fadLen,i) ; 856 factOut(kFatal, 1, str ) ; 857 858 uint iq; 859 for (iq=0; iq < rd[i].fadLen ; iq++) { 860 if (rd[i].rBuf->B[iq] != rd[i].xBuf->B[iq] ) { 861 snprintf(str,MXSTR,"ERROR %4d %4d %x %x",i,iq,rd[i].rBuf->B[iq], rd[i].xBuf->B[iq]); 862 factOut(kFatal, 1, str ) ; 863 } 864 } 865 } 866 867 891 #ifdef EVTDEBUG 892 int xchk = memcmp(&rd[i].xBuf->B[0], &rd[i].rBuf->B[0], rd[i].fadLen ) ; 893 if (xchk != 0) { 894 snprintf(str,MXSTR,"ERROR OVERWRITE %d %d on port %d",xchk,rd[i].fadLen,i) ; 895 factOut(kFatal, 1, str ) ; 896 897 uint iq; 898 for (iq=0; iq < rd[i].fadLen ; iq++) { 899 if (rd[i].rBuf->B[iq] != rd[i].xBuf->B[iq] ) { 900 snprintf(str,MXSTR,"ERROR %4d %4d %x %x",i,iq,rd[i].rBuf->B[iq], rd[i].xBuf->B[iq]); 901 factOut(kFatal, 1, str ) ; 902 } 903 } 904 } 905 #endif 906 int qncpy = 0 ; 868 907 boardId = b ; 869 908 int fadBoard = ntohs(rd[i].rBuf->S[12] ) ; … … 871 910 if (boardId != (fadCrate*10 + fadBoard%256) ) { 872 911 snprintf(str,MXSTR,"wrong Board ID %d %d %d",fadCrate,fadBoard%256,boardId) ; 873 if (errcnt0++ < 99 ) factOut(kWarn,301, str ) ; //print only few times912 factOut(kWarn,301, str ) ; 874 913 } 875 914 if ( mBuffer[evID].board[ boardId ] != -1) { … … 885 924 memcpy( &mBuffer[evID].FADhead[boardId].start_package_flag, 886 925 &rd[i].rBuf->S[0], head_len) ; 887 // xxx roi = mBuffer[evID].nRoi;926 qncpy+=head_len ; 888 927 889 928 src = head_len/2 ; … … 898 937 src++ ; 899 938 900 // xxx if ( ( px != 8 && pixR == roi )901 // xxx || ( px == 8 && pixR >= roi ) ) { //we have a reasonable roi902 939 903 mBuffer[evID].fEvent->StartPix[pixS] =pixC; 904 dest= pixS * roi[0] ; 905 memcpy( 940 mBuffer[evID].fEvent->StartPix[pixS] =pixC; 941 dest= pixS * roi[0] ; 942 memcpy( 943 &mBuffer[evID].fEvent->Adc_Data[dest], 944 &rd[i].rBuf->S[src], roi[0] * 2) ; 945 qncpy+=roi[0]*2 ; 946 src+= pixR ; 947 948 if ( px==8 ) { 949 tmS =boardId*4 + drs ; 950 if ( pixR > roi[0]) { //and we have additional TM info 951 dest= tmS * roi[0] + NPIX* roi[0] ; 952 int srcT= src - roi[0] ; 953 mBuffer[evID].fEvent->StartTM[tmS] = (pixC+pixR-roi[0])%1024 ; 954 memcpy( 906 955 &mBuffer[evID].fEvent->Adc_Data[dest], 907 &rd[i].rBuf->S[src], roi[0] * 2) ; 908 src+= pixR ; 909 910 if ( px==8 ) { 911 if ( pixR > roi[0]) { //and we have additional TM info 912 tmS =boardId*4 + drs ; 913 dest= tmS * roi[0] + NPIX* roi[0] ; 914 int srcT= src - roi[0] ; 915 mBuffer[evID].fEvent->StartTM[tmS] = (pixC+pixR-roi[0])%1024 ; 916 memcpy( 917 &mBuffer[evID].fEvent->Adc_Data[dest], 918 &rd[i].rBuf->S[srcT], roi[0] * 2) ; 919 } else { 920 mBuffer[evID].fEvent->StartTM[tmS] = -1 ; 921 } 956 &rd[i].rBuf->S[srcT], roi[0] * 2) ; 957 qncpy+=roi[0]*2 ; 958 } else { 959 mBuffer[evID].fEvent->StartTM[tmS] = -1 ; 922 960 } 923 // xxx } else { 924 // xxx snprintf(str,MXSTR,"wrong roi %d %d %d %d",px,pixR,roi,src-2); 925 // xxx gi.evtErr++ ; 926 // xxx factOut(kError,202, str ) ; 927 // xxx goto EndBuf ; 928 // xxx } 961 } 929 962 } 930 963 }// now we have stored a new board contents into Event structure … … 936 969 937 970 if (++mBuffer[evID].nBoard >= actBoards ) { 938 snprintf(str,MXSTR,"%5d complete event %8d %8d %2d",mBuffer[evID].evNum,evtCtrl.evtBuf[iDx],iDx,evtCtrl.evtStat[ iDx ]); 939 factOut(kDebug,-1, str ) ; 940 971 int qnrun =0 ; 941 972 if (mBuffer[evID].runNum != actrun ) { // have we already reported first event of this run ??? 942 973 actrun = mBuffer[evID].runNum ; 943 974 int ir ; 944 975 for ( ir=0; ir<MAX_RUN; ir++) { 976 qnrun++ ; 945 977 if ( runCtrl[ir].runId == actrun) { 946 978 if ( ++runCtrl[ir].lastEvt ==0 ) { 947 979 gotNewRun( actrun, mBuffer[evID].FADhead ); 948 snprintf(str,MXSTR,"gotNewRun %d (ev %d)",mBuffer[evID].runNum,mBuffer[evID].evNum);949 factOut(kInfo,1, str ) ;980 snprintf(str,MXSTR,"gotNewRun %d (ev %d)",mBuffer[evID].runNum,mBuffer[evID].evNum); 981 factOut(kInfo,1, str ) ; 950 982 break ; 951 983 } … … 953 985 } 954 986 } 987 snprintf(str,MXSTR,"%5d complete event roi %4d roiTM %d cpy %8d %5d", 988 mBuffer[evID].evNum,roi[0],roi[8]-roi[0],qncpy,qnrun); 989 factOut(kDebug,-1, str ) ; 990 factOut(kInfo,-1, str ) ; 955 991 956 992 //complete event read ---> flag for next processing … … 985 1021 rd[i].bufLen += k ; 986 1022 memcpy(&rd[i].rBuf->B[0], &rd[i].rBuf->B[k], rd[i].bufPos ) ; 1023 #ifdef EVTDEBUG 987 1024 memcpy(&rd[i].xBuf->B[0], &rd[i].xBuf->B[k], rd[i].bufPos ) ; 1025 #endif 988 1026 } 989 1027 if ( rd[i].bufPos >= minLen ) { … … 996 1034 rd[i].fadLen = ntohs(rd[i].rBuf->S[1])*2 ; 997 1035 rd[i].fadVers= ntohs(rd[i].rBuf->S[2]) ; 1036 rd[i].ftmTyp = ntohl(rd[i].rBuf->S[5]) ; 998 1037 rd[i].evtID = ntohl(rd[i].rBuf->I[4]) ; //(FADevt) 999 1038 rd[i].ftmID = ntohl(rd[i].rBuf->I[5]) ; //(FTMevt) … … 1005 1044 int fadcrate = fadboard/256 ; 1006 1045 fadboard = (fadcrate*10 + fadboard%256) ; 1007 snprintf(str,MXSTR,"sk %3d head: %5d %5d %5d %10d %4d %6d",i,rd[i].fadLen,rd[i].evtID,rd[i].ftmID,rd[i].runID,fadboard,jrd) ; 1008 factOut(kDebug,1, str ) ; 1046 #ifdef EVTDEBUG 1047 snprintf(str,MXSTR,"sk %3d head: %5d %5d %5d %10d %4d %6d",i,rd[i].fadLen,rd[i].evtID,rd[i].ftmID,rd[i].runID,fadboard,jrd) ; 1048 factOut(kDebug,1, str ) ; 1049 #endif 1009 1050 1010 1051 if (rd[i].runID ==0 ) rd[i].runID = gi_myRun ; … … 1029 1070 } //finished trying to read all sockets 1030 1071 1031 snprintf(str,MXSTR,"Loop ---- %3d --- %8d",numokx,jrdx); 1032 factOut(kDebug,-1, str ) ; 1072 #ifdef EVTDEBUG 1073 snprintf(str,MXSTR,"Loop ---- %3d --- %8d",numokx,jrdx); 1074 factOut(kDebug,-1, str ) ; 1075 #endif 1076 1033 1077 gi.numRead[ numok ] ++ ; 1034 1078 … … 1285 1329 int id = evtCtrl.evtBuf[k0] ; 1286 1330 int ievt = mBuffer[id].evNum ; 1331 int itevt= mBuffer[id].trgNum ; 1332 int itrg = mBuffer[id].trgTyp ; 1287 1333 int roi = mBuffer[id].nRoi ; 1288 1334 int roiTM= mBuffer[id].nRoiTM ; 1335 int Errors=mBuffer[id].Errors ; 1289 1336 // uint32_t irun = mBuffer[id].runNum ; 1290 1337 //snprintf(str,MXSTR,"P processing %d %d %d %d",ievt,k,id,evtCtrl.evtStat[k0]) ; … … 1292 1339 1293 1340 //make sure unused pixels/tmarks are cleared to zero 1341 if (roiTM == roi) roiTM=0 ; 1294 1342 int ip,it,dest,ib; 1295 1343 for (ip=0; ip<NPIX; ip++) { … … 1311 1359 mBuffer[id].fEvent->RoiTM = roiTM ; 1312 1360 mBuffer[id].fEvent->EventNum = ievt ; 1313 mBuffer[id].fEvent->TriggerType = 0 ; // TBD 1361 mBuffer[id].fEvent->TriggerNum = itevt ; 1362 mBuffer[id].fEvent->TriggerType = itrg ; 1363 mBuffer[id].fEvent->Errors = Errors ; 1314 1364 mBuffer[id].fEvent->SoftTrig = 0 ; 1365 1366 1315 1367 for (ib=0; ib<NBOARDS; ib++) { 1316 1368 if (mBuffer[id].board[ib] == -1 ) { //board is not read
Note:
See TracChangeset
for help on using the changeset viewer.