Changeset 15347 for trunk/FACT++/src
- Timestamp:
- 04/15/13 17:02:29 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/EventBuilder.c
r15346 r15347 561 561 // < 0 if no space left 562 562 563 struct timeval tv; 564 uint32_t tsec, tusec; 565 uint oldest; 566 int jold; 567 568 int i, b, evFree; 563 // int evFree; 569 564 // int headmem = 0; 570 565 // size_t needmem = 0; 571 566 572 567 573 b = sk / 7;568 const int b = sk / 7; 574 569 575 570 if (nRoi[0] < 0 || nRoi[0] > 1024) { … … 596 591 597 592 598 i = evID % MAX_EVT;599 evFree = -1;593 int i = evID % MAX_EVT; 594 int evFree = -1; 600 595 601 596 for (int k = 0; k < MAX_RUN; k++) { … … 635 630 i = evFree; //found free entry; use it ... 636 631 632 struct timeval tv; 637 633 gettimeofday (&tv, NULL); 638 tsec = tv.tv_sec; 639 tusec = tv.tv_usec; 634 635 const uint32_t tsec = tv.tv_sec; 636 const uint32_t tusec = tv.tv_usec; 640 637 641 638 //check if runId already registered in runCtrl 642 639 evFree = -1; 643 oldest = g_actTime + 1000; 644 jold = -1; 640 641 uint oldest = g_actTime + 1000; 642 int jold = -1; 643 645 644 for (int k = 0; k < MAX_RUN; k++) { 646 645 if (runCtrl[k].runId == runID) { … … 1049 1048 { 1050 1049 /* *** main loop reading FAD data and sorting them to complete events */ 1051 int head_len, frst_len, numok, numok2, numokx, dest, evID, i, k; 1052 int actBoards = 0, minLen; 1053 int32_t jrd; 1050 int actBoards = 0; 1054 1051 uint gi_SecTime; //time in seconds 1055 int boardId, roi[9], drs, px, src, pixS, /*pixH,*/ pixC, pixR, tmS;1056 1057 int goodhed = 0;1058 1052 1059 1053 int sockDef[NBOARDS]; //internal state of sockets 1060 int jrdx;1061 1062 1063 struct timespec xwait;1064 1065 1054 1066 1055 struct timeval tv; … … 1086 1075 1087 1076 1088 head_len = sizeof (PEVNT_HEADER); 1089 frst_len = head_len; //max #bytes to read first: fad_header only, so each event must be longer, even for roi=0 1090 minLen = head_len; //min #bytes needed to check header: full header for debug 1077 int frst_len = sizeof(PEVNT_HEADER); //max #bytes to read first: fad_header only, so each event must be longer, even for roi=0 1078 int minLen = sizeof(PEVNT_HEADER); //min #bytes needed to check header: full header for debug 1091 1079 1092 1080 start.S = 0xFB01; … … 1094 1082 1095 1083 /* initialize run control logics */ 1096 for (i = 0; i < MAX_RUN; i++) {1084 for (int i = 0; i < MAX_RUN; i++) { 1097 1085 runCtrl[i].runId = 0; 1098 1086 runCtrl[i].fileId = -2; … … 1100 1088 } 1101 1089 gi_resetS = gi_resetR = 9; 1102 for (i = 0; i < NBOARDS; i++)1090 for (int i = 0; i < NBOARDS; i++) 1103 1091 sockDef[i] = 0; 1104 1092 … … 1114 1102 gi_runStat = g_runStat; 1115 1103 gj.readStat = g_runStat; 1116 numok = numok2 = 0; 1117 1118 int cntsock = 8 - NUMSOCK ; 1104 1105 const int cntsock = 8 - NUMSOCK ; 1119 1106 1120 1107 if (gi_resetS > 0) { 1121 1108 //make sure all sockets are preallocated as 'not exist' 1122 for (i = 0; i < MAX_SOCK; i++) {1109 for (int i = 0; i < MAX_SOCK; i++) { 1123 1110 rd[i].socket = -1; 1124 1111 rd[i].sockStat = 99; 1125 1112 } 1126 1113 1127 for ( k = 0; k < NBOARDS; k++) {1114 for (int k = 0; k < NBOARDS; k++) { 1128 1115 gi_NumConnect[k] = 0; 1129 1116 gi.numConn[k] = 0; … … 1171 1158 1172 1159 1173 int b, p, p0, s0, nch; 1174 nch = 0; 1175 for (b = 0; b < NBOARDS; b++) { 1176 k = b * 7; 1160 int nch = 0; 1161 for (int b = 0; b < NBOARDS; b++) { 1162 int k = b * 7; 1177 1163 if (g_port[b].sockDef != sockDef[b]) { //something has changed ... 1178 1164 nch++; … … 1180 1166 gi.numConn[b] = 0; 1181 1167 gj.numConn[b] = 0; 1168 1169 int s0; 1182 1170 if (sockDef[b] == 0) 1183 1171 s0 = 0; //sockets to be defined and opened … … 1187 1175 s0 = +1; //sockets to be closed and reopened 1188 1176 1189 if (s0 == 0) 1190 p0 = ntohs (g_port[b].sockAddr.sin_port); 1191 else 1192 p0 = 0; 1193 1194 for (p = p0 + 1; p < p0 + 8; p++) { 1177 const int p0 = s0==0 ? 0 : ntohs (g_port[b].sockAddr.sin_port); 1178 1179 for (int p = p0 + 1; p < p0 + 8; p++) { 1195 1180 GenSock (s0, k, p, &g_port[b].sockAddr, &rd[k]); //generate address and socket 1196 1181 k++; … … 1202 1187 if (nch > 0) { 1203 1188 actBoards = 0; 1204 for ( b = 0; b < NBOARDS; b++) {1189 for (int b = 0; b < NBOARDS; b++) { 1205 1190 if (sockDef[b] > 0) 1206 1191 actBoards++; … … 1209 1194 1210 1195 1211 jrdx = 0; 1212 numokx = 0; 1213 numok = 0; //count number of succesfull actions 1196 #ifdef EVTDEBUG 1197 int jrdx = 0; 1198 #endif 1199 1200 int numokx = 0; 1201 int numok = 0; //count number of succesfull actions 1214 1202 1215 1203 /* … … 1229 1217 */ 1230 1218 1231 for (i = 0; i < MAX_SOCK; i+=7) { //check all sockets if something to read1232 1233 b = i / 7 ;1234 p = i % 7 ;1219 for (int i = 0; i < MAX_SOCK; i+=7) { //check all sockets if something to read 1220 1221 const int b = i / 7 ; 1222 //const int p = i % 7 ; 1235 1223 1236 1224 /*if ( p >= NUMSOCK) { ; } 1237 else*/ { 1238 if (sockDef[b] > 0) 1239 s0 = +1; 1240 else 1241 s0 = -1; 1225 else*/ { 1226 const int s0 = sockDef[b] > 0 ? +1 : -1; 1242 1227 1243 1228 if (rd[i].sockStat < 0) { //try to connect if not yet done … … 1291 1276 numok++; 1292 1277 1293 jrd =1278 const int32_t jrd = 1294 1279 recv (rd[i].socket, &rd[i].rBuf->B[rd[i].bufPos], 1295 1280 rd[i].bufLen, MSG_DONTWAIT); … … 1339 1324 //if (jrd > 0) { 1340 1325 numokx++; 1326 #ifdef EVTDEBUG 1341 1327 jrdx += jrd; 1328 #endif 1342 1329 //} 1343 1330 … … 1447 1434 1448 1435 1449 int jr, kr;1450 int checkRoi;1451 int roi Hopper = head_len/2 + 2; //points to the very first roi1436 int checkRoi; 1437 int roiHopper = sizeof(PEVNT_HEADER)/2 + 2; //points to the very first roi 1438 int roi[9]; 1452 1439 roi[0] = rd[i].rBuf->S[roiHopper]; 1453 1440 roiHopper += roi[0]+4;//skip to the second roi (i.e. next board, same patch-pixel) 1454 for ( kr = 1; kr < 4; kr++)1441 for (int kr = 1; kr < 4; kr++) 1455 1442 { 1456 1443 checkRoi = rd[i].rBuf->S[roiHopper]; … … 1464 1451 } 1465 1452 //roiHopper now points to the first pixel of board 2. Do the 8 remaining pixels 1466 for ( jr = 1; jr < 9; jr++) {1453 for (int jr = 1; jr < 9; jr++) { 1467 1454 roi[jr] = rd[i].rBuf->S[roiHopper]; 1468 1455 checkRoi = roi[jr]; 1469 for ( kr = 1; kr < 4; kr++)1456 for (int kr = 1; kr < 4; kr++) 1470 1457 { 1471 1458 roiHopper += checkRoi+4; … … 1487 1474 // actid = rd[i].ftmID; 1488 1475 1489 evID = mBufEvt (rd[i].evtID, rd[i].runID, roi, i,1476 const int evID = mBufEvt (rd[i].evtID, rd[i].runID, roi, i, 1490 1477 rd[i].fadLen, rd[i].ftmTyp, rd[i].ftmID, 1491 1478 rd[i].evtID); … … 1530 1517 } 1531 1518 #endif 1532 int qncpy = 0;1533 boardId = b;1534 int fadBoard = rd[i].rBuf->S[12];1535 int fadCrate = fadBoard / 256; 1519 const int boardId = b; 1520 const int fadBoard = rd[i].rBuf->S[12]; 1521 const int fadCrate = fadBoard / 256; 1522 1536 1523 if (boardId != (fadCrate * 10 + fadBoard % 256)) { 1537 1524 factPrintf(kWarn, 301, "Board ID mismatch. Expected %d, got %d (C=%d, B=%d)", … … 1551 1538 } 1552 1539 1553 int iDx = evtIdx[evID]; //index into evtCtrl1540 const int iDx = evtIdx[evID]; //index into evtCtrl 1554 1541 1555 1542 memcpy (&mBuffer[evID].FADhead[boardId].start_package_flag, 1556 &rd[i].rBuf->S[0], head_len); 1557 qncpy += head_len; 1558 1559 src = head_len / 2; 1560 for (px = 0; px < 9; px++) { //different sort in FAD board..... 1561 for (drs = 0; drs < 4; drs++) { 1543 &rd[i].rBuf->S[0], sizeof(PEVNT_HEADER)); 1544 1545 int src = sizeof(PEVNT_HEADER) / 2; 1546 for (int px = 0; px < 9; px++) { //different sort in FAD board..... 1547 for (int drs = 0; drs < 4; drs++) { 1562 1548 // pixH = rd[i].rBuf->S[src++]; // ID 1563 1549 src++; 1564 pixC = rd[i].rBuf->S[src++]; // start-cell1565 pixR = rd[i].rBuf->S[src++]; // roi1550 const int pixC = rd[i].rBuf->S[src++]; // start-cell 1551 const int pixR = rd[i].rBuf->S[src++]; // roi 1566 1552 //here we should check if pixH is correct .... 1567 1553 1568 pixS = boardId * 36 + drs * 9 + px;1554 const int pixS = boardId * 36 + drs * 9 + px; 1569 1555 src++; 1570 1556 1571 1557 1572 1558 mBuffer[evID].fEvent->StartPix[pixS] = pixC; 1573 dest = pixS * roi[0]; 1559 1560 int dest = pixS * roi[0]; 1574 1561 memcpy (&mBuffer[evID].fEvent->Adc_Data[dest], 1575 1562 &rd[i].rBuf->S[src], roi[0] * 2); 1576 qncpy += roi[0] * 2;1577 1563 src += pixR; 1578 1564 1579 1565 if (px == 8) { 1580 tmS = boardId * 4 + drs;1566 const int tmS = boardId * 4 + drs; 1581 1567 if (pixR > roi[0]) { //and we have additional TM info 1582 1568 dest = tmS * roi[0] + NPIX * roi[0]; … … 1586 1572 memcpy (&mBuffer[evID].fEvent->Adc_Data[dest], 1587 1573 &rd[i].rBuf->S[srcT], roi[0] * 2); 1588 qncpy += roi[0] * 2;1589 1574 } else { 1590 1575 mBuffer[evID].fEvent->StartTM[tmS] = -1; … … 1622 1607 } 1623 1608 } 1624 #ifdef EVTDEBUG1625 factPrintf(kDebug, -1, "%5d complete event roi %4d roiTM %d cpy %8d %5d",1626 mBuffer[evID].evNum, roi[0], roi[8] - roi[0], qncpy, qnrun);1627 #endif1628 1609 //complete event read ---> flag for next processing 1629 1610 evtCtrl.evtStat[iDx] = 99; … … 1650 1631 } 1651 1632 1652 //check if startflag correct; else shift block .... 1633 //check if startflag correct; else shift block .... 1634 int k; 1653 1635 for (k = 0; k < rd[i].bufPos - 1; k++) { 1654 1636 if (rd[i].rBuf->B[k] == start.B[1] … … 1660 1642 if (k >= rd[i].bufPos - 1) { //no start of header found 1661 1643 rd[i].bufPos = 0; 1662 rd[i].bufLen = head_len;1644 rd[i].bufLen = sizeof(PEVNT_HEADER); 1663 1645 continue; 1664 1646 } … … 1690 1672 1691 1673 // TGB: This needs much more checks than just the first two bytes! 1692 goodhed++;1693 1674 1694 1675 // Swap everything except start_package_flag. … … 1766 1747 gi_SecTime = g_actTime; 1767 1748 1749 gj.bufNew = gj.bufEvt = 0; 1768 1750 1769 1751 //loop over all active events and flag those older than read-timeout 1770 1752 //delete those that are written to disk .... 1771 1772 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr; 1773 if (kd < 0) 1774 kd += (MAX_EVT * MAX_RUN); 1775 1776 gj.bufNew = gj.bufEvt = 0; 1777 1778 const int k1 = evtCtrl.frstPtr; 1779 for (int k=k1; k<k1+kd; k++) 1753 for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN) 1780 1754 { 1781 const int k0 = k % (MAX_EVT * MAX_RUN);1782 1783 1755 if (k0==evtCtrl.frstPtr && evtCtrl.evtStat[k0]<0) 1784 1756 { … … 1848 1820 gj.maxMem = gj.usdMem; 1849 1821 gj.maxEvt = gj.bufTot; 1850 for ( b = 0; b < NBOARDS; b++)1822 for (int b = 0; b < NBOARDS; b++) 1851 1823 gj.rateBytes[b] = 0; 1852 1824 /* … … 1894 1866 //must close all open sockets ... 1895 1867 factPrintf(kInfo, -1, "Close all sockets..."); 1896 for (i = 0; i < MAX_SOCK; i++) {1868 for (int i = 0; i < MAX_SOCK; i++) { 1897 1869 if (rd[i].sockStat == 0) { 1898 1870 GenSock (-1, i, 0, NULL, &rd[i]); //close and destroy open socket … … 1913 1885 { 1914 1886 //flag all events as 'read finished' 1915 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr; 1916 if (kd < 0) 1917 kd += (MAX_EVT * MAX_RUN); 1918 1919 const int k1 = evtCtrl.frstPtr; 1920 for (int k=k1; k<k1+kd; k++) 1921 { 1922 const int k0 = k % (MAX_EVT * MAX_RUN); 1887 for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN) 1888 { 1923 1889 if (evtCtrl.evtStat[k0] > 0 && evtCtrl.evtStat[k0] < 90) 1924 1890 { … … 1944 1910 numclear = 0; 1945 1911 1946 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr; 1947 if (kd < 0) 1948 kd += (MAX_EVT * MAX_RUN); 1949 1950 const int k1 = evtCtrl.frstPtr; 1951 for (int k=k1; k<k1+kd; k++) 1912 for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN) 1952 1913 { 1953 const int k0 = k % (MAX_EVT * MAX_RUN);1954 1914 if (evtCtrl.evtStat[k0] > minclear) 1955 1915 { … … 1980 1940 CloseRunFile (0, 0, 0); //ask all Runs to be closed 1981 1941 } 1982 if (gi_resetX > 0) { 1983 xwait.tv_sec = gi_resetX; 1984 xwait.tv_nsec = 0; 1985 nanosleep (&xwait, NULL); 1942 if (gi_resetX > 0) 1943 { 1944 struct timespec xwait; 1945 xwait.tv_sec = gi_resetX; 1946 xwait.tv_nsec = 0; 1947 nanosleep (&xwait, NULL); 1986 1948 } 1987 1949 … … 2019 1981 int numProc = 0; 2020 1982 2021 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr; 2022 if (kd < 0) 2023 kd += (MAX_EVT * MAX_RUN); 2024 2025 const int k1 = evtCtrl.frstPtr; 2026 for (int k=k1; k<k1+kd; k++) 1983 for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN) 2027 1984 { 2028 const int k0 = k % (MAX_EVT * MAX_RUN);2029 2030 1985 if (!(evtCtrl.evtStat[k0] == 1000 + threadID)) 2031 1986 { … … 2085 2040 { 2086 2041 /* *** main loop processing file, including SW-trigger */ 2087 int status , j;2042 int status; 2088 2043 2089 2044 int lastRun = 0; //usually run from last event still valid … … 2125 2080 int numProc = 0; 2126 2081 2127 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr; 2128 if (kd < 0) 2129 kd += (MAX_EVT * MAX_RUN); 2130 2131 const int k1 = evtCtrl.frstPtr; 2132 for (int k=k1; k<k1+kd; k++) 2082 for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN) 2133 2083 { 2134 const int k0 = k % (MAX_EVT * MAX_RUN);2135 2084 if (!(evtCtrl.evtStat[k0] > 90 && evtCtrl.evtStat[k0] < 1000)) 2136 2085 { … … 2329 2278 2330 2279 factPrintf(kInfo, -1, "Abort Processing Process ..."); 2331 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr;2332 if (kd < 0)2333 kd += (MAX_EVT * MAX_RUN);2334 2280 2335 2281 for (int k = 0; k < gi_maxProc; k++) { … … 2337 2283 } 2338 2284 2339 const int k1 = evtCtrl.frstPtr; 2340 for (int k=k1; k<k1+kd; k++) 2285 for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN) 2341 2286 { 2342 const int k0 = k % (MAX_EVT * MAX_RUN);2343 2287 if (evtCtrl.evtStat[k0] >= 0 && evtCtrl.evtStat[k0] < 1000) 2344 2288 evtCtrl.evtStat[k0] = 9800; //flag event as 'processed' … … 2462 2406 int numWait = 0; 2463 2407 2464 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr; 2465 if (kd < 0) 2466 kd += (MAX_EVT * MAX_RUN); 2467 2468 const int k1 = evtCtrl.frstPtr; 2469 for (int k=k1; k<k1+kd; k++) 2408 for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN) 2470 2409 { 2471 const int k0 = k % (MAX_EVT * MAX_RUN);2472 2473 2410 if (evtCtrl.evtStat[k0] <= 5000 || evtCtrl.evtStat[k0] >= 9000) 2474 2411 { … … 2483 2420 //we must drain the buffer asap 2484 2421 if (gi_resetR > 1) 2485 { 2422 { 2486 2423 evtCtrl.evtStat[k0] = 9904; 2487 2424 continue;
Note:
See TracChangeset
for help on using the changeset viewer.