Index: /trunk/FACT++/src/EventBuilder.c
===================================================================
--- /trunk/FACT++/src/EventBuilder.c	(revision 12490)
+++ /trunk/FACT++/src/EventBuilder.c	(revision 12491)
@@ -158,8 +158,7 @@
 void* ETI_Malloc(int evtId, int evtIndex)
 {
-    int i,j;
-    for (i=0;i<numAllocatedChunks;i++) {
+    for (int i=0;i<numAllocatedChunks;i++) {
         if (EtiMemoryChunks[i].nFreeSlots > 0) {
-            for (j=0;j<EtiMemoryChunks[i].nSlots;j++)
+            for (int j=0;j<EtiMemoryChunks[i].nSlots;j++)
             {
                 if (EtiMemoryChunks[i].events[j] == -1)
@@ -204,5 +203,5 @@
     mBufferMapping[evtIndex].slot = 0;
 
-    for (i=1;i<numNewSlots;i++)
+    for (int i=1;i<numNewSlots;i++)
     {
         EtiMemoryChunks[numAllocatedChunks].pointers[i] = &(((char*)(EtiMemoryChunks[numAllocatedChunks].pointers[i-1]))[MAX_SLOT_SIZE]);
@@ -453,10 +452,7 @@
 // initialize mBuffer (mark all entries as unused\empty)
 
-   int i,j;
-   uint32_t actime;
-
-   actime = g_actTime + 50000000;
-
-   for (i = 0; i < MAX_EVT * MAX_RUN; i++) {
+   uint32_t actime = g_actTime + 50000000;
+
+   for (int i = 0; i < MAX_EVT * MAX_RUN; i++) {
       mBuffer[i].evNum = mBuffer[i].nRoi = -1;
       mBuffer[i].runNum = 0;
@@ -472,5 +468,5 @@
       //END ETIENNE
    }
-   for (j=0;j<MAX_CHUNKS;j++)
+   for (int j=0;j<MAX_CHUNKS;j++)
        EtiMemoryChunks[j].pointers[0] = NULL;
 
@@ -505,5 +501,5 @@
    int jold;
 
-   int i, k, jr, b, evFree;
+   int i, b, evFree;
    int headmem = 0;
    size_t needmem = 0;
@@ -520,5 +516,5 @@
    }
 
-   for (jr = 1; jr < 8; jr++) {
+   for (int jr = 1; jr < 8; jr++) {
       if (nRoi[jr] != nRoi[0]) {
          snprintf (str, MXSTR, "wrong nRoi[%d]: %d %d", jr, nRoi[jr],
@@ -542,5 +538,5 @@
    evFree = -1;
 
-   for (k = 0; k < MAX_RUN; k++) {
+   for (int k = 0; k < MAX_RUN; k++) {
       if (mBuffer[i].evNum == evID && mBuffer[i].runNum == runID) {     //event is already registered;
          // is it ok ????
@@ -588,5 +584,5 @@
    oldest = g_actTime + 1000;
    jold = -1;
-   for (k = 0; k < MAX_RUN; k++) {
+   for (int k = 0; k < MAX_RUN; k++) {
       if (runCtrl[k].runId == runID) {
 //         if (runCtrl[k].procId > 0) {   //run is closed -> reject
@@ -676,5 +672,5 @@
        mBuffer[i].fEvent = NULL;
        gj.usdMem = 0;
-       for (k=0;k<numAllocatedChunks;k++)
+       for (int k=0;k<numAllocatedChunks;k++)
            gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE;
        if (gj.usdMem > gj.maxMem)
@@ -722,13 +718,13 @@
    //flag all boards as unused
    mBuffer[i].nBoard = 0;
-   for (k = 0; k < NBOARDS; k++) {
+   for (int k = 0; k < NBOARDS; k++) {
       mBuffer[i].board[k] = -1;
    }
    //flag all pixels as unused
-   for (k = 0; k < NPIX; k++) {
+   for (int k = 0; k < NPIX; k++) {
       mBuffer[i].fEvent->StartPix[k] = -1;
    }
    //flag all TMark as unused
-   for (k = 0; k < NTMARK; k++) {
+   for (int k = 0; k < NTMARK; k++) {
       mBuffer[i].fEvent->StartTM[k] = -1;
    }
@@ -750,5 +746,5 @@
    //gj.usdMem += needmem + headmem + gi_maxSize;
    gj.usdMem = 0;
-   for (k=0;k<numAllocatedChunks;k++)
+   for (int k=0;k<numAllocatedChunks;k++)
    {
        gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE;
@@ -818,6 +814,5 @@
 //   gj.usdMem = gj.usdMem - freemem - headmem - gi_maxSize;
    gj.usdMem = 0;
-   int k;
-   for (k=0;k<numAllocatedChunks;k++)
+   for (int k=0;k<numAllocatedChunks;k++)
    {
        gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE;
@@ -840,10 +835,8 @@
 resetEvtStat ()
 {
-   int i;
-
-   for (i = 0; i < MAX_SOCK; i++)
+   for (int i = 0; i < MAX_SOCK; i++)
       gi.numRead[i] = 0;
 
-   for (i = 0; i < NBOARDS; i++) {
+   for (int i = 0; i < NBOARDS; i++) {
       gi.gotByte[i] = 0;
       gi.gotErr[i] = 0;
@@ -1228,23 +1221,14 @@
 
                   //End of the header. to channels now
-                  int eCount = 0;
                   int eStart = 36;
-                  int ePatchesCount = 0;
-                  int currentRoi;
-                  for (;ePatchesCount<4*9;ePatchesCount++)
+                  for (int ePatchesCount = 0; ePatchesCount<4*9;ePatchesCount++)
                   {
-                      rd[i].rBuf->S[eStart+eCount] = ntohs(rd[i].rBuf->S[eStart+eCount]);//id
-                      eCount++;
-                      rd[i].rBuf->S[eStart+eCount] = ntohs(rd[i].rBuf->S[eStart+eCount]);//start_cell
-                      eCount++;
-                      rd[i].rBuf->S[eStart+eCount] = ntohs(rd[i].rBuf->S[eStart+eCount]);//roi
-                      currentRoi = rd[i].rBuf->S[eStart+eCount];
-                      eCount++;
-                      rd[i].rBuf->S[eStart+eCount] = ntohs(rd[i].rBuf->S[eStart+eCount]);//filling
-                      eCount++;
-                      eCount+=currentRoi;//skip the pixel data
-                  }
-	//			  snprintf(str, MXSTR, "Swapped %d bytes", eCount*2);
-	//			  factOut(kError, 000, str);
+                      rd[i].rBuf->S[eStart+0] = ntohs(rd[i].rBuf->S[eStart+0]);//id
+                      rd[i].rBuf->S[eStart+1] = ntohs(rd[i].rBuf->S[eStart+1]);//start_cell
+                      rd[i].rBuf->S[eStart+2] = ntohs(rd[i].rBuf->S[eStart+2]);//roi
+                      rd[i].rBuf->S[eStart+3] = ntohs(rd[i].rBuf->S[eStart+3]);//filling
+
+                      eStart += 4+rd[i].rBuf->S[eStart+2];//skip the pixel data
+                  }
 
                   //channels done. footer now
@@ -1480,46 +1464,43 @@
                      }
                      goodhed++;
-                     //ETIENNE swap the header bytes already
-                     int eStart = 0;
-                     rd[i].rBuf->S[eStart+0] = ntohs(rd[i].rBuf->S[eStart+0]);//start_package_flag
-                     rd[i].rBuf->S[eStart+1] = ntohs(rd[i].rBuf->S[eStart+1]);//package_length
-                     rd[i].rBuf->S[eStart+2] = ntohs(rd[i].rBuf->S[eStart+2]);//version_no
-                     rd[i].rBuf->S[eStart+3] = ntohs(rd[i].rBuf->S[eStart+3]);//PLLLCK
-                     rd[i].rBuf->S[eStart+4] = ntohs(rd[i].rBuf->S[eStart+4]);//trigger_crc
-                     rd[i].rBuf->S[eStart+5] = ntohs(rd[i].rBuf->S[eStart+5]);//trigger_type
-                     rd[i].rBuf->I[eStart+3] = ntohl(rd[i].rBuf->I[eStart+3]);//trigger_id
-                     rd[i].rBuf->I[eStart+4] = ntohl(rd[i].rBuf->I[eStart+4]);//fad_evt_counter
-                     rd[i].rBuf->I[eStart+5] = ntohl(rd[i].rBuf->I[eStart+5]);//REFCLK_frequency
-                    rd[i].rBuf->S[eStart+12] = ntohs(rd[i].rBuf->S[eStart+12]);//board_id
-                     rd[i].rBuf->S[eStart+14] = ntohs(rd[i].rBuf->S[eStart+14]);//number_of_triggers_to_generate
-                     rd[i].rBuf->S[eStart+15] = ntohs(rd[i].rBuf->S[eStart+15]);//trigger_generator_prescaler
-                   //  int eInter = ntohl(rd[i].rBuf->I[eStart+8]);//DNA
-                    // rd[i].rBuf->I[eStart+8] = ntohl(rd[i].rBuf->I[eStart+9]);
-                     //rd[i].rBuf->I[eStart+9] = eInter;
-                     rd[i].rBuf->I[eStart+10] = ntohl(rd[i].rBuf->I[eStart+10]);//time
-                     rd[i].rBuf->I[eStart+11] = ntohl(rd[i].rBuf->I[eStart+11]);//runnumber;
-                     int eCount = 24;
-                     for (;eCount<24+NTemp;eCount++)
-                         rd[i].rBuf->S[eStart+eCount] = ntohs(rd[i].rBuf->S[eStart+eCount]);//drs_temperature
-                     for (; eCount < 24+NTemp+NDAC; eCount++)
-                         rd[i].rBuf->S[eStart+eCount] = ntohs(rd[i].rBuf->S[eStart+eCount]);//dac
-
-
- //                    rd[i].fadLen = rd[i].rBuf->S[1] * 2;
-//					 snprintf(str, MXSTR, "Size of the fad buffer: %d", rd[i].fadLen);
-//					 factOut(kError, 000, str);
-					 
+
+                     // Swap everything except start_package_flag.
+                     // It is to difficult to find out where it is used how,
+                     // but it doesn't really matter because it is not really
+                     // used anywehere else
+                     rd[i].rBuf->S[1]  = ntohs(rd[i].rBuf->S[1]);    // package_length
+                     rd[i].rBuf->S[2]  = ntohs(rd[i].rBuf->S[2]);    // version_no
+                     rd[i].rBuf->S[3]  = ntohs(rd[i].rBuf->S[3]);    // PLLLCK
+                     rd[i].rBuf->S[4]  = ntohs(rd[i].rBuf->S[4]);    // trigger_crc
+                     rd[i].rBuf->S[5]  = ntohs(rd[i].rBuf->S[5]);    // trigger_type
+
+                     rd[i].rBuf->S[12] = ntohs(rd[i].rBuf->S[12]);   // board id
+                     rd[i].rBuf->S[13] = ntohs(rd[i].rBuf->S[13]);   // adc_clock_phase_shift
+                     rd[i].rBuf->S[14] = ntohs(rd[i].rBuf->S[14]);   // number_of_triggers_to_generate
+                     rd[i].rBuf->S[15] = ntohs(rd[i].rBuf->S[15]);   // trigger_generator_prescaler
+
+                     rd[i].rBuf->I[3]  = ntohl(rd[i].rBuf->I[3]);    // trigger_id
+                     rd[i].rBuf->I[4]  = ntohl(rd[i].rBuf->I[4]);    // fad_evt_counter
+                     rd[i].rBuf->I[5]  = ntohl(rd[i].rBuf->I[5]);    // REFCLK_frequency
+
+                     rd[i].rBuf->I[10] = ntohl(rd[i].rBuf->I[10]);   // runnumber;
+                     rd[i].rBuf->I[11] = ntohl(rd[i].rBuf->I[11]);   // time;
+
+                     for (int s=24;s<24+NTemp+NDAC;s++)
+                         rd[i].rBuf->S[s] = ntohs(rd[i].rBuf->S[s]); // drs_temperature / dac
+ 
+                     rd[i].fadLen  = rd[i].rBuf->S[1] * 2;
                      rd[i].fadVers = rd[i].rBuf->S[2];
-                     rd[i].ftmTyp = rd[i].rBuf->S[5];
-                     rd[i].ftmID = rd[i].rBuf->I[3];    //(FTMevt)
-                     rd[i].evtID = rd[i].rBuf->I[4];    //(FADevt)
-                     rd[i].runID = rd[i].rBuf->I[11];
-                     rd[i].bufTyp = 1;  //ready to read full record
-                     rd[i].bufLen = rd[i].fadLen - rd[i].bufPos;
-
+                     rd[i].ftmTyp  = rd[i].rBuf->S[5];
+                     rd[i].ftmID   = rd[i].rBuf->I[3];    //(FTMevt)
+                     rd[i].evtID   = rd[i].rBuf->I[4];    //(FADevt)
+                     rd[i].runID   = rd[i].rBuf->I[11];
+                     rd[i].bufTyp  = 1;  //ready to read full record
+                     rd[i].bufLen  = rd[i].fadLen - rd[i].bufPos;
+
+#ifdef EVTDEBUG
                      int fadboard = rd[i].rBuf->S[12];
                      int fadcrate = fadboard / 256;
                      fadboard = (fadcrate * 10 + fadboard % 256);
-#ifdef EVTDEBUG
                      snprintf (str, MXSTR,
                                "sk %3d head: %5d %5d %5d %10d %4d %6d", i,
@@ -1534,8 +1515,9 @@
                      if (rd[i].bufLen <= head_len || rd[i].bufLen > MAX_LEN) {
                         snprintf (str, MXSTR,
-                                  "illegal event-length on port %d", i);
+                                  "Illegal event-length %d on port %d, %d expected.", rd[i].bufLen, i, head_len);
                         factOut (kFatal, 881, str);
                         rd[i].bufLen = 100000;  //?
                      }
+
                      int fadBoard = rd[i].rBuf->S[12];
                      debugHead (i, fadBoard, rd[i].rBuf);
