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