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