Index: /trunk/FACT++/src/EventBuilder.c
===================================================================
--- /trunk/FACT++/src/EventBuilder.c	(revision 15458)
+++ /trunk/FACT++/src/EventBuilder.c	(revision 15459)
@@ -626,12 +626,17 @@
     int found = 0;
 
+    // fileId==-2: not yet used or run assigned but not open
+    // fileId== 0: file open
+    // fileId>0:   run closed
+
     for (int k = 0; k < MAX_RUN; k++)
     {
+        // Check if run already registered (old entries should have runId==-1)
         if (runCtrl[k].runId == runID)
         {
             if (runCtrl[k].roi0 != nRoi[0] || runCtrl[k].roi8 != nRoi[8])
             {
-                factPrintf(kError, 931, "Mismatch of roi within run. Expected roi=%d and roi_tm=%d, got %d and %d.",
-                           runCtrl[k].roi0, runCtrl[k].roi8, nRoi[0], nRoi[8]);
+                factPrintf(kError, 931, "Mismatch of roi within run. Expected roi=%d and roi_tm=%d, got %d and %d (runID=%d, evID=%d)",
+                           runCtrl[k].roi0, runCtrl[k].roi8, nRoi[0], nRoi[8], runID, evID);
                 return -9301;
             }
@@ -641,8 +646,26 @@
         }
 
+        // This is just for sanity. We use the oldest free entry (until
+        // we have understood the concept and can use "just" a free entry
+        if (runCtrl[k].runId==0 && runCtrl[k].closeTime < oldest)
+        {
+            oldest = runCtrl[k].closeTime;
+            jold = k;
+        }
+/*
+        // Empty slot already found?
         if (evFree>=0)
             continue;
-
-        //not yet used
+*/
+
+        /*
+        if (runCtrl[k].fileId!=0)
+        {
+            evFree = k;
+            continue;
+        }*/
+
+        /*
+        // Slot not yet used
         if (runCtrl[k].fileId < 0)
         {
@@ -651,13 +674,13 @@
         }
 
-        //already closed
+        // Slot already closed
         if (runCtrl[k].fileId > 0 && runCtrl[k].closeTime < oldest)
         {
             oldest = runCtrl[k].closeTime;
             jold = k;
-        }
+        }*/
     }
 
-    if (!found)
+    if (!found) // Run not yet registered, register run
     {
         if (evFree < 0 && jold < 0)
@@ -670,6 +693,6 @@
             evFree = jold;
 
-        factPrintf(kInfo, 503, "New run %d (evFree=%d) registered with roi=%d and roi_tm=%d", runID,
-                   evFree, nRoi[0], nRoi[8]);
+        factPrintf(kInfo, 503, "New run %d (evID=%d, evFree=%d) registered with roi=%d and roi_tm=%d",
+                   runID, evID, evFree, nRoi[0], nRoi[8]);
 
         runCtrl[evFree].runId      = runID;
@@ -678,12 +701,11 @@
         runCtrl[evFree].fileId     = -2;
         runCtrl[evFree].procId     = -2;
-        runCtrl[evFree].lastEvt    = -1;
-        runCtrl[evFree].nextEvt    = 0;
-        runCtrl[evFree].actEvt     = 0;
-        runCtrl[evFree].procEvt    = 0;
-        runCtrl[evFree].maxEvt     = 999999999;       //max number events allowed
+        runCtrl[evFree].lastEvt    = 1;         // Number of events partially started to read
+        runCtrl[evFree].actEvt     = 0;         // Number of written events (write)
+        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;
+        runCtrl[evFree].lastTime   = tsec;      // Time when the last event was written
         runCtrl[evFree].closeTime  = tsec + 3600 * 24;     //max time allowed
 
@@ -695,6 +717,4 @@
     }
 
-    // FIXME: Why is that done if FOUND?
-
     //flag all boards as unused
     mBuffer[i].nBoard = 0;
@@ -702,20 +722,20 @@
         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].buffer            = NULL;
-    mBuffer[i].FADhead           = NULL;
+    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].buffer    = NULL;
+    mBuffer[i].FADhead   = NULL;
 
 /*
@@ -1147,5 +1167,5 @@
 /* initialize run control logics */
    for (int i = 0; i < MAX_RUN; i++) {
-      runCtrl[i].runId = 0;
+      runCtrl[i].runId  =  0;
       runCtrl[i].fileId = -2;
       runCtrl[i].procId = -2;
@@ -1491,5 +1511,5 @@
               const int iDx = evtIdx[evID];       //index into evtCtrl
 
-              evtCtrl.evtStat[iDx]++;
+              //evtCtrl.evtStat[iDx]++;
               evtCtrl.pcTime[iDx] = g_actTime;
 
@@ -1499,21 +1519,27 @@
 
               // have we already reported first (partial) event of this run ???
-              if (mBuffer[evID].nBoard>0 && mBuffer[evID].runNum != actrun)
+              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;
-
-                  for (int ir = 0; ir < MAX_RUN; ir++)
-                  {
-                      if (runCtrl[ir].runId == actrun)
-                      {
-                          if (++runCtrl[ir].lastEvt == 0)
-                          {
-                              gotNewRun (actrun, NULL);
-                              factPrintf(kInfo, 1, "gotNewRun called for run %d, event %d",
-                                         mBuffer[evID].runNum, mBuffer[evID].evNum);
-                              break;
-                          }
-                      }
-                  }
               }
 
@@ -1521,6 +1547,15 @@
               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;
-                  //gi.evtTot++;
               }
 
@@ -1604,10 +1639,7 @@
               rd[i].ftmID   = rd[i].rBuf->I[3];    //(FTMevt)
               rd[i].evtID   = rd[i].rBuf->I[4];    //(FADevt)
-              rd[i].runID   = rd[i].rBuf->I[11];
+              rd[i].runID   = rd[i].rBuf->I[11]==0 ? (int)g_actTime : rd[i].rBuf->I[11];
               rd[i].bufTyp  = 1;  //ready to read full record
               rd[i].bufLen  = rd[i].fadLen - rd[i].bufPos;
-
-              if (rd[i].runID == 0)
-                  rd[i].runID = g_actTime;
 
               const int fadBoard = rd[i].rBuf->S[12];
@@ -1655,7 +1687,6 @@
               reportIncomplete(k0);
 
-              evtCtrl.evtStat[k0] = 91;     //timeout for incomplete events
-              //gi.evtSkp++;
-              //gi.evtTot++;
+              //timeout for incomplete events
+              evtCtrl.evtStat[k0] = 91;
               gj.evtSkip++;
 
@@ -1665,5 +1696,5 @@
           // complete event in Buffer
           if (evtCtrl.evtStat[k0] >= 95)
-              gj.bufEvt++;     
+              gj.bufEvt++;
 
           // Check the less likely case: 'useless' or 'delete'
@@ -1968,9 +1999,7 @@
            }
 
-           /*** if (evtCtrl.evtStat[k0] > 90 && evtCtrl.evtStat[k0] < 1000) ***/
-
            //we are asked to flush buffers asap
            if (gi_resetR > 1)
-           {        
+           {
                evtCtrl.evtStat[k0] = 9991;
                continue;
@@ -1984,31 +2013,39 @@
            const int32_t  ievt = mBuffer[id].evNum;
 
-           int j = lastRun;
+           // Find entry in runCtrl which belongs to the event mBuffer[id]
+           // (only check if there is a need to check)
            if (runCtrl[lastRun].runId != irun)
            {
                //check which fileID to use (or open if needed)
-               for (j = 0; j < MAX_RUN; j++) {
+               int j;
+               for (j=0;j<MAX_RUN; j++)
                    if (runCtrl[j].runId == irun)
                        break;
-               }
-               if (j >= MAX_RUN) {
-                   factPrintf(kFatal, 901, "procEvt: Can not find run %d for event %d in %d", irun, ievt, id);
-                   // FIXME: The event has to be flagged such that it will be deleted! Although that should never happen
+
+               if (j>=MAX_RUN)
+               {
+                   factPrintf(kFatal, 901, "writeEvt: Can not find run %d for event %d in %d", irun, ievt, id);
+                   // FIXME: What is the right action? (Flag event for deletion?)
                    continue;
                }
+
                lastRun = j;
            }
 
-           if (runCtrl[j].fileId < 0)
+           // File not yet open
+           if (runCtrl[lastRun].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
+               // This loop is unique to procEvt
+               for (int j=0; j<MAX_RUN; j++)
+               {
+                   if (runCtrl[j].fileId == 0)
+                   {
+                       runCtrl[j].procId = 2; //--> do no longer accept events for processing
+
                        //----                  problem: processing still going on ==> must wait for closing ....
-                       factPrintf(kInfo, -1, "procEvt: Finished run since new one opened %d", runCtrl[j1].runId);
-                       runFinish1(runCtrl[j1].runId);
+                       factPrintf(kInfo, -1, "procEvt: Finished run since new one opened %d", runCtrl[j].runId);
+                       runFinish1(runCtrl[j].runId);
                    }
                }
@@ -2017,11 +2054,11 @@
                actRun.RunType = -1;  //to be adapted
 
-               actRun.Nroi = runCtrl[j].roi0;
-               actRun.NroiTM = runCtrl[j].roi8;
+               actRun.Nroi   = runCtrl[lastRun].roi0;
+               actRun.NroiTM = runCtrl[lastRun].roi8;
                //ETIENNE don't reset it to zero as it is taken care of in DataWriteFits
                //                  if (actRun.Nroi == actRun.NroiTM)
                //                     actRun.NroiTM = 0;
-               actRun.RunTime = runCtrl[j].firstTime;
-               actRun.RunUsec = runCtrl[j].firstUsec;
+               actRun.RunTime = runCtrl[lastRun].firstTime;
+               actRun.RunUsec = runCtrl[lastRun].firstUsec;
                actRun.NBoard  = NBOARDS;
                actRun.NPix    = NPIX;
@@ -2031,35 +2068,35 @@
                memcpy(actRun.FADhead, mBuffer[id].FADhead, NBOARDS*sizeof(PEVNT_HEADER));
 
-               runCtrl[j].fileHd = runOpen (irun, &actRun, sizeof (actRun));
-               if (runCtrl[j].fileHd == NULL)
+               runCtrl[lastRun].fileHd = runOpen (irun, &actRun, sizeof (actRun));
+               if (runCtrl[lastRun].fileHd == NULL)
                {
                    factPrintf(kError, 502, "procEvt: Could not open a file for run %d (runOpen failed)", irun);
-                   runCtrl[j].fileId = 91;
-                   runCtrl[j].procId = 91;
+                   runCtrl[lastRun].fileId = 91;
+                   runCtrl[lastRun].procId = 91;  // Is not set in writeEvt
+                   continue;
                }
-               else
+
+               runCtrl[lastRun].fileId = 0;
+               runCtrl[lastRun].procId = 0;  // Is not set in writeEvt
+
+               factPrintf(kInfo, -1, "procEvt: Opened new file for run %d (evt=%d)", irun, ievt);
+           }
+
+           //-------- also check if run shall be closed (==> skip event, but do not close the file !!! )
+           if (runCtrl[lastRun].procId == 0)
+           {
+               if (runCtrl[lastRun].closeTime < g_actTime ||
+                   runCtrl[lastRun].lastTime < g_actTime - 300 ||
+                   runCtrl[lastRun].maxEvt <= runCtrl[lastRun].procEvt)
                {
-                   factPrintf(kInfo, -1, "procEvt: Opened new file for run %d (evt=%d)", irun, ievt);
-                   runCtrl[j].fileId = 0;
-                   runCtrl[j].procId = 0;
+                   factPrintf(kInfo, 502, "procEvt: Reached end of run condition for run %d", irun);
+                   runFinish1 (runCtrl[lastRun].runId);
+                   runCtrl[lastRun].procId = 1;
                }
            }
 
-           //-------- also check if run shall be closed (==> skip event, but do not close the file !!! )
-           if (runCtrl[j].procId == 0)
+           // Skip event because of no active run
+           if (runCtrl[lastRun].procId != 0)
            {
-               if (runCtrl[j].closeTime < g_actTime ||
-                   runCtrl[j].lastTime < g_actTime - 300 ||
-                   runCtrl[j].maxEvt <= runCtrl[j].procEvt)
-               {
-                   factPrintf(kInfo, 502, "procEvt: Reached end of run condition for run %d", irun);
-                   runFinish1 (runCtrl[j].runId);
-                   runCtrl[j].procId = 1;
-               }
-           }
-           if (runCtrl[j].procId != 0) {
-#ifdef EVTDEBUG
-               factPrintf(kDebug, 502, "procEvt: Skip event %d because no active run %d", ievt, irun);
-#endif
                evtCtrl.evtStat[k0] = 9091;
                continue;
@@ -2134,5 +2171,5 @@
            {
                evtCtrl.evtStat[k0] = 1000;
-               runCtrl[j].procEvt++;
+               runCtrl[lastRun].procEvt++;
            }
        }
@@ -2293,6 +2330,4 @@
            }
 
-           /*** if (evtCtrl.evtStat[k0] > 5000 && evtCtrl.evtStat[k0] < 9000) ***/
-
            //we must drain the buffer asap
            if (gi_resetR > 1)
@@ -2307,21 +2342,19 @@
            const int32_t  ievt = mBuffer[id].evNum;
 
-           //gi.wrtTot++;
-
-           int j = lastRun;
-
+           // Find entry in runCtrl which belongs to the event mBuffer[id]
+           // (only check if there is a need to check)
            if (runCtrl[lastRun].runId != irun)
            {
                //check which fileID to use (or open if needed)
-               for (j = 0; j < MAX_RUN; j++)
-               {
+               int j;
+               for (j=0;j<MAX_RUN; j++)
                    if (runCtrl[j].runId == irun)
                        break;
-               }
-
-               if (j >= MAX_RUN)
+
+               if (j>=MAX_RUN)
                {
                    factPrintf(kFatal, 901, "writeEvt: Can not find run %d for event %d in %d", irun, ievt, id);
-                   //gi.wrtErr++;
+                   // FIXME: What is the right action?
+                   continue;
                }
 
@@ -2329,90 +2362,92 @@
            }
 
-           if (runCtrl[j].fileId < 0) {
+           // File not yet open
+           if (runCtrl[lastRun].fileId < 0)
+           {
                actRun.Version = 1;
                actRun.RunType = -1;  //to be adapted
 
-               actRun.Nroi = runCtrl[j].roi0;
-               actRun.NroiTM = runCtrl[j].roi8;
+               actRun.Nroi   = runCtrl[lastRun].roi0;
+               actRun.NroiTM = runCtrl[lastRun].roi8;
                //ETIENNE don't reset it to zero as it is taken care of in DataWriteFits
                //                  if (actRun.Nroi == actRun.NroiTM)
                //                     actRun.NroiTM = 0;
-               actRun.RunTime = runCtrl[j].firstTime;
-               actRun.RunUsec = runCtrl[j].firstUsec;
-               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) {
+               actRun.RunTime = runCtrl[lastRun].firstTime;
+               actRun.RunUsec = runCtrl[lastRun].firstUsec;
+               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[lastRun].fileHd = runOpen (irun, &actRun, sizeof (actRun));
+               if (runCtrl[lastRun].fileHd == NULL)
+               {
                    factPrintf(kError, 502, "writeEvt: Could not open a file for run %d (runOpen failed)", irun);
-                   runCtrl[j].fileId = 91;
-               } else {
-                   factPrintf(kInfo, -1, "writeEvt: Opened new file for run %d (evt %d)", irun, ievt);
-                   runCtrl[j].fileId = 0;
+                   runCtrl[lastRun].fileId = 91;
+                   continue;
                }
 
+               runCtrl[lastRun].fileId = 0;
+               factPrintf(kInfo, -1, "writeEvt: Opened new file for run %d (evt %d)", irun, ievt);
            }
 
-           if (runCtrl[j].fileId != 0) {
-               if (runCtrl[j].fileId < 0) {
-                   factPrintf(kError, 123, "writeEvt: Never opened file for run %d", irun);
-               } else if (runCtrl[j].fileId < 100) {
+           if (runCtrl[lastRun].fileId > 0)
+           {
+               // There is an event but file is already closed
+               /*
+               if (runCtrl[j].fileId < 100)
+               {
                    factPrintf(kWarn, 123, "writeEvt: File for run %d is closed", irun);
                    runCtrl[j].fileId += 100;
-               } else {
-#ifdef EVTDEBUG
-                   factPrintf(kDebug, 123, "writeEvt: File for run %d is closed", irun);
-#endif
+               }*/
+
+               evtCtrl.evtStat[k0] = 9903;
+           }
+
+           // File is open
+           if (runCtrl[lastRun].fileId==0)
+           {
+               const int rc = runWrite(runCtrl[lastRun].fileHd, mBuffer[id].fEvent,
+                                       sizeof (mBuffer[id]));
+               if (rc >= 0)
+               {
+                   // Sucessfully wrote event
+                   runCtrl[lastRun].lastTime = g_actTime;
+                   runCtrl[lastRun].actEvt++;
+                   evtCtrl.evtStat[k0] = 9901;
                }
-               evtCtrl.evtStat[k0] = 9903;
-               //gi.wrtErr++;
-           } else {
-               // snprintf (str, MXSTR,"write event %d size %d",ievt,sizeof (mBuffer[id]));
-               // factOut (kInfo, 504, str);
-               const int rc = runWrite (runCtrl[j].fileHd, mBuffer[id].fEvent,
-                                        sizeof (mBuffer[id]));
-               if (rc >= 0) {
-                   runCtrl[j].lastTime = g_actTime;
-                   runCtrl[j].actEvt++;
-                   evtCtrl.evtStat[k0] = 9901;
-#ifdef EVTDEBUG
-                   factPrintf(kDebug, 504, "%5d successfully wrote for run %d id %5d", ievt, irun, k0);
-#endif
-                   //               gj.writEvt++ ;
-               } else {
+               else
+               {
                    factPrintf(kError, 503, "writeEvt: Writing event for run %d failed (runWrite)", irun);
                    evtCtrl.evtStat[k0] = 9902;
-                   //gi.wrtErr++;
                }
 
-               checkAndCloseRun(j, irun, rc<0, 1);
+               checkAndCloseRun(lastRun, irun, rc<0, 1);
            }
        }
-
-      //check if we should close a run (mainly when no event pending)
-      //ETIENNE but first figure out which one is the latest run with a complete event.
-      //i.e. max run Id and lastEvt >= 0
-      //this condition is sufficient because all pending events were written already in the loop just above
-      //actrun
-      uint32_t lastStartedTime = 0;
-      uint32_t runIdFound = 0;
-      if (actrun != 0)
-      {//If we have an active run, look for its start time
-          for (int j=0;j<MAX_RUN;j++)
-          {
-              if (runCtrl[j].runId == actrun)
-              {
-                  lastStartedTime = runCtrl[j].lastTime;
-                  runIdFound = 1;
-              }
-          }
-      }
-      else
-          runIdFound = 1;
+/*
+       //check if we should close a run (mainly when no event pending)
+       //ETIENNE but first figure out which one is the latest run with a complete event.
+       //i.e. max run Id and lastEvt >= 0
+       //this condition is sufficient because all pending events were written already in the loop just above
+       //actrun
+       uint32_t lastStartedTime = 0;
+       uint32_t runIdFound = 1;
+
+       //If we have an active run, look for its start time
+       if (actrun != 0)
+       {
+           runIdfound = 0;
+           for (int j=0;j<MAX_RUN;j++)
+           {
+               if (runCtrl[j].runId == actrun)
+               {
+                   lastStartedTime = runCtrl[j].lastTime;
+                   runIdFound = 1;
+               }
+           }
+       }
 
       if (runIdFound == 0)
@@ -2420,28 +2455,30 @@
           factPrintf(kInfo, 0, "An Active run (number %u) has been registered, but it could not be found in the runs list", actrun);
       }
-//	  snprintf(str, MXSTR, "Maximum runId: %d", maxStartedRun);
-//	  factOut(kInfo, 000, str);
-      //Also check if some files will never be opened
-      //EDIT: this is completely useless, because as run Numbers are taken from FADs board,
-      //I will never get run numbers for which no file is to be opened
-	  for (int j=0;j<MAX_RUN;j++)
-      {
-          if ((runCtrl[j].fileId < 0) &&
-              (runCtrl[j].lastTime < lastStartedTime) &&
-			  (runCtrl[j].runId != 0))
-          {
-              factPrintf(kInfo, 0, "writeEvt: No file will be opened for run %u. Last run: %u (started)", runCtrl[j].runId, actrun);
-           ;//TODO notify that this run will never be opened
-          }
-      }
-      for (int j=0; j<MAX_RUN; j++)
-      {
-          if (runCtrl[j].fileId == 0)
-          {
-              //ETIENNE added the condition at this line. dunno what to do with run 0: skipping it
-              const int cond = runCtrl[j].lastTime < lastStartedTime && runCtrl[j].runId != 0;
-              checkAndCloseRun(j, runCtrl[j].runId, cond, 2);
-          }
-      }
+
+       //Also check if some files will never be opened
+       //EDIT: this is completely useless, because as run Numbers are taken from FADs board,
+       //I will never get run numbers for which no file is to be opened
+       for (int j=0;j<MAX_RUN;j++)
+       {
+           if ((runCtrl[j].fileId < 0) &&
+               (runCtrl[j].lastTime < lastStartedTime) &&
+               (runCtrl[j].runId != 0))
+           {
+               factPrintf(kInfo, 0, "writeEvt: No file will be opened for run %u. Last run: %u (started)", runCtrl[j].runId, actrun);
+               ;//TODO notify that this run will never be opened
+           }
+       }
+       */
+
+       // Although the are no pending events, we have to check if a run should be closed (timeout)
+       for (int j=0; j<MAX_RUN; j++)
+       {
+           if (runCtrl[j].fileId == 0)
+           {
+               //ETIENNE added the condition at this line. dunno what to do with run 0: skipping it
+               const int cond = /*runCtrl[j].lastTime < lastStartedTime &&*/ runCtrl[j].runId == 0;
+               checkAndCloseRun(j, runCtrl[j].runId, cond, 2);
+           }
+       }
 
       //seems we have nothing to do, so sleep a little
@@ -2449,19 +2486,17 @@
           usleep(1);
 
-      if (gj.readStat < -10 && numWait == 0) {  //nothing left to do
-          factPrintf(kInfo, -1, "Finish Write Process ...");
-         gw_runStat = -22;      //==> we should exit
-         gj.writStat = -22;     //==> we should exit
-         goto closerun;
-      }
-      gw_runStat = gi_runStat;
-      gj.writStat = gj.readStat;
-
-   }
-
-   //must close all open files ....
-   factPrintf(kInfo, -1, "Abort Writing Process ...");
-
- closerun:
+       //nothing left to do
+       if (gj.readStat < -10 && numWait == 0)
+       {
+           factPrintf(kInfo, -1, "Finish Write Process ...");
+           gw_runStat = -22;      //==> we should exit
+           gj.writStat = -22;     //==> we should exit
+           break;
+       }
+
+       gw_runStat = gi_runStat;
+       gj.writStat = gj.readStat;
+   }
+
    factPrintf(kInfo, -1, "Close all open files ...");
    for (int j=0; j<MAX_RUN; j++)
@@ -2473,10 +2508,8 @@
    gw_runStat = -99;
    gj.writStat = -99;
+
    factPrintf(kInfo, -1, "Exit Writing Process ...");
+
    return 0;
-
-
-
-
 } /*-----------------------------------------------------------------*/
 
