]> jspc29.x-matter.uni-frankfurt.de Git - daqdata.git/commitdiff
rfio to lustre feature added. Sergey.
authorhadaq <hadaq>
Thu, 10 Dec 2009 11:45:11 +0000 (11:45 +0000)
committerhadaq <hadaq>
Thu, 10 Dec 2009 11:45:11 +0000 (11:45 +0000)
hadaq/evtbuild.c

index 5f29ea0ca01235118e63bbd47a1064273c039309..ac2f41af883607dc0ea11a33375dfab48e47fdbb 100644 (file)
@@ -1,10 +1,8 @@
-static char *rcsId =
-  "$Header: /misc/hadesprojects/daq/cvsroot/eventbuilder/hadaq/evtbuild.c,v 6.94 2008-10-06 12:50:14 hadaq Exp $";
-
+static char *rcsId = "$Header: /misc/hadesprojects/daq/cvsroot/eventbuilder/hadaq/evtbuild.c,v 6.95 2009-12-10 11:45:11 hadaq Exp $";
 
 #define _POSIX_C_SOURCE 199309L
 #define SYSLOG_NAMES
-/* #define RFIO */
+#define RFIO 1
 
 #include <unistd.h>
 #include <getopt.h>
@@ -38,19 +36,19 @@ static char *rcsId =
 #include "genid32.h"
 
 #ifdef RFIO
-#include "rawapin.h"              /* for rfio */
+#include "rawapin.h"                   /* for rfio */
 static RFILE *fRemote = NULL;
 #endif
 
 #define MAXINPATH 100
 
-#define NEVTIDS 64UL           /* must be 2^n */
-#define NEVTIDS_IN_FILE 0UL    /* must be 2^n */
+#define NEVTIDS 64UL                   /* must be 2^n */
+#define NEVTIDS_IN_FILE 0UL            /* must be 2^n */
 #define EVENT_NUM_OFFSET 100
 #define DEBUG2 0
-#define CHECK_MISMATCH 1
+#define CHECK_MISMATCH 0
 #define BEAM 1
-#define TIMEOFFSET 1200000000   /* needed to reconstruct time from runId */
+#define TIMEOFFSET 1200000000  /* needed to reconstruct time from runId */
 static FILE *outFile;
 static FILE *outLustreFile;
 static FILE *outSecondFile;
@@ -59,7 +57,7 @@ static AnsiTape *outTape;
 static uint32_t seqNr;
 static uint32_t res_seqNr;
 static uint32_t runNr;
-static uint32_t newRunId;  /* needed to get new RUN id from epics ctrl */
+static uint32_t newRunId;              /* needed to get new RUN id from epics ctrl */
 static time_t ourTime;
 static long file_size;
 static long res_file_size;
@@ -70,1936 +68,1817 @@ static time_t res_time;
 static int diff_time;
 static int trig_mismatch;
 
-typedef struct TheArgsS
-{
-  unsigned long nrOfMsgs;
-  char outPath[PARAM_MAX_VALUE_LEN];
-  char outDev[PARAM_MAX_VALUE_LEN];
-  unsigned long runNr;
-  char expId[PARAM_MAX_VALUE_LEN];
-  char *slowCtrlFiles[PARAM_MAX_ARRAY_LEN];
-  char slowCtrlFilesS[PARAM_MAX_ARRAY_LEN][PARAM_MAX_NAME_LEN];
-  int slowCtrlFileCnt;
-  unsigned long isStandalone;
-  unsigned long priority;
-  unsigned long queueSize;
-  char verbosity[PARAM_MAX_VALUE_LEN];
-  unsigned long evtId;
-  unsigned long maxFileSz;
-  unsigned short no_rpc;
-  unsigned int resdownscale;
-  unsigned int resnumevents;
-  char respath[PARAM_MAX_VALUE_LEN];
-  unsigned short write_data;
-  char sec_path[PARAM_MAX_VALUE_LEN];
-  unsigned int ressizelimit;
-  double secsizelimit;
-  double resdown_offset;
-
-  /* the following arguments are for the variable queue size. S.Y. */
-  unsigned long varQSize[MAXINPATH];
-  int varQSizeCnt;
-  
-  char rfioRemotePath[PARAM_MAX_VALUE_LEN];
-  char lustrePath[PARAM_MAX_VALUE_LEN];
-  unsigned short buffStat;
-  unsigned short epicsCtrl;
-  unsigned short ignore;   /* ignore all trigger mismatch conditions */
-  char shmname[PARAM_MAX_VALUE_LEN];
-  unsigned int ebnum;
-  char runinfo2ora[PARAM_MAX_VALUE_LEN];
+typedef struct TheArgsS {
+       unsigned long nrOfMsgs;
+       char outPath[PARAM_MAX_VALUE_LEN];
+       char outDev[PARAM_MAX_VALUE_LEN];
+       unsigned long runNr;
+       char expId[PARAM_MAX_VALUE_LEN];
+       char *slowCtrlFiles[PARAM_MAX_ARRAY_LEN];
+       char slowCtrlFilesS[PARAM_MAX_ARRAY_LEN][PARAM_MAX_NAME_LEN];
+       int slowCtrlFileCnt;
+       unsigned long isStandalone;
+       unsigned long priority;
+       unsigned long queueSize;
+       char verbosity[PARAM_MAX_VALUE_LEN];
+       unsigned long evtId;
+       unsigned long maxFileSz;
+       unsigned short no_rpc;
+       unsigned int resdownscale;
+       unsigned int resnumevents;
+       char respath[PARAM_MAX_VALUE_LEN];
+       unsigned short write_data;
+       char sec_path[PARAM_MAX_VALUE_LEN];
+       unsigned int ressizelimit;
+       double secsizelimit;
+       double resdown_offset;
+
+       /* the following arguments are for the variable queue size. S.Y. */
+       unsigned long varQSize[MAXINPATH];
+       int varQSizeCnt;
+
+       char rfioRemotePath[PARAM_MAX_VALUE_LEN];
+       char rfioLustrePath[PARAM_MAX_VALUE_LEN];
+       char lustrePath[PARAM_MAX_VALUE_LEN];
+       unsigned short buffStat;
+       unsigned short epicsCtrl;
+       unsigned short ignore;          /* ignore all trigger mismatch conditions */
+       char shmname[PARAM_MAX_VALUE_LEN];
+       unsigned int ebnum;
+       char runinfo2ora[PARAM_MAX_VALUE_LEN];
 } TheArgs;
 
-typedef struct TheStatsS
-{
-  unsigned long *evtsDiscarded;
-  unsigned long *evtsComplete;
-  unsigned long *evtsDataError;
-  unsigned long *evtsTagError;
-  unsigned long *bytesWritten;
-  unsigned long *evtId[NEVTIDS];
-  unsigned long *trigNr[MAXINPATH];
-  unsigned long *evtsRes;
-  unsigned long *evtbuildBuff[MAXINPATH];
-  unsigned long *nrOfMsgs;
-  unsigned long *runId;
+typedef struct TheStatsS {
+       unsigned long *evtsDiscarded;
+       unsigned long *evtsComplete;
+       unsigned long *evtsDataError;
+       unsigned long *evtsTagError;
+       unsigned long *bytesWritten;
+       unsigned long *evtId[NEVTIDS];
+       unsigned long *trigNr[MAXINPATH];
+       unsigned long *evtsRes;
+       unsigned long *evtbuildBuff[MAXINPATH];
+       unsigned long *nrOfMsgs;
+       unsigned long *runId;
 } TheStats;
 
 static jmp_buf terminateJmp;
 
-void sigHandler (int sig)
+void sigHandler(int sig)
 {
-  longjmp (terminateJmp, sig);
+       longjmp(terminateJmp, sig);
 }
 
-static void *appendFile (void *my, const char *path)
+static void *appendFile(void *my, const char *path)
 {
-  void *subEvt;
-  char *dataBuf;
-  FILE *file;
-  size_t fileSize;
-
-  if (NULL == (file = fopen (path, "r")))
-    {
-      syslog (LOG_ERR, "%s, %d: %s", __FILE__, __LINE__, strerror (errno));
-      return my;
-    }
-  subEvt = newSubEvt (SubEvtDecoding_text, SubEvtId_slowTest, 0);
-
-  if (NULL == (dataBuf = malloc (strlen (path + 2))))
-    {
-      syslog (LOG_ERR, "%s, %d: %s", __FILE__, __LINE__, strerror (errno));
-      return my;
-    }
-  sprintf (dataBuf, "#%s\n", path);
-  subEvt = SubEvt_appendData (subEvt, dataBuf, strlen (dataBuf));
-  free (dataBuf);
-
-  fseek (file, 0, SEEK_END);
-  fileSize = ftell (file);
-  fseek (file, 0, SEEK_SET);
-  if (NULL == (dataBuf = malloc (fileSize)))
-    {
-      syslog (LOG_ERR, "%s, %d: %s", __FILE__, __LINE__, strerror (errno));
-      return my;
-    }
-  fread (dataBuf, 1, fileSize, file);
-
-  subEvt = SubEvt_appendData (subEvt, dataBuf, fileSize);
-  my = Evt_appendSubEvt (my, subEvt);
-
-  deleteSubEvt (subEvt);
-  free (dataBuf);
-  fclose (file);
-
-  return my;
+       void *subEvt;
+       char *dataBuf;
+       FILE *file;
+       size_t fileSize;
+
+       if (NULL == (file = fopen(path, "r"))) {
+               syslog(LOG_ERR, "%s, %d: %s", __FILE__, __LINE__, strerror(errno));
+               return my;
+       }
+       subEvt = newSubEvt(SubEvtDecoding_text, SubEvtId_slowTest, 0);
+
+       if (NULL == (dataBuf = malloc(strlen(path + 2)))) {
+               syslog(LOG_ERR, "%s, %d: %s", __FILE__, __LINE__, strerror(errno));
+               return my;
+       }
+       sprintf(dataBuf, "#%s\n", path);
+       subEvt = SubEvt_appendData(subEvt, dataBuf, strlen(dataBuf));
+       free(dataBuf);
+
+       fseek(file, 0, SEEK_END);
+       fileSize = ftell(file);
+       fseek(file, 0, SEEK_SET);
+       if (NULL == (dataBuf = malloc(fileSize))) {
+               syslog(LOG_ERR, "%s, %d: %s", __FILE__, __LINE__, strerror(errno));
+               return my;
+       }
+       fread(dataBuf, 1, fileSize, file);
+
+       subEvt = SubEvt_appendData(subEvt, dataBuf, fileSize);
+       my = Evt_appendSubEvt(my, subEvt);
+
+       deleteSubEvt(subEvt);
+       free(dataBuf);
+       fclose(file);
+
+       return my;
 }
 
-static void usage (const char *progName)
+static void usage(const char *progName)
 {
-  syslog (LOG_ERR, "Usage: %s [-x expId]", progName);
-  syslog (LOG_ERR, "Usage: [-m nrOfMsgs] [-f slowCtrlFile ...]");
-  syslog (LOG_ERR, "Usage: [-o outPath] [-d null|tape|file|stdout]");
-  syslog (LOG_ERR, "Usage: [-q queueSize] [-r runNumber]");
-  syslog (LOG_ERR, "Usage: [--shmname shmem_name] extension of shared memory name to be opened");
-  syslog (LOG_ERR, "Usage: [-a (agent)] [-p priority] [-I evtId]");
-  syslog (LOG_ERR, "Usage: [-v debug|info|notice|warning|err|alert|crit|emerg]");
-  syslog (LOG_ERR, "Usage: [--norpc]");
-  syslog (LOG_ERR, "Usage: [--filesize max_size] maximum size of output file in MB");
-  syslog (LOG_ERR, "Usage: [--resdownscale downscale_factor] downscale factor for the res events");
-  syslog (LOG_ERR, "Usage: [--resnumevents evt_num] maximum number of events in a resfile");
-  syslog (LOG_ERR, "Usage: [--respath path] path for the res directory");
-  syslog (LOG_ERR, "Usage: [--secsizelimit max_size] maximum size of second directory with the mirrored data [in MB]");
-  syslog (LOG_ERR, "Usage: [--ressizelimit max_file_num] maximum  number of files in res dir");
-  syslog (LOG_ERR, "Usage: [--write_data path] path to the directory with mirrored data");
+       syslog(LOG_ERR, "Usage: %s [-x expId]", progName);
+       syslog(LOG_ERR, "Usage: [-m nrOfMsgs] [-f slowCtrlFile ...]");
+       syslog(LOG_ERR, "Usage: [-o outPath] [-d null|tape|file|stdout]");
+       syslog(LOG_ERR, "Usage: [-q queueSize] [-r runNumber]");
+       syslog(LOG_ERR, "Usage: [--shmname shmem_name] extension of shared memory name to be opened");
+       syslog(LOG_ERR, "Usage: [-a (agent)] [-p priority] [-I evtId]");
+       syslog(LOG_ERR, "Usage: [-v debug|info|notice|warning|err|alert|crit|emerg]");
+       syslog(LOG_ERR, "Usage: [--norpc]");
+       syslog(LOG_ERR, "Usage: [--filesize max_size] maximum size of output file in MB");
+       syslog(LOG_ERR, "Usage: [--resdownscale downscale_factor] downscale factor for the res events");
+       syslog(LOG_ERR, "Usage: [--resnumevents evt_num] maximum number of events in a resfile");
+       syslog(LOG_ERR, "Usage: [--respath path] path for the res directory");
+       syslog(LOG_ERR, "Usage: [--secsizelimit max_size] maximum size of second directory with the mirrored data [in MB]");
+       syslog(LOG_ERR, "Usage: [--ressizelimit max_file_num] maximum  number of files in res dir");
+       syslog(LOG_ERR, "Usage: [--write_data path] path to the directory with mirrored data");
 #ifdef RFIO
-  syslog (LOG_ERR, "Usage: [--rfio path_to_tape_archive] example: --rfio rfiodaq:gstore:/hadaqtest/test002");
+       syslog(LOG_ERR, "Usage: [--rfio path_to_tape_archive] example: --rfio rfiodaq:gstore:/hadaqtest/test002");
+       syslog(LOG_ERR, "Usage: [--rfiolustre path] example: --rfiolustre /lustre/hades/daq");
 #endif
-  syslog (LOG_ERR, "Usage: [--buffstat] show fill levels of buffers");
-  syslog (LOG_ERR, "Usage: [--epicsctrl] enable synch and distribution of RUN Id by Epics for parallel event builders");
-  syslog (LOG_ERR, "Usage: [--lustre path_to_lustre] path to the file on the Lustre cluster");
-  syslog (LOG_ERR, "Usage: [--ebnum] number of the event builder");
-  syslog (LOG_ERR, "Usage: [--orapath path] path to eb_runinfo2ora.txt");
-  syslog (LOG_ERR, "Usage: [--ignore] ignore trigger mismatch conditions");
+       syslog(LOG_ERR, "Usage: [--buffstat] show fill levels of buffers");
+       syslog(LOG_ERR, "Usage: [--epicsctrl] enable synch and distribution of RUN Id by Epics for parallel event builders");
+       syslog(LOG_ERR, "Usage: [--lustre path_to_lustre] path to the file on the Lustre cluster");
+       syslog(LOG_ERR, "Usage: [--ebnum] number of the event builder");
+       syslog(LOG_ERR, "Usage: [--orapath path] path to eb_runinfo2ora.txt");
+       syslog(LOG_ERR, "Usage: [--ignore] ignore trigger mismatch conditions");
 }
 
-static void argsDump (TheArgs * my)
+static void argsDump(TheArgs *my)
 {
-  int i;
-
-  syslog (LOG_DEBUG, "nrOfMsgs: %d", my->nrOfMsgs);
-  for (i = 0; i < my->slowCtrlFileCnt; i++) {
-    syslog (LOG_DEBUG, "slowCtrlFiles[%d]: %s", i, my->slowCtrlFiles[i]);
-  }
-  syslog (LOG_DEBUG, "outPath: %s", my->outPath);
-  syslog (LOG_DEBUG, "outDev: %s", my->outDev);
-  syslog (LOG_DEBUG, "runNr: %d", my->runNr);
-  syslog (LOG_DEBUG, "expId: %s", my->expId);
-  syslog (LOG_DEBUG, "priority: %d", my->priority);
-  syslog (LOG_DEBUG, "isStandalone: %d", my->isStandalone);
-  syslog (LOG_DEBUG, "queueSize: %d", my->queueSize);
-  syslog (LOG_DEBUG, "verbosity: %s", my->verbosity);
-  syslog (LOG_DEBUG, "evtId: %ld", my->evtId);
-  syslog (LOG_DEBUG, "maxFileSz: %ld", my->maxFileSz);
-  if( strcmp( my->shmname, "" ) != 0 ) {
-      syslog (LOG_DEBUG, "shmem name: %s", my->shmname);
-  }
-  if (my->resdownscale != 0) {
-      syslog (LOG_DEBUG, "resdownscale: %ld", my->resdownscale);
-      syslog (LOG_DEBUG, "resnumevents: %ld", my->resnumevents);
-      syslog (LOG_DEBUG, "respath: %s", my->respath);
-      syslog (LOG_DEBUG, "secsizelimit: %ld", my->secsizelimit);
-      syslog (LOG_DEBUG, "ressizelimit: %d", my->ressizelimit);
-  }
-  if (my->no_rpc == 1) {
-      syslog (LOG_DEBUG, "no rpc is set");
-  }
-  if (my->write_data == 1) {
-      syslog (LOG_DEBUG, "sec_path: %s", my->sec_path);
-  }
-  if( strcmp( my->rfioRemotePath, "" ) != 0 ) {
-      syslog (LOG_DEBUG, "rfio path: %s", my->rfioRemotePath);
-  }
-  if( strcmp( my->lustrePath, "" ) != 0 ) {
-      syslog (LOG_DEBUG, "lustre path: %s", my->lustrePath);
-  }
-  syslog (LOG_DEBUG, "EB numder: %d", my->ebnum);
+       int i;
+
+       syslog(LOG_DEBUG, "nrOfMsgs: %d", my->nrOfMsgs);
+       for (i = 0; i < my->slowCtrlFileCnt; i++) {
+               syslog(LOG_DEBUG, "slowCtrlFiles[%d]: %s", i, my->slowCtrlFiles[i]);
+       }
+       syslog(LOG_DEBUG, "outPath: %s", my->outPath);
+       syslog(LOG_DEBUG, "outDev: %s", my->outDev);
+       syslog(LOG_DEBUG, "runNr: %d", my->runNr);
+       syslog(LOG_DEBUG, "expId: %s", my->expId);
+       syslog(LOG_DEBUG, "priority: %d", my->priority);
+       syslog(LOG_DEBUG, "isStandalone: %d", my->isStandalone);
+       syslog(LOG_DEBUG, "queueSize: %d", my->queueSize);
+       syslog(LOG_DEBUG, "verbosity: %s", my->verbosity);
+       syslog(LOG_DEBUG, "evtId: %ld", my->evtId);
+       syslog(LOG_DEBUG, "maxFileSz: %ld", my->maxFileSz);
+       if (strcmp(my->shmname, "") != 0) {
+               syslog(LOG_DEBUG, "shmem name: %s", my->shmname);
+       }
+       if (my->resdownscale != 0) {
+               syslog(LOG_DEBUG, "resdownscale: %ld", my->resdownscale);
+               syslog(LOG_DEBUG, "resnumevents: %ld", my->resnumevents);
+               syslog(LOG_DEBUG, "respath: %s", my->respath);
+               syslog(LOG_DEBUG, "secsizelimit: %ld", my->secsizelimit);
+               syslog(LOG_DEBUG, "ressizelimit: %d", my->ressizelimit);
+       }
+       if (my->no_rpc == 1) {
+               syslog(LOG_DEBUG, "no rpc is set");
+       }
+       if (my->write_data == 1) {
+               syslog(LOG_DEBUG, "sec_path: %s", my->sec_path);
+       }
+       if (strcmp(my->rfioRemotePath, "") != 0) {
+               syslog(LOG_DEBUG, "rfio path: %s", my->rfioRemotePath);
+       }
+       if (strcmp(my->rfioLustrePath, "") != 0) {
+               syslog(LOG_DEBUG, "rfio lustre path: %s", my->rfioLustrePath);
+       }
+       if (strcmp(my->lustrePath, "") != 0) {
+               syslog(LOG_DEBUG, "lustre path: %s", my->lustrePath);
+       }
+       syslog(LOG_DEBUG, "EB numder: %d", my->ebnum);
 }
 
-static void argsDefault (TheArgs * my)
+static void argsDefault(TheArgs *my)
 {
-  int i;
-
-  my->nrOfMsgs = 0;
-  for (i = 0; i < PARAM_MAX_ARRAY_LEN; i++)
-    {
-      my->slowCtrlFiles[i] = my->slowCtrlFilesS[i];
-    }
-  my->slowCtrlFileCnt = 0;
-  strcpy (my->outPath, "");
-  strcpy (my->outDev, "null");
-  strcpy (my->expId, "xx");
-  strcpy (my->shmname, "");
-  my->priority = 0;
-  my->isStandalone = 1;
-  my->queueSize = 4 * 1024 * 1024UL;
-  strcpy (my->verbosity, "info");
-  my->evtId = 0;
-  my->maxFileSz = (1.5 * 1024 * 1024 * 1024UL - 1);
-  my->no_rpc = 0;
-  my->write_data = 0;
-  my->resdownscale = 0;
-  my->resdown_offset = 0;
-  my->resnumevents = -1;
-  my->secsizelimit = 0.;
-  my->ressizelimit = 0;
-  strcpy (my->respath, "");
-
-  for (i = 0; i < MAXINPATH; i++) {
-    my->varQSize[i] = 4 * 1024 * 1024UL;
-  }
-
-  strcpy (my->rfioRemotePath, "");
-  strcpy (my->lustrePath, "");
-  my->buffStat  = 0;
-  my->epicsCtrl = 0;
-  my->ebnum     = 1;
-  my->ignore    = 0;
-
-  /* read path from DAQ_SETUP and define full file name */
-  sprintf(my->runinfo2ora,"%s_runinfo2ora.txt",getenv("DAQ_SETUP"));
+       int i;
+
+       my->nrOfMsgs = 0;
+       for (i = 0; i < PARAM_MAX_ARRAY_LEN; i++) {
+               my->slowCtrlFiles[i] = my->slowCtrlFilesS[i];
+       }
+       my->slowCtrlFileCnt = 0;
+       strcpy(my->outPath, "");
+       strcpy(my->outDev, "null");
+       strcpy(my->expId, "xx");
+       strcpy(my->shmname, "");
+       my->priority = 0;
+       my->isStandalone = 1;
+       my->queueSize = 4 * 1024 * 1024UL;
+       strcpy(my->verbosity, "info");
+       my->evtId = 0;
+       my->maxFileSz = (1.5 * 1024 * 1024 * 1024UL - 1);
+       my->no_rpc = 0;
+       my->write_data = 0;
+       my->resdownscale = 0;
+       my->resdown_offset = 0;
+       my->resnumevents = -1;
+       my->secsizelimit = 0.;
+       my->ressizelimit = 0;
+       strcpy(my->respath, "");
+
+       for (i = 0; i < MAXINPATH; i++) {
+               my->varQSize[i] = 4 * 1024 * 1024UL;
+       }
+
+       strcpy(my->rfioRemotePath, "");
+       strcpy(my->rfioLustrePath, "");
+       strcpy(my->lustrePath, "");
+       my->buffStat = 0;
+       my->epicsCtrl = 0;
+       my->ebnum = 1;
+       my->ignore = 0;
+
+       /* read path from DAQ_SETUP and define full file name */
+       sprintf(my->runinfo2ora, "%s_runinfo2ora.txt", getenv("DAQ_SETUP"));
 }
 
-static int argsFromCL (TheArgs * my, int argc, char *argv[])
+static int argsFromCL(TheArgs *my, int argc, char *argv[])
 {
-  extern char *optarg;
-  int i;
-  while (1)
-    {
-      int this_option_optind = optind ? optind : 1;
-      int option_index = 0;
-
-      static struct option long_options[] = {
-       {"norpc",        0, 0, 't'},
-       {"filesize",     1, 0, 'z'},
-       {"resdownscale", 1, 0, 'e'},
-       {"resnumevents", 1, 0, 'n'},
-       {"respath",      1, 0, 'h'},
-       {"secsizelimit", 1, 0, 'l'},
-       {"ressizelimit", 1, 0, 's'},
-       {"write_data",   1, 0, 'w'},
-       {"help",         0, 0, 'H'},
-       {"rfio",         1, 0, 'R'},
-       {"buffstat",     0, 0, 'b'},
-       {"epicsctrl",    0, 0, 'E'},
-       {"lustre",       1, 0, 'L'},
-       {"shmname",      1, 0, 'S'},
-       {"ebnum",        1, 0, 'B'},
-       {"orapath",      1, 0, 'O'},
-       {"ignore",       0, 0, 'i'},
-       {0, 0, 0, 0}
-      };
-      i = getopt_long (argc, argv,
-                    "am:f:r:o:d:q:p:v:x:I:tz:e:n:h:w:tz:e:n:Hs:l:R:bEL:S:B:O:i",
-                    long_options, &option_index);
-      if (i == -1)
-       break;
-      switch (i)
-       {
-       case 'm':
-         my->nrOfMsgs = strtoul (optarg, NULL, 0);
-         break;
-       case 'f':
-         strcpy (my->slowCtrlFiles[my->slowCtrlFileCnt++], optarg);
-         break;
-       case 'o':
-         strcpy (my->outPath, optarg);
-         break;
-       case 'd':
-         strcpy (my->outDev, optarg);
-         break;
-       case 'x':
-         strcpy (my->expId, optarg);
-         break;
-       case 'a':
-         my->isStandalone = 0;
-         break;
-       case 'p':
-         my->priority = strtoul (optarg, NULL, 0);
-         break;
-       case 'q':
-         my->queueSize = strtoul (optarg, NULL, 0);
-         break;
-       case 'v':
-         strcpy (my->verbosity, optarg);
-         break;
-       case 'I':
-         my->evtId = strtoul (optarg, NULL, 0);
-         break;
-       case 't':                           /* norpc - no arg */
-         my->no_rpc = 1;
-         break;
-       case 'z':                           /* from MBytes to Bytes */
-         my->maxFileSz = (1024 * 1024UL * strtoul( optarg, NULL, 0 ));
-         break;
-       case 'e':                           /* need resnumevents & respath */
-         my->resdownscale = strtoul (optarg, NULL, 0);
-         if(my->resdownscale > 0)
-           my->resdown_offset = EVENT_NUM_OFFSET / (my->resdownscale);
-         break;
-       case 'n':               
-         my->resnumevents = strtoul (optarg, NULL, 0);
-         break;
-       case 'l':         
-         my->secsizelimit = (double) strtoul (optarg, NULL,0);
-         break;
-       case 's':          
-         my->ressizelimit = strtoul (optarg, NULL, 0);
-         break;
-       case 'h':           
-         strcpy (my->respath, optarg);
-         break;
-       case 'w':            
-         my->write_data = 1;
-         strcpy (my->sec_path, optarg);
-         break;
-       case 'R':
-         strcpy( my->rfioRemotePath, optarg );
-         break;
-       case 'L':
-         strcpy( my->lustrePath, optarg );
-         break;
-       case 'S':
-         strcpy( my->shmname, optarg );
-         break;
-       case 'b':
-         my->buffStat = 1;
-         break;
-       case 'E':
-         my->epicsCtrl = 1;
-         break;
-       case 'i':
-         my->ignore = 1;
-         break;
-       case 'B':
-         my->ebnum = strtoul (optarg, NULL, 0);
-         break;
-       case 'O':
-         sprintf(my->runinfo2ora,"%s_runinfo2ora.txt",optarg);
-         break;
-       case 'H':
-         usage (argv[0]);
-         return -1;
-         break;
-       default:
-         usage (argv[0]);
-         return -1;
-         break;
-       }
-    }
-  if (optind < argc)
-    {
-      printf ("non-option ARGV-elements: ");
-      while (optind < argc)
-       printf ("%s ", argv[optind++]);
-      printf ("\n");
-    }
-
-  return 0;
+       extern char *optarg;
+       int i;
+       while (1) {
+               int this_option_optind = optind ? optind : 1;
+               int option_index = 0;
+
+               static struct option long_options[] = {
+                       {"norpc", 0, 0, 't'},
+                       {"filesize", 1, 0, 'z'},
+                       {"resdownscale", 1, 0, 'e'},
+                       {"resnumevents", 1, 0, 'n'},
+                       {"respath", 1, 0, 'h'},
+                       {"secsizelimit", 1, 0, 'l'},
+                       {"ressizelimit", 1, 0, 's'},
+                       {"write_data", 1, 0, 'w'},
+                       {"help", 0, 0, 'H'},
+                       {"rfio", 1, 0, 'R'},
+                       {"rfiolustre", 1, 0, 'A'},
+                       {"buffstat", 0, 0, 'b'},
+                       {"epicsctrl", 0, 0, 'E'},
+                       {"lustre", 1, 0, 'L'},
+                       {"shmname", 1, 0, 'S'},
+                       {"ebnum", 1, 0, 'B'},
+                       {"orapath", 1, 0, 'O'},
+                       {"ignore", 0, 0, 'i'},
+                       {0, 0, 0, 0}
+               };
+               i = getopt_long(argc, argv, "am:f:r:o:d:q:p:v:x:I:tz:e:n:h:w:tz:e:n:Hs:l:R:A:bEL:S:B:O:i", long_options, &option_index);
+               if (i == -1)
+                       break;
+               switch (i) {
+               case 'm':
+                       my->nrOfMsgs = strtoul(optarg, NULL, 0);
+                       break;
+               case 'f':
+                       strcpy(my->slowCtrlFiles[my->slowCtrlFileCnt++], optarg);
+                       break;
+               case 'o':
+                       strcpy(my->outPath, optarg);
+                       break;
+               case 'd':
+                       strcpy(my->outDev, optarg);
+                       break;
+               case 'x':
+                       strcpy(my->expId, optarg);
+                       break;
+               case 'a':
+                       my->isStandalone = 0;
+                       break;
+               case 'p':
+                       my->priority = strtoul(optarg, NULL, 0);
+                       break;
+               case 'q':
+                       my->queueSize = strtoul(optarg, NULL, 0);
+                       break;
+               case 'v':
+                       strcpy(my->verbosity, optarg);
+                       break;
+               case 'I':
+                       my->evtId = strtoul(optarg, NULL, 0);
+                       break;
+               case 't':                               /* norpc - no arg */
+                       my->no_rpc = 1;
+                       break;
+               case 'z':                               /* from MBytes to Bytes */
+                       my->maxFileSz = (1024 * 1024UL * strtoul(optarg, NULL, 0));
+                       break;
+               case 'e':                               /* need resnumevents & respath */
+                       my->resdownscale = strtoul(optarg, NULL, 0);
+                       if (my->resdownscale > 0)
+                               my->resdown_offset = EVENT_NUM_OFFSET / (my->resdownscale);
+                       break;
+               case 'n':
+                       my->resnumevents = strtoul(optarg, NULL, 0);
+                       break;
+               case 'l':
+                       my->secsizelimit = (double) strtoul(optarg, NULL, 0);
+                       break;
+               case 's':
+                       my->ressizelimit = strtoul(optarg, NULL, 0);
+                       break;
+               case 'h':
+                       strcpy(my->respath, optarg);
+                       break;
+               case 'w':
+                       my->write_data = 1;
+                       strcpy(my->sec_path, optarg);
+                       break;
+               case 'R':
+                       strcpy(my->rfioRemotePath, optarg);
+                       break;
+               case 'A':
+                       strcpy(my->rfioLustrePath, optarg);
+                       break;
+               case 'L':
+                       strcpy(my->lustrePath, optarg);
+                       break;
+               case 'S':
+                       strcpy(my->shmname, optarg);
+                       break;
+               case 'b':
+                       my->buffStat = 1;
+                       break;
+               case 'E':
+                       my->epicsCtrl = 1;
+                       break;
+               case 'i':
+                       my->ignore = 1;
+                       break;
+               case 'B':
+                       my->ebnum = strtoul(optarg, NULL, 0);
+                       break;
+               case 'O':
+                       sprintf(my->runinfo2ora, "%s_runinfo2ora.txt", optarg);
+                       break;
+               case 'H':
+                       usage(argv[0]);
+                       return -1;
+                       break;
+               default:
+                       usage(argv[0]);
+                       return -1;
+                       break;
+               }
+       }
+       if (optind < argc) {
+               printf("non-option ARGV-elements: ");
+               while (optind < argc)
+                       printf("%s ", argv[optind++]);
+               printf("\n");
+       }
+
+       return 0;
 }
 
-static int argsCheck( TheArgs *my )
+static int argsCheck(TheArgs *my)
 {
-  /*
-   *   Check the content of TheArgs.
-   */
-
-  /* check my->maxFileSz */
-  if( (strcmp(my->outDev, "null") != 0) && (my->maxFileSz <= 0 || my->maxFileSz >= 1024 * 1024UL * 2000) ) {
-    fprintf( stderr, "<E> evtbuild.c, argsCheck(): --filesize must be >0MB and <2000MB\n");
-    return 1;
-  }
-
-  /* Conditions: if the Remote Event Server is used */
-  if ( ( my->resdownscale != 0 && ( my->resnumevents == -1 || 
-                                   (strcmp(my->respath, "") == 0) ) ) ||
-       ( my->resnumevents != -1 && ( my->resdownscale == 0 ||
-                                    (strcmp(my->respath, "") == 0) ) ) ||
-       ( (strcmp(my->respath, "") != 0) && ( my->resnumevents == -1 ||
-                                            my->resdownscale == 0 ) ) ) {
-       
-    fprintf( stderr, "<E> evtbuild.c, argsCheck(): options --resdownscale --resnumevents --respath must be specified together\n");
-    return 1;
-  }
-
-  /* check my->resdownscale */
-  if( (strcmp(my->respath, "") != 0) && 
-      ( my->resdownscale <= 0 || my->resdownscale >= 100001 ) ) { 
-    fprintf( stderr,"<E> evtbuild.c, argsCheck(): --resdownscale must be >0 and <100001\n");
-    return 1;
-  }
-
-  /* check my->resnumevents */
-  if( (strcmp(my->respath, "") != 0) && 
-      ( my->resnumevents <= 99 || my->resnumevents >= 1000000001 ) ) {
-     fprintf( stderr,"<E> evtbuild.c, argsCheck(): --resnumevents must be >99 and <1000000001\n");
-     return 1;
-  }
-
-  /* check my->secsizelimit */
-  if( (strcmp(my->respath, "") != 0) && 
-      ( my->secsizelimit <= -0.0001 || my->secsizelimit >= 0.0001 ) &&
-      ( my->secsizelimit <= 10 || my->secsizelimit >= 100001 ) ) {
-    fprintf( stderr,"<E> evtbuild.c, argsCheck(): --secsizelimit must be >10 and <100000 MB\n");
-    return 1;
-  }
-
-  /* check my->ressizelimit */
-  if( (strcmp(my->respath, "") != 0) && 
-      ( my->ressizelimit <= 5 || my->ressizelimit >= 1000 ) ) {
-    fprintf( stderr, "<E> evtbuild.c, argsCheck(): --ressizelimit must be >5 and <1000\n");
-    return 1;
-  }
-
-  if (((my->maxFileSz) < (my->queueSize))) {
-    fprintf( stderr, "<E> evtbuild.c, argsCheck(): --filesize must be larger than queuesize(-q)\n");
-    return 1;
-  }
-
-  /* the condition ressizelimit has to be together with respath */
-  if ((my->ressizelimit) != 0 && (strcmp(my->respath, "") == 0) ) {
-    fprintf( stderr, "<E> evtbuild.c, argsCheck(): --respath is not given\n" );
-    return 1;
-  }
-  
-  return 0;
+       /*
+        *   Check the content of TheArgs.
+        */
+
+       /* check my->maxFileSz */
+       if ((strcmp(my->outDev, "null") != 0) && (my->maxFileSz <= 0 || my->maxFileSz >= 1024 * 1024UL * 2000)) {
+               fprintf(stderr, "<E> evtbuild.c, argsCheck(): --filesize must be >0MB and <2000MB\n");
+               return 1;
+       }
+
+       /* Conditions: if the Remote Event Server is used */
+       if ((my->resdownscale != 0 && (my->resnumevents == -1 ||
+                                                                  (strcmp(my->respath, "") == 0))) ||
+               (my->resnumevents != -1 && (my->resdownscale == 0 ||
+                                                                       (strcmp(my->respath, "") == 0))) ||
+               ((strcmp(my->respath, "") != 0) && (my->resnumevents == -1 || my->resdownscale == 0))) {
+
+               fprintf(stderr,
+                               "<E> evtbuild.c, argsCheck(): options --resdownscale --resnumevents --respath must be specified together\n");
+               return 1;
+       }
+
+       /* check my->resdownscale */
+       if ((strcmp(my->respath, "") != 0) && (my->resdownscale <= 0 || my->resdownscale >= 100001)) {
+               fprintf(stderr, "<E> evtbuild.c, argsCheck(): --resdownscale must be >0 and <100001\n");
+               return 1;
+       }
+
+       /* check my->resnumevents */
+       if ((strcmp(my->respath, "") != 0) && (my->resnumevents <= 99 || my->resnumevents >= 1000000001)) {
+               fprintf(stderr, "<E> evtbuild.c, argsCheck(): --resnumevents must be >99 and <1000000001\n");
+               return 1;
+       }
+
+       /* check my->secsizelimit */
+       if ((strcmp(my->respath, "") != 0) &&
+               (my->secsizelimit <= -0.0001 || my->secsizelimit >= 0.0001) && (my->secsizelimit <= 10 || my->secsizelimit >= 100001)) {
+               fprintf(stderr, "<E> evtbuild.c, argsCheck(): --secsizelimit must be >10 and <100000 MB\n");
+               return 1;
+       }
+
+       /* check my->ressizelimit */
+       if ((strcmp(my->respath, "") != 0) && (my->ressizelimit <= 5 || my->ressizelimit >= 1000)) {
+               fprintf(stderr, "<E> evtbuild.c, argsCheck(): --ressizelimit must be >5 and <1000\n");
+               return 1;
+       }
+
+       if (((my->maxFileSz) < (my->queueSize))) {
+               fprintf(stderr, "<E> evtbuild.c, argsCheck(): --filesize must be larger than queuesize(-q)\n");
+               return 1;
+       }
+
+       /* the condition ressizelimit has to be together with respath */
+       if ((my->ressizelimit) != 0 && (strcmp(my->respath, "") == 0)) {
+               fprintf(stderr, "<E> evtbuild.c, argsCheck(): --respath is not given\n");
+               return 1;
+       }
+
+       return 0;
 }
 
-static int argsFromParam (TheArgs *my, int argc, char *argv[])
+static int argsFromParam(TheArgs *my, int argc, char *argv[])
 {
-  Param paramS, *param = &paramS;
-  int paramWasFound;
-  char *name;
-
-  conSetupParam (param, getenv ("DAQ_SETUP"));
-
-  name = (char *) basename (argv[0]);
-
-  Param_getInt    (param, name, "nrofmsgs", &paramWasFound, &my->nrOfMsgs);
-  Param_getStringArray (param, name, "slwctrlfile", PARAM_MAX_ARRAY_LEN,
-                       &my->slowCtrlFileCnt, my->slowCtrlFiles);
-  Param_getString (param, name, "outpath", &paramWasFound, my->outPath);
-  Param_getString (param, name, "outdev",  &paramWasFound, my->outDev);
-  Param_getString (param, name, "expid",   &paramWasFound, my->expId);
-  Param_getInt    (param, name, "stndln",  &paramWasFound, &my->isStandalone);
-  Param_getInt    (param, name, "prio",    &paramWasFound, &my->priority);
-  Param_getInt    (param, name, "qsize",   &paramWasFound, &my->queueSize);
-  Param_getString (param, name, "verb",    &paramWasFound, my->verbosity);
-  Param_getInt    (param, name, "evtid",   &paramWasFound, &my->evtId);
-  Param_getInt    (param, name, "maxfilesz", &paramWasFound, &my->maxFileSz);
-
-  Param_getIntArray(param, name, "varqsize", MAXINPATH, &my->varQSizeCnt, my->varQSize);
-  desParam (param);
+       Param paramS, *param = &paramS;
+       int paramWasFound;
+       char *name;
+
+       conSetupParam(param, getenv("DAQ_SETUP"));
+
+       name = (char *) basename(argv[0]);
+
+       Param_getInt(param, name, "nrofmsgs", &paramWasFound, &my->nrOfMsgs);
+       Param_getStringArray(param, name, "slwctrlfile", PARAM_MAX_ARRAY_LEN, &my->slowCtrlFileCnt, my->slowCtrlFiles);
+       Param_getString(param, name, "outpath", &paramWasFound, my->outPath);
+       Param_getString(param, name, "outdev", &paramWasFound, my->outDev);
+       Param_getString(param, name, "expid", &paramWasFound, my->expId);
+       Param_getInt(param, name, "stndln", &paramWasFound, &my->isStandalone);
+       Param_getInt(param, name, "prio", &paramWasFound, &my->priority);
+       Param_getInt(param, name, "qsize", &paramWasFound, &my->queueSize);
+       Param_getString(param, name, "verb", &paramWasFound, my->verbosity);
+       Param_getInt(param, name, "evtid", &paramWasFound, &my->evtId);
+       Param_getInt(param, name, "maxfilesz", &paramWasFound, &my->maxFileSz);
+
+       Param_getIntArray(param, name, "varqsize", MAXINPATH, &my->varQSizeCnt, my->varQSize);
+       desParam(param);
 }
 
-static char *unit (unsigned long v)
+static char *unit(unsigned long v)
 {
-  static char retVal[6];
-  static char u[] = " kM";
-  int i;
+       static char retVal[6];
+       static char u[] = " kM";
+       int i;
 
-  for (i = 0; v >= 10000 && i < sizeof (u) - 2; v /= 1000, i++) {}
-  sprintf (retVal, "%4d%c", v, u[i]);
+       for (i = 0; v >= 10000 && i < sizeof(u) - 2; v /= 1000, i++) {
+       }
+       sprintf(retVal, "%4d%c", v, u[i]);
 
-  return retVal;
+       return retVal;
 }
 
 static void printTime()
 {
-  struct timeval tv;
-  struct tm* ptm;
-  char time_string[40];
-  long milliseconds;
+       struct timeval tv;
+       struct tm *ptm;
+       char time_string[40];
+       long milliseconds;
 
-  gettimeofday( &tv, NULL );
+       gettimeofday(&tv, NULL);
 
-  /* Obtain the time of day, and convert it to a tm struct. */
-  ptm = localtime (&tv.tv_sec);
+       /* Obtain the time of day, and convert it to a tm struct. */
+       ptm = localtime(&tv.tv_sec);
 
-  /* Format the date and time, down to a single second. */
-  strftime (time_string, sizeof (time_string), "%Y-%m-%d %H:%M:%S", ptm);
+       /* Format the date and time, down to a single second. */
+       strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ptm);
 
-  /* Compute milliseconds from microseconds. */
-  milliseconds = tv.tv_usec / 1000;
+       /* Compute milliseconds from microseconds. */
+       milliseconds = tv.tv_usec / 1000;
 
-  /* Print the formatted time, in seconds, followed by a decimal point
-     and the milliseconds. */
-  printf ("Time: %s.%03ld\n", time_string, milliseconds);
+       /* Print the formatted time, in seconds, followed by a decimal point
+          and the milliseconds. */
+       printf("Time: %s.%03ld\n", time_string, milliseconds);
 }
 
-static void add2Stat( TheArgs * theArgs, TheStats * my, float interval, ShmTrans **shmtr ) 
+static void add2Stat(TheArgs *theArgs, TheStats *my, float interval, ShmTrans **shmtr)
 {
-  /* Add statistic for fill levels of buffers in percentage. */
-
-  static time_t t_0 = 0;
-  float buffSize, queueSize;
-  time_t t, dT;
-  int i;
-  unsigned long fillLevel;
-
-  t = time (NULL);
-  dT = t - t_0;
-  
-  if( dT >= interval ) {    
-    for( i=0; i<theArgs->nrOfMsgs; i++ ) {
-      buffSize  = 2*theArgs->varQSize[i];
-      queueSize = HadTuQueue_size(shmtr[i]->rdQueue);
-      
-      /* Add here statistic for fill levels of buffers */
-      fillLevel = (unsigned long) (100*queueSize+0.5)/buffSize;
-      (*my->evtbuildBuff[i]) = fillLevel;
-    }
-  }
-
-  t_0 = t;
-}
+       /* Add statistic for fill levels of buffers in percentage. */
 
-static void statsBufferDump (TheArgs *theArgs, TheStats *my, float interval, 
-                            HadTuQueue **htuq, ShmTrans **shmtr, char *progName)
-{
-  static unsigned long lastEC2;
-  static unsigned long lastBW2;
-
-  static time_t t0 = 0;
-  time_t t, dT;
-  int i, j;
-  int col = 0;
-  char emptybuffer[] = "-";
-  int outputGraph = 1;
-  int outputNum = 0;
-
-  if (theArgs->isStandalone && theArgs->buffStat) {
-
-    t = time (NULL);
-    dT = t - t0;
-    
-    if (dT >= interval) {
-      
-      if( outputNum == 1) {
-       fputs("==============================================================================\n\n",stderr);
-       
-       for(i=0; i<theArgs->nrOfMsgs; i++) {
-         
-         fprintf (stderr, "q[%2d]: ", i); 
-         if (!HadTuQueue_empty(shmtr[i]->rdQueue))
-           fprintf (stderr, "%8d ", HadTuQueue_size(shmtr[i]->rdQueue));
-         else
-           fprintf (stderr, "%8s ", emptybuffer); 
-         
-         col++;
-         if (col == 6) {
-           fputc ('\n', stderr);
-           col = 0;
-         }
-       }
-      }
-      if( outputGraph == 1 ){
-       fputs ("------------------ buffer fill levels ----------------------------------------\n", stderr);
-       
+       static time_t t_0 = 0;
        float buffSize, queueSize;
-       int maxnorm = 10.;
-       
-       for( j=0; j<maxnorm; j++ ){
-         fprintf (stderr, "%1d ", maxnorm - j - 1); 
-         for( i=0; i<theArgs->nrOfMsgs; i++ ){
-           buffSize  = 2*theArgs->varQSize[i];
-           queueSize = HadTuQueue_size(shmtr[i]->rdQueue);
-           
-           if(maxnorm - maxnorm*queueSize/buffSize < j){
-             if (!HadTuQueue_empty(shmtr[i]->rdQueue)) 
-               fputc( '|', stderr );
-             else
-               fputc( '-', stderr );
-             
-           }
-           else{
-             fputc( ' ', stderr );
-           }
-         }
-         fputc( '\n', stderr );
-       }
-       
-       /* The following is just to print the numbers of buffers*/
-       int factor, mod;
-       
-       fputs( "q:", stderr );
-       factor = 0;
-       for( i=0; i<theArgs->nrOfMsgs; i++ ) {
-         mod = i%10;
-         fprintf (stderr, "%1d", mod);
-       }
-       fputc( '\n', stderr );
-       
-       fputs( "  ", stderr );
-       for( i=0; i<theArgs->nrOfMsgs; i++ ) {
-         mod = i%10;
-         if( mod == 0 )
-           fprintf (stderr, "%1d", i/10 );
-         else
-           fputc( ' ', stderr );
-       }
-       fputc( '\n', stderr );
-       fputs( "------------------------------------------------------------------------------\n", stderr );
-       
-       /* Print Trigger Numbers for all queues */
-       char trigNum[theArgs->nrOfMsgs][10];
-       
-       for( i=0; i < theArgs->nrOfMsgs; i++ ) {
-         sprintf( trigNum[i], "%08x", *my->trigNr[i] );
-       }
-       
-       for( j=0; j < 8; j++ ) {
-         fputc (' ', stderr);
-         fputc (' ', stderr);
-         for( i=0; i < theArgs->nrOfMsgs; i++ ) {
-           fprintf (stderr, "%c", trigNum[i][j]);
-         }
-         fputc ('\n', stderr);
-       }
-       fputs( "------------------------------------------------------------------------------\n", stderr );
-       
-       fprintf (stderr, "%7s:%6s", "evtComp",
-                unit (*my->evtsComplete));
-       if( dT > 0 ) 
-         fprintf (stderr, "%10s:%6s", " evtComp/s",
-                  unit ((*my->evtsComplete - lastEC2) / dT));
-       fprintf (stderr, "%10s:%6s", " bytesWrit",
-                unit (*my->bytesWritten));
-       if( dT > 0 )
-         fprintf (stderr, "%12s:%6s", " bytesWrit/s",
-                  unit ((*my->bytesWritten - lastBW2) / dT));
-       fputc ('\n', stderr);
-       fprintf (stderr, "%7s:%6s", "evtDisc",
-                unit (*my->evtsDiscarded));
-       fprintf (stderr, "%10s:%6s", " evtDatErr",
-                unit (*my->evtsDataError));
-       fprintf (stderr, "%10s:%6s", " evtTagErr",
-                unit (*my->evtsTagError));
-       
-       fputc ('\n', stderr);
-       
-       lastEC2 = *my->evtsComplete;
-       lastBW2 = *my->bytesWritten;
-       
-       /* get wall-clock time */
-       printTime();
-
-       unsigned long runId2print;
-       char buf[_POSIX_PATH_MAX];
-       sprintf( buf, "%s%s", progName, theArgs->shmname );
-       Worker_getStatistic( buf, "runId", &runId2print);
-       printf("ioc: RUN Id = %lu  RUN Nr = %lu\n", runId2print, runNr);
-      }
-    }
-    
-    t0 = t;
-  }
+       time_t t, dT;
+       int i;
+       unsigned long fillLevel;
+
+       t = time(NULL);
+       dT = t - t_0;
+
+       if (dT >= interval) {
+               for (i = 0; i < theArgs->nrOfMsgs; i++) {
+                       buffSize = 2 * theArgs->varQSize[i];
+                       queueSize = HadTuQueue_size(shmtr[i]->rdQueue);
+
+                       /* Add here statistic for fill levels of buffers */
+                       fillLevel = (unsigned long) (100 * queueSize + 0.5) / buffSize;
+                       (*my->evtbuildBuff[i]) = fillLevel;
+               }
+       }
+
+       t_0 = t;
 }
 
-unsigned long getRunId( TheArgs *my )
+static void statsBufferDump(TheArgs *theArgs, TheStats *my, float interval, HadTuQueue **htuq, ShmTrans **shmtr, char *progName)
 {
-  unsigned long myRunId = 0;
-  struct timespec tv = { 0, 1e+8 };
-
-  while( my->epicsCtrl && myRunId == 0 ) {
-    if( Worker_getStatistic( "daq_evtbuild", "runId", &myRunId) == -1 ) {
-      fprintf( stderr, "<E> evtbuild.c: getRunId: Worker_getStatistic: cannot get runId!" );
-      sleep(1);
-    }
-    else {
-
-      if( myRunId == 0 ) 
-       nanosleep( &tv, NULL );
-    }
-    
-  }
-
-  return myRunId;
+       static unsigned long lastEC2;
+       static unsigned long lastBW2;
+
+       static time_t t0 = 0;
+       time_t t, dT;
+       int i, j;
+       int col = 0;
+       char emptybuffer[] = "-";
+       int outputGraph = 1;
+       int outputNum = 0;
+
+       if (theArgs->isStandalone && theArgs->buffStat) {
+
+               t = time(NULL);
+               dT = t - t0;
+
+               if (dT >= interval) {
+
+                       if (outputNum == 1) {
+                               fputs("==============================================================================\n\n", stderr);
+
+                               for (i = 0; i < theArgs->nrOfMsgs; i++) {
+
+                                       fprintf(stderr, "q[%2d]: ", i);
+                                       if (!HadTuQueue_empty(shmtr[i]->rdQueue))
+                                               fprintf(stderr, "%8d ", HadTuQueue_size(shmtr[i]->rdQueue));
+                                       else
+                                               fprintf(stderr, "%8s ", emptybuffer);
+
+                                       col++;
+                                       if (col == 6) {
+                                               fputc('\n', stderr);
+                                               col = 0;
+                                       }
+                               }
+                       }
+                       if (outputGraph == 1) {
+                               fputs("------------------ buffer fill levels ----------------------------------------\n", stderr);
+
+                               float buffSize, queueSize;
+                               int maxnorm = 10.;
+
+                               for (j = 0; j < maxnorm; j++) {
+                                       fprintf(stderr, "%1d ", maxnorm - j - 1);
+                                       for (i = 0; i < theArgs->nrOfMsgs; i++) {
+                                               buffSize = 2 * theArgs->varQSize[i];
+                                               queueSize = HadTuQueue_size(shmtr[i]->rdQueue);
+
+                                               if (maxnorm - maxnorm * queueSize / buffSize < j) {
+                                                       if (!HadTuQueue_empty(shmtr[i]->rdQueue))
+                                                               fputc('|', stderr);
+                                                       else
+                                                               fputc('-', stderr);
+
+                                               } else {
+                                                       fputc(' ', stderr);
+                                               }
+                                       }
+                                       fputc('\n', stderr);
+                               }
+
+                               /* The following is just to print the numbers of buffers */
+                               int factor, mod;
+
+                               fputs("q:", stderr);
+                               factor = 0;
+                               for (i = 0; i < theArgs->nrOfMsgs; i++) {
+                                       mod = i % 10;
+                                       fprintf(stderr, "%1d", mod);
+                               }
+                               fputc('\n', stderr);
+
+                               fputs("  ", stderr);
+                               for (i = 0; i < theArgs->nrOfMsgs; i++) {
+                                       mod = i % 10;
+                                       if (mod == 0)
+                                               fprintf(stderr, "%1d", i / 10);
+                                       else
+                                               fputc(' ', stderr);
+                               }
+                               fputc('\n', stderr);
+                               fputs("------------------------------------------------------------------------------\n", stderr);
+
+                               /* Print Trigger Numbers for all queues */
+                               char trigNum[theArgs->nrOfMsgs][10];
+
+                               for (i = 0; i < theArgs->nrOfMsgs; i++) {
+                                       sprintf(trigNum[i], "%08x", *my->trigNr[i]);
+                               }
+
+                               for (j = 0; j < 8; j++) {
+                                       fputc(' ', stderr);
+                                       fputc(' ', stderr);
+                                       for (i = 0; i < theArgs->nrOfMsgs; i++) {
+                                               fprintf(stderr, "%c", trigNum[i][j]);
+                                       }
+                                       fputc('\n', stderr);
+                               }
+                               fputs("------------------------------------------------------------------------------\n", stderr);
+
+                               fprintf(stderr, "%7s:%6s", "evtComp", unit(*my->evtsComplete));
+                               if (dT > 0)
+                                       fprintf(stderr, "%10s:%6s", " evtComp/s", unit((*my->evtsComplete - lastEC2) / dT));
+                               fprintf(stderr, "%10s:%6s", " bytesWrit", unit(*my->bytesWritten));
+                               if (dT > 0)
+                                       fprintf(stderr, "%12s:%6s", " bytesWrit/s", unit((*my->bytesWritten - lastBW2) / dT));
+                               fputc('\n', stderr);
+                               fprintf(stderr, "%7s:%6s", "evtDisc", unit(*my->evtsDiscarded));
+                               fprintf(stderr, "%10s:%6s", " evtDatErr", unit(*my->evtsDataError));
+                               fprintf(stderr, "%10s:%6s", " evtTagErr", unit(*my->evtsTagError));
+
+                               fputc('\n', stderr);
+
+                               lastEC2 = *my->evtsComplete;
+                               lastBW2 = *my->bytesWritten;
+
+                               /* get wall-clock time */
+                               printTime();
+
+                               unsigned long runId2print;
+                               char buf[_POSIX_PATH_MAX];
+                               sprintf(buf, "%s%s", progName, theArgs->shmname);
+                               Worker_getStatistic(buf, "runId", &runId2print);
+                               printf("ioc: RUN Id = %lu  RUN Nr = %lu\n", runId2print, runNr);
+                       }
+               }
+
+               t0 = t;
+       }
 }
 
-static void statsDump (TheArgs * theArgs, TheStats * my, int interval)
+unsigned long getRunId(TheArgs *my)
 {
-  static unsigned long lastEC;
-  static unsigned long lastEE;
-  static unsigned long lastTE;
-  static unsigned long lastED;
-  static unsigned long lastBW;
-  static time_t t0 = 0;
-  time_t t, dT;
-  int i;
-
-  if (theArgs->isStandalone && strcmp (theArgs->verbosity, "info") == 0 &&
-      !(theArgs->buffStat))
-    {
-      t = time (NULL);
-      dT = t - t0;
-      if (dT >= interval)
-       {
-         int col = 0;
-
-         fputs
-           ("==============================================================================\n",
-            stderr);
-         fprintf (stderr, "%19s:%6s", "evtsComplete  ",
-                  unit (*my->evtsComplete));
-         fprintf (stderr, "%19s:%6s", "evtsDiscarded  ",
-                  unit (*my->evtsDiscarded));
-         fprintf (stderr, "%19s:%6s", "bytesWritten  ",
-                  unit (*my->bytesWritten));
-         fputc ('\n', stderr);
-         fprintf (stderr, "%19s:%6s", "evtsDataError  ",
-                  unit (*my->evtsDataError));
-         fprintf (stderr, "%19s:%6s", "evtsTagError  ",
-                  unit (*my->evtsTagError));
-         fputc ('\n', stderr);
-
-         if ( dT > 0 ){
-           fprintf (stderr, "%19s:%6s", "evtsComplete/s",
-                    unit ((*my->evtsComplete - lastEC) / dT));
-           fprintf (stderr, "%19s:%6s", "evtsDiscarded/s",
-                    unit ((*my->evtsDiscarded - lastED) / dT));
-           fprintf (stderr, "%19s:%6s", "bytesWritten/s",
-                    unit ((*my->bytesWritten - lastBW) / dT));
-           fputc ('\n', stderr);
-           fprintf (stderr, "%19s:%6s", "evtsDataError/s",
-                    unit ((*my->evtsDataError - lastEE) / dT));
-           fprintf (stderr, "%19s:%6s", "evtsTagError/s",
-                    unit ((*my->evtsTagError - lastTE) / dT));
-           fputc ('\n', stderr);
-         }
-
-         lastEC = *my->evtsComplete;
-         lastEE = *my->evtsDataError;
-         lastTE = *my->evtsTagError;
-         lastED = *my->evtsDiscarded;
-         lastBW = *my->bytesWritten;
-
-         fputs
-           ("------------------------------------------------------------------------------\n",
-            stderr);
-         col = 0;
-         for (i = 0; i < NEVTIDS; i++)
-           {
-             if (*my->evtId[i] != 0)
-               {
-                 fprintf (stderr, "%17s%02x:%6s", "evtId", i,
-                          unit (*my->evtId[i]));
-                 if (++col == 3)
-                   {
-                     fputc ('\n', stderr);
-                     col = 0;
-                   }
+       unsigned long myRunId = 0;
+       struct timespec tv = { 0, 1e+8 };
+
+       while (my->epicsCtrl && myRunId == 0) {
+               if (Worker_getStatistic("daq_evtbuild", "runId", &myRunId) == -1) {
+                       fprintf(stderr, "<E> evtbuild.c: getRunId: Worker_getStatistic: cannot get runId!");
+                       sleep(1);
+               } else {
+
+                       if (myRunId == 0)
+                               nanosleep(&tv, NULL);
                }
-           }
-         if (col != 0)
-           {
-             fputc ('\n', stderr);
-           }
-         col = 0;
-
-         fputs
-           ("------------------------------------------------------------------------------\n",
-            stderr);
-         for (i = 0; i < theArgs->nrOfMsgs; i++)
-           {
-             fprintf (stderr, "%12s%02d: 0x%08x", "trigNr", i,
-                      *my->trigNr[i]);
-             if (++col == 3)
-               {
-                 fputc ('\n', stderr);
-                 col = 0;
+
+       }
+
+       return myRunId;
+}
+
+static void statsDump(TheArgs *theArgs, TheStats *my, int interval)
+{
+       static unsigned long lastEC;
+       static unsigned long lastEE;
+       static unsigned long lastTE;
+       static unsigned long lastED;
+       static unsigned long lastBW;
+       static time_t t0 = 0;
+       time_t t, dT;
+       int i;
+
+       if (theArgs->isStandalone && strcmp(theArgs->verbosity, "info") == 0 && !(theArgs->buffStat)) {
+               t = time(NULL);
+               dT = t - t0;
+               if (dT >= interval) {
+                       int col = 0;
+
+                       fputs("==============================================================================\n", stderr);
+                       fprintf(stderr, "%19s:%6s", "evtsComplete  ", unit(*my->evtsComplete));
+                       fprintf(stderr, "%19s:%6s", "evtsDiscarded  ", unit(*my->evtsDiscarded));
+                       fprintf(stderr, "%19s:%6s", "bytesWritten  ", unit(*my->bytesWritten));
+                       fputc('\n', stderr);
+                       fprintf(stderr, "%19s:%6s", "evtsDataError  ", unit(*my->evtsDataError));
+                       fprintf(stderr, "%19s:%6s", "evtsTagError  ", unit(*my->evtsTagError));
+                       fputc('\n', stderr);
+
+                       if (dT > 0) {
+                               fprintf(stderr, "%19s:%6s", "evtsComplete/s", unit((*my->evtsComplete - lastEC) / dT));
+                               fprintf(stderr, "%19s:%6s", "evtsDiscarded/s", unit((*my->evtsDiscarded - lastED) / dT));
+                               fprintf(stderr, "%19s:%6s", "bytesWritten/s", unit((*my->bytesWritten - lastBW) / dT));
+                               fputc('\n', stderr);
+                               fprintf(stderr, "%19s:%6s", "evtsDataError/s", unit((*my->evtsDataError - lastEE) / dT));
+                               fprintf(stderr, "%19s:%6s", "evtsTagError/s", unit((*my->evtsTagError - lastTE) / dT));
+                               fputc('\n', stderr);
+                       }
+
+                       lastEC = *my->evtsComplete;
+                       lastEE = *my->evtsDataError;
+                       lastTE = *my->evtsTagError;
+                       lastED = *my->evtsDiscarded;
+                       lastBW = *my->bytesWritten;
+
+                       fputs("------------------------------------------------------------------------------\n", stderr);
+                       col = 0;
+                       for (i = 0; i < NEVTIDS; i++) {
+                               if (*my->evtId[i] != 0) {
+                                       fprintf(stderr, "%17s%02x:%6s", "evtId", i, unit(*my->evtId[i]));
+                                       if (++col == 3) {
+                                               fputc('\n', stderr);
+                                               col = 0;
+                                       }
+                               }
+                       }
+                       if (col != 0) {
+                               fputc('\n', stderr);
+                       }
+                       col = 0;
+
+                       fputs("------------------------------------------------------------------------------\n", stderr);
+                       for (i = 0; i < theArgs->nrOfMsgs; i++) {
+                               fprintf(stderr, "%12s%02d: 0x%08x", "trigNr", i, *my->trigNr[i]);
+                               if (++col == 3) {
+                                       fputc('\n', stderr);
+                                       col = 0;
+                               }
+                       }
+                       if (col != 0) {
+                               fputc('\n', stderr);
+                       }
+                       t0 = t;
                }
-           }
-         if (col != 0)
-           {
-             fputc ('\n', stderr);
-           }
-         t0 = t;
-       }
-    }
+       }
 }
 
 
 static void storeRunInfoStart(time_t t, TheArgs *myArgs)
 {
-  /* open ascii file eb_runinfo2ora.txt to store simple information for 
-     the started RUN. The format: start <run_id> <filename> <date> <time>
-     where "start" is a key word which defines START RUN info. -S.Y.
-  */
-  FILE *fp;
-  char ltime[20];                 /* local time */
-  strftime(ltime, 20, "%Y-%m-%d %H:%M:%S", localtime(&t));
-
-  fp = fopen(myArgs->runinfo2ora,"a+");
-  fprintf(fp, "start %d %d %s %s\n", myArgs->runNr, myArgs->ebnum, myArgs->outPath, ltime);
-  fclose(fp);
+       /* open ascii file eb_runinfo2ora.txt to store simple information for 
+          the started RUN. The format: start <run_id> <filename> <date> <time>
+          where "start" is a key word which defines START RUN info. -S.Y.
+        */
+
+       FILE *fp;
+       char ltime[20];                         /* local time */
+       strftime(ltime, 20, "%Y-%m-%d %H:%M:%S", localtime(&t));
+
+       fp = fopen(myArgs->runinfo2ora, "a+");
+       fprintf(fp, "start %d %d %s %s\n", myArgs->runNr, myArgs->ebnum, myArgs->outPath, ltime);
+       fclose(fp);
 }
 
 static void storeRunInfoStop(time_t t, TheArgs *myArgs, TheStats *myStats)
 {
-  /* open ascii file eb_runinfo2ora.txt to store simple information for 
-     the stoped RUN. The format: stop <run_id> <date> <time> <events> <bytes>
-     where "stop" is a key word which defines STOP RUN info. -S.Y.
-  */
-
-  FILE *fp;
-  char ltime[20];                 /* local time */
-  strftime(ltime, 20, "%Y-%m-%d %H:%M:%S", localtime(&t));
-
-  fp = fopen(myArgs->runinfo2ora,"a+");
-  fprintf(fp, "stop %d %d %s %s %s ", myArgs->runNr, myArgs->ebnum, myArgs->outPath, ltime, unit(*myStats->evtsComplete));
-  fprintf(fp, "%s\n", unit(*myStats->bytesWritten));
-  fclose(fp);  
+       /* open ascii file eb_runinfo2ora.txt to store simple information for 
+          the stoped RUN. The format: stop <run_id> <date> <time> <events> <bytes>
+          where "stop" is a key word which defines STOP RUN info. -S.Y.
+        */
+
+       FILE *fp;
+       char ltime[20];                         /* local time */
+       strftime(ltime, 20, "%Y-%m-%d %H:%M:%S", localtime(&t));
+
+       fp = fopen(myArgs->runinfo2ora, "a+");
+       fprintf(fp, "stop %d %d %s %s %s ", myArgs->runNr, myArgs->ebnum, myArgs->outPath, ltime, unit(*myStats->evtsComplete));
+       fprintf(fp, "%s\n", unit(*myStats->bytesWritten));
+       fclose(fp);
 }
 
-static void storeInfoStart (const char *n, time_t t, TheArgs * my)
+static void storeInfoStart(const char *n, time_t t, TheArgs *my)
 {
-  Param pS, *p = &pS;
-  int i;
-  char s[20];
-
-  conSetupParam (p, getenv ("DAQ_SETUP"));
-
-  strftime (s, 20, "%Y-%m-%dT%H:%M:%S", localtime (&t));
-  Param_storeString (p, n, "startdate", s);
-  Param_storeInt (p, n, "nrofmsgs", my->nrOfMsgs);
-  for (i = 0; i < my->slowCtrlFileCnt; i++)
-    {
-      sprintf (s, "slwctrlfile%d", i);
-      Param_storeString (p, n, s, my->slowCtrlFiles[i]);
-    }
-  Param_storeString (p, n, "outpath", my->outPath);
-  Param_storeString (p, n, "outdev", my->outDev);
-  Param_storeInt (p, n, "runnr", my->runNr);
-  Param_storeString (p, n, "expid", my->expId);
-  Param_storeInt (p, n, "prio", my->priority);
-  Param_storeInt (p, n, "stndln", my->isStandalone);
-  Param_storeInt (p, n, "qsize", my->queueSize);
-  Param_storeString (p, n, "verb", my->verbosity);
-  if (my->resdownscale != 0)
-    {
-      Param_storeInt (p, n, "resdownscale", my->resdownscale);
-      Param_storeInt (p, n, "resnumevents", my->resnumevents);
-      Param_storeString (p, n, "respath", my->respath);
-      Param_storeInt (p, n, "ressizelimit", my->ressizelimit);
-
-    }
-  if (my->no_rpc == 1)
-    {
-      Param_storeInt (p, n, "rpc", my->no_rpc);
-    }
-  if (my->write_data == 1)
-    {
-      Param_storeString (p, n, "sec_path", my->sec_path);
-    }
-
-  desParam (p);
+       Param pS, *p = &pS;
+       int i;
+       char s[20];
+
+       conSetupParam(p, getenv("DAQ_SETUP"));
+
+       strftime(s, 20, "%Y-%m-%dT%H:%M:%S", localtime(&t));
+       Param_storeString(p, n, "startdate", s);
+       Param_storeInt(p, n, "nrofmsgs", my->nrOfMsgs);
+       for (i = 0; i < my->slowCtrlFileCnt; i++) {
+               sprintf(s, "slwctrlfile%d", i);
+               Param_storeString(p, n, s, my->slowCtrlFiles[i]);
+       }
+       Param_storeString(p, n, "outpath", my->outPath);
+       Param_storeString(p, n, "outdev", my->outDev);
+       Param_storeInt(p, n, "runnr", my->runNr);
+       Param_storeString(p, n, "expid", my->expId);
+       Param_storeInt(p, n, "prio", my->priority);
+       Param_storeInt(p, n, "stndln", my->isStandalone);
+       Param_storeInt(p, n, "qsize", my->queueSize);
+       Param_storeString(p, n, "verb", my->verbosity);
+       if (my->resdownscale != 0) {
+               Param_storeInt(p, n, "resdownscale", my->resdownscale);
+               Param_storeInt(p, n, "resnumevents", my->resnumevents);
+               Param_storeString(p, n, "respath", my->respath);
+               Param_storeInt(p, n, "ressizelimit", my->ressizelimit);
+
+       }
+       if (my->no_rpc == 1) {
+               Param_storeInt(p, n, "rpc", my->no_rpc);
+       }
+       if (my->write_data == 1) {
+               Param_storeString(p, n, "sec_path", my->sec_path);
+       }
+
+       desParam(p);
 }
 
-static void storeInfoStop (const char *n, time_t t, Worker * w, TheArgs * my)
+static void storeInfoStop(const char *n, time_t t, Worker *w, TheArgs *my)
 {
-  Param pS, *p = &pS;
-  int i;
-  char s[20];
+       Param pS, *p = &pS;
+       int i;
+       char s[20];
 
-  conSetupParam (p, getenv ("DAQ_SETUP"));
+       conSetupParam(p, getenv("DAQ_SETUP"));
 
-  for (i = 0; i < my->nrOfMsgs && strcmp (w->statistics[i].name, "") != 0; i++)
-    {
-      Param_storeInt (p, n, w->statistics[i].name, w->statistics[i].value);
-    }
-  strftime (s, 20, "%Y-%m-%dT%H:%M:%S", localtime (&t));
-  Param_storeString (p, n, "stopdate", s);
+       for (i = 0; i < my->nrOfMsgs && strcmp(w->statistics[i].name, "") != 0; i++) {
+               Param_storeInt(p, n, w->statistics[i].name, w->statistics[i].value);
+       }
+       strftime(s, 20, "%Y-%m-%dT%H:%M:%S", localtime(&t));
+       Param_storeString(p, n, "stopdate", s);
 
-  desParam (p);
+       desParam(p);
 }
 
-int is_mismatch_enough_to_stop(TheArgs * theArgs, TheStats * my) 
+int is_mismatch_enough_to_stop(TheArgs *theArgs, TheStats *my)
 {
-  /* min file size to start checking */
-  unsigned long minfilesize  = (theArgs->maxFileSz) * 0.1; /* 0.02 */
-  /* max number of discarded evts allowed */
-  unsigned long maxdiscarded = (*my->evtsComplete) * 0.1; /* 0.03 */
-  /* max number of evts with tag error allowed */
-  unsigned long maxtagerror  = (*my->evtsComplete) * 0.03;
-
-  if ((*my->bytesWritten) >= minfilesize && !(theArgs->ignore) ) {
-    if ((*my->evtsDiscarded) > maxdiscarded && !(theArgs->ignore) ) {
-
-      syslog (LOG_ERR, "<E> evtbuild.c: Too many events are broken!");
-      syslog (LOG_ERR, "<D> evtbuild.c: bytes written: %u, minimum file size: %u",
-             minfilesize, (*my->bytesWritten) );
-      syslog (LOG_ERR, "<D> evtbuild.c: discarded evts: %u, allowed: %u, total evts num: %u", 
-             (*my->evtsDiscarded), maxdiscarded, (*my->evtsComplete));
+       /* min file size to start checking */
+       unsigned long minfilesize = (theArgs->maxFileSz) * 0.1; /* 0.02 */
+       /* max number of discarded evts allowed */
+       unsigned long maxdiscarded = (*my->evtsComplete) * 0.1; /* 0.03 */
+       /* max number of evts with tag error allowed */
+       unsigned long maxtagerror = (*my->evtsComplete) * 0.03;
+
+       if ((*my->bytesWritten) >= minfilesize && !(theArgs->ignore)) {
+               if ((*my->evtsDiscarded) > maxdiscarded && !(theArgs->ignore)) {
+
+                       syslog(LOG_ERR, "<E> evtbuild.c: Too many events are broken!");
+                       syslog(LOG_ERR, "<D> evtbuild.c: bytes written: %u, minimum file size: %u", minfilesize, (*my->bytesWritten));
+                       syslog(LOG_ERR, "<D> evtbuild.c: discarded evts: %u, allowed: %u, total evts num: %u",
+                                  (*my->evtsDiscarded), maxdiscarded, (*my->evtsComplete));
 #if BEAM
-      system ("echo tagerror | netcat -w1 hadesdaq 12122");
+                       system("echo tagerror | netcat -w1 hadesdaq 12122");
 #endif
-      sleep(5); 
-    }
-    if ((*my->evtsTagError) > maxtagerror && !(theArgs->ignore) ) {
+                       sleep(5);
+               }
+               if ((*my->evtsTagError) > maxtagerror && !(theArgs->ignore)) {
 
-      syslog(LOG_ERR, "<E> evtbuild.c: Too many TagErrors!");
-      syslog (LOG_ERR, "<D> evtbuild.c: bytes written: %u, minimum file size: %u",
-             minfilesize, (*my->bytesWritten) );
-      syslog (LOG_ERR, "<D> evtbuild.c: tagerror evts: %u, allowed: %u, total evts num: %u", 
-             (*my->evtsTagError), maxtagerror, (*my->evtsComplete));
+                       syslog(LOG_ERR, "<E> evtbuild.c: Too many TagErrors!");
+                       syslog(LOG_ERR, "<D> evtbuild.c: bytes written: %u, minimum file size: %u", minfilesize, (*my->bytesWritten));
+                       syslog(LOG_ERR, "<D> evtbuild.c: tagerror evts: %u, allowed: %u, total evts num: %u",
+                                  (*my->evtsTagError), maxtagerror, (*my->evtsComplete));
 
 #if BEAM
-      system ("echo tagerror | netcat -w1 hadesdaq 12122");
+                       system("echo tagerror | netcat -w1 hadesdaq 12122");
 #endif
-      sleep(5); 
-    }
-  }
-  return 0;
+                       sleep(5);
+               }
+       }
+       return 0;
 }
 
-int get_file_number_in_dir (char *path)
+int get_file_number_in_dir(char *path)
 {
-  int file_number;
-  file_number = 0;
-  struct dirent *dirptr;
-  struct stat bufS, *buf = &bufS;
-  DIR *dir;
-  char tmppath[PARAM_MAX_VALUE_LEN];
-
-  dir = opendir (path);
-
-  if (dir == NULL) {
-    syslog(LOG_ERR, "%s, %d: trying to open directory %s: %s", 
-          __FILE__, __LINE__, path, strerror(errno));
-    exit (EXIT_FAILURE);
-  }
+       int file_number;
+       file_number = 0;
+       struct dirent *dirptr;
+       struct stat bufS, *buf = &bufS;
+       DIR *dir;
+       char tmppath[PARAM_MAX_VALUE_LEN];
+
+       dir = opendir(path);
+
+       if (dir == NULL) {
+               syslog(LOG_ERR, "%s, %d: trying to open directory %s: %s", __FILE__, __LINE__, path, strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
-  while (NULL != (dirptr = readdir (dir)))
-    file_number ++;
+       while (NULL != (dirptr = readdir(dir)))
+               file_number++;
 
-  closedir(dir);
+       closedir(dir);
 
-  return file_number;
+       return file_number;
 }
 
-double get_directory_size (char *path)
+double get_directory_size(char *path)
 {
-  double directory_size;
-  directory_size = 0.;
-  struct dirent *dirptr;
-  struct stat bufS, *buf = &bufS;
-  DIR *dir;
-  char tmppath[PARAM_MAX_VALUE_LEN];
-
-  dir = opendir (path);
-
-  if (dir == NULL) {
-      syslog(LOG_ERR, "%s, %d: trying to open directory %s: %s", 
-            __FILE__, __LINE__, path, strerror(errno));
-      exit (EXIT_FAILURE);
-  }
-
-  while (NULL != (dirptr = readdir (dir))) {
-      strcpy (tmppath, path);
-      strcat (tmppath, "/");
-      strcat (tmppath, dirptr->d_name);
-      lstat (tmppath, buf);
-
-      if( !(S_ISDIR (buf->st_mode)) ) {
-         directory_size += (double)(buf->st_size/(double)(1024.*1024.));
-      }
-  }
-
-  closedir(dir);
-  return directory_size;
+       double directory_size;
+       directory_size = 0.;
+       struct dirent *dirptr;
+       struct stat bufS, *buf = &bufS;
+       DIR *dir;
+       char tmppath[PARAM_MAX_VALUE_LEN];
+
+       dir = opendir(path);
+
+       if (dir == NULL) {
+               syslog(LOG_ERR, "%s, %d: trying to open directory %s: %s", __FILE__, __LINE__, path, strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       while (NULL != (dirptr = readdir(dir))) {
+               strcpy(tmppath, path);
+               strcat(tmppath, "/");
+               strcat(tmppath, dirptr->d_name);
+               lstat(tmppath, buf);
+
+               if (!(S_ISDIR(buf->st_mode))) {
+                       directory_size += (double) (buf->st_size / (double) (1024. * 1024.));
+               }
+       }
+
+       closedir(dir);
+       return directory_size;
 }
 
 #define DEBUG 0
-static int remove_file (char *path)
+static int remove_file(char *path)
 {
-  /*delete files if limit was reached*/
-
-  DIR *dir;
-  struct dirent *dirptr;
-  struct stat bufS, *buf = &bufS;
-  int recover_size;
-  time_t last_modification_time = 2147483647;
-  char tmppath[PARAM_MAX_VALUE_LEN];
-  char last_modification_file[PARAM_MAX_VALUE_LEN];
-  dir = opendir (path);
-
-  if (dir == NULL) {
-      syslog(LOG_ERR, "%s, %d: opening dir %s failed", __FILE__, __LINE__, path);
-      exit (EXIT_FAILURE);
-  }
-
-  while (1) {
-
-    if (NULL == (dirptr = readdir (dir)))
-        break;
-    
-    strcpy (tmppath, path);
-    strcat (tmppath, "/");
-    strcat (tmppath, dirptr->d_name);
-    lstat (tmppath, buf);
-
-    if(strstr(tmppath,"hld") == NULL)
-        continue;
-      
-
-    if ( !(S_ISDIR(buf->st_mode)) ) {
-      if (buf->st_mtime < last_modification_time) {
-       last_modification_time = buf->st_mtime;
-       recover_size = buf->st_size;
-       strcpy (last_modification_file, tmppath);
-      }
-    }
-  }
-
-  closedir(dir);
-
-  if( 0 != unlink (last_modification_file) ) {
-    syslog(LOG_ERR, "%s, %d: cannot unlink %s", __FILE__, __LINE__, last_modification_file);
-    exit(0);
-  }
-  return recover_size;
+       /*delete files if limit was reached */
+
+       DIR *dir;
+       struct dirent *dirptr;
+       struct stat bufS, *buf = &bufS;
+       int recover_size;
+       time_t last_modification_time = 2147483647;
+       char tmppath[PARAM_MAX_VALUE_LEN];
+       char last_modification_file[PARAM_MAX_VALUE_LEN];
+       dir = opendir(path);
+
+       if (dir == NULL) {
+               syslog(LOG_ERR, "%s, %d: opening dir %s failed", __FILE__, __LINE__, path);
+               exit(EXIT_FAILURE);
+       }
+
+       while (1) {
+
+               if (NULL == (dirptr = readdir(dir)))
+                       break;
+
+               strcpy(tmppath, path);
+               strcat(tmppath, "/");
+               strcat(tmppath, dirptr->d_name);
+               lstat(tmppath, buf);
+
+               if (strstr(tmppath, "hld") == NULL)
+                       continue;
+
+
+               if (!(S_ISDIR(buf->st_mode))) {
+                       if (buf->st_mtime < last_modification_time) {
+                               last_modification_time = buf->st_mtime;
+                               recover_size = buf->st_size;
+                               strcpy(last_modification_file, tmppath);
+                       }
+               }
+       }
+
+       closedir(dir);
+
+       if (0 != unlink(last_modification_file)) {
+               syslog(LOG_ERR, "%s, %d: cannot unlink %s", __FILE__, __LINE__, last_modification_file);
+               exit(0);
+       }
+       return recover_size;
 }
 
-static int openFile (TheArgs * theArgs)
+static int openFile(TheArgs *theArgs)
 {
 
-  char fileName[_POSIX_PATH_MAX];
-  static char outPath[_POSIX_PATH_MAX];
-  static char outLustrePath[_POSIX_PATH_MAX];
-  static char sec_path[_POSIX_PATH_MAX];
-  static once = 1;
-
-  diff_time = 1;
-
-  struct timeval tv;
-  
-  if( !(theArgs->epicsCtrl) ) {
-    /* runNr = genId32 (); */
-    gettimeofday( &tv, NULL );
-    runNr = tv.tv_sec - TIMEOFFSET;
-  }
-
-  seqNr = 0;
-  
-  theArgs->runNr = runNr;
-  
-  if (once) {
-    file_size = theArgs->maxFileSz;
-    strcpy (sec_path, theArgs->sec_path);
-    strcpy (outPath, theArgs->outPath);
-    strcpy (outLustrePath, theArgs->lustrePath);
-    once = 0;
-  }
-  else {
-    strcpy (theArgs->outPath, outPath);
-    strcpy (theArgs->lustrePath, outLustrePath);
-    strcpy (theArgs->sec_path, sec_path);
-  }
-
-  /* construct a default filename */
-  strcpy (fileName, theArgs->expId);
-  /*   %y : year (00-99)              %M : minute (00-59) 
-   *   %j : day of year (001-366)     %S : second (00-59)
-   *   %H : hour (00-23)
-   */
-  time_t iocTime;
-  iocTime = theArgs->runNr + TIMEOFFSET;
-  strftime( fileName + strlen(fileName), 18, "%y%j%H%M%S",
-           localtime(&iocTime) );
-  
-  /*   if ebnum == 0 then we assume that there is only 1 EB, 
-   *   RUNID distribution by IOC is not needed and file name
-   *   can be in the usual format without EB identificator.
-   */
-  if( theArgs->epicsCtrl && theArgs->ebnum > 0 ) 
-    sprintf( fileName, "%s%02d.hld", fileName, theArgs->ebnum);
-  else
-    sprintf( fileName, "%s.hld", fileName);
-
-  outTape = NULL;
-  outFile = NULL;
-  outSecondFile = NULL;
-  outLustreFile = NULL;
-
-  if (strcmp (theArgs->outDev, "null") == 0) {
-    outFile = NULL;
-  }
-  else if (strcmp (theArgs->outDev, "stdout") == 0) {
-    outFile = stdout;
-  }
-  else if (strcmp (theArgs->outDev, "file") == 0) {
-    if (strcmp (theArgs->outPath, "") == 0) {
-      strcpy (theArgs->outPath, fileName);
-    }
-    else {
-      struct stat bufS, *buf = &bufS;
-      
-      stat (theArgs->outPath, buf);
-      if (S_ISDIR (buf->st_mode)) {
-       strcat (theArgs->outPath, "/");
-       strcat (theArgs->outPath, fileName);
-      }
-    }
-    
-    if (NULL == (outFile = fopen (theArgs->outPath, "wb"))) {
-      syslog (LOG_ERR, "opening file %s: %s", theArgs->outPath,
-             strerror (errno));
-      outFile = NULL;
-      return -1;
-    }
-    else {
-      struct statfs bufS, *buf = &bufS;
-      statfs (theArgs->outPath, buf);
-      if (theArgs->maxFileSz / buf->f_bsize > buf->f_bavail) {
-       errno = ENOSPC;
-       outFile = NULL;
-       unlink (theArgs->outPath);
-       syslog (LOG_ERR, "opening file %s: %s", theArgs->outPath, strerror (errno));
-       return -1;
-      }
-    }
-  }
-  else if (strcmp (theArgs->outDev, "tape") == 0) {
-    if (strcmp (theArgs->outPath, "") == 0) {
-      strcpy (theArgs->outPath, fileName);
-    }
-    if (NULL == (outTape = openAnsiTape (theArgs->outPath, "/dev/tape"))) {
-      syslog (LOG_ERR, "opening tape %s: %s", theArgs->outPath, strerror (errno));
-      outFile = NULL;
-      return -1;
-    }
-  }
-  else {
-    syslog (LOG_ERR, "unknown outputDev \"%s\"", theArgs->outDev);
-    return -1;
-  }
-
-  if (theArgs->write_data) {
-    dirSize = get_directory_size (sec_path);
-    
-    while (dirSize > theArgs->secsizelimit) {
-      /*delete files if limit was reached */
-      remove_file (sec_path);
-      dirSize = get_directory_size (sec_path);
-    }
-    if (strcmp (theArgs->sec_path, "") == 0) {
-      strcpy (theArgs->sec_path, fileName);
-    }
-    else {
-      struct stat bufS, *buf = &bufS;
-      
-      stat (theArgs->sec_path, buf);
-      if (S_ISDIR (buf->st_mode)) {
-       strcat (theArgs->sec_path, "/");
-       strcat (theArgs->sec_path, fileName);
-      }
-    }
-    if (NULL == (outSecondFile = fopen (theArgs->sec_path, "wb"))) {
-      syslog (LOG_ERR, "opening file %s: %s", theArgs->sec_path, strerror (errno));
-      outSecondFile = NULL;
-      return -1;
-    }
-    else {
-      struct statfs bufS, *buf = &bufS;
-      statfs (theArgs->sec_path, buf);
-      if (theArgs->maxFileSz / buf->f_bsize > buf->f_bavail) {
-       errno = ENOSPC;
-       if (0 != fclose (outSecondFile)) {
-         fprintf (stderr, "<E> evtbuild.c, openFile: fclose 'outSecondFile' failed\n");
+       char fileName[_POSIX_PATH_MAX];
+       static char outPath[_POSIX_PATH_MAX];
+       static char outLustrePath[_POSIX_PATH_MAX];
+       static char sec_path[_POSIX_PATH_MAX];
+       static once = 1;
+
+       diff_time = 1;
+
+       struct timeval tv;
+
+       if (!(theArgs->epicsCtrl)) {
+               /* runNr = genId32 (); */
+               gettimeofday(&tv, NULL);
+               runNr = tv.tv_sec - TIMEOFFSET;
        }
+
+       seqNr = 0;
+
+       theArgs->runNr = runNr;
+
+       if (once) {
+               file_size = theArgs->maxFileSz;
+               strcpy(sec_path, theArgs->sec_path);
+               strcpy(outPath, theArgs->outPath);
+               strcpy(outLustrePath, theArgs->lustrePath);
+               once = 0;
+       } else {
+               strcpy(theArgs->outPath, outPath);
+               strcpy(theArgs->lustrePath, outLustrePath);
+               strcpy(theArgs->sec_path, sec_path);
+       }
+
+       /* construct a default filename */
+       strcpy(fileName, theArgs->expId);
+
+       /*   %y : year (00-99)              %M : minute (00-59) 
+        *   %j : day of year (001-366)     %S : second (00-59)
+        *   %H : hour (00-23)
+        */
+       time_t iocTime;
+       iocTime = theArgs->runNr + TIMEOFFSET;
+       strftime(fileName + strlen(fileName), 18, "%y%j%H%M%S", localtime(&iocTime));
+
+       /*   if ebnum == 0 then we assume that there is only 1 EB, 
+        *   RUNID distribution by IOC is not needed and file name
+        *   can be in the usual format without EB identificator.
+        */
+       if (theArgs->epicsCtrl && theArgs->ebnum > 0)
+               sprintf(fileName, "%s%02d.hld", fileName, theArgs->ebnum);
+       else
+               sprintf(fileName, "%s.hld", fileName);
+
+       outTape = NULL;
+       outFile = NULL;
        outSecondFile = NULL;
-       unlink (theArgs->sec_path);
-       syslog (LOG_ERR, "opening file %s: %s", theArgs->sec_path, strerror (errno));
-       return -1;
-      }
-    }
-  }
-  
-  /* Open file on Lustre */
-  if( strcmp (theArgs->lustrePath, "") != 0 ) {
-    struct stat bufS, *buf = &bufS;
-    stat (theArgs->lustrePath, buf);
-    
-    /* If lustrePath is a dir name, add fileName to the lustrePath */
-    if (S_ISDIR (buf->st_mode)) {
-      strcat (theArgs->lustrePath, "/");
-      strcat (theArgs->lustrePath, fileName);
-    }    
-    
-    if (NULL == (outLustreFile = fopen (theArgs->lustrePath, "wb"))) {
-      syslog (LOG_ERR, "opening file %s: %s", theArgs->lustrePath, strerror (errno));
-    }
-    else {
-      struct statfs bufS, *buf = &bufS;
-      statfs (theArgs->lustrePath, buf);
-      
-      /*   Check if the number of free available blocks
-       *   is enough to write the file with maximum size.
-       */
-      if (theArgs->maxFileSz / buf->f_bsize > buf->f_bavail) {
-       errno = ENOSPC;
-       
        outLustreFile = NULL;
-       unlink (theArgs->lustrePath);
-       syslog (LOG_ERR, "%s, %d: opening file %s: %s", 
-               __FILE__, __LINE__, theArgs->lustrePath, strerror (errno));
-       return -1;      
-      }
-    }
-  }
 
+       if (strcmp(theArgs->outDev, "null") == 0) {
+               outFile = NULL;
+       } else if (strcmp(theArgs->outDev, "stdout") == 0) {
+               outFile = stdout;
+       } else if (strcmp(theArgs->outDev, "file") == 0) {
+               if (strcmp(theArgs->outPath, "") == 0) {
+                       strcpy(theArgs->outPath, fileName);
+               } else {
+                       struct stat bufS, *buf = &bufS;
+
+                       stat(theArgs->outPath, buf);
+                       if (S_ISDIR(buf->st_mode)) {
+                               strcat(theArgs->outPath, "/");
+                               strcat(theArgs->outPath, fileName);
+                       }
+               }
+
+               if (NULL == (outFile = fopen(theArgs->outPath, "wb"))) {
+                       syslog(LOG_ERR, "opening file %s: %s", theArgs->outPath, strerror(errno));
+                       outFile = NULL;
+                       return -1;
+               } else {
+                       struct statfs bufS, *buf = &bufS;
+                       statfs(theArgs->outPath, buf);
+                       if (theArgs->maxFileSz / buf->f_bsize > buf->f_bavail) {
+                               errno = ENOSPC;
+                               outFile = NULL;
+                               unlink(theArgs->outPath);
+                               syslog(LOG_ERR, "opening file %s: %s", theArgs->outPath, strerror(errno));
+                               return -1;
+                       }
+               }
+       } else if (strcmp(theArgs->outDev, "tape") == 0) {
+               if (strcmp(theArgs->outPath, "") == 0) {
+                       strcpy(theArgs->outPath, fileName);
+               }
+               if (NULL == (outTape = openAnsiTape(theArgs->outPath, "/dev/tape"))) {
+                       syslog(LOG_ERR, "opening tape %s: %s", theArgs->outPath, strerror(errno));
+                       outFile = NULL;
+                       return -1;
+               }
+       } else {
+               syslog(LOG_ERR, "unknown outputDev \"%s\"", theArgs->outDev);
+               return -1;
+       }
+
+       if (theArgs->write_data) {
+               dirSize = get_directory_size(sec_path);
+
+               while (dirSize > theArgs->secsizelimit) {
+                       /*delete files if limit was reached */
+                       remove_file(sec_path);
+                       dirSize = get_directory_size(sec_path);
+               }
+               if (strcmp(theArgs->sec_path, "") == 0) {
+                       strcpy(theArgs->sec_path, fileName);
+               } else {
+                       struct stat bufS, *buf = &bufS;
+
+                       stat(theArgs->sec_path, buf);
+                       if (S_ISDIR(buf->st_mode)) {
+                               strcat(theArgs->sec_path, "/");
+                               strcat(theArgs->sec_path, fileName);
+                       }
+               }
+               if (NULL == (outSecondFile = fopen(theArgs->sec_path, "wb"))) {
+                       syslog(LOG_ERR, "opening file %s: %s", theArgs->sec_path, strerror(errno));
+                       outSecondFile = NULL;
+                       return -1;
+               } else {
+                       struct statfs bufS, *buf = &bufS;
+                       statfs(theArgs->sec_path, buf);
+                       if (theArgs->maxFileSz / buf->f_bsize > buf->f_bavail) {
+                               errno = ENOSPC;
+                               if (0 != fclose(outSecondFile)) {
+                                       fprintf(stderr, "<E> evtbuild.c, openFile: fclose 'outSecondFile' failed\n");
+                               }
+                               outSecondFile = NULL;
+                               unlink(theArgs->sec_path);
+                               syslog(LOG_ERR, "opening file %s: %s", theArgs->sec_path, strerror(errno));
+                               return -1;
+                       }
+               }
+       }
+
+       /* Open file on Lustre */
+       if (strcmp(theArgs->lustrePath, "") != 0) {
+               struct stat bufS, *buf = &bufS;
+               stat(theArgs->lustrePath, buf);
+
+               /* If lustrePath is a dir name, add fileName to the lustrePath */
+               if (S_ISDIR(buf->st_mode)) {
+                       strcat(theArgs->lustrePath, "/");
+                       strcat(theArgs->lustrePath, fileName);
+               }
+
+               if (NULL == (outLustreFile = fopen(theArgs->lustrePath, "wb"))) {
+                       syslog(LOG_ERR, "opening file %s: %s", theArgs->lustrePath, strerror(errno));
+               } else {
+                       struct statfs bufS, *buf = &bufS;
+                       statfs(theArgs->lustrePath, buf);
+
+                       /*   Check if the number of free available blocks
+                        *   is enough to write the file with maximum size.
+                        */
+                       if (theArgs->maxFileSz / buf->f_bsize > buf->f_bavail) {
+                               errno = ENOSPC;
+
+                               outLustreFile = NULL;
+                               unlink(theArgs->lustrePath);
+                               syslog(LOG_ERR, "%s, %d: opening file %s: %s", __FILE__, __LINE__, theArgs->lustrePath, strerror(errno));
+                               return -1;
+                       }
+               }
+       }
 #ifdef RFIO
-  /* open file on Data Mover */
-  if( fRemote != NULL ) {
-    char rfioPath[_POSIX_PATH_MAX];
-    strcpy( rfioPath, theArgs->rfioRemotePath );
-    strcat( rfioPath, "/" );
-    strcat( rfioPath, fileName );    
-
-    fprintf ( stderr, "<D> evtbuild.c, rfio_fnewfile(): trying to open remote file %s\n", rfioPath );
-    if( 0 != rfio_fnewfile( fRemote, rfioPath ) ) {
-      syslog( LOG_ERR, "%s, %d: cannot open remote file %s: %s", 
-             __FILE__, __LINE__, rfioPath, strerror(errno) );
-      exit (EXIT_FAILURE);
-    }
-  }
+       /* open file on Data Mover */
+       if (fRemote != NULL) {
+               char rfioPath[_POSIX_PATH_MAX];
+               strcpy(rfioPath, theArgs->rfioRemotePath);
+               strcat(rfioPath, "/");
+               strcat(rfioPath, fileName);
+
+               fprintf(stderr, "<D> evtbuild.c, rfio_fnewfile(): trying to open remote file %s\n", rfioPath);
+               if (0 != rfio_fnewfile(fRemote, rfioPath)) {
+                       syslog(LOG_ERR, "%s, %d: cannot open remote file %s: %s", __FILE__, __LINE__, rfioPath, strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
+       }
 #endif
 
-  return 0;
+       return 0;
 }
-  
-static int openRESFile (TheArgs * theArgs)
+
+static int openRESFile(TheArgs *theArgs)
 {
-  char fileName[_POSIX_PATH_MAX];
-  static res_once = 1;
-  static char respath[_POSIX_PATH_MAX];
-
-  res_seqNr = 0;
-
-  if (res_once) {
-    res_once = 0;
-    strcpy (respath, theArgs->respath);
-  }
-  else {
-    strcpy (theArgs->respath, respath);
-  }
-
-  if (theArgs->ressizelimit != 0) {
-      res_dirNr = get_file_number_in_dir (respath);
-
-      while (res_dirNr > theArgs->ressizelimit) {
-
-         /*delete files if limit was reached */
-         remove_file (respath);
-         res_dirNr--;
-       }
-  }
-  strcpy (fileName, theArgs->expId);
-  strftime (fileName + strlen (fileName), 15, "%y%j%H%M%S_",
-           localtime (&res_time));
-  static int filecounter = 1;
-
-  if( diff_time == 1 ) {
-         filecounter = 1;
-         diff_time = 0;
-  } else { 
-         filecounter++;
-  }
-
-  char app[8];
-
-  sprintf (app, "%d", filecounter);
-  strcat (fileName,app);
-  strcat (fileName,".hld");
-
-  if (strcmp (theArgs->respath, "") == 0) {
-      strcpy (theArgs->respath, fileName);
-  }
-  else {
-      struct stat bufS, *buf = &bufS;
-
-      stat (theArgs->respath, buf);
-
-      if (S_ISDIR (buf->st_mode)) {
-         strcat (theArgs->respath, "/");
-         strcat (theArgs->respath, fileName);
-      }
-  }
-
-  /* construct a default filename */
-  outRESFile = NULL;
-
-  if (NULL == (outRESFile = fopen (theArgs->respath, "wb"))) {
-      syslog (LOG_ERR, "%s, %d: opening file %s: %s", 
-             __FILE__, __LINE__, theArgs->respath, strerror (errno));
-      outRESFile = NULL;
-      return -1;
-    }
-  else
-    {
-      struct statfs bufS, *buf = &bufS;
-      statfs (theArgs->respath, buf);
-      if ((theArgs->maxFileSz) / buf->f_bsize > buf->f_bavail)
-       {
-         errno = ENOSPC;
-         if (0 != fclose (outRESFile)) {
-           syslog(LOG_ERR, "%s, %d: trying fclose 'outRESFile': %s", 
-                  __FILE__, __LINE__, strerror(errno));
-         }
-         outRESFile = NULL;
-         unlink (theArgs->respath);
-
-         syslog (LOG_ERR, "%s, %d: opening file %s: %s", 
-                 __FILE__, __LINE__, theArgs->respath, strerror (errno));
-         return -1;
-       }
-    }
-
-  return 0;
+       char fileName[_POSIX_PATH_MAX];
+       static res_once = 1;
+       static char respath[_POSIX_PATH_MAX];
+
+       res_seqNr = 0;
+
+       if (res_once) {
+               res_once = 0;
+               strcpy(respath, theArgs->respath);
+       } else {
+               strcpy(theArgs->respath, respath);
+       }
+
+       if (theArgs->ressizelimit != 0) {
+               res_dirNr = get_file_number_in_dir(respath);
+
+               while (res_dirNr > theArgs->ressizelimit) {
+
+                       /*delete files if limit was reached */
+                       remove_file(respath);
+                       res_dirNr--;
+               }
+       }
+       strcpy(fileName, theArgs->expId);
+       strftime(fileName + strlen(fileName), 15, "%y%j%H%M%S_", localtime(&res_time));
+       static int filecounter = 1;
+
+       if (diff_time == 1) {
+               filecounter = 1;
+               diff_time = 0;
+       } else {
+               filecounter++;
+       }
+
+       char app[8];
+
+       sprintf(app, "%d", filecounter);
+       strcat(fileName, app);
+       strcat(fileName, ".hld");
+
+       if (strcmp(theArgs->respath, "") == 0) {
+               strcpy(theArgs->respath, fileName);
+       } else {
+               struct stat bufS, *buf = &bufS;
+
+               stat(theArgs->respath, buf);
+
+               if (S_ISDIR(buf->st_mode)) {
+                       strcat(theArgs->respath, "/");
+                       strcat(theArgs->respath, fileName);
+               }
+       }
+
+       /* construct a default filename */
+       outRESFile = NULL;
+
+       if (NULL == (outRESFile = fopen(theArgs->respath, "wb"))) {
+               syslog(LOG_ERR, "%s, %d: opening file %s: %s", __FILE__, __LINE__, theArgs->respath, strerror(errno));
+               outRESFile = NULL;
+               return -1;
+       } else {
+               struct statfs bufS, *buf = &bufS;
+               statfs(theArgs->respath, buf);
+               if ((theArgs->maxFileSz) / buf->f_bsize > buf->f_bavail) {
+                       errno = ENOSPC;
+                       if (0 != fclose(outRESFile)) {
+                               syslog(LOG_ERR, "%s, %d: trying fclose 'outRESFile': %s", __FILE__, __LINE__, strerror(errno));
+                       }
+                       outRESFile = NULL;
+                       unlink(theArgs->respath);
+
+                       syslog(LOG_ERR, "%s, %d: opening file %s: %s", __FILE__, __LINE__, theArgs->respath, strerror(errno));
+                       return -1;
+               }
+       }
+
+       return 0;
 }
 
-static int writeFile (void *evt)
+static int writeFile(void *evt)
 {
 
-  int writeFileR;
-
-  Evt_setSeqNr (evt, seqNr++);
-  Evt_setRunNr (evt, runNr);
+       int writeFileR;
 
-  if (outFile != NULL) {
-      writeFileR = fwrite (evt, 1, Evt_paddedSize (evt), outFile);
-  }
-  else if (outTape != NULL) {
-    writeFileR = writeAnsiTape (outTape, evt, Evt_paddedSize (evt));
-  }
+       Evt_setSeqNr(evt, seqNr++);
+       Evt_setRunNr(evt, runNr);
 
+       if (outFile != NULL) {
+               writeFileR = fwrite(evt, 1, Evt_paddedSize(evt), outFile);
+       } else if (outTape != NULL) {
+               writeFileR = writeAnsiTape(outTape, evt, Evt_paddedSize(evt));
+       }
 #ifdef RFIO
-  /* write to Data Mover via RFIO */
-  if( fRemote != NULL ) {
-    if( 0 >= rfio_fwrite( evt, 1, Evt_paddedSize(evt), fRemote ) )
-      syslog(LOG_ERR, "%s, %d: writing file via RFIO: %s", __FILE__, __LINE__, strerror(errno));
-  }
+       /* write to Data Mover via RFIO */
+       if (fRemote != NULL) {
+               if (0 >= rfio_fwrite(evt, 1, Evt_paddedSize(evt), fRemote))
+                       syslog(LOG_ERR, "%s, %d: writing file via RFIO: %s", __FILE__, __LINE__, strerror(errno));
+       }
 #endif
 
-  /* write to Lustre */
-  if (outLustreFile != NULL) {
-      writeFileR = fwrite (evt, 1, Evt_paddedSize (evt), outLustreFile);
-  }
+       /* write to Lustre */
+       if (outLustreFile != NULL) {
+               writeFileR = fwrite(evt, 1, Evt_paddedSize(evt), outLustreFile);
+       }
 
-  /* writing file in the second dir  */
-  if (outSecondFile != NULL) {
-    fwrite (evt, 1, Evt_paddedSize (evt), outSecondFile);
-  }
+       /* writing file in the second dir  */
+       if (outSecondFile != NULL) {
+               fwrite(evt, 1, Evt_paddedSize(evt), outSecondFile);
+       }
 
-  return writeFileR;
+       return writeFileR;
 }
 
-static int writeRESFile (void *evt)
+static int writeRESFile(void *evt)
 {
-  int writeFileR;
-  Evt_setSeqNr (evt, res_seqNr++);
-  Evt_setRunNr (evt, runNr);
-  if (outRESFile != NULL) {
-      writeFileR = fwrite (evt, 1, Evt_paddedSize (evt), outRESFile);
-  }
-  Evt_setSeqNr (evt, seqNr);
-  
-  return writeFileR;
+       int writeFileR;
+       Evt_setSeqNr(evt, res_seqNr++);
+       Evt_setRunNr(evt, runNr);
+       if (outRESFile != NULL) {
+               writeFileR = fwrite(evt, 1, Evt_paddedSize(evt), outRESFile);
+       }
+       Evt_setSeqNr(evt, seqNr);
+
+       return writeFileR;
 }
 
-static int closeFile ()
+static int closeFile()
 {
-  int closeFileR;
-  int closeLustreFileR;
-
-  /* closing file in the second dir  */
-  if (outSecondFile != NULL) {
-      dirSize += file_size;
-      if (0 != fclose (outSecondFile)) {
-       syslog(LOG_ERR, "%s, %d: trying fclose 'outSecondFile': %s", 
-              __FILE__, __LINE__, strerror(errno));
-      }
-    }
-
-  if (outFile != NULL) {
-      closeFileR = fclose (outFile);
-      if (0 != closeFileR) {
-       syslog(LOG_ERR, "%s, %d: trying fclose 'outFile': %s", 
-              __FILE__, __LINE__, strerror(errno));
-      }
-    }
-  else if (outTape != NULL) {
-    closeFileR = closeAnsiTape (outTape);
-  }
-
-  /* close file on Lustre */
-  if (outLustreFile != NULL) {
-    closeLustreFileR = fclose (outLustreFile);
-    if (0 != closeLustreFileR) {
-      syslog(LOG_ERR, "%s, %d: trying fclose 'outLustreFile': %s", 
-            __FILE__, __LINE__, strerror(errno));
-      exit (EXIT_FAILURE);
-    }
-  }
+       int closeFileR;
+       int closeLustreFileR;
+
+       /* closing file in the second dir  */
+       if (outSecondFile != NULL) {
+               dirSize += file_size;
+               if (0 != fclose(outSecondFile)) {
+                       syslog(LOG_ERR, "%s, %d: trying fclose 'outSecondFile': %s", __FILE__, __LINE__, strerror(errno));
+               }
+       }
+
+       if (outFile != NULL) {
+               closeFileR = fclose(outFile);
+               if (0 != closeFileR) {
+                       syslog(LOG_ERR, "%s, %d: trying fclose 'outFile': %s", __FILE__, __LINE__, strerror(errno));
+               }
+       } else if (outTape != NULL) {
+               closeFileR = closeAnsiTape(outTape);
+       }
 
+       /* close file on Lustre */
+       if (outLustreFile != NULL) {
+               closeLustreFileR = fclose(outLustreFile);
+               if (0 != closeLustreFileR) {
+                       syslog(LOG_ERR, "%s, %d: trying fclose 'outLustreFile': %s", __FILE__, __LINE__, strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
+       }
 #ifdef RFIO
-  /* close file on Data Mover */
-  if( fRemote != NULL ) { 
-    if( 0 != rfio_fendfile( fRemote ) ) {
-      syslog(LOG_ERR, "%s, %d: trying rfio_fendfile: %s", 
-            __FILE__, __LINE__, strerror(errno));
-      exit (EXIT_FAILURE);
-    }
-  }
+       /* close file on Data Mover */
+       if (fRemote != NULL) {
+               if (0 != rfio_fendfile(fRemote)) {
+                       syslog(LOG_ERR, "%s, %d: trying rfio_fendfile: %s", __FILE__, __LINE__, strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
+       }
 #endif
 
-  return closeFileR;
+       return closeFileR;
 }
 
-static int closeRESFile (TheArgs * theArgs)
+static int closeRESFile(TheArgs *theArgs)
 {
-  int closeFileR;
-  if (outRESFile != NULL) {
-      res_dirSize += res_file_size;
-      closeFileR = fclose (outRESFile);
-      if (0 != closeFileR) {
-       syslog(LOG_ERR, "%s, %d: trying fclose 'outRESFile': %s", 
-              __FILE__, __LINE__, strerror(errno));
-      }
-  }
-  else {
-    syslog(LOG_ERR, "%s, %d: closeRESFile failed: outRESFile is NULL", 
-          __FILE__, __LINE__);
-  }
-
-  return closeFileR;
+       int closeFileR;
+       if (outRESFile != NULL) {
+               res_dirSize += res_file_size;
+               closeFileR = fclose(outRESFile);
+               if (0 != closeFileR) {
+                       syslog(LOG_ERR, "%s, %d: trying fclose 'outRESFile': %s", __FILE__, __LINE__, strerror(errno));
+               }
+       } else {
+               syslog(LOG_ERR, "%s, %d: closeRESFile failed: outRESFile is NULL", __FILE__, __LINE__);
+       }
+
+       return closeFileR;
 }
 
 #ifdef RFIO
-static int rfio_openConnection (TheArgs * theArgs)
+static int rfio_openConnection(TheArgs *theArgs)
 {
 
-  if( (strcmp (theArgs->rfioRemotePath, "") != 0) ){
-    char rfioBase[128] = "";
-    char *pcc;
-    strcpy( rfioBase, theArgs->rfioRemotePath );
-    /*
-     *  rfio_fopen requires a base name (something like rfiodaq:gstore:).
-     *  Get pointer pcc to last ":" in the string rfioBase 
-     *  and cut away  everything what follows.
-     */
-    pcc = (char *) strrchr( rfioBase, ':');
-    pcc++;
-    strncpy(pcc, "\0", 1);             /* terminates after node name */
-
-    fprintf ( stderr, "<I> evtbuild.c, rfio_fopen(): trying to open connection to Data Mover: %s\n", rfioBase );
-    fRemote = rfio_fopen( rfioBase, "wb" );
-
-    if (fRemote == NULL) {
-      syslog(LOG_ERR, "%s, %d: trying to open connection to Data Mover %s: %s", 
-            __FILE__, __LINE__, rfioBase, strerror(errno));
-      exit (EXIT_FAILURE);
-    }
-  }
-
-  return 0;
+       if ((strcmp(theArgs->rfioRemotePath, "") != 0)) {
+               char rfioBase[128] = "";
+               char *pcc;
+               strcpy(rfioBase, theArgs->rfioRemotePath);
+               /*
+                *  rfio_fopen requires a base name (something like rfiodaq:gstore:).
+                *  Get pointer pcc to last ":" in the string rfioBase 
+                *  and cut away  everything what follows.
+                */
+               pcc = (char *) strrchr(rfioBase, ':');
+               pcc++;
+               strncpy(pcc, "\0", 1);  /* terminates after node name */
+
+               fprintf(stderr, "<I> evtbuild.c, rfio_fopen(): trying to open connection to Data Mover: %s\n", rfioBase);
+               /* fRemote = rfio_fopen( rfioBase, "wb" ); */
+
+               fRemote = rfio_fopen_gsidaq(rfioBase, "wb", 2, theArgs->rfioLustrePath, 1, 10, 1);
+
+               if (fRemote == NULL) {
+                       syslog(LOG_ERR, "%s, %d: trying to open connection to Data Mover %s: %s",
+                                  __FILE__, __LINE__, rfioBase, strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
+       }
+
+       return 0;
 }
 
-static int rfio_closeConnection (TheArgs * theArgs)
+static int rfio_closeConnection(TheArgs *theArgs)
 {
-  if( (strcmp (theArgs->rfioRemotePath, "") != 0) && fRemote != NULL ) {
-    
-    if ( 0 != rfio_fclose( fRemote ) ) {
-      syslog(LOG_ERR, "%s, %d: trying to close connection to Data Mover: %s", 
-            __FILE__, __LINE__, strerror(errno));
-      exit (EXIT_FAILURE);
-    }
-  }
-
-  return 0;
+       if ((strcmp(theArgs->rfioRemotePath, "") != 0) && fRemote != NULL) {
+
+               if (0 != rfio_fclose(fRemote)) {
+                       syslog(LOG_ERR, "%s, %d: trying to close connection to Data Mover: %s", __FILE__, __LINE__, strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
+       }
+
+       return 0;
 }
 #endif
 
 /* BUGBUG bailOut not proper yet */
-int main (int argc, char *argv[])
+int main(int argc, char *argv[])
 {
 
-  int i;
-  TheArgs theArgsS, *theArgs = &theArgsS;
-  TheStats theStatsS, *theStats = &theStatsS;
-  ShmTrans **shmTrans;
-  HadTuQueue **hadTuQueue;
-  Worker *worker;
-
-  int scanWasSuccessful;
-  uint32_t currTrigNr;
-  uint32_t currTrigTag;
-  uint32_t currId;
-
-  signal(SIGINT, sigHandler);
-  signal(SIGTERM, sigHandler);
-  signal(SIGHUP, sigHandler);
-
-  openlog (argv[0], LOG_PID | LOG_PERROR, LOG_LOCAL0);
-  setlogmask (LOG_UPTO (LOG_INFO));
-
-  argsDefault (theArgs);
-  argsFromParam (theArgs, argc, argv);
-
-  if (0 > argsFromCL (theArgs, argc, argv)) {
-      usage (argv[0]);
-      exit (EXIT_FAILURE);
-  }
-
-  if( argsCheck(theArgs) ) {
-    sleep(10);
-    exit (EXIT_FAILURE);
-  }
-
-  for (i = 0; prioritynames[i].c_name != NULL && 
-        0 != strcmp (prioritynames[i].c_name, theArgs->verbosity); i++) {}
-
-  if (prioritynames[i].c_name == NULL)
-      exit (EXIT_FAILURE);
-  else
-      setlogmask (LOG_UPTO (prioritynames[i].c_val));
-
-  /* normalize experiment id */
-  theArgs->expId[0] = tolower (theArgs->expId[0]);
-  theArgs->expId[1] = tolower (theArgs->expId[1]);
-  theArgs->expId[2] = '\0';
-
-  /*   Create a name for a worker's shared memory segment
-   *   which consists of a program name and a base name provided by user 
-   */
-  char buf[_POSIX_PATH_MAX];
-  sprintf( buf, "%s%s", argv[0], theArgs->shmname );
-
-  if (NULL == (worker = Worker_initBegin (buf, sigHandler, theArgs->priority, 
-                                         theArgs->isStandalone))) {
-      syslog (LOG_ERR, "%s, %d: %s", __FILE__, __LINE__, strerror (errno));
-      exit (EXIT_FAILURE);
-  }
-      
-  if (theArgs->no_rpc) 
-      syslog (LOG_WARNING, "DISABLE of online service");
-  else 
-    if (-1 == initOnline ()) 
-         syslog (LOG_WARNING, "unable to initialize online service");
-
-  if (theArgs->nrOfMsgs == 0) {
-      /*
-         no '-m' option was on command line, we assume that the
-         readout task (daq_readout) is running on the same node and
-         communicates directly via shared memory
-       */
-      syslog (LOG_NOTICE, "nrOfMsgs not set, assuming readout is local");
-
-      shmTrans = malloc (sizeof (ShmTrans *));
-      hadTuQueue = malloc (sizeof (HadTuQueue *));
-
-      sprintf( buf, "subevtqueue%s", theArgs->shmname );
-      shmTrans[0] = ShmTrans_create( buf, 2 * theArgs->queueSize );
-
-      hadTuQueue[0] = NULL;
-      theArgs->nrOfMsgs = 1;
-  }
-  else {
-      shmTrans = malloc (theArgs->nrOfMsgs * sizeof (ShmTrans *));
-      hadTuQueue = malloc (theArgs->nrOfMsgs * sizeof (HadTuQueue *));
-
-      for (i = 0; i < theArgs->nrOfMsgs; i++) {
-         char buf[_POSIX_PATH_MAX];
-
-         /* use shared mem name from CL if given */
-         sprintf (buf, "netqueue%s%d", theArgs->shmname, i);
-
-         if( theArgs->queueSize > 0 ) {
-           /* The queueSize is given in CL.  */
-           shmTrans[i] = ShmTrans_create (buf, 2 * theArgs->queueSize);
-         }
-         else{
-           shmTrans[i] = ShmTrans_create (buf, 2 * (long)theArgs->varQSize[i]);
-
-           if( shmTrans[i] == NULL ) {
-             fprintf ( stderr, "<E> ShmTrans_create: could not create buffer /dev/shm/%s.shm\n", buf );        
-           }      
-         }
-
-         hadTuQueue[i] = NULL;
-      }
-  }
-
-  theStats->evtsDiscarded     = Worker_addStatistic (worker, "evtsDiscarded");
-  theStats->evtsComplete      = Worker_addStatistic (worker, "evtsComplete");
-  theStats->evtsDataError     = Worker_addStatistic (worker, "evtsDataError");
-  theStats->evtsTagError      = Worker_addStatistic (worker, "evtsTagError");
-  theStats->bytesWritten      = Worker_addStatistic (worker, "bytesWritten");
-  theStats->runId             = Worker_addStatistic (worker, "runId");
-  (*theStats->runId)          = 0; /* initialize to zero */
-
-  unsigned long *retVal = NULL;
-  theStats->evtsRes = (unsigned long *) &retVal;
-
-  for (i = 0; i < NEVTIDS; i++) {
-    char buf[WORKER_MAX_NAME_LEN];
-    
-    sprintf (buf, "evtId%d", i);
-    theStats->evtId[i] = Worker_addStatistic (worker, buf);
-  }
-  for (i = 0; i < theArgs->nrOfMsgs; i++) {
-    char buf[WORKER_MAX_NAME_LEN];
-    
-    sprintf (buf, "trigNr%d", i);
-    theStats->trigNr[i] = Worker_addStatistic (worker, buf);
-  }
-
-  /* Add statistic for fill levels of buffers. */
-  for( i=0; i<theArgs->nrOfMsgs; i++ ) {
-    char buf[WORKER_MAX_NAME_LEN];
-    sprintf( buf, "evtbuildBuff%d", i );
-    theStats->evtbuildBuff[i] = Worker_addStatistic( worker, buf );
-  }
-
-  theStats->nrOfMsgs = Worker_addStatistic( worker, "nrOfMsgs" );
-  (*theStats->nrOfMsgs) = theArgs->nrOfMsgs;  
-
-  argsDump (theArgs);
-
-  Worker_initEnd (worker);
+       int i;
+       TheArgs theArgsS, *theArgs = &theArgsS;
+       TheStats theStatsS, *theStats = &theStatsS;
+       ShmTrans **shmTrans;
+       HadTuQueue **hadTuQueue;
+       Worker *worker;
 
-#ifdef RFIO
-  rfio_openConnection( theArgs );
-#endif
+       int scanWasSuccessful;
+       uint32_t currTrigNr;
+       uint32_t currTrigTag;
+       uint32_t currId;
 
-  currId = 0;
-  while (setjmp (terminateJmp) == 0)
-    {
-      void *evt;
-      void *subEvt;
-      int step;
-      int evtIsBroken = 0;
-      int dataError = 0;
-      int tagError = 0;
-
-      add2Stat( theArgs, theStats, 1, shmTrans );
-      statsBufferDump( theArgs, theStats, 1, hadTuQueue, shmTrans, argv[0] );
-      statsDump (theArgs, theStats, 1);
-
-      if (*theStats->bytesWritten == 0)
-       {
-
-         if( theArgs->epicsCtrl )
-           runNr = getRunId( theArgs );
-/*         res_time = ourTime = runNr + TIMEOFFSET; */
-/*       } */
-/*       else */
-         res_time = ourTime = time (NULL);
+       signal(SIGINT, sigHandler);
+       signal(SIGTERM, sigHandler);
+       signal(SIGHUP, sigHandler);
 
-         char s[20];
-         time_t my_time;
-         my_time = time(NULL);
-         strftime( s, 20, "%Y-%m-%dT%H:%M:%S", localtime(&my_time) );
-         printf("local time: %s\n", s);
-         strftime( s, 20, "%Y-%m-%dT%H:%M:%S", localtime(&ourTime) );
-         printf("ioc time: %s\n", s);
-         printf("ioc runid: %d\n", runNr);
+       openlog(argv[0], LOG_PID | LOG_PERROR, LOG_LOCAL0);
+       setlogmask(LOG_UPTO(LOG_INFO));
 
-         if (-1 == openFile (theArgs))
-           {
-             syslog (LOG_ERR, "error opening output file, exiting");
-             exit (EXIT_FAILURE);
-           }
+       argsDefault(theArgs);
+       argsFromParam(theArgs, argc, argv);
+
+       if (0 > argsFromCL(theArgs, argc, argv)) {
+               usage(argv[0]);
+               exit(EXIT_FAILURE);
+       }
 
-         storeInfoStart (argv[0], ourTime, theArgs);
+       if (argsCheck(theArgs)) {
+               sleep(10);
+               exit(EXIT_FAILURE);
+       }
+
+       for (i = 0; prioritynames[i].c_name != NULL && 0 != strcmp(prioritynames[i].c_name, theArgs->verbosity); i++) {
+       }
 
-         /* store simple start run info */
-         storeRunInfoStart(ourTime, theArgs);    
+       if (prioritynames[i].c_name == NULL)
+               exit(EXIT_FAILURE);
+       else
+               setlogmask(LOG_UPTO(prioritynames[i].c_val));
 
-         evt = newEvt (EvtDecoding_64bitAligned, EvtId_runStart);
+       /* normalize experiment id */
+       theArgs->expId[0] = tolower(theArgs->expId[0]);
+       theArgs->expId[1] = tolower(theArgs->expId[1]);
+       theArgs->expId[2] = '\0';
 
-         for (i = 0; i < theArgs->slowCtrlFileCnt; i++) {
-             evt = appendFile (evt, theArgs->slowCtrlFiles[i]);
-           }
+       /*   Create a name for a worker's shared memory segment
+        *   which consists of a program name and a base name provided by user 
+        */
+       char buf[_POSIX_PATH_MAX];
+       sprintf(buf, "%s%s", argv[0], theArgs->shmname);
 
-         (*theStats->bytesWritten) += Evt_size (evt);
-         writeFile (evt);
-         deleteEvt (evt);
+       if (NULL == (worker = Worker_initBegin(buf, sigHandler, theArgs->priority, theArgs->isStandalone))) {
+               syslog(LOG_ERR, "%s, %d: %s", __FILE__, __LINE__, strerror(errno));
+               exit(EXIT_FAILURE);
        }
 
-      if ((*theStats->evtsRes) == 0)
-       {
-#if 0
-         ourTime = time (NULL);
-#endif
-/* remote event server  - resdownscale - resnumevents*/
-         if (theArgs->resdownscale) {
-             if (-1 == openRESFile (theArgs)) {
-                 syslog (LOG_ERR, "error opening RES output file");
-                 exit (EXIT_FAILURE);
+       if (theArgs->no_rpc)
+               syslog(LOG_WARNING, "DISABLE of online service");
+       else if (-1 == initOnline())
+               syslog(LOG_WARNING, "unable to initialize online service");
+
+       if (theArgs->nrOfMsgs == 0) {
+               /*
+                  no '-m' option was on command line, we assume that the
+                  readout task (daq_readout) is running on the same node and
+                  communicates directly via shared memory
+                */
+               syslog(LOG_NOTICE, "nrOfMsgs not set, assuming readout is local");
+
+               shmTrans = malloc(sizeof(ShmTrans *));
+               hadTuQueue = malloc(sizeof(HadTuQueue *));
+
+               sprintf(buf, "subevtqueue%s", theArgs->shmname);
+               shmTrans[0] = ShmTrans_create(buf, 2 * theArgs->queueSize);
+
+               hadTuQueue[0] = NULL;
+               theArgs->nrOfMsgs = 1;
+       } else {
+               shmTrans = malloc(theArgs->nrOfMsgs * sizeof(ShmTrans *));
+               hadTuQueue = malloc(theArgs->nrOfMsgs * sizeof(HadTuQueue *));
+
+               for (i = 0; i < theArgs->nrOfMsgs; i++) {
+                       char buf[_POSIX_PATH_MAX];
+
+                       /* use shared mem name from CL if given */
+                       sprintf(buf, "netqueue%s%d", theArgs->shmname, i);
+
+                       if (theArgs->queueSize > 0) {
+                               /* The queueSize is given in CL.  */
+                               shmTrans[i] = ShmTrans_create(buf, 2 * theArgs->queueSize);
+                       } else {
+                               shmTrans[i] = ShmTrans_create(buf, 2 * (long) theArgs->varQSize[i]);
+
+                               if (shmTrans[i] == NULL) {
+                                       fprintf(stderr, "<E> ShmTrans_create: could not create buffer /dev/shm/%s.shm\n", buf);
+                               }
+                       }
+
+                       hadTuQueue[i] = NULL;
                }
+       }
+
+       theStats->evtsDiscarded = Worker_addStatistic(worker, "evtsDiscarded");
+       theStats->evtsComplete = Worker_addStatistic(worker, "evtsComplete");
+       theStats->evtsDataError = Worker_addStatistic(worker, "evtsDataError");
+       theStats->evtsTagError = Worker_addStatistic(worker, "evtsTagError");
+       theStats->bytesWritten = Worker_addStatistic(worker, "bytesWritten");
+       theStats->runId = Worker_addStatistic(worker, "runId");
+       (*theStats->runId) = 0;         /* initialize to zero */
+
+       unsigned long *retVal = NULL;
+       theStats->evtsRes = (unsigned long *) &retVal;
 
-             evt = newEvt (EvtDecoding_64bitAligned, EvtId_runStart);
-             (*theStats->evtsRes)++;
-             writeRESFile (evt);
-             deleteEvt (evt);
-           }
+       for (i = 0; i < NEVTIDS; i++) {
+               char buf[WORKER_MAX_NAME_LEN];
+
+               sprintf(buf, "evtId%d", i);
+               theStats->evtId[i] = Worker_addStatistic(worker, buf);
        }
+       for (i = 0; i < theArgs->nrOfMsgs; i++) {
+               char buf[WORKER_MAX_NAME_LEN];
 
-      evt = newEvt (EvtDecoding_64bitAligned, EvtId_data);
+               sprintf(buf, "trigNr%d", i);
+               theStats->trigNr[i] = Worker_addStatistic(worker, buf);
+       }
 
-      for (i = 0; i < theArgs->nrOfMsgs && !evtIsBroken; i += step)
-       {
-         uint32_t trigNr;
-         uint32_t trigTag;
+       /* Add statistic for fill levels of buffers. */
+       for (i = 0; i < theArgs->nrOfMsgs; i++) {
+               char buf[WORKER_MAX_NAME_LEN];
+               sprintf(buf, "evtbuildBuff%d", i);
+               theStats->evtbuildBuff[i] = Worker_addStatistic(worker, buf);
+       }
 
-         if (hadTuQueue[i] == NULL)
-           {
-             void *storage;
+       theStats->nrOfMsgs = Worker_addStatistic(worker, "nrOfMsgs");
+       (*theStats->nrOfMsgs) = theArgs->nrOfMsgs;
 
-             storage = ShmTrans_recv (shmTrans[i]);
-             hadTuQueue[i] = malloc (HadTuQueue_sizeOf ());
-             conHadTuQueue_voidP (hadTuQueue[i], storage);
-           }
-         subEvt = HadTuQueue_front (hadTuQueue[i]);
-#ifndef NDEBUG
-         syslog (LOG_DEBUG, "hadTuQueue[%d]: %p = subEvt: %s", i, subEvt,
-                 SubEvt_2charP (subEvt));
+       argsDump(theArgs);
+
+       Worker_initEnd(worker);
+
+#ifdef RFIO
+       rfio_openConnection(theArgs);
 #endif
 
-         (*theStats->trigNr[i]) = SubEvt_trigNr (subEvt);
+       currId = 0;
+       while (setjmp(terminateJmp) == 0) {
+               void *evt;
+               void *subEvt;
+               int step;
+               int evtIsBroken = 0;
+               int dataError = 0;
+               int tagError = 0;
 
-         if (i == 0)
-           {
-             currTrigNr = SubEvt_trigNr (subEvt) >> 8;
-             currTrigTag = SubEvt_trigNr (subEvt) & 0xff;
+               add2Stat(theArgs, theStats, 1, shmTrans);
+               statsBufferDump(theArgs, theStats, 1, hadTuQueue, shmTrans, argv[0]);
+               statsDump(theArgs, theStats, 1);
 
-             if (theArgs->evtId != 0)
-                 currId = theArgs->evtId;
-             else
-                 currId = SubEvt_pureId (subEvt);
+               if (*theStats->bytesWritten == 0) {
 
-             syslog (LOG_DEBUG,
-                     "currTrigNr: 0x%06x, currTrigTag 0x%02x, currId 0x%08x",
-                     currTrigNr, currTrigTag, currId);
-           }
+                       if (theArgs->epicsCtrl)
+                               runNr = getRunId(theArgs);
+/*         res_time = ourTime = runNr + TIMEOFFSET; */
+/*       } */
+/*       else */
+                       res_time = ourTime = time(NULL);
+
+                       char s[20];
+                       time_t my_time;
+                       my_time = time(NULL);
+                       strftime(s, 20, "%Y-%m-%dT%H:%M:%S", localtime(&my_time));
+                       printf("local time: %s\n", s);
+                       strftime(s, 20, "%Y-%m-%dT%H:%M:%S", localtime(&ourTime));
+                       printf("ioc time: %s\n", s);
+                       printf("ioc runid: %d\n", runNr);
 
-         trigNr = SubEvt_trigNr (subEvt) >> 8;
-         trigTag = SubEvt_trigNr (subEvt) & 0xff;
+                       if (-1 == openFile(theArgs)) {
+                               syslog(LOG_ERR, "error opening output file, exiting");
+                               exit(EXIT_FAILURE);
+                       }
 
-         if (trigNr == currTrigNr)
-           {
-             if (SubEvt_size (subEvt) > SubEvt_hdrSize ())
-               {
-                 /* sub evt is not empty */
-                 if (SubEvt_dataError (subEvt))
-                     dataError = 1;
+                       storeInfoStart(argv[0], ourTime, theArgs);
 
-                 if (trigTag != currTrigTag)
-                     tagError = 1;
+                       /* store simple start run info */
+                       storeRunInfoStart(ourTime, theArgs);
 
-                 evt = Evt_appendSubEvt (evt, subEvt);
+                       evt = newEvt(EvtDecoding_64bitAligned, EvtId_runStart);
+
+                       for (i = 0; i < theArgs->slowCtrlFileCnt; i++) {
+                               evt = appendFile(evt, theArgs->slowCtrlFiles[i]);
+                       }
+
+                       (*theStats->bytesWritten) += Evt_size(evt);
+                       writeFile(evt);
+                       deleteEvt(evt);
                }
-             HadTuQueue_pop (hadTuQueue[i]);
-             step = 1;
-           }
-         else if (trigNr < currTrigNr)
-           {
-             /* BUGBUG subevt discarded, not in statistic */
-             HadTuQueue_pop (hadTuQueue[i]);
-             step = 0;
-           }
-         else
-           {
-             evtIsBroken = 1;
-           }
-         if (HadTuQueue_empty (hadTuQueue[i]))
-           {
-             desHadTuQueue (hadTuQueue[i]);
-             free (hadTuQueue[i]);
-             hadTuQueue[i] = NULL;
-             ShmTrans_free (shmTrans[i]);
-           }
-       }
-      if (!evtIsBroken)
-       {
-         (*theStats->evtsComplete)++;
-
-         (*theStats->evtId[currId & (NEVTIDS - 1)])++;
-         Evt_setId (evt, currId & (NEVTIDS_IN_FILE - 1));
-
-         if (dataError)
-           {
-             Evt_setDataError (evt);
-             (*theStats->evtsDataError)++;
-           }
-         if (tagError)
-           {
-             Evt_setDataError (evt);
-             (*theStats->evtsTagError)++;
-#if CHECK_MISMATCH
-              is_mismatch_enough_to_stop(theArgs, theStats);
+
+               if ((*theStats->evtsRes) == 0) {
+#if 0
+                       ourTime = time(NULL);
 #endif
-           }
-         (*theStats->bytesWritten) += Evt_size (evt);
-         writeFile (evt);
-
-         if (theArgs->resdownscale)
-           {
-
-             /*
-              *   If the setting are as follows: 
-              *   EVENT_NUM_OFFSET        == 100
-              *   theArgs->resdown_offset == 5     (100/20)
-              *   then from each 100 events only 
-              *   first 5 events are written to refFile. 
-              */
-             if (((*theStats->evtsComplete) % EVENT_NUM_OFFSET) <
-                 theArgs->resdown_offset)
-               {
-                 (*theStats->evtsRes)++;
-                 writeRESFile (evt);
+/* remote event server  - resdownscale - resnumevents*/
+                       if (theArgs->resdownscale) {
+                               if (-1 == openRESFile(theArgs)) {
+                                       syslog(LOG_ERR, "error opening RES output file");
+                                       exit(EXIT_FAILURE);
+                               }
+
+                               evt = newEvt(EvtDecoding_64bitAligned, EvtId_runStart);
+                               (*theStats->evtsRes)++;
+                               writeRESFile(evt);
+                               deleteEvt(evt);
+                       }
                }
-           }
-         Evt_online (evt);
-       }
-      else
-       {
-         (*theStats->evtsDiscarded)++;
-#if CHECK_MISMATCH
-          is_mismatch_enough_to_stop(theArgs, theStats);
-#endif
-       }
 
-      deleteEvt (evt);
+               evt = newEvt(EvtDecoding_64bitAligned, EvtId_data);
 
-      newRunId = getRunId( theArgs );
+               for (i = 0; i < theArgs->nrOfMsgs && !evtIsBroken; i += step) {
+                       uint32_t trigNr;
+                       uint32_t trigTag;
 
-      /*
-       *  The following conditions mean:
-       *  theArgs->epicsCtrl == 1 && runNr < newRunId
-       *           New RUN Id was generated, close the file and open new one.
-       *  theArgs->epicsCtrl == 1 && (*theStats->bytesWritten) >= 1900000000
-       *           Due to whatever reason the file size exceeded an allowed limit,
-       *           close the file.
-       *  theArgs->epicsCtrl == 1 && runNr == 0
-       *           Something went wrong with sinchronization of Event Builders,
-       *           close the file.
-       */          
-      if ( ( !(theArgs->epicsCtrl) && (*theStats->bytesWritten) >= ((theArgs->maxFileSz) - (theArgs->queueSize)) ) ||
-          (theArgs->epicsCtrl && runNr < newRunId) ||
-          (theArgs->epicsCtrl && (*theStats->bytesWritten) >= 1900000000) ||
-          (theArgs->epicsCtrl && newRunId == 0))
-       {
-         evt = newEvt (EvtDecoding_64bitAligned, EvtId_runStop);
+                       if (hadTuQueue[i] == NULL) {
+                               void *storage;
 
-         for (i = 0; i < theArgs->slowCtrlFileCnt; i++)
-             evt = appendFile (evt, theArgs->slowCtrlFiles[i]);
+                               storage = ShmTrans_recv(shmTrans[i]);
+                               hadTuQueue[i] = malloc(HadTuQueue_sizeOf());
+                               conHadTuQueue_voidP(hadTuQueue[i], storage);
+                       }
+                       subEvt = HadTuQueue_front(hadTuQueue[i]);
+#ifndef NDEBUG
+                       syslog(LOG_DEBUG, "hadTuQueue[%d]: %p = subEvt: %s", i, subEvt, SubEvt_2charP(subEvt));
+#endif
+
+                       (*theStats->trigNr[i]) = SubEvt_trigNr(subEvt);
+
+                       if (i == 0) {
+                               currTrigNr = SubEvt_trigNr(subEvt) >> 8;
+                               currTrigTag = SubEvt_trigNr(subEvt) & 0xff;
+
+                               if (theArgs->evtId != 0)
+                                       currId = theArgs->evtId;
+                               else
+                                       currId = SubEvt_pureId(subEvt);
+
+                               syslog(LOG_DEBUG, "currTrigNr: 0x%06x, currTrigTag 0x%02x, currId 0x%08x", currTrigNr, currTrigTag, currId);
+                       }
+
+                       trigNr = SubEvt_trigNr(subEvt) >> 8;
+                       trigTag = SubEvt_trigNr(subEvt) & 0xff;
+
+                       if (trigNr == currTrigNr) {
+                               if (SubEvt_size(subEvt) > SubEvt_hdrSize()) {
+                                       /* sub evt is not empty */
+                                       if (SubEvt_dataError(subEvt))
+                                               dataError = 1;
+
+                                       if (trigTag != currTrigTag)
+                                               tagError = 1;
+
+                                       evt = Evt_appendSubEvt(evt, subEvt);
+                               }
+                               HadTuQueue_pop(hadTuQueue[i]);
+                               step = 1;
+                       } else if (trigNr < currTrigNr) {
+                               /* BUGBUG subevt discarded, not in statistic */
+                               HadTuQueue_pop(hadTuQueue[i]);
+                               step = 0;
+                       } else {
+                               evtIsBroken = 1;
+                       }
+                       if (HadTuQueue_empty(hadTuQueue[i])) {
+                               desHadTuQueue(hadTuQueue[i]);
+                               free(hadTuQueue[i]);
+                               hadTuQueue[i] = NULL;
+                               ShmTrans_free(shmTrans[i]);
+                       }
+               }
+               if (!evtIsBroken) {
+                       (*theStats->evtsComplete)++;
+
+                       (*theStats->evtId[currId & (NEVTIDS - 1)])++;
+                       Evt_setId(evt, currId & (NEVTIDS_IN_FILE - 1));
+
+                       if (dataError) {
+                               Evt_setDataError(evt);
+                               (*theStats->evtsDataError)++;
+                       }
+                       if (tagError) {
+                               Evt_setDataError(evt);
+                               (*theStats->evtsTagError)++;
+#if CHECK_MISMATCH
+                               is_mismatch_enough_to_stop(theArgs, theStats);
+#endif
+                       }
+                       (*theStats->bytesWritten) += Evt_size(evt);
+                       writeFile(evt);
+
+                       if (theArgs->resdownscale) {
+
+                               /*
+                                *   If the setting are as follows: 
+                                *   EVENT_NUM_OFFSET        == 100
+                                *   theArgs->resdown_offset == 5     (100/20)
+                                *   then from each 100 events only 
+                                *   first 5 events are written to refFile. 
+                                */
+                               if (((*theStats->evtsComplete) % EVENT_NUM_OFFSET) < theArgs->resdown_offset) {
+                                       (*theStats->evtsRes)++;
+                                       writeRESFile(evt);
+                               }
+                       }
+                       Evt_online(evt);
+               } else {
+                       (*theStats->evtsDiscarded)++;
+#if CHECK_MISMATCH
+                       is_mismatch_enough_to_stop(theArgs, theStats);
+#endif
+               }
 
-         (*theStats->bytesWritten) += Evt_size (evt);
-         writeFile (evt);
-         deleteEvt (evt);
+               deleteEvt(evt);
+
+               newRunId = getRunId(theArgs);
+
+               /*
+                *  The following conditions mean:
+                *  theArgs->epicsCtrl == 1 && runNr < newRunId
+                *           New RUN Id was generated, close the file and open new one.
+                *  theArgs->epicsCtrl == 1 && (*theStats->bytesWritten) >= 1900000000
+                *           Due to whatever reason the file size exceeded an allowed limit,
+                *           close the file.
+                *  theArgs->epicsCtrl == 1 && runNr == 0
+                *           Something went wrong with sinchronization of Event Builders,
+                *           close the file.
+                */
+               if ((!(theArgs->epicsCtrl) && (*theStats->bytesWritten) >= ((theArgs->maxFileSz) - (theArgs->queueSize))) ||
+                       (theArgs->epicsCtrl && runNr < newRunId) ||
+                       (theArgs->epicsCtrl && (*theStats->bytesWritten) >= 1900000000) || (theArgs->epicsCtrl && newRunId == 0)) {
+                       evt = newEvt(EvtDecoding_64bitAligned, EvtId_runStop);
+
+                       for (i = 0; i < theArgs->slowCtrlFileCnt; i++)
+                               evt = appendFile(evt, theArgs->slowCtrlFiles[i]);
+
+                       (*theStats->bytesWritten) += Evt_size(evt);
+                       writeFile(evt);
+                       deleteEvt(evt);
 
 /*       if( theArgs->epicsCtrl ) */
 /*         ourTime = newRunId + TIMEOFFSET; */
 /*       else */
-         ourTime = time (NULL);
+                       ourTime = time(NULL);
 
-         closeFile ();
+                       closeFile();
 
-         storeInfoStop (argv[0], ourTime, worker, theArgs);
+                       storeInfoStop(argv[0], ourTime, worker, theArgs);
 
-         /* store simple stop run info */
-         storeRunInfoStop(ourTime, theArgs, theStats);   
+                       /* store simple stop run info */
+                       storeRunInfoStop(ourTime, theArgs, theStats);
 
-         (*theStats->bytesWritten) = 0;
-         (*theStats->evtsComplete) = 0;
-         (*theStats->evtsDiscarded) = 0;
-         (*theStats->evtsDataError) = 0;
-         (*theStats->evtsTagError) = 0;
+                       (*theStats->bytesWritten) = 0;
+                       (*theStats->evtsComplete) = 0;
+                       (*theStats->evtsDiscarded) = 0;
+                       (*theStats->evtsDataError) = 0;
+                       (*theStats->evtsTagError) = 0;
 
-         for (i = 0; i < theArgs->nrOfMsgs; i++)
-             (*theStats->trigNr[i]) = 0;
+                       for (i = 0; i < theArgs->nrOfMsgs; i++)
+                               (*theStats->trigNr[i]) = 0;
 
-         for (i = 0; i < NEVTIDS; i++)
-             (*theStats->evtId[i]) = 0;
+                       for (i = 0; i < NEVTIDS; i++)
+                               (*theStats->evtId[i]) = 0;
 
-       }
+               }
 
-      if (theArgs->resdownscale)
-       {
-         /*
-          *   Number of events written to resFile is 
-          *   limited to resnumevents.
-          */
-         if ((*theStats->evtsRes) >= theArgs->resnumevents)
-           {
+               if (theArgs->resdownscale) {
+                       /*
+                        *   Number of events written to resFile is 
+                        *   limited to resnumevents.
+                        */
+                       if ((*theStats->evtsRes) >= theArgs->resnumevents) {
 #if 0
-             ourTime = time (NULL);
+                               ourTime = time(NULL);
 #endif
-             evt = newEvt (EvtDecoding_64bitAligned, EvtId_runStop);
-             writeRESFile (evt);
-             deleteEvt (evt);
-             closeRESFile (theArgs);
-             (*theStats->evtsRes) = 0;
-           }
-       }
-    }
-  
-  ourTime = time (NULL);
-  closeFile ();
+                               evt = newEvt(EvtDecoding_64bitAligned, EvtId_runStop);
+                               writeRESFile(evt);
+                               deleteEvt(evt);
+                               closeRESFile(theArgs);
+                               (*theStats->evtsRes) = 0;
+                       }
+               }
+       }
+
+       ourTime = time(NULL);
+       closeFile();
 
 #ifdef RFIO
-  rfio_closeConnection( theArgs ); 
+       rfio_closeConnection(theArgs);
 #endif
 
-  if (theArgs->resdownscale)
-      closeRESFile (theArgs);
+       if (theArgs->resdownscale)
+               closeRESFile(theArgs);
 
-  storeInfoStop (argv[0], ourTime, worker, theArgs);
+       storeInfoStop(argv[0], ourTime, worker, theArgs);
 
-  /* store simple stop run info */
-  storeRunInfoStop(ourTime, theArgs, theStats);
+       /* store simple stop run info */
+       storeRunInfoStop(ourTime, theArgs, theStats);
 
-  statsDump (theArgs, theStats, 1);
+       statsDump(theArgs, theStats, 1);
 
-  for (i = 0; i < theArgs->nrOfMsgs; i++)
-      ShmTrans_remove (shmTrans[i]);
+       for (i = 0; i < theArgs->nrOfMsgs; i++)
+               ShmTrans_remove(shmTrans[i]);
 
-  finiOnline ();
-  Worker_fini (worker);
-  exit (EXIT_SUCCESS);
+       finiOnline();
+       Worker_fini(worker);
+       exit(EXIT_SUCCESS);
 }