Coding conventions + Bug fix in NextBunch function of AliAltroData. The GetStartBin...
[u/mrichter/AliRoot.git] / RAW / dateStream.cxx
CommitLineData
f3a800f8 1/*
2 dateStream.c
3 ============
4
5 Utility to simulate a DATE raw data stream using a given set of raw
6 data files and a configuration file.
7
8 Revision history:
9
10 V01.00 4/05/2004 RD Created
11 V01.01 25/10/2005 RD Support added for timestamp
12 V01.02 4/04/2006 RD Support for CDH
13 V01.03 24/05/2006 RD Added "Direct disk access" option
14*/
15#define VID "1.03"
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <errno.h>
20#include <assert.h>
21#include <ctype.h>
22#include <time.h>
21e7df44 23#include <cassert>
f3a800f8 24#include "event.h"
25
26#define DESCRIPTION "DATE raw data stream simulator"
27#ifdef AIX
28static
29#endif
30char fileHandlerIdent[]= "@(#)""" __FILE__ """: """ DESCRIPTION \
31 """ """ VID """ """ \
32 """ compiled """ __DATE__ """ """ __TIME__;
33
34#define DBG_BASE if ( debug > 0 )
35#define DBG_DETAILED if ( debug > 1 )
36#define DBG_VERBOSE if ( debug > 2 )
37
38#ifndef TRUE
39# define TRUE (0 == 0)
40#endif
41#ifndef FALSE
42# define FALSE (0 == 1)
43#endif
44
45const char *myName;
46int debug;
47FILE *outF;
a8bf0b36 48typedef enum { unknown, ldc, gdc } workingAsType;
49typedef enum { collider, fixedTarget } workingModeType;
50workingAsType workingAs;
51workingModeType workingMode;
f3a800f8 52struct ldcDescriptorStruct {
53 eventLdcIdType id;
54 struct ldcDescriptorStruct *next;
55} *ldcsHead, *ldcsTail;
56void *eventsHead, *eventsTail;
57struct gdcEventDescriptorStruct {
58 struct ldcEventDescriptorStruct *head;
59 struct ldcEventDescriptorStruct *tail;
60 struct gdcEventDescriptorStruct *next;
61 struct eventHeaderStruct header;
62 int loaded;
63} *currGdc;
64struct ldcEventDescriptorStruct {
65 struct equipmentEventDescriptorStruct *head;
66 struct equipmentEventDescriptorStruct *tail;
67 struct ldcEventDescriptorStruct *next;
68 eventLdcIdType id;
69 struct eventHeaderStruct header;
70 int loaded;
71} *currLdc;
72struct equipmentEventDescriptorStruct {
73 struct equipmentEventDescriptorStruct *next;
74 equipmentIdType id;
75 struct payloadDescriptorStruct *payload;
76 struct equipmentHeaderStruct header;
77} *currEvent;
78struct payloadDescriptorStruct {
79 struct payloadDescriptorStruct *next;
80 char *fileName;
81 int fileSize;
82 int size;
83 void *data;
84} *payloadsHead, *payloadsTail;
85int lineNo;
86eventGdcIdType currGdcId;
87eventLdcIdType currLdcId;
88equipmentIdType currEquipmentId;
89int currRunNb;
90int numOfLdcs;
91int numOfEvents;
92int createSorEor;
93int handleCDH;
94eventIdType oneEventDelta;
95eventIdType currEventId;
96int gotAliceTrigger;
97int bufferData;
98
99void dumpPayload( const struct payloadDescriptorStruct *p ) {
100 char *c;
101 int i;
102 int printable;
103
104 if ( p->data != NULL ) {
105 for ( i = 0, c = (char *)p->data, printable = TRUE;
106 printable && i != p->size;
107 c++, i++ )
108 printable = isascii( *c );
109 if ( printable ) {
110 printf( " \"" );
111 for ( i = 0, c = (char *)p->data; i != p->size; c++, i++ ) {
112 if ( *c == '\n' )
113 printf( "\"\n \"" );
114 else
115 putchar( *c );
116 }
117 if ( *c != '\n' ) printf( "\"\n" );
118 } else {
119 long32 *v;
120 for ( i = 0, v = (long32 *)p->data;
121 i+4 <= p->size;
122 v++, i += 4 ) {
123 if ( i % (4*8) == 0 ) {
124 if ( i != 0 ) printf( "\n" );
125 printf( " " );
126 }
127 printf( "%08x ", *v );
128 }
129 if ( i < p->size ) {
130 int j = 0;
a8bf0b36 131
f3a800f8 132 printf( "\n " );
133 while ( i != p->size ) {
134 printf( "%02x ", *((char *)p->data + p->size - j - 1) & 0xff );
135 j++;
136 i++;
137 }
138 }
139 }
140 printf( "\n" );
141 }
142} /* End of dumpPayload */
143
144void dumpEvents() {
145 assert( workingAs == ldc || workingAs == gdc );
146 if ( eventsHead != NULL ) {
147 printf( "Events:\n" );
148 if ( workingAs == gdc ) {
149 struct gdcEventDescriptorStruct *gdc;
150
151 for ( gdc = (struct gdcEventDescriptorStruct *)eventsHead;
152 gdc != NULL;
153 gdc = gdc->next ) {
154 struct ldcEventDescriptorStruct *ldc;
155
156 printf( " GDC (%p)\n", gdc );
157 for ( ldc = gdc->head; ldc != NULL; ldc = ldc->next ) {
158 struct equipmentEventDescriptorStruct *eq;
159
160 printf( " LDC (%p): %d\n", ldc, ldc->id );
161 for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
162 printf( " EQUIPMENT (%p): %d PAYLOAD (%p):",
163 eq,
164 eq->id,
165 eq->payload );
166 fflush( stdout );
167 printf( "\"%s\" (%d bytes)\n",
168 eq->payload->fileName,
169 eq->payload->size );
170 dumpPayload( eq->payload );
171 }
172 }
173 }
174 }
175 if ( workingAs == ldc ) {
176 struct ldcEventDescriptorStruct *ldc;
177
178 for ( ldc = (struct ldcEventDescriptorStruct *)eventsHead;
179 ldc != NULL;
180 ldc = ldc->next ) {
181 struct equipmentEventDescriptorStruct *eq;
182
183 printf( " LDC\n" );
184 for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
185 printf( " EQUIPMENT (%p): %d PAYLOAD (%p):",
186 eq,
187 eq->id,
188 eq->payload );
189 fflush( stdout );
190 printf( "\"%s\" (%d bytes)\n",
191 eq->payload->fileName,
192 eq->payload->size );
193 dumpPayload( eq->payload );
194 }
195 }
196 }
197 } else {
198 printf( "Events: EMPTY\n" );
199 }
200} /* End of dumpEvents */
201
202void getLine( char *line, const int maxSize ) {
203 int read;
204 int c;
a8bf0b36 205
f3a800f8 206 for ( read = 0; !feof( stdin ) && !ferror( stdin ) && read != maxSize; read++ ) {
207 if ( (line[read] = getchar()) == '\n' ) break;
208 }
209 if ( ferror( stdin ) ) {
210 fprintf( stderr,
211 "%s: failed to read configuration input errno:%d ",
212 myName, errno );
213 perror( "" );
214 exit( 1 );
215 }
216 if ( feof( stdin ) ) read--;
217 if ( read == maxSize && line[read] != '\n' ) {
218 fprintf( stderr,
219 "%s: Input line # %d too long (%d chars max)\n",
220 myName, lineNo, maxSize-1 );
221 exit( 1 );
222 }
223 line[ read ] = 0;
224 DBG_VERBOSE {
225 if ( !( read == 0 && feof( stdin ) ) ) {
226 printf( "%d) [%3d] \"%s\"", lineNo, read, line );
227 }
228 }
229 for ( c = 0; c != read; c++ ) {
230 if ( line[c] == '#' ) {
231 line[c] = 0;
232 break;
233 }
234 }
235 DBG_VERBOSE {
236 if ( read != c ) {
237 printf( " => \"%s\"", line );
238 }
239 if ( feof( stdin ) ) printf( "<<< EOF >>>" );
240 if ( ferror( stdin ) ) printf( "<<< FERROR >>>" );
241 printf( "\n" );
242 }
243} /* End of getLine */
244
245void handleLdc( eventLdcIdType ldcId ) {
246 struct ldcDescriptorStruct *ldc;
247
248 if ( ldcsHead != NULL ) {
249 for ( ldc = ldcsHead; ldc != NULL; ldc = ldc->next ) {
250 if ( ldc->id == ldcId ) {
251 return;
252 }
253 }
254 }
255 if ( (ldc = (struct ldcDescriptorStruct *)malloc( sizeof( *ldc ) )) == NULL ) {
256 fprintf( stderr,
257 "%s: Failed to malloc for %d bytes (struct ldcDescriptorStruct)\n",
258 myName, (int)sizeof( *ldc ) );
259 exit( 1 );
260 }
261 ldc->id = ldcId;
262 ldc->next = NULL;
263 if ( ldcsHead == NULL ) {
264 ldcsHead = ldcsTail = ldc;
265 } else {
266 ldcsTail->next = ldc;
267 ldcsTail = ldc;
268 }
269 numOfLdcs++;
270} /* End of handleLdc */
271
272void createNewEvent() {
273 assert( workingAs == ldc || workingAs == gdc );
274 if ( workingAs == ldc ) {
275 struct ldcEventDescriptorStruct *p;
276
277 if ( (p = (struct ldcEventDescriptorStruct *)malloc( sizeof( *p ) ))
278 == NULL ) {
279 fprintf( stderr,
280 "%s: failed to malloc for %d bytes (createNewEvent: struct ldcEventDescriptorStruct)",
281 myName, (int)sizeof( *p ) );
282 perror( "" );
283 exit( 1 );
284 }
285 p->loaded = FALSE;
286 p->head = p->tail = NULL;
287 p->next = NULL;
288 currLdc = p;
289 if ( eventsHead == NULL ) {
290 eventsHead = eventsTail = p;
291 } else {
292 struct ldcEventDescriptorStruct *q =
293 (struct ldcEventDescriptorStruct *)eventsTail;
a8bf0b36 294
f3a800f8 295 q->next = p;
296 eventsTail = p;
297 }
298 p->id = currLdcId;
299 } else if ( workingAs == gdc ) {
300 struct gdcEventDescriptorStruct *p;
301
302 if ( (p = (struct gdcEventDescriptorStruct *)malloc( sizeof( *p ) ))
303 == NULL ) {
304 fprintf( stderr,
305 "%s: failed to malloc for %d bytes (createNewEvent: struct gdcEventDescriptorStruct)",
306 myName, (int)sizeof( *p ) );
307 perror( "" );
308 exit( 1 );
309 }
310 p->loaded = FALSE;
311 p->next = NULL;
312 p->head = p->tail = NULL;
313 currGdc = p;
314 if ( eventsHead == NULL ) {
315 eventsHead = eventsTail = p;
316 } else {
317 struct gdcEventDescriptorStruct *q =
318 (struct gdcEventDescriptorStruct *)eventsTail;
a8bf0b36 319
f3a800f8 320 q->next = p;
321 eventsTail = p;
322 }
323 }
324} /* End of createNewEvent */
325
326void createNewLdcEvent() {
327 struct gdcEventDescriptorStruct *gdcDesc;
328 struct ldcEventDescriptorStruct *p;
329
330 if ( (p = (struct ldcEventDescriptorStruct *)malloc( sizeof( *p ) ))
331 == NULL ) {
332 fprintf( stderr,
333 "%s: failed to malloc for %d bytes (createNewLdcEvent: struct ldcEventDescriptorStruct)",
334 myName, (int)sizeof( *p ) );
335 perror( "" );
336 exit( 1 );
337 }
338 p->id = currLdcId;
339 p->head = p->tail = NULL;
340 p->next = NULL;
341 gdcDesc = (struct gdcEventDescriptorStruct *)eventsTail;
342 if ( gdcDesc->head == NULL ) {
343 gdcDesc->head = gdcDesc->tail = p;
344 } else {
345 gdcDesc->tail->next = p;
346 gdcDesc->tail = p;
347 }
348 currLdc = p;
349} /* End of createNewLdcEvent */
350
351void loadBuffer( struct payloadDescriptorStruct * const payload ) {
352 FILE *f;
353 int bytesRead;
a8bf0b36 354
f3a800f8 355 if ( (f = fopen( payload->fileName, "r" )) == NULL ) {
356 fprintf( stderr,
357 "%s: line:%d payload file \"%s\" not found or not readable, errno:%d. ",
358 myName,
359 lineNo,
360 payload->fileName,
361 errno );
362 perror( "System-dependent error " );
363 exit( 1 );
364 }
365 if ( (payload->data = malloc( payload->size )) == NULL ) {
366 fprintf( stderr,
367 "%s: line:%d Failed to malloc for payload file \"%s\" size:%d errno:%d ",
368 myName,
369 lineNo,
370 payload->fileName,
371 payload->size,
372 errno );
373 perror( "System-dependent status " );
374 exit( 1 );
375 }
376 if ( (bytesRead = fread( payload->data, payload->fileSize, 1, f )) != 1 ) {
377 fprintf( stderr,
378 "%s: line:%d Failed to read payload file \"%s\" size:%d requested:1 got:%d feof:%s ferror:%s errno:%d ",
379 myName,
380 lineNo,
381 payload->fileName,
382 payload->size,
383 bytesRead,
384 feof(f) ? "TRUE" : "false",
385 ferror(f) ? "TRUE" : "false",
386 errno );
387 perror( "System-dependent status " );
388 exit( 1 );
389 }
390 fclose(f);
391 if ( payload->size != payload->fileSize ) {
392 memset( (char *)payload->data + payload->fileSize,
393 0,
394 payload->size - payload->fileSize );
395 }
396} /* End of loadBuffer */
397
398void unloadBuffer( struct payloadDescriptorStruct * const payload ) {
399 if ( payload->data != NULL ) {
400 free( payload->data );
401 payload->data = NULL;
402 }
403} /* End of unloadBuffer */
404
405void unloadAllBuffers() {
406 struct payloadDescriptorStruct *payload;
407
408 for ( payload = payloadsHead; payload != NULL; payload = payload->next ) {
409 unloadBuffer( payload );
410 }
411} /* End of unloadAllBuffers */
412
413void loadPayload( const char *fileName ) {
414 struct payloadDescriptorStruct *payload;
415
416 for ( payload = payloadsHead; payload != NULL; payload = payload->next ) {
417 if ( strcmp( fileName, payload->fileName ) == 0 )
418 break;
419 }
420 if ( payload == NULL ) {
421 FILE *f;
a8bf0b36 422
f3a800f8 423 if ( (payload = (struct payloadDescriptorStruct *)malloc( sizeof( *payload ) ))
424 == NULL ) {
425 fprintf( stderr,
426 "%s: failed to malloc for %d bytes (loadPayload/payloadDescriptorStruct)\n",
427 myName,
428 (int)sizeof( *payload ) );
429 exit( 1 );
430 }
431 if ( (payload->fileName = strdup( fileName )) == NULL ) {
432 fprintf( stderr,
433 "%s: failed to duplicate string \"%s\" (loadPaload/fileName)\n",
434 myName,
435 fileName );
436 exit( 1 );
437 }
438 if ( (f = fopen( fileName, "r" )) == NULL ) {
439 fprintf( stderr,
440 "%s: line:%d payload file \"%s\" not found or not readable, errno:%d. ",
441 myName,
442 lineNo,
443 fileName,
444 errno );
445 perror( "System-dependent error " );
446 exit( 1 );
447 }
448 if ( fseek( f, 0L, SEEK_END ) != 0 ) {
449 fprintf( stderr,
450 "%s: line:%d Failed to seek payload file \"%s\" errno:%d ",
451 myName,
452 lineNo,
453 fileName,
454 errno );
455 perror( "System-dependent error " );
456 exit( 1 );
457 }
458 if ( (payload->size = ftell( f )) <= 0 ) {
459 fprintf( stderr,
460 "%s: line:%d Failed to get file \"%s\" size size:%d errno:%d ",
461 myName,
462 lineNo,
463 fileName,
464 payload->size,
465 errno );
466 perror( "System-dependent status " );
467 exit( 1 );
468 }
469 payload->fileSize = payload->size;
470 while ( (payload->size & 3) != 0 ) payload->size++;
471 fclose( f );
a8bf0b36 472
f3a800f8 473 if ( bufferData ) {
474 loadBuffer( payload );
475 } else {
476 payload->data = NULL;
477 }
478
479 payload->next = NULL;
480 if ( payloadsHead == NULL ) {
481 payloadsHead = payloadsTail = payload;
482 } else {
483 payloadsTail->next = payload;
484 payloadsTail = payload;
485 }
486 DBG_VERBOSE {
487 int b, n;
a8bf0b36 488
f3a800f8 489 printf( "%d) Payload \"%s\" loaded at %p\n",
490 lineNo,
491 fileName,
492 payload );
493 if ( bufferData ) {
494 if ( handleCDH ) {
495 struct commonDataHeaderStruct *cdh =
496 (struct commonDataHeaderStruct *)payload->data;
497
498 printf( " CDH: blockLenght:%d=0x%08x ",
499 cdh->cdhBlockLength, cdh->cdhBlockLength );
500 if ( cdh->cdhBlockLength < sizeof( *cdh ) ) {
73d4128a 501 printf( "TOO SMALL (minimum:%ld=0x%08lx)\n",
a8bf0b36 502 (unsigned long)sizeof( *cdh ),
503 (unsigned long)sizeof( *cdh ) );
f3a800f8 504 } else {
505 printf( "version:%d=0x%x ", cdh->cdhVersion, cdh->cdhVersion );
506 if ( cdh->cdhVersion != CDH_VERSION ) {
507 printf( "EXPECTED:%d=%x (decoding may be inaccurate) ",
508 CDH_VERSION, CDH_VERSION );
509 }
510 }
511 printf( "L1TriggerMessage:0x%x", cdh->cdhL1TriggerMessage );
512 if ( cdh->cdhL1TriggerMessage != 0 ) {
513 for ( b = 0, n = 0; b != 10; b++ ) {
514 if ( (cdh->cdhL1TriggerMessage & (1<<b)) != 0 ) {
515 if ( n++ != 0 )printf( "+" );
516 switch (b) {
517 case 0: printf( "L1SwC" ); break;
518 case 1: printf( "ESR" ); break;
519 case 2: printf( "RoC1" ); break;
520 case 3: printf( "RoC2" ); break;
521 case 4: printf( "RoC3" ); break;
522 case 5: printf( "RoC4" ); break;
523 case 6: printf( "ClT" ); break;
524 default: printf( "spare %d", b+14 );
525 }
526 }
527 }
528 printf( ">" );
529 }
530 printf( " " );
531 if ( cdh->cdhMBZ0 != 0 )
532 printf( "MBZ0:0x%x ",
533 cdh->cdhMBZ0 );
534 printf( "\n" );
535
536 printf( " " );
537 printf( "EventId2(orbit):%d=0x%x ",
538 cdh->cdhEventId2, cdh->cdhEventId2 );
539 printf( "EventId1(bunchCrossing):%d=0x%x ",
540 cdh->cdhEventId1, cdh->cdhEventId1 );
541 printf( "\n" );
542
543 printf( " " );
544 if ( cdh->cdhMBZ1 != 0 )
545 printf( "MBZ1:0x%x ",
546 cdh->cdhMBZ1 );
547 printf( "BlockAttributes:0x%x",
548 cdh->cdhBlockAttributes );
549 if ( cdh->cdhBlockAttributes != 0 ) {
550 printf( "=<" );
551 for ( b = 0, n = 0; b != 8; b++ ) {
552 if ( (cdh->cdhBlockAttributes & (1<<b)) != 0 ) {
553 if ( n++ != 0 )
554 printf( "+" );
555 printf( "%d", b );
556 }
557 }
558 printf( ">" );
559 }
560 printf( " " );
561 printf( "ParticipatingSubDetectors:0x%x ",
562 cdh->cdhParticipatingSubDetectors );
563 printf( "\n" );
564 printf( " " );
565
566 if ( cdh->cdhMBZ2 != 0 )
567 printf( "MBZ2:0x%x ",
568 cdh->cdhMBZ2 );
569 printf( "Status/Error:0x%x", cdh->cdhStatusErrorBits );
570 if ( cdh->cdhStatusErrorBits != 0 ) {
571 printf( "=<" );
572 for ( b = 0,n = 0; b != 16; b++ ) {
573 if ( (cdh->cdhStatusErrorBits & (1<<b)) != 0 ) {
574 if ( n++ != 0 ) printf( "+" );
575 switch (b) {
576 case 0: printf( "TriggerOverLapError" ); break;
577 case 1: printf( "TriggerMissingError" ); break;
578 case 2: printf( "DataParityError" ); break;
579 case 3: printf( "ControlParityError" ); break;
580 case 4: printf( "TriggerInformationUnavailable" ); break;
581 case 5: printf( "FEEError" ); break;
582 case 6: printf( "HLTDecision" ); break;
583 case 7: printf( "HLTPayload" ); break;
584 case 8: printf( "DDGPayload" ); break;
585 default: printf( "spare %d", b );
586 }
587 }
588 }
589 printf( ">" );
590 }
591 printf( " " );
592 printf( "MiniEventId(bunchCrossing):%d=0x%x ",
593 cdh->cdhMiniEventId, cdh->cdhMiniEventId );
594 printf( "\n" );
595
596 printf( " " );
597 printf( "Trigger classes: 0x(%05x-%08x)",
598 cdh->cdhTriggerClassesHigh,
599 cdh->cdhTriggerClassesLow );
600 if ( cdh->cdhTriggerClassesHigh != 0
601 || cdh->cdhTriggerClassesLow != 0 ) {
602 printf( "=<" );
603 for ( b=0, n=0; b != 32; b++ ) {
604 if ( (cdh->cdhTriggerClassesLow & (1<<b)) != 0 ) {
605 if ( n++ != 0 ) printf( "+" );
606 printf( "%d", b );
607 }
608 }
609 for ( b=0; b != 18; b++ ) {
610 if ( (cdh->cdhTriggerClassesHigh & (1<<b)) != 0 ) {
611 if ( n++ != 0 ) printf( "+" );
612 printf( "%d", b+32 );
613 }
614 }
615 printf( ">" );
616 }
617 printf( "\n" );
618
619 printf( " " );
620 if ( cdh->cdhMBZ3 != 0 ) {
621 printf( "MBZ3:0x%x ",
622 cdh->cdhMBZ3 );
623 }
624 printf( "ROI:0x(%08x-%01x)", cdh->cdhRoiHigh, cdh->cdhRoiLow );
625 if ( cdh->cdhRoiHigh != 0
626 || cdh->cdhRoiLow != 0 ) {
627 printf( "=<" );
628 for ( b=0, n=0; b != 5; b++ ) {
629 if ( (cdh->cdhRoiLow & (1<<b)) != 0 ) {
630 if ( n++ != 0 ) printf( "+" );
631 printf( "%d", b );
632 }
633 }
634 for ( b=0; b != 32; b++ ) {
635 if ( (cdh->cdhRoiHigh & (1<<b)) != 0 ) {
636 if ( n++ != 0 ) printf( "+" );
637 printf( "%d", b+4 );
638 }
639 }
640 printf( ">" );
641 }
642 printf( "\n" );
643 }
644 }
645 }
646 } else {
647 DBG_VERBOSE
648 printf( "%d) Payload \"%s\" already loaded at %p\n",
649 lineNo,
650 fileName,
651 payload );
652 }
653
654 currEvent->payload = payload;
655} /* End of loadPayload */
656
657void parseEquipment( char * const line ) {
658 struct equipmentEventDescriptorStruct *equipment;
659 int payloadFound = FALSE;
660 char *p;
661 char *keyword;
662
663 if ( (equipment =
664 (struct equipmentEventDescriptorStruct *)malloc( sizeof( *equipment ) )) == NULL ) {
665 fprintf( stderr,
666 "%s: filed to malloc for %d bytes (parseEquipment/equipmentEventDescriptorStruct) errno:%d ",
667 myName,
668 (int)sizeof( *equipment ),
669 errno );
670 perror( "" );
671 exit( 1 );
672 }
673 currEvent = equipment;
674
675 p = line;
676 while ( (keyword = strtok_r( p, " \t", &p )) != NULL ) {
677 DBG_VERBOSE printf( "%d) Equipment - Keyword:\"%s\"\n",
678 lineNo,
679 keyword );
680 if ( strcasecmp( "id", keyword ) == 0 ) {
681 char *idNum;
682
683 if ( (idNum = strtok_r( p, " \t", &p )) == NULL ) {
684 fprintf( stderr,
685 "%s: line:%d EQUIPMENT declaration, ID needed",
686 myName,
687 lineNo );
688 exit( 1 );
689 }
690 if ( sscanf( idNum, "%d", &currEquipmentId ) != 1 ) {
691 fprintf( stderr,
692 "%s: line:%d EQUIPMENT declaration, numeric ID needed (%s)",
693 myName,
694 lineNo,
695 idNum );
696 exit( 1 );
697 }
698 DBG_VERBOSE printf( "%d) EQUIPMENT - ID:%d\n",
699 lineNo,
700 currEquipmentId );
701 } else if ( strncasecmp( "pay", keyword, 3 ) == 0 ) {
702 char *fileName;
703
704 if ( (fileName = strtok_r( p, " \t", &p )) == NULL ) {
705 fprintf( stderr,
706 "%s line:%d Payload without filename found\n",
707 myName,
708 lineNo );
709 exit( 1 );
710 }
711 DBG_VERBOSE printf( "%d) Equipment - Payload:\"%s\"\n",
712 lineNo,
713 fileName );
714 if ( payloadFound ) {
715 fprintf( stderr,
716 "%s line:%d Payload with multiple filenames found\n",
717 myName,
718 lineNo );
719 exit( 1 );
720 }
721 loadPayload( fileName );
722 payloadFound = TRUE;
723 } else {
724 fprintf( stderr,
725 "%s: line:%d Equipment declaration, unknown keyword \"%s\"\n",
726 myName,
727 lineNo,
728 keyword );
729 exit( 1 );
730 }
731 }
732 if ( !payloadFound ) {
733 fprintf( stderr,
734 "%s: line:%d Equipment without payload found\n",
735 myName,
736 lineNo );
737 exit( 1 );
738 }
739
740 equipment->id = currEquipmentId;
741 equipment->next = NULL;
742 if ( currLdc->head == NULL ) {
743 currLdc->head = currLdc->tail = equipment;
744 } else {
745 currLdc->tail->next = equipment;
746 currLdc->tail = equipment;
747 }
748} /* End of parseEquipment */
749
750void parseGdc( char * const line ) {
751 char *p;
752 char *keyword;
a8bf0b36 753
f3a800f8 754 p = line;
755 while ( (keyword = strtok_r( p, " \t", &p )) != NULL ) {
756 if ( strcasecmp( "id", keyword ) == 0 ) {
757 char *idNum;
758
759 if ( (idNum = strtok_r( p, " \t", &p )) == NULL ) {
760 fprintf( stderr,
761 "%s: line:%d GDC declaration, ID needed",
762 myName,
763 lineNo );
764 exit( 1 );
765 }
766 if ( sscanf( idNum, "%d", &currGdcId ) != 1 ) {
767 fprintf( stderr,
768 "%s: line:%d GDC declaration, numeric ID needed (%s)",
769 myName,
770 lineNo,
771 idNum );
772 exit( 1 );
773 }
774 DBG_VERBOSE printf( "%d) GDC - ID:%d\n",
775 lineNo,
776 currGdcId );
777 } else {
778 fprintf( stderr,
779 "%s: line:%d GDC declaration, unknown keyword \"%s\"\n",
780 myName,
781 lineNo,
782 keyword );
783 exit( 1 );
784 }
785 }
786} /* End of parseGdc */
787
788void parseLdc( char * const line ) {
789 char *p;
790 char *keyword;
a8bf0b36 791
f3a800f8 792 p = line;
793 while ( (keyword = strtok_r( p, " \t", &p )) != NULL ) {
794 if ( strcasecmp( "id", keyword ) == 0 ) {
795 char *idNum;
796
797 if ( (idNum = strtok_r( p, " \t", &p )) == NULL ) {
798 fprintf( stderr,
799 "%s: line:%d LDC declaration, ID needed",
800 myName,
801 lineNo );
802 exit( 1 );
803 }
804 if ( sscanf( idNum, "%d", &currLdcId ) != 1 ) {
805 fprintf( stderr,
806 "%s: line:%d LDC declaration, numeric ID needed (%s)",
807 myName,
808 lineNo,
809 idNum );
810 exit( 1 );
811 }
812 DBG_VERBOSE printf( "%d) LDC - ID:%d\n",
813 lineNo,
814 currLdcId );
815 } else {
816 fprintf( stderr,
817 "%s: line:%d LDC declaration, unknown keyword \"%s\"\n",
818 myName,
819 lineNo,
820 keyword );
821 exit( 1 );
822 }
823 }
824} /* End of parseLdc */
825
826void parseRules() {
827 char line[ 1025 ];
828
829 currLdcId = HOST_ID_MIN;
830 currGdcId = HOST_ID_MIN;
831
832 for ( lineNo = 1; !feof( stdin ); lineNo++ ) {
833 getLine( line, sizeof(line) );
834 if ( strlen(line) != 0 ) {
835 char *p;
836 char *keyword;
837
838 if ( (keyword = strtok_r( line, " \t", &p )) != NULL ) {
839 DBG_VERBOSE printf( "%d) Keyword:\"%s\"\n", lineNo, keyword );
840 if ( strcasecmp( "gdc", keyword ) == 0 ) {
841 if ( workingAs != gdc && workingAs != unknown ) {
842 fprintf( stderr,
843 "%s: line:%d GDC found when working in non-GDC mode (e.g. as a LDC)\n",
844 myName, lineNo );
845 exit( 1 );
846 }
847 workingAs = gdc;
848 parseGdc( p );
849 createNewEvent();
850 currLdcId = HOST_ID_MIN;
851 currLdc = NULL;
852 currEquipmentId = 0;
853 } else if ( strcasecmp( "ldc", keyword ) == 0 ) {
854 if ( workingAs != gdc && workingAs != ldc && workingAs != unknown ) {
855 fprintf( stderr,
856 "%s: line:%d LDC found when working in non-LDC/GDC mode\n",
857 myName, lineNo );
858 exit( 1 );
859 }
860 if ( workingAs == unknown ) workingAs = ldc;
861 parseLdc( p );
862 if ( workingAs == ldc ) {
863 createNewEvent();
864 currEquipmentId = 0;
865 } else {
866 createNewLdcEvent();
867 handleLdc( currLdcId );
868 currLdcId++;
869 }
870 currEvent = NULL;
871 } else if ( strncasecmp( "equ", keyword, 3 ) == 0 ) {
872 if ( workingAs == unknown
873 || (workingAs == ldc && currLdc == NULL )
874 || (workingAs == gdc && currGdc == NULL ) ) {
875 fprintf( stderr,
876 "%s: line:%d Unexpected EQUIPMENT declaration (LDC or GDC needed first)\n",
877 myName,
878 lineNo );
879 exit( 1 );
880 }
881 parseEquipment( p );
882 currEquipmentId++;
883 } else {
884 fprintf( stderr,
885 "%s: line:%d Parse error in \"%s\" unknown keyword\n",
886 myName,
887 lineNo,
888 keyword );
889 exit( 1 );
890 }
891 }
892 }
893 } while ( !feof( stdin ) );
894 lineNo -= 2;
895
896 DBG_VERBOSE {
897 printf( "End of parse: %d line%s found\n",
898 lineNo,
899 lineNo != 1 ? "s" : "" );
900 printf( "Working as %s\n",
901 workingAs == gdc ? "GDC" :
902 workingAs == ldc ? "LDC" :
903 "UNKNOWN" );
904 if ( workingAs == gdc ) {
905 struct ldcDescriptorStruct *ldc;
a8bf0b36 906
f3a800f8 907 printf( "LDCs (%d):", numOfLdcs );
908 for ( ldc = ldcsHead; ldc != NULL; ldc = ldc->next ) {
909 printf( " %d", ldc->id );
910 }
911 printf( "\n" );
912 }
913 dumpEvents();
914 }
915
916 if ( workingAs == ldc ) {
917 assert( ldcsHead == ldcsTail );
918 assert( ldcsTail == NULL );
919 }
920
921 if ( workingAs == gdc ) {
922 struct ldcDescriptorStruct *ldc;
a8bf0b36 923
f3a800f8 924 assert( ldcsHead != NULL );
925 assert( ldcsTail != NULL );
926 assert( ldcsTail->next == NULL );
927 for ( ldc = ldcsHead; ldc->next != NULL; ldc = ldc->next );
928 assert ( ldc == ldcsTail );
929 }
930
931 if ( workingAs == unknown ) {
932 DBG_VERBOSE printf( "Empty configuration: nothing to do!\n" );
933 exit( 0 );
934 }
935
936 assert( (eventsHead == NULL && eventsTail == NULL)
937 || (eventsHead != NULL && eventsTail != NULL) );
938} /* End of parseRules */
939
940void loadTimestamp( struct eventHeaderStruct * const ev ) {
941 time_t t;
a8bf0b36 942
f3a800f8 943 if ( time( &t ) == (time_t)-1 ) {
944 fprintf( stderr,
945 "%s: failed to get system time errno:%d (%s)\n",
946 myName, errno, strerror( errno ) );
947 exit( 1 );
948 }
949 ev->eventTimestamp = (eventTimestampType)t;
950} /* End of loadTimestamp */
951
952void initEvent( struct eventHeaderStruct * const ev ) {
953 memset( ev, 0, sizeof( *ev ) );
954
955 ev->eventMagic = EVENT_MAGIC_NUMBER;
956 ev->eventHeadSize = EVENT_HEAD_BASE_SIZE;
957 ev->eventVersion = EVENT_CURRENT_VERSION;
958 ev->eventRunNb = currRunNb;
959 ZERO_EVENT_ID( ev->eventId );
960 ZERO_TRIGGER_PATTERN( ev->eventTriggerPattern );
961 ZERO_DETECTOR_PATTERN( ev->eventDetectorPattern );
962 RESET_ATTRIBUTES( ev->eventTypeAttribute );
963 if ( workingMode == collider )
964 SET_SYSTEM_ATTRIBUTE( ev->eventTypeAttribute, ATTR_ORBIT_BC );
965 ev->eventLdcId = VOID_ID;
966 ev->eventGdcId = VOID_ID;
967 loadTimestamp( ev );
968} /* End of initEvent */
969
f6508a2e 970int Swap(int x)
971{
972 // Swap the endianess of the integer value 'x'
973
974 return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
975 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
976}
977
f3a800f8 978void outputEvent( const void * const ev,
979 const int size ) {
980 int done;
981
982 DBG_VERBOSE {
f6508a2e 983 const long32 * const v = (long32 *)ev;
f3a800f8 984 printf( "Writing %d bytes @ %p (%d)\n", size, ev, *v );
985 }
a8bf0b36 986
f6508a2e 987 // .............................Test endianess..............................
988 int temp = 1;
989 char* ptemp = (char*) &temp;
990
991 if (ptemp[0]!=1) { // Mac platform: ptemp != 1..............................................................................
992 int bufSize= size; if (bufSize > (int) sizeof(eventHeaderStruct)) { bufSize = sizeof(eventHeaderStruct); }
993 char* evTemp = (char*) malloc (bufSize);
994 memcpy(evTemp, ev, bufSize);
995
996 if ((bufSize % sizeof(int)) != 0) {
997 fprintf( stderr, "%s: size of the input buffer ev is not multiple of 4 (size = %d)\n", myName, bufSize);
998 exit( 1 );
999 }
1000 else {
1001 // Invert header to evTemp.....................................................
1002 int* buf = (int*) evTemp;
1003 for (int i=0; i < (int) (bufSize / sizeof(int)); i++, buf++) {
1004 int value = Swap(*buf);
1005 memcpy(evTemp + (i * sizeof(int)), &value, sizeof(int));
1006 }
1007
1008 // Write inverted header to file...............................................
1009 if ((done = fwrite( evTemp, bufSize, 1, outF )) != 1 ) {
1010 fprintf( stderr, "%s: failed to write inverted header. event size:%d bytes, errno:%d (%s)\n", myName, size, errno, strerror( errno ) );
1011 exit( 1 );
1012 }
1013
1014 if (size > bufSize) { // Still theraw-data payload to write (but not inverted, since it is inverted eariler).............
1015 if ((done = fwrite( (char*)ev + bufSize, size - bufSize, 1, outF )) != 1 ) {
1016 fprintf( stderr, "%s: failed to write additional event size:%d bytes, errno:%d (%s)\n", myName, size, errno, strerror( errno ) );
1017 exit( 1 );
1018 }
1019 }
1020 }
1021 free(evTemp);
1022 }
1023 else { // Intel platform: ptemp == 1............................................................................
1024 if ((done = fwrite( ev, size, 1, outF )) != 1 ) {
1025 fprintf( stderr, "%s: failed to write event size:%d bytes, errno:%d (%s)\n", myName, size, errno, strerror( errno ) );
1026 exit( 1 );
1027 }
f3a800f8 1028 }
1029} /* End of outputEvent */
1030
1031void createSorAndEor( const int sor ) {
1032 unsigned char event[ 1000 ];
1033 struct eventHeaderStruct *ev;
1034 struct eventHeaderStruct sev;
1035
1036 assert( workingAs == ldc || workingAs == gdc );
a8bf0b36 1037
f3a800f8 1038 if ( !createSorEor ) return;
1039 ev = (struct eventHeaderStruct *)event;
1040 initEvent( ev );
1041 ev->eventSize = sizeof( event );
1042 ev->eventType = sor ? START_OF_RUN : END_OF_RUN;
1043 if ( workingMode == fixedTarget )
1044 LOAD_RAW_EVENT_ID( ev->eventId, 0, 0, 0 );
1045 else
1046 LOAD_EVENT_ID( ev->eventId, 0, 0, 0 );
1047 SET_SYSTEM_ATTRIBUTE( ev->eventTypeAttribute, ATTR_P_START );
1048
1049 if ( workingAs == ldc ) {
1050 currLdc = (struct ldcEventDescriptorStruct *)eventsHead;
1051 }
1052 if ( workingAs == gdc ) {
1053 initEvent( &sev );
1054 sev.eventGdcId = currGdcId;
1055 ev->eventGdcId = currGdcId;
1056 currGdc = (struct gdcEventDescriptorStruct *)eventsHead;
1057 currLdc = currGdc->head;
1058 }
1059 ev->eventLdcId = currLdc->id;
a8bf0b36 1060
f3a800f8 1061 if ( workingAs == ldc ) {
1062 loadTimestamp( ev );
1063 outputEvent( ev, ev->eventSize );
1064 }
1065 if ( workingAs == gdc ) {
1066 struct ldcDescriptorStruct *ldc;
1067
1068 loadTimestamp( ev );
a8bf0b36 1069
f3a800f8 1070 sev.eventSize = sizeof( sev ) + numOfLdcs * ev->eventSize;
1071 sev.eventType = sor ? START_OF_RUN : END_OF_RUN ;
1072 COPY_EVENT_ID( ev->eventId, sev.eventId );
1073 COPY_SYSTEM_ATTRIBUTES( ev->eventTypeAttribute, sev.eventTypeAttribute );
1074 SET_SYSTEM_ATTRIBUTE( sev.eventTypeAttribute, ATTR_SUPER_EVENT );
1075 loadTimestamp( &sev );
1076 outputEvent( &sev, sizeof( sev ) );
1077
1078 ev->eventGdcId = currGdcId;
1079 for ( ldc = ldcsHead; ldc != NULL; ldc = ldc->next ) {
1080 ev->eventLdcId = ldc->id;
1081 outputEvent( ev, ev->eventSize );
1082 }
1083 }
a8bf0b36 1084
f3a800f8 1085 ADD_EVENT_ID( ev->eventId, oneEventDelta );
1086 ev->eventSize = ev->eventSize / 2;
1087 ev->eventType = sor ? START_OF_RUN_FILES : END_OF_RUN_FILES;
1088 CLEAR_SYSTEM_ATTRIBUTE( ev->eventTypeAttribute, ATTR_P_START );
1089 if ( workingAs == ldc ) {
1090 loadTimestamp( ev );
1091 outputEvent( ev, ev->eventSize );
1092 }
1093 if ( workingAs == gdc ) {
1094 struct ldcDescriptorStruct *ldc;
1095
1096 loadTimestamp( ev );
a8bf0b36 1097
f3a800f8 1098 sev.eventSize = ev->eventSize;
1099 sev.eventType = sor ? START_OF_RUN_FILES : END_OF_RUN_FILES;
1100 COPY_EVENT_ID( ev->eventId, sev.eventId );
1101 COPY_SYSTEM_ATTRIBUTES( ev->eventTypeAttribute, sev.eventTypeAttribute );
1102 CLEAR_SYSTEM_ATTRIBUTE( sev.eventTypeAttribute, ATTR_SUPER_EVENT );
1103 outputEvent( &sev, sizeof( sev ) );
1104 outputEvent( ev, ev->eventSize - sizeof( sev ) );
1105
1106 sev.eventSize = sizeof( sev ) + ev->eventSize;
1107 sev.eventType = sor ? START_OF_RUN_FILES : END_OF_RUN_FILES;
1108 COPY_EVENT_ID( ev->eventId, sev.eventId );
1109 COPY_SYSTEM_ATTRIBUTES( ev->eventTypeAttribute, sev.eventTypeAttribute );
1110 SET_SYSTEM_ATTRIBUTE( sev.eventTypeAttribute, ATTR_SUPER_EVENT );
a8bf0b36 1111
f3a800f8 1112 loadTimestamp( &sev );
1113
1114 ev->eventGdcId = currGdcId;
1115 for ( ldc = ldcsHead; ldc != NULL; ldc = ldc->next ) {
1116 loadTimestamp( &sev );
1117 outputEvent( &sev, sizeof( sev ) );
1118 ev->eventLdcId = ldc->id;
1119 outputEvent( ev, ev->eventSize );
1120 }
1121 }
1122
1123 ADD_EVENT_ID( ev->eventId, oneEventDelta );
1124 ev->eventSize = sizeof( *ev );
1125 ev->eventType = sor ? START_OF_RUN : END_OF_RUN;
1126 SET_SYSTEM_ATTRIBUTE( ev->eventTypeAttribute, ATTR_P_END );
1127 if ( workingAs == ldc ) {
1128 loadTimestamp( ev );
1129 outputEvent( ev, ev->eventSize );
1130 }
1131 if ( workingAs == gdc ) {
1132 struct ldcDescriptorStruct *ldc;
1133
1134 loadTimestamp( ev );
1135
1136 sev.eventSize = sizeof( sev ) + numOfLdcs * ev->eventSize;
1137 sev.eventType = sor ? START_OF_RUN : END_OF_RUN;
1138 COPY_EVENT_ID( ev->eventId, sev.eventId );
1139 COPY_SYSTEM_ATTRIBUTES( ev->eventTypeAttribute, sev.eventTypeAttribute );
1140 SET_SYSTEM_ATTRIBUTE( sev.eventTypeAttribute, ATTR_SUPER_EVENT );
1141 loadTimestamp( &sev );
a8bf0b36 1142
f3a800f8 1143 outputEvent( &sev, sizeof( sev ) );
1144
1145 for ( ldc = ldcsHead; ldc != NULL; ldc = ldc->next ) {
1146 ev->eventLdcId = ldc->id;
1147 outputEvent( ev, ev->eventSize );
1148 }
1149 }
1150} /* End of createSorEor */
1151
1152void createSor() {
1153 createSorAndEor( TRUE );
1154} /* End of createSor */
1155
1156void createEor() {
1157 createSorAndEor( FALSE );
1158} /* End of createEor */
1159
1160void loadCdh( struct commonDataHeaderStruct * const cdh,
1161 eventIdType * const eventId ) {
1162 if ( !handleCDH ) return;
1163
1164 if ( gotAliceTrigger ) {
1165 cdh->cdhEventId1 = EVENT_ID_GET_BUNCH_CROSSING( *eventId );
1166 cdh->cdhEventId2 = EVENT_ID_GET_ORBIT( *eventId );
1167 } else {
1168 cdh->cdhEventId1 = 0;
1169 cdh->cdhEventId2 = EVENT_ID_GET_NB_IN_RUN( *eventId );
1170 }
1171 cdh->cdhMiniEventId = cdh->cdhEventId1;
1172}
1173void decodeCDH( struct ldcEventDescriptorStruct * const ldc,
1174 const struct payloadDescriptorStruct * const payloadDesc );
1175
1176void createEvent( void ) {
1177 assert( workingAs == ldc || workingAs == gdc );
1178
1179 /* Step 1: load all buffers (if needed) and compose the GDC/LDC headers */
1180 if ( workingAs == gdc ) {
1181 struct ldcEventDescriptorStruct *ldc;
a8bf0b36 1182
f3a800f8 1183 for( ldc = currGdc->head; ldc != NULL; ldc = ldc->next ) {
1184 COPY_EVENT_ID( currEventId, ldc->header.eventId );
1185 loadTimestamp( &ldc->header );
1186 }
1187 COPY_EVENT_ID( currEventId, currGdc->header.eventId );
1188 loadTimestamp( &currGdc->header );
a8bf0b36 1189
f3a800f8 1190 for( ldc = currGdc->head; ldc != NULL; ldc = ldc->next ) {
1191 struct equipmentEventDescriptorStruct *eq;
1192 int n;
1193
1194 for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
1195 if ( !bufferData ) {
1196 loadBuffer( eq->payload );
1197 if ( !currGdc->loaded ) decodeCDH( ldc, eq->payload );
1198 }
1199 loadCdh( (struct commonDataHeaderStruct*)eq->payload->data,
1200 &currEventId );
1201 }
1202
1203 if ( !currGdc->loaded ) {
1204 for ( n = 0; n != EVENT_TRIGGER_PATTERN_WORDS; n++ )
1205 currGdc->header.eventTriggerPattern[n] |= ldc->header.eventTriggerPattern[n];
1206 for ( n = 0; n != EVENT_DETECTOR_PATTERN_WORDS; n++ )
1207 currGdc->header.eventDetectorPattern[n] |= ldc->header.eventDetectorPattern[n];
1208 for ( n = 0; n != ALL_ATTRIBUTE_WORDS; n++ )
1209 currGdc->header.eventTypeAttribute[n] |= ldc->header.eventTypeAttribute[n];
1210 currGdc->loaded = TRUE;
1211 }
1212 }
1213 } else if ( workingAs == ldc ) {
1214 struct equipmentEventDescriptorStruct *eq;
1215
1216 COPY_EVENT_ID( currEventId, currLdc->header.eventId );
1217 loadTimestamp( &currLdc->header );
1218
1219 for ( eq = currLdc->head; eq != NULL; eq = eq->next ) {
1220 if ( !bufferData ) {
1221 loadBuffer( eq->payload );
1222 if ( !currLdc->loaded ) decodeCDH( currLdc, eq->payload );
1223 }
1224 loadCdh( (struct commonDataHeaderStruct*)eq->payload->data,
1225 &currEventId );
1226 currLdc->loaded = TRUE;
1227 }
1228 }
1229 ADD_EVENT_ID( currEventId, oneEventDelta );
1230
1231 /* Step 2: output the event */
1232 if ( workingAs == gdc ) {
1233 struct ldcEventDescriptorStruct *ldc;
a8bf0b36 1234
f3a800f8 1235 outputEvent( &currGdc->header, sizeof( currGdc->header ) );
1236
1237 for( ldc = currGdc->head; ldc != NULL; ldc = ldc->next ) {
1238 struct equipmentEventDescriptorStruct *eq;
a8bf0b36 1239
f3a800f8 1240 outputEvent( &ldc->header, sizeof( ldc->header ) );
1241
1242 for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
1243 outputEvent( &eq->header, sizeof( eq->header ) );
1244 outputEvent( eq->payload->data, eq->payload->size );
1245 if ( !bufferData ) unloadBuffer( eq->payload );
1246 }
1247 }
1248 if ( (currGdc = currGdc->next) == NULL )
1249 currGdc = (struct gdcEventDescriptorStruct *)eventsHead;
1250 } else if ( workingAs == ldc ) {
1251 struct equipmentEventDescriptorStruct *eq;
a8bf0b36 1252
f3a800f8 1253 outputEvent( &currLdc->header, sizeof( currLdc->header ) );
a8bf0b36 1254
f3a800f8 1255 for ( eq = currLdc->head; eq != NULL; eq = eq->next ) {
1256 outputEvent( &eq->header, sizeof( eq->header ) );
1257 outputEvent( eq->payload->data, eq->payload->size );
1258 if ( !bufferData ) unloadBuffer( eq->payload );
1259 }
1260 if ( (currLdc = currLdc->next) == NULL )
1261 currLdc = (struct ldcEventDescriptorStruct *)eventsHead;
1262 }
1263} /* End of createEvent */
1264
1265void createEvents() {
1266 int eventNum = 0;
1267
1268 currGdc = (struct gdcEventDescriptorStruct *)eventsHead;
1269 currLdc = (struct ldcEventDescriptorStruct *)eventsHead;
1270 currEvent = NULL;
1271
1272 createSor();
1273 for ( eventNum = 0;
1274 eventNum != numOfEvents && numOfEvents != 0;
1275 eventNum++ ) {
1276 createEvent();
1277 }
1278 createEor();
1279} /* End of createEvents */
1280
1281int usage() {
1282 fprintf( stderr,
1283 "Usage: %s [-?][-d][-i definitionFile][-o outputFile][-# numOfEvents][-s][-F|-C]\n\
1284 -? This text\n\
1285 -v Print version ID and exit\n\
1286 -d Enable debug (repeat for more verbosity)\n\
1287 -i definitionFile File with the description of the events to create (default: stdin)\n\
1288 -o outputFile File used to store events (default: stdout)\n\
1289 -# numOfEvents Number of events to generate (default: 1 event)\n\
1290 -s Do not generate SOR/EOR files (valid only for GDCs)\n\
1291 -F/-C Working in Fixed Target (F) or Collider (C) mode\n\
1292 -c Handles CDH\n\
1293 -D Direct disc access (no buffering)\n",
1294 myName );
1295 return 1;
1296} /* End of usage */
1297
1298void parseArgs( int argc, char **argv ) {
1299 int arg = 1;
1300 int inFileName = -1;
1301 int outFileName = -1;
1302
1303 myName = argv[0] ;
1304 while ( arg < argc ) {
1305 if ( strcmp( "-?", argv[ arg ] ) == 0 ) {
1306 usage();
1307 exit( 0 );
1308 }
1309 if ( strcmp( "-i", argv[ arg ] ) == 0 ) {
1310 if ( ++arg == argc ) exit( usage() );
1311 inFileName = arg;
1312 if ( freopen( argv[arg], "r", stdin ) == NULL ){
1313 fprintf( stderr,
1314 "%s: failed to open input definition \"%s\" errno:%d ",
1315 myName, argv[arg], errno );
1316 perror( "" );
1317 exit( 1 );
1318 }
1319 } else if ( strcmp( "-v", argv[ arg ] ) == 0 ) {
1320 printf( "%s\n", fileHandlerIdent );
1321 exit( 0 );
1322 } else if ( strcmp( "-o", argv[ arg ] ) == 0 ) {
1323 if ( ++arg == argc ) exit( usage() );
1324 outFileName = arg;
1325 } else if ( strcmp( "-#", argv[ arg ] ) == 0 ) {
1326 int n;
a8bf0b36 1327
f3a800f8 1328 if ( ++arg == argc ) exit( usage() );
1329 if ( sscanf( argv[ arg ], "%d", &n ) != 1 ) exit( usage() );
1330 if ( n < 0 ) exit( usage() );
1331 numOfEvents = n;
1332 } else if ( strcmp( "-s", argv[ arg ] ) == 0 ) {
1333 createSorEor = FALSE;
1334 } else if ( strcmp( "-F", argv[ arg ] ) == 0 ) {
1335 workingMode = fixedTarget;
1336 } else if ( strcmp( "-C", argv[ arg ] ) == 0 ) {
1337 workingMode = collider;
1338 } else if ( strcmp( "-d", argv[ arg ] ) == 0 ) {
1339 debug++;
1340 } else if ( strcmp( "-c", argv[ arg ] ) == 0 ) {
1341 handleCDH = TRUE;
1342 } else if ( strcmp( "-D", argv[ arg ] ) == 0 ) {
1343 bufferData = FALSE;
1344 } else {
1345 fprintf( stderr, "%s: Unknown switch \"%s\"\n", myName, argv[argc] );
1346 exit( usage() );
1347 }
1348 arg++;
1349 }
1350
1351 if ( workingMode == fixedTarget )
1352 LOAD_RAW_EVENT_ID( oneEventDelta, 1, 0, 1 );
1353 else
1354 LOAD_EVENT_ID( oneEventDelta, 0, 0, 1 );
1355 ZERO_EVENT_ID( currEventId );
1356
1357 DBG_VERBOSE {
1358 printf( "Configuration:\n" );
1359 printf( " Debug level: %d\n", debug );
1360 printf( " Configuration: %s\n",
1361 inFileName == -1 ? "stdin" : argv[ inFileName ] );
1362 printf( " Output: %s\n",
1363 outFileName == -1 ? "stdout" : argv[ outFileName ] );
1364 printf( " Working mode: %s\n",
1365 workingMode == fixedTarget ? "fixed target" : "collider" );
1366 printf( " Number of events: %d\n", numOfEvents );
1367 printf( " %s SOR/EOR files\n",
1368 createSorEor ? "Create" : "Do not create" );
1369 printf( " CDH handling: %s\n",
1370 handleCDH ? "enabled" : "disabled" );
1371 printf( " data buffering: %s\n",
1372 bufferData ? "enabled" : "DISABLED" );
1373 }
1374
1375 if ( outFileName == -1 ) {
1376 DBG_BASE
1377 printf( "No more trace information from this point...\n" );
1378 debug = 0;
1379 outF = stdout;
1380 } else {
1381 if ( ( outF = fopen( argv[ outFileName ], "w" ) ) == NULL ) {
1382 fprintf( stderr,
1383 "%s: failed to open output file \"%s\" for writing, errno:%d (%s)\n",
1384 myName,
1385 argv[ outFileName ],
1386 errno,
1387 strerror( errno ) );
1388 exit( 1 );
1389 }
1390 DBG_DETAILED
1391 printf( "Output file \"%s\" opened OK for writing\n",
1392 argv[ outFileName ] );
1393 }
1394} /* End of parseArgs */
1395void initEquipment( struct equipmentHeaderStruct * const eq ) {
1396 memset( eq, 0, sizeof( *eq ) );
1397 RESET_ATTRIBUTES( eq->equipmentTypeAttribute );
1398 eq->equipmentBasicElementSize = 4;
1399} /* End of initEquipment */
1400
1401void decodeCDH( struct ldcEventDescriptorStruct * const ldc,
1402 const struct payloadDescriptorStruct * const payloadDesc ) {
1403 if ( handleCDH ) {
1404 static struct commonDataHeaderStruct *cdhRef = NULL;
1405 struct commonDataHeaderStruct *cdh;
1406 static int softwareTriggerIndicator = FALSE;
1407 int attr;
1408 int trig;
a8bf0b36 1409
f3a800f8 1410 if ( payloadDesc->size < CDH_SIZE ) {
1411 fprintf( stderr,
1412 "%s: payload too small got:%d CDH:%d\n",
1413 myName,
1414 payloadDesc->size,
1415 CDH_SIZE );
1416 exit( 1 );
1417 }
1418 if ( (cdh = (struct commonDataHeaderStruct *)payloadDesc->data) != NULL ) {
1419 if ( cdh->cdhVersion != CDH_VERSION ) {
1420 fprintf( stderr,
1421 "%s: CDH version mismatch expected:%d got:%d\n",
1422 myName,
1423 CDH_VERSION,
1424 cdh->cdhVersion );
1425 exit( 1 );
1426 }
1427 if ( cdhRef == NULL ) {
1428 cdhRef = cdh;
1429#define CDH_TRIGGER_INFORMATION_UNAVAILABLE_MASK (1<<CDH_TRIGGER_INFORMATION_UNAVAILABLE_BIT)
1430 gotAliceTrigger = (cdhRef->cdhStatusErrorBits & CDH_TRIGGER_INFORMATION_UNAVAILABLE_MASK) == 0;
1431 if ( gotAliceTrigger && workingMode == fixedTarget ) {
1432 fprintf( stderr,
1433 "%s: ALICE trigger and fixed target mode are not compatible.\n\
1434Either work in Collider mode or set the trigger unavailable status bit in the CDH.\n",
1435 myName );
1436 exit( 1 );
1437 }
1438 if ( gotAliceTrigger ) {
1439 if ( (cdh->cdhL1TriggerMessage & 0x40) != 0 ) {
1440 fprintf( stderr,
1441 "%s: CDH is a calibration trigger (unsupported) L1TriggerMessage:0x%x\n",
1442 myName, cdh->cdhL1TriggerMessage );
1443 exit( 1 );
1444 }
1445 if ( (cdh->cdhL1TriggerMessage & 0x01) != 0 ) {
1446 softwareTriggerIndicator = TRUE;
1447 }
1448 if ( softwareTriggerIndicator ) {
1449 switch ((cdh->cdhL1TriggerMessage >> 2) & 0xF) {
1450 case 0xD:
1451 case 0xC:
1452 case 0xB:
1453 case 0xA:
1454 case 0x9:
1455 break;
1456 case 0xF:
1457 /* L1SwC bit = on, Clt bit = off, RoC[4..1] = 0xF --> END_OF_DATA */
1458 case 0xE:
1459 /* L1SwC bit = on, Clt bit = off, RoC[4..1] = 0xE0 --> START_OF_DATA */
1460 case 0x8:
1461 /* L1SwC bit = on, Clt bit = off, RoC[4] = 1, but not 0xE or 0xF
1462 --> SYSTEM_SOFTWARE_TRIGGER_EVENT */
1463 default:
1464 /* L1SwC bit = on, Clt bit = off, RoC[4] = 0
1465 --> DETECTOR_SOFTWARE_TRIGGER_EVENT */
1466 fprintf( stderr,
1467 "%s: CDH trigger SOD/EOD/SST/DST (unsupported) \
1468L1TriggerMessage:0x%x ALICETrigger:%s\n",
1469 myName,
1470 cdh->cdhL1TriggerMessage,
1471 gotAliceTrigger ? "yes" : "no" );
1472 exit( 1 );
1473 }
1474 }
1475 }
1476 } else {
1477 if ( (cdh->cdhStatusErrorBits & CDH_TRIGGER_INFORMATION_UNAVAILABLE_MASK) !=
1478 (cdhRef->cdhStatusErrorBits & CDH_TRIGGER_INFORMATION_UNAVAILABLE_MASK) ) {
1479 fprintf( stderr,
1480 "%s: CDH coherency check failed. \
1481Trigger information reference:%savailable current:%savailable\n",
1482 myName,
1483 (cdhRef->cdhStatusErrorBits & CDH_TRIGGER_INFORMATION_UNAVAILABLE_MASK) == 0 ? "UN" : "",
1484 (cdh->cdhStatusErrorBits & CDH_TRIGGER_INFORMATION_UNAVAILABLE_MASK) == 0 ? "UN" : "" );
1485 exit( 1 );
1486 }
1487 if ( gotAliceTrigger ) {
1488 if ( cdhRef->cdhL1TriggerMessage != cdh->cdhL1TriggerMessage ) {
1489 fprintf( stderr,
1490 "%s: CDH coherency check failed. \
1491L1 trigger message reference:0x%x current:0x%x\n",
1492 myName,
1493 cdhRef->cdhL1TriggerMessage,
1494 cdh->cdhL1TriggerMessage );
1495 exit( 1 );
1496 }
1497 if ( cdh->cdhParticipatingSubDetectors != cdhRef->cdhParticipatingSubDetectors ) {
1498 fprintf( stderr,
1499 "%s: CDH coherency check failed. \
1500ParticipatingSubDetectors reference:0x%x current:0x%x\n",
1501 myName,
1502 cdhRef->cdhParticipatingSubDetectors,
1503 cdh->cdhParticipatingSubDetectors );
1504 exit( 1 );
1505 }
1506 if ( cdh->cdhTriggerClassesLow != cdhRef->cdhTriggerClassesLow
1507 || cdh->cdhTriggerClassesHigh != cdhRef->cdhTriggerClassesHigh ) {
1508 fprintf( stderr,
1509 "%s: CDH coherency check failed. \
1510TriggerClassesHigh/Low reference:0x%x-%x current:0x%x-%x\n",
1511 myName,
1512 cdhRef->cdhTriggerClassesHigh, cdhRef->cdhTriggerClassesLow,
1513 cdh ->cdhTriggerClassesHigh, cdh ->cdhTriggerClassesLow );
1514 exit( 1 );
1515 }
1516 if ( cdh->cdhBlockLength != 0xffffffff ) {
1517 if ( (unsigned)payloadDesc->size != cdh->cdhBlockLength ) {
1518 fprintf( stderr,
1519 "%s: CDH coherency check failed. \
1520Payload size:%d (0x%08x) CDH block length:%d (0x%08x)\n",
1521 myName,
1522 payloadDesc->size, payloadDesc->size,
1523 cdh->cdhBlockLength, cdh->cdhBlockLength );
1524 exit( 1 );
1525 }
1526 }
1527 if ( cdh->cdhRoiLow != cdhRef->cdhRoiLow
1528 || cdh->cdhRoiHigh != cdhRef->cdhRoiHigh ) {
1529 fprintf( stderr,
1530 "%s: CDH coherency check failed. \
1531RoiHigh/Low reference:0x%x-%x current:0x%x-%x\n",
1532 myName,
1533 cdhRef->cdhRoiHigh, cdhRef->cdhRoiLow,
1534 cdh ->cdhRoiHigh, cdh ->cdhRoiLow );
1535 exit( 1 );
1536 }
1537 }
1538 if ( cdh->cdhMBZ0 != 0
1539 || cdh->cdhMBZ1 != 0
1540 || cdh->cdhMBZ2 != 0
1541 || cdh->cdhMBZ3 != 0 ) {
1542 fprintf( stderr,
1543 "%s: CDH check failed. MBZ0:0x%x MBZ1:0x%x MBZ2:0x%x MBZ3:0x%x\n",
1544 myName,
1545 cdh->cdhMBZ0, cdh->cdhMBZ1, cdh->cdhMBZ2, cdh->cdhMBZ3 );
1546 exit( 1 );
1547 }
1548 }
1549 for ( attr = 0; attr != 8; attr++ ) {
1550 if ( (cdh->cdhBlockAttributes & (1<<attr)) != 0 ) {
1551 SET_USER_ATTRIBUTE( ldc->header.eventTypeAttribute, attr );
1552 }
1553 }
1554 for ( trig = 0; trig != 32; trig++ ) {
1555 if ( (cdh->cdhTriggerClassesLow & (1<<trig)) != 0 ) {
1556 SET_TRIGGER_IN_PATTERN( ldc->header.eventTriggerPattern,
1557 trig );
1558 }
1559 }
1560 for ( trig = 0; trig != 18; trig++ ) {
1561 if ( (cdh->cdhTriggerClassesHigh & (1<<trig)) != 0 ) {
1562 SET_TRIGGER_IN_PATTERN( ldc->header.eventTriggerPattern,
1563 32+trig );
1564 }
1565 }
1566 if ( gotAliceTrigger )
1567 VALIDATE_TRIGGER_PATTERN( ldc->header.eventTriggerPattern );
1568 }
1569 }
1570} /* End of decodeCDH */
1571
1572void initEvents() {
1573 assert( workingAs == ldc || workingAs == gdc );
1574
1575 if ( workingAs == gdc ) {
1576 struct gdcEventDescriptorStruct *gdc;
1577
1578 for ( gdc = (struct gdcEventDescriptorStruct *)eventsHead;
1579 gdc != NULL;
1580 gdc = gdc->next ) {
1581 struct ldcEventDescriptorStruct *ldc;
a8bf0b36 1582
f3a800f8 1583 initEvent( &gdc->header );
1584 gdc->header.eventSize = gdc->header.eventHeadSize;
1585 gdc->header.eventType = PHYSICS_EVENT;
1586 SET_SYSTEM_ATTRIBUTE( gdc->header.eventTypeAttribute, ATTR_SUPER_EVENT );
1587 gdc->header.eventGdcId = currGdcId;
1588 for ( ldc = gdc->head; ldc != NULL; ldc = ldc->next ) {
1589 struct equipmentEventDescriptorStruct *eq;
1590
1591 initEvent( &ldc->header );
1592 ldc->header.eventSize = ldc->header.eventHeadSize;
1593 ldc->header.eventType = PHYSICS_EVENT;
1594 ldc->header.eventGdcId = currGdcId;
1595 ldc->header.eventLdcId = ldc->id;
1596 for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
1597 initEquipment( &eq->header );
1598 eq->header.equipmentId = eq->id;
1599 if ( workingMode == collider )
1600 SET_SYSTEM_ATTRIBUTE( eq->header.equipmentTypeAttribute,
1601 ATTR_ORBIT_BC );
1602 eq->header.equipmentSize = eq->payload->size + sizeof( eq->header );
1603 ldc->header.eventSize += eq->header.equipmentSize;
1604 decodeCDH( ldc, eq->payload );
1605 OR_ALL_ATTRIBUTES( eq->header.equipmentTypeAttribute,
1606 ldc->header.eventTypeAttribute );
1607 OR_ALL_ATTRIBUTES( eq->header.equipmentTypeAttribute,
1608 gdc->header.eventTypeAttribute );
1609 }
1610 gdc->header.eventSize += ldc->header.eventSize;
1611 }
1612 }
1613
1614 DBG_VERBOSE {
1615 printf( "Headers:\n" );
1616 for ( gdc = (struct gdcEventDescriptorStruct *)eventsHead;
1617 gdc != NULL;
1618 gdc = gdc->next ) {
1619 struct ldcEventDescriptorStruct *ldc;
1620
1621 printf( " GDC:%d size:%d vers:%08x\n",
1622 currGdcId,
1623 gdc->header.eventSize,
1624 gdc->header.eventVersion);
1625 for ( ldc = gdc->head; ldc != NULL; ldc = ldc->next ) {
1626 struct equipmentEventDescriptorStruct *eq;
1627
1628 printf( " LDC:%d size:%d vers:%08x\n",
1629 ldc->id,
1630 ldc->header.eventSize,
1631 ldc->header.eventVersion );
1632 for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
1633 printf( " EQ:%d size:%d %spayload:%d\n",
1634 eq->id,
1635 eq->header.equipmentSize,
1636 eq->header.equipmentSize - sizeof( struct equipmentHeaderStruct ) == (unsigned)eq->payload->size ? "" : "-ERROR",
1637 eq->payload->size );
1638 }
1639 }
1640 }
1641 }
1642 } else if ( workingAs == ldc ) {
1643 struct ldcEventDescriptorStruct *ldc;
a8bf0b36 1644
f3a800f8 1645 for ( ldc = (struct ldcEventDescriptorStruct *)eventsHead;
1646 ldc != NULL;
1647 ldc = ldc->next ) {
1648 struct equipmentEventDescriptorStruct *eq;
1649
1650 initEvent( &ldc->header );
1651 ldc->header.eventSize = ldc->header.eventHeadSize;
1652 ldc->header.eventType = PHYSICS_EVENT;
1653 ldc->header.eventGdcId = VOID_ID;
1654 ldc->header.eventLdcId = ldc->id;
1655 for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
1656 initEquipment( &eq->header );
1657 eq->header.equipmentId = eq->id;
1658 if ( workingMode == collider )
1659 SET_SYSTEM_ATTRIBUTE( eq->header.equipmentTypeAttribute,
1660 ATTR_ORBIT_BC );
1661 eq->header.equipmentSize = eq->payload->size + sizeof( eq->header );
1662 ldc->header.eventSize += eq->header.equipmentSize;
1663 decodeCDH( ldc, eq->payload );
1664 OR_ALL_ATTRIBUTES( eq->header.equipmentTypeAttribute,
1665 ldc->header.eventTypeAttribute );
1666 }
1667 }
1668 DBG_VERBOSE {
1669 printf( "Headers:\n" );
1670 for ( ldc = (struct ldcEventDescriptorStruct *)eventsHead;
1671 ldc != NULL;
1672 ldc = ldc->next ) {
1673 struct equipmentEventDescriptorStruct *eq;
1674
1675 printf( " LDC:%d size:%d vers:%08x\n",
1676 ldc->id,
1677 ldc->header.eventSize,
1678 ldc->header.eventVersion );
1679 for ( eq = ldc->head; eq != NULL; eq = eq->next ) {
1680 printf( " EQ:%d size:%d %spayload:%d\n",
1681 eq->id,
1682 eq->header.equipmentSize,
1683 eq->header.equipmentSize - sizeof( struct equipmentHeaderStruct ) == (unsigned)eq->payload->size ? "" : "-ERROR",
1684 eq->payload->size );
1685 }
1686 }
1687 }
1688 }
1689} /* End of initEvents */
1690
1691void initVars() {
1692 debug = 0;
1693 workingAs = unknown;
1694 workingMode = fixedTarget;
1695 ldcsHead = ldcsTail = NULL;
1696 eventsHead = eventsTail = NULL;
1697 currGdc = NULL;
1698 currLdc = NULL;
1699 currEvent = NULL;
1700 payloadsHead = payloadsTail = NULL;
1701 currRunNb = 1;
1702 numOfLdcs = 0;
1703 numOfEvents = 1;
1704 createSorEor = TRUE;
1705 handleCDH = FALSE;
1706 gotAliceTrigger = TRUE;
1707 bufferData=TRUE;
1708} /* End of initVars */
1709
1710int main( int argc, char **argv ) {
1711 initVars();
1712 parseArgs( argc, argv );
1713 parseRules();
1714 initEvents();
1715 createEvents();
1716 return 0;
1717} /* End of main */