Index: /trunk/FACT++/src/EventBuilder.c
===================================================================
--- /trunk/FACT++/src/EventBuilder.c	(revision 12473)
+++ /trunk/FACT++/src/EventBuilder.c	(revision 12474)
@@ -2316,9 +2316,38 @@
 
       //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
+      unsigned int maxStartedRun = 0;
+      for (j=0;j<MAX_RUN;j++)
+      {
+          if ((runCtrl[j].runId > maxStartedRun) &&
+              (runCtrl[j].lastEvt > -1) &&
+			  (runCtrl[j].runId != 0))
+              maxStartedRun = runCtrl[j].runId;
+      }
+//	  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 (j=0;j<MAX_RUN;j++)
+      {
+          if ((runCtrl[j].fileId < 0) &&
+              (runCtrl[j].runId < maxStartedRun) &&
+			  (runCtrl[j].runId != 0))
+          {
+              snprintf (str, MXSTR, "No file will be opened for run #%ud. latest run: %ud (started)", runCtrl[j].runId, maxStartedRun);
+              factOut (kInfo, 000, str);
+           ;//TODO notify that this run will never be opened
+          }
+      }
       for (j = 0; j < MAX_RUN; j++) {
          if (runCtrl[j].fileId == 0
              && (runCtrl[j].closeTime < g_actTime
                  || runCtrl[j].lastTime < g_actTime - 300
-                 || runCtrl[j].maxEvt <= runCtrl[j].actEvt)) {
+                 || runCtrl[j].maxEvt <= runCtrl[j].actEvt
+                 || (runCtrl[j].runId < maxStartedRun && runCtrl[j].runId != 0))) //ETIENNE added the condition at this line. dunno what to do with run 0: skipping it
+{
             if (runCtrl[j].runId == gi_myRun)
                gi_myRun = g_actTime;
