Index: trunk/FACT++/src/EventBuilder.c
===================================================================
--- trunk/FACT++/src/EventBuilder.c	(revision 11603)
+++ trunk/FACT++/src/EventBuilder.c	(revision 11689)
@@ -1,4 +1,4 @@
 
-//#define nanosleep(x,y)
+// // // #define EVTDEBUG
 
 
@@ -34,5 +34,7 @@
 #define MIN_LEN  32        // min #bytes needed to interpret FADheader
 #define MAX_LEN 256*1024   // size of read-buffer per socket
-   
+
+//#define nanosleep(x,y)
+
 extern FileHandle_t  runOpen(uint32_t irun, RUN_HEAD *runhd, size_t len ) ;
 extern int  runWrite(FileHandle_t fileHd ,  EVENT    *event, size_t len ) ;
@@ -61,4 +63,6 @@
 int  g_runStat   ;
 int  g_reset     ;
+int  g_useFTM    ;
+
 int gi_reset, gi_resetR, gi_resetS, gi_resetW, gi_resetX ;
 size_t g_maxMem  ;  //maximum memory allowed for buffer
@@ -138,12 +142,15 @@
   int  runID  ;          // run       "
   int  ftmID  ;          // event ID from FTM
-  uint fadLen ;         // FADlength of event currently read
+  uint fadLen ;          // FADlength of event currently read
   int  fadVers ;         // Version of FAD
+  int  ftmTyp ;          // trigger type
   int  board ;           // boardID (softwareID: 0..40 )
   int  Port ;
 
+  CNV_FACT *rBuf ;
+
+#ifdef EVTDEBUG
   CNV_FACT *xBuf ;      //a copy of rBuf (temporary for debuging) 
-  CNV_FACT *rBuf ;
-
+#endif
 
 } READ_STRUCT ;
@@ -202,5 +209,7 @@
   if (flag < 0) {
      free(rd->rBuf) ;   //and never open again
+#ifdef EVTDEBUG
      free(rd->xBuf) ;   //and never open again
+#endif
      rd->rBuf = NULL ;     
      rd->sockStat = 99 ;  
@@ -215,5 +224,7 @@
      rd->SockAddr.sin_addr = sockAddr->sin_addr ;
 
+#ifdef EVTDEBUG
      rd->xBuf = malloc(sizeof(CNV_FACT) ) ;
+#endif
      rd->rBuf = malloc(sizeof(CNV_FACT) ) ;
      if ( rd->rBuf == NULL ) {
@@ -275,5 +286,6 @@
 
 
-int mBufEvt( int evID, uint runID, int nRoi[8], int sk, int fadlen) {
+int mBufEvt( int evID, uint runID, int nRoi[], int sk, 
+             int fadlen, int trgTyp, int trgNum, int fadNum) {
 // generate a new Event into mBuffer:   
 // make sure only complete Event are possible, so 'free' will always work
@@ -337,4 +349,11 @@
             return -8201 ;
          }
+
+//       count for inconsistencies
+
+         if ( mBuffer[i].trgNum != trgNum ) mBuffer[i].Errors++   ;
+         if ( mBuffer[i].fadNum != fadNum ) mBuffer[i].Errors+=100 ;
+         if ( mBuffer[i].trgTyp != trgTyp ) mBuffer[i].Errors+=10000 ;
+
          //everything seems fine so far ==> use this slot ....
          return i ;  
@@ -460,7 +479,11 @@
    mBuffer[i].nRoi    = nRoi[0] ;
    mBuffer[i].nRoiTM  = nRoi[8] ;
-   mBuffer[i].evNum   = evID  ;
+   mBuffer[i].evNum   = evID ;
    mBuffer[i].runNum  = runID ;
+   mBuffer[i].fadNum  = fadNum;
+   mBuffer[i].trgNum  = trgNum;
+   mBuffer[i].trgTyp  = trgTyp ;
    mBuffer[i].evtLen  = needmem ;
+   mBuffer[i].Errors  = 0 ;
 
    gj.usdMem += needmem + headmem;
@@ -577,5 +600,5 @@
   int32_t jrd ;
   uint gi_SecTime ;        //time in seconds
-  int boardId, roi[8],drs,px,src,pixS,pixH,pixC,pixR,tmS ;
+  int boardId, roi[9],drs,px,src,pixS,pixH,pixC,pixR,tmS ;
 
   int goodhed = 0 ;
@@ -758,9 +781,11 @@
           if (jrd >0 ) {
              debugStream(i,&rd[i].rBuf->B[ rd[i].bufPos],jrd) ;
+#ifdef EVTDEBUG
              memcpy(&rd[i].xBuf->B[ rd[i].bufPos], &rd[i].rBuf->B[ rd[i].bufPos], jrd) ;
- snprintf(str,MXSTR,"read sock %3d bytes %5d len %5d first %d %d",i,jrd,rd[i].bufLen,
- rd[i].rBuf->B[ rd[i].bufPos  ],
- rd[i].rBuf->B[ rd[i].bufPos  +1] );
- factOut(kDebug,301, str ) ;
+             snprintf(str,MXSTR,"read sock %3d bytes %5d len %5d first %d %d",i,jrd,rd[i].bufLen,
+             rd[i].rBuf->B[ rd[i].bufPos  ],
+             rd[i].rBuf->B[ rd[i].bufPos  +1] );
+             factOut(kDebug,301, str ) ;
+#endif
           }
 
@@ -796,4 +821,8 @@
         if ( rd[i].bufTyp <0 ) { // we are skipping this board ...
 //         just do nothing
+#ifdef EVTDEBUG
+           snprintf(str,MXSTR,"skipping %d bytes on socket %d",jrd,i) ;
+           factOut(kInfo,301, str ) ;
+#endif
 
         } else if ( rd[i].bufTyp >0 ) { // we are reading data ...
@@ -814,10 +843,12 @@
                 goto EndBuf ;
 
-//           } else {
-//              snprintf(str,MXSTR,"good  end of buffer found sock %3d len %5d %d %d : %d %d - %d %d : %d %d",
-//                 i,rd[i].fadLen, 
-//                 rd[i].rBuf->B[ 0 ],             rd[i].rBuf->B[ 1 ],           start.B[1],start.B[0],
-//                 rd[i].rBuf->B[ rd[i].bufPos-2], rd[i].rBuf->B[ rd[i].bufPos-1], stop.B[1], stop.B[0]);
-//              factOut(kDebug,301, str ) ;
+#ifdef EVTDEBUG
+             } else {
+                snprintf(str,MXSTR,"good  end of buffer found sock %3d len %5d %d %d : %d %d - %d %d : %d %d",
+                   i,rd[i].fadLen, 
+                   rd[i].rBuf->B[ 0 ],             rd[i].rBuf->B[ 1 ],           start.B[1],start.B[0],
+                   rd[i].rBuf->B[ rd[i].bufPos-2], rd[i].rBuf->B[ rd[i].bufPos-1], stop.B[1], stop.B[0]);
+                factOut(kDebug,301, str ) ;
+#endif
              }
 
@@ -831,5 +862,11 @@
              }
              //get index into mBuffer for this event (create if needed)
-             evID = mBufEvt( rd[i].evtID, rd[i].runID, roi, i, rd[i].fadLen ) ;
+
+             int actid; 
+             if (g_useFTM >0) actid = rd[i].evtID ;
+             else             actid = rd[i].ftmID ;
+
+             evID = mBufEvt( rd[i].evtID, rd[i].runID, roi, i, 
+                    rd[i].fadLen, rd[i].ftmTyp, rd[i].ftmID, rd[i].evtID ) ;
 
              if (evID <-1000) {
@@ -837,9 +874,10 @@
              }
              if (evID < 0) {    //no space left, retry later
-
-     if ( rd[i].bufLen != 0) { 
-        snprintf(str,MXSTR,"something screwed up");
-        factOut(kFatal, 1, str ) ;
-     }
+#ifdef EVTDEBUG
+                if ( rd[i].bufLen != 0) { 
+                    snprintf(str,MXSTR,"something screwed up");
+                    factOut(kFatal, 1, str ) ;
+                }
+#endif
                 xwait.tv_sec = 0;
                 xwait.tv_nsec= 10000000 ;  // sleep for ~10 msec
@@ -851,19 +889,20 @@
              //we have a valid entry in mBuffer[]; fill it
 
-int xchk = memcmp(&rd[i].xBuf->B[0], &rd[i].rBuf->B[0], rd[i].fadLen   ) ;
-if (xchk != 0) {
-   snprintf(str,MXSTR,"ERROR OVERWRITE %d %d on port %d",xchk,rd[i].fadLen,i) ;
-   factOut(kFatal, 1, str ) ;
-
-   uint iq;
-   for (iq=0; iq < rd[i].fadLen  ; iq++) {
-      if (rd[i].rBuf->B[iq] != rd[i].xBuf->B[iq] ) {
-         snprintf(str,MXSTR,"ERROR %4d %4d %x %x",i,iq,rd[i].rBuf->B[iq], rd[i].xBuf->B[iq]);
-         factOut(kFatal, 1, str ) ;
-      }
-   }
-}
-
-
+#ifdef EVTDEBUG
+            int xchk = memcmp(&rd[i].xBuf->B[0], &rd[i].rBuf->B[0], rd[i].fadLen   ) ;
+            if (xchk != 0) {
+               snprintf(str,MXSTR,"ERROR OVERWRITE %d %d on port %d",xchk,rd[i].fadLen,i) ;
+               factOut(kFatal, 1, str ) ;
+
+               uint iq;
+               for (iq=0; iq < rd[i].fadLen  ; iq++) {
+                  if (rd[i].rBuf->B[iq] != rd[i].xBuf->B[iq] ) {
+                     snprintf(str,MXSTR,"ERROR %4d %4d %x %x",i,iq,rd[i].rBuf->B[iq], rd[i].xBuf->B[iq]);
+                     factOut(kFatal, 1, str ) ;
+                  }
+               }
+             }
+#endif
+             int qncpy = 0 ;
              boardId = b ;
              int fadBoard = ntohs(rd[i].rBuf->S[12] ) ;
@@ -871,5 +910,5 @@
              if (boardId != (fadCrate*10 + fadBoard%256) ) {
                 snprintf(str,MXSTR,"wrong Board ID %d %d %d",fadCrate,fadBoard%256,boardId) ;
-                if (errcnt0++ < 99 ) factOut(kWarn,301, str ) ;  //print only few times
+                factOut(kWarn,301, str ) ;  
              }
              if ( mBuffer[evID].board[ boardId ] != -1) {   
@@ -885,5 +924,5 @@
              memcpy( &mBuffer[evID].FADhead[boardId].start_package_flag,
                         &rd[i].rBuf->S[0], head_len) ;
-//  xxx          roi  = mBuffer[evID].nRoi ;
+             qncpy+=head_len ;
 
              src  = head_len/2 ;
@@ -898,33 +937,27 @@
                    src++  ;
 
-//  xxx                 if ( ( px != 8 && pixR == roi )
-//  xxx                   || ( px == 8 && pixR >= roi ) ) {   //we have a reasonable roi
                       
-                      mBuffer[evID].fEvent->StartPix[pixS] =pixC;
-                      dest= pixS * roi[0] ;
-                      memcpy(
+                   mBuffer[evID].fEvent->StartPix[pixS] =pixC;
+                   dest= pixS * roi[0] ;
+                   memcpy(
+                        &mBuffer[evID].fEvent->Adc_Data[dest],
+                        &rd[i].rBuf->S[src],  roi[0] * 2) ;
+                   qncpy+=roi[0]*2 ;
+                   src+= pixR ;
+
+                   if ( px==8 ) {
+                      tmS =boardId*4 + drs ;
+                      if ( pixR > roi[0]) {          //and we have additional TM info
+                         dest= tmS * roi[0] + NPIX* roi[0] ;
+                         int srcT= src - roi[0] ;
+                         mBuffer[evID].fEvent->StartTM[tmS] = (pixC+pixR-roi[0])%1024 ;
+                         memcpy(
                            &mBuffer[evID].fEvent->Adc_Data[dest],
-                           &rd[i].rBuf->S[src],  roi[0] * 2) ;
-                      src+= pixR ;
-
-                      if ( px==8 ) {
-                         if ( pixR > roi[0]) {          //and we have additional TM info
-                            tmS =boardId*4 + drs ;
-                            dest= tmS * roi[0] + NPIX* roi[0] ;
-                            int srcT= src - roi[0] ;
-                            mBuffer[evID].fEvent->StartTM[tmS] = (pixC+pixR-roi[0])%1024 ;
-                            memcpy(
-                              &mBuffer[evID].fEvent->Adc_Data[dest],
-                              &rd[i].rBuf->S[srcT],  roi[0] * 2) ;
-                         } else {
-                            mBuffer[evID].fEvent->StartTM[tmS] = -1 ;
-                         }
+                           &rd[i].rBuf->S[srcT],  roi[0] * 2) ;
+                           qncpy+=roi[0]*2 ;
+                      } else {
+                         mBuffer[evID].fEvent->StartTM[tmS] = -1 ;
                       }
-//  xxx                 } else {
-//  xxx                    snprintf(str,MXSTR,"wrong roi %d %d %d %d",px,pixR,roi,src-2);
-//  xxx                    gi.evtErr++ ;
-//  xxx                    factOut(kError,202, str ) ;
-//  xxx                    goto EndBuf ;
-//  xxx                 }
+                   }
                 }
              }// now we have stored a new board contents into Event structure
@@ -936,16 +969,15 @@
 
              if (++mBuffer[evID].nBoard >= actBoards ) {
-                snprintf(str,MXSTR,"%5d complete event %8d %8d %2d",mBuffer[evID].evNum,evtCtrl.evtBuf[iDx],iDx,evtCtrl.evtStat[ iDx ]);
-                factOut(kDebug,-1, str ) ;
-
+                int qnrun =0 ;
                 if (mBuffer[evID].runNum != actrun ) {        // have we already reported first event of this run ???
                    actrun = mBuffer[evID].runNum ;
                    int ir ;
                    for ( ir=0; ir<MAX_RUN; ir++) {
+                      qnrun++ ;
                       if ( runCtrl[ir].runId == actrun) {
                          if ( ++runCtrl[ir].lastEvt ==0 ) {
                             gotNewRun( actrun, mBuffer[evID].FADhead ); 
-                snprintf(str,MXSTR,"gotNewRun %d (ev %d)",mBuffer[evID].runNum,mBuffer[evID].evNum);
-                factOut(kInfo,1, str ) ;
+                            snprintf(str,MXSTR,"gotNewRun %d (ev %d)",mBuffer[evID].runNum,mBuffer[evID].evNum);
+                            factOut(kInfo,1, str ) ;
                             break ;
                          }
@@ -953,4 +985,8 @@
                    }
                 }
+                snprintf(str,MXSTR,"%5d complete event roi %4d roiTM %d cpy %8d %5d",
+                   mBuffer[evID].evNum,roi[0],roi[8]-roi[0],qncpy,qnrun);
+                factOut(kDebug,-1, str ) ;
+factOut(kInfo,-1, str ) ;
 
                 //complete event read ---> flag for next processing
@@ -985,5 +1021,7 @@
                  rd[i].bufLen += k ;
                  memcpy(&rd[i].rBuf->B[0], &rd[i].rBuf->B[k], rd[i].bufPos ) ;
+#ifdef EVTDEBUG
                  memcpy(&rd[i].xBuf->B[0], &rd[i].xBuf->B[k], rd[i].bufPos ) ;
+#endif
               }
               if ( rd[i].bufPos >= minLen ) {
@@ -996,4 +1034,5 @@
                  rd[i].fadLen = ntohs(rd[i].rBuf->S[1])*2 ; 
                  rd[i].fadVers= ntohs(rd[i].rBuf->S[2]) ;
+                 rd[i].ftmTyp = ntohl(rd[i].rBuf->S[5]) ; 
                  rd[i].evtID  = ntohl(rd[i].rBuf->I[4]) ; //(FADevt)
                  rd[i].ftmID  = ntohl(rd[i].rBuf->I[5]) ; //(FTMevt)
@@ -1005,6 +1044,8 @@
                  int fadcrate = fadboard/256 ;
                      fadboard = (fadcrate*10 + fadboard%256) ; 
-snprintf(str,MXSTR,"sk %3d head: %5d %5d %5d %10d %4d %6d",i,rd[i].fadLen,rd[i].evtID,rd[i].ftmID,rd[i].runID,fadboard,jrd) ;
-factOut(kDebug,1, str ) ;
+#ifdef EVTDEBUG
+                     snprintf(str,MXSTR,"sk %3d head: %5d %5d %5d %10d %4d %6d",i,rd[i].fadLen,rd[i].evtID,rd[i].ftmID,rd[i].runID,fadboard,jrd) ;
+                     factOut(kDebug,1, str ) ;
+#endif
 
                  if (rd[i].runID ==0 ) rd[i].runID = gi_myRun ;
@@ -1029,6 +1070,9 @@
     } //finished trying to read all sockets
 
-snprintf(str,MXSTR,"Loop ---- %3d --- %8d",numokx,jrdx);
-factOut(kDebug,-1, str ) ;
+#ifdef EVTDEBUG
+    snprintf(str,MXSTR,"Loop ---- %3d --- %8d",numokx,jrdx);
+    factOut(kDebug,-1, str ) ;
+#endif
+
     gi.numRead[ numok ] ++ ;
 
@@ -1285,6 +1329,9 @@
            int      id   = evtCtrl.evtBuf[k0] ;
            int      ievt = mBuffer[id].evNum ;
+           int      itevt= mBuffer[id].trgNum ;
+           int      itrg = mBuffer[id].trgTyp ;
            int      roi  = mBuffer[id].nRoi ;
            int      roiTM= mBuffer[id].nRoiTM ;
+           int      Errors=mBuffer[id].Errors ;
 //         uint32_t irun = mBuffer[id].runNum ;
 //snprintf(str,MXSTR,"P processing %d %d %d %d",ievt,k,id,evtCtrl.evtStat[k0]) ;
@@ -1292,4 +1339,5 @@
 
 //make sure unused pixels/tmarks are cleared to zero
+           if (roiTM == roi) roiTM=0 ;
            int ip,it,dest,ib;
            for (ip=0; ip<NPIX; ip++) {
@@ -1311,6 +1359,10 @@
            mBuffer[id].fEvent->RoiTM = roiTM ;
            mBuffer[id].fEvent->EventNum = ievt ;
-           mBuffer[id].fEvent->TriggerType = 0 ; // TBD
+           mBuffer[id].fEvent->TriggerNum = itevt ;
+           mBuffer[id].fEvent->TriggerType = itrg ;
+           mBuffer[id].fEvent->Errors = Errors ;
            mBuffer[id].fEvent->SoftTrig = 0 ;
+
+
            for (ib=0; ib<NBOARDS; ib++) {
               if (mBuffer[id].board[ib] == -1 ) {  //board is not read
