--- /dev/null
+/*
+ dateStream.c
+ ============
+
+ Utility to simulate a DATE raw data stream using a given set of raw
+ data files and a configuration file.
+
+ Revision history:
+
+ V01.00 4/05/2004 RD Created
+ V01.01 25/10/2005 RD Support added for timestamp
+ V01.02 4/04/2006 RD Support for CDH
+ V01.03 24/05/2006 RD Added "Direct disk access" option
+*/
+#define VID "1.03"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <assert.h>
+#include <ctype.h>
+#include <time.h>
+
+#include "event.h"
+
+#define DESCRIPTION "DATE raw data stream simulator"
+#ifdef AIX
+static
+#endif
+char fileHandlerIdent[]= "@(#)""" __FILE__ """: """ DESCRIPTION \
+ """ """ VID """ """ \
+ """ compiled """ __DATE__ """ """ __TIME__;
+
+#define DBG_BASE if ( debug > 0 )
+#define DBG_DETAILED if ( debug > 1 )
+#define DBG_VERBOSE if ( debug > 2 )
+
+#ifndef TRUE
+# define TRUE (0 == 0)
+#endif
+#ifndef FALSE
+# define FALSE (0 == 1)
+#endif
+
+const char *myName;
+int debug;
+FILE *outF;
+enum { unknown, ldc, gdc } workingAs;
+enum { collider, fixedTarget } workingMode;
+struct ldcDescriptorStruct {
+ eventLdcIdType id;
+ struct ldcDescriptorStruct *next;
+} *ldcsHead, *ldcsTail;
+void *eventsHead, *eventsTail;
+struct gdcEventDescriptorStruct {
+ struct ldcEventDescriptorStruct *head;
+ struct ldcEventDescriptorStruct *tail;
+ struct gdcEventDescriptorStruct *next;
+ struct eventHeaderStruct header;
+ int loaded;
+} *currGdc;
+struct ldcEventDescriptorStruct {
+ struct equipmentEventDescriptorStruct *head;
+ struct equipmentEventDescriptorStruct *tail;
+ struct ldcEventDescriptorStruct *next;
+ eventLdcIdType id;
+ struct eventHeaderStruct header;
+ int loaded;
+} *currLdc;
+struct equipmentEventDescriptorStruct {
+ struct equipmentEventDescriptorStruct *next;
+ equipmentIdType id;
+ struct payloadDescriptorStruct *payload;
+ struct equipmentHeaderStruct header;
+} *currEvent;
+struct payloadDescriptorStruct {
+ struct payloadDescriptorStruct *next;
+ char *fileName;
+ int fileSize;
+ int size;
+ void *data;
+} *payloadsHead, *payloadsTail;
+int lineNo;
+eventGdcIdType currGdcId;
+eventLdcIdType currLdcId;
+equipmentIdType currEquipmentId;
+int currRunNb;
+int numOfLdcs;
+int numOfEvents;
+int createSorEor;
+int handleCDH;
+eventIdType oneEventDelta;
+eventIdType currEventId;
+int gotAliceTrigger;
+int bufferData;
+
+void dumpPayload( const struct payloadDescriptorStruct *p ) {
+ char *c;
+ int i;
+ int printable;
+
+ if ( p->data != NULL ) {
+ for ( i = 0, c = (char *)p->data, printable = TRUE;
+ printable && i != p->size;
+ c++, i++ )
+ printable = isascii( *c );
+ if ( printable ) {
+ printf( " \"" );
+ for ( i = 0, c = (char *)p->data; i != p->size; c++, i++ ) {
+ if ( *c == '\n' )
+ printf( "\"\n \"" );
+ else
+ putchar( *c );
+ }
+ if ( *c != '\n' ) printf( "\"\n" );
+ } else {
+ long32 *v;
+ for ( i = 0, v = (long32 *)p->data;
+ i+4 <= p->size;
+ v++, i += 4 ) {
+ if ( i % (4*8) == 0 ) {
+ if ( i != 0 ) printf( "\n" );
+ printf( " " );
+ }
+ printf( "%08x ", *v );
+ }
+ if ( i < p->size ) {
+ int j = 0;
+
+ printf( "\n " );
+ while ( i != p->size ) {
+ printf( "%02x ", *((char *)p->data + p->size - j - 1) & 0xff );
+ j++;
+ i++;
+ }
+ }
+ }
+ printf( "\n" );
+ }
+} /* End of dumpPayload */
+
+void dumpEvents() {
+ assert( workingAs == ldc || workingAs == gdc );
+ if ( eventsHead != NULL ) {
+ printf( "Events:\n" );
+ if ( workingAs == gdc ) {
+ struct gdcEventDescriptorStruct *gdc;
+
+ for ( gdc = (struct gdcEventDescriptorStruct *)eventsHead;
+ gdc != NULL;
+ gdc = gdc->next ) {
+ struct ldcEventDescriptorStruct *ldc;
+
+ printf( " GDC (%p)\n", gdc );
+ for ( ldc = gdc->head; ldc != NULL; ldc = ldc->next ) {
+ struct equipmentEventDescriptorStruct *eq;
+
+ printf( " LDC (%p): %d\n", ldc, ldc->id );
+ for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
+ printf( " EQUIPMENT (%p): %d PAYLOAD (%p):",
+ eq,
+ eq->id,
+ eq->payload );
+ fflush( stdout );
+ printf( "\"%s\" (%d bytes)\n",
+ eq->payload->fileName,
+ eq->payload->size );
+ dumpPayload( eq->payload );
+ }
+ }
+ }
+ }
+ if ( workingAs == ldc ) {
+ struct ldcEventDescriptorStruct *ldc;
+
+ for ( ldc = (struct ldcEventDescriptorStruct *)eventsHead;
+ ldc != NULL;
+ ldc = ldc->next ) {
+ struct equipmentEventDescriptorStruct *eq;
+
+ printf( " LDC\n" );
+ for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
+ printf( " EQUIPMENT (%p): %d PAYLOAD (%p):",
+ eq,
+ eq->id,
+ eq->payload );
+ fflush( stdout );
+ printf( "\"%s\" (%d bytes)\n",
+ eq->payload->fileName,
+ eq->payload->size );
+ dumpPayload( eq->payload );
+ }
+ }
+ }
+ } else {
+ printf( "Events: EMPTY\n" );
+ }
+} /* End of 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;
+ }
+ if ( ferror( stdin ) ) {
+ fprintf( stderr,
+ "%s: failed to read configuration input errno:%d ",
+ myName, errno );
+ perror( "" );
+ exit( 1 );
+ }
+ if ( feof( stdin ) ) read--;
+ if ( read == maxSize && line[read] != '\n' ) {
+ fprintf( stderr,
+ "%s: Input line # %d too long (%d chars max)\n",
+ myName, lineNo, maxSize-1 );
+ exit( 1 );
+ }
+ line[ read ] = 0;
+ DBG_VERBOSE {
+ if ( !( read == 0 && feof( stdin ) ) ) {
+ printf( "%d) [%3d] \"%s\"", lineNo, read, line );
+ }
+ }
+ for ( c = 0; c != read; c++ ) {
+ if ( line[c] == '#' ) {
+ line[c] = 0;
+ break;
+ }
+ }
+ DBG_VERBOSE {
+ if ( read != c ) {
+ printf( " => \"%s\"", line );
+ }
+ if ( feof( stdin ) ) printf( "<<< EOF >>>" );
+ if ( ferror( stdin ) ) printf( "<<< FERROR >>>" );
+ printf( "\n" );
+ }
+} /* End of getLine */
+
+void handleLdc( eventLdcIdType ldcId ) {
+ struct ldcDescriptorStruct *ldc;
+
+ if ( ldcsHead != NULL ) {
+ for ( ldc = ldcsHead; ldc != NULL; ldc = ldc->next ) {
+ if ( ldc->id == ldcId ) {
+ return;
+ }
+ }
+ }
+ if ( (ldc = (struct ldcDescriptorStruct *)malloc( sizeof( *ldc ) )) == NULL ) {
+ fprintf( stderr,
+ "%s: Failed to malloc for %d bytes (struct ldcDescriptorStruct)\n",
+ myName, (int)sizeof( *ldc ) );
+ exit( 1 );
+ }
+ ldc->id = ldcId;
+ ldc->next = NULL;
+ if ( ldcsHead == NULL ) {
+ ldcsHead = ldcsTail = ldc;
+ } else {
+ ldcsTail->next = ldc;
+ ldcsTail = ldc;
+ }
+ numOfLdcs++;
+} /* End of handleLdc */
+
+void createNewEvent() {
+ assert( workingAs == ldc || workingAs == gdc );
+ if ( workingAs == ldc ) {
+ struct ldcEventDescriptorStruct *p;
+
+ if ( (p = (struct ldcEventDescriptorStruct *)malloc( sizeof( *p ) ))
+ == NULL ) {
+ fprintf( stderr,
+ "%s: failed to malloc for %d bytes (createNewEvent: struct ldcEventDescriptorStruct)",
+ myName, (int)sizeof( *p ) );
+ perror( "" );
+ exit( 1 );
+ }
+ p->loaded = FALSE;
+ p->head = p->tail = NULL;
+ p->next = NULL;
+ currLdc = p;
+ if ( eventsHead == NULL ) {
+ eventsHead = eventsTail = p;
+ } else {
+ struct ldcEventDescriptorStruct *q =
+ (struct ldcEventDescriptorStruct *)eventsTail;
+
+ q->next = p;
+ eventsTail = p;
+ }
+ p->id = currLdcId;
+ } else if ( workingAs == gdc ) {
+ struct gdcEventDescriptorStruct *p;
+
+ if ( (p = (struct gdcEventDescriptorStruct *)malloc( sizeof( *p ) ))
+ == NULL ) {
+ fprintf( stderr,
+ "%s: failed to malloc for %d bytes (createNewEvent: struct gdcEventDescriptorStruct)",
+ myName, (int)sizeof( *p ) );
+ perror( "" );
+ exit( 1 );
+ }
+ p->loaded = FALSE;
+ p->next = NULL;
+ p->head = p->tail = NULL;
+ currGdc = p;
+ if ( eventsHead == NULL ) {
+ eventsHead = eventsTail = p;
+ } else {
+ struct gdcEventDescriptorStruct *q =
+ (struct gdcEventDescriptorStruct *)eventsTail;
+
+ q->next = p;
+ eventsTail = p;
+ }
+ }
+} /* End of createNewEvent */
+
+void createNewLdcEvent() {
+ struct gdcEventDescriptorStruct *gdcDesc;
+ struct ldcEventDescriptorStruct *p;
+
+ if ( (p = (struct ldcEventDescriptorStruct *)malloc( sizeof( *p ) ))
+ == NULL ) {
+ fprintf( stderr,
+ "%s: failed to malloc for %d bytes (createNewLdcEvent: struct ldcEventDescriptorStruct)",
+ myName, (int)sizeof( *p ) );
+ perror( "" );
+ exit( 1 );
+ }
+ p->id = currLdcId;
+ p->head = p->tail = NULL;
+ p->next = NULL;
+ gdcDesc = (struct gdcEventDescriptorStruct *)eventsTail;
+ if ( gdcDesc->head == NULL ) {
+ gdcDesc->head = gdcDesc->tail = p;
+ } else {
+ gdcDesc->tail->next = p;
+ gdcDesc->tail = p;
+ }
+ currLdc = p;
+} /* End of 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,
+ payload->fileName,
+ errno );
+ perror( "System-dependent error " );
+ exit( 1 );
+ }
+ if ( (payload->data = malloc( payload->size )) == NULL ) {
+ fprintf( stderr,
+ "%s: line:%d Failed to malloc for payload file \"%s\" size:%d errno:%d ",
+ myName,
+ lineNo,
+ payload->fileName,
+ payload->size,
+ errno );
+ perror( "System-dependent status " );
+ exit( 1 );
+ }
+ if ( (bytesRead = fread( payload->data, payload->fileSize, 1, f )) != 1 ) {
+ 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,
+ payload->fileName,
+ payload->size,
+ bytesRead,
+ feof(f) ? "TRUE" : "false",
+ ferror(f) ? "TRUE" : "false",
+ errno );
+ perror( "System-dependent status " );
+ exit( 1 );
+ }
+ fclose(f);
+ if ( payload->size != payload->fileSize ) {
+ memset( (char *)payload->data + payload->fileSize,
+ 0,
+ payload->size - payload->fileSize );
+ }
+} /* End of loadBuffer */
+
+void unloadBuffer( struct payloadDescriptorStruct * const payload ) {
+ if ( payload->data != NULL ) {
+ free( payload->data );
+ payload->data = NULL;
+ }
+} /* End of unloadBuffer */
+
+void unloadAllBuffers() {
+ struct payloadDescriptorStruct *payload;
+
+ for ( payload = payloadsHead; payload != NULL; payload = payload->next ) {
+ unloadBuffer( payload );
+ }
+} /* End of unloadAllBuffers */
+
+void loadPayload( const char *fileName ) {
+ struct payloadDescriptorStruct *payload;
+
+ for ( payload = payloadsHead; payload != NULL; payload = payload->next ) {
+ if ( strcmp( fileName, payload->fileName ) == 0 )
+ break;
+ }
+ if ( payload == NULL ) {
+ FILE *f;
+
+ if ( (payload = (struct payloadDescriptorStruct *)malloc( sizeof( *payload ) ))
+ == NULL ) {
+ fprintf( stderr,
+ "%s: failed to malloc for %d bytes (loadPayload/payloadDescriptorStruct)\n",
+ myName,
+ (int)sizeof( *payload ) );
+ exit( 1 );
+ }
+ if ( (payload->fileName = strdup( fileName )) == NULL ) {
+ fprintf( stderr,
+ "%s: failed to duplicate string \"%s\" (loadPaload/fileName)\n",
+ myName,
+ fileName );
+ exit( 1 );
+ }
+ if ( (f = fopen( fileName, "r" )) == NULL ) {
+ fprintf( stderr,
+ "%s: line:%d payload file \"%s\" not found or not readable, errno:%d. ",
+ myName,
+ lineNo,
+ fileName,
+ errno );
+ perror( "System-dependent error " );
+ exit( 1 );
+ }
+ if ( fseek( f, 0L, SEEK_END ) != 0 ) {
+ fprintf( stderr,
+ "%s: line:%d Failed to seek payload file \"%s\" errno:%d ",
+ myName,
+ lineNo,
+ fileName,
+ errno );
+ perror( "System-dependent error " );
+ exit( 1 );
+ }
+ if ( (payload->size = ftell( f )) <= 0 ) {
+ fprintf( stderr,
+ "%s: line:%d Failed to get file \"%s\" size size:%d errno:%d ",
+ myName,
+ lineNo,
+ fileName,
+ payload->size,
+ errno );
+ perror( "System-dependent status " );
+ exit( 1 );
+ }
+ payload->fileSize = payload->size;
+ while ( (payload->size & 3) != 0 ) payload->size++;
+ fclose( f );
+
+ if ( bufferData ) {
+ loadBuffer( payload );
+ } else {
+ payload->data = NULL;
+ }
+
+ payload->next = NULL;
+ if ( payloadsHead == NULL ) {
+ payloadsHead = payloadsTail = payload;
+ } else {
+ payloadsTail->next = payload;
+ payloadsTail = payload;
+ }
+ DBG_VERBOSE {
+ int b, n;
+
+ printf( "%d) Payload \"%s\" loaded at %p\n",
+ lineNo,
+ fileName,
+ payload );
+ if ( bufferData ) {
+ if ( handleCDH ) {
+ 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 ) );
+ } else {
+ printf( "version:%d=0x%x ", cdh->cdhVersion, cdh->cdhVersion );
+ if ( cdh->cdhVersion != CDH_VERSION ) {
+ printf( "EXPECTED:%d=%x (decoding may be inaccurate) ",
+ CDH_VERSION, CDH_VERSION );
+ }
+ }
+ printf( "L1TriggerMessage:0x%x", cdh->cdhL1TriggerMessage );
+ if ( cdh->cdhL1TriggerMessage != 0 ) {
+ for ( b = 0, n = 0; b != 10; b++ ) {
+ if ( (cdh->cdhL1TriggerMessage & (1<<b)) != 0 ) {
+ if ( n++ != 0 )printf( "+" );
+ switch (b) {
+ case 0: printf( "L1SwC" ); break;
+ case 1: printf( "ESR" ); break;
+ case 2: printf( "RoC1" ); break;
+ case 3: printf( "RoC2" ); break;
+ case 4: printf( "RoC3" ); break;
+ case 5: printf( "RoC4" ); break;
+ case 6: printf( "ClT" ); break;
+ default: printf( "spare %d", b+14 );
+ }
+ }
+ }
+ printf( ">" );
+ }
+ printf( " " );
+ if ( cdh->cdhMBZ0 != 0 )
+ printf( "MBZ0:0x%x ",
+ cdh->cdhMBZ0 );
+ printf( "\n" );
+
+ printf( " " );
+ printf( "EventId2(orbit):%d=0x%x ",
+ cdh->cdhEventId2, cdh->cdhEventId2 );
+ printf( "EventId1(bunchCrossing):%d=0x%x ",
+ cdh->cdhEventId1, cdh->cdhEventId1 );
+ printf( "\n" );
+
+ printf( " " );
+ if ( cdh->cdhMBZ1 != 0 )
+ printf( "MBZ1:0x%x ",
+ cdh->cdhMBZ1 );
+ printf( "BlockAttributes:0x%x",
+ cdh->cdhBlockAttributes );
+ if ( cdh->cdhBlockAttributes != 0 ) {
+ printf( "=<" );
+ for ( b = 0, n = 0; b != 8; b++ ) {
+ if ( (cdh->cdhBlockAttributes & (1<<b)) != 0 ) {
+ if ( n++ != 0 )
+ printf( "+" );
+ printf( "%d", b );
+ }
+ }
+ printf( ">" );
+ }
+ printf( " " );
+ printf( "ParticipatingSubDetectors:0x%x ",
+ cdh->cdhParticipatingSubDetectors );
+ printf( "\n" );
+ printf( " " );
+
+ if ( cdh->cdhMBZ2 != 0 )
+ printf( "MBZ2:0x%x ",
+ cdh->cdhMBZ2 );
+ printf( "Status/Error:0x%x", cdh->cdhStatusErrorBits );
+ if ( cdh->cdhStatusErrorBits != 0 ) {
+ printf( "=<" );
+ for ( b = 0,n = 0; b != 16; b++ ) {
+ if ( (cdh->cdhStatusErrorBits & (1<<b)) != 0 ) {
+ if ( n++ != 0 ) printf( "+" );
+ switch (b) {
+ case 0: printf( "TriggerOverLapError" ); break;
+ case 1: printf( "TriggerMissingError" ); break;
+ case 2: printf( "DataParityError" ); break;
+ case 3: printf( "ControlParityError" ); break;
+ case 4: printf( "TriggerInformationUnavailable" ); break;
+ case 5: printf( "FEEError" ); break;
+ case 6: printf( "HLTDecision" ); break;
+ case 7: printf( "HLTPayload" ); break;
+ case 8: printf( "DDGPayload" ); break;
+ default: printf( "spare %d", b );
+ }
+ }
+ }
+ printf( ">" );
+ }
+ printf( " " );
+ printf( "MiniEventId(bunchCrossing):%d=0x%x ",
+ cdh->cdhMiniEventId, cdh->cdhMiniEventId );
+ printf( "\n" );
+
+ printf( " " );
+ printf( "Trigger classes: 0x(%05x-%08x)",
+ cdh->cdhTriggerClassesHigh,
+ cdh->cdhTriggerClassesLow );
+ if ( cdh->cdhTriggerClassesHigh != 0
+ || cdh->cdhTriggerClassesLow != 0 ) {
+ printf( "=<" );
+ for ( b=0, n=0; b != 32; b++ ) {
+ if ( (cdh->cdhTriggerClassesLow & (1<<b)) != 0 ) {
+ if ( n++ != 0 ) printf( "+" );
+ printf( "%d", b );
+ }
+ }
+ for ( b=0; b != 18; b++ ) {
+ if ( (cdh->cdhTriggerClassesHigh & (1<<b)) != 0 ) {
+ if ( n++ != 0 ) printf( "+" );
+ printf( "%d", b+32 );
+ }
+ }
+ printf( ">" );
+ }
+ printf( "\n" );
+
+ printf( " " );
+ if ( cdh->cdhMBZ3 != 0 ) {
+ printf( "MBZ3:0x%x ",
+ cdh->cdhMBZ3 );
+ }
+ printf( "ROI:0x(%08x-%01x)", cdh->cdhRoiHigh, cdh->cdhRoiLow );
+ if ( cdh->cdhRoiHigh != 0
+ || cdh->cdhRoiLow != 0 ) {
+ printf( "=<" );
+ for ( b=0, n=0; b != 5; b++ ) {
+ if ( (cdh->cdhRoiLow & (1<<b)) != 0 ) {
+ if ( n++ != 0 ) printf( "+" );
+ printf( "%d", b );
+ }
+ }
+ for ( b=0; b != 32; b++ ) {
+ if ( (cdh->cdhRoiHigh & (1<<b)) != 0 ) {
+ if ( n++ != 0 ) printf( "+" );
+ printf( "%d", b+4 );
+ }
+ }
+ printf( ">" );
+ }
+ printf( "\n" );
+ }
+ }
+ }
+ } else {
+ DBG_VERBOSE
+ printf( "%d) Payload \"%s\" already loaded at %p\n",
+ lineNo,
+ fileName,
+ payload );
+ }
+
+ currEvent->payload = payload;
+} /* End of loadPayload */
+
+void parseEquipment( char * const line ) {
+ struct equipmentEventDescriptorStruct *equipment;
+ int payloadFound = FALSE;
+ char *p;
+ char *keyword;
+
+ if ( (equipment =
+ (struct equipmentEventDescriptorStruct *)malloc( sizeof( *equipment ) )) == NULL ) {
+ fprintf( stderr,
+ "%s: filed to malloc for %d bytes (parseEquipment/equipmentEventDescriptorStruct) errno:%d ",
+ myName,
+ (int)sizeof( *equipment ),
+ errno );
+ perror( "" );
+ exit( 1 );
+ }
+ currEvent = equipment;
+
+ p = line;
+ while ( (keyword = strtok_r( p, " \t", &p )) != NULL ) {
+ DBG_VERBOSE printf( "%d) Equipment - Keyword:\"%s\"\n",
+ lineNo,
+ keyword );
+ if ( strcasecmp( "id", keyword ) == 0 ) {
+ char *idNum;
+
+ if ( (idNum = strtok_r( p, " \t", &p )) == NULL ) {
+ fprintf( stderr,
+ "%s: line:%d EQUIPMENT declaration, ID needed",
+ myName,
+ lineNo );
+ exit( 1 );
+ }
+ if ( sscanf( idNum, "%d", &currEquipmentId ) != 1 ) {
+ fprintf( stderr,
+ "%s: line:%d EQUIPMENT declaration, numeric ID needed (%s)",
+ myName,
+ lineNo,
+ idNum );
+ exit( 1 );
+ }
+ DBG_VERBOSE printf( "%d) EQUIPMENT - ID:%d\n",
+ lineNo,
+ currEquipmentId );
+ } else if ( strncasecmp( "pay", keyword, 3 ) == 0 ) {
+ char *fileName;
+
+ if ( (fileName = strtok_r( p, " \t", &p )) == NULL ) {
+ fprintf( stderr,
+ "%s line:%d Payload without filename found\n",
+ myName,
+ lineNo );
+ exit( 1 );
+ }
+ DBG_VERBOSE printf( "%d) Equipment - Payload:\"%s\"\n",
+ lineNo,
+ fileName );
+ if ( payloadFound ) {
+ fprintf( stderr,
+ "%s line:%d Payload with multiple filenames found\n",
+ myName,
+ lineNo );
+ exit( 1 );
+ }
+ loadPayload( fileName );
+ payloadFound = TRUE;
+ } else {
+ fprintf( stderr,
+ "%s: line:%d Equipment declaration, unknown keyword \"%s\"\n",
+ myName,
+ lineNo,
+ keyword );
+ exit( 1 );
+ }
+ }
+ if ( !payloadFound ) {
+ fprintf( stderr,
+ "%s: line:%d Equipment without payload found\n",
+ myName,
+ lineNo );
+ exit( 1 );
+ }
+
+ equipment->id = currEquipmentId;
+ equipment->next = NULL;
+ if ( currLdc->head == NULL ) {
+ currLdc->head = currLdc->tail = equipment;
+ } else {
+ currLdc->tail->next = equipment;
+ currLdc->tail = equipment;
+ }
+} /* End of parseEquipment */
+
+void parseGdc( char * const line ) {
+ char *p;
+ char *keyword;
+
+ p = line;
+ while ( (keyword = strtok_r( p, " \t", &p )) != NULL ) {
+ if ( strcasecmp( "id", keyword ) == 0 ) {
+ char *idNum;
+
+ if ( (idNum = strtok_r( p, " \t", &p )) == NULL ) {
+ fprintf( stderr,
+ "%s: line:%d GDC declaration, ID needed",
+ myName,
+ lineNo );
+ exit( 1 );
+ }
+ if ( sscanf( idNum, "%d", &currGdcId ) != 1 ) {
+ fprintf( stderr,
+ "%s: line:%d GDC declaration, numeric ID needed (%s)",
+ myName,
+ lineNo,
+ idNum );
+ exit( 1 );
+ }
+ DBG_VERBOSE printf( "%d) GDC - ID:%d\n",
+ lineNo,
+ currGdcId );
+ } else {
+ fprintf( stderr,
+ "%s: line:%d GDC declaration, unknown keyword \"%s\"\n",
+ myName,
+ lineNo,
+ keyword );
+ exit( 1 );
+ }
+ }
+} /* End of parseGdc */
+
+void parseLdc( char * const line ) {
+ char *p;
+ char *keyword;
+
+ p = line;
+ while ( (keyword = strtok_r( p, " \t", &p )) != NULL ) {
+ if ( strcasecmp( "id", keyword ) == 0 ) {
+ char *idNum;
+
+ if ( (idNum = strtok_r( p, " \t", &p )) == NULL ) {
+ fprintf( stderr,
+ "%s: line:%d LDC declaration, ID needed",
+ myName,
+ lineNo );
+ exit( 1 );
+ }
+ if ( sscanf( idNum, "%d", &currLdcId ) != 1 ) {
+ fprintf( stderr,
+ "%s: line:%d LDC declaration, numeric ID needed (%s)",
+ myName,
+ lineNo,
+ idNum );
+ exit( 1 );
+ }
+ DBG_VERBOSE printf( "%d) LDC - ID:%d\n",
+ lineNo,
+ currLdcId );
+ } else {
+ fprintf( stderr,
+ "%s: line:%d LDC declaration, unknown keyword \"%s\"\n",
+ myName,
+ lineNo,
+ keyword );
+ exit( 1 );
+ }
+ }
+} /* End of parseLdc */
+
+void parseRules() {
+ char line[ 1025 ];
+
+ currLdcId = HOST_ID_MIN;
+ currGdcId = HOST_ID_MIN;
+
+ for ( lineNo = 1; !feof( stdin ); lineNo++ ) {
+ 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 );
+ 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 );
+ exit( 1 );
+ }
+ workingAs = gdc;
+ parseGdc( p );
+ createNewEvent();
+ currLdcId = HOST_ID_MIN;
+ currLdc = NULL;
+ currEquipmentId = 0;
+ } else if ( strcasecmp( "ldc", keyword ) == 0 ) {
+ if ( workingAs != gdc && workingAs != ldc && workingAs != unknown ) {
+ fprintf( stderr,
+ "%s: line:%d LDC found when working in non-LDC/GDC mode\n",
+ myName, lineNo );
+ exit( 1 );
+ }
+ if ( workingAs == unknown ) workingAs = ldc;
+ parseLdc( p );
+ if ( workingAs == ldc ) {
+ createNewEvent();
+ currEquipmentId = 0;
+ } else {
+ createNewLdcEvent();
+ handleLdc( currLdcId );
+ currLdcId++;
+ }
+ currEvent = NULL;
+ } else if ( strncasecmp( "equ", keyword, 3 ) == 0 ) {
+ if ( workingAs == unknown
+ || (workingAs == ldc && currLdc == NULL )
+ || (workingAs == gdc && currGdc == NULL ) ) {
+ fprintf( stderr,
+ "%s: line:%d Unexpected EQUIPMENT declaration (LDC or GDC needed first)\n",
+ myName,
+ lineNo );
+ exit( 1 );
+ }
+ parseEquipment( p );
+ currEquipmentId++;
+ } else {
+ fprintf( stderr,
+ "%s: line:%d Parse error in \"%s\" unknown keyword\n",
+ myName,
+ lineNo,
+ keyword );
+ exit( 1 );
+ }
+ }
+ }
+ } while ( !feof( stdin ) );
+ lineNo -= 2;
+
+ DBG_VERBOSE {
+ printf( "End of parse: %d line%s found\n",
+ lineNo,
+ lineNo != 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 );
+ }
+ printf( "\n" );
+ }
+ dumpEvents();
+ }
+
+ if ( workingAs == ldc ) {
+ assert( ldcsHead == ldcsTail );
+ assert( ldcsTail == NULL );
+ }
+
+ 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 );
+ assert ( ldc == ldcsTail );
+ }
+
+ if ( workingAs == unknown ) {
+ DBG_VERBOSE printf( "Empty configuration: nothing to do!\n" );
+ exit( 0 );
+ }
+
+ assert( (eventsHead == NULL && eventsTail == NULL)
+ || (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 ) );
+
+ ev->eventMagic = EVENT_MAGIC_NUMBER;
+ ev->eventHeadSize = EVENT_HEAD_BASE_SIZE;
+ ev->eventVersion = EVENT_CURRENT_VERSION;
+ ev->eventRunNb = currRunNb;
+ ZERO_EVENT_ID( ev->eventId );
+ ZERO_TRIGGER_PATTERN( ev->eventTriggerPattern );
+ ZERO_DETECTOR_PATTERN( ev->eventDetectorPattern );
+ RESET_ATTRIBUTES( ev->eventTypeAttribute );
+ if ( workingMode == collider )
+ SET_SYSTEM_ATTRIBUTE( ev->eventTypeAttribute, ATTR_ORBIT_BC );
+ ev->eventLdcId = VOID_ID;
+ ev->eventGdcId = VOID_ID;
+ loadTimestamp( ev );
+} /* End of initEvent */
+
+void outputEvent( const void * const ev,
+ const int size ) {
+ int done;
+
+ DBG_VERBOSE {
+ 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 );
+ }
+} /* End of outputEvent */
+
+void createSorAndEor( const int sor ) {
+ unsigned char event[ 1000 ];
+ struct eventHeaderStruct *ev;
+ struct eventHeaderStruct sev;
+
+ assert( workingAs == ldc || workingAs == gdc );
+
+ if ( !createSorEor ) return;
+ ev = (struct eventHeaderStruct *)event;
+ initEvent( ev );
+ ev->eventSize = sizeof( event );
+ ev->eventType = sor ? START_OF_RUN : END_OF_RUN;
+ if ( workingMode == fixedTarget )
+ LOAD_RAW_EVENT_ID( ev->eventId, 0, 0, 0 );
+ else
+ LOAD_EVENT_ID( ev->eventId, 0, 0, 0 );
+ SET_SYSTEM_ATTRIBUTE( ev->eventTypeAttribute, ATTR_P_START );
+
+ if ( workingAs == ldc ) {
+ currLdc = (struct ldcEventDescriptorStruct *)eventsHead;
+ }
+ if ( workingAs == gdc ) {
+ initEvent( &sev );
+ sev.eventGdcId = currGdcId;
+ ev->eventGdcId = currGdcId;
+ currGdc = (struct gdcEventDescriptorStruct *)eventsHead;
+ 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;
+ for ( ldc = ldcsHead; ldc != NULL; ldc = ldc->next ) {
+ ev->eventLdcId = ldc->id;
+ 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 );
+ COPY_SYSTEM_ATTRIBUTES( ev->eventTypeAttribute, sev.eventTypeAttribute );
+ CLEAR_SYSTEM_ATTRIBUTE( sev.eventTypeAttribute, ATTR_SUPER_EVENT );
+ outputEvent( &sev, sizeof( sev ) );
+ outputEvent( ev, ev->eventSize - sizeof( sev ) );
+
+ sev.eventSize = sizeof( sev ) + ev->eventSize;
+ sev.eventType = sor ? START_OF_RUN_FILES : END_OF_RUN_FILES;
+ 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 );
+ }
+ }
+
+ ADD_EVENT_ID( ev->eventId, oneEventDelta );
+ ev->eventSize = sizeof( *ev );
+ 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 ) {
+ ev->eventLdcId = ldc->id;
+ outputEvent( ev, ev->eventSize );
+ }
+ }
+} /* End of createSorEor */
+
+void createSor() {
+ createSorAndEor( TRUE );
+} /* End of createSor */
+
+void createEor() {
+ createSorAndEor( FALSE );
+} /* End of createEor */
+
+void loadCdh( struct commonDataHeaderStruct * const cdh,
+ eventIdType * const eventId ) {
+ if ( !handleCDH ) return;
+
+ if ( gotAliceTrigger ) {
+ cdh->cdhEventId1 = EVENT_ID_GET_BUNCH_CROSSING( *eventId );
+ cdh->cdhEventId2 = EVENT_ID_GET_ORBIT( *eventId );
+ } else {
+ cdh->cdhEventId1 = 0;
+ cdh->cdhEventId2 = EVENT_ID_GET_NB_IN_RUN( *eventId );
+ }
+ cdh->cdhMiniEventId = cdh->cdhEventId1;
+}
+void decodeCDH( struct ldcEventDescriptorStruct * const ldc,
+ const struct payloadDescriptorStruct * const payloadDesc );
+
+void createEvent( void ) {
+ assert( workingAs == ldc || workingAs == gdc );
+
+ /* 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;
+
+ for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
+ if ( !bufferData ) {
+ loadBuffer( eq->payload );
+ if ( !currGdc->loaded ) decodeCDH( ldc, eq->payload );
+ }
+ loadCdh( (struct commonDataHeaderStruct*)eq->payload->data,
+ &currEventId );
+ }
+
+ if ( !currGdc->loaded ) {
+ for ( n = 0; n != EVENT_TRIGGER_PATTERN_WORDS; n++ )
+ currGdc->header.eventTriggerPattern[n] |= ldc->header.eventTriggerPattern[n];
+ for ( n = 0; n != EVENT_DETECTOR_PATTERN_WORDS; n++ )
+ currGdc->header.eventDetectorPattern[n] |= ldc->header.eventDetectorPattern[n];
+ for ( n = 0; n != ALL_ATTRIBUTE_WORDS; n++ )
+ currGdc->header.eventTypeAttribute[n] |= ldc->header.eventTypeAttribute[n];
+ currGdc->loaded = TRUE;
+ }
+ }
+ } 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 );
+ }
+ loadCdh( (struct commonDataHeaderStruct*)eq->payload->data,
+ &currEventId );
+ currLdc->loaded = TRUE;
+ }
+ }
+ 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 ) {
+ outputEvent( &eq->header, sizeof( eq->header ) );
+ outputEvent( eq->payload->data, eq->payload->size );
+ if ( !bufferData ) unloadBuffer( eq->payload );
+ }
+ }
+ if ( (currGdc = currGdc->next) == NULL )
+ 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 );
+ if ( !bufferData ) unloadBuffer( eq->payload );
+ }
+ if ( (currLdc = currLdc->next) == NULL )
+ currLdc = (struct ldcEventDescriptorStruct *)eventsHead;
+ }
+} /* End of createEvent */
+
+void createEvents() {
+ int eventNum = 0;
+
+ currGdc = (struct gdcEventDescriptorStruct *)eventsHead;
+ currLdc = (struct ldcEventDescriptorStruct *)eventsHead;
+ currEvent = NULL;
+
+ createSor();
+ for ( eventNum = 0;
+ eventNum != numOfEvents && numOfEvents != 0;
+ eventNum++ ) {
+ createEvent();
+ }
+ createEor();
+} /* End of createEvents */
+
+int usage() {
+ fprintf( stderr,
+ "Usage: %s [-?][-d][-i definitionFile][-o outputFile][-# numOfEvents][-s][-F|-C]\n\
+ -? This text\n\
+ -v Print version ID and exit\n\
+ -d Enable debug (repeat for more verbosity)\n\
+ -i definitionFile File with the description of the events to create (default: stdin)\n\
+ -o outputFile File used to store events (default: stdout)\n\
+ -# numOfEvents Number of events to generate (default: 1 event)\n\
+ -s Do not generate SOR/EOR files (valid only for GDCs)\n\
+ -F/-C Working in Fixed Target (F) or Collider (C) mode\n\
+ -c Handles CDH\n\
+ -D Direct disc access (no buffering)\n",
+ myName );
+ return 1;
+} /* End of usage */
+
+void parseArgs( int argc, char **argv ) {
+ int arg = 1;
+ int inFileName = -1;
+ int outFileName = -1;
+
+ myName = argv[0] ;
+ while ( arg < argc ) {
+ if ( strcmp( "-?", argv[ arg ] ) == 0 ) {
+ usage();
+ exit( 0 );
+ }
+ if ( strcmp( "-i", argv[ arg ] ) == 0 ) {
+ if ( ++arg == argc ) exit( usage() );
+ inFileName = arg;
+ if ( freopen( argv[arg], "r", stdin ) == NULL ){
+ fprintf( stderr,
+ "%s: failed to open input definition \"%s\" errno:%d ",
+ myName, argv[arg], errno );
+ perror( "" );
+ exit( 1 );
+ }
+ } else if ( strcmp( "-v", argv[ arg ] ) == 0 ) {
+ printf( "%s\n", fileHandlerIdent );
+ exit( 0 );
+ } else if ( strcmp( "-o", argv[ arg ] ) == 0 ) {
+ if ( ++arg == argc ) exit( usage() );
+ 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() );
+ numOfEvents = n;
+ } else if ( strcmp( "-s", argv[ arg ] ) == 0 ) {
+ createSorEor = FALSE;
+ } else if ( strcmp( "-F", argv[ arg ] ) == 0 ) {
+ workingMode = fixedTarget;
+ } else if ( strcmp( "-C", argv[ arg ] ) == 0 ) {
+ workingMode = collider;
+ } else if ( strcmp( "-d", argv[ arg ] ) == 0 ) {
+ debug++;
+ } else if ( strcmp( "-c", argv[ arg ] ) == 0 ) {
+ handleCDH = TRUE;
+ } else if ( strcmp( "-D", argv[ arg ] ) == 0 ) {
+ bufferData = FALSE;
+ } else {
+ fprintf( stderr, "%s: Unknown switch \"%s\"\n", myName, argv[argc] );
+ exit( usage() );
+ }
+ arg++;
+ }
+
+ if ( workingMode == fixedTarget )
+ LOAD_RAW_EVENT_ID( oneEventDelta, 1, 0, 1 );
+ else
+ LOAD_EVENT_ID( oneEventDelta, 0, 0, 1 );
+ ZERO_EVENT_ID( currEventId );
+
+ DBG_VERBOSE {
+ printf( "Configuration:\n" );
+ printf( " Debug level: %d\n", debug );
+ printf( " Configuration: %s\n",
+ inFileName == -1 ? "stdin" : argv[ inFileName ] );
+ printf( " Output: %s\n",
+ outFileName == -1 ? "stdout" : argv[ outFileName ] );
+ printf( " Working mode: %s\n",
+ workingMode == fixedTarget ? "fixed target" : "collider" );
+ printf( " Number of events: %d\n", numOfEvents );
+ printf( " %s SOR/EOR files\n",
+ createSorEor ? "Create" : "Do not create" );
+ printf( " CDH handling: %s\n",
+ handleCDH ? "enabled" : "disabled" );
+ printf( " data buffering: %s\n",
+ bufferData ? "enabled" : "DISABLED" );
+ }
+
+ if ( outFileName == -1 ) {
+ DBG_BASE
+ printf( "No more trace information from this point...\n" );
+ debug = 0;
+ outF = stdout;
+ } else {
+ if ( ( outF = fopen( argv[ outFileName ], "w" ) ) == NULL ) {
+ fprintf( stderr,
+ "%s: failed to open output file \"%s\" for writing, errno:%d (%s)\n",
+ myName,
+ argv[ outFileName ],
+ errno,
+ strerror( errno ) );
+ exit( 1 );
+ }
+ DBG_DETAILED
+ printf( "Output file \"%s\" opened OK for writing\n",
+ argv[ outFileName ] );
+ }
+} /* End of parseArgs */
+void initEquipment( struct equipmentHeaderStruct * const eq ) {
+ memset( eq, 0, sizeof( *eq ) );
+ RESET_ATTRIBUTES( eq->equipmentTypeAttribute );
+ eq->equipmentBasicElementSize = 4;
+} /* End of initEquipment */
+
+void decodeCDH( struct ldcEventDescriptorStruct * const ldc,
+ const struct payloadDescriptorStruct * const payloadDesc ) {
+ if ( handleCDH ) {
+ static struct commonDataHeaderStruct *cdhRef = NULL;
+ 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",
+ myName,
+ payloadDesc->size,
+ CDH_SIZE );
+ exit( 1 );
+ }
+ if ( (cdh = (struct commonDataHeaderStruct *)payloadDesc->data) != NULL ) {
+ if ( cdh->cdhVersion != CDH_VERSION ) {
+ fprintf( stderr,
+ "%s: CDH version mismatch expected:%d got:%d\n",
+ myName,
+ CDH_VERSION,
+ cdh->cdhVersion );
+ exit( 1 );
+ }
+ if ( cdhRef == NULL ) {
+ cdhRef = cdh;
+#define CDH_TRIGGER_INFORMATION_UNAVAILABLE_MASK (1<<CDH_TRIGGER_INFORMATION_UNAVAILABLE_BIT)
+ gotAliceTrigger = (cdhRef->cdhStatusErrorBits & CDH_TRIGGER_INFORMATION_UNAVAILABLE_MASK) == 0;
+ if ( gotAliceTrigger && workingMode == fixedTarget ) {
+ fprintf( stderr,
+ "%s: ALICE trigger and fixed target mode are not compatible.\n\
+Either work in Collider mode or set the trigger unavailable status bit in the CDH.\n",
+ myName );
+ exit( 1 );
+ }
+ if ( gotAliceTrigger ) {
+ if ( (cdh->cdhL1TriggerMessage & 0x40) != 0 ) {
+ fprintf( stderr,
+ "%s: CDH is a calibration trigger (unsupported) L1TriggerMessage:0x%x\n",
+ myName, cdh->cdhL1TriggerMessage );
+ exit( 1 );
+ }
+ if ( (cdh->cdhL1TriggerMessage & 0x01) != 0 ) {
+ softwareTriggerIndicator = TRUE;
+ }
+ if ( softwareTriggerIndicator ) {
+ switch ((cdh->cdhL1TriggerMessage >> 2) & 0xF) {
+ case 0xD:
+ case 0xC:
+ case 0xB:
+ case 0xA:
+ case 0x9:
+ break;
+ case 0xF:
+ /* L1SwC bit = on, Clt bit = off, RoC[4..1] = 0xF --> END_OF_DATA */
+ case 0xE:
+ /* L1SwC bit = on, Clt bit = off, RoC[4..1] = 0xE0 --> START_OF_DATA */
+ case 0x8:
+ /* L1SwC bit = on, Clt bit = off, RoC[4] = 1, but not 0xE or 0xF
+ --> SYSTEM_SOFTWARE_TRIGGER_EVENT */
+ default:
+ /* L1SwC bit = on, Clt bit = off, RoC[4] = 0
+ --> DETECTOR_SOFTWARE_TRIGGER_EVENT */
+ fprintf( stderr,
+ "%s: CDH trigger SOD/EOD/SST/DST (unsupported) \
+L1TriggerMessage:0x%x ALICETrigger:%s\n",
+ myName,
+ cdh->cdhL1TriggerMessage,
+ gotAliceTrigger ? "yes" : "no" );
+ exit( 1 );
+ }
+ }
+ }
+ } else {
+ if ( (cdh->cdhStatusErrorBits & CDH_TRIGGER_INFORMATION_UNAVAILABLE_MASK) !=
+ (cdhRef->cdhStatusErrorBits & CDH_TRIGGER_INFORMATION_UNAVAILABLE_MASK) ) {
+ fprintf( stderr,
+ "%s: CDH coherency check failed. \
+Trigger information reference:%savailable current:%savailable\n",
+ myName,
+ (cdhRef->cdhStatusErrorBits & CDH_TRIGGER_INFORMATION_UNAVAILABLE_MASK) == 0 ? "UN" : "",
+ (cdh->cdhStatusErrorBits & CDH_TRIGGER_INFORMATION_UNAVAILABLE_MASK) == 0 ? "UN" : "" );
+ exit( 1 );
+ }
+ if ( gotAliceTrigger ) {
+ if ( cdhRef->cdhL1TriggerMessage != cdh->cdhL1TriggerMessage ) {
+ fprintf( stderr,
+ "%s: CDH coherency check failed. \
+L1 trigger message reference:0x%x current:0x%x\n",
+ myName,
+ cdhRef->cdhL1TriggerMessage,
+ cdh->cdhL1TriggerMessage );
+ exit( 1 );
+ }
+ if ( cdh->cdhParticipatingSubDetectors != cdhRef->cdhParticipatingSubDetectors ) {
+ fprintf( stderr,
+ "%s: CDH coherency check failed. \
+ParticipatingSubDetectors reference:0x%x current:0x%x\n",
+ myName,
+ cdhRef->cdhParticipatingSubDetectors,
+ cdh->cdhParticipatingSubDetectors );
+ exit( 1 );
+ }
+ if ( cdh->cdhTriggerClassesLow != cdhRef->cdhTriggerClassesLow
+ || cdh->cdhTriggerClassesHigh != cdhRef->cdhTriggerClassesHigh ) {
+ fprintf( stderr,
+ "%s: CDH coherency check failed. \
+TriggerClassesHigh/Low reference:0x%x-%x current:0x%x-%x\n",
+ myName,
+ cdhRef->cdhTriggerClassesHigh, cdhRef->cdhTriggerClassesLow,
+ cdh ->cdhTriggerClassesHigh, cdh ->cdhTriggerClassesLow );
+ exit( 1 );
+ }
+ if ( cdh->cdhBlockLength != 0xffffffff ) {
+ if ( (unsigned)payloadDesc->size != cdh->cdhBlockLength ) {
+ fprintf( stderr,
+ "%s: CDH coherency check failed. \
+Payload size:%d (0x%08x) CDH block length:%d (0x%08x)\n",
+ myName,
+ payloadDesc->size, payloadDesc->size,
+ cdh->cdhBlockLength, cdh->cdhBlockLength );
+ exit( 1 );
+ }
+ }
+ if ( cdh->cdhRoiLow != cdhRef->cdhRoiLow
+ || cdh->cdhRoiHigh != cdhRef->cdhRoiHigh ) {
+ fprintf( stderr,
+ "%s: CDH coherency check failed. \
+RoiHigh/Low reference:0x%x-%x current:0x%x-%x\n",
+ myName,
+ cdhRef->cdhRoiHigh, cdhRef->cdhRoiLow,
+ cdh ->cdhRoiHigh, cdh ->cdhRoiLow );
+ exit( 1 );
+ }
+ }
+ if ( cdh->cdhMBZ0 != 0
+ || cdh->cdhMBZ1 != 0
+ || cdh->cdhMBZ2 != 0
+ || cdh->cdhMBZ3 != 0 ) {
+ fprintf( stderr,
+ "%s: CDH check failed. MBZ0:0x%x MBZ1:0x%x MBZ2:0x%x MBZ3:0x%x\n",
+ myName,
+ cdh->cdhMBZ0, cdh->cdhMBZ1, cdh->cdhMBZ2, cdh->cdhMBZ3 );
+ exit( 1 );
+ }
+ }
+ for ( attr = 0; attr != 8; attr++ ) {
+ if ( (cdh->cdhBlockAttributes & (1<<attr)) != 0 ) {
+ SET_USER_ATTRIBUTE( ldc->header.eventTypeAttribute, attr );
+ }
+ }
+ for ( trig = 0; trig != 32; trig++ ) {
+ if ( (cdh->cdhTriggerClassesLow & (1<<trig)) != 0 ) {
+ SET_TRIGGER_IN_PATTERN( ldc->header.eventTriggerPattern,
+ trig );
+ }
+ }
+ for ( trig = 0; trig != 18; trig++ ) {
+ if ( (cdh->cdhTriggerClassesHigh & (1<<trig)) != 0 ) {
+ SET_TRIGGER_IN_PATTERN( ldc->header.eventTriggerPattern,
+ 32+trig );
+ }
+ }
+ if ( gotAliceTrigger )
+ VALIDATE_TRIGGER_PATTERN( ldc->header.eventTriggerPattern );
+ }
+ }
+} /* End of decodeCDH */
+
+void initEvents() {
+ assert( workingAs == ldc || workingAs == gdc );
+
+ if ( workingAs == gdc ) {
+ struct gdcEventDescriptorStruct *gdc;
+
+ for ( gdc = (struct gdcEventDescriptorStruct *)eventsHead;
+ 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;
+ for ( ldc = gdc->head; ldc != NULL; ldc = ldc->next ) {
+ struct equipmentEventDescriptorStruct *eq;
+
+ initEvent( &ldc->header );
+ ldc->header.eventSize = ldc->header.eventHeadSize;
+ ldc->header.eventType = PHYSICS_EVENT;
+ ldc->header.eventGdcId = currGdcId;
+ ldc->header.eventLdcId = ldc->id;
+ for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
+ initEquipment( &eq->header );
+ eq->header.equipmentId = eq->id;
+ if ( workingMode == collider )
+ SET_SYSTEM_ATTRIBUTE( eq->header.equipmentTypeAttribute,
+ ATTR_ORBIT_BC );
+ eq->header.equipmentSize = eq->payload->size + sizeof( eq->header );
+ ldc->header.eventSize += eq->header.equipmentSize;
+ decodeCDH( ldc, eq->payload );
+ OR_ALL_ATTRIBUTES( eq->header.equipmentTypeAttribute,
+ ldc->header.eventTypeAttribute );
+ OR_ALL_ATTRIBUTES( eq->header.equipmentTypeAttribute,
+ gdc->header.eventTypeAttribute );
+ }
+ gdc->header.eventSize += ldc->header.eventSize;
+ }
+ }
+
+ DBG_VERBOSE {
+ printf( "Headers:\n" );
+ for ( gdc = (struct gdcEventDescriptorStruct *)eventsHead;
+ gdc != NULL;
+ gdc = gdc->next ) {
+ struct ldcEventDescriptorStruct *ldc;
+
+ printf( " GDC:%d size:%d vers:%08x\n",
+ currGdcId,
+ gdc->header.eventSize,
+ gdc->header.eventVersion);
+ for ( ldc = gdc->head; ldc != NULL; ldc = ldc->next ) {
+ struct equipmentEventDescriptorStruct *eq;
+
+ printf( " LDC:%d size:%d vers:%08x\n",
+ ldc->id,
+ ldc->header.eventSize,
+ ldc->header.eventVersion );
+ for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
+ printf( " EQ:%d size:%d %spayload:%d\n",
+ eq->id,
+ eq->header.equipmentSize,
+ eq->header.equipmentSize - sizeof( struct equipmentHeaderStruct ) == (unsigned)eq->payload->size ? "" : "-ERROR",
+ eq->payload->size );
+ }
+ }
+ }
+ }
+ } else if ( workingAs == ldc ) {
+ struct ldcEventDescriptorStruct *ldc;
+
+ for ( ldc = (struct ldcEventDescriptorStruct *)eventsHead;
+ ldc != NULL;
+ ldc = ldc->next ) {
+ struct equipmentEventDescriptorStruct *eq;
+
+ initEvent( &ldc->header );
+ ldc->header.eventSize = ldc->header.eventHeadSize;
+ ldc->header.eventType = PHYSICS_EVENT;
+ ldc->header.eventGdcId = VOID_ID;
+ ldc->header.eventLdcId = ldc->id;
+ for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
+ initEquipment( &eq->header );
+ eq->header.equipmentId = eq->id;
+ if ( workingMode == collider )
+ SET_SYSTEM_ATTRIBUTE( eq->header.equipmentTypeAttribute,
+ ATTR_ORBIT_BC );
+ eq->header.equipmentSize = eq->payload->size + sizeof( eq->header );
+ ldc->header.eventSize += eq->header.equipmentSize;
+ decodeCDH( ldc, eq->payload );
+ OR_ALL_ATTRIBUTES( eq->header.equipmentTypeAttribute,
+ ldc->header.eventTypeAttribute );
+ }
+ }
+ DBG_VERBOSE {
+ printf( "Headers:\n" );
+ for ( ldc = (struct ldcEventDescriptorStruct *)eventsHead;
+ ldc != NULL;
+ ldc = ldc->next ) {
+ struct equipmentEventDescriptorStruct *eq;
+
+ printf( " LDC:%d size:%d vers:%08x\n",
+ ldc->id,
+ ldc->header.eventSize,
+ ldc->header.eventVersion );
+ for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
+ printf( " EQ:%d size:%d %spayload:%d\n",
+ eq->id,
+ eq->header.equipmentSize,
+ eq->header.equipmentSize - sizeof( struct equipmentHeaderStruct ) == (unsigned)eq->payload->size ? "" : "-ERROR",
+ eq->payload->size );
+ }
+ }
+ }
+ }
+} /* End of initEvents */
+
+void initVars() {
+ debug = 0;
+ workingAs = unknown;
+ workingMode = fixedTarget;
+ ldcsHead = ldcsTail = NULL;
+ eventsHead = eventsTail = NULL;
+ currGdc = NULL;
+ currLdc = NULL;
+ currEvent = NULL;
+ payloadsHead = payloadsTail = NULL;
+ currRunNb = 1;
+ numOfLdcs = 0;
+ numOfEvents = 1;
+ createSorEor = TRUE;
+ handleCDH = FALSE;
+ gotAliceTrigger = TRUE;
+ bufferData=TRUE;
+} /* End of initVars */
+
+int main( int argc, char **argv ) {
+ initVars();
+ parseArgs( argc, argv );
+ parseRules();
+ initEvents();
+ createEvents();
+ return 0;
+} /* End of main */
--- /dev/null
+/****************************************************************************
+ *
+ * event.h: DATE event data format
+ *
+ * Revision History:
+ * V01.00 RD PVV 09-Jan-97 Initial version
+ * V01.01 AV 24-Feb-97 Added START_OF_RUN_FILES and triggerNb
+ * V02.02 RD 13-Mar-97 Detector ID mask type added
+ * V02.03 PVV 20-Mar-97 Detector ID on 128 bits
+ * V02.03 RD PVV 20-Mar-97 Added EVENT_DATA_TRUNCATED
+ * V02.04 PVV 06-May-97 Added EVENT_TYPE_MASK
+ * V02.05 RD PVV 09-May-97 Increase EVENT_DATA_MAX_SIZE to 50 K
+ * V02.06 WB MG 22-May-97 Added END_OF_RUN_FILES
+ * V02.07 WB 23-May-97 Added errorCode, deadTime, deadTimeusec
+ * EVENT_DATA_MAX_SIZE set to 100 * 1024
+ * MAX_DETECTORS set to 126
+ * V02.08 PVV 02-Jun-97 Modify the encoding of types
+ * V02.09 WB RD PVV 28-Jul-98 Add fileSeqNb in the header.
+ * Reduce detector mask to 3 long
+ * V02.10 RD 31-Jul-98 (start|end)OfRunFiles added to eventType
+ * V02.11 PVV RD 02-Sep-98 Event type re-numbered
+ * Equipment bit added to event type
+ * V02.12 AV PVV RD 03-Sep-98 FileSeqNo moved before detectorId
+ * V02.13 RD 08-Oct-98 32 bits fields defined as long32
+ * V02.13 RD 19-Feb-99 Endianness/swap definitions added
+ * V02.14 WB PVV RD 21-Jun-99 typeAttribute added
+ * V02.15 RD 27-Jul-99 Macros for typeAttribute handling added
+ * V02.16 RD 19-Nov-99 Bug in Attributes test/set fixed
+ * V02.17 WB PVV RD 08-May-00 System attributes for SOR and EOR added
+ * V02.18 RD 18-May-00 EVENT_H_ID added
+ * V02.19 RD 10-Aug-00 Macros for detectorId handling added
+ * V03.00 RD 23-Nov-00 Version for DATE V4
+ * V03.01 AV KS 05-Apr-02 Introduction of eventLocationDescriptorStruct
+ * V03.02 KS 10-May-02 Added ATTR_KEEP_PAGES for COLE
+ * RD 30-Apr-04 Added definitions for the Common Data Header
+ * RD 24-Jun-04 Added definitions for HLT DECISION
+ * RD 13-Jul-04 Added macros to OR attributes
+ * V03.03 RD 25-May-05 Added eventTimestamp
+ * V03.04 RD 17-Aug-05 Added VANGUARD and REARGUARD events
+ * V03.05 RD 05-Sep-05 Added SYSTEM and DETECTOR software tri evts
+ * V03.06 RD 14-Sep-05 VANGUARD/REARGUARD changed into START_OF_DATA
+ * and END_OF_DATA
+ *
+ * Preprocessor definitions:
+ * NDEBUG Define BEFORE including this file to disable run-time checks on
+ * various parameters
+ *
+ * Related facilities:
+ * validateEvent.c Validation program, should be run after EACH change to
+ * the definitions given here below
+ ***************************************************************************/
+#ifndef __event_h__
+#define __event_h__
+
+#define EVENT_MAJOR_VERSION_NUMBER 0x0003
+#define EVENT_MINOR_VERSION_NUMBER 0x0006
+
+/* ========== System includes ========= */
+#include <string.h> /* Needed by: memset, memcpy */
+#include <assert.h> /* Needed by: assert */
+
+/* ========== Definitions for the event header ========== */
+
+/* ---------- Header base size ---------- */
+/* This value must be updated for each change in the eventHeaderStruct */
+#define EVENT_HEAD_BASE_SIZE 68
+
+/* ---------- Event size ---------- */
+typedef unsigned long32 eventSizeType;
+
+/* ---------- Magic signature and its byte-swapped version ---------- */
+typedef unsigned long32 eventMagicType;
+#define EVENT_MAGIC_NUMBER ((eventMagicType)0xDA1E5AFE)
+#define EVENT_MAGIC_NUMBER_SWAPPED ((eventMagicType)0xFE5A1EDA)
+
+/* ---------- Header size ---------- */
+typedef unsigned long32 eventHeadSizeType;
+
+/* ---------- Unique version identifier ---------- */
+#define EVENT_CURRENT_VERSION \
+ (((EVENT_MAJOR_VERSION_NUMBER<<16)&0xffff0000)|\
+ (EVENT_MINOR_VERSION_NUMBER&0x0000ffff))
+typedef unsigned long32 eventVersionType;
+
+/* ---------- Event type ---------- */
+typedef unsigned long32 eventTypeType;
+#define START_OF_RUN ((eventTypeType) 1)
+#define END_OF_RUN ((eventTypeType) 2)
+#define START_OF_RUN_FILES ((eventTypeType) 3)
+#define END_OF_RUN_FILES ((eventTypeType) 4)
+#define START_OF_BURST ((eventTypeType) 5)
+#define END_OF_BURST ((eventTypeType) 6)
+#define PHYSICS_EVENT ((eventTypeType) 7)
+#define CALIBRATION_EVENT ((eventTypeType) 8)
+#define EVENT_FORMAT_ERROR ((eventTypeType) 9)
+#define START_OF_DATA ((eventTypeType)10)
+#define END_OF_DATA ((eventTypeType)11)
+#define SYSTEM_SOFTWARE_TRIGGER_EVENT ((eventTypeType)12)
+#define DETECTOR_SOFTWARE_TRIGGER_EVENT ((eventTypeType)13)
+#define EVENT_TYPE_MIN 1
+#define EVENT_TYPE_MAX 13
+enum eventTypeEnum {
+ startOfRun = START_OF_RUN,
+ endOfRun = END_OF_RUN,
+ startOfRunFiles = START_OF_RUN_FILES,
+ endOfRunFiles = END_OF_RUN_FILES,
+ startOfBurst = START_OF_BURST,
+ endOfBurst = END_OF_BURST,
+ physicsEvent = PHYSICS_EVENT,
+ calibrationEvent = CALIBRATION_EVENT,
+ formatError = EVENT_FORMAT_ERROR,
+ startOfData = START_OF_DATA,
+ endOfData = END_OF_DATA,
+ systemSoftwareTriggerEvent = SYSTEM_SOFTWARE_TRIGGER_EVENT,
+ detectorSoftwareTriggerEvent = DETECTOR_SOFTWARE_TRIGGER_EVENT
+};
+#define EVENT_TYPE_OK(t) (((t) >= EVENT_TYPE_MIN) && (((t) <= EVENT_TYPE_MAX)))
+
+/* ---------- Run number ---------- */
+typedef unsigned long32 eventRunNbType;
+
+/* ---------- The eventId field ---------- */
+#define EVENT_ID_BYTES 8
+#define EVENT_ID_WORDS ((EVENT_ID_BYTES) >> 2)
+typedef unsigned long32 eventIdType[EVENT_ID_WORDS];
+
+ /* PERIOD - ORBIT - BUNCH crossing type events */
+#define EVENT_ID_MAX_PERIOD 0x0fffffff
+#define EVENT_ID_MAX_ORBIT 0x00ffffff
+#define EVENT_ID_MAX_BUNCH_CROSSING 0x00000fff
+#define LOAD_EVENT_ID(id,s,o,bc) \
+ (EVENT_ID_SET_PERIOD(id,s), \
+ EVENT_ID_SET_ORBIT(id,o), \
+ EVENT_ID_SET_BUNCH_CROSSING(id,bc))
+#define EVENT_ID_GET_BUNCH_CROSSING(id) ((id)[1]&0x00000fff)
+#define EVENT_ID_GET_ORBIT(id) \
+ ((((id)[0]<<20)&0xf00000)|(((id)[1]>>12)&0xfffff))
+#define EVENT_ID_GET_PERIOD(id) (((id)[0]>>4)&0x0fffffff)
+
+#define EVENT_ID_SET_BUNCH_CROSSING(id,v) \
+ (assert(((v)>=0)&&((v)<=EVENT_ID_MAX_BUNCH_CROSSING)), \
+ (id)[1]=((id)[1]&0xfffff000)|((v)&0xfff))
+#define EVENT_ID_SET_ORBIT(id,v) \
+ (assert(((v)>=0)&&((v)<=EVENT_ID_MAX_ORBIT)), \
+ (id)[0]=(((id)[0])&0xfffffff0)|(((v)&0x00f00000)>>20), \
+ (id)[1]=(((id)[1])&0x00000fff)|(((v)&0x000fffff)<<12))
+#define EVENT_ID_SET_PERIOD(id,v) \
+ (assert(((v)>=0)&&((v)<=EVENT_ID_MAX_PERIOD)), \
+ (id)[0]=(((id)[0])&0x0000000f)|(((v)&0x0fffffff)<<4))
+
+ /* RAW type event */
+#define EVENT_ID_MAX_NB_IN_RUN 0xffffffff
+#define EVENT_ID_MAX_BURST_NB 0x00000fff
+#define EVENT_ID_MAX_NB_IN_BURST 0x000fffff
+#define LOAD_RAW_EVENT_ID(id,nir,bn,nib) \
+ (assert(((bn)>=0) && ((bn)<=EVENT_ID_MAX_BURST_NB) && \
+ ((nib)>=0) && ((nib)<=EVENT_ID_MAX_NB_IN_BURST)), \
+ (id)[0]=nir, \
+ (id)[1]=((bn<<20)&0xfff00000)|(nib&0x000fffff))
+#define EVENT_ID_SET_NB_IN_RUN(id,nir) \
+ (assert(((nir)>=0)&&((nir)<=EVENT_ID_MAX_NB_IN_RUN)), \
+ (id)[0]=(nir))
+#define EVENT_ID_SET_BURST_NB(id,bn) \
+ (assert(((bn)>=0)&&((bn)<=EVENT_ID_MAX_BURST_NB)), \
+ (id)[1]=((id)[1]&0x000fffff)|(((bn)<<20)&0xfff00000))
+#define EVENT_ID_SET_NB_IN_BURST(id,nib) \
+ (assert(((nib)>=0)&&((nib)<=EVENT_ID_MAX_NB_IN_BURST)), \
+ (id)[1]=((id)[1]&0xfff00000)|((nib)&0x000fffff))
+#define EVENT_ID_GET_NB_IN_RUN(id) ((id)[0])
+#define EVENT_ID_GET_BURST_NB(id) (((id)[1]>>20)&0x00000fff)
+#define EVENT_ID_GET_NB_IN_BURST(id) ((id)[1]&0x000fffff)
+
+ /* All events */
+#define EQ_EVENT_ID(a,b) ((((a)[0])==((b)[0]))&&(((a)[1])==((b)[1])))
+#define GT_EVENT_ID(a,b) \
+ (((((a)[0])>((b)[0])))||((((a)[0])==((b)[0]))&&(((a)[1])>((b)[1]))))
+#define LT_EVENT_ID(a,b) \
+ ((((a)[0])<((b)[0])) || ((((a)[0])==((b)[0]))&&(((a)[1])<((b)[1]))))
+#define GE_EVENT_ID(a,b) (!LT_EVENT_ID(a,b))
+#define LE_EVENT_ID(a,b) (!GT_EVENT_ID(a,b))
+#define COPY_EVENT_ID(from,to) \
+ memcpy((void*)to,(const void*)from,EVENT_ID_BYTES)
+#define ADD_EVENT_ID(a,b) ((a)[1]+=(b)[1],(a)[0]+=(b)[0])
+#define SUB_EVENT_ID(a,b) ((a)[1]-=(b)[1],(a)[0]-=(b)[0])
+#define ZERO_EVENT_ID(id) memset(id,0,EVENT_ID_BYTES)
+
+/* ---------- Trigger pattern (and relative masks) ---------- */
+#define EVENT_TRIGGER_PATTERN_BYTES 8
+#define EVENT_TRIGGER_PATTERN_WORDS ((EVENT_TRIGGER_PATTERN_BYTES)>>2)
+typedef unsigned long32 eventTriggerPatternType[EVENT_TRIGGER_PATTERN_WORDS];
+#define EVENT_TRIGGER_ID_MIN 1
+#define EVENT_TRIGGER_ID_MAX 50
+#define CHECK_TRIGGER(t) (assert(((t)>=EVENT_TRIGGER_ID_MIN) && \
+ ((t)<=EVENT_TRIGGER_ID_MAX)))
+#define TRIGGER_TO_BIT(t) (1<<((t)&0x1f))
+#define TRIGGER_TO_WORD(t) (CHECK_TRIGGER(t), (t)>>5)
+#define ZERO_TRIGGER_PATTERN(p) memset(p,0,EVENT_TRIGGER_PATTERN_BYTES)
+#define SET_TRIGGER_IN_PATTERN(p,id) (p)[TRIGGER_TO_WORD(id)] |= \
+ TRIGGER_TO_BIT(id)
+#define CLEAR_TRIGGER_IN_PATTERN(p,id) (p)[TRIGGER_TO_WORD(id)] &= \
+ ~(TRIGGER_TO_BIT(id))
+#define FLIP_TRIGGER_IN_PATTERN(p,id) (p)[TRIGGER_TO_WORD(id)] ^= \
+ TRIGGER_TO_BIT(id)
+#define TEST_TRIGGER_IN_PATTERN(p,id) (((p)[TRIGGER_TO_WORD(id)] & \
+ TRIGGER_TO_BIT(id)) != 0)
+#define TRIGGER_PATTERN_INVALID(p) (((p)[0] & 1) == 0)
+#define TRIGGER_PATTERN_VALID(p) (((p)[0] & 1) != 0)
+#define VALIDATE_TRIGGER_PATTERN(p) ((p)[0] |= 1)
+#define INVALIDATE_TRIGGER_PATTERN(p) ((p)[0] &= 0xfffffffe)
+#define COPY_TRIGGER_PATTERN(f,t) memcpy(t,f,EVENT_TRIGGER_PATTERN_BYTES)
+#define TRIGGER_PATTERN_OK(p) (((p)[1] & 0xfff80000) == 0)
+
+/* ---------- Detectors cluster (and relative masks) ---------- */
+#define EVENT_DETECTOR_PATTERN_BYTES 4
+#define EVENT_DETECTOR_PATTERN_WORDS (EVENT_DETECTOR_PATTERN_BYTES>>2)
+typedef unsigned long32 eventDetectorPatternType[EVENT_DETECTOR_PATTERN_WORDS];
+#define EVENT_DETECTOR_ID_MIN 1
+#define EVENT_DETECTOR_ID_MAX 24
+#define CHECK_DETECTOR(d) (assert(((d) >= EVENT_DETECTOR_ID_MIN) &&\
+ ((d) <= EVENT_DETECTOR_ID_MAX)))
+#define DETECTOR_TO_BIT(d) (CHECK_DETECTOR(d), 1<<(d))
+#define ZERO_DETECTOR_PATTERN(p) ((p)[0] = 0)
+#define SET_DETECTOR_IN_PATTERN(p,d) ((p)[0] |= DETECTOR_TO_BIT(d))
+#define CLEAR_DETECTOR_IN_PATTERN(p,d) ((p)[0] &= ~(DETECTOR_TO_BIT(d)))
+#define FLIP_DETECTOR_IN_PATTERN(p,d) ((p)[0] ^= DETECTOR_TO_BIT(d))
+#define TEST_DETECTOR_IN_PATTERN(p,d) (((p)[0] & DETECTOR_TO_BIT(d))!=0)
+#define DETECTOR_PATTERN_INVALID(p) (((p)[0] & 1) == 0)
+#define DETECTOR_PATTERN_VALID(p) (((p)[0] & 1) != 0)
+#define VALIDATE_DETECTOR_PATTERN(p) ((p)[0] |= 1)
+#define INVALIDATE_DETECTOR_PATTERN(p) ((p)[0] &= 0xfffffffe)
+#define COPY_DETECTOR_PATTERN(f,t) ((t)[0] = (f)[0])
+#define DETECTOR_PATTERN_OK(p) (((p)[0] & 0xfe000000) == 0)
+
+/* ---------- The sizes and positions of the typeAttribute field ---------- */
+#define ALL_ATTRIBUTE_WORDS 3
+#define ALL_ATTRIBUTE_BYTES (ALL_ATTRIBUTE_WORDS * 4)
+#define ALL_ATTRIBUTE_BITS (ALL_ATTRIBUTE_BYTES * 8)
+#define USER_ATTRIBUTE_WORDS 2
+#define USER_ATTRIBUTE_BYTES (USER_ATTRIBUTE_WORDS * 4)
+#define USER_ATTRIBUTE_BITS (USER_ATTRIBUTE_BYTES * 8)
+#define FIRST_USER_ATTRIBUTE 0
+#define LAST_USER_ATTRIBUTE (USER_ATTRIBUTE_BITS - 1)
+#define SYSTEM_ATTRIBUTE_WORDS 1
+#define SYSTEM_ATTRIBUTE_BYTES (SYSTEM_ATTRIBUTE_WORDS * 4)
+#define SYSTEM_ATTRIBUTE_BITS (SYSTEM_ATTRIBUTE_BYTES * 8)
+#define FIRST_SYSTEM_ATTRIBUTE USER_ATTRIBUTE_BITS
+#define LAST_SYSTEM_ATTRIBUTE (USER_ATTRIBUTE_BITS + \
+ SYSTEM_ATTRIBUTE_BITS - 1)
+typedef unsigned long32 eventTypeAttributeType[ALL_ATTRIBUTE_WORDS];
+
+ /* Word and bit definitions */
+#define SYS_ATTR_2_W(b) (assert(((b)>=64)&&((b)<=95)),2)
+#define USR_ATTR_2_W(b) (assert(((b)>= 0)&&((b)<=63)),(b)>>5)
+#define ATTR_2_W(b) (assert(((b)>= 0)&&((b)<=95)),(b)>>5)
+#define ATTR_2_B(b) (1<<((b)&0x1f))
+
+ /* Macros to handle all attributes without distinction */
+#define RESET_ATTRIBUTES(m) ((m)[2] = (m)[1] = (m)[0] = 0)
+#define SET_ANY_ATTRIBUTE(m,b) (m)[ATTR_2_W(b)] |= ATTR_2_B(b)
+#define CLEAR_ANY_ATTRIBUTE(m,b) (m)[ATTR_2_W(b)] &= ~(ATTR_2_B(b))
+#define FLIP_ANY_ATTRIBUTE(m,b) (m)[ATTR_2_W(b)] ^= ATTR_2_B(b)
+#define TEST_ANY_ATTRIBUTE(m,b) (((m)[ATTR_2_W(b)] & ATTR_2_B(b))!= 0)
+#define COPY_ALL_ATTRIBUTES( from, to ) \
+ memcpy((void *)&to[0], (const void *)&from[0], ALL_ATTRIBUTE_BYTES)
+#define OR_ALL_ATTRIBUTES( from, to ) \
+ ((to)[2] |= (from)[2], (to)[1] |= (from)[1], (to)[0] |= (from)[0])
+
+ /* Macros to handle SYSTEM attributes */
+#define RESET_SYSTEM_ATTRIBUTES(m) ((m)[2] = 0)
+#define SET_SYSTEM_ATTRIBUTE(m,b) (m)[SYS_ATTR_2_W(b)] |= ATTR_2_B(b)
+#define CLEAR_SYSTEM_ATTRIBUTE(m,b) (m)[SYS_ATTR_2_W(b)] &= ~(ATTR_2_B(b))
+#define FLIP_SYSTEM_ATTRIBUTE(m,b) (m)[SYS_ATTR_2_W(b)] ^= ATTR_2_B(b)
+#define TEST_SYSTEM_ATTRIBUTE(m,b) (((m)[SYS_ATTR_2_W(b)] & ATTR_2_B(b)) != 0)
+#define COPY_SYSTEM_ATTRIBUTES( from, to ) \
+ memcpy((void *)&to[2], (const void *)&from[2], SYSTEM_ATTRIBUTE_BYTES)
+#define OR_SYSTEM_ATTRIBUTES( from, to ) ((to)[2] |= (from)[2])
+
+ /* Macros to handle USER attributes */
+#define RESET_USER_ATTRIBUTES(m) ((m)[0] = (m)[1] = 0)
+#define SET_USER_ATTRIBUTE(m,b) (m)[USR_ATTR_2_W(b)] |= ATTR_2_B(b)
+#define CLEAR_USER_ATTRIBUTE(m,b) (m)[USR_ATTR_2_W(b)] &= ~(ATTR_2_B(b))
+#define FLIP_USER_ATTRIBUTE(m,b) (m)[USR_ATTR_2_W(b)] ^= ATTR_2_B(b)
+#define TEST_USER_ATTRIBUTE(m,b) (((m)[USR_ATTR_2_W(b)] & ATTR_2_B(b)) != 0)
+#define COPY_USER_ATTRIBUTES( from, to ) \
+ memcpy((void *)&to[0], (const void *)&from[0], USER_ATTRIBUTE_BYTES)
+#define OR_USER_ATTRIBUTES( from, to ) \
+ ((to)[1] |= (from)[1], (to)[0] |= (from)[0])
+
+ /* System attributes assignment */
+#define ATTR_P_START 64 /* Start of a phase */
+#define ATTR_P_END 65 /* End of a phase */
+#define ATTR_START_OF_RUN_START ATTR_P_START/* Start of SOR phase */
+#define ATTR_START_OF_RUN_END ATTR_P_END /* End of SOR phase */
+#define ATTR_END_OF_RUN_START ATTR_P_START/* Start of EOR phase */
+#define ATTR_END_OF_RUN_END ATTR_P_END /* End of SOR phase */
+#define ATTR_EVENT_SWAPPED 66 /* Swapped event header */
+#define ATTR_EVENT_PAGED 67 /* Paged event */
+#define ATTR_SUPER_EVENT 68 /* Super event */
+#define ATTR_ORBIT_BC 69 /* Orbit/bunch crossing in ID*/
+#define ATTR_KEEP_PAGES 70 /* Do not deallocate pages */
+#define ATTR_HLT_DECISION 71 /* Event contains HLT decis. */
+
+#define ATTR_EVENT_DATA_TRUNCATED 94 /* Truncated payload */
+#define ATTR_EVENT_ERROR 95 /* Invalid event content */
+
+#define SYSTEM_ATTRIBUTES_OK(m) \
+ ((((m)[2]) & ~(ATTR_2_B(ATTR_P_START) | \
+ ATTR_2_B(ATTR_P_END) | \
+ ATTR_2_B(ATTR_EVENT_SWAPPED) | \
+ ATTR_2_B(ATTR_EVENT_PAGED) | \
+ ATTR_2_B(ATTR_SUPER_EVENT) | \
+ ATTR_2_B(ATTR_ORBIT_BC) | \
+ ATTR_2_B(ATTR_KEEP_PAGES) | \
+ ATTR_2_B(ATTR_HLT_DECISION) | \
+ ATTR_2_B(ATTR_EVENT_DATA_TRUNCATED) | \
+ ATTR_2_B(ATTR_EVENT_ERROR))) == 0)
+
+/* ---------- LDC and GDC identifier ---------- */
+typedef unsigned long32 eventHostIdType;
+typedef eventHostIdType eventLdcIdType;
+typedef eventHostIdType eventGdcIdType;
+#define HOST_ID_MIN ((eventHostIdType)0) /* The minimum allowed ID */
+#define HOST_ID_MAX ((eventHostIdType)511) /* The maximum allowed ID */
+#define VOID_ID ((eventHostIdType)-1) /* Unloaded ID */
+
+/* ---------- timestamp ---------- */
+/* The following definition is in common for 32 and 64 bit machines.
+ In both architectures, the field must be loaded into a time_t
+ variable before being used. Failure to do so may cause undefined
+ results up to the early termination of the process.
+
+ The recommended procedure to use this field is the following:
+
+ #include <time.h>
+
+ time_t t;
+
+ t = eventHeaderStruct.eventTimestamp;
+ cTime( &t ); (or whatever else can be done with a time_t)
+
+ Please note that the available timestamp will wrap sometime
+ around Jan 18, 19:14:07, 2038...
+*/
+typedef unsigned long32 eventTimestampType;
+
+/* ---------- The event header structure (with + without data) ---------- */
+struct eventHeaderStruct {
+ eventSizeType eventSize;
+ eventMagicType eventMagic;
+ eventHeadSizeType eventHeadSize;
+ eventVersionType eventVersion;
+ eventTypeType eventType;
+ eventRunNbType eventRunNb;
+ eventIdType eventId;
+ eventTriggerPatternType eventTriggerPattern;
+ eventDetectorPatternType eventDetectorPattern;
+ eventTypeAttributeType eventTypeAttribute;
+ eventLdcIdType eventLdcId;
+ eventGdcIdType eventGdcId;
+ eventTimestampType eventTimestamp;
+};
+
+struct eventStruct {
+ struct eventHeaderStruct eventHeader;
+ unsigned short eventRawData[1];
+};
+
+/* ========== Definitions for the Vector ========== */
+typedef short eventVectorBankIdType;
+typedef unsigned int eventVectorSizeType;
+typedef unsigned int eventVectorOffsetType;
+
+struct eventVectorStruct {
+ eventVectorBankIdType eventVectorBankId;
+ unsigned eventVectorPointsToVector : 1;
+ eventVectorSizeType eventVectorSize;
+ eventVectorOffsetType eventVectorStartOffset;
+};
+
+/* ========== Definitions for the payload descriptor ========== */
+typedef unsigned long32 eventNumEquipmentsType;
+typedef struct eventVectorStruct eventExtensionVectorType;
+
+struct vectorPayloadDescriptorStruct {
+ eventNumEquipmentsType eventNumEquipments;
+ eventExtensionVectorType eventExtensionVector;
+};
+
+/* ========== Definitions for the equipment header ========== */
+typedef long32 equipmentSizeType;
+typedef long32 equipmentTypeType;
+typedef long32 equipmentIdType;
+typedef eventTypeAttributeType equipmentTypeAttributeType;
+typedef long32 equipmentBasicElementSizeType;
+
+struct equipmentHeaderStruct {
+ equipmentSizeType equipmentSize;
+ equipmentTypeType equipmentType;
+ equipmentIdType equipmentId;
+ equipmentTypeAttributeType equipmentTypeAttribute;
+ equipmentBasicElementSizeType equipmentBasicElementSize;
+};
+
+struct equipmentDescriptorStruct {
+ struct equipmentHeaderStruct equipmentHeader;
+ struct eventVectorStruct equipmentVector;
+};
+
+struct equipmentStruct {
+ struct equipmentHeaderStruct equipmentHeader;
+ unsigned short equipmentRawData[1];
+};
+
+/* ========== Definition of the event location for the simpleFifo ========== */
+struct eventLocationDescriptorStruct {
+ eventVectorBankIdType eventBankId;
+ eventVectorOffsetType eventOffset;
+};
+
+/* ========== Global macros ========== */
+
+/* The macro PAGED_EVENT_SIZE receives in input the ADDRESS of a paged
+ event and returns the size (in bytes) of the first page of the
+ event */
+#define PAGED_EVENT_SIZE( event ) \
+ (EVENT_HEAD_BASE_SIZE +sizeof( struct vectorPayloadDescriptorStruct ) + \
+ ((*(eventNumEquipmentsType *)((void*)event+EVENT_HEAD_BASE_SIZE))* \
+ (sizeof( struct equipmentDescriptorStruct ))))
+
+/* ========== Common data header ========== */
+#define CDH_SIZE (8 * 4)
+#define CDH_VERSION 1
+
+#define CDH_TRIGGER_OVERLAP_ERROR_BIT 0
+#define CDH_TRIGGER_MISSING_ERROR_BIT 1
+#define CDH_DATA_PARITY_ERROR_BIT 2
+#define CDH_CONTROL_PARITY_ERROR_BIT 3
+#define CDH_TRIGGER_INFORMATION_UNAVAILABLE_BIT 4
+#define CDH_FEE_ERROR_BIT 5
+#define CDH_HLT_DECISION_BIT 6
+#define CDH_HLT_PAYLOAD_BIT 7
+#define CDH_HLT_DDG_PAYLOAD_BIT 8
+
+/* Please note how the above data structure has been
+ defined for LE systems. Code running on BE systems
+ must have all fields reverted to work correctly! */
+struct commonDataHeaderStruct {
+ unsigned cdhBlockLength : 32;
+ /* ------------------------------------- */
+ unsigned cdhEventId1 : 12;
+ unsigned cdhMBZ0 : 2;
+ unsigned cdhL1TriggerMessage : 10;
+ unsigned cdhVersion : 8;
+ /* ------------------------------------- */
+ unsigned cdhEventId2 : 24;
+ unsigned cdhMBZ1 : 8;
+ /* ------------------------------------- */
+ unsigned cdhParticipatingSubDetectors : 24;
+ unsigned cdhBlockAttributes : 8;
+ /* ------------------------------------- */
+ unsigned cdhMiniEventId : 12;
+ unsigned cdhStatusErrorBits : 16;
+ unsigned cdhMBZ2 : 4;
+ /* ------------------------------------- */
+ unsigned cdhTriggerClassesLow : 32;
+ /* ------------------------------------- */
+ unsigned cdhTriggerClassesHigh : 18;
+ unsigned cdhMBZ3 : 10;
+ unsigned cdhRoiLow : 4;
+ /* ------------------------------------- */
+ unsigned cdhRoiHigh : 32;
+};
+
+#endif