Changeset 15413
- Timestamp:
- 04/24/13 14:00:40 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/EventBuilder.c
r15379 r15413 535 535 } /*-----------------------------------------------------------------*/ 536 536 537 void swapEventHeaderBytes(int i);538 537 int checkRoiConsistency(int i, int roi[]); 539 538 … … 553 552 // size_t needmem = 0; 554 553 555 swapEventHeaderBytes(sk);556 554 557 555 int nRoi[9]; … … 559 557 return -9999; 560 558 561 const int b = sk / 7; 562 563 if (nRoi[0] < 0 || nRoi[0] > 1024) { 564 factPrintf(kError, 999, "Illegal roi in channel 0: %d (allowed: 0<=roi<=1024)", nRoi[0]); 565 gj.badRoiR++; 566 gj.badRoi[b]++; 567 return -9999; 568 } 569 570 for (int jr = 1; jr < 8; jr++) { 571 if (nRoi[jr] != nRoi[0]) { 572 factPrintf(kError, 711, "Mismatch of roi (%d) in channel %d with roi (%d) in channel 0.", nRoi[jr], jr, nRoi[0]); 573 gj.badRoiB++; 574 gj.badRoi[b]++; 575 return -7101; 576 } 577 } 578 if (nRoi[8] < nRoi[0]) { 579 factPrintf(kError, 712, "Mismatch of roi (%d) in channel 8. Should be larger or equal than the roi (%d) in channel 0.", nRoi[8], nRoi[0]); 580 gj.badRoiB++; 581 gj.badRoi[b]++; 582 return -7102; 583 } 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; 591 592 int i = evID % MAX_EVT; 593 int evFree = -1; 594 595 for (int k = 0; k < MAX_RUN; k++) { 596 if (mBuffer[i].evNum == evID && mBuffer[i].runNum == runID) { //event is already registered; 597 // is it ok ???? 598 if (mBuffer[i].nRoi != nRoi[0] 599 || mBuffer[i].nRoiTM != nRoi[8]) { 600 factPrintf(kError, 821, "Mismatch of roi within event. Expected roi=%d and roi_tm=%d, got %d and %d.", 601 mBuffer[i].nRoi, mBuffer[i].nRoiTM, nRoi[0], nRoi[8]); 602 gj.badRoiE++; 603 gj.badRoi[b]++; 604 return -8201; 605 } 606 // count for inconsistencies 607 608 if (mBuffer[i].trgNum != trgNum) 609 mBuffer[i].Errors[0]++; 610 if (mBuffer[i].fadNum != fadNum) 611 mBuffer[i].Errors[1]++; 612 if (mBuffer[i].trgTyp != trgTyp) 613 mBuffer[i].Errors[2]++; 614 615 //everything seems fine so far ==> use this slot .... 616 return i; 617 } 618 if (evFree < 0 && mBuffer[i].evNum < 0) 619 evFree = i; 620 i += MAX_EVT; 621 } 622 623 624 //event does not yet exist; create it 625 if (evFree < 0) { //no space available in ctrl 626 factPrintf(kError, 881, "No control slot to keep event %d", evID); 627 return -1; 628 } 629 i = evFree; //found free entry; use it ... 630 631 struct timeval tv; 632 gettimeofday (&tv, NULL); 633 634 const uint32_t tsec = tv.tv_sec; 635 const uint32_t tusec = tv.tv_usec; 636 637 //check if runId already registered in runCtrl 638 evFree = -1; 639 640 uint oldest = g_actTime + 1000; 641 int jold = -1; 642 643 for (int k = 0; k < MAX_RUN; k++) { 644 if (runCtrl[k].runId == runID) { 645 // if (runCtrl[k].procId > 0) { //run is closed -> reject 646 // snprintf (str, MXSTR, "skip event since run %d finished", runID); 647 // factOut (kInfo, 931, str); 648 // return -21; 649 // } 650 651 if (runCtrl[k].roi0 != nRoi[0] 652 || runCtrl[k].roi8 != nRoi[8]) { 653 factPrintf(kError, 931, "Mismatch of roi within run. Expected roi=%d and roi_tm=%d, got %d and %d.", 654 runCtrl[k].roi0, runCtrl[k].roi8, nRoi[0], nRoi[8]); 655 gj.badRoiR++; 656 gj.badRoi[b]++; 657 return -9301; 658 } 659 goto RUNFOUND; 660 } else if (evFree < 0 && runCtrl[k].fileId < 0) { //not yet used 661 evFree = k; 662 } else if (evFree < 0 && runCtrl[k].fileId > 0) { //already closed 663 if (runCtrl[k].closeTime < oldest) { 559 //const int b = sk / 7; 560 561 const int evID = rd[sk].evtID; 562 const uint runID = rd[sk].runID; 563 const int fadlen = rd[sk].fadLen; 564 const int trgTyp = rd[sk].ftmTyp; 565 const int trgNum = rd[sk].ftmID; 566 const int fadNum = rd[sk].evtID; 567 568 int i = evID % MAX_EVT; 569 int evFree = -1; 570 571 for (int k = 0; k < MAX_RUN; k++) 572 { 573 //event is already registered; 574 if (mBuffer[i].evNum == evID && mBuffer[i].runNum == runID) 575 { 576 // is it ok ???? 577 if (mBuffer[i].nRoi != nRoi[0] || mBuffer[i].nRoiTM != nRoi[8]) 578 { 579 factPrintf(kError, 821, "Mismatch of roi within event. Expected roi=%d and roi_tm=%d, got %d and %d.", 580 mBuffer[i].nRoi, mBuffer[i].nRoiTM, nRoi[0], nRoi[8]); 581 return -8201; 582 } 583 584 // count for inconsistencies 585 if (mBuffer[i].trgNum != trgNum) 586 mBuffer[i].Errors[0]++; 587 if (mBuffer[i].fadNum != fadNum) 588 mBuffer[i].Errors[1]++; 589 if (mBuffer[i].trgTyp != trgTyp) 590 mBuffer[i].Errors[2]++; 591 592 //everything seems fine so far ==> use this slot .... 593 return i; 594 } 595 596 if (evFree < 0 && mBuffer[i].evNum < 0) 597 evFree = i; 598 599 i += MAX_EVT; 600 } 601 602 603 //event does not yet exist; create it 604 605 if (evFree < 0) //no space available in ctrl 606 { 607 factPrintf(kError, 881, "No control slot to keep event %d", evID); 608 return -1; 609 } 610 611 i = evFree; //found free entry; use it ... 612 613 // FIXME: This should be the time of the first receiped board 614 struct timeval tv; 615 gettimeofday (&tv, NULL); 616 617 const uint32_t tsec = tv.tv_sec; 618 const uint32_t tusec = tv.tv_usec; 619 620 //check if runId already registered in runCtrl 621 evFree = -1; 622 623 uint oldest = g_actTime + 1000; 624 int jold = -1; 625 626 int found = 0; 627 628 for (int k = 0; k < MAX_RUN; k++) 629 { 630 if (runCtrl[k].runId == runID) 631 { 632 if (runCtrl[k].roi0 != nRoi[0] || runCtrl[k].roi8 != nRoi[8]) 633 { 634 factPrintf(kError, 931, "Mismatch of roi within run. Expected roi=%d and roi_tm=%d, got %d and %d.", 635 runCtrl[k].roi0, runCtrl[k].roi8, nRoi[0], nRoi[8]); 636 return -9301; 637 } 638 639 found = 1; 640 break; 641 } 642 643 if (evFree>=0) 644 continue; 645 646 //not yet used 647 if (runCtrl[k].fileId < 0) 648 { 649 evFree = k; 650 continue; 651 } 652 653 //already closed 654 if (runCtrl[k].fileId > 0 && runCtrl[k].closeTime < oldest) 655 { 664 656 oldest = runCtrl[k].closeTime; 665 657 jold = k; 666 } 667 } 668 } 669 670 if (evFree < 0 && jold < 0) { 671 factPrintf(kFatal, 883, "Not able to register the new run %d", runID); 672 return -1001; 673 } 674 675 if (evFree < 0) 676 evFree = jold; 677 factPrintf(kInfo, 503, "New run %d (evFree=%d) registered with roi=%d and roi_tm=%d", runID, 678 evFree, nRoi[0], nRoi[8]); 679 runCtrl[evFree].runId = runID; 680 runCtrl[evFree].roi0 = nRoi[0]; 681 runCtrl[evFree].roi8 = nRoi[8]; 682 runCtrl[evFree].fileId = -2; 683 runCtrl[evFree].procId = -2; 684 runCtrl[evFree].lastEvt = -1; 685 runCtrl[evFree].nextEvt = 0; 686 runCtrl[evFree].actEvt = 0; 687 runCtrl[evFree].procEvt = 0; 688 runCtrl[evFree].maxEvt = 999999999; //max number events allowed 689 runCtrl[evFree].firstUsec = tusec; 690 runCtrl[evFree].firstTime = runCtrl[evFree].lastTime = tsec; 691 runCtrl[evFree].closeTime = tsec + 3600 * 24; //max time allowed 692 // runCtrl[evFree].lastTime = 0; 693 694 runTail[evFree].nEventsOk = 695 runTail[evFree].nEventsRej = 696 runTail[evFree].nEventsBad = 697 runTail[evFree].PCtime0 = runTail[evFree].PCtimeX = 0; 698 699 RUNFOUND: 700 //ETIENNE 701 /* needmem = sizeof (EVENT) + NPIX * nRoi[0] * 2 + NTMARK * nRoi[0] * 2; // 702 703 headmem = NBOARDS * sizeof (PEVNT_HEADER); 704 705 if (gj.usdMem + needmem + headmem + gi_maxSize > g_maxMem) { 706 gj.maxMem = gj.usdMem + needmem + headmem + gi_maxSize; 707 if (gi_memStat > 0) { 708 gi_memStat = -99; 709 snprintf (str, MXSTR, "no memory left to keep event %6d sock %3d", 710 evID, sk); 711 factOut (kError, 882, str); 712 } else { 713 snprintf (str, MXSTR, "no memory left to keep event %6d sock %3d", 714 evID, sk); 715 factOut (kDebug, 882, str); 716 } 717 return -11; 718 } 719 */ 720 721 #ifdef THOMAS_MALLOC 722 mBuffer[i].FADhead = TGB_Malloc(); 723 mBuffer[i].fEvent = NULL; 724 mBuffer[i].buffer = NULL; 725 if (mBuffer[i].FADhead == NULL) { 726 factPrintf(kError, 882, "malloc header failed for event %d", evID); 727 return -12; 728 } 729 mBuffer[i].fEvent = (void*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM); 658 } 659 } 660 661 if (!found) 662 { 663 if (evFree < 0 && jold < 0) 664 { 665 factPrintf(kFatal, 883, "Not able to register the new run %d", runID); 666 return -1001; 667 } 668 669 if (evFree < 0) 670 evFree = jold; 671 672 factPrintf(kInfo, 503, "New run %d (evFree=%d) registered with roi=%d and roi_tm=%d", runID, 673 evFree, nRoi[0], nRoi[8]); 674 675 runCtrl[evFree].runId = runID; 676 runCtrl[evFree].roi0 = nRoi[0]; 677 runCtrl[evFree].roi8 = nRoi[8]; 678 runCtrl[evFree].fileId = -2; 679 runCtrl[evFree].procId = -2; 680 runCtrl[evFree].lastEvt = -1; 681 runCtrl[evFree].nextEvt = 0; 682 runCtrl[evFree].actEvt = 0; 683 runCtrl[evFree].procEvt = 0; 684 runCtrl[evFree].maxEvt = 999999999; //max number events allowed 685 runCtrl[evFree].firstTime = tsec; 686 runCtrl[evFree].firstUsec = tusec; 687 runCtrl[evFree].lastTime = tsec; 688 runCtrl[evFree].closeTime = tsec + 3600 * 24; //max time allowed 689 690 runTail[evFree].nEventsOk = 0; 691 runTail[evFree].nEventsRej = 0; 692 runTail[evFree].nEventsBad = 0; 693 runTail[evFree].PCtime0 = 0; 694 runTail[evFree].PCtimeX = 0; 695 } 696 697 // FIXME: Why is that done if FOUND? 698 699 //flag all boards as unused 700 mBuffer[i].nBoard = 0; 701 for (int k = 0; k < NBOARDS; k++) 702 mBuffer[i].board[k] = -1; 703 704 mBuffer[i].pcTime[0] = tsec; 705 mBuffer[i].pcTime[1] = tusec; 706 mBuffer[i].nRoi = nRoi[0]; 707 mBuffer[i].nRoiTM = nRoi[8]; 708 mBuffer[i].evNum = evID; 709 mBuffer[i].runNum = runID; 710 mBuffer[i].fadNum = fadNum; 711 mBuffer[i].trgNum = trgNum; 712 mBuffer[i].trgTyp = trgTyp; 713 mBuffer[i].Errors[0] = 0; 714 mBuffer[i].Errors[1] = 0; 715 mBuffer[i].Errors[2] = 0; 716 mBuffer[i].Errors[3] = 0; 717 mBuffer[i].fEvent = NULL; 718 mBuffer[i].buffer = NULL; 719 mBuffer[i].FADhead = NULL; 720 721 /* 722 #ifdef ETIENNE_MALLOC 723 mBuffer[i].FADhead = ETI_Malloc(evID, i); 724 mBuffer[i].buffer = NULL; 725 if (mBuffer[i].FADhead != NULL) 726 mBuffer[i].fEvent = (EVENT*)&(((char*)(mBuffer[i].FADhead))[MAX_HEAD_MEM]); 727 else 728 { 729 mBuffer[i].fEvent = NULL; 730 gj.usdMem = 0; 731 for (int k=0;k<numAllocatedChunks;k++) 732 gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE; 733 if (gj.usdMem > gj.maxMem) 734 gj.maxMem = gj.usdMem; 735 else 736 { 737 factPrintf(kDebug, 882, "No memory left to keep event %6d sock %3d", evID, sk); 738 } 739 return -11; 740 } 730 741 #endif 731 742 743 #ifdef STANDARD_MALLOC 744 mBuffer[i].FADhead = malloc (MAX_HEAD_MEM+MAX_EVT_MEM); 745 mBuffer[i].fEvent = NULL; 746 mBuffer[i].buffer = NULL; 747 if (mBuffer[i].FADhead == NULL) 748 { 749 factPrintf(kError, 882, "malloc header failed for event %d", evID); 750 return -12; 751 } 752 mBuffer[i].fEvent = (void*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM); 753 #endif 754 732 755 #ifdef ETIENNE_MALLOC 733 mBuffer[i].FADhead = ETI_Malloc(evID, i); 734 mBuffer[i].buffer = NULL; 735 if (mBuffer[i].FADhead != NULL) 736 mBuffer[i].fEvent = (EVENT*)&(((char*)(mBuffer[i].FADhead))[MAX_HEAD_MEM]); 737 else 738 { 739 mBuffer[i].fEvent = NULL; 740 gj.usdMem = 0; 741 for (int k=0;k<numAllocatedChunks;k++) 742 gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE; 743 if (gj.usdMem > gj.maxMem) 744 gj.maxMem = gj.usdMem; 745 /*if (gi_memStat > 0) { 746 gi_memStat = -99; 747 snprintf (str, MXSTR, "No memory left to keep event %6d sock %3d", 748 evID, sk); 749 factOut (kError, 882, str); 750 }*/ 751 #ifdef EVTDEBUG 752 else 753 { 754 factPrintf(kDebug, 882, "No memory left to keep event %6d sock %3d", evID, sk); 755 } 756 //ETIENNE 757 //gj.usdMem += needmem + headmem + gi_maxSize; 758 gj.usdMem = 0; 759 for (int k=0;k<numAllocatedChunks;k++) 760 gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE; 761 //END ETIENNE 756 762 #endif 757 return -11; 758 } 759 #endif 760 761 #ifdef STANDARD_MALLOC 762 763 mBuffer[i].FADhead = malloc (MAX_HEAD_MEM+MAX_EVT_MEM); 764 mBuffer[i].fEvent = NULL; 765 mBuffer[i].buffer = NULL; 766 if (mBuffer[i].FADhead == NULL) { 767 factPrintf(kError, 882, "malloc header failed for event %d", evID); 768 return -12; 769 } 770 mBuffer[i].fEvent = (void*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM); 771 #endif 772 773 /* 774 mBuffer[i].FADhead = malloc (headmem); 775 if (mBuffer[i].FADhead == NULL) { 776 snprintf (str, MXSTR, "malloc header failed for event %d", evID); 777 factOut (kError, 882, str); 778 return -12; 779 } 780 781 mBuffer[i].fEvent = malloc (needmem); 782 if (mBuffer[i].fEvent == NULL) { 783 snprintf (str, MXSTR, "malloc data failed for event %d", evID); 784 factOut (kError, 882, str); 785 free (mBuffer[i].FADhead); 786 mBuffer[i].FADhead = NULL; 787 return -22; 788 } 789 790 mBuffer[i].buffer = malloc (gi_maxSize); 791 if (mBuffer[i].buffer == NULL) { 792 snprintf (str, MXSTR, "malloc buffer failed for event %d", evID); 793 factOut (kError, 882, str); 794 free (mBuffer[i].FADhead); 795 mBuffer[i].FADhead = NULL; 796 free (mBuffer[i].fEvent); 797 mBuffer[i].fEvent = NULL; 798 return -32; 799 }*/ 800 //END ETIENNE 801 //flag all boards as unused 802 mBuffer[i].nBoard = 0; 803 for (int k = 0; k < NBOARDS; k++) { 804 mBuffer[i].board[k] = -1; 805 } 806 //flag all pixels as unused 807 for (int k = 0; k < NPIX; k++) { 808 mBuffer[i].fEvent->StartPix[k] = -1; 809 } 810 //flag all TMark as unused 811 for (int k = 0; k < NTMARK; k++) { 812 mBuffer[i].fEvent->StartTM[k] = -1; 813 } 814 815 mBuffer[i].fEvent->NumBoards = 0; 816 mBuffer[i].fEvent->PCUsec = tusec; 817 mBuffer[i].fEvent->PCTime = mBuffer[i].pcTime = tsec; 818 mBuffer[i].nRoi = nRoi[0]; 819 mBuffer[i].nRoiTM = nRoi[8]; 820 mBuffer[i].evNum = evID; 821 mBuffer[i].runNum = runID; 822 mBuffer[i].fadNum = fadNum; 823 mBuffer[i].trgNum = trgNum; 824 mBuffer[i].trgTyp = trgTyp; 825 // mBuffer[i].evtLen = needmem; 826 mBuffer[i].Errors[0] = 827 mBuffer[i].Errors[1] = mBuffer[i].Errors[2] = mBuffer[i].Errors[3] = 0; 828 829 #ifdef ETIENNE_MALLOC 830 //ETIENNE 831 //gj.usdMem += needmem + headmem + gi_maxSize; 832 gj.usdMem = 0; 833 for (int k=0;k<numAllocatedChunks;k++) 834 { 835 gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE; 836 } 837 //END ETIENNE 838 #endif 839 840 #ifdef THOMAS_MALLOC 841 gj.usdMem = tgb_inuse; 842 #endif 843 844 if (gj.usdMem > gj.maxMem) 845 gj.maxMem = gj.usdMem; 846 847 gj.bufTot++; 848 if (gj.bufTot > gj.maxEvt) 849 gj.maxEvt = gj.bufTot; 850 851 gj.rateNew++; 852 853 //register event in 'active list (reading)' 854 855 evtCtrl.evtBuf[evtCtrl.lastPtr] = i; 856 evtCtrl.evtStat[evtCtrl.lastPtr] = 0; 857 evtCtrl.pcTime[evtCtrl.lastPtr] = g_actTime; 858 evtIdx[i] = evtCtrl.lastPtr; 859 860 861 #ifdef EVTDEBUG 862 factPrintf(kDebug, -11, "%5d %8d start new evt %8d %8d sock %3d len %5d t %10d", 863 evID, runID, i, evtCtrl.lastPtr, sk, fadlen, mBuffer[i].pcTime); 864 #endif 865 evtCtrl.lastPtr++; 866 evtCtrl.lastPtr %= MAX_EVT * MAX_RUN; 867 868 //gi.evtGet++; 869 870 return i; 763 */ 764 765 return i; 871 766 872 767 } /*-----------------------------------------------------------------*/ 873 768 874 769 void initEvent(int i) 770 { 771 mBuffer[i].fEvent = (void*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM); 772 773 //flag all pixels as unused 774 for (int k = 0; k < NPIX; k++) 775 mBuffer[i].fEvent->StartPix[k] = -1; 776 777 //flag all TMark as unused 778 for (int k = 0; k < NTMARK; k++) 779 mBuffer[i].fEvent->StartTM[k] = -1; 780 781 mBuffer[i].fEvent->NumBoards = 0; 782 mBuffer[i].fEvent->PCTime = mBuffer[i].pcTime[0]; 783 mBuffer[i].fEvent->PCUsec = mBuffer[i].pcTime[1]; 784 } 875 785 876 786 … … 1027 937 int roiPtr = sizeof(PEVNT_HEADER)/2 + 2; 1028 938 1029 roi[0] = rd[i].rBuf->S[roiPtr];939 roi[0] = ntohs(rd[i].rBuf->S[roiPtr]); 1030 940 1031 941 for (int jr = 0; jr < 9; jr++) 1032 942 { 1033 roi[jr] = rd[i].rBuf->S[roiPtr]; 1034 1035 if (roi[jr]!=roi[0]) 943 roi[jr] = ntohs(rd[i].rBuf->S[roiPtr]); 944 945 if (roi[jr]<0 || roi[jr]>1024) 946 { 947 factPrintf(kError, 999, "Illegal roi in channel %d (allowed: 0<=roi<=1024)", jr, roi[jr]); 948 return 0; 949 } 950 951 // Check that the roi of pixels jr are compatible with the one of pixel 0 952 if (jr!=8 && roi[jr]!=roi[0]) 1036 953 { 1037 954 xjr = jr; … … 1039 956 } 1040 957 958 // Check that the roi of all other DRS chips on boards are compatible 1041 959 for (int kr = 1; kr < 4; kr++) 1042 960 { 1043 const int kroi = rd[i].rBuf->S[roiPtr];961 const int kroi = ntohs(rd[i].rBuf->S[roiPtr]); 1044 962 if (kroi != roi[jr]) 1045 963 { … … 1052 970 } 1053 971 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; 972 if (xjr>=0) 973 { 974 if (xkr<0) 975 factPrintf(kFatal, 1, "Inconsistent Roi accross chips [DRS=%d], expected %d, got %d", xjr, roi[0], roi[xjr]); 976 else 977 factPrintf(kFatal, 1, "Inconsistent Roi accross channels [DRS=%d Ch=%d], expected %d, got %d", xjr, xkr, roi[xjr], ntohs(rd[i].rBuf->S[roiPtr])); 978 979 return 0; 980 } 981 982 //const int b = i / 7; 983 984 /* 985 if (roi[0]<0 || roi[0] > 1024) 986 { 987 factPrintf(kError, 999, "Illegal roi in channel 0: %d (allowed: 0<=roi<=1024)", roi[0]); 988 gj.badRoiR++; 989 gj.badRoi[b]++; 990 return 0; 991 } 992 */ 993 /* 994 for (int jr = 1; jr < 8; jr++) 995 { 996 if (roi[jr] != roi[0]) 997 { 998 factPrintf(kError, 711, "Mismatch of roi (%d) in channel %d with roi (%d) in channel 0.", roi[jr], jr, roi[0]); 999 gj.badRoiB++; 1000 gj.badRoi[b]++; 1001 return 0; 1002 } 1003 } 1004 */ 1005 if (roi[8] < roi[0]) 1006 { 1007 factPrintf(kError, 712, "Mismatch of roi (%d) in channel 8. Should be larger or equal than the roi (%d) in channel 0.", roi[8], roi[0]); 1008 //gj.badRoiB++; 1009 //gj.badRoi[b]++; 1010 return 0; 1011 } 1012 1013 return 1; 1063 1014 } 1064 1015 … … 1080 1031 void copyData(int i, int evID, /*int roi,*/ int boardId) 1081 1032 { 1033 swapEventHeaderBytes(i); 1034 1082 1035 memcpy(&mBuffer[evID].FADhead[boardId].start_package_flag, 1083 1036 &rd[i].rBuf->S[0], sizeof(PEVNT_HEADER)); … … 1189 1142 const int minLen = sizeof(PEVNT_HEADER); //min #bytes needed to check header: full header for debug 1190 1143 1191 int frst_len = sizeof(PEVNT_HEADER); //max #bytes to read first: fad_header only, so each event must be longer, even for roi=01192 1193 1144 start.S = 0xFB01; 1194 1145 stop.S = 0x04FE; … … 1243 1194 #endif 1244 1195 gj.bufNew = gj.bufEvt = 0; 1245 gj.badRoiE = gj.badRoiR = gj.badRoiB =1246 1247 1248 for (int b = 0; b < NBOARDS; b++)1249 gj.badRoi[b] = 0;1196 //gj.badRoiE = gj.badRoiR = gj.badRoiB = 0; 1197 gj.evtSkip = gj.evtWrite = gj.evtErr = 0; 1198 1199 //for (int b = 0; b < NBOARDS; b++) 1200 // gj.badRoi[b] = 0; 1250 1201 1251 1202 mBufInit (); //initialize buffers … … 1349 1300 if (sockDef[b] > 0) 1350 1301 { 1351 rd[i].bufTyp = 0; // expect a header1352 rd[i].bufLen = frst_len; // max size to read at begining1302 rd[i].bufTyp = 0; // expect a header 1303 rd[i].bufLen = sizeof(PEVNT_HEADER); // max size to read at begining 1353 1304 } 1354 1305 else … … 1370 1321 1371 1322 // Do not read from this socket 1372 if (rd[i].bufLen< =0)1323 if (rd[i].bufLen<0) 1373 1324 continue; 1374 1325 1375 1326 //numok++; 1376 1327 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) 1328 if (rd[i].bufLen>0) 1383 1329 { 1384 // There was just nothing waiting 1385 if (errno==EWOULDBLOCK || errno==EAGAIN) 1330 const int32_t jrd = 1331 recv(rd[i].socket, &rd[i].rBuf->B[rd[i].bufPos], 1332 rd[i].bufLen, MSG_DONTWAIT); 1333 1334 // recv failed 1335 if (jrd<0) 1386 1336 { 1387 //numok--; 1337 // There was just nothing waiting 1338 if (errno==EWOULDBLOCK || errno==EAGAIN) 1339 { 1340 //numok--; 1341 continue; 1342 } 1343 1344 factPrintf(kError, 442, "Reading from socket %d failed: %m (recv,rc=%d)", i, errno); 1345 //gi.gotErr[b]++; 1388 1346 continue; 1389 1347 } 1390 1348 1391 factPrintf(kError, 442, "Reading from socket %d failed: %m (recv,rc=%d)", i, errno); 1392 //gi.gotErr[b]++; 1393 continue; 1349 // connection was closed ... 1350 if (jrd==0) 1351 { 1352 factPrintf(kInfo, 441, "Socket %d closed by FAD", i); 1353 1354 const int s0 = sockDef[b] > 0 ? +1 : -1; 1355 GenSock(s0, i, 0, NULL, &rd[i]); 1356 1357 //gi.gotErr[b]++; 1358 1359 gi_NumConnect[b]-= cntsock ; 1360 //gi.numConn[b]--; 1361 gj.numConn[b]--; 1362 1363 continue; 1364 } 1365 // Success (jrd > 0) 1366 1367 gj.rateBytes[b] += jrd; 1368 1369 // are we skipping this board ... 1370 if (rd[i].bufTyp < 0) 1371 continue; 1372 1373 rd[i].bufPos += jrd; //==> prepare for continuation 1374 rd[i].bufLen -= jrd; 1375 1376 #ifdef EVTDEBUG 1377 debugRead(i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, rd[i].bufTyp, tv.tv_sec, tv.tv_usec); // i=socket; jrd=#bytes; ievt=eventid; 1=finished event 1378 #endif 1394 1379 } 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 continuation1423 rd[i].bufLen -= jrd;1424 1380 1425 1381 // are we reading data? … … 1429 1385 if (rd[i].bufLen > 0) 1430 1386 continue; 1431 1432 // ready to read next header1433 rd[i].bufTyp = 0;1434 rd[i].bufLen = frst_len;1435 rd[i].bufPos = 0;1436 1387 1437 1388 if (rd[i].rBuf->B[rd[i].fadLen - 1] != stop.B[0] || … … 1442 1393 i, rd[i].evtID, rd[i].fadLen, stop.B[0], stop.B[1], 1443 1394 rd[i].rBuf->B[rd[i].fadLen - 1], rd[i].rBuf->B[rd[i].fadLen - 2]); 1395 1396 // ready to read next header 1397 rd[i].bufTyp = 0; 1398 rd[i].bufLen = sizeof(PEVNT_HEADER); 1399 rd[i].bufPos = 0; 1400 1444 1401 continue; 1445 1402 } 1446 1447 #ifdef EVTDEBUG1448 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 event1449 #endif1450 1403 1451 1404 // int actid; … … 1458 1411 const int evID = mBufEvt(i); 1459 1412 1413 // no free entry in mBuffer, retry later 1414 if (evID < 0) 1415 continue; 1416 1417 // If memory has not been allocated yet, allocate it 1418 if (evID > 0 && mBuffer[evID].FADhead == NULL) 1419 { 1420 mBuffer[evID].FADhead = TGB_Malloc(); 1421 if (mBuffer[evID].FADhead == NULL) 1422 { 1423 // If this works properly, this is a hack which can be removed, or 1424 // replaced by a signal or dim message 1425 if (rd[i].bufTyp==2) 1426 factPrintf(kError, 882, "malloc failed for event %d", evID); 1427 rd[i].bufTyp = 2; 1428 continue; 1429 } 1430 1431 initEvent(evID); 1432 1433 // Some statistics 1434 gj.usdMem = tgb_inuse; 1435 1436 if (gj.usdMem > gj.maxMem) 1437 gj.maxMem = gj.usdMem; 1438 1439 gj.rateNew++; 1440 gj.bufTot++; 1441 if (gj.bufTot > gj.maxEvt) 1442 gj.maxEvt = gj.bufTot; 1443 1444 //register event in 'active list (reading)' 1445 evtIdx[evID] = evtCtrl.lastPtr; 1446 1447 evtCtrl.evtBuf[evtCtrl.lastPtr] = evID; 1448 evtCtrl.evtStat[evtCtrl.lastPtr] = 0; 1449 evtCtrl.pcTime[evtCtrl.lastPtr] = g_actTime; 1450 1451 evtCtrl.lastPtr++; 1452 evtCtrl.lastPtr %= MAX_EVT * MAX_RUN; 1453 } 1454 1455 // ready to read next header 1456 rd[i].bufTyp = 0; 1457 rd[i].bufLen = sizeof(PEVNT_HEADER); 1458 rd[i].bufPos = 0; 1459 1460 // Fatal error occured. Event cannot be processed. Skip it. Start reading next header. 1460 1461 if (evID < -1000) 1461 1462 continue; 1462 1463 1463 // no space left, retry later1464 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 1464 //we have a valid entry in mBuffer[]; fill it 1473 1474 1465 const int boardId = b; 1475 1466 const int fadBoard = rd[i].rBuf->S[12]; … … 1489 1480 rd[i].rBuf->B[rd[i].fadLen - 2], 1490 1481 rd[i].rBuf->B[rd[i].fadLen - 1]); 1491 // FIXME: Is that action the right one? 1492 continue; 1482 continue; // Continue reading next header 1493 1483 } 1494 1484 … … 1533 1523 //gi.evtTot++; 1534 1524 } 1525 1526 // Continue reading next header 1535 1527 } 1536 1528 else … … 1619 1611 1620 1612 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 1613 debugHead(i, fadBoard, rd[i].rBuf); 1614 1615 // Continue reading data 1616 1623 1617 } // end if data or header 1618 1624 1619 } // end for loop over all sockets 1625 1620 1626 //gi.numRead[numok]++;1627 1628 1621 g_actTime = time (NULL); 1629 if (g_actTime <= gi_SecTime) { 1622 if (g_actTime <= gi_SecTime) 1623 { 1630 1624 usleep(1); 1631 1625 continue;
Note:
See TracChangeset
for help on using the changeset viewer.