Index: trunk/FACT++/src/EventBuilder.c
===================================================================
--- trunk/FACT++/src/EventBuilder.c	(revision 12090)
+++ trunk/FACT++/src/EventBuilder.c	(revision 12091)
@@ -2,6 +2,8 @@
 // // // #define EVTDEBUG
 
-#define NUMSOCK   1          //set to 7 for old configuration
-#define MAXREAD  65536       //64kB wiznet buffer
+#define NUMSOCK   1             //set to 7 for old configuration
+#define MAXREAD  65536          //64kB wiznet buffer
+#define MIN_LEN  32             // min #bytes needed to interpret FADheader
+#define MAX_LEN 256*1024        // size of read-buffer per socket
 
 #include <stdlib.h>
@@ -21,5 +23,5 @@
 #include <netinet/tcp.h>
 #include <pthread.h>
-#include <sched.h>                              
+#include <sched.h>
 
 #include "EventBuilder.h"
@@ -35,6 +37,7 @@
 };
 
-#define MIN_LEN  32             // min #bytes needed to interpret FADheader
-#define MAX_LEN 256*1024        // size of read-buffer per socket
+
+void *gi_procPtr;
+
 
 //#define nanosleep(x,y)
@@ -45,4 +48,10 @@
 //extern int runFinish (uint32_t runnr);
 
+
+
+extern void * runStart (uint32_t irun, RUN_HEAD * runhd, size_t len);
+
+extern int  runEnd (uint32_t, void * runPtr );
+
 extern void factOut (int severity, int err, char *message);
 
@@ -54,8 +63,9 @@
 extern void factStatNew (EVT_STAT gi);
 
-extern int eventCheck (uint32_t runNr, PEVNT_HEADER * fadhd, EVENT * event);
+extern int eventCheck (uint32_t runNr, PEVNT_HEADER * fadhd, EVENT * event,
+                       int mboard);
 
 extern int subProcEvt (int threadID, PEVNT_HEADER * fadhd, EVENT * event,
-                       int8_t * buffer);
+                       int16_t mboard, void * buffer);
 
 extern void debugHead (int i, int j, void *buf);
@@ -71,6 +81,8 @@
 int g_maxProc;
 int g_maxSize;
-int gi_maxSize;
+int gi_maxSize =0;
 int gi_maxProc;
+uint32_t gi_actRun;
+void *gi_runPtr;
 
 uint g_actTime;
@@ -446,5 +458,5 @@
       if (runCtrl[k].runId == runID) {
 //         if (runCtrl[k].procId > 0) {   //run is closed -> reject
-//            snprintf (str, MXSTR, "skip event since run %d finished", runID);
+//            snprintf (str, MXSTR, "skip event since run %d already closed", runID);
 //            factOut (kInfo, 931, str);
 //            return -21;
@@ -494,5 +506,4 @@
       runCtrl[evFree].firstTime = runCtrl[evFree].lastTime = tsec;
       runCtrl[evFree].closeTime = tsec + 3600 * 24;     //max time allowed
-//    runCtrl[evFree].lastTime = 0;
 
       runTail[evFree].nEventsOk =
@@ -539,14 +550,19 @@
    }
 
-   mBuffer[i].buffer = malloc (gi_maxSize);
-   if (mBuffer[i].buffer == NULL) {
-      snprintf (str, MXSTR, "malloc buffer failed for event %d", evID);
-      factOut (kError, 882, str);
-      free (mBuffer[i].FADhead);
-      mBuffer[i].FADhead = NULL;
-      free (mBuffer[i].fEvent);
-      mBuffer[i].fEvent = NULL;
-      return -32;
-   }
+// if (gi_maxSize > 0) {
+//    mBuffer[i].buffer = malloc (gi_maxSize);
+//    if (mBuffer[i].buffer == NULL) {
+//       snprintf (str, MXSTR, "malloc buffer failed for event %d", evID);
+//       factOut (kError, 882, str);
+//       free (mBuffer[i].FADhead);
+//       mBuffer[i].FADhead = NULL;
+//       free (mBuffer[i].fEvent);
+//       mBuffer[i].fEvent = NULL;
+//       return -32;
+//    }
+// } else {
+//    mBuffer[i].buffer = NULL;
+// }
+
    //flag all boards as unused
    mBuffer[i].nBoard = 0;
@@ -631,6 +647,8 @@
    mBuffer[i].FADhead = NULL;
 
-   free (mBuffer[i].buffer);
-   mBuffer[i].buffer = NULL;
+// if (gi_maxSize > 0) {
+//    free (mBuffer[i].buffer);
+//    mBuffer[i].buffer = NULL;
+// }
 
    headmem = NBOARDS * sizeof (PEVNT_HEADER);
@@ -711,4 +729,5 @@
    int goodhed = 0;
 
+   char str[MXSTR];
    int sockDef[NBOARDS];        //internal state of sockets
    int jrdx;
@@ -730,14 +749,14 @@
 
 /* CPU_ZERO initializes all the bits in the mask to zero. */
-   CPU_ZERO (&mask);
+//   CPU_ZERO (&mask);
 /* CPU_SET sets only the bit corresponding to cpu. */
    cpu = 7;
-   CPU_SET (cpu, &mask);
+//   CPU_SET (cpu, &mask);
 
 /* sched_setaffinity returns 0 in success */
-   if (sched_setaffinity (0, sizeof (mask), &mask) == -1) {
-      snprintf (str, MXSTR, "W ---> can not create affinity to %d", cpu);
-      factOut (kWarn, -1, str);
-   }
+//   if (sched_setaffinity (0, sizeof (mask), &mask) == -1) {
+//      snprintf (str, MXSTR, "W ---> can not create affinity to %d", cpu);
+//      factOut (kWarn, -1, str);
+//   }
 
 
@@ -758,4 +777,7 @@
    for (i = 0; i < NBOARDS; i++)
       sockDef[i] = 0;
+
+   gi_actRun = 0;
+
 
  START:
@@ -772,5 +794,5 @@
    numok = numok2 = 0;
 
-   int cntsock = 8 - NUMSOCK ;
+   int cntsock = 8 - NUMSOCK;
 
    if (gi_resetS > 0) {
@@ -802,5 +824,5 @@
          gj.evtSkip = gj.evtWrite = gj.evtErr = 0;
 
-      int b,p;
+      int b, p;
       for (b = 0; b < NBOARDS; b++)
          gj.badRoi[b] = 0;
@@ -846,5 +868,6 @@
 
             for (p = p0 + 1; p < p0 + 8; p++) {
-               GenSock (s0, k, p, &g_port[b].sockAddr, &rd[k]); //generate address and socket
+               if (p < NUMSOCK)
+                  GenSock (s0, k, p, &g_port[b].sockAddr, &rd[k]);      //generate address and socket
                k++;
             }
@@ -867,376 +890,380 @@
 
       for (i = 0; i < MAX_SOCK; i++) {  //check all sockets if something to read
-         b = i / 7 ;
-         p = i % 7 ;
-
-//if ( b==32 && p>0) {  ; }
-if ( p >= NUMSOCK) { ; }
-else {
-         if (sockDef[b] > 0)
-            s0 = +1;
-         else
-            s0 = -1;
-
-         if (rd[i].sockStat < 0) {      //try to connect if not yet done
-            rd[i].sockStat = connect (rd[i].socket,
-                                      (struct sockaddr *) &rd[i].SockAddr,
-                                      sizeof (rd[i].SockAddr));
-            if (rd[i].sockStat == 0) {  //successfull ==>
-               if (sockDef[b] > 0) {
-                  rd[i].bufTyp = 0;     //  expect a header
-                  rd[i].bufLen = frst_len;      //  max size to read at begining
-               } else {
-                  rd[i].bufTyp = -1;    //  full data to be skipped
-                  rd[i].bufLen = MAX_LEN;       //huge for skipping
+         b = i / 7;
+         p = i % 7;
+
+         if (p >= NUMSOCK) {;
+         } else {
+            if (sockDef[b] > 0)
+               s0 = +1;
+            else
+               s0 = -1;
+
+            if (rd[i].sockStat < 0) {   //try to connect if not yet done
+               rd[i].sockStat = connect (rd[i].socket,
+                                         (struct sockaddr *) &rd[i].SockAddr,
+                                         sizeof (rd[i].SockAddr));
+               if (rd[i].sockStat == 0) {       //successfull ==>
+                  if (sockDef[b] > 0) {
+                     rd[i].bufTyp = 0;  //  expect a header
+                     rd[i].bufLen = frst_len;   //  max size to read at begining
+                  } else {
+                     rd[i].bufTyp = -1; //  full data to be skipped
+                     rd[i].bufLen = MAX_LEN;    //huge for skipping
+                  }
+                  rd[i].bufPos = 0;     //  no byte read so far
+                  rd[i].skip = 0;       //  start empty
+                  gi_NumConnect[b] += cntsock;
+
+                  gi.numConn[b]++;
+                  gj.numConn[b]++;
+                  snprintf (str, MXSTR, "+++connect %d %d", b, gi.numConn[b]);
+                  factOut (kInfo, -1, str);
                }
-               rd[i].bufPos = 0;        //  no byte read so far
-               rd[i].skip = 0;  //  start empty
-//             gi_NumConnect[b]++;
-               gi_NumConnect[b] += cntsock ;
-
-               gi.numConn[b]++;
-               gj.numConn[b]++;
-               snprintf (str, MXSTR, "+++connect %d %d", b, gi.numConn[b]);
-               factOut (kInfo, -1, str);
             }
-         }
-
-         if (rd[i].sockStat == 0) {     //we have a connection ==> try to read
-            if (rd[i].bufLen > 0) {     //might be nothing to read [buffer full]
-               numok++;
-               size_t maxread = rd[i].bufLen ;
-               if (maxread > MAXREAD ) maxread=MAXREAD ;
-
-               jrd =
-                  recv (rd[i].socket, &rd[i].rBuf->B[rd[i].bufPos],
-                        maxread, MSG_DONTWAIT);
-//                      rd[i].bufLen, MSG_DONTWAIT);
-
-               if (jrd > 0) {
-                  debugStream (i, &rd[i].rBuf->B[rd[i].bufPos], jrd);
+
+            if (rd[i].sockStat == 0) {  //we have a connection ==> try to read
+               if (rd[i].bufLen > 0) {  //might be nothing to read [buffer full]
+                  numok++;
+                  size_t maxread = rd[i].bufLen;
+                  if (maxread > MAXREAD)
+                     maxread = MAXREAD;
+
+                  jrd =
+                     recv (rd[i].socket, &rd[i].rBuf->B[rd[i].bufPos],
+                           maxread, MSG_DONTWAIT);
+
+                  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);
-#endif
-               }
-
-               if (jrd == 0) {  //connection has closed ...
-                  snprintf (str, MXSTR, "Socket %d closed by FAD", i);
-                  factOut (kInfo, 441, str);
-                  GenSock (s0, i, 0, NULL, &rd[i]);
-                  gi.gotErr[b]++;
-//                gi_NumConnect[b]--;
-                  gi_NumConnect[b]-= cntsock ;
-                  gi.numConn[b]--;
-                  gj.numConn[b]--;
-
-               } else if (jrd < 0) {    //did not read anything
-                  if (errno != EAGAIN && errno != EWOULDBLOCK) {
-                     snprintf (str, MXSTR, "Error Reading from %d | %m", i);
-                     factOut (kError, 442, str);
-                     gi.gotErr[b]++;
-                  } else
-                     numok--;   //else nothing waiting to be read
-                  jrd = 0;
-               }
-            } else {
-               jrd = 0;         //did read nothing as requested
-               snprintf (str, MXSTR, "do not read from socket %d  %d", i,
-                         rd[i].bufLen);
-               factOut (kDebug, 301, str);
-            }
-
-            gi.gotByte[b] += jrd;
-            gj.rateBytes[b] += jrd;
-
-            if (jrd > 0) {
-               numokx++;
-               jrdx += jrd;
-            }
-
-
-            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 ...
-               if (jrd < rd[i].bufLen) {        //not yet all read
-                  rd[i].bufPos += jrd;  //==> prepare for continuation
-                  rd[i].bufLen -= jrd;
-                  debugRead (i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, 0, tsec, tusec);    // i=socket; jrd=#bytes; ievt=eventid; 0=reading data
-               } else {         //full dataset read
-                  rd[i].bufLen = 0;
-                  rd[i].bufPos = rd[i].fadLen;
-                  if (rd[i].rBuf->B[rd[i].bufPos - 1] != stop.B[0]
-                      || rd[i].rBuf->B[rd[i].bufPos - 2] != stop.B[1]) {
-                     gi.evtErr++;
+                     memcpy (&rd[i].xBuf->B[rd[i].bufPos],
+                             &rd[i].rBuf->B[rd[i].bufPos], jrd);
                      snprintf (str, MXSTR,
-                               "wrong end of buffer found sock %3d ev %4d len %5d %3d %3d - %3d %3d ",
-                               i, rd[i].fadLen, rd[i].evtID, rd[i].rBuf->B[0],
-                               rd[i].rBuf->B[1],
-                               rd[i].rBuf->B[rd[i].bufPos - 2],
-                               rd[i].rBuf->B[rd[i].bufPos - 1]);
-                     factOut (kError, 301, str);
-                     goto EndBuf;
-
-#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]);
+                               "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
                   }
 
-                  if (jrd > 0)
-                     debugRead (i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, 1, tsec, tusec); // i=socket; jrd=#bytes; ievt=eventid; 1=finished event
-
-                  //we have a complete buffer, copy to WORK area
-                  int jr;
-                  roi[0] = ntohs (rd[i].rBuf->S[head_len / 2 + 2]);
-                  for (jr = 0; jr < 9; jr++) {
-                     roi[jr] =
-                        ntohs (rd[i].
-                               rBuf->S[head_len / 2 + 2 + jr * (roi[0] + 4)]);
+                  if (jrd == 0) {       //connection has closed ...
+                     snprintf (str, MXSTR, "Socket %d closed by FAD", i);
+                     factOut (kInfo, 441, str);
+                     GenSock (s0, i, 0, NULL, &rd[i]);
+                     gi.gotErr[b]++;
+                     gi_NumConnect[b] -= cntsock;
+                     gi.numConn[b]--;
+                     gj.numConn[b]--;
+
+                  } else if (jrd < 0) { //did not read anything
+                     if (errno != EAGAIN && errno != EWOULDBLOCK) {
+                        snprintf (str, MXSTR, "Error Reading from %d | %m",
+                                  i);
+                        factOut (kError, 442, str);
+                        gi.gotErr[b]++;
+                     } else
+                        numok--;        //else nothing waiting to be read
+                     jrd = 0;
                   }
-                  //get index into mBuffer for this event (create if needed)
-
-                  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) {
-                     goto EndBuf;       //not usable board/event/run --> skip it
-                  }
-                  if (evID < 0) {       //no space left, retry later
+               } else {
+                  jrd = 0;      //did read nothing as requested
+                  snprintf (str, MXSTR, "do not read from socket %d  %d", i,
+                            rd[i].bufLen);
+                  factOut (kDebug, 301, str);
+               }
+
+               gi.gotByte[b] += jrd;
+               gj.rateBytes[b] += jrd;
+
+               if (jrd > 0) {
+                  numokx++;
+                  jrdx += jrd;
+               }
+
+
+               if (rd[i].bufTyp < 0) {  // we are skipping this board ...
+                  //         just do nothing
 #ifdef EVTDEBUG
-                     if (rd[i].bufLen != 0) {
-                        snprintf (str, MXSTR, "something screwed up");
-                        factOut (kFatal, 1, str);
+                  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 ...
+                  if (jrd < rd[i].bufLen) {     //not yet all read
+                     rd[i].bufPos += jrd;       //==> prepare for continuation
+                     rd[i].bufLen -= jrd;
+                     debugRead (i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, 0, tsec, tusec); // i=socket; jrd=#bytes; ievt=eventid; 0=reading data
+                  } else {      //full dataset read
+                     rd[i].bufLen = 0;
+                     rd[i].bufPos = rd[i].fadLen;
+                     if (rd[i].rBuf->B[rd[i].bufPos - 1] != stop.B[0]
+                         || rd[i].rBuf->B[rd[i].bufPos - 2] != stop.B[1]) {
+                        gi.evtErr++;
+                        snprintf (str, MXSTR,
+                                  "wrong end of buffer found sock %3d ev %4d len %5d %3d %3d - %3d %3d ",
+                                  i, rd[i].fadLen, rd[i].evtID,
+                                  rd[i].rBuf->B[0], rd[i].rBuf->B[1],
+                                  rd[i].rBuf->B[rd[i].bufPos - 2],
+                                  rd[i].rBuf->B[rd[i].bufPos - 1]);
+                        factOut (kError, 301, str);
+                        goto EndBuf;
+
+#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
                      }
-#endif
-                     xwait.tv_sec = 0;
-                     xwait.tv_nsec = 10000000;  // sleep for ~10 msec
-                     nanosleep (&xwait, NULL);
-                     goto EndBuf1;      //hope there is free space next round
-                  }
-                  //we have a valid entry in mBuffer[]; fill it
-
+
+                     if (jrd > 0)
+                        debugRead (i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, 1, tsec, tusec);      // i=socket; jrd=#bytes; ievt=eventid; 1=finished event
+
+                     //we have a complete buffer, copy to WORK area
+                     int jr;
+                     roi[0] = ntohs (rd[i].rBuf->S[head_len / 2 + 2]);
+                     for (jr = 0; jr < 9; jr++) {
+                        roi[jr] =
+                           ntohs (rd[i].rBuf->S[head_len / 2 + 2 +
+                                          jr * (roi[0] + 4)]);
+                     }
+                     //get index into mBuffer for this event (create if needed)
+
+                     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) {
+                        goto EndBuf;    //not usable board/event/run --> skip it
+                     }
+                     if (evID < 0) {    //no space left, retry later
 #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]);
+                        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
+                        nanosleep (&xwait, NULL);
+                        goto EndBuf1;   //hope there is free space next round
                      }
-                  }
-#endif
-                  int qncpy = 0;
-                  boardId = b;
-                  int fadBoard = ntohs (rd[i].rBuf->S[12]);
-                  int fadCrate = fadBoard / 256;
-                  if (boardId != (fadCrate * 10 + fadBoard % 256)) {
-                     snprintf (str, MXSTR, "wrong Board ID %d %d %d",
-                               fadCrate, fadBoard % 256, boardId);
-                     factOut (kWarn, 301, str);
-                  }
-                  if (mBuffer[evID].board[boardId] != -1) {
-                     snprintf (str, MXSTR,
-                               "double board: ev %5d, b %3d, %3d;  len %5d %3d %3d - %3d %3d ",
-                               evID, boardId, i, rd[i].fadLen,
-                               rd[i].rBuf->B[0], rd[i].rBuf->B[1],
-                               rd[i].rBuf->B[rd[i].bufPos - 2],
-                               rd[i].rBuf->B[rd[i].bufPos - 1]);
-                     factOut (kWarn, 501, str);
-                     goto EndBuf;       //--> skip Board
-                  }
-
-                  int iDx = evtIdx[evID];       //index into evtCtrl
-
-                  memcpy (&mBuffer[evID].FADhead[boardId].start_package_flag,
-                          &rd[i].rBuf->S[0], head_len);
-                  qncpy += head_len;
-
-                  src = head_len / 2;
-                  for (px = 0; px < 9; px++) {  //different sort in FAD board.....
-                     for (drs = 0; drs < 4; drs++) {
-                        pixH = ntohs (rd[i].rBuf->S[src++]);    // ID
-                        pixC = ntohs (rd[i].rBuf->S[src++]);    // start-cell
-                        pixR = ntohs (rd[i].rBuf->S[src++]);    // roi
-//here we should check if pixH is correct ....
-
-                        pixS = boardId * 36 + drs * 9 + px;
-                        src++;
-
-
-                        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[srcT], roi[0] * 2);
-                              qncpy += roi[0] * 2;
-                           } else {
-                              mBuffer[evID].fEvent->StartTM[tmS] = -1;
+                     //we have a valid entry in mBuffer[]; fill it
+
+#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);
                            }
                         }
                      }
-                  }             // now we have stored a new board contents into Event structure
-
-                  mBuffer[evID].fEvent->NumBoards++;
-                  mBuffer[evID].board[boardId] = boardId;
-                  evtCtrl.evtStat[iDx]++;
-                  evtCtrl.pcTime[iDx] = g_actTime;
-
-                  if (++mBuffer[evID].nBoard >= actBoards) {
-                     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);
-                                 break;
+#endif
+                     int qncpy = 0;
+                     boardId = b;
+                     int fadBoard = ntohs (rd[i].rBuf->S[12]);
+                     int fadCrate = fadBoard / 256;
+                     if (boardId != (fadCrate * 10 + fadBoard % 256)) {
+                        snprintf (str, MXSTR, "wrong Board ID %d %d %d",
+                                  fadCrate, fadBoard % 256, boardId);
+                        factOut (kWarn, 301, str);
+                     }
+                     if (mBuffer[evID].board[boardId] != -1) {
+                        snprintf (str, MXSTR,
+                                  "double board: ev %5d, b %3d, %3d;  len %5d %3d %3d - %3d %3d ",
+                                  evID, boardId, i, rd[i].fadLen,
+                                  rd[i].rBuf->B[0], rd[i].rBuf->B[1],
+                                  rd[i].rBuf->B[rd[i].bufPos - 2],
+                                  rd[i].rBuf->B[rd[i].bufPos - 1]);
+                        factOut (kWarn, 501, str);
+                        goto EndBuf;    //--> skip Board
+                     }
+
+                     int iDx = evtIdx[evID];    //index into evtCtrl
+
+                     memcpy (&mBuffer[evID].FADhead[boardId].
+                             start_package_flag, &rd[i].rBuf->S[0], head_len);
+                     qncpy += head_len;
+
+                     src = head_len / 2;
+                     for (px = 0; px < 9; px++) {       //different sort in FAD board.....
+                        for (drs = 0; drs < 4; drs++) {
+                           pixH = ntohs (rd[i].rBuf->S[src++]); // ID
+                           pixC = ntohs (rd[i].rBuf->S[src++]); // start-cell
+                           pixR = ntohs (rd[i].rBuf->S[src++]); // roi
+//here we should check if pixH is correct ....
+
+                           pixS = boardId * 36 + drs * 9 + px;
+                           src++;
+
+
+                           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[srcT],
+                                         roi[0] * 2);
+                                 qncpy += roi[0] * 2;
+                              } else {
+                                 mBuffer[evID].fEvent->StartTM[tmS] = -1;
                               }
                            }
                         }
+                     }          // now we have stored a new board contents into Event structure
+
+                     mBuffer[evID].fEvent->NumBoards++;
+                     mBuffer[evID].board[boardId] = boardId;
+                     evtCtrl.evtStat[iDx]++;
+                     evtCtrl.pcTime[iDx] = g_actTime;
+
+                     if (++mBuffer[evID].nBoard >= actBoards) {
+                        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);
+                                    break;
+                                 }
+                              }
+                           }
+                        }
+                        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);
+
+                        //complete event read ---> flag for next processing
+                        evtCtrl.evtStat[iDx] = 99;
+                        gi.evtTot++;
                      }
-                     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);
-
-                     //complete event read ---> flag for next processing
-                     evtCtrl.evtStat[iDx] = 99;
-                     gi.evtTot++;
+
+                   EndBuf:
+                     rd[i].bufTyp = 0;  //ready to read next header
+                     rd[i].bufLen = frst_len;
+                     rd[i].bufPos = 0;
+                   EndBuf1:
+                     ;
                   }
 
-                EndBuf:
-                  rd[i].bufTyp = 0;     //ready to read next header
-                  rd[i].bufLen = frst_len;
-                  rd[i].bufPos = 0;
-                EndBuf1:
-                  ;
-               }
-
-            } else {            //we are reading event header
-               rd[i].bufPos += jrd;
-               rd[i].bufLen -= jrd;
-               if (rd[i].bufPos >= minLen) {    //sufficient data to take action
-                  //check if startflag correct; else shift block ....
-                  for (k = 0; k < rd[i].bufPos - 1; k++) {
-                     if (rd[i].rBuf->B[k] == start.B[1]
-                         && rd[i].rBuf->B[k + 1] == start.B[0])
-                        break;
-                  }
-                  rd[i].skip += k;
-
-                  if (k >= rd[i].bufPos - 1) {  //no start of header found
-                     rd[i].bufPos = 0;
-                     rd[i].bufLen = head_len;
-                  } else if (k > 0) {
-                     rd[i].bufPos -= k;
-                     rd[i].bufLen += k;
-                     memcpy (&rd[i].rBuf->B[0], &rd[i].rBuf->B[k],
-                             rd[i].bufPos);
+               } else {         //we are reading event header
+                  rd[i].bufPos += jrd;
+                  rd[i].bufLen -= jrd;
+                  if (rd[i].bufPos >= minLen) { //sufficient data to take action
+                     //check if startflag correct; else shift block ....
+                     for (k = 0; k < rd[i].bufPos - 1; k++) {
+                        if (rd[i].rBuf->B[k] == start.B[1]
+                            && rd[i].rBuf->B[k + 1] == start.B[0])
+                           break;
+                     }
+                     rd[i].skip += k;
+
+                     if (k >= rd[i].bufPos - 1) {       //no start of header found
+                        rd[i].bufPos = 0;
+                        rd[i].bufLen = head_len;
+                     } else if (k > 0) {
+                        rd[i].bufPos -= k;
+                        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);
+                        memcpy (&rd[i].xBuf->B[0], &rd[i].xBuf->B[k],
+                                rd[i].bufPos);
 #endif
-                  }
-                  if (rd[i].bufPos >= minLen) {
-                     if (rd[i].skip > 0) {
-                        snprintf (str, MXSTR, "skipped %d bytes on port%d",
-                                  rd[i].skip, i);
-                        factOut (kInfo, 666, str);
-                        rd[i].skip = 0;
                      }
-                     goodhed++;
-                     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].ftmID = ntohl (rd[i].rBuf->I[3]);    //(FTMevt)
-                     rd[i].evtID = ntohl (rd[i].rBuf->I[4]);    //(FADevt)
-                     rd[i].runID = ntohl (rd[i].rBuf->I[11]);
-                     rd[i].bufTyp = 1;  //ready to read full record
-                     rd[i].bufLen = rd[i].fadLen - rd[i].bufPos;
-
-                     int fadboard = ntohs (rd[i].rBuf->S[12]);
-                     int fadcrate = fadboard / 256;
-                     fadboard = (fadcrate * 10 + fadboard % 256);
+                     if (rd[i].bufPos >= minLen) {
+                        if (rd[i].skip > 0) {
+                           snprintf (str, MXSTR, "skipped %d bytes on port%d",
+                                     rd[i].skip, i);
+                           factOut (kInfo, 666, str);
+                           rd[i].skip = 0;
+                        }
+                        goodhed++;
+                        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].ftmID = ntohl (rd[i].rBuf->I[3]); //(FTMevt)
+                        rd[i].evtID = ntohl (rd[i].rBuf->I[4]); //(FADevt)
+                        rd[i].runID = ntohl (rd[i].rBuf->I[11]);
+                        rd[i].bufTyp = 1;       //ready to read full record
+                        rd[i].bufLen = rd[i].fadLen - rd[i].bufPos;
+
+                        int fadboard = ntohs (rd[i].rBuf->S[12]);
+                        int fadcrate = fadboard / 256;
+                        fadboard = (fadcrate * 10 + fadboard % 256);
 #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);
+                        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;
-
-                     if (rd[i].bufLen <= head_len || rd[i].bufLen > MAX_LEN) {
-                        snprintf (str, MXSTR,
-                                  "illegal event-length on port %d", i);
-                        factOut (kFatal, 881, str);
-                        rd[i].bufLen = 100000;  //?
+                        if (rd[i].runID == 0)
+                           rd[i].runID = gi_myRun;
+
+                        if (rd[i].bufLen <= head_len
+                            || rd[i].bufLen > MAX_LEN) {
+                           snprintf (str, MXSTR,
+                                     "illegal event-length on port %d", i);
+                           factOut (kFatal, 881, str);
+                           rd[i].bufLen = 100000;       //?
+                        }
+                        int fadBoard = ntohs (rd[i].rBuf->S[12]);
+                        debugHead (i, fadBoard, rd[i].rBuf);
+                        debugRead (i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, -1, tsec, tusec);     // i=socket; jrd=#bytes; ievt=eventid;-1=start event
+                     } else {
+                        debugRead (i, jrd, 0, 0, 0, -2, tsec, tusec);   // i=socket; jrd=#bytes; ievt=eventid; -2=start event, unknown id yet
                      }
-                     int fadBoard = ntohs (rd[i].rBuf->S[12]);
-                     debugHead (i, fadBoard, rd[i].rBuf);
-                     debugRead (i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, -1, tsec, tusec);        // i=socket; jrd=#bytes; ievt=eventid;-1=start event
                   } else {
                      debugRead (i, jrd, 0, 0, 0, -2, tsec, tusec);      // i=socket; jrd=#bytes; ievt=eventid; -2=start event, unknown id yet
                   }
-               } else {
-                  debugRead (i, jrd, 0, 0, 0, -2, tsec, tusec); // i=socket; jrd=#bytes; ievt=eventid; -2=start event, unknown id yet
-               }
-
-            }                   //end interpreting last read
-}
+
+               }                //end interpreting last read
+            }
          }                      //end of successful read anything
       }                         //finished trying to read all sockets
@@ -1272,9 +1299,29 @@
                    && evtCtrl.pcTime[k0] < g_actTime - 10) {
                   int id = evtCtrl.evtBuf[k0];
+                  int ic,ib,ik,jb;
                   snprintf (str, MXSTR, "%5d skip short evt %8d %8d %2d",
                             mBuffer[id].evNum, evtCtrl.evtBuf[k0], k0,
                             evtCtrl.evtStat[k0]);
                   factOut (kWarn, 601, str);
-                  evtCtrl.evtStat[k0] = 91;     //timeout for incomplete events
+
+                  ik=0;
+                  for (ib=0; ib<NBOARDS; ib++) {
+                     if (ib%10==0) {
+                        snprintf (&str[ik], MXSTR, "'");
+                        ik++;
+                     }
+                     jb = mBuffer[id].board[ib];
+                     if ( jb<0 ) {
+                        snprintf (&str[ik], MXSTR, ".");
+                        ik++;
+                     } else {
+                        snprintf (&str[ik], MXSTR, "%d",jb%10);
+                        ik+=1;
+                     }
+                  }
+                  snprintf (&str[ik], MXSTR, "'");
+                  factOut (kWarn, 601, str);
+
+                  evtCtrl.evtStat[k0] = 93;     //timeout for incomplete events
                   gi.evtSkp++;
                   gi.evtTot++;
@@ -1371,6 +1418,5 @@
             GenSock (-1, i, 0, NULL, &rd[i]);   //close and destroy open socket   
             if (i % 7 == 0) {
-//             gi_NumConnect[i / 7]--;
-               gi_NumConnect[i / 7]-= cntsock ;
+               gi_NumConnect[i / 7] -= cntsock;
                gi.numConn[i / 7]--;
                gj.numConn[i / 7]--;
@@ -1476,4 +1522,5 @@
 subProc (void *thrid)
 {
+   char str[MXSTR];
    int threadID, status, numWait, numProc, kd, k1, k0, k, jret;
    struct timespec xwait;
@@ -1501,5 +1548,6 @@
                jret =
                   subProcEvt (threadID, mBuffer[id].FADhead,
-                              mBuffer[id].fEvent, mBuffer[id].buffer);
+                              mBuffer[id].fEvent, mBuffer[id].mBoard, gi_runPtr );
+
                if (jret <= threadID) {
                   snprintf (str, MXSTR,
@@ -1544,5 +1592,5 @@
 {
 /* *** main loop processing file, including SW-trigger */
-   int numProc, numWait;
+   int numProc, numWait, iskip;
    int k, status, j;
    struct timespec xwait;
@@ -1561,19 +1609,19 @@
 
 /* CPU_ZERO initializes all the bits in the mask to zero. */
-   CPU_ZERO (&mask);
+//   CPU_ZERO (&mask);
 /* CPU_SET sets only the bit corresponding to cpu. */
 // CPU_SET(  0 , &mask );  leave for system
 // CPU_SET(  1 , &mask );  used by write process
-   CPU_SET (2, &mask);
-   CPU_SET (3, &mask);
-   CPU_SET (4, &mask);
-   CPU_SET (5, &mask);
-   CPU_SET (6, &mask);
+//   CPU_SET (2, &mask);
+//   CPU_SET (3, &mask);
+//   CPU_SET (4, &mask);
+//   CPU_SET (5, &mask);
+//   CPU_SET (6, &mask);
 // CPU_SET(  7 , &mask );  used by read process
 /* sched_setaffinity returns 0 in success */
-   if (sched_setaffinity (0, sizeof (mask), &mask) == -1) {
-      snprintf (str, MXSTR, "P ---> can not create affinity to %d", cpu);
-      factOut (kWarn, -1, str);
-   }
+//   if (sched_setaffinity (0, sizeof (mask), &mask) == -1) {
+//      snprintf (str, MXSTR, "P ---> can not create affinity to %d", cpu);
+//      factOut (kWarn, -1, str);
+//   }
 
 
@@ -1602,98 +1650,122 @@
             } else {
 
-//-------- it is better to open the run already here, so call can be used to initialize
-//-------- buffers etc. needed to interprete run (e.g. DRS calibration)
                int id = evtCtrl.evtBuf[k0];
                uint32_t irun = mBuffer[id].runNum;
                int32_t ievt = mBuffer[id].evNum;
-               if (runCtrl[lastRun].runId == irun) {
-                  j = lastRun;
-               } else {
-                  //check which fileID to use (or open if needed)
-                  for (j = 0; j < MAX_RUN; j++) {
-                     if (runCtrl[j].runId == irun)
-                        break;
+               iskip = 0 ;
+               if (irun != gi_actRun) { // we should open a new run ?
+                  if (irun < gi_actRun) { //  very old event; skip it
+                     iskip = -1 ;         //  but anyhow checkEvent() will be done
+                     snprintf (str, MXSTR,
+                               "P event %d from old run???", ievt,irun);
+                     factOut (kWarn, 901, str);
+                  } else if (gi_actRun != 0) { //    but there is still old one open ?
+
+//  // // // // // //MUST WAIT UNTIL PENDING OLD EVENTS PROCESSED ...
+//  loop over all waiting events; and decide what to do ....
+                     int q,q0 ;
+                     int qcnt=0; 
+                     for (q=k1; q < (k1 + kd); q++) {
+                        q0 = q % (MAX_EVT * MAX_RUN);
+                        if (evtCtrl.evtStat[q0] > 0 
+                         && evtCtrl.evtStat[q0] < 9000) {
+                           //this event is still active in the queue
+                           int qd = evtCtrl.evtBuf[q0];
+                           uint32_t qrun = mBuffer[qd].runNum;
+                           if (qrun < irun ) {
+                              if (evtCtrl.evtStat[q0] < 92) {
+                                 //incomplete old event -> reduce timeout 
+                                 evtCtrl.pcTime[q0] = 0 ;
+                              }
+                              qcnt++ ;
+                           }
+                        }
+                     }
+                     if (qcnt == 0 ) {
+                        runEnd (gi_actRun, gi_runPtr );
+                        snprintf (str, MXSTR,
+                                  "P run %d ended", gi_actRun);
+                        factOut (kWarn, 901, str);
+                        gi_actRun = 0;  //there are no events from old runs left
+                     }
                   }
-                  if (j >= MAX_RUN) {
-                     snprintf (str, MXSTR,
-                               "P error: can not find run %d for event %d in %d",
-                               irun, ievt, id);
-                     factOut (kFatal, 901, str);
-                  }
-                  lastRun = j;
-               }
-
-               if (runCtrl[j].fileId < 0) {
-//----            we need to open a new run ==> make sure all older runs are
-//----            finished and marked to be closed ....
-                  int j1;
-                  for (j1 = 0; j1 < MAX_RUN; j1++) {
-                     if (runCtrl[j1].fileId == 0) {
-                        runCtrl[j1].procId = 2; //--> do no longer accept events for processing
-//----                  problem: processing still going on ==> must wait for closing ....
+
+
+                  if (gi_actRun == 0) { //    now we can try to open new run
+                     for (j = 0; j < MAX_RUN; j++) {
+                        if (runCtrl[j].runId == irun)
+                           break;
+                     }
+                     if (j >= MAX_RUN) {
                         snprintf (str, MXSTR,
-                                  "P finish run since new one opened %d",
-                                  runCtrl[j1].runId);
-                        runFinish1 (runCtrl[j1].runId);
+                                  "P error: can not find run %d for event %d in %d",
+                                  irun, ievt, id);
+                        factOut (kFatal, 901, str);
+                        j = 0;
                      }
 
-                  }
-
-                  actRun.Version = 1;
-                  actRun.RunType = -1;  //to be adapted
-
-                  actRun.Nroi = runCtrl[j].roi0;
-                  actRun.NroiTM = runCtrl[j].roi8;
-                  if (actRun.Nroi == actRun.NroiTM)
-                     actRun.NroiTM = 0;
-                  actRun.RunTime = runCtrl[j].firstTime;
-                  actRun.RunUsec = runCtrl[j].firstTime;
-                  actRun.NBoard = NBOARDS;
-                  actRun.NPix = NPIX;
-                  actRun.NTm = NTMARK;
-                  actRun.Nroi = mBuffer[id].nRoi;
-                  memcpy (actRun.FADhead, mBuffer[id].FADhead,
-                          NBOARDS * sizeof (PEVNT_HEADER));
-
-                  runCtrl[j].fileHd =
-                     runOpen (irun, &actRun, sizeof (actRun));
-                  if (runCtrl[j].fileHd == NULL) {
-                     snprintf (str, MXSTR,
-                               "P could not open a file for run %d", irun);
-                     factOut (kError, 502, str);
-                     runCtrl[j].fileId = 91;
-                     runCtrl[j].procId = 91;
-                  } else {                                                 
-                     snprintf (str, MXSTR, "P opened new run_file %d evt %d",
-                               irun, ievt);
-                     factOut (kInfo, -1, str);
-                     runCtrl[j].fileId = 0;
-                     runCtrl[j].procId = 0;
-                  }
-
-               }
-//-------- also check if run shall be closed (==> skip event, but do not close the file !!! )  
-               if (runCtrl[j].procId == 0) {
-                  if (runCtrl[j].closeTime < g_actTime
-                      || runCtrl[j].lastTime < g_actTime - 300
-                      || runCtrl[j].maxEvt <= runCtrl[j].procEvt) {
-                     snprintf (str, MXSTR,
-                               "P reached end of run condition for run %d",
-                               irun);
-                     factOut (kInfo, 502, str);
-                     runFinish1 (runCtrl[j].runId);
-                     runCtrl[j].procId = 1;
+                     if (runCtrl[j].fileId == 0) {
+                        snprintf (str, MXSTR,
+                                  "P run %d is already open ???", irun);
+                        factOut (kWarn, 901, str);
+                     } else if (runCtrl[j].fileId > 0) {
+                        snprintf (str, MXSTR,
+                                  "P run %d is already closed ???", irun);
+                        factOut (kWarn, 901, str);
+                     } else {   // now open new run
+
+                        actRun.Version = 1;
+                        actRun.RunType = -1;    //to be adapted
+
+                        actRun.Nroi = runCtrl[j].roi0;
+                        actRun.NroiTM = runCtrl[j].roi8;
+                        if (actRun.Nroi == actRun.NroiTM)
+                           actRun.NroiTM = 0;
+                        actRun.RunTime = runCtrl[j].firstTime;
+                        actRun.RunUsec = runCtrl[j].firstTime;
+                        actRun.NBoard = NBOARDS;
+                        actRun.NPix = NPIX;
+                        actRun.NTm = NTMARK;
+                        actRun.Nroi = mBuffer[id].nRoi;
+                        memcpy (actRun.FADhead, mBuffer[id].FADhead,
+                                NBOARDS * sizeof (PEVNT_HEADER));
+
+                        gi_actRun = irun;
+                        gi_runPtr =
+                           runStart (irun, &actRun, sizeof (actRun));
+                        if (gi_runPtr == NULL) {
+                           snprintf (str, MXSTR,
+                                     "P could not start run %d", irun);
+                           factOut (kFatal, 502, str);
+                        } else {
+                           snprintf (str, MXSTR, "P started new run %d",
+                                     irun);
+                           factOut (kInfo, -1, str);
+                        }
+
+                        runCtrl[j].fileHd =
+                           runOpen (irun, &actRun, sizeof (actRun));
+                        if (runCtrl[j].fileHd == NULL) {
+                           snprintf (str, MXSTR,
+                                     "P could not open a file for run %d",
+                                     irun);
+                           factOut (kError, 502, str);
+                           runCtrl[j].fileId = 91;
+                           runCtrl[j].procId = 91;
+                        } else {
+                           snprintf (str, MXSTR,
+                                     "P opened new run_file %d evt %d", irun,
+                                     ievt);
+                           factOut (kInfo, -1, str);
+                           runCtrl[j].fileId = 0;
+                           runCtrl[j].procId = 0;
+                        }
+                     }
                   }
                }
-               if (runCtrl[j].procId != 0) {
-                  snprintf (str, MXSTR,
-                            "P skip event %d because no active run %d", ievt,
-                            irun);
-                  factOut (kDebug, 502, str);
-                  evtCtrl.evtStat[k0] = 9091;
-               } else {
-//--------
-//--------
-                      id = evtCtrl.evtBuf[k0];
+
+               if (irun == gi_actRun) { 
+//                now we are sure the run is ready and we can start processing the event
+                  id = evtCtrl.evtBuf[k0];
                   int itevt = mBuffer[id].trgNum;
                   int itrg = mBuffer[id].trgTyp;
@@ -1701,5 +1773,5 @@
                   int roiTM = mBuffer[id].nRoiTM;
 
-//make sure unused pixels/tmarks are cleared to zero
+                  //make sure unused pixels/tmarks are cleared to zero
                   if (roiTM == roi)
                      roiTM = 0;
@@ -1719,5 +1791,5 @@
 
 
-//and set correct event header ; also check for consistency in event (not yet)
+                  //and set correct event header 
                   mBuffer[id].fEvent->Roi = roi;
                   mBuffer[id].fEvent->RoiTM = roiTM;
@@ -1731,7 +1803,7 @@
                   mBuffer[id].fEvent->SoftTrig = 0;
 
-
+                  int mboard = -1;
                   for (ib = 0; ib < NBOARDS; ib++) {
-                     if (mBuffer[id].board[ib] == -1) { //board is not read
+                     if (mBuffer[id].board[ib] == -1) {    //board is not read
                         mBuffer[id].FADhead[ib].start_package_flag = 0;
                         mBuffer[id].fEvent->BoardTime[ib] = 0;
@@ -1739,18 +1811,24 @@
                         mBuffer[id].fEvent->BoardTime[ib] =
                            ntohl (mBuffer[id].FADhead[ib].time);
+                        if (mboard < 0)
+                           mboard = ib;
                      }
                   }
-
+                  mBuffer[id].mBoard = mboard; 
                   int i = eventCheck (mBuffer[id].runNum, mBuffer[id].FADhead,
-                                      mBuffer[id].fEvent);
+                                      mBuffer[id].fEvent, mboard);
                   gi.procTot++;
                   numProc++;
 
-                  if (i < 0) {
-                     evtCtrl.evtStat[k0] = 9999;        //flag event to be skipped
+                  if (i < 0 || iskip < 0 ) {
+                     evtCtrl.evtStat[k0] = 9999;   //flag event to be skipped
                      gi.procErr++;
                   } else {
-                     evtCtrl.evtStat[k0] = 1000;
+                     evtCtrl.evtStat[k0] = 1000;   //flag event to be processed
                      runCtrl[j].procEvt++;
+                     if (runCtrl[j].procId != 0)
+                        mBuffer[id].toWrite = -1;  //flag event to be processed but now written     
+                     else
+                        mBuffer[id].toWrite = 0;
                   }
                }
@@ -1863,11 +1941,11 @@
 
 /* CPU_ZERO initializes all the bits in the mask to zero. */
-   CPU_ZERO (&mask);
+//   CPU_ZERO (&mask);
 /* CPU_SET sets only the bit corresponding to cpu. */
-   CPU_SET (cpu, &mask);
+//   CPU_SET (cpu, &mask);
 /* sched_setaffinity returns 0 in success */
-   if (sched_setaffinity (0, sizeof (mask), &mask) == -1) {
-      snprintf (str, MXSTR, "W ---> can not create affinity to %d", cpu);
-   }
+//   if (sched_setaffinity (0, sizeof (mask), &mask) == -1) {
+//      snprintf (str, MXSTR, "W ---> can not create affinity to %d", cpu);
+//   }
 
    int lastRun = 0;             //usually run from last event still valid
@@ -1941,5 +2019,5 @@
                      snprintf (str, MXSTR, "W opened new run_file %d evt %d",
                                irun, ievt);
-                     factOut (kInfo, -1, str);
+                     factOut (kWarn, -1, str);
                      runCtrl[j].fileId = 0;
                   }
@@ -1947,5 +2025,5 @@
                }
 
-               if (runCtrl[j].fileId != 0) {
+               if (runCtrl[j].fileId != 0 || mBuffer[id].toWrite < 0) {
                   if (runCtrl[j].fileId < 0) {
                      snprintf (str, MXSTR,
@@ -1975,5 +2053,4 @@
                                ievt, irun, k0);
                      factOut (kDebug, 504, str);
-//               gj.writEvt++ ;
                   } else {
                      snprintf (str, MXSTR, "W error writing event for run %d",
@@ -2161,7 +2238,7 @@
 
 //prepare for subProcesses
-   gi_maxSize = g_maxSize;
-   if (gi_maxSize <= 0)
-      gi_maxSize = 1;
+//   gi_maxSize = g_maxSize;
+//   if (gi_maxSize < 0)
+      gi_maxSize = 0;
 
    gi_maxProc = g_maxProc;
@@ -2242,5 +2319,5 @@
 int
 subProcEvt (int threadID, PEVNT_HEADER * fadhd, EVENT * event,
-            int8_t * buffer)
+            int16_t mboard, void * buffer)
 {
    printf ("called subproc %d\n", threadID);
@@ -2287,5 +2364,5 @@
 
 int
-eventCheck (uint32_t runNr, PEVNT_HEADER * fadhd, EVENT * event)
+eventCheck (uint32_t runNr, PEVNT_HEADER * fadhd, EVENT * event, int mboard)
 {
    int i = 0;
@@ -2377,5 +2454,5 @@
 
    g_maxProc = 20;
-   g_maxSize = 30000;
+   g_maxSize = 0;
 
    g_runStat = 40;
Index: trunk/FACT++/src/EventBuilderWrapper.h
===================================================================
--- trunk/FACT++/src/EventBuilderWrapper.h	(revision 12090)
+++ trunk/FACT++/src/EventBuilderWrapper.h	(revision 12091)
@@ -735,5 +735,5 @@
     array<uint16_t,2> fVecRoi;
 
-    int eventCheck(uint32_t runNr, PEVNT_HEADER *fadhd, EVENT *event)
+    int eventCheck(uint32_t runNr, PEVNT_HEADER *fadhd, EVENT *event, int iboard)
     {
         /*
@@ -908,5 +908,5 @@
     }
 
-    int subProcEvt(int threadID, PEVNT_HEADER *fadhd, EVENT *event, int8_t */*buffer*/)
+    int subProcEvt(int threadID, PEVNT_HEADER *fadhd, EVENT *event, int16_t iboard, void */*buffer*/)
     {
         switch (threadID)
@@ -1292,4 +1292,35 @@
     }
 
+    // -----
+
+    void *runStart(uint32_t irun, RUN_HEAD *runhd, size_t len)
+    {
+        return NULL;
+    }
+
+    int subProcEvt(int threadID, PEVNT_HEADER *fadhd, EVENT *event, int16_t mboard, void *runPtr)
+    {
+        return EventBuilderWrapper::This->subProcEvt(threadID, fadhd, event, mboard, runPtr);
+    }
+
+    int runEnd(uint32_t, void *runPtr)
+    {
+        return 0;
+    }
+
+    // -----
+
+    int eventCheck(uint32_t runNr, PEVNT_HEADER *fadhd, EVENT *event, int mboard)
+    {
+        return EventBuilderWrapper::This->eventCheck(runNr, fadhd, event, mboard);
+    }
+
+    void gotNewRun(int runnr, PEVNT_HEADER *headers)
+    {
+        return EventBuilderWrapper::This->gotNewRun(runnr, headers);
+    }
+
+    // -----
+
     void factOut(int severity, int err, const char *message)
     {
@@ -1307,4 +1338,6 @@
     }
 
+    // ------
+
     void debugHead(int socket, int/*board*/, void *buf)
     {
@@ -1323,20 +1356,4 @@
         EventBuilderWrapper::This->debugRead(isock, ibyte, event, ftmevt, runno, state, tsec, tusec);
     }
-
-    int eventCheck(uint32_t runNr, PEVNT_HEADER *fadhd, EVENT *event)
-    {
-        return EventBuilderWrapper::This->eventCheck(runNr, fadhd, event);
-    }
-
-    void gotNewRun(int runnr, PEVNT_HEADER *headers)
-    {
-        return EventBuilderWrapper::This->gotNewRun(runnr, headers);
-    }
-
-    int subProcEvt(int threadID, PEVNT_HEADER *fadhd, EVENT *event, int8_t *buffer)
-    {
-        return EventBuilderWrapper::This->subProcEvt(threadID, fadhd, event, buffer);
-    }
-
 }
 
Index: trunk/FACT++/src/FAD.h
===================================================================
--- trunk/FACT++/src/FAD.h	(revision 12090)
+++ trunk/FACT++/src/FAD.h	(revision 12091)
@@ -222,10 +222,11 @@
   int16_t  nRoi   ;
   int16_t  nRoiTM ;
+  int16_t  mBoard ;
   uint32_t pcTime ;
   int32_t  evtLen ;
+  int16_t  toWrite ;
   uint8_t  Errors[4] ;
   EVENT   *fEvent ;
   PEVNT_HEADER *FADhead; //
-  int8_t  *buffer ;
 
 } WRK_DATA ;             //internal to eventbuilder
