]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - RAW/dateStream.cxx
During simulation: fill STU region w/ non null time sums
[u/mrichter/AliRoot.git] / RAW / dateStream.cxx
index 006d14a78d917134c4bd08a8d79f6aa1a8cd35f1..f49eca0feba51efc1ddcadfba5cb183e9e8abe09 100644 (file)
@@ -20,7 +20,7 @@
 #include <assert.h>
 #include <ctype.h>
 #include <time.h>
-
+#include <cassert>
 #include "event.h"
 
 #define DESCRIPTION "DATE raw data stream simulator"
@@ -45,8 +45,10 @@ char fileHandlerIdent[]= "@(#)""" __FILE__ """: """ DESCRIPTION \
 const char *myName;
 int debug;
 FILE *outF;
-enum { unknown, ldc, gdc } workingAs;
-enum { collider, fixedTarget } workingMode;
+typedef enum { unknown, ldc, gdc } workingAsType;
+typedef enum { collider, fixedTarget } workingModeType;
+workingAsType workingAs;
+workingModeType workingMode;
 struct ldcDescriptorStruct {
   eventLdcIdType id;
   struct ldcDescriptorStruct *next;
@@ -58,6 +60,8 @@ struct gdcEventDescriptorStruct {
   struct gdcEventDescriptorStruct *next;
   struct eventHeaderStruct header;
   int loaded;
+  unsigned long32 detPattern;
+  eventTimestampType timestamp; 
 } *currGdc;
 struct ldcEventDescriptorStruct {
   struct equipmentEventDescriptorStruct *head;
@@ -66,6 +70,8 @@ struct ldcEventDescriptorStruct {
   eventLdcIdType id;
   struct eventHeaderStruct header;
   int loaded;
+  unsigned long32 detPattern;
+  eventTimestampType timestamp; 
 } *currLdc;
 struct equipmentEventDescriptorStruct {
   struct equipmentEventDescriptorStruct *next;
@@ -80,8 +86,10 @@ struct payloadDescriptorStruct {
   int size;
   void *data;
 } *payloadsHead, *payloadsTail;
-int lineNo;
+int lineNmb;
 eventGdcIdType currGdcId;
+unsigned long32 currDetPattern;
+eventTimestampType currTimestamp; 
 eventLdcIdType currLdcId;
 equipmentIdType currEquipmentId;
 int currRunNb;
@@ -94,6 +102,8 @@ eventIdType currEventId;
 int gotAliceTrigger;
 int bufferData;
 
+struct commonDataHeaderStruct *cdhRef = NULL;
+
 void dumpPayload( const struct payloadDescriptorStruct *p ) {
   char *c;
   int i;
@@ -126,7 +136,7 @@ void dumpPayload( const struct payloadDescriptorStruct *p ) {
       }
       if ( i < p->size ) {
        int j = 0;
-      
+
        printf( "\n       " );
        while ( i != p->size ) {
          printf( "%02x ", *((char *)p->data + p->size - j - 1) & 0xff );
@@ -151,16 +161,16 @@ void dumpEvents() {
            gdc = gdc->next ) {
        struct ldcEventDescriptorStruct *ldc;
 
-       printf( " GDC (%p)\n", gdc );
+       printf( " GDC (%p)\n", (void*)gdc );
        for ( ldc = gdc->head; ldc != NULL; ldc = ldc->next ) {
          struct equipmentEventDescriptorStruct *eq;
            
-         printf( "   LDC (%p): %d\n", ldc, ldc->id );
+         printf( "   LDC (%p): %d\n", (void*)ldc, ldc->id );
          for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
            printf( "     EQUIPMENT (%p): %d PAYLOAD (%p):",
-                   eq,
+                   (void*)eq,
                    eq->id,
-                   eq->payload );
+                   (void*)eq->payload );
            fflush( stdout );
            printf( "\"%s\" (%d bytes)\n",
                    eq->payload->fileName,
@@ -181,9 +191,9 @@ void dumpEvents() {
        printf( "   LDC\n" );
        for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
          printf( "     EQUIPMENT (%p): %d PAYLOAD (%p):",
-                 eq,
+                 (void*)eq,
                  eq->id,
-                 eq->payload );
+                 (void*)eq->payload );
          fflush( stdout );
          printf( "\"%s\" (%d bytes)\n",
                  eq->payload->fileName,
@@ -200,7 +210,7 @@ void dumpEvents() {
 void getLine( char *line, const int maxSize ) {
   int read;
   int c;
-  
+
   for ( read = 0; !feof( stdin ) && !ferror( stdin ) && read != maxSize; read++ ) {
     if ( (line[read] = getchar()) == '\n' ) break;
   }
@@ -215,13 +225,13 @@ void getLine( char *line, const int maxSize ) {
   if ( read == maxSize && line[read] != '\n' ) {
     fprintf( stderr,
             "%s: Input line # %d too long (%d chars max)\n",
-            myName, lineNo, maxSize-1 );
+            myName, lineNmb, maxSize-1 );
     exit( 1 );
   }
   line[ read ] = 0;
   DBG_VERBOSE {
     if ( !( read == 0 && feof( stdin ) ) ) {
-      printf( "%d) [%3d] \"%s\"", lineNo, read, line );
+      printf( "%d) [%3d] \"%s\"", lineNmb, read, line );
     }
   }
   for ( c = 0; c != read; c++ ) {
@@ -289,11 +299,13 @@ void createNewEvent() {
     } else {
       struct ldcEventDescriptorStruct *q =
        (struct ldcEventDescriptorStruct *)eventsTail;
-      
+
       q->next = p;
       eventsTail = p;
     }
     p->id = currLdcId;
+    p->detPattern = currDetPattern;
+    p->timestamp = currTimestamp;
   } else if ( workingAs == gdc ) {
     struct gdcEventDescriptorStruct *p;
 
@@ -314,10 +326,12 @@ void createNewEvent() {
     } else {
       struct gdcEventDescriptorStruct *q =
        (struct gdcEventDescriptorStruct *)eventsTail;
-      
+
       q->next = p;
       eventsTail = p;
     }
+    p->detPattern = currDetPattern;
+    p->timestamp = currTimestamp;
   }
 } /* End of createNewEvent */
 
@@ -334,6 +348,8 @@ void createNewLdcEvent() {
     exit( 1 );
   }
   p->id = currLdcId;
+  p->detPattern = currDetPattern;
+  p->timestamp = currTimestamp;
   p->head = p->tail = NULL;
   p->next = NULL;
   gdcDesc = (struct gdcEventDescriptorStruct *)eventsTail;
@@ -349,12 +365,12 @@ void createNewLdcEvent() {
 void loadBuffer( struct payloadDescriptorStruct * const payload ) {
   FILE *f;
   int bytesRead;
-  
+
   if ( (f = fopen( payload->fileName, "r" )) == NULL ) {
     fprintf( stderr,
             "%s: line:%d payload file \"%s\" not found or not readable, errno:%d. ",
             myName,
-            lineNo,
+            lineNmb,
             payload->fileName,
             errno );
     perror( "System-dependent error " );
@@ -364,7 +380,7 @@ void loadBuffer( struct payloadDescriptorStruct * const payload ) {
     fprintf( stderr,
             "%s: line:%d Failed to malloc for payload file \"%s\" size:%d errno:%d ",
             myName,
-            lineNo,
+            lineNmb,
             payload->fileName,
             payload->size,
             errno );
@@ -375,7 +391,7 @@ void loadBuffer( struct payloadDescriptorStruct * const payload ) {
     fprintf( stderr,
             "%s: line:%d Failed to read payload file \"%s\" size:%d requested:1 got:%d feof:%s ferror:%s errno:%d ",
             myName,
-            lineNo,
+            lineNmb,
             payload->fileName,
             payload->size,
             bytesRead,
@@ -417,7 +433,7 @@ void loadPayload( const char *fileName ) {
   }
   if ( payload == NULL ) {
     FILE *f;
-    
+
     if ( (payload = (struct payloadDescriptorStruct *)malloc( sizeof( *payload ) ))
           == NULL ) {
       fprintf( stderr,
@@ -437,7 +453,7 @@ void loadPayload( const char *fileName ) {
       fprintf( stderr,
               "%s: line:%d payload file \"%s\" not found or not readable, errno:%d. ",
               myName,
-              lineNo,
+              lineNmb,
               fileName,
               errno );
       perror( "System-dependent error " );
@@ -447,7 +463,7 @@ void loadPayload( const char *fileName ) {
       fprintf( stderr,
               "%s: line:%d Failed to seek payload file \"%s\" errno:%d ",
               myName,
-              lineNo,
+              lineNmb,
               fileName,
               errno );
       perror( "System-dependent error " );
@@ -457,7 +473,7 @@ void loadPayload( const char *fileName ) {
       fprintf( stderr,
               "%s: line:%d Failed to get file \"%s\" size size:%d errno:%d ",
               myName,
-              lineNo,
+              lineNmb,
               fileName,
               payload->size,
               errno );
@@ -467,7 +483,7 @@ void loadPayload( const char *fileName ) {
     payload->fileSize = payload->size;
     while ( (payload->size & 3) != 0 ) payload->size++;
     fclose( f );
-    
+
     if ( bufferData ) {
       loadBuffer( payload );
     } else {
@@ -483,21 +499,23 @@ void loadPayload( const char *fileName ) {
     }
     DBG_VERBOSE {
       int b, n;
-      
+
       printf( "%d)       Payload \"%s\" loaded at %p\n",
-             lineNo,
+             lineNmb,
              fileName,
-             payload );
+             (void*)payload );
       if ( bufferData ) {
-       if ( handleCDH ) {
+       if ( handleCDH &&
+            strncmp(fileName,"TRG_",4) != 0 ) {
          struct commonDataHeaderStruct *cdh =
            (struct commonDataHeaderStruct *)payload->data;
 
          printf( " CDH: blockLenght:%d=0x%08x ",
                  cdh->cdhBlockLength, cdh->cdhBlockLength );
          if ( cdh->cdhBlockLength < sizeof( *cdh ) ) {
-           printf( "TOO SMALL (minimum:%d=0x%08x)\n",
-                   sizeof( *cdh ), sizeof( *cdh ) );
+           printf( "TOO SMALL (minimum:%ld=0x%08lx)\n",
+                  (unsigned long)sizeof( *cdh ),
+                  (unsigned long)sizeof( *cdh ) );
          } else {
            printf( "version:%d=0x%x ", cdh->cdhVersion, cdh->cdhVersion );
            if ( cdh->cdhVersion != CDH_VERSION ) {
@@ -643,9 +661,9 @@ void loadPayload( const char *fileName ) {
   } else {
     DBG_VERBOSE
       printf( "%d)       Payload \"%s\" already loaded at %p\n",
-             lineNo,
+             lineNmb,
              fileName,
-             payload );
+             (void*)payload );
   }
 
   currEvent->payload = payload;
@@ -672,7 +690,7 @@ void parseEquipment( char * const line ) {
   p = line;
   while ( (keyword = strtok_r( p, " \t", &p )) != NULL ) {
     DBG_VERBOSE printf( "%d)     Equipment - Keyword:\"%s\"\n",
-                       lineNo,
+                       lineNmb,
                        keyword );
     if ( strcasecmp( "id", keyword ) == 0 ) {
       char *idNum;
@@ -681,19 +699,19 @@ void parseEquipment( char * const line ) {
        fprintf( stderr,
                 "%s: line:%d EQUIPMENT declaration, ID needed",
                 myName,
-                lineNo );
+                lineNmb );
        exit( 1 );
       }
       if ( sscanf( idNum, "%d", &currEquipmentId ) != 1 ) {
        fprintf( stderr,
                 "%s: line:%d EQUIPMENT declaration, numeric ID needed (%s)",
                 myName,
-                lineNo,
+                lineNmb,
                 idNum );
        exit( 1 );
       }
       DBG_VERBOSE printf( "%d)     EQUIPMENT - ID:%d\n",
-                         lineNo,
+                         lineNmb,
                          currEquipmentId );
     } else if ( strncasecmp( "pay", keyword, 3 ) == 0 ) {
       char *fileName;
@@ -702,17 +720,17 @@ void parseEquipment( char * const line ) {
        fprintf( stderr,
                 "%s line:%d Payload without filename found\n",
                 myName,
-                lineNo );
+                lineNmb );
        exit( 1 );
       }
       DBG_VERBOSE printf( "%d)     Equipment - Payload:\"%s\"\n",
-                         lineNo,
+                         lineNmb,
                          fileName );
       if ( payloadFound ) {
        fprintf( stderr,
                 "%s line:%d Payload with multiple filenames found\n",
                 myName,
-                lineNo );
+                lineNmb );
        exit( 1 );
       }
       loadPayload( fileName );
@@ -721,7 +739,7 @@ void parseEquipment( char * const line ) {
       fprintf( stderr,
               "%s: line:%d Equipment declaration, unknown keyword \"%s\"\n",
               myName,
-              lineNo,
+              lineNmb,
               keyword );
       exit( 1 );
     }
@@ -730,7 +748,7 @@ void parseEquipment( char * const line ) {
     fprintf( stderr,
             "%s: line:%d Equipment without payload found\n",
             myName,
-            lineNo );
+            lineNmb );
     exit( 1 );
   }
 
@@ -747,7 +765,7 @@ void parseEquipment( char * const line ) {
 void parseGdc( char * const line ) {
   char *p;
   char *keyword;
-  
+
   p = line;
   while ( (keyword = strtok_r( p, " \t", &p )) != NULL ) {
     if ( strcasecmp( "id", keyword ) == 0 ) {
@@ -757,25 +775,69 @@ void parseGdc( char * const line ) {
        fprintf( stderr,
                 "%s: line:%d GDC declaration, ID needed",
                 myName,
-                lineNo );
+                lineNmb );
        exit( 1 );
       }
-      if ( sscanf( idNum, "%d", &currGdcId ) != 1 ) {
+      int inCurrGdcId;
+      if ( sscanf( idNum, "%d", &inCurrGdcId ) != 1 ) {
        fprintf( stderr,
                 "%s: line:%d GDC declaration, numeric ID needed (%s)",
                 myName,
-                lineNo,
+                lineNmb,
                 idNum );
        exit( 1 );
       }
+      currGdcId = (eventGdcIdType)inCurrGdcId;
       DBG_VERBOSE printf( "%d)     GDC - ID:%d\n",
-                         lineNo,
+                         lineNmb,
                          currGdcId );
+    } else if ( strcasecmp( "DetectorPattern", keyword ) == 0 ) {
+      char *detPattern;
+
+      if ( (detPattern = strtok_r( p, " \t", &p )) == NULL ) {
+       fprintf( stderr,
+                "%s: line:%d GDC declaration, DetectorPattern needed",
+                myName,
+                lineNmb );
+       exit( 1 );
+      }
+      if ( sscanf( detPattern, "%u", &currDetPattern ) != 1 ) {
+       fprintf( stderr,
+                "%s: line:%d GDC declaration, numeric DetectorPattern needed (%s)",
+                myName,
+                lineNmb,
+                detPattern );
+       exit( 1 );
+      }
+      DBG_VERBOSE printf( "%d)     GDC - DetectorPattern:%u\n",
+                         lineNmb,
+                         currDetPattern );
+    } else if ( strcasecmp( "Timestamp", keyword ) == 0 ) {
+      char *timestamp;
+
+      if ( (timestamp = strtok_r( p, " \t", &p )) == NULL ) {
+       fprintf( stderr,
+                "%s: line:%d GDC declaration, Timestamp needed",
+                myName,
+                lineNmb );
+       exit( 1 );
+      }
+      if ( sscanf( timestamp, "%u", &currTimestamp ) != 1 ) {
+       fprintf( stderr,
+                "%s: line:%d GDC declaration, numeric Timestamp needed (%s)",
+                myName,
+                lineNmb,
+                timestamp );
+       exit( 1 );
+      }
+      DBG_VERBOSE printf( "%d)     GDC - Timestamp:%u\n",
+                         lineNmb,
+                         currTimestamp );
     } else {
       fprintf( stderr,
               "%s: line:%d GDC declaration, unknown keyword \"%s\"\n",
               myName,
-              lineNo,
+              lineNmb,
               keyword );
       exit( 1 );
     }  
@@ -785,7 +847,7 @@ void parseGdc( char * const line ) {
 void parseLdc( char * const line ) {
   char *p;
   char *keyword;
-  
+
   p = line;
   while ( (keyword = strtok_r( p, " \t", &p )) != NULL ) {
     if ( strcasecmp( "id", keyword ) == 0 ) {
@@ -795,25 +857,27 @@ void parseLdc( char * const line ) {
        fprintf( stderr,
                 "%s: line:%d LDC declaration, ID needed",
                 myName,
-                lineNo );
+                lineNmb );
        exit( 1 );
       }
-      if ( sscanf( idNum, "%d", &currLdcId ) != 1 ) {
+      int inCurrLdcId;
+      if ( sscanf( idNum, "%d", &inCurrLdcId ) != 1 ) {
        fprintf( stderr,
                 "%s: line:%d LDC declaration, numeric ID needed (%s)",
                 myName,
-                lineNo,
+                lineNmb,
                 idNum );
        exit( 1 );
       }
+      currLdcId = (eventLdcIdType)inCurrLdcId;
       DBG_VERBOSE printf( "%d)     LDC - ID:%d\n",
-                         lineNo,
+                         lineNmb,
                          currLdcId );
     } else {
       fprintf( stderr,
               "%s: line:%d LDC declaration, unknown keyword \"%s\"\n",
               myName,
-              lineNo,
+              lineNmb,
               keyword );
       exit( 1 );
     }  
@@ -825,20 +889,22 @@ void parseRules() {
 
   currLdcId = HOST_ID_MIN;
   currGdcId = HOST_ID_MIN;
+  currDetPattern = 0;
+  currTimestamp = 0;
 
-  for ( lineNo = 1; !feof( stdin ); lineNo++ ) {
+  for ( lineNmb = 1; !feof( stdin ); lineNmb++ ) {
     getLine( line, sizeof(line) );
     if ( strlen(line) != 0 ) {
       char *p;
       char *keyword;
 
       if ( (keyword = strtok_r( line, " \t", &p )) != NULL ) {
-       DBG_VERBOSE printf( "%d)   Keyword:\"%s\"\n", lineNo, keyword );
+       DBG_VERBOSE printf( "%d)   Keyword:\"%s\"\n", lineNmb, keyword );
        if ( strcasecmp( "gdc", keyword ) == 0 ) {
          if ( workingAs != gdc && workingAs != unknown ) {
            fprintf( stderr,
                     "%s: line:%d GDC found when working in non-GDC mode (e.g. as a LDC)\n",
-                    myName, lineNo );
+                    myName, lineNmb );
            exit( 1 );
          }
          workingAs = gdc;
@@ -851,7 +917,7 @@ void parseRules() {
          if ( workingAs != gdc && workingAs != ldc && workingAs != unknown ) {
            fprintf( stderr,
                     "%s: line:%d LDC found when working in non-LDC/GDC mode\n",
-                    myName, lineNo );
+                    myName, lineNmb );
            exit( 1 );
          }
          if ( workingAs == unknown ) workingAs = ldc;
@@ -872,7 +938,7 @@ void parseRules() {
            fprintf( stderr,
                     "%s: line:%d Unexpected EQUIPMENT declaration (LDC or GDC needed first)\n",
                     myName,
-                    lineNo );
+                    lineNmb );
            exit( 1 );
          }
          parseEquipment( p );
@@ -881,26 +947,26 @@ void parseRules() {
          fprintf( stderr,
                   "%s: line:%d Parse error in \"%s\" unknown keyword\n",
                   myName,
-                  lineNo,
+                  lineNmb,
                   keyword );
          exit( 1 );
        }
       }
     }
-  } while ( !feof( stdin ) );
-  lineNo -= 2;
+  } while ( !feof( stdin ) ) {}
+  lineNmb -= 2;
 
   DBG_VERBOSE {
     printf( "End of parse: %d line%s found\n",
-           lineNo,
-           lineNo != 1 ? "s" : "" );
+           lineNmb,
+           lineNmb != 1 ? "s" : "" );
     printf( "Working as %s\n",
            workingAs == gdc ? "GDC" :
             workingAs == ldc ? "LDC" :
              "UNKNOWN" );
     if ( workingAs == gdc ) {
       struct ldcDescriptorStruct *ldc;
-      
+
       printf( "LDCs (%d):", numOfLdcs );
       for ( ldc = ldcsHead; ldc != NULL; ldc = ldc->next ) {
        printf( " %d", ldc->id );
@@ -917,11 +983,11 @@ void parseRules() {
 
   if ( workingAs == gdc ) {
     struct ldcDescriptorStruct *ldc;
-    
+
     assert( ldcsHead != NULL );
     assert( ldcsTail != NULL );
     assert( ldcsTail->next == NULL );
-    for ( ldc = ldcsHead; ldc->next != NULL; ldc = ldc->next );
+    for ( ldc = ldcsHead; ldc->next != NULL; ldc = ldc->next ) {}
     assert ( ldc == ldcsTail );
   }
 
@@ -934,18 +1000,6 @@ void parseRules() {
        || (eventsHead != NULL && eventsTail != NULL) );
 } /* End of parseRules */
 
-void loadTimestamp( struct eventHeaderStruct * const ev ) {
-  time_t t;
-  
-  if ( time( &t ) == (time_t)-1 ) {
-    fprintf( stderr,
-            "%s: failed to get system time errno:%d (%s)\n",
-            myName, errno, strerror( errno ) );
-    exit( 1 );
-  }
-  ev->eventTimestamp = (eventTimestampType)t;
-} /* End of loadTimestamp */
-
 void initEvent( struct eventHeaderStruct * const ev ) {
   memset( ev, 0, sizeof( *ev ) );
 
@@ -961,23 +1015,66 @@ void initEvent( struct eventHeaderStruct * const ev ) {
     SET_SYSTEM_ATTRIBUTE( ev->eventTypeAttribute, ATTR_ORBIT_BC );
   ev->eventLdcId = VOID_ID;
   ev->eventGdcId = VOID_ID;
-  loadTimestamp( ev );
 } /* End of initEvent */
 
+int Swap(int x)
+{
+   // Swap the endianess of the integer value 'x'
+
+   return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) <<  8) |
+           ((x & 0x00ff0000U) >>  8) | ((x & 0xff000000U) >> 24));
+}
+
 void outputEvent( const void * const ev,
                  const int size ) {
   int done;
 
   DBG_VERBOSE {
-    const long32 * const v = (long32 *)ev;
+    const long32 * const v = (long32 *)ev; 
     printf( "Writing %d bytes @ %p (%d)\n", size, ev, *v );
   }
-  
-  if ( (done = fwrite( ev, size, 1, outF )) != 1 ) {
-    fprintf( stderr,
-            "%s: failed to write event size:%d bytes, errno:%d (%s)\n",
-            myName, size, errno, strerror( errno ) );
-    exit( 1 );
+
+  // .............................Test endianess..............................
+  int temp = 1;
+  char* ptemp = (char*) &temp;
+
+  if (ptemp[0]!=1) { // Mac platform: ptemp != 1..............................................................................
+     int  bufSize= size; if (bufSize > (int) sizeof(eventHeaderStruct)) { bufSize = sizeof(eventHeaderStruct); }
+     char* evTemp = (char*) malloc (bufSize);
+     memcpy(evTemp, ev, bufSize);
+
+     if ((bufSize % sizeof(int)) != 0) {
+            fprintf( stderr, "%s: size of the input buffer ev is not multiple of 4 (size = %d)\n", myName, bufSize);
+            exit( 1 );
+          }
+     else {
+            // Invert header to evTemp.....................................................
+            int* buf = (int*) evTemp; 
+            for (int i=0; i < (int) (bufSize / sizeof(int)); i++, buf++) {
+                 int value = Swap(*buf); 
+                 memcpy(evTemp + (i * sizeof(int)), &value, sizeof(int)); 
+            }
+
+            // Write inverted header to file...............................................
+            if ((done = fwrite( evTemp, bufSize, 1, outF )) != 1 ) {
+                 fprintf( stderr, "%s: failed to write inverted header. event size:%d bytes, errno:%d (%s)\n", myName, size, errno, strerror( errno ) );
+                 exit( 1 );
+            }
+
+            if (size > bufSize) {  // Still theraw-data payload to write (but not inverted, since it is inverted eariler).............
+                if ((done = fwrite( (char*)ev + bufSize, size - bufSize, 1, outF )) != 1 ) {
+                    fprintf( stderr, "%s: failed to write additional event size:%d bytes, errno:%d (%s)\n", myName, size, errno, strerror( errno ) );
+                    exit( 1 );
+               }
+            }
+     }
+     free(evTemp);
+  }
+  else {             // Intel platform: ptemp == 1............................................................................
+     if ((done = fwrite( ev, size, 1, outF )) != 1 ) {
+          fprintf( stderr, "%s: failed to write event size:%d bytes, errno:%d (%s)\n", myName, size, errno, strerror( errno ) );
+          exit( 1 );
+     }
   }
 } /* End of outputEvent */
 
@@ -987,7 +1084,7 @@ void createSorAndEor( const int sor ) {
   struct eventHeaderStruct sev;
 
   assert( workingAs == ldc || workingAs == gdc );
-  
+
   if ( !createSorEor ) return;
   ev = (struct eventHeaderStruct *)event;
   initEvent( ev );
@@ -1010,22 +1107,18 @@ void createSorAndEor( const int sor ) {
     currLdc = currGdc->head;
   }
   ev->eventLdcId = currLdc->id;
-  
+
   if ( workingAs == ldc ) {
-    loadTimestamp( ev );
     outputEvent( ev, ev->eventSize );
   }
   if ( workingAs == gdc ) {
     struct ldcDescriptorStruct *ldc;
 
-    loadTimestamp( ev );
-    
     sev.eventSize = sizeof( sev ) + numOfLdcs * ev->eventSize;
     sev.eventType = sor ? START_OF_RUN : END_OF_RUN ;
     COPY_EVENT_ID( ev->eventId, sev.eventId );
     COPY_SYSTEM_ATTRIBUTES( ev->eventTypeAttribute, sev.eventTypeAttribute );
     SET_SYSTEM_ATTRIBUTE( sev.eventTypeAttribute, ATTR_SUPER_EVENT );
-    loadTimestamp( &sev );
     outputEvent( &sev, sizeof( sev ) );
 
     ev->eventGdcId = currGdcId;
@@ -1034,20 +1127,17 @@ void createSorAndEor( const int sor ) {
       outputEvent( ev, ev->eventSize );
     }
   }
-  
+
   ADD_EVENT_ID( ev->eventId, oneEventDelta );
   ev->eventSize = ev->eventSize / 2;
   ev->eventType = sor ? START_OF_RUN_FILES : END_OF_RUN_FILES;
   CLEAR_SYSTEM_ATTRIBUTE( ev->eventTypeAttribute, ATTR_P_START );
   if ( workingAs == ldc ) {
-    loadTimestamp( ev );
     outputEvent( ev, ev->eventSize );
   }
   if ( workingAs == gdc ) {
     struct ldcDescriptorStruct *ldc;
 
-    loadTimestamp( ev );
-    
     sev.eventSize = ev->eventSize;
     sev.eventType = sor ? START_OF_RUN_FILES : END_OF_RUN_FILES;
     COPY_EVENT_ID( ev->eventId, sev.eventId );
@@ -1061,12 +1151,9 @@ void createSorAndEor( const int sor ) {
     COPY_EVENT_ID( ev->eventId, sev.eventId );
     COPY_SYSTEM_ATTRIBUTES( ev->eventTypeAttribute, sev.eventTypeAttribute );
     SET_SYSTEM_ATTRIBUTE( sev.eventTypeAttribute, ATTR_SUPER_EVENT );
-    
-    loadTimestamp( &sev );
 
     ev->eventGdcId = currGdcId;
     for ( ldc = ldcsHead; ldc != NULL; ldc = ldc->next ) {
-      loadTimestamp( &sev );
       outputEvent( &sev, sizeof( sev ) );
       ev->eventLdcId = ldc->id;
       outputEvent( ev, ev->eventSize );
@@ -1078,21 +1165,17 @@ void createSorAndEor( const int sor ) {
   ev->eventType = sor ? START_OF_RUN : END_OF_RUN;
   SET_SYSTEM_ATTRIBUTE( ev->eventTypeAttribute, ATTR_P_END );
   if ( workingAs == ldc ) {
-    loadTimestamp( ev );
     outputEvent( ev, ev->eventSize );
   }
   if ( workingAs == gdc ) {
     struct ldcDescriptorStruct *ldc;
 
-    loadTimestamp( ev );
-
     sev.eventSize = sizeof( sev ) + numOfLdcs * ev->eventSize;
     sev.eventType = sor ? START_OF_RUN : END_OF_RUN;
     COPY_EVENT_ID( ev->eventId, sev.eventId );
     COPY_SYSTEM_ATTRIBUTES( ev->eventTypeAttribute, sev.eventTypeAttribute );
     SET_SYSTEM_ATTRIBUTE( sev.eventTypeAttribute, ATTR_SUPER_EVENT );
-    loadTimestamp( &sev );
-    
+
     outputEvent( &sev, sizeof( sev ) );
 
     for ( ldc = ldcsHead; ldc != NULL; ldc = ldc->next ) {
@@ -1111,9 +1194,13 @@ void createEor() {
 } /* End of createEor */
 
 void loadCdh( struct commonDataHeaderStruct * const cdh,
-                    eventIdType            * const eventId ) {
+                    eventIdType            * const eventId,
+                    equipmentIdType id ) {
   if ( !handleCDH ) return;
 
+  // CTP raw-data does not contain CDH
+  if ( id == 4352) return;
+
   if ( gotAliceTrigger ) {
     cdh->cdhEventId1 = EVENT_ID_GET_BUNCH_CROSSING( *eventId );
     cdh->cdhEventId2 = EVENT_ID_GET_ORBIT( *eventId );
@@ -1124,7 +1211,8 @@ void loadCdh( struct commonDataHeaderStruct * const cdh,
   cdh->cdhMiniEventId = cdh->cdhEventId1;
 }
 void decodeCDH( struct ldcEventDescriptorStruct       * const ldc,
-               const struct payloadDescriptorStruct  * const payloadDesc );
+               const struct payloadDescriptorStruct  * const payloadDesc,
+               equipmentIdType id );
 
 void createEvent( void ) {
   assert( workingAs == ldc || workingAs == gdc );
@@ -1132,14 +1220,12 @@ void createEvent( void ) {
   /* Step 1: load all buffers (if needed) and compose the GDC/LDC headers */
   if ( workingAs == gdc ) {
     struct ldcEventDescriptorStruct *ldc;
-    
+
     for( ldc = currGdc->head; ldc != NULL; ldc = ldc->next ) {
       COPY_EVENT_ID( currEventId, ldc->header.eventId );
-      loadTimestamp( &ldc->header );
     }
     COPY_EVENT_ID( currEventId, currGdc->header.eventId );
-    loadTimestamp( &currGdc->header );
-    
+
     for( ldc = currGdc->head; ldc != NULL; ldc = ldc->next ) {
       struct equipmentEventDescriptorStruct *eq;
       int n;
@@ -1147,10 +1233,11 @@ void createEvent( void ) {
       for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
        if ( !bufferData ) {
          loadBuffer( eq->payload );
-         if ( !currGdc->loaded ) decodeCDH( ldc, eq->payload );
+         decodeCDH( ldc, eq->payload, eq->id );
        }
        loadCdh( (struct commonDataHeaderStruct*)eq->payload->data,
-                &currEventId );
+                &currEventId,
+                eq->id);
       }
 
       if ( !currGdc->loaded ) {
@@ -1163,33 +1250,35 @@ void createEvent( void ) {
        currGdc->loaded = TRUE;
       }
     }
+    cdhRef = NULL;
   } else if ( workingAs == ldc ) {
     struct equipmentEventDescriptorStruct *eq;
 
     COPY_EVENT_ID( currEventId, currLdc->header.eventId );
-    loadTimestamp( &currLdc->header );
 
     for ( eq = currLdc->head; eq != NULL; eq = eq->next ) {
       if ( !bufferData ) {
        loadBuffer( eq->payload );
-       if ( !currLdc->loaded ) decodeCDH( currLdc, eq->payload );
+       decodeCDH( currLdc, eq->payload, eq->id );
       }
       loadCdh( (struct commonDataHeaderStruct*)eq->payload->data,
-              &currEventId );
+              &currEventId,
+              eq->id);
       currLdc->loaded = TRUE;
     }
+    cdhRef = NULL;
   }
   ADD_EVENT_ID( currEventId, oneEventDelta );
 
   /* Step 2: output the event */
   if ( workingAs == gdc ) {
     struct ldcEventDescriptorStruct *ldc;
-    
+
     outputEvent( &currGdc->header, sizeof( currGdc->header ) );
 
     for( ldc = currGdc->head; ldc != NULL; ldc = ldc->next ) {
       struct equipmentEventDescriptorStruct *eq;
-      
+
       outputEvent( &ldc->header, sizeof( ldc->header ) );
 
       for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
@@ -1202,9 +1291,9 @@ void createEvent( void ) {
       currGdc = (struct gdcEventDescriptorStruct *)eventsHead;
   } else if ( workingAs == ldc ) {
     struct equipmentEventDescriptorStruct *eq;
-    
+
     outputEvent( &currLdc->header, sizeof( currLdc->header ) );
-      
+
     for ( eq = currLdc->head; eq != NULL; eq = eq->next ) {
       outputEvent( &eq->header, sizeof( eq->header ) );
       outputEvent( eq->payload->data, eq->payload->size );
@@ -1277,7 +1366,7 @@ void parseArgs( int argc, char **argv ) {
       outFileName = arg;
     } else if ( strcmp( "-#", argv[ arg ] ) == 0 ) {
       int n;
-      
+
       if ( ++arg == argc ) exit( usage() );
       if ( sscanf( argv[ arg ], "%d", &n ) != 1 ) exit( usage() );
       if ( n < 0 ) exit( usage() );
@@ -1294,6 +1383,12 @@ void parseArgs( int argc, char **argv ) {
       handleCDH = TRUE;
     } else if ( strcmp( "-D", argv[ arg ] ) == 0 ) {
       bufferData = FALSE;
+    } else if ( strcmp( "-run", argv[ arg ] ) == 0 ) {
+      int runnumber;
+      if ( ++arg == argc ) exit( usage() );
+      if ( sscanf( argv[ arg ], "%d", &runnumber ) != 1 ) exit( usage() );
+      if ( runnumber < 0 ) exit( usage() );
+      currRunNb = runnumber;
     } else {
       fprintf( stderr, "%s: Unknown switch \"%s\"\n", myName, argv[argc] );
       exit( usage() );
@@ -1352,14 +1447,15 @@ void initEquipment( struct equipmentHeaderStruct * const eq ) {
 } /* End of initEquipment */
 
 void decodeCDH(       struct ldcEventDescriptorStruct * const ldc,
-               const struct payloadDescriptorStruct  * const payloadDesc ) {
-  if ( handleCDH ) {
-    static struct commonDataHeaderStruct *cdhRef = NULL;
+               const struct payloadDescriptorStruct  * const payloadDesc,
+                     equipmentIdType id ) {
+  if ( handleCDH && 
+       id != 4352 ) {
     struct commonDataHeaderStruct *cdh;
     static int softwareTriggerIndicator = FALSE;
     int attr;
     int trig;
-    
+
     if ( payloadDesc->size < CDH_SIZE ) {
       fprintf( stderr,
               "%s: payload too small got:%d CDH:%d\n",
@@ -1532,12 +1628,14 @@ void initEvents() {
          gdc != NULL;
          gdc = gdc->next ) {
       struct ldcEventDescriptorStruct *ldc;
-      
+
       initEvent( &gdc->header );
       gdc->header.eventSize = gdc->header.eventHeadSize;
       gdc->header.eventType = PHYSICS_EVENT;
       SET_SYSTEM_ATTRIBUTE( gdc->header.eventTypeAttribute, ATTR_SUPER_EVENT );
       gdc->header.eventGdcId = currGdcId;
+      COPY_DETECTOR_PATTERN(&gdc->detPattern, gdc->header.eventDetectorPattern);
+      gdc->header.eventTimestamp = gdc->timestamp;
       for ( ldc = gdc->head; ldc != NULL; ldc = ldc->next ) {
        struct equipmentEventDescriptorStruct *eq;
 
@@ -1545,6 +1643,8 @@ void initEvents() {
        ldc->header.eventSize = ldc->header.eventHeadSize;
        ldc->header.eventType = PHYSICS_EVENT;
        ldc->header.eventGdcId = currGdcId;
+       COPY_DETECTOR_PATTERN(&ldc->detPattern, ldc->header.eventDetectorPattern);
+       ldc->header.eventTimestamp = ldc->timestamp;
        ldc->header.eventLdcId = ldc->id;
        for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
          initEquipment( &eq->header );
@@ -1554,7 +1654,7 @@ void initEvents() {
                                  ATTR_ORBIT_BC );
          eq->header.equipmentSize = eq->payload->size + sizeof( eq->header );
          ldc->header.eventSize += eq->header.equipmentSize;
-         decodeCDH( ldc, eq->payload );
+         decodeCDH( ldc, eq->payload, eq->id );
          OR_ALL_ATTRIBUTES( eq->header.equipmentTypeAttribute,
                             ldc->header.eventTypeAttribute );
          OR_ALL_ATTRIBUTES( eq->header.equipmentTypeAttribute,
@@ -1562,6 +1662,7 @@ void initEvents() {
        }
        gdc->header.eventSize += ldc->header.eventSize;
       }
+      cdhRef = NULL;
     }
 
     DBG_VERBOSE {
@@ -1594,7 +1695,7 @@ void initEvents() {
     }
   } else if ( workingAs == ldc ) {
     struct ldcEventDescriptorStruct *ldc;
-      
+
     for ( ldc = (struct ldcEventDescriptorStruct *)eventsHead;
          ldc != NULL;
          ldc = ldc->next ) {
@@ -1613,10 +1714,11 @@ void initEvents() {
                                ATTR_ORBIT_BC );
        eq->header.equipmentSize = eq->payload->size + sizeof( eq->header );
        ldc->header.eventSize += eq->header.equipmentSize;
-       decodeCDH( ldc, eq->payload );
+       decodeCDH( ldc, eq->payload, eq->id );
        OR_ALL_ATTRIBUTES( eq->header.equipmentTypeAttribute,
                           ldc->header.eventTypeAttribute );
       }
+      cdhRef = NULL;
     }
     DBG_VERBOSE {
       printf( "Headers:\n" );
@@ -1651,7 +1753,7 @@ void initVars() {
   currLdc = NULL;
   currEvent = NULL;
   payloadsHead = payloadsTail = NULL;
-  currRunNb = 1;
+  currRunNb = -1;
   numOfLdcs = 0;
   numOfEvents = 1;
   createSorEor = TRUE;