Index: trunk/FACT++/src/EventBuilder.c
===================================================================
--- trunk/FACT++/src/EventBuilder.c	(revision 15459)
+++ trunk/FACT++/src/EventBuilder.c	(revision 15460)
@@ -176,6 +176,6 @@
     TGB_entry *last = tgb_last;
 
-    TGB_entry *mem = last->mem;
-    tgb_last       = last->prev;
+    void *mem = last->mem;
+    tgb_last  = last->prev;
 
     free(last);
@@ -187,5 +187,5 @@
 {
     // Add the last free slot to the stack
-    TGB_entry *entry = malloc(sizeof(TGB_entry));
+    TGB_entry *entry = (TGB_entry*)malloc(sizeof(TGB_entry));
 
     entry->prev = tgb_last;
@@ -321,8 +321,5 @@
 
 
-RUN_HEAD actRun;
-
 RUN_CTRL runCtrl[MAX_RUN];
-
 RUN_TAIL runTail[MAX_RUN];
 
@@ -406,5 +403,5 @@
 int
 GenSock (int flag, int sid, int port, struct sockaddr_in *sockAddr,
-         READ_STRUCT * rd)
+         READ_STRUCT * rs)
 {
 /*
@@ -425,11 +422,11 @@
    if (sid % 7 >= NUMSOCK) {
       //this is a not used socket, so do nothing ...
-      rd->sockStat = 77;
-      rd->rBuf = NULL ;
+      rs->sockStat = 77;
+      rs->rBuf = NULL ;
       return 0;
    }
 
-   if (rd->sockStat == 0) {     //close socket if open
-      j = close (rd->socket);
+   if (rs->sockStat == 0) {     //close socket if open
+      j = close (rs->socket);
       if (j > 0) {
           factPrintf(kFatal, 771, "Closing socket %d failed: %m (close,rc=%d)", sid, errno);
@@ -439,9 +436,9 @@
    }
 
-   rd->sockStat = 99;
+   rs->sockStat = 99;
 
    if (flag < 0) {
-      free (rd->rBuf);          //and never open again
-      rd->rBuf = NULL;
+      free (rs->rBuf);          //and never open again
+      rs->rBuf = NULL;
       return 0;
    }
@@ -449,13 +446,13 @@
 
    if (flag == 0) {             //generate address and buffer ...
-      rd->Port = port;
-      rd->SockAddr.sin_family = sockAddr->sin_family;
-      rd->SockAddr.sin_port = htons (port);
-      rd->SockAddr.sin_addr = sockAddr->sin_addr;
-
-      rd->rBuf = malloc (sizeof (CNV_FACT));
-      if (rd->rBuf == NULL) {
+      rs->Port = port;
+      rs->SockAddr.sin_family = sockAddr->sin_family;
+      rs->SockAddr.sin_port = htons (port);
+      rs->SockAddr.sin_addr = sockAddr->sin_addr;
+
+      rs->rBuf = (CNV_FACT*)malloc (sizeof (CNV_FACT));
+      if (rs->rBuf == NULL) {
          factPrintf(kFatal, 774, "Could not create local buffer %d (malloc failed)", sid);
-         rd->sockStat = 77;
+         rs->sockStat = 77;
          return -3;
       }
@@ -463,5 +460,5 @@
 
 
-   if ((rd->socket = socket (PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) <= 0) {
+   if ((rs->socket = socket (PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) <= 0) {
        factPrintf(kFatal, 773, "Generating socket %d failed: %m (socket,rc=%d)", sid, errno);
       rd->sockStat = 88;
@@ -469,17 +466,17 @@
    }
    optval = 1;
-   if (setsockopt (rd->socket, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
+   if (setsockopt (rs->socket, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
        factPrintf(kInfo, 173, "Setting SO_KEEPALIVE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
    }
    optval = 10;                 //start after 10 seconds
-   if (setsockopt (rd->socket, SOL_TCP, TCP_KEEPIDLE, &optval, optlen) < 0) {
+   if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPIDLE, &optval, optlen) < 0) {
        factPrintf(kInfo, 173, "Setting TCP_KEEPIDLE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
    }
    optval = 10;                 //do every 10 seconds
-   if (setsockopt (rd->socket, SOL_TCP, TCP_KEEPINTVL, &optval, optlen) < 0) {
+   if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPINTVL, &optval, optlen) < 0) {
       factPrintf(kInfo, 173, "Setting TCP_KEEPINTVL for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
    }
    optval = 2;                  //close after 2 unsuccessful tries
-   if (setsockopt (rd->socket, SOL_TCP, TCP_KEEPCNT, &optval, optlen) < 0) {
+   if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPCNT, &optval, optlen) < 0) {
       factPrintf(kInfo, 173, "Setting TCP_KEEPCNT for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
    }
@@ -487,6 +484,6 @@
    factPrintf(kInfo, 773, "Successfully generated socket %d", sid);
 
-   rd->sockStat = -1;           //try to (re)open socket
-   rd->errCnt = 0;
+   rs->sockStat = -1;           //try to (re)open socket
+   rs->errCnt = 0;
    return 0;
 
@@ -526,5 +523,5 @@
 #endif
 
-   actRun.FADhead = malloc (NBOARDS * sizeof (PEVNT_HEADER));
+   //actRun.FADhead = malloc (NBOARDS * sizeof (PEVNT_HEADER));
 
    evtCtrl.frstPtr = 0;
@@ -789,5 +786,5 @@
 void initEvent(int i)
 {
-    mBuffer[i].fEvent = (void*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM);
+    mBuffer[i].fEvent = (EVENT*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM);
 
     //flag all pixels as unused
@@ -1399,171 +1396,7 @@
           }
 
-          // are we reading data?
-          if (rd[i].bufTyp > 0)
+          //we are reading event header
+          if (rd[i].bufTyp <= 0)
           {
-              // not yet all read
-              if (rd[i].bufLen > 0)
-                  continue;
-
-              if (rd[i].rBuf->B[rd[i].fadLen - 1] != stop.B[0] ||
-                  rd[i].rBuf->B[rd[i].fadLen - 2] != stop.B[1])
-              {
-                  //gi.evtErr++;
-                  factPrintf(kError, 301, "End-of-event flag wrong on socket %3d for event %4d (len=%5d), expected %3d %3d, got %3d %3d",
-                             i, rd[i].evtID, rd[i].fadLen, stop.B[0], stop.B[1],
-                             rd[i].rBuf->B[rd[i].fadLen - 1], rd[i].rBuf->B[rd[i].fadLen - 2]);
-
-                  // ready to read next header
-                  rd[i].bufTyp = 0;
-                  rd[i].bufLen = sizeof(PEVNT_HEADER);
-                  rd[i].bufPos = 0;
-
-                  continue;
-              }
-
-              //  int actid;
-              //  if (g_useFTM > 0)
-              //     actid = rd[i].evtID;
-              //  else
-              //     actid = rd[i].ftmID;
-
-              //get index into mBuffer for this event (create if needed)
-              const int evID = mBufEvt(i);
-
-              // no free entry in mBuffer, retry later
-              if (evID == -1)
-                  continue;
-
-              // We have a valid entry, but no memory has yet been allocated
-              if (evID >= 0 && mBuffer[evID].FADhead == NULL)
-              {
-                  // Try to get memory from the big buffer
-                  mBuffer[evID].FADhead = TGB_Malloc();
-                  if (mBuffer[evID].FADhead == NULL)
-                  {
-                      // If this works properly, this is a hack which can be removed, or
-                      // replaced by a signal or dim message
-                      if (rd[i].bufTyp==2)
-                          factPrintf(kError, 882, "malloc failed for event %d", evID);
-                      rd[i].bufTyp = 2;
-                      continue;
-                  }
-
-                  // Initialice mBuffer[evID]->fEvent
-                  initEvent(evID);
-
-                  // Some statistics
-                  gj.usdMem = tgb_inuse;
-
-                  if (gj.usdMem > gj.maxMem)
-                      gj.maxMem = gj.usdMem;
-
-                  gj.rateNew++;
-                  gj.bufTot++;
-                  if (gj.bufTot > gj.maxEvt)
-                      gj.maxEvt = gj.bufTot;
-
-                  //register event in 'active list (reading)'
-                  evtIdx[evID] = evtCtrl.lastPtr;
-
-                  evtCtrl.evtBuf[evtCtrl.lastPtr]  = evID;
-                  evtCtrl.evtStat[evtCtrl.lastPtr] = 0;
-                  evtCtrl.pcTime[evtCtrl.lastPtr]  = g_actTime;
-
-                  evtCtrl.lastPtr++;
-                  evtCtrl.lastPtr %= MAX_EVT * MAX_RUN;
-              }
-
-              // ready to read next header
-              rd[i].bufTyp = 0;
-              rd[i].bufLen = sizeof(PEVNT_HEADER);
-              rd[i].bufPos = 0;
-
-              // Fatal error occured. Event cannot be processed. Skip it. Start reading next header.
-              if (evID < -1000)
-                  continue;
-
-              //we have a valid entry in mBuffer[]; fill it
-              const int fadBoard = rd[i].rBuf->S[12];
-              const int fadCrate = fadBoard>>8;
-
-              if (board != (fadCrate * 10 + (fadBoard&0xff)))
-              {
-                  factPrintf(kWarn, 301, "Board ID mismatch. Expected %d, got %d (C=%d, B=%d)",
-                             board, fadBoard, fadCrate, fadBoard&0xff);
-              }
-
-              if (mBuffer[evID].board[board] != -1)
-              {
-                  factPrintf(kWarn, 501, "Got event %5d from board %3d (i=%3d, len=%5d) twice: Starts with %3d %3d - ends with %3d %3d",
-                             evID, board, i, rd[i].fadLen,
-                             rd[i].rBuf->B[0], rd[i].rBuf->B[1],
-                             rd[i].rBuf->B[rd[i].fadLen - 2],
-                             rd[i].rBuf->B[rd[i].fadLen - 1]);
-                  continue; // Continue reading next header
-              }
-
-              // Copy data from rd[i] to mBuffer[evID]
-              copyData(i, evID, board);
-
-              // now we have stored a new board contents into Event structure
-
-              const int iDx = evtIdx[evID];       //index into evtCtrl
-
-              //evtCtrl.evtStat[iDx]++;
-              evtCtrl.pcTime[iDx] = g_actTime;
-
-              mBuffer[evID].fEvent->NumBoards++;
-              mBuffer[evID].board[board] = board;
-              mBuffer[evID].nBoard++;
-
-              // have we already reported first (partial) event of this run ???
-              if (mBuffer[evID].nBoard==1 && mBuffer[evID].runNum != actrun)
-              {
-                  // Signal the fadctrl that a new run has been started
-                  gotNewRun(mBuffer[evID].runNum, NULL);
-
-                  factPrintf(kInfo, 1, "gotNewRun called, prev run %d, new run %d, event %d",
-                             actrun, mBuffer[evID].runNum, mBuffer[evID].evNum);
-
-                  for (int j=0; j<MAX_RUN; j++)
-                  {
-                      // Since we have started a new run, we know already when to close the
-                      // previous run in terms of number of events
-                      if (runCtrl[j].runId==actrun)
-                          runCtrl[j].maxEvt = runCtrl[j].lastEvt;
-
-                      // We got the first part of this event, so this is
-                      // the number of events we expect for this run
-                      if (runCtrl[j].runId==mBuffer[evID].runNum)
-                          runCtrl[j].lastEvt++;
-                  }
-
-                  // Change 'actrun' the the new runnumber
-                  actrun = mBuffer[evID].runNum;
-              }
-
-              //complete event read ---> flag for next processing
-              if (mBuffer[evID].nBoard >= actBoards)
-              {
-                  // This is a non-ideal hack to lower the probability that
-                  // in mBufEvt the search for correct entry in runCtrl
-                  // will not return a super-old entry
-                  for (int ir=0; ir<MAX_RUN; ir++)
-                  {
-                      if (runCtrl[ir].runId != actrun && runCtrl[ir].fileId>0)
-                          runCtrl[ir].runId = 0;
-                  }
-
-                  // Flag that the event is ready for processing
-                  evtCtrl.evtStat[iDx] = 99;
-              }
-
-              // Continue reading next header
-          }
-          else
-          {
-              //we are reading event header
-
               //not yet sufficient data to take action
               if (rd[i].bufPos < minLen)
@@ -1646,7 +1479,166 @@
               debugHead(i, fadBoard, rd[i].rBuf);
 
-              // Continue reading data
-
-          } // end if data or header
+              continue;
+          }
+
+          // are we reading data
+
+          // not yet all read
+          if (rd[i].bufLen > 0)
+              continue;
+
+          if (rd[i].rBuf->B[rd[i].fadLen - 1] != stop.B[0] ||
+              rd[i].rBuf->B[rd[i].fadLen - 2] != stop.B[1])
+          {
+              //gi.evtErr++;
+              factPrintf(kError, 301, "End-of-event flag wrong on socket %3d for event %4d (len=%5d), expected %3d %3d, got %3d %3d",
+                         i, rd[i].evtID, rd[i].fadLen, stop.B[0], stop.B[1],
+                         rd[i].rBuf->B[rd[i].fadLen - 1], rd[i].rBuf->B[rd[i].fadLen - 2]);
+
+              // ready to read next header
+              rd[i].bufTyp = 0;
+              rd[i].bufLen = sizeof(PEVNT_HEADER);
+              rd[i].bufPos = 0;
+
+              continue;
+          }
+
+          //  int actid;
+          //  if (g_useFTM > 0)
+          //     actid = rd[i].evtID;
+          //  else
+          //     actid = rd[i].ftmID;
+
+          //get index into mBuffer for this event (create if needed)
+          const int evID = mBufEvt(i);
+
+          // no free entry in mBuffer, retry later
+          if (evID == -1)
+              continue;
+
+          // We have a valid entry, but no memory has yet been allocated
+          if (evID >= 0 && mBuffer[evID].FADhead == NULL)
+          {
+              // Try to get memory from the big buffer
+              mBuffer[evID].FADhead = (PEVT_HEADER*)TGB_Malloc();
+              if (mBuffer[evID].FADhead == NULL)
+              {
+                  // If this works properly, this is a hack which can be removed, or
+                  // replaced by a signal or dim message
+                  if (rd[i].bufTyp==2)
+                      factPrintf(kError, 882, "malloc failed for event %d", evID);
+                  rd[i].bufTyp = 2;
+                  continue;
+              }
+
+              // Initialice mBuffer[evID]->fEvent
+              initEvent(evID);
+
+              // Some statistics
+              gj.usdMem = tgb_inuse;
+
+              if (gj.usdMem > gj.maxMem)
+                  gj.maxMem = gj.usdMem;
+
+              gj.rateNew++;
+              gj.bufTot++;
+              if (gj.bufTot > gj.maxEvt)
+                  gj.maxEvt = gj.bufTot;
+
+              //register event in 'active list (reading)'
+              evtIdx[evID] = evtCtrl.lastPtr;
+
+              evtCtrl.evtBuf[evtCtrl.lastPtr]  = evID;
+              evtCtrl.evtStat[evtCtrl.lastPtr] = 0;
+              evtCtrl.pcTime[evtCtrl.lastPtr]  = g_actTime;
+
+              evtCtrl.lastPtr++;
+              evtCtrl.lastPtr %= MAX_EVT * MAX_RUN;
+          }
+
+          // ready to read next header
+          rd[i].bufTyp = 0;
+          rd[i].bufLen = sizeof(PEVNT_HEADER);
+          rd[i].bufPos = 0;
+
+          // Fatal error occured. Event cannot be processed. Skip it. Start reading next header.
+          if (evID < -1000)
+              continue;
+
+          //we have a valid entry in mBuffer[]; fill it
+          const int fadBoard = rd[i].rBuf->S[12];
+          const int fadCrate = fadBoard>>8;
+
+          if (board != (fadCrate * 10 + (fadBoard&0xff)))
+          {
+              factPrintf(kWarn, 301, "Board ID mismatch. Expected %d, got %d (C=%d, B=%d)",
+                         board, fadBoard, fadCrate, fadBoard&0xff);
+          }
+
+          if (mBuffer[evID].board[board] != -1)
+          {
+              factPrintf(kWarn, 501, "Got event %5d from board %3d (i=%3d, len=%5d) twice: Starts with %3d %3d - ends with %3d %3d",
+                         evID, board, i, rd[i].fadLen,
+                         rd[i].rBuf->B[0], rd[i].rBuf->B[1],
+                         rd[i].rBuf->B[rd[i].fadLen - 2],
+                         rd[i].rBuf->B[rd[i].fadLen - 1]);
+              continue; // Continue reading next header
+          }
+
+          // Copy data from rd[i] to mBuffer[evID]
+          copyData(i, evID, board);
+
+          // now we have stored a new board contents into Event structure
+
+          const int iDx = evtIdx[evID];       //index into evtCtrl
+
+          mBuffer[evID].fEvent->NumBoards++;
+          mBuffer[evID].board[board] = board;
+          mBuffer[evID].nBoard++;
+
+          evtCtrl.evtStat[iDx] = mBuffer[evID].nBoard;
+          evtCtrl.pcTime[iDx]  = g_actTime;
+
+          // have we already reported first (partial) event of this run ???
+          if (mBuffer[evID].nBoard==1 && mBuffer[evID].runNum != actrun)
+          {
+              // Signal the fadctrl that a new run has been started
+              gotNewRun(mBuffer[evID].runNum, NULL);
+
+              factPrintf(kInfo, 1, "gotNewRun called, prev run %d, new run %d, event %d",
+                         actrun, mBuffer[evID].runNum, mBuffer[evID].evNum);
+
+              for (int j=0; j<MAX_RUN; j++)
+              {
+                  // Since we have started a new run, we know already when to close the
+                  // previous run in terms of number of events
+                  if (runCtrl[j].runId==actrun)
+                      runCtrl[j].maxEvt = runCtrl[j].lastEvt;
+
+                  // We got the first part of this event, so this is
+                  // the number of events we expect for this run
+                  if (runCtrl[j].runId==mBuffer[evID].runNum)
+                      runCtrl[j].lastEvt++;
+              }
+
+              // Change 'actrun' the the new runnumber
+              actrun = mBuffer[evID].runNum;
+          }
+
+          // event not yet complete
+          if (mBuffer[evID].nBoard < actBoards)
+              continue;
+
+          // This is a non-ideal hack to lower the probability that
+          // in mBufEvt the search for correct entry in runCtrl
+          // will not return a super-old entry
+          for (int ir=0; ir<MAX_RUN; ir++)
+          {
+              if (runCtrl[ir].runId != actrun && runCtrl[ir].fileId>0)
+                  runCtrl[ir].runId = 0;
+          }
+
+          // Flag that the event is ready for processing
+          evtCtrl.evtStat[iDx] = 99;
 
       } // end for loop over all sockets
@@ -2051,4 +2043,6 @@
                }
 
+               RUN_HEAD actRun;
+
                actRun.Version = 1;
                actRun.RunType = -1;  //to be adapted
@@ -2365,4 +2359,6 @@
            if (runCtrl[lastRun].fileId < 0)
            {
+               RUN_HEAD actRun;
+
                actRun.Version = 1;
                actRun.RunType = -1;  //to be adapted
