Changeset 11521 for trunk/FACT++/src/EventBuilder.c
- Timestamp:
- 07/21/11 18:34:57 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/EventBuilder.c
r11511 r11521 275 275 276 276 277 int mBufEvt( int evID, uint runID, int nRoi , int sk, int fadlen) {277 int mBufEvt( int evID, uint runID, int nRoi[8], int sk, int fadlen) { 278 278 // generate a new Event into mBuffer: 279 279 // make sure only complete Event are possible, so 'free' will always work 280 280 // returns index into mBuffer[], or negative value in case of error 281 282 283 int i, k, evFree ; 281 // error: <-9000 if roi screwed up (not consistent with run) 282 // <-8000 (not consistent with event) 283 // <-7000 (not consistent with board) 284 // < 0 if no space left 285 286 struct timeval *tv, atv; 287 tv=&atv; 288 uint32_t tsec, tusec ; 289 290 int i, k, jr, b, evFree ; 284 291 int headmem=0 ; 285 292 size_t needmem = 0 ; 286 293 287 if (nRoi <0 || nRoi > 1024) { 288 snprintf(str,MXSTR,"illegal nRoi: %d",nRoi) ; 289 factOut(kError, 1, str ) ; 294 295 b = sk/7 ; 296 297 if (nRoi[0] <0 || nRoi[0] > 1024) { 298 snprintf(str,MXSTR,"illegal nRoi[0]: %d",nRoi[0]) ; 299 factOut(kError, 999, str ) ; 300 gj.badRoiR++ ; 301 gj.badRoi[b]++ ; 290 302 return -9999 ; 291 303 } 292 304 305 for (jr=1; jr<8; jr++) { 306 if ( nRoi[jr] != nRoi[0] ) { 307 snprintf(str,MXSTR,"wrong nRoi[%d]: %d %d",jr,nRoi[jr],nRoi[0]) ; 308 factOut(kError,711, str ) ; 309 gj.badRoiB++ ; 310 gj.badRoi[b]++ ; 311 return -7101 ; 312 } 313 } 314 if ( nRoi[8] < nRoi[0] ) { 315 snprintf(str,MXSTR,"wrong nRoi_TM: %d %d",nRoi[8],nRoi[0]) ; 316 factOut(kError,712, str ) ; 317 gj.badRoiB++ ; 318 gj.badRoi[b]++ ; 319 return -7102 ; 320 } 321 322 293 323 i = evID % MAX_EVT ; 294 324 evFree = -1 ; … … 296 326 for ( k=0; k<MAX_RUN; k++) { 297 327 if ( mBuffer[i].evNum == evID 298 && mBuffer[i].runNum== runID ) { 328 && mBuffer[i].runNum== runID ) { //event is already registered; 329 // is it ok ???? 330 if ( mBuffer[i].nRoi != nRoi[0] 331 || mBuffer[i].nRoiTM != nRoi[8] ) { 332 snprintf(str,MXSTR,"illegal evt_roi %d %d ; %d %d", 333 nRoi[0],nRoi[8], mBuffer[i].nRoi, mBuffer[i].nRoiTM ); 334 factOut(kError,821, str ) ; 335 gj.badRoiE++ ; 336 gj.badRoi[b]++ ; 337 return -8201 ; 338 } 339 //everything seems fine so far ==> use this slot .... 299 340 return i ; 300 341 } … … 304 345 305 346 306 //event does not yet exist; create 347 //event does not yet exist; create it 307 348 if (evFree < 0 ) { //no space available in ctrl 308 349 snprintf(str,MXSTR,"no control slot to keep event %d",evID) ; … … 312 353 i = evFree ; //found free entry; use it ... 313 354 355 gettimeofday( tv, NULL); 356 tsec = atv.tv_sec ; 357 tusec= atv.tv_usec ; 358 359 //check if runId already registered in runCtrl 360 evFree = -1 ; 361 for (k=0; k<MAX_RUN; k++) { 362 if (runCtrl[k].runId == runID ) { 363 if ( runCtrl[k].roi0 != nRoi[0] 364 || runCtrl[k].roi8 != nRoi[8] ) { 365 snprintf(str,MXSTR,"illegal run_roi %d %d ; %d %d", 366 nRoi[0],nRoi[8],runCtrl[k].roi0,runCtrl[k].roi8 ); 367 factOut(kError,931, str ) ; 368 gj.badRoiR++ ; 369 gj.badRoi[b]++ ; 370 return -9301 ; 371 } 372 goto RUNFOUND ; 373 } 374 else if (evFree < 0 && runCtrl[k].runId == 0 ) evFree = k ; 375 } 314 376 315 needmem = sizeof(EVENT) + NPIX*nRoi*2 + NTMARK*nRoi*2; // 377 if (evFree <0 ) { 378 snprintf(str,MXSTR,"not able to register the new run %d",runID); 379 factOut(kFatal,883, str ) ; 380 return -1001 ; 381 } else { 382 snprintf(str,MXSTR,"register new run %d roi: %d %d",runID,nRoi[0],nRoi[8]) ; 383 factOut(kInfo,503, str ) ; 384 runCtrl[evFree].runId = runID ; 385 runCtrl[evFree].roi0 = nRoi[0] ; 386 runCtrl[evFree].roi8 = nRoi[8] ; 387 runCtrl[evFree].fileId = -2 ; 388 runCtrl[evFree].lastEvt= -1 ; 389 runCtrl[evFree].nextEvt= 0; 390 runCtrl[evFree].actEvt = 0; 391 runCtrl[evFree].maxEvt = 999999999 ; //max number events allowed 392 runCtrl[evFree].firstUsec=tusec ; 393 runCtrl[evFree].firstTime= 394 runCtrl[evFree].lastTime=tsec ; 395 runCtrl[evFree].closeTime=tsec + 3600*24 ; //max time allowed 396 runCtrl[evFree].lastTime = 0 ; 397 398 runTail[evFree].nEventsOk = 399 runTail[evFree].nEventsRej = 400 runTail[evFree].nEventsBad = 401 runTail[evFree].PCtime0 = 402 runTail[evFree].PCtimeX = 0 ; 403 } 404 405 RUNFOUND: 406 407 needmem = sizeof(EVENT) + NPIX*nRoi[0]*2 + NTMARK*nRoi[0]*2; // 316 408 317 409 headmem = NBOARDS* sizeof(PEVNT_HEADER) ; … … 346 438 } 347 439 440 441 348 442 //flag all boards as unused 349 443 mBuffer[i].nBoard = 0 ; … … 351 445 mBuffer[i].board[k] = -1; 352 446 } 353 354 447 //flag all pixels as unused 355 448 for (k=0; k<NPIX; k++ ) { 356 449 mBuffer[i].fEvent->StartPix[k] = -1 ; 357 450 } 358 359 451 //flag all TMark as unused 360 452 for (k=0; k<NTMARK; k++ ) { … … 362 454 } 363 455 364 mBuffer[i].fEvent->PCUsec = g_actUsec ; 456 mBuffer[i].fEvent->NumBoards = 0 ; 457 mBuffer[i].fEvent->PCUsec = tusec ; 365 458 mBuffer[i].fEvent->PCTime = 366 mBuffer[i].pcTime = g_actTime ; 367 mBuffer[i].nRoi = nRoi ; 459 mBuffer[i].pcTime = tsec ; 460 mBuffer[i].nRoi = nRoi[0] ; 461 mBuffer[i].nRoiTM = nRoi[8] ; 368 462 mBuffer[i].evNum = evID ; 369 463 mBuffer[i].runNum = runID ; … … 383 477 384 478 385 snprintf(str,MXSTR,"%5d start new evt %8d %8d sock %3d len %5d t %10d",386 evID, i,evtCtrl.lastPtr,sk,fadlen,mBuffer[i].pcTime);479 snprintf(str,MXSTR,"%5d %8d start new evt %8d %8d sock %3d len %5d t %10d", 480 evID,runID,i,evtCtrl.lastPtr,sk,fadlen,mBuffer[i].pcTime); 387 481 factOut(kDebug,-11, str ) ; 388 482 evtCtrl.lastPtr++ ; … … 391 485 gi.evtGet++ ; 392 486 393 //check if runId already registered in runCtrl394 evFree = -1 ;395 for (k=0; k<MAX_RUN; k++) {396 if (runCtrl[k].runId == runID ) return i ;//run exists already397 else if (evFree < 0 && runCtrl[k].runId == 0 ) evFree = k ;398 }399 400 if (evFree <0 ) {401 snprintf(str,MXSTR,"not able to register the new run %d",runID);402 factOut(kFatal,883, str ) ;403 } else {404 runCtrl[evFree].runId = runID ;405 runCtrl[evFree].fileId = -2 ;406 runCtrl[evFree].lastEvt= -1 ;407 runCtrl[evFree].nextEvt= 0;408 runCtrl[evFree].actEvt = 0;409 runCtrl[evFree].maxEvt = 999999999 ; //max number events allowed410 runCtrl[evFree].firstUsec=g_actUsec ;411 runCtrl[evFree].firstTime=412 runCtrl[evFree].lastTime=g_actTime ;413 runCtrl[evFree].closeTime=g_actTime + 3600*24 ; //max time allowed414 runCtrl[evFree].lastTime = 0 ;415 416 runTail[evFree].nEventsOk =417 runTail[evFree].nEventsRej =418 runTail[evFree].nEventsBad =419 runTail[evFree].PCtime0 =420 runTail[evFree].PCtimeX = 0 ;421 }422 423 487 return i ; 424 488 … … 469 533 gi.gotByte[i] = 0 ; 470 534 gi.gotErr[i] = 0 ; 535 471 536 } 472 537 … … 508 573 int32_t jrd ; 509 574 uint gi_SecTime ; //time in seconds 510 int boardId, roi ,drs,px,src,pixS,pixH,pixC,pixR,tmS ;575 int boardId, roi[8],drs,px,src,pixS,pixH,pixC,pixR,tmS ; 511 576 512 577 int goodhed = 0 ; … … 525 590 526 591 527 gi_myRun = g_actTime ;528 592 snprintf(str,MXSTR,"start initializing"); 529 593 factOut(kInfo,-1, str ) ; … … 560 624 561 625 START: 626 gettimeofday( tv, NULL); 627 g_actTime = tsec = atv.tv_sec ; 628 g_actUsec = tusec= atv.tv_usec ; 629 gi_myRun = g_actTime ; 562 630 evtCtrl.frstPtr = 0 ; 563 631 evtCtrl.lastPtr = 0 ; … … 592 660 gj.totMem = g_maxMem ; 593 661 gj.bufNew = gj.bufEvt = 0 ; 594 gj.evtSkip= gj.evtWrite = gj.evtErr = 0 ; 595 662 gj.badRoiE = gj.badRoiR = gj.badRoiB = 663 gj.evtSkip = gj.evtWrite = gj.evtErr = 0 ; 664 665 int b ; 666 for (b=0; b<NBOARDS; b++) gj.badRoi[b]=0 ; 596 667 597 668 mBufInit() ; //initialize buffers … … 608 679 gi_runStat = g_runStat; 609 680 gj.readStat= g_runStat; 681 gettimeofday( tv, NULL); 682 g_actTime = tsec = atv.tv_sec ; 683 g_actUsec = tusec= atv.tv_usec ; 684 610 685 611 686 int b,p,p0,s0,nch; … … 649 724 if (sockDef[b] > 0) s0= +1 ; 650 725 else s0= -1 ; 651 652 gettimeofday( tv, NULL);653 g_actTime = tsec = atv.tv_sec ;654 g_actUsec = tusec= atv.tv_usec ;655 726 656 727 if (rd[i].sockStat <0 ) { //try to connect if not yet done … … 749 820 750 821 //we have a complete buffer, copy to WORK area 751 roi = ntohs(rd[i].rBuf->S[ head_len/2 + 2 ]) ; 822 int jr ; 823 roi[0] = ntohs(rd[i].rBuf->S[ head_len/2 + 2 ]) ; 824 for (jr=0; jr<9; jr++) { 825 roi[jr] = ntohs(rd[i].rBuf->S[ head_len/2 + 2 + jr*(roi[0]+4) ]) ; 826 } 752 827 //get index into mBuffer for this event (create if needed) 753 828 evID = mBufEvt( rd[i].evtID, rd[i].runID, roi, i, rd[i].fadLen ) ; 754 829 755 if (evID <-9000) goto EndBuf ; //illegal event, skip it ... 756 if (evID < 0) { 830 if (evID <-1000) { 831 goto EndBuf ; //not usable board/event/run --> skip it 832 } 833 if (evID < 0) { //no space left, retry later 757 834 758 835 if ( rd[i].bufLen != 0) { … … 803 880 memcpy( &mBuffer[evID].FADhead[boardId].start_package_flag, 804 881 &rd[i].rBuf->S[0], head_len) ; 805 882 // xxx roi = mBuffer[evID].nRoi ; 806 883 807 884 src = head_len/2 ; … … 816 893 src++ ; 817 894 818 819 895 // xxx if ( ( px != 8 && pixR == roi ) 896 // xxx || ( px == 8 && pixR >= roi ) ) { //we have a reasonable roi 820 897 821 898 mBuffer[evID].fEvent->StartPix[pixS] =pixC; 822 dest= pixS * roi ;899 dest= pixS * roi[0] ; 823 900 memcpy( 824 901 &mBuffer[evID].fEvent->Adc_Data[dest], 825 &rd[i].rBuf->S[src], roi * 2) ;902 &rd[i].rBuf->S[src], roi[0] * 2) ; 826 903 src+= pixR ; 827 904 828 905 if ( px==8 ) { 829 if ( pixR > roi ) { //and we have additional TM info906 if ( pixR > roi[0]) { //and we have additional TM info 830 907 tmS =boardId*4 + drs ; 831 dest= tmS * roi + NPIX* roi;832 int srcT= src - roi ;833 mBuffer[evID].fEvent->StartTM[tmS] = (pixC+pixR-roi )%1024 ;908 dest= tmS * roi[0] + NPIX* roi[0] ; 909 int srcT= src - roi[0] ; 910 mBuffer[evID].fEvent->StartTM[tmS] = (pixC+pixR-roi[0])%1024 ; 834 911 memcpy( 835 912 &mBuffer[evID].fEvent->Adc_Data[dest], 836 &rd[i].rBuf->S[srcT], roi * 2) ;913 &rd[i].rBuf->S[srcT], roi[0] * 2) ; 837 914 } else { 838 915 mBuffer[evID].fEvent->StartTM[tmS] = -1 ; 839 916 } 840 917 } 841 842 843 844 845 846 918 // xxx } else { 919 // xxx snprintf(str,MXSTR,"wrong roi %d %d %d %d",px,pixR,roi,src-2); 920 // xxx gi.evtErr++ ; 921 // xxx factOut(kError,202, str ) ; 922 // xxx goto EndBuf ; 923 // xxx } 847 924 } 848 925 }// now we have stored a new board contents into Event structure 849 926 927 mBuffer[evID].fEvent->NumBoards++ ; 850 928 mBuffer[evID].board[ boardId ] = boardId ; 851 929 evtCtrl.evtStat[ iDx ]++ ; … … 857 935 858 936 if (mBuffer[evID].runNum != actrun ) { // have we already reported first event of this run ??? 859 actrun = mBuffer[ i].runNum ;937 actrun = mBuffer[evID].runNum ; 860 938 int ir ; 861 939 for ( ir=0; ir<MAX_RUN; ir++) { 862 940 if ( runCtrl[ir].runId == actrun) { 863 if ( runCtrl[ir].lastEvt <0 ) { 864 runCtrl[ir].lastEvt = +1 ; 941 if ( ++runCtrl[ir].lastEvt ==0 ) { 865 942 gotNewRun( actrun, mBuffer[evID].FADhead ); 866 943 snprintf(str,MXSTR,"gotNewRun %d (ev %d)",mBuffer[evID].runNum,mBuffer[evID].evNum); … … 1010 1087 factStat(gj); 1011 1088 factStatNew(gi) ; 1012 1013 1089 gj.rateNew = gj.rateWrite = 0 ; 1090 gj.maxMem = gj.usdMem ; 1014 1091 for ( b=0; b<NBOARDS; b++) gj.rateBytes[b] =0 ; 1015 1092 } … … 1390 1467 factOut(kFatal,901, str ) ; 1391 1468 gi.wrtErr++ ; 1392 for ( j=0; j<MAX_RUN; j++) printf("j %d run.j %d run %d\n",j,runCtrl[j].runId,irun );1393 exit(111);1394 1469 } 1395 1470 lastRun = j ; … … 1398 1473 if (runCtrl[j].fileId < 0 ) { 1399 1474 actRun.Version = 1 ; 1400 actRun.RunType = -1 ; 1401 1475 actRun.RunType = -1 ; //to be adapted 1476 1477 actRun.Nroi = runCtrl[j].roi0 ; 1478 actRun.NroiTM = runCtrl[j].roi8 ; 1479 if ( actRun.Nroi == actRun.NroiTM ) actRun.NroiTM = 0 ; 1402 1480 actRun.RunTime = runCtrl[j].firstTime ; 1403 1481 actRun.RunUsec = runCtrl[j].firstTime ; … … 1414 1492 runCtrl[j].fileId = 91 ; 1415 1493 } else { 1416 snprintf(str,MXSTR,"W opened new run_file %d ",irun) ;1494 snprintf(str,MXSTR,"W opened new run_file %d evt %d",irun,ievt) ; 1417 1495 factOut(kInfo,-1, str ) ; 1418 1496 runCtrl[j].fileId = 0 ;
Note:
See TracChangeset
for help on using the changeset viewer.