Changeset 15360
- Timestamp:
- 04/17/13 16:29:13 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/EventBuilder.c
r15356 r15360 102 102 //int gi_memStat = +1; 103 103 104 uint32_t gi_myRun = 0;105 104 uint32_t actrun = 0; 106 105 … … 366 365 CNV_FACT *rBuf; 367 366 368 #ifdef EVTDEBUG369 CNV_FACT *xBuf; //a copy of rBuf (temporary for debuging)370 #endif371 372 367 } READ_STRUCT; 373 368 … … 448 443 if (flag < 0) { 449 444 free (rd->rBuf); //and never open again 450 #ifdef EVTDEBUG451 free (rd->xBuf); //and never open again452 #endif453 445 rd->rBuf = NULL; 454 446 return 0; … … 462 454 rd->SockAddr.sin_addr = sockAddr->sin_addr; 463 455 464 #ifdef EVTDEBUG465 rd->xBuf = malloc (sizeof (CNV_FACT));466 #endif467 456 rd->rBuf = malloc (sizeof (CNV_FACT)); 468 457 if (rd->rBuf == NULL) { … … 546 535 } /*-----------------------------------------------------------------*/ 547 536 548 549 550 551 int 552 mBufEvt (int evID, uint runID, int nRoi[], int sk, 553 int fadlen, int trgTyp, int trgNum, int fadNum) 537 void swapEventHeaderBytes(int i); 538 int checkRoiConsistency(int i, int roi[]); 539 540 int mBufEvt (int sk) 541 /*(int evID, uint runID, int nRoi[], int sk, int fadlen, int trgTyp, int trgNum, int fadNum)*/ 554 542 { 555 543 // generate a new Event into mBuffer: … … 565 553 // size_t needmem = 0; 566 554 555 swapEventHeaderBytes(sk); 556 557 int nRoi[9]; 558 if (!checkRoiConsistency(sk, nRoi)) 559 return -9999; 567 560 568 561 const int b = sk / 7; … … 590 583 } 591 584 585 const int evID = rd[sk].evtID; 586 const uint runID = rd[sk].runID; 587 const int fadlen = rd[sk].fadLen; 588 const int trgTyp = rd[sk].ftmTyp; 589 const int trgNum = rd[sk].ftmID; 590 const int fadNum = rd[sk].evtID; 592 591 593 592 int i = evID % MAX_EVT; … … 1020 1019 } 1021 1020 1021 int checkRoiConsistency(int i, int roi[]) 1022 { 1023 int xjr = -1; 1024 int xkr = -1; 1025 1026 //points to the very first roi 1027 int roiPtr = sizeof(PEVNT_HEADER)/2 + 2; 1028 1029 roi[0] = rd[i].rBuf->S[roiPtr]; 1030 1031 for (int jr = 0; jr < 9; jr++) 1032 { 1033 roi[jr] = rd[i].rBuf->S[roiPtr]; 1034 1035 if (roi[jr]!=roi[0]) 1036 { 1037 xjr = jr; 1038 break; 1039 } 1040 1041 for (int kr = 1; kr < 4; kr++) 1042 { 1043 const int kroi = rd[i].rBuf->S[roiPtr]; 1044 if (kroi != roi[jr]) 1045 { 1046 xjr = jr; 1047 xkr = kr; 1048 break; 1049 } 1050 roiPtr += kroi+4; 1051 } 1052 } 1053 1054 if (xjr<0) 1055 return 1; 1056 1057 if (xkr<0) 1058 factPrintf(kFatal, 1, "Inconsistent Roi accross boards B=%d, expected %d, got %d", xjr, roi[xjr], roi[0]); 1059 else 1060 factPrintf(kFatal, 1, "Inconsistent Roi accross patches B=%d P=%d, expected %d, got %d", xjr, xkr, roi[xjr], rd[i].rBuf->S[roiPtr]); 1061 1062 return 0; 1063 } 1064 1065 void swapEventHeaderBytes(int i) 1066 { 1067 //End of the header. to channels now 1068 int eStart = 36; 1069 for (int ePatchesCount = 0; ePatchesCount<4*9;ePatchesCount++) 1070 { 1071 rd[i].rBuf->S[eStart+0] = ntohs(rd[i].rBuf->S[eStart+0]);//id 1072 rd[i].rBuf->S[eStart+1] = ntohs(rd[i].rBuf->S[eStart+1]);//start_cell 1073 rd[i].rBuf->S[eStart+2] = ntohs(rd[i].rBuf->S[eStart+2]);//roi 1074 rd[i].rBuf->S[eStart+3] = ntohs(rd[i].rBuf->S[eStart+3]);//filling 1075 1076 eStart += 4+rd[i].rBuf->S[eStart+2];//skip the pixel data 1077 } 1078 } 1079 1080 void copyData(int i, int evID, /*int roi,*/ int boardId) 1081 { 1082 memcpy(&mBuffer[evID].FADhead[boardId].start_package_flag, 1083 &rd[i].rBuf->S[0], sizeof(PEVNT_HEADER)); 1084 1085 int src = sizeof(PEVNT_HEADER) / 2; 1086 1087 // consistency of ROIs have been checked already (is it all correct?) 1088 const int roi = rd[i].rBuf->S[src+2]; 1089 1090 // different sort in FAD board..... 1091 for (int px = 0; px < 9; px++) 1092 { 1093 for (int drs = 0; drs < 4; drs++) 1094 { 1095 // pixH = rd[i].rBuf->S[src++]; // ID 1096 src++; 1097 1098 const int pixC = rd[i].rBuf->S[src++]; // start-cell 1099 const int pixR = rd[i].rBuf->S[src++]; // roi 1100 //here we should check if pixH is correct .... 1101 1102 const int pixS = boardId * 36 + drs * 9 + px; 1103 src++; 1104 1105 mBuffer[evID].fEvent->StartPix[pixS] = pixC; 1106 1107 const int dest1 = pixS * roi; 1108 memcpy(&mBuffer[evID].fEvent->Adc_Data[dest1], 1109 &rd[i].rBuf->S[src], roi * 2); 1110 1111 src += pixR; 1112 1113 if (px == 8) 1114 { 1115 const int tmS = boardId * 4 + drs; 1116 1117 //and we have additional TM info 1118 if (pixR > roi) 1119 { 1120 const int dest2 = tmS * roi + NPIX * roi; 1121 1122 const int srcT = src - roi; 1123 mBuffer[evID].fEvent->StartTM[tmS] = (pixC + pixR - roi) % 1024; 1124 1125 memcpy(&mBuffer[evID].fEvent->Adc_Data[dest2], 1126 &rd[i].rBuf->S[srcT], roi * 2); 1127 } 1128 else 1129 { 1130 mBuffer[evID].fEvent->StartTM[tmS] = -1; 1131 1132 //ETIENNE because the TM channels are always processed during drs calib, 1133 //set them to zero if they are not present 1134 //I suspect that it may be more efficient to set all the allocated mem to 1135 //zero when allocating it 1136 // dest = tmS*roi[0] + NPIX*roi[0]; 1137 // bzero(&mBuffer[evID].fEvent->Adc_Data[dest],roi[0]*2); 1138 } 1139 } 1140 } 1141 } 1142 } 1022 1143 1023 1144 … … 1044 1165 1045 1166 1046 void * 1047 readFAD (void *ptr) 1167 void *readFAD (void *ptr) 1048 1168 { 1049 1169 /* *** main loop reading FAD data and sorting them to complete events */ 1050 int actBoards = 0; 1051 uint gi_SecTime; //time in seconds 1052 1053 int sockDef[NBOARDS]; //internal state of sockets 1054 1055 struct timeval tv; 1056 uint32_t tsec, tusec; 1057 1058 1059 factPrintf(kInfo, -1, "Start initializing (readFAD)"); 1170 1171 factPrintf(kInfo, -1, "Start initializing (readFAD)"); 1060 1172 1061 1173 // int cpu = 7; //read thread … … 1075 1187 1076 1188 1189 const int minLen = sizeof(PEVNT_HEADER); //min #bytes needed to check header: full header for debug 1190 1077 1191 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 debug1079 1192 1080 1193 start.S = 0xFB01; … … 1088 1201 } 1089 1202 gi_resetS = gi_resetR = 9; 1090 for (int i = 0; i < NBOARDS; i++) 1091 sockDef[i] = 0; 1203 1204 int sockDef[NBOARDS]; //internal state of sockets 1205 memset(sockDef, 0, NBOARDS*sizeof(int)); 1092 1206 1093 1207 START: 1094 gettimeofday (&tv, NULL);1095 g_actTime = tsec = tv.tv_sec;1096 g_actUsec = tusec = tv.tv_usec;1097 gi_myRun = g_actTime;1098 1208 evtCtrl.frstPtr = 0; 1099 1209 evtCtrl.lastPtr = 0; 1100 1210 1101 gi_SecTime = g_actTime; 1102 gi_runStat = g_runStat; 1103 gj.readStat = g_runStat; 1211 //time in seconds 1212 uint gi_SecTime = time(NULL);; 1104 1213 1105 1214 const int cntsock = 8 - NUMSOCK ; … … 1148 1257 gi_reset = gi_resetR = gi_resetS = gi_resetW = 0; 1149 1258 1150 while (g_runStat >= 0 && g_reset == 0) { //loop until global variable g_runStat claims stop 1151 1259 //loop until global variable g_runStat claims stop 1260 while (g_runStat >= 0 && g_reset == 0) 1261 { 1152 1262 gi_runStat = g_runStat; 1153 1263 gj.readStat = g_runStat; 1264 1265 struct timeval tv; 1154 1266 gettimeofday (&tv, NULL); 1155 g_actTime = t sec = tv.tv_sec;1156 g_actUsec = t usec = tv.tv_usec;1157 1158 1159 int nch = 0;1160 for (int b = 0; b < NBOARDS; b++){1161 int k = b * 7;1162 if (g_port[b].sockDef != sockDef[b]) { //something has changed ...1163 nch++;1164 gi_NumConnect[b] = 0; //must close all connections 1165 gi.numConn[b] = 0;1166 gj.numConn[b] = 0;1167 1168 int s0; 1169 if (sockDef[b] == 0)1170 s0 = 0; //sockets to be defined and opened1171 else if (g_port[b].sockDef == 0)1172 s0 = -1; //sockets to be destroyed 1173 else1174 s0 = +1; //sockets to be closed and reopened1175 1176 const int p0 = s0==0 ? ntohs (g_port[b].sockAddr.sin_port) : 0; 1177 1178 for (int p = p0 + 1; p < p0 + 8; p++) { 1179 GenSock (s0, k, p, &g_port[b].sockAddr, &rd[k]); //generate address and socket1180 k++;1181 }1182 sockDef[b] = g_port[b].sockDef; 1183 }1267 g_actTime = tv.tv_sec; 1268 g_actUsec = tv.tv_usec; 1269 1270 1271 for (int b = 0; b < NBOARDS; b++) 1272 { 1273 // Nothing has changed 1274 if (g_port[b].sockDef == sockDef[b]) 1275 continue; 1276 1277 gi_NumConnect[b] = 0; //must close all connections 1278 gi.numConn[b] = 0; 1279 gj.numConn[b] = 0; 1280 1281 // s0 = 0: sockets to be defined and opened 1282 // s0 = -1: sockets to be destroyed 1283 // s0 = +1: sockets to be closed and reopened 1284 1285 int s0 = 0; 1286 if (sockDef[b] != 0) 1287 s0 = g_port[b].sockDef==0 ? -1 : +1; 1288 1289 const int p0 = s0==0 ? ntohs (g_port[b].sockAddr.sin_port) : 0; 1290 1291 int k = b * 7; 1292 for (int p = p0 + 1; p < p0 + 8; p++, k++) 1293 GenSock (s0, k, p, &g_port[b].sockAddr, &rd[k]); //generate address and socket 1294 1295 sockDef[b] = g_port[b].sockDef; 1184 1296 } 1185 1297 1186 if (nch > 0) { 1187 actBoards = 0; 1188 for (int b = 0; b < NBOARDS; b++) { 1189 if (sockDef[b] > 0) 1190 actBoards++; 1191 } 1192 } 1193 1194 1195 #ifdef EVTDEBUG 1196 int jrdx = 0; 1197 #endif 1198 1199 int numokx = 0; 1200 int numok = 0; //count number of succesfull actions 1298 // count the number of active boards 1299 int actBoards = 0; 1300 for (int b = 0; b < NBOARDS; b++) 1301 if (sockDef[b] > 0) 1302 actBoards++; 1303 1304 //count number of succesfull actions 1305 int numok = 0; 1201 1306 1202 1307 /* … … 1216 1321 */ 1217 1322 1218 for (int i = 0; i < MAX_SOCK; i+=7) { //check all sockets if something to read 1323 //check all sockets if something to read 1324 for (int i = 0; i < MAX_SOCK; i+=7) 1325 { 1326 // Do not try to connect this socket 1327 if (rd[i].sockStat > 0) 1328 continue; 1219 1329 1220 1330 const int b = i / 7 ; 1221 1331 //const int p = i % 7 ; 1222 1332 1223 /*if ( p >= NUMSOCK) { ; } 1224 else*/ { 1225 const int s0 = sockDef[b] > 0 ? +1 : -1; 1226 1227 if (rd[i].sockStat < 0) { //try to connect if not yet done 1228 if (rd[i].sockStat == -1) { 1229 rd[i].sockStat = connect (rd[i].socket, 1230 (struct sockaddr *) &rd[i].SockAddr, 1231 sizeof (rd[i].SockAddr)); 1232 if (rd[i].sockStat == -1) { 1233 rd[i].errCnt++ ; 1234 usleep(25000); 1235 // if (rd[i].errCnt < 100) rd[i].sockStat = rd[i].errCnt ; 1236 // else if (rd[i].errCnt < 1000) rd[i].sockStat = 1000 ; 1237 // else rd[i].sockStat = 10000 ; 1238 } 1239 //printf("try to connect %d -> %d\n",i,rd[i].sockStat); 1240 1241 } 1242 if (rd[i].sockStat < -1 ) { 1243 rd[i].sockStat++ ; 1244 } 1245 if (rd[i].sockStat == 0) { //successfull ==> 1246 if (sockDef[b] > 0) { 1247 rd[i].bufTyp = 0; // expect a header 1248 rd[i].bufLen = frst_len; // max size to read at begining 1249 } else { 1250 rd[i].bufTyp = -1; // full data to be skipped 1251 rd[i].bufLen = MAX_LEN; //huge for skipping 1252 } 1253 rd[i].bufPos = 0; // no byte read so far 1254 rd[i].skip = 0; // start empty 1255 // gi_NumConnect[b]++; 1256 gi_NumConnect[b] += cntsock ; 1257 1258 gi.numConn[b]++; 1259 gj.numConn[b]++; 1260 factPrintf(kInfo, -1, "New connection %d (number of connections: %d)", b, gi.numConn[b]); 1261 } 1262 } 1263 1264 if (rd[i].sockStat == 0) { //we have a connection ==> try to read 1265 1266 if (rd[i].bufLen<=0) 1267 { 1333 if (rd[i].sockStat == -1) 1334 { 1335 //try to connect if not yet done 1336 rd[i].sockStat = connect (rd[i].socket, 1337 (struct sockaddr *) &rd[i].SockAddr, 1338 sizeof (rd[i].SockAddr)); 1339 // Failed 1340 if (rd[i].sockStat == -1) 1341 { 1342 rd[i].errCnt++; 1343 usleep(25000); 1344 continue; 1345 } 1346 1347 // Success (rd[i].sockStat == 0) 1348 1349 if (sockDef[b] > 0) 1350 { 1351 rd[i].bufTyp = 0; // expect a header 1352 rd[i].bufLen = frst_len; // max size to read at begining 1353 } 1354 else 1355 { 1356 rd[i].bufTyp = -1; // full data to be skipped 1357 rd[i].bufLen = MAX_LEN; // huge for skipping 1358 } 1359 1360 rd[i].bufPos = 0; // no byte read so far 1361 rd[i].skip = 0; // start empty 1362 1363 gi_NumConnect[b] += cntsock; 1364 1365 gi.numConn[b]++; 1366 gj.numConn[b]++; 1367 1368 factPrintf(kInfo, -1, "New connection %d (number of connections: %d)", b, gi.numConn[b]); 1369 } 1370 1371 // Do not read from this socket 1372 if (rd[i].bufLen<=0) 1373 continue; 1374 1375 numok++; 1376 1377 const int32_t jrd = 1378 recv(rd[i].socket, &rd[i].rBuf->B[rd[i].bufPos], 1379 rd[i].bufLen, MSG_DONTWAIT); 1380 1381 // recv failed 1382 if (jrd<0) 1383 { 1384 // There was just nothing waiting 1385 if (errno==EWOULDBLOCK || errno==EAGAIN) 1386 { 1387 numok--; 1388 continue; 1389 } 1390 1391 factPrintf(kError, 442, "Reading from socket %d failed: %m (recv,rc=%d)", i, errno); 1392 gi.gotErr[b]++; 1393 continue; 1394 } 1395 1396 // connection was closed ... 1397 if (jrd==0) 1398 { 1399 factPrintf(kInfo, 441, "Socket %d closed by FAD", i); 1400 1401 const int s0 = sockDef[b] > 0 ? +1 : -1; 1402 GenSock(s0, i, 0, NULL, &rd[i]); 1403 1404 gi.gotErr[b]++; 1405 1406 gi_NumConnect[b]-= cntsock ; 1407 gi.numConn[b]--; 1408 gj.numConn[b]--; 1409 1410 continue; 1411 } 1412 1413 // Success (jrd > 0) 1414 1415 gi.gotByte[b] += jrd; 1416 gj.rateBytes[b] += jrd; 1417 1418 // are we skipping this board ... 1419 if (rd[i].bufTyp < 0) 1420 continue; 1421 1422 rd[i].bufPos += jrd; //==> prepare for continuation 1423 rd[i].bufLen -= jrd; 1424 1425 // are we reading data? 1426 if (rd[i].bufTyp > 0) 1427 { 1428 // not yet all read 1429 if (rd[i].bufLen > 0) 1430 continue; 1431 1432 // ready to read next header 1433 rd[i].bufTyp = 0; 1434 rd[i].bufLen = frst_len; 1435 rd[i].bufPos = 0; 1436 1437 if (rd[i].rBuf->B[rd[i].fadLen - 1] != stop.B[0] || 1438 rd[i].rBuf->B[rd[i].fadLen - 2] != stop.B[1]) 1439 { 1440 gi.evtErr++; 1441 factPrintf(kError, 301, "End-of-event flag wrong on socket %3d for event %4d (len=%5d), expected %3d %3d, got %3d %3d", 1442 i, rd[i].evtID, rd[i].fadLen, stop.B[0], stop.B[1], 1443 rd[i].rBuf->B[rd[i].fadLen - 1], rd[i].rBuf->B[rd[i].fadLen - 2]); 1444 continue; 1445 } 1446 1268 1447 #ifdef EVTDEBUG 1269 factPrintf(kDebug, 301, "do not read from socket %d %d", i, rd[i].bufLen);1448 debugRead (i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, 1, tv.tv_sec, tv.tv_usec); // i=socket; jrd=#bytes; ievt=eventid; 1=finished event 1270 1449 #endif 1271 continue; 1272 } 1273 1274 // if (rd[i].bufLen > 0) { //might be nothing to read [buffer full] 1275 numok++; 1276 1277 const int32_t jrd = 1278 recv (rd[i].socket, &rd[i].rBuf->B[rd[i].bufPos], 1279 rd[i].bufLen, MSG_DONTWAIT); 1280 1281 #ifdef EVTDEBUG 1282 if (jrd > 0) { 1283 debugStream (i, &rd[i].rBuf->B[rd[i].bufPos], jrd); 1284 memcpy (&rd[i].xBuf->B[rd[i].bufPos], 1285 &rd[i].rBuf->B[rd[i].bufPos], jrd); 1286 factPrintf(kDebug, 301, "read sock %3d bytes %5d len %5d first %d %d", 1287 i, jrd, rd[i].bufLen, rd[i].rBuf->B[rd[i].bufPos], rd[i].rBuf->B[rd[i].bufPos + 1]); 1288 } 1289 #endif 1290 1291 if (jrd == 0) { //connection has closed ... 1292 factPrintf(kInfo, 441, "Socket %d closed by FAD", i); 1293 GenSock (s0, i, 0, NULL, &rd[i]); 1294 gi.gotErr[b]++; 1295 // gi_NumConnect[b]--; 1296 gi_NumConnect[b]-= cntsock ; 1297 gi.numConn[b]--; 1298 gj.numConn[b]--; 1299 1300 } else if (jrd < 0) { //did not read anything 1301 if (errno != EAGAIN && errno != EWOULDBLOCK) { 1302 factPrintf(kError, 442, "Reading from socket %d failed: %m (recv,rc=%d)", i, errno); 1303 gi.gotErr[b]++; 1304 } else 1305 numok--; //else nothing waiting to be read 1306 //jrd = 0; 1307 } 1308 /* } else { 1309 // jrd = 0; //did read nothing as requested 1310 #ifdef EVTDEBUG 1311 snprintf (str, MXSTR, "do not read from socket %d %d", i, 1312 rd[i].bufLen); 1313 factOut (kDebug, 301, str); 1314 #endif 1315 } 1316 */ 1317 if (jrd<=0) 1318 continue; 1319 1320 gi.gotByte[b] += jrd; 1321 gj.rateBytes[b] += jrd; 1322 1323 //if (jrd > 0) { 1324 numokx++; 1325 #ifdef EVTDEBUG 1326 jrdx += jrd; 1327 #endif 1328 //} 1329 1330 1331 if (rd[i].bufTyp < 0) { // we are skipping this board ... 1332 // just do nothing 1333 #ifdef EVTDEBUG 1334 factPrintf(kInfo, 301, "skipping %d bytes on socket %d", jrd, i); 1335 #endif 1336 continue; 1337 } 1338 1339 rd[i].bufPos += jrd; //==> prepare for continuation 1340 rd[i].bufLen -= jrd; 1341 1342 if (rd[i].bufTyp > 0) { // we are reading data ... 1343 1344 if (rd[i].bufLen>0/*jrd < rd[i].bufLen*/) { //not yet all read 1345 #ifdef EVTDEBUG 1346 debugRead (i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, 0, tsec, tusec); // i=socket; jrd=#bytes; ievt=eventid; 0=reading data 1347 #endif 1450 1451 // int actid; 1452 // if (g_useFTM > 0) 1453 // actid = rd[i].evtID; 1454 // else 1455 // actid = rd[i].ftmID; 1456 1457 //get index into mBuffer for this event (create if needed) 1458 const int evID = mBufEvt(i); 1459 1460 if (evID < -1000) 1348 1461 continue; 1349 } 1350 1351 //int pos = rdi_bufPos = rd[i].bufPos; 1352 //int pos = rdi_bufLen = rd[i].bufLen; 1353 //int pos = rdi_bufTyp = rd[i].bufTyp; 1354 1355 rd[i].bufTyp = 0; //ready to read next header 1356 rd[i].bufLen = frst_len; 1357 rd[i].bufPos = 0; 1358 1359 //{ 1360 //full dataset read 1361 //rd[i].bufLen = 0; 1362 //rd[i].bufPos = rd[i].fadLen; 1363 if (rd[i].rBuf->B[rd[i].fadLen - 1] != stop.B[0] || 1364 rd[i].rBuf->B[rd[i].fadLen - 2] != stop.B[1]) { 1365 gi.evtErr++; 1366 factPrintf(kError, 301, "End-of-event flag wrong on socket %3d for event %4d (len=%5d), expected %3d %3d, got %3d %3d", 1367 i, rd[i].evtID, rd[i].fadLen, stop.B[0], stop.B[1], 1368 rd[i].rBuf->B[rd[i].fadLen - 1], rd[i].rBuf->B[rd[i].fadLen - 2]); 1369 //goto EndBuf; 1370 continue; 1371 1372 #ifdef EVTDEBUG 1373 } else { 1374 factPrintf(kDebug, 301, "good end of buffer found sock %3d len %5d %d %d : %d %d - %d %d : %d %d", 1375 i, rd[i].fadLen, rd[i].rBuf->B[0], 1376 rd[i].rBuf->B[1], start.B[1], start.B[0], 1377 rd[i].rBuf->B[rd[i].fadLen - 2], 1378 rd[i].rBuf->B[rd[i].fadLen - 1], stop.B[1], 1379 stop.B[0]); 1380 #endif 1381 } 1382 1383 #ifdef EVTDEBUG 1384 //if (jrd > 0) 1385 debugRead (i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, 1, tsec, tusec); // i=socket; jrd=#bytes; ievt=eventid; 1=finished event 1386 #endif 1387 1388 /* //we have a complete buffer, copy to WORK area 1389 int jr, kr; 1390 int checkRoi; 1391 roi[0] = ntohs (rd[i].rBuf->S[head_len / 2 + 2]); 1392 for (kr = 1; kr < 4; kr++) 1462 1463 // no space left, retry later 1464 if (evID < 0) 1465 { 1466 rd[i].bufTyp = -1; 1467 rd[i].bufLen = 0; 1468 rd[i].bufPos = rd[i].fadLen; 1469 continue; 1470 } 1471 1472 //we have a valid entry in mBuffer[]; fill it 1473 1474 const int boardId = b; 1475 const int fadBoard = rd[i].rBuf->S[12]; 1476 const int fadCrate = fadBoard / 256; 1477 1478 if (boardId != (fadCrate * 10 + fadBoard % 256)) 1479 { 1480 factPrintf(kWarn, 301, "Board ID mismatch. Expected %d, got %d (C=%d, B=%d)", 1481 boardId, fadBoard, fadCrate, fadBoard % 256); 1482 } 1483 1484 if (mBuffer[evID].board[boardId] != -1) 1485 { 1486 factPrintf(kWarn, 501, "Got event %5d from board %3d (i=%3d, len=%5d) twice: Starts with %3d %3d - ends with %3d %3d", 1487 evID, boardId, i, rd[i].fadLen, 1488 rd[i].rBuf->B[0], rd[i].rBuf->B[1], 1489 rd[i].rBuf->B[rd[i].fadLen - 2], 1490 rd[i].rBuf->B[rd[i].fadLen - 1]); 1491 // FIXME: Is that action the right one? 1492 continue; 1493 } 1494 1495 // Copy data from rd[i] to mBuffer[evID] 1496 copyData(i, evID, boardId); 1497 1498 // now we have stored a new board contents into Event structure 1499 1500 const int iDx = evtIdx[evID]; //index into evtCtrl 1501 1502 evtCtrl.evtStat[iDx]++; 1503 evtCtrl.pcTime[iDx] = g_actTime; 1504 1505 mBuffer[evID].fEvent->NumBoards++; 1506 mBuffer[evID].board[boardId] = boardId; 1507 mBuffer[evID].nBoard++; 1508 1509 // have we already reported first (partial) event of this run ??? 1510 if (mBuffer[evID].nBoard>0 && mBuffer[evID].runNum != actrun) 1511 { 1512 actrun = mBuffer[evID].runNum; 1513 1514 for (int ir = 0; ir < MAX_RUN; ir++) 1393 1515 { 1394 checkRoi = ntohs(rd[i].rBuf->S[head_len/ 2 + 1395 + kr*(roi[jr-1]+4)]); 1396 if (checkRoi != roi[0]) 1516 if (runCtrl[ir].runId == actrun) 1397 1517 { 1398 snprintf (str, MXSTR, "Inconsistent Roi accross board patches"); 1399 factOut (kFatal, 1, str); 1400 goto EndBuf; 1401 } 1402 1403 } 1404 for (jr = 1; jr < 9; jr++) { 1405 roi[jr] = 1406 ntohs (rd[i]. 1407 rBuf->S[head_len / 2 + 2 + jr * (roi[jr-1] + 4)]); 1408 1409 } 1410 */ 1411 //we have a complete buffer, copy to WORK area 1412 1413 1414 //End of the header. to channels now 1415 int eStart = 36; 1416 for (int ePatchesCount = 0; ePatchesCount<4*9;ePatchesCount++) 1417 { 1418 rd[i].rBuf->S[eStart+0] = ntohs(rd[i].rBuf->S[eStart+0]);//id 1419 rd[i].rBuf->S[eStart+1] = ntohs(rd[i].rBuf->S[eStart+1]);//start_cell 1420 rd[i].rBuf->S[eStart+2] = ntohs(rd[i].rBuf->S[eStart+2]);//roi 1421 rd[i].rBuf->S[eStart+3] = ntohs(rd[i].rBuf->S[eStart+3]);//filling 1422 1423 eStart += 4+rd[i].rBuf->S[eStart+2];//skip the pixel data 1424 } 1425 1426 //channels done. footer now 1427 // rd[i].rBuf->S[eStart+eCount] = ntohs(rd[i].rBuf->S[eStart+eCount]);//package_crc 1428 // eCount++; 1429 // rd[i].rBuf->S[eStart+eCount] = ntohs(rd[i].rBuf->S[eStart+eCount]);//end_package_flag 1430 // snprintf(str, MXSTR, "Bytes read %d bytes swapped %d", jrd, eCount*2); 1431 // factOut(kInfo, 000, str); 1432 //ETIENNE end of bytes swapping already 1433 1434 1435 int checkRoi; 1436 int roiHopper = sizeof(PEVNT_HEADER)/2 + 2; //points to the very first roi 1437 int roi[9]; 1438 roi[0] = rd[i].rBuf->S[roiHopper]; 1439 roiHopper += roi[0]+4;//skip to the second roi (i.e. next board, same patch-pixel) 1440 for (int kr = 1; kr < 4; kr++) 1441 { 1442 checkRoi = rd[i].rBuf->S[roiHopper]; 1443 if (checkRoi != roi[0]) 1444 { 1445 factPrintf(kError, 1, "Inconsistent Roi accross boards B=%d, expected %d, got %d", kr, checkRoi, roi[0]); 1446 // goto EndBuf; 1447 continue; 1448 } 1449 roiHopper += checkRoi+4; 1450 } 1451 //roiHopper now points to the first pixel of board 2. Do the 8 remaining pixels 1452 for (int jr = 1; jr < 9; jr++) { 1453 roi[jr] = rd[i].rBuf->S[roiHopper]; 1454 checkRoi = roi[jr]; 1455 for (int kr = 1; kr < 4; kr++) 1456 { 1457 roiHopper += checkRoi+4; 1458 checkRoi = rd[i].rBuf->S[roiHopper]; 1459 if (checkRoi != roi[jr]) 1460 { 1461 factPrintf(kFatal, 1, "Inconsistent Roi accross patches B=%d P=%d, expected %d, got %d", kr, jr, roi[jr], checkRoi); 1462 // goto EndBuf; 1463 continue; 1464 } 1465 } 1466 } 1467 //get index into mBuffer for this event (create if needed) 1468 1469 // int actid; 1470 // if (g_useFTM > 0) 1471 // actid = rd[i].evtID; 1472 // else 1473 // actid = rd[i].ftmID; 1474 1475 const int evID = mBufEvt (rd[i].evtID, rd[i].runID, roi, i, 1476 rd[i].fadLen, rd[i].ftmTyp, rd[i].ftmID, 1477 rd[i].evtID); 1478 1479 if (evID < -1000) { 1480 // goto EndBuf; //not usable board/event/run --> skip it 1481 //rd[i].bufTyp = 0; //ready to read next header 1482 //rd[i].bufLen = frst_len; 1483 //rd[i].bufPos = 0; 1484 continue; 1485 } 1486 if (evID < 0) { //no space left, retry later 1487 #ifdef EVTDEBUG 1488 if (rd[i].bufLen != 0) { 1489 factPrintf(kFatal, 1, "something screwed up"); 1490 } 1491 #endif 1492 rd[i].bufTyp = -1; 1493 rd[i].bufLen = 0; 1494 rd[i].bufPos = rd[i].fadLen; 1495 1496 //xwait.tv_sec = 0; 1497 //xwait.tv_nsec = 10000000; // sleep for ~10 msec 1498 //nanosleep (&xwait, NULL); 1499 continue; 1500 //goto EndBuf1; //hope there is free space next round 1501 } 1502 //we have a valid entry in mBuffer[]; fill it 1503 1504 #ifdef EVTDEBUG 1505 int xchk = memcmp (&rd[i].xBuf->B[0], &rd[i].rBuf->B[0], 1506 rd[i].fadLen); 1507 if (xchk != 0) { 1508 factPrintf(kFatal, 1, "ERROR OVERWRITE %d %d on port %d", xchk, rd[i].fadLen, i); 1509 1510 uint iq; 1511 for (iq = 0; iq < rd[i].fadLen; iq++) { 1512 if (rd[i].rBuf->B[iq] != rd[i].xBuf->B[iq]) { 1513 factPrintf(kFatal, 1, "ERROR %4d %4d %x %x", i, iq, rd[i].rBuf->B[iq], rd[i].xBuf->B[iq]); 1514 } 1515 } 1516 } 1517 #endif 1518 const int boardId = b; 1519 const int fadBoard = rd[i].rBuf->S[12]; 1520 const int fadCrate = fadBoard / 256; 1521 1522 if (boardId != (fadCrate * 10 + fadBoard % 256)) { 1523 factPrintf(kWarn, 301, "Board ID mismatch. Expected %d, got %d (C=%d, B=%d)", 1524 boardId, fadBoard, fadCrate, fadBoard % 256); 1525 } 1526 if (mBuffer[evID].board[boardId] != -1) { 1527 factPrintf(kWarn, 501, "Got event %5d from board %3d (i=%3d, len=%5d) twice: Starts with %3d %3d - ends with %3d %3d", 1528 evID, boardId, i, rd[i].fadLen, 1529 rd[i].rBuf->B[0], rd[i].rBuf->B[1], 1530 rd[i].rBuf->B[rd[i].fadLen - 2], 1531 rd[i].rBuf->B[rd[i].fadLen - 1]); 1532 // goto EndBuf; //--> skip Board 1533 //rd[i].bufTyp = 0; //ready to read next header 1534 //rd[i].bufLen = frst_len; 1535 //rd[i].bufPos = 0; 1536 continue; 1537 } 1538 1539 const int iDx = evtIdx[evID]; //index into evtCtrl 1540 1541 memcpy (&mBuffer[evID].FADhead[boardId].start_package_flag, 1542 &rd[i].rBuf->S[0], sizeof(PEVNT_HEADER)); 1543 1544 int src = sizeof(PEVNT_HEADER) / 2; 1545 for (int px = 0; px < 9; px++) { //different sort in FAD board..... 1546 for (int drs = 0; drs < 4; drs++) { 1547 // pixH = rd[i].rBuf->S[src++]; // ID 1548 src++; 1549 const int pixC = rd[i].rBuf->S[src++]; // start-cell 1550 const int pixR = rd[i].rBuf->S[src++]; // roi 1551 //here we should check if pixH is correct .... 1552 1553 const int pixS = boardId * 36 + drs * 9 + px; 1554 src++; 1555 1556 1557 mBuffer[evID].fEvent->StartPix[pixS] = pixC; 1558 1559 int dest = pixS * roi[0]; 1560 memcpy (&mBuffer[evID].fEvent->Adc_Data[dest], 1561 &rd[i].rBuf->S[src], roi[0] * 2); 1562 src += pixR; 1563 1564 if (px == 8) { 1565 const int tmS = boardId * 4 + drs; 1566 if (pixR > roi[0]) { //and we have additional TM info 1567 dest = tmS * roi[0] + NPIX * roi[0]; 1568 int srcT = src - roi[0]; 1569 mBuffer[evID].fEvent->StartTM[tmS] = 1570 (pixC + pixR - roi[0]) % 1024; 1571 memcpy (&mBuffer[evID].fEvent->Adc_Data[dest], 1572 &rd[i].rBuf->S[srcT], roi[0] * 2); 1573 } else { 1574 mBuffer[evID].fEvent->StartTM[tmS] = -1; 1575 //ETIENNE because the TM channels are always processed during drs calib, 1576 //set them to zero if they are not present 1577 //I suspect that it may be more efficient to set all the allocated mem to 1578 //zero when allocating it 1579 // dest = tmS*roi[0] + NPIX*roi[0]; 1580 // bzero(&mBuffer[evID].fEvent->Adc_Data[dest],roi[0]*2); 1581 } 1582 } 1583 } 1584 } // now we have stored a new board contents into Event structure 1585 1586 mBuffer[evID].fEvent->NumBoards++; 1587 mBuffer[evID].board[boardId] = boardId; 1588 evtCtrl.evtStat[iDx]++; 1589 evtCtrl.pcTime[iDx] = g_actTime; 1590 1591 mBuffer[evID].nBoard++; 1592 1593 // have we already reported first (partial) event of this run ??? 1594 if (mBuffer[evID].nBoard>0 && mBuffer[evID].runNum != actrun) 1595 { 1596 actrun = mBuffer[evID].runNum; 1597 1598 for (int ir = 0; ir < MAX_RUN; ir++) 1599 { 1600 if (runCtrl[ir].runId == actrun) 1518 if (++runCtrl[ir].lastEvt == 0) 1601 1519 { 1602 if (++runCtrl[ir].lastEvt == 0) 1603 { 1604 gotNewRun (actrun, NULL); 1605 factPrintf(kInfo, 1, "gotNewRun called for run %d, event %d", 1606 mBuffer[evID].runNum, mBuffer[evID].evNum); 1607 break; 1608 } 1520 gotNewRun (actrun, NULL); 1521 factPrintf(kInfo, 1, "gotNewRun called for run %d, event %d", 1522 mBuffer[evID].runNum, mBuffer[evID].evNum); 1523 break; 1609 1524 } 1610 1525 } 1611 1526 } 1612 1613 //complete event read ---> flag for next processing 1614 if (mBuffer[evID].nBoard >= actBoards) 1615 { 1616 evtCtrl.evtStat[iDx] = 99; 1617 gi.evtTot++; 1618 } 1619 1620 1621 /* 1622 if (mBuffer[evID].nBoard >= actBoards) 1623 { 1624 // have we already reported first event of this run ??? 1625 if (mBuffer[evID].runNum != actrun) 1626 { 1627 actrun = mBuffer[evID].runNum; 1628 1629 for (int ir = 0; ir < MAX_RUN; ir++) 1630 { 1631 if (runCtrl[ir].runId == actrun) 1632 { 1633 if (++runCtrl[ir].lastEvt == 0) 1634 { 1635 gotNewRun (actrun, mBuffer[evID].FADhead); 1636 factPrintf(kInfo, 1, "gotNewRun called for run %d, event %d", 1637 mBuffer[evID].runNum, mBuffer[evID].evNum); 1638 break; 1639 } 1640 } 1641 } 1642 } 1643 //complete event read ---> flag for next processing 1644 evtCtrl.evtStat[iDx] = 99; 1645 gi.evtTot++; 1646 } 1647 */ 1648 // EndBuf: 1649 //rd[i].bufTyp = 0; //ready to read next header 1650 //rd[i].bufLen = frst_len; 1651 //rd[i].bufPos = 0; 1652 //EndBuf1: 1653 ; 1654 //} 1655 1656 } else { //we are reading event header 1657 //rd[i].bufPos += jrd; 1658 //rd[i].bufLen -= jrd; 1659 if (rd[i].bufPos < minLen) //{ //sufficient data to take action 1660 { 1661 #ifdef EVTDEBUG 1662 debugRead (i, jrd, 0, 0, 0, -2, tsec, tusec); // i=socket; jrd=#bytes; ievt=eventid; -2=start event, unknown id yet 1663 #endif 1664 continue; 1665 } 1666 1667 //check if startflag correct; else shift block .... 1668 int k; 1669 for (k = 0; k < rd[i].bufPos - 1; k++) { 1670 if (rd[i].rBuf->B[k] == start.B[1] 1671 && rd[i].rBuf->B[k + 1] == start.B[0]) 1672 break; 1673 } 1674 rd[i].skip += k; 1675 1676 if (k >= rd[i].bufPos - 1) { //no start of header found 1677 rd[i].bufPos = 0; 1678 rd[i].bufLen = sizeof(PEVNT_HEADER); 1679 continue; 1680 } 1681 1682 if (k > 0) { 1683 rd[i].bufPos -= k; 1684 rd[i].bufLen += k; 1685 memmove (&rd[i].rBuf->B[0], &rd[i].rBuf->B[k], 1686 rd[i].bufPos); 1687 #ifdef EVTDEBUG 1688 memcpy (&rd[i].xBuf->B[0], &rd[i].xBuf->B[k], 1689 rd[i].bufPos); 1690 #endif 1691 } 1692 1693 if (rd[i].bufPos < minLen) 1694 { 1695 #ifdef EVTDEBUG 1696 debugRead (i, jrd, 0, 0, 0, -2, tsec, tusec); // i=socket; jrd=#bytes; ievt=eventid; -2=start event, unknown id yet 1697 #endif 1698 continue; 1699 } 1700 1701 //{ 1702 if (rd[i].skip > 0) { 1703 factPrintf(kInfo, 666, "Skipped %d bytes on port %d", rd[i].skip, i); 1704 rd[i].skip = 0; 1705 } 1706 1707 // TGB: This needs much more checks than just the first two bytes! 1708 1709 // Swap everything except start_package_flag. 1710 // It is to difficult to find out where it is used how, 1711 // but it doesn't really matter because it is not really 1712 // used anywehere else 1713 // rd[i].rBuf->S[1] = ntohs(rd[i].rBuf->S[1]); // package_length 1714 rd[i].rBuf->S[2] = ntohs(rd[i].rBuf->S[2]); // version_no 1715 rd[i].rBuf->S[3] = ntohs(rd[i].rBuf->S[3]); // PLLLCK 1716 rd[i].rBuf->S[4] = ntohs(rd[i].rBuf->S[4]); // trigger_crc 1717 rd[i].rBuf->S[5] = ntohs(rd[i].rBuf->S[5]); // trigger_type 1718 1719 rd[i].rBuf->S[12] = ntohs(rd[i].rBuf->S[12]); // board id 1720 rd[i].rBuf->S[13] = ntohs(rd[i].rBuf->S[13]); // adc_clock_phase_shift 1721 rd[i].rBuf->S[14] = ntohs(rd[i].rBuf->S[14]); // number_of_triggers_to_generate 1722 rd[i].rBuf->S[15] = ntohs(rd[i].rBuf->S[15]); // trigger_generator_prescaler 1723 1724 rd[i].rBuf->I[3] = ntohl(rd[i].rBuf->I[3]); // trigger_id 1725 rd[i].rBuf->I[4] = ntohl(rd[i].rBuf->I[4]); // fad_evt_counter 1726 rd[i].rBuf->I[5] = ntohl(rd[i].rBuf->I[5]); // REFCLK_frequency 1727 1728 rd[i].rBuf->I[10] = ntohl(rd[i].rBuf->I[10]); // runnumber; 1729 rd[i].rBuf->I[11] = ntohl(rd[i].rBuf->I[11]); // time; 1730 1731 for (int s=24;s<24+NTemp+NDAC;s++) 1732 rd[i].rBuf->S[s] = ntohs(rd[i].rBuf->S[s]); // drs_temperature / dac 1733 1734 rd[i].fadLen = ntohs(rd[i].rBuf->S[1]) * 2; 1735 rd[i].fadVers = rd[i].rBuf->S[2]; 1736 rd[i].ftmTyp = rd[i].rBuf->S[5]; 1737 rd[i].ftmID = rd[i].rBuf->I[3]; //(FTMevt) 1738 rd[i].evtID = rd[i].rBuf->I[4]; //(FADevt) 1739 rd[i].runID = rd[i].rBuf->I[11]; 1740 rd[i].bufTyp = 1; //ready to read full record 1741 rd[i].bufLen = rd[i].fadLen - rd[i].bufPos; 1742 1743 #ifdef EVTDEBUG 1744 int fadboard = rd[i].rBuf->S[12]; 1745 int fadcrate = fadboard / 256; 1746 fadboard = (fadcrate * 10 + fadboard % 256); 1747 factPrintf(kDebug, 1, "sk %3d head: %5d %5d %5d %10d %4d %6d", 1748 i, rd[i].fadLen, rd[i].evtID, rd[i].ftmID, rd[i].runID, fadboard, jrd); 1749 #endif 1750 1751 if (rd[i].runID == 0) 1752 rd[i].runID = gi_myRun; 1753 1754 /*if (rd[i].bufLen <= head_len || rd[i].bufLen > MAX_LEN) { 1755 snprintf (str, MXSTR, 1756 "Illegal event-length %d on port %d, %d expected.", rd[i].bufLen, i, head_len); 1757 factOut (kFatal, 881, str); 1758 rd[i].bufLen = 100000; //? 1759 }*/ 1760 1761 int fadBoard = rd[i].rBuf->S[12]; 1762 debugHead (i, fadBoard, rd[i].rBuf); 1763 debugRead (i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, -1, tsec, tusec); // i=socket; jrd=#bytes; ievt=eventid;-1=start event 1764 } //end interpreting last read 1765 } 1766 } //end of successful read anything 1767 } //finished trying to read all sockets 1768 1769 #ifdef EVTDEBUG 1770 factDebug(kDebug, -1, "Loop ---- %3d --- %8d", numokx, jrdx); 1771 #endif 1527 } 1528 1529 //complete event read ---> flag for next processing 1530 if (mBuffer[evID].nBoard >= actBoards) 1531 { 1532 evtCtrl.evtStat[iDx] = 99; 1533 gi.evtTot++; 1534 } 1535 } 1536 else 1537 { 1538 //we are reading event header 1539 1540 //not yet sufficient data to take action 1541 if (rd[i].bufPos < minLen) 1542 continue; 1543 1544 //check if startflag correct; else shift block .... 1545 // FIXME: This is not enough... this combination of 1546 // bytes can be anywhere... at least the end bytes 1547 // must be checked somewhere, too. 1548 int k; 1549 for (k = 0; k < rd[i].bufPos - 1; k++) 1550 { 1551 if (rd[i].rBuf->B[k] == start.B[1] && rd[i].rBuf->B[k+1] == start.B[0]) 1552 break; 1553 } 1554 rd[i].skip += k; 1555 1556 //no start of header found 1557 if (k >= rd[i].bufPos - 1) 1558 { 1559 rd[i].bufPos = 0; 1560 rd[i].bufLen = sizeof(PEVNT_HEADER); 1561 continue; 1562 } 1563 1564 if (k > 0) 1565 { 1566 rd[i].bufPos -= k; 1567 rd[i].bufLen += k; 1568 memmove (&rd[i].rBuf->B[0], &rd[i].rBuf->B[k], 1569 rd[i].bufPos); 1570 } 1571 1572 if (rd[i].bufPos < minLen) 1573 continue; 1574 1575 if (rd[i].skip > 0) 1576 { 1577 factPrintf(kInfo, 666, "Skipped %d bytes on port %d", rd[i].skip, i); 1578 rd[i].skip = 0; 1579 } 1580 1581 // TGB: This needs much more checks than just the first two bytes! 1582 1583 // Swap everything except start_package_flag. 1584 // It is to difficult to find out where it is used how, 1585 // but it doesn't really matter because it is not really 1586 // used anywehere else 1587 // rd[i].rBuf->S[1] = ntohs(rd[i].rBuf->S[1]); // package_length 1588 rd[i].rBuf->S[2] = ntohs(rd[i].rBuf->S[2]); // version_no 1589 rd[i].rBuf->S[3] = ntohs(rd[i].rBuf->S[3]); // PLLLCK 1590 rd[i].rBuf->S[4] = ntohs(rd[i].rBuf->S[4]); // trigger_crc 1591 rd[i].rBuf->S[5] = ntohs(rd[i].rBuf->S[5]); // trigger_type 1592 1593 rd[i].rBuf->S[12] = ntohs(rd[i].rBuf->S[12]); // board id 1594 rd[i].rBuf->S[13] = ntohs(rd[i].rBuf->S[13]); // adc_clock_phase_shift 1595 rd[i].rBuf->S[14] = ntohs(rd[i].rBuf->S[14]); // number_of_triggers_to_generate 1596 rd[i].rBuf->S[15] = ntohs(rd[i].rBuf->S[15]); // trigger_generator_prescaler 1597 1598 rd[i].rBuf->I[3] = ntohl(rd[i].rBuf->I[3]); // trigger_id 1599 rd[i].rBuf->I[4] = ntohl(rd[i].rBuf->I[4]); // fad_evt_counter 1600 rd[i].rBuf->I[5] = ntohl(rd[i].rBuf->I[5]); // REFCLK_frequency 1601 1602 rd[i].rBuf->I[10] = ntohl(rd[i].rBuf->I[10]); // runnumber; 1603 rd[i].rBuf->I[11] = ntohl(rd[i].rBuf->I[11]); // time; 1604 1605 for (int s=24;s<24+NTemp+NDAC;s++) 1606 rd[i].rBuf->S[s] = ntohs(rd[i].rBuf->S[s]); // drs_temperature / dac 1607 1608 rd[i].fadLen = ntohs(rd[i].rBuf->S[1]) * 2; 1609 rd[i].fadVers = rd[i].rBuf->S[2]; 1610 rd[i].ftmTyp = rd[i].rBuf->S[5]; 1611 rd[i].ftmID = rd[i].rBuf->I[3]; //(FTMevt) 1612 rd[i].evtID = rd[i].rBuf->I[4]; //(FADevt) 1613 rd[i].runID = rd[i].rBuf->I[11]; 1614 rd[i].bufTyp = 1; //ready to read full record 1615 rd[i].bufLen = rd[i].fadLen - rd[i].bufPos; 1616 1617 if (rd[i].runID == 0) 1618 rd[i].runID = g_actTime; 1619 1620 const int fadBoard = rd[i].rBuf->S[12]; 1621 debugHead (i, fadBoard, rd[i].rBuf); 1622 debugRead (i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, -1, tv.tv_sec, tv.tv_usec); // i=socket; jrd=#bytes; ievt=eventid;-1=start event 1623 } // end if data or header 1624 } // end for loop over all sockets 1772 1625 1773 1626 gi.numRead[numok]++; … … 1834 1687 } 1835 1688 1836 1837 gj.deltaT = 1000; //temporary, must be improved 1838 1839 for (int ib = 0; ib < NBOARDS; ib++)1840 gj.totBytes[ib] += gj.rateBytes[ib]; 1689 gj.deltaT = 1000; //temporary, must be improved 1690 1691 for (int ib = 0; ib < NBOARDS; ib++) 1692 gj.totBytes[ib] += gj.rateBytes[ib]; 1693 1841 1694 #ifdef THOMAS_MALLOC 1842 1695 gj.totMem = tgb_memory; … … 1844 1697 gj.totMem = g_maxMem; 1845 1698 #endif 1846 if (gj.maxMem > gj.xxxMem) 1847 gj.xxxMem = gj.maxMem; 1848 if (gj.maxEvt > gj.xxxEvt) 1849 gj.xxxEvt = gj.maxEvt; 1850 1851 factStat (gj); 1852 factStatNew (gi); 1853 gj.rateNew = gj.rateWrite = 0; 1854 gj.maxMem = gj.usdMem; 1855 gj.maxEvt = gj.bufTot; 1856 for (int b = 0; b < NBOARDS; b++) 1857 gj.rateBytes[b] = 0; 1858 /* 1859 } 1860 if (numok > 0) 1861 numok2 = 0; 1862 else if (numok2++ > 3) { 1863 if (g_runStat == 1) { 1864 xwait.tv_sec = 1; 1865 xwait.tv_nsec = 0; // hibernate for 1 sec 1866 } else { 1867 xwait.tv_sec = 0; 1868 xwait.tv_nsec = 1000; // sleep for ~1 usec 1869 } 1870 nanosleep (&xwait, NULL); 1871 } 1872 */ 1873 } //and do next loop over all sockets ... 1699 1700 if (gj.maxMem > gj.xxxMem) 1701 gj.xxxMem = gj.maxMem; 1702 if (gj.maxEvt > gj.xxxEvt) 1703 gj.xxxEvt = gj.maxEvt; 1704 1705 factStat (gj); 1706 factStatNew (gi); 1707 gj.rateNew = gj.rateWrite = 0; 1708 gj.maxMem = gj.usdMem; 1709 gj.maxEvt = gj.bufTot; 1710 for (int b = 0; b < NBOARDS; b++) 1711 gj.rateBytes[b] = 0; 1712 1713 } // while (g_runStat >= 0 && g_reset == 0) 1874 1714 1875 1715 factPrintf(kInfo, -1, "Stop reading ... RESET=%d", g_reset); 1876 1716 1877 if (g_reset > 0) { 1878 gi_reset = g_reset; 1879 gi_resetR = gi_reset % 10; //shall we stop reading ? 1880 gi_resetS = (gi_reset / 10) % 10; //shall we close sockets ? 1881 gi_resetW = (gi_reset / 100) % 10; //shall we close files ? 1882 gi_resetX = gi_reset / 1000; //shall we simply wait resetX seconds ? 1883 g_reset = 0; 1884 } else { 1885 gi_reset = 0; 1886 if (g_runStat == -1) 1887 gi_resetR = 1; 1888 else 1889 gi_resetR = 7; 1890 gi_resetS = 7; //close all sockets 1891 gi_resetW = 7; //close all files 1892 gi_resetX = 0; 1893 1894 //inform others we have to quit .... 1895 gi_runStat = -11; //inform all that no update to happen any more 1896 gj.readStat = -11; //inform all that no update to happen any more 1897 } 1898 1899 if (gi_resetS > 0) { 1900 //must close all open sockets ... 1901 factPrintf(kInfo, -1, "Close all sockets..."); 1902 for (int i = 0; i < MAX_SOCK; i++) { 1903 if (rd[i].sockStat == 0) { 1904 GenSock (-1, i, 0, NULL, &rd[i]); //close and destroy open socket 1905 if (i % 7 == 0) { 1906 // gi_NumConnect[i / 7]--; 1907 gi_NumConnect[i / 7]-= cntsock ; 1908 gi.numConn[i / 7]--; 1909 gj.numConn[i / 7]--; 1910 sockDef[i / 7] = 0; //flag ro recreate the sockets ... 1911 rd[i / 7].sockStat = -1; //and try to open asap 1912 } 1913 } 1914 } 1717 if (g_reset > 0) 1718 { 1719 gi_reset = g_reset; 1720 gi_resetR = gi_reset % 10; //shall we stop reading ? 1721 gi_resetS = (gi_reset / 10) % 10; //shall we close sockets ? 1722 gi_resetW = (gi_reset / 100) % 10; //shall we close files ? 1723 gi_resetX = gi_reset / 1000; //shall we simply wait resetX seconds ? 1724 g_reset = 0; 1725 } 1726 else 1727 { 1728 gi_reset = 0; 1729 gi_resetR = g_runStat == -1 ? 1 : 7; 1730 1731 gi_resetS = 7; //close all sockets 1732 gi_resetW = 7; //close all files 1733 gi_resetX = 0; 1734 1735 //inform others we have to quit .... 1736 gi_runStat = -11; //inform all that no update to happen any more 1737 gj.readStat = -11; //inform all that no update to happen any more 1738 } 1739 1740 if (gi_resetS > 0) 1741 { 1742 //must close all open sockets ... 1743 factPrintf(kInfo, -1, "Close all sockets..."); 1744 1745 for (int i = 0; i < MAX_SOCK; i++) 1746 { 1747 if (rd[i].sockStat != 0) 1748 continue; 1749 1750 GenSock(-1, i, 0, NULL, &rd[i]); //close and destroy open socket 1751 if (i%7) 1752 continue; 1753 1754 gi_NumConnect[i / 7]-= cntsock ; 1755 gi.numConn[i / 7]--; 1756 gj.numConn[i / 7]--; 1757 sockDef[i / 7] = 0; //flag ro recreate the sockets ... 1758 rd[i / 7].sockStat = -1; //and try to open asap 1759 } 1915 1760 } 1916 1761 … … 1970 1815 } 1971 1816 1972 if (gi_reset > 0) { 1973 if (gi_resetW > 0) { 1817 if (gi_reset > 0) 1818 { 1819 if (gi_resetW > 0) 1974 1820 CloseRunFile (0, 0, 0); //ask all Runs to be closed 1975 } 1821 1976 1822 if (gi_resetX > 0) 1977 1823 { … … 1987 1833 } 1988 1834 1989 1990 1991 1835 factPrintf(kInfo, -1, "Exit read Process..."); 1992 1836 … … 1997 1841 gi_runStat = -99; 1998 1842 gj.readStat = -99; 1843 1999 1844 factStat (gj); 2000 1845 factStatNew (gi); 1846 2001 1847 return 0; 2002 1848 … … 2376 2222 2377 2223 //close run for whatever reason 2378 if (runCtrl[j].runId == gi_myRun)2379 gi_myRun = g_actTime;2380 2381 2224 int ii = 0; 2382 2225 if (cond)
Note:
See TracChangeset
for help on using the changeset viewer.