Index: /trunk/FACT++/src/EventBuilder.c
===================================================================
--- /trunk/FACT++/src/EventBuilder.c	(revision 15466)
+++ /trunk/FACT++/src/EventBuilder.c	(revision 15467)
@@ -123,8 +123,5 @@
 GUI_STAT gj;
 
-//int evtIdx[MAX_EVT * MAX_RUN];  //index from mBuffer to evtCtrl
-
 EVT_CTRL evtCtrl[MAX_EVT * MAX_RUN]; //control of events during processing
-WRK_DATA mBuffer[MAX_EVT * MAX_RUN]; //local working space
 
 //#define MXSTR 1000
@@ -502,13 +499,12 @@
 // initialize mBuffer (mark all entries as unused\empty)
 
-   uint32_t actime = g_actTime + 50000000;
+    //uint32_t actime = g_actTime + 50000000;
 
    for (int i = 0; i < MAX_EVT * MAX_RUN; i++) {
-      mBuffer[i].evNum = mBuffer[i].nRoi = -1;
-      mBuffer[i].runNum = 0;
-
-      evtCtrl[i].mBuffer_idx = -1;
+      evtCtrl[i].evNum = evtCtrl[i].nRoi = -1;
+      evtCtrl[i].runNum = 0;
+
+      //evtCtrl[i].mBuffer_idx = -1;
       evtCtrl[i].evtStat = -1;
-      //evtCtrl[i].lastRecv = actime;       //initiate to far future
 
 #ifdef ETIENNE_MALLOC
@@ -536,6 +532,5 @@
 int checkRoiConsistency(int i, int roi[]);
 
-int mBufEvt (int sk)
-/*(int evID, uint runID, int nRoi[], int sk, int fadlen, int trgTyp, int trgNum, int fadNum)*/
+int mBufEvt(int sk)
 {
 // generate a new Event into mBuffer:   
@@ -551,5 +546,4 @@
 //   size_t needmem = 0;
 
-
     int nRoi[9];
     if (!checkRoiConsistency(sk, nRoi))
@@ -560,53 +554,57 @@
     const int  evID   = rd[sk].evtID;
     const uint runID  = rd[sk].runID;
-    const int  fadlen = rd[sk].fadLen;
     const int  trgTyp = rd[sk].ftmTyp;
     const int  trgNum = rd[sk].ftmID;
     const int  fadNum = rd[sk].evtID;
 
-    int i = evID % MAX_EVT;
-    int evFree = -1;
-
-    for (int k = 0; k < MAX_RUN; k++)
+    int beg = (evtCtrl_lastPtr + MAX_EVT*MAX_RUN - 1) % (MAX_EVT*MAX_RUN);
+    int end = (evtCtrl_frstPtr + MAX_EVT*MAX_RUN - 1) % (MAX_EVT*MAX_RUN);
+
+    for (int k=beg; k!=end; k--, k += MAX_EVT*MAX_RUN, k %= MAX_EVT*MAX_RUN)
     {
-        //event is already registered;
-        if (mBuffer[i].evNum == evID && mBuffer[i].runNum == runID)
+        // If the run is different, go on searching.
+        // We cannot stop searching if a lower run-id is found as in
+        // the case of the events, because theoretically, there
+        // can be the same run on two different days.
+        if (runID != evtCtrl[k].runNum)
+            continue;
+
+        // The event in the control structure has an event id with
+        // a lower event id than the current one. All previous events
+        // should have an even lower event id, so there is no way it
+        // can be found in the structure.
+        if (evtCtrl[k].evNum < evID/* && runID == evtCtrl[k].runNum*/)
+            break;
+
+        if (evID != evtCtrl[k].evNum/* || runID != evtCtrl[k].runNum*/)
+            continue;
+
+        // is it ok ????
+        if (evtCtrl[k].nRoi != nRoi[0] || evtCtrl[k].nRoiTM != nRoi[8])
         {
-            // is it ok ????
-            if (mBuffer[i].nRoi != nRoi[0] || mBuffer[i].nRoiTM != nRoi[8])
-            {
-                factPrintf(kError, 821, "Mismatch of roi within event. Expected roi=%d and roi_tm=%d, got %d and %d.",
-                           mBuffer[i].nRoi, mBuffer[i].nRoiTM, nRoi[0], nRoi[8]);
-                return -8201;
-            }
-
-            // count for inconsistencies
-            if (mBuffer[i].trgNum != trgNum)
-                mBuffer[i].Errors[0]++;
-            if (mBuffer[i].fadNum != fadNum)
-               mBuffer[i].Errors[1]++;
-            if (mBuffer[i].trgTyp != trgTyp)
-                mBuffer[i].Errors[2]++;
-
-            //everything seems fine so far ==> use this slot ....
-            return i;
+            factPrintf(kError, 821, "Mismatch of roi within event. Expected roi=%d and roi_tm=%d, got %d and %d.",
+                       evtCtrl[k].nRoi, evtCtrl[k].nRoiTM, nRoi[0], nRoi[8]);
+            return -8201;
         }
 
-        if (evFree < 0 && mBuffer[i].evNum < 0)
-            evFree = i;
-
-        i += MAX_EVT;
+        // count for inconsistencies
+        if (evtCtrl[k].trgNum != trgNum)
+            evtCtrl[k].Errors[0]++;
+        if (evtCtrl[k].fadNum != fadNum)
+            evtCtrl[k].Errors[1]++;
+        if (evtCtrl[k].trgTyp != trgTyp)
+            evtCtrl[k].Errors[2]++;
+
+        //everything seems fine so far ==> use this slot ....
+        return k;
     }
 
-
     //event does not yet exist; create it
 
-    if (evFree < 0) //no space available in ctrl
+    if (end-beg==1 || (end==0 && beg==MAX_EVT*MAX_RUN-1)) //no space available in ctrl
     {
-        factPrintf(kError, 881, "No control slot to keep event %d", evID);
+        factPrintf(kError, 881, "No control slot to keep event %d (run %d) %d %d", evID, runID, beg, end);
         return -1;
     }
-
-    i = evFree;                  //found free entry; use it ...
 
     // FIXME: This should be the time of the first receiped board
@@ -618,5 +616,4 @@
 
     //check if runId already registered in runCtrl
-    evFree = -1;
 
     uint oldest = g_actTime + 1000;
@@ -629,5 +626,5 @@
     // fileId>0:   run closed
 
-    for (int k = 0; k < MAX_RUN; k++)
+    for (int k=0; k<MAX_RUN; k++)
     {
         // Check if run already registered (old entries should have runId==-1)
@@ -653,36 +650,9 @@
             jold = k;
         }
-/*
-        // Empty slot already found?
-        if (evFree>=0)
-            continue;
-*/
-
-        /*
-        if (runCtrl[k].fileId!=0)
-        {
-            evFree = k;
-            continue;
-        }*/
-
-        /*
-        // Slot not yet used
-        if (runCtrl[k].fileId < 0)
-        {
-            evFree = k;
-            continue;
-        }
-
-        // Slot already closed
-        if (runCtrl[k].fileId > 0 && runCtrl[k].closeTime < oldest)
-        {
-            oldest = runCtrl[k].closeTime;
-            jold = k;
-        }*/
     }
 
     if (!found) // Run not yet registered, register run
     {
-        if (evFree < 0 && jold < 0)
+        if (jold < 0)
         {
             factPrintf(kFatal, 883, "Not able to register the new run %d", runID);
@@ -690,6 +660,5 @@
         }
 
-        if (evFree < 0)
-            evFree = jold;
+        int evFree = jold;
 
         factPrintf(kInfo, 503, "New run %d (evID=%d, evFree=%d) registered with roi=%d and roi_tm=%d",
@@ -705,103 +674,60 @@
         runCtrl[evFree].procEvt    = 0;         // Number of successfully checked events (checkEvent)
         runCtrl[evFree].maxEvt     = 999999999; // max number events allowed
-        //runCtrl[evFree].firstTime  = tsec;
-        //runCtrl[evFree].firstUsec  = tusec;
         runCtrl[evFree].lastTime   = tsec;      // Time when the last event was written
         runCtrl[evFree].closeTime  = tsec + 3600 * 24;     //max time allowed
-/*
-        runTail[evFree].nEventsOk  = 0;
-        runTail[evFree].nEventsRej = 0;
-        runTail[evFree].nEventsBad = 0;
-        runTail[evFree].PCtime0    = 0;
-        runTail[evFree].PCtimeX    = 0;
-        */
     }
 
+    const int k = evtCtrl_lastPtr;
+
     //flag all boards as unused
-    mBuffer[i].nBoard = 0;
-    for (int k = 0; k < NBOARDS; k++)
-        mBuffer[i].board[k] = -1;
-
-    mBuffer[i].pcTime[0] = tsec;
-    mBuffer[i].pcTime[1] = tusec;
-    mBuffer[i].nRoi      = nRoi[0];
-    mBuffer[i].nRoiTM    = nRoi[8];
-    mBuffer[i].evNum     = evID;
-    mBuffer[i].runNum    = runID;
-    mBuffer[i].fadNum    = fadNum;
-    mBuffer[i].trgNum    = trgNum;
-    mBuffer[i].trgTyp    = trgTyp;
-    mBuffer[i].Errors[0] = 0;
-    mBuffer[i].Errors[1] = 0;
-    mBuffer[i].Errors[2] = 0;
-    mBuffer[i].Errors[3] = 0;
-    mBuffer[i].fEvent    = NULL;
-    mBuffer[i].FADhead   = NULL;
-    //mBuffer[i].buffer    = NULL;
-
-/*
-#ifdef ETIENNE_MALLOC
-    mBuffer[i].FADhead = ETI_Malloc(evID, i);
-    mBuffer[i].buffer = NULL;
-    if (mBuffer[i].FADhead != NULL)
-        mBuffer[i].fEvent = (EVENT*)&(((char*)(mBuffer[i].FADhead))[MAX_HEAD_MEM]);
-    else
-    {
-        mBuffer[i].fEvent = NULL;
-        gj.usdMem = 0;
-        for (int k=0;k<numAllocatedChunks;k++)
-            gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE;
-        if (gj.usdMem > gj.maxMem)
-            gj.maxMem = gj.usdMem;
-        else
-        {
-            factPrintf(kDebug, 882, "No memory left to keep event %6d sock %3d", evID, sk);
-        }
-        return -11;
-    }
-#endif
-
-#ifdef STANDARD_MALLOC
-    mBuffer[i].FADhead = malloc (MAX_HEAD_MEM+MAX_EVT_MEM);
-    mBuffer[i].fEvent  = NULL;
-    mBuffer[i].buffer  = NULL;
-    if (mBuffer[i].FADhead == NULL)
-    {
-        factPrintf(kError, 882, "malloc header failed for event %d", evID);
-        return -12;
-    }
-    mBuffer[i].fEvent = (void*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM);
-#endif
-
-#ifdef ETIENNE_MALLOC
-    //ETIENNE
-    //gj.usdMem += needmem + headmem + gi_maxSize;
-    gj.usdMem = 0;
-    for (int k=0;k<numAllocatedChunks;k++)
-        gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE;
-    //END ETIENNE
-#endif
-*/
-
-    return i;
+    evtCtrl[k].nBoard = 0;
+    for (int b=0; b<NBOARDS; b++)
+        evtCtrl[k].board[b] = -1;
+
+    evtCtrl[k].pcTime[0] = tsec;
+    evtCtrl[k].pcTime[1] = tusec;
+    evtCtrl[k].nRoi      = nRoi[0];
+    evtCtrl[k].nRoiTM    = nRoi[8];
+    evtCtrl[k].evNum     = evID;
+    evtCtrl[k].runNum    = runID;
+    evtCtrl[k].fadNum    = fadNum;
+    evtCtrl[k].trgNum    = trgNum;
+    evtCtrl[k].trgTyp    = trgTyp;
+    evtCtrl[k].Errors[0] = 0;
+    evtCtrl[k].Errors[1] = 0;
+    evtCtrl[k].Errors[2] = 0;
+    evtCtrl[k].Errors[3] = 0;
+    evtCtrl[k].fEvent    = NULL;
+    evtCtrl[k].FADhead   = NULL;
+
+    evtCtrl[k].evtStat = 0;
+
+    // This is dangerous, because theoretically, it can result is
+    // acessing invalid memory in another thread if this is split
+    // in two instructions. Must be done only _after_ the contents
+    // have been initialized
+    evtCtrl_lastPtr = (evtCtrl_lastPtr+1) % MAX_EVT * MAX_RUN;
+
+    return k;
 
 } /*-----------------------------------------------------------------*/
 
+
 void initEvent(int i)
 {
-    mBuffer[i].fEvent = (EVENT*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM);
-    memset(mBuffer[i].fEvent->Adc_Data, 0, (NPIX+NTMARK)*2*mBuffer[i].nRoi);
+    evtCtrl[i].fEvent = (EVENT*)((char*)evtCtrl[i].FADhead+MAX_HEAD_MEM);
+    memset(evtCtrl[i].fEvent->Adc_Data, 0, (NPIX+NTMARK)*2*evtCtrl[i].nRoi);
 
     //flag all pixels as unused
     for (int k = 0; k < NPIX; k++)
-        mBuffer[i].fEvent->StartPix[k] = -1;
+        evtCtrl[i].fEvent->StartPix[k] = -1;
 
     //flag all TMark as unused
     for (int k = 0; k < NTMARK; k++)
-        mBuffer[i].fEvent->StartTM[k] = -1;
-
-    mBuffer[i].fEvent->NumBoards = 0;
-    mBuffer[i].fEvent->PCTime    = mBuffer[i].pcTime[0];
-    mBuffer[i].fEvent->PCUsec    = mBuffer[i].pcTime[1];
+        evtCtrl[i].fEvent->StartTM[k] = -1;
+
+    evtCtrl[i].fEvent->NumBoards = 0;
+    evtCtrl[i].fEvent->PCTime    = evtCtrl[i].pcTime[0];
+    evtCtrl[i].fEvent->PCUsec    = evtCtrl[i].pcTime[1];
 }
 
@@ -825,5 +751,5 @@
    //ETIENNE
 #ifdef THOMAS_MALLOC
-   TGB_free(mBuffer[i].FADhead);
+   TGB_free(evtCtrl[i].FADhead);
 #endif
 
@@ -833,8 +759,8 @@
 
 //   free (mBuffer[i].fEvent);
-   mBuffer[i].fEvent = NULL;
+   evtCtrl[i].fEvent = NULL;
 
 //   free (mBuffer[i].FADhead);
-   mBuffer[i].FADhead = NULL;
+   evtCtrl[i].FADhead = NULL;
 
 //   free (mBuffer[i].buffer);
@@ -842,6 +768,6 @@
 //END ETIENNE
 //   headmem = NBOARDS * sizeof (PEVNT_HEADER);
-   mBuffer[i].evNum = mBuffer[i].nRoi = -1;
-   mBuffer[i].runNum = 0;
+   evtCtrl[i].evNum = evtCtrl[i].nRoi = -1;
+   evtCtrl[i].runNum = 0;
 
 #ifdef ETIENNE_MALLOC
@@ -910,5 +836,5 @@
 {
     factPrintf(kWarn, 601, "%5d skip incomplete evt %8d",
-               mBuffer[id].evNum, id);
+               evtCtrl[id].evNum, id);
 
     uint64_t report = 0;
@@ -922,5 +848,5 @@
             str[ik++] = '|';
 
-        const int jb = mBuffer[id].board[ib];
+        const int jb = evtCtrl[id].board[ib];
         if (jb>=0) // data received from that board
         {
@@ -1053,5 +979,5 @@
     swapEventHeaderBytes(i);
 
-    memcpy(&mBuffer[evID].FADhead[boardId].start_package_flag,
+    memcpy(&evtCtrl[evID].FADhead[boardId].start_package_flag,
            &rd[i].rBuf->S[0], sizeof(PEVNT_HEADER));
 
@@ -1076,8 +1002,8 @@
             src++;
 
-            mBuffer[evID].fEvent->StartPix[pixS] = pixC;
+            evtCtrl[evID].fEvent->StartPix[pixS] = pixC;
 
             const int dest1 = pixS * roi;
-            memcpy(&mBuffer[evID].fEvent->Adc_Data[dest1],
+            memcpy(&evtCtrl[evID].fEvent->Adc_Data[dest1],
                    &rd[i].rBuf->S[src], roi * 2);
 
@@ -1094,12 +1020,12 @@
 
                     const int srcT = src - roi;
-                    mBuffer[evID].fEvent->StartTM[tmS] = (pixC + pixR - roi) % 1024;
-
-                    memcpy(&mBuffer[evID].fEvent->Adc_Data[dest2],
+                    evtCtrl[evID].fEvent->StartTM[tmS] = (pixC + pixR - roi) % 1024;
+
+                    memcpy(&evtCtrl[evID].fEvent->Adc_Data[dest2],
                            &rd[i].rBuf->S[srcT], roi * 2);
                 }
                 else
                 {
-                    mBuffer[evID].fEvent->StartTM[tmS] = -1;
+                    evtCtrl[evID].fEvent->StartTM[tmS] = -1;
 
                     //ETIENNE because the TM channels are always processed during drs calib,
@@ -1521,9 +1447,9 @@
 
           // We have a valid entry, but no memory has yet been allocated
-          if (evID >= 0 && mBuffer[evID].FADhead == NULL)
+          if (evID >= 0 && evtCtrl[evID].FADhead == NULL)
           {
               // Try to get memory from the big buffer
-              mBuffer[evID].FADhead = (PEVNT_HEADER*)TGB_Malloc();
-              if (mBuffer[evID].FADhead == NULL)
+              evtCtrl[evID].FADhead = (PEVNT_HEADER*)TGB_Malloc();
+              if (evtCtrl[evID].FADhead == NULL)
               {
                   // If this works properly, this is a hack which can be removed, or
@@ -1550,12 +1476,12 @@
 
               //register event in 'active list (reading)'
-              mBuffer[evID].evtCtrl_idx = evtCtrl_lastPtr;
-
-              evtCtrl[evtCtrl_lastPtr].mBuffer_idx = evID;
-              evtCtrl[evtCtrl_lastPtr].evtStat = 0;
+              //mBuffer[evID].evtCtrl_idx = evtCtrl_lastPtr;
+
+              //evtCtrl[evtCtrl_lastPtr].mBuffer_idx = evID;
+              //evtCtrl[evtCtrl_lastPtr].evtStat = 0;
               //evtCtrl[evtCtrl.lastPtr].pcTime  = g_actTime;
 
-              evtCtrl_lastPtr++;
-              evtCtrl_lastPtr %= MAX_EVT * MAX_RUN;
+              //evtCtrl_lastPtr++;
+              //evtCtrl_lastPtr %= MAX_EVT * MAX_RUN;
           }
 
@@ -1579,5 +1505,5 @@
           }
 
-          if (mBuffer[evID].board[board] != -1)
+          if (evtCtrl[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",
@@ -1594,21 +1520,17 @@
           // now we have stored a new board contents into Event structure
 
-          mBuffer[evID].fEvent->NumBoards++;
-          mBuffer[evID].board[board] = board;
-          mBuffer[evID].nBoard++;
-
-          const int iDx = mBuffer[evID].evtCtrl_idx;//evtIdx[evID];       //index into evtCtrl
-
-          evtCtrl[iDx].evtStat  = mBuffer[evID].nBoard;
-          //evtCtrl[iDx].lastRecv = g_actTime;
+          evtCtrl[evID].fEvent->NumBoards++;
+          evtCtrl[evID].board[board] = board;
+          evtCtrl[evID].nBoard++;
+          evtCtrl[evID].evtStat  = evtCtrl[evID].nBoard;
 
           // have we already reported first (partial) event of this run ???
-          if (mBuffer[evID].nBoard==1 && mBuffer[evID].runNum != actrun)
+          if (evtCtrl[evID].nBoard==1 && evtCtrl[evID].runNum != actrun)
           {
               // Signal the fadctrl that a new run has been started
-              gotNewRun(mBuffer[evID].runNum, NULL);
+              gotNewRun(evtCtrl[evID].runNum, NULL);
 
               factPrintf(kInfo, 1, "gotNewRun called, prev run %d, new run %d, event %d",
-                         actrun, mBuffer[evID].runNum, mBuffer[evID].evNum);
+                         actrun, evtCtrl[evID].runNum, evtCtrl[evID].evNum);
 
               for (int j=0; j<MAX_RUN; j++)
@@ -1621,14 +1543,14 @@
                   // 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)
+                  if (runCtrl[j].runId==evtCtrl[evID].runNum)
                       runCtrl[j].lastEvt++;
               }
 
               // Change 'actrun' the the new runnumber
-              actrun = mBuffer[evID].runNum;
+              actrun = evtCtrl[evID].runNum;
           }
 
           // event not yet complete
-          if (mBuffer[evID].nBoard < actBoards)
+          if (evtCtrl[evID].nBoard < actBoards)
               continue;
 
@@ -1643,5 +1565,5 @@
 
           // Flag that the event is ready for processing
-          evtCtrl[iDx].evtStat = 99;
+          evtCtrl[evID].evtStat = 99;
 
       } // end for loop over all sockets
@@ -1662,4 +1584,5 @@
       for (int k0=evtCtrl_frstPtr; k0!=evtCtrl_lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN)
       {
+          /*
           if (k0==evtCtrl_frstPtr && evtCtrl[k0].evtStat<0)
           {
@@ -1669,5 +1592,5 @@
               // Continue because evtCtrl.evtStat[k0] must be <0
               continue;
-          }
+          }*/
 
           // Check the more likely case first: incomplete events
@@ -1677,7 +1600,7 @@
 
               // Event has not yet timed out or was reported already
-              const int id = evtCtrl[k0].mBuffer_idx;
-
-              if (evtCtrl[k0].evtStat>=90 || mBuffer[id].pcTime[0]/*evtCtrl[k0].lastRecv*/>=g_actTime - 30)
+              const int id = k0;//vtCtrl[k0].mBuffer_idx;
+
+              if (evtCtrl[k0].evtStat>=90 || evtCtrl[id].pcTime[0]/*evtCtrl[k0].lastRecv*/>=g_actTime - 30)
                   continue;
 
@@ -1698,10 +1621,20 @@
           if (evtCtrl[k0].evtStat==0 || evtCtrl[k0].evtStat >= 9000)
           {
-              const int id = evtCtrl[k0].mBuffer_idx;
+              const int id = k0;//evtCtrl[k0].mBuffer_idx;
 #ifdef EVTDEBUG
-              factPrintf(kDebug, -1, "%5d free event buffer, nb=%3d", mBuffer[id].evNum, mBuffer[id].nBoard);
+              factPrintf(kDebug, -1, "%5d free event buffer, nb=%3d", evtCtrl[id].evNum, evtCtrl[id].nBoard);
 #endif
               mBufFree (id);   //event written--> free memory
               evtCtrl[k0].evtStat = -1;
+
+              if (k0==evtCtrl_frstPtr)
+              {
+                  evtCtrl_frstPtr++;
+                  evtCtrl_frstPtr %= MAX_EVT * MAX_RUN;
+              }
+              else
+                  factPrintf(kDebug, -1, "Freed a non-first slot");
+
+
               gj.evtWrite++;
               gj.rateWrite++;
@@ -1819,10 +1752,18 @@
              if (evtCtrl[k0].evtStat > minclear)
              {
-                 const int id = evtCtrl[k0].mBuffer_idx;
+                 const int id = k0;//evtCtrl[k0].mBuffer_idx;
 #ifdef EVTDEBUG
-                 factPrintf(kDebug, -1, "ev %5d free event buffer, nb=%3d", mBuffer[id].evNum, mBuffer[id].nBoard);
+                 factPrintf(kDebug, -1, "ev %5d free event buffer, nb=%3d", evtCtrl[id].evNum, evtCtrl[id].nBoard);
 #endif
                  mBufFree (id);   //event written--> free memory
                  evtCtrl[k0].evtStat = -1;
+
+              if (k0==evtCtrl_frstPtr)
+              {
+                  evtCtrl_frstPtr++;
+                  evtCtrl_frstPtr %= MAX_EVT * MAX_RUN;
+              }
+              else
+                  factPrintf(kDebug, -1, "Freed a non-first slot");
              }
              else
@@ -1830,9 +1771,10 @@
                      numclear++;      //writing is still ongoing...
 
+             /*
              if (k0 == evtCtrl_frstPtr && evtCtrl[k0].evtStat < 0)
              {
                  evtCtrl_frstPtr++;
                  evtCtrl_frstPtr %= MAX_EVT * MAX_RUN;
-             }
+             }*/
          }
 
@@ -1903,8 +1845,8 @@
             if (gi_resetR<=1)
             {
-                const int id = evtCtrl[k0].mBuffer_idx;
-
-                jret = subProcEvt(threadID, mBuffer[id].FADhead,
-                                  mBuffer[id].fEvent, NULL/*mBuffer[id].buffer*/);
+                const int id = k0;//evtCtrl[k0].mBuffer_idx;
+
+                jret = subProcEvt(threadID, evtCtrl[id].FADhead,
+                                  evtCtrl[id].fEvent, NULL/*mBuffer[id].buffer*/);
 
 
@@ -2005,8 +1947,8 @@
            //-------- 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)
-           const int id = evtCtrl[k0].mBuffer_idx;
-
-           const uint32_t irun = mBuffer[id].runNum;
-           const int32_t  ievt = mBuffer[id].evNum;
+           const int id = k0;//evtCtrl[k0].mBuffer_idx;
+
+           const uint32_t irun = evtCtrl[id].runNum;
+           const int32_t  ievt = evtCtrl[id].evNum;
 
            // Find entry in runCtrl which belongs to the event mBuffer[id]
@@ -2051,13 +1993,13 @@
                actRun.Version =  1;
                actRun.RunType = -1;  //to be adapted
-               actRun.Nroi    = mBuffer[id].nRoi;      //runCtrl[lastRun].roi0;
-               actRun.NroiTM  = mBuffer[id].nRoiTM;    //runCtrl[lastRun].roi8;
-               actRun.RunTime = mBuffer[id].pcTime[0]; //runCtrl[lastRun].firstTime;
-               actRun.RunUsec = mBuffer[id].pcTime[1]; //runCtrl[lastRun].firstUsec;
+               actRun.Nroi    = evtCtrl[id].nRoi;      //runCtrl[lastRun].roi0;
+               actRun.NroiTM  = evtCtrl[id].nRoiTM;    //runCtrl[lastRun].roi8;
+               actRun.RunTime = evtCtrl[id].pcTime[0]; //runCtrl[lastRun].firstTime;
+               actRun.RunUsec = evtCtrl[id].pcTime[1]; //runCtrl[lastRun].firstUsec;
                actRun.NBoard  = NBOARDS;
                actRun.NPix    = NPIX;
                actRun.NTm     = NTMARK;
 
-               memcpy(actRun.FADhead, mBuffer[id].FADhead, NBOARDS*sizeof(PEVNT_HEADER));
+               memcpy(actRun.FADhead, evtCtrl[id].FADhead, NBOARDS*sizeof(PEVNT_HEADER));
 
                runCtrl[lastRun].fileHd = runOpen (irun, &actRun, sizeof (actRun));
@@ -2126,14 +2068,14 @@
 
            //and set correct event header ; also check for consistency in event (not yet)
-           mBuffer[id].fEvent->Roi         = mBuffer[id].nRoi;
-           mBuffer[id].fEvent->RoiTM       = mBuffer[id].nRoiTM;
-           mBuffer[id].fEvent->EventNum    = mBuffer[id].evNum;
-           mBuffer[id].fEvent->TriggerNum  = mBuffer[id].trgNum;
-           mBuffer[id].fEvent->TriggerType = mBuffer[id].trgTyp;
-           mBuffer[id].fEvent->Errors[0]   = mBuffer[id].Errors[0];
-           mBuffer[id].fEvent->Errors[1]   = mBuffer[id].Errors[1];
-           mBuffer[id].fEvent->Errors[2]   = mBuffer[id].Errors[2];
-           mBuffer[id].fEvent->Errors[3]   = mBuffer[id].Errors[3];
-           mBuffer[id].fEvent->SoftTrig    = 0;
+           evtCtrl[id].fEvent->Roi         = evtCtrl[id].nRoi;
+           evtCtrl[id].fEvent->RoiTM       = evtCtrl[id].nRoiTM;
+           evtCtrl[id].fEvent->EventNum    = evtCtrl[id].evNum;
+           evtCtrl[id].fEvent->TriggerNum  = evtCtrl[id].trgNum;
+           evtCtrl[id].fEvent->TriggerType = evtCtrl[id].trgTyp;
+           evtCtrl[id].fEvent->Errors[0]   = evtCtrl[id].Errors[0];
+           evtCtrl[id].fEvent->Errors[1]   = evtCtrl[id].Errors[1];
+           evtCtrl[id].fEvent->Errors[2]   = evtCtrl[id].Errors[2];
+           evtCtrl[id].fEvent->Errors[3]   = evtCtrl[id].Errors[3];
+           evtCtrl[id].fEvent->SoftTrig    = 0;
 
 
@@ -2141,17 +2083,17 @@
            {
                // board is not read
-               if (mBuffer[id].board[ib] == -1)
+               if (evtCtrl[id].board[ib] == -1)
                {
-                   mBuffer[id].FADhead[ib].start_package_flag = 0;
-                   mBuffer[id].fEvent->BoardTime[ib] = 0;
+                   evtCtrl[id].FADhead[ib].start_package_flag = 0;
+                   evtCtrl[id].fEvent->BoardTime[ib] = 0;
                }
                else
                {
-                   mBuffer[id].fEvent->BoardTime[ib] = mBuffer[id].FADhead[ib].time;
+                   evtCtrl[id].fEvent->BoardTime[ib] = evtCtrl[id].FADhead[ib].time;
                }
            }
 
-           const int rc = eventCheck(mBuffer[id].runNum, mBuffer[id].FADhead,
-                                     mBuffer[id].fEvent);
+           const int rc = eventCheck(evtCtrl[id].runNum, evtCtrl[id].FADhead,
+                                     evtCtrl[id].fEvent);
            //gi.procTot++;
            numProc++;
@@ -2331,8 +2273,8 @@
            }
 
-           const int id = evtCtrl[k0].mBuffer_idx;
-
-           const uint32_t irun = mBuffer[id].runNum;
-           const int32_t  ievt = mBuffer[id].evNum;
+           const int id = k0;//evtCtrl[k0].mBuffer_idx;
+
+           const uint32_t irun = evtCtrl[id].runNum;
+           const int32_t  ievt = evtCtrl[id].evNum;
 
            // Find entry in runCtrl which belongs to the event mBuffer[id]
@@ -2362,13 +2304,13 @@
                actRun.Version =  1;
                actRun.RunType = -1;  //to be adapted
-               actRun.Nroi    = mBuffer[id].nRoi;     //runCtrl[lastRun].roi0;
-               actRun.NroiTM  = mBuffer[id].nRoiTM;   //runCtrl[lastRun].roi8;
-               actRun.RunTime = mBuffer[id].pcTime[0];//runCtrl[lastRun].firstTime;
-               actRun.RunUsec = mBuffer[id].pcTime[1];//runCtrl[lastRun].firstUsec;
+               actRun.Nroi    = evtCtrl[id].nRoi;     //runCtrl[lastRun].roi0;
+               actRun.NroiTM  = evtCtrl[id].nRoiTM;   //runCtrl[lastRun].roi8;
+               actRun.RunTime = evtCtrl[id].pcTime[0];//runCtrl[lastRun].firstTime;
+               actRun.RunUsec = evtCtrl[id].pcTime[1];//runCtrl[lastRun].firstUsec;
                actRun.NBoard  = NBOARDS;
                actRun.NPix    = NPIX;
                actRun.NTm     = NTMARK;
 
-               memcpy(actRun.FADhead, mBuffer[id].FADhead, NBOARDS * sizeof (PEVNT_HEADER));
+               memcpy(actRun.FADhead, evtCtrl[id].FADhead, NBOARDS * sizeof (PEVNT_HEADER));
 
                runCtrl[lastRun].fileHd = runOpen (irun, &actRun, sizeof (actRun));
@@ -2400,6 +2342,6 @@
            if (runCtrl[lastRun].fileId==0)
            {
-               const int rc = runWrite(runCtrl[lastRun].fileHd, mBuffer[id].fEvent,
-                                       sizeof (mBuffer[id]));
+               const int rc = runWrite(runCtrl[lastRun].fileHd, evtCtrl[id].fEvent,
+                                       sizeof (evtCtrl[id]));
                if (rc >= 0)
                {
Index: /trunk/FACT++/src/FAD.h
===================================================================
--- /trunk/FACT++/src/FAD.h	(revision 15466)
+++ /trunk/FACT++/src/FAD.h	(revision 15467)
@@ -209,5 +209,5 @@
 //---------------------------------------------------------------
 
-
+/*
 typedef struct {
   int32_t  evNum ;
@@ -232,5 +232,5 @@
 
 } WRK_DATA ;             //internal to eventbuilder
-
+*/
 
 //---------------------------------------------------------------
@@ -240,5 +240,5 @@
 //  int frstPtr ;             //first used index
 //  int lastPtr ;             //last  used index
-    int mBuffer_idx;//[MAX_EVT*MAX_RUN] ;     //index of event in mBuffer
+//    int mBuffer_idx;//[MAX_EVT*MAX_RUN] ;     //index of event in mBuffer
   int evtStat;//[MAX_EVT*MAX_RUN] ;    //status of event:
                                       // -1=empty
@@ -253,5 +253,30 @@
                                 //(TO BE REVISED)
 
-//  uint32_t lastRecv;//[MAX_EVT*MAX_RUN] ;     //time when last action happened
+  //  uint32_t lastRecv;//[MAX_EVT*MAX_RUN] ;     //time when last action happened
+
+
+  // ====================================================
+
+  int32_t  evNum;
+  int32_t  fadNum;
+  int32_t  trgNum;
+  int32_t  trgTyp;
+  uint32_t runNum;
+  int32_t  fadLen;
+  int32_t  nBoard;
+  int16_t  board[NBOARDS];
+  int16_t  nRoi;
+  int16_t  nRoiTM;
+  uint32_t pcTime[2] ;
+  uint8_t  Errors[4] ;
+
+  uint32_t evtCtrl_idx;
+
+  EVENT   *fEvent ;
+  PEVNT_HEADER *FADhead; //
+
+  // ====================================================
+
+
 } EVT_CTRL ;     //internal to eventbuilder
 
