3 ** Take a raw data file and produce a DATE-formatted data file
11 #include <sys/types.h>
15 #include "DateEvent.h"
18 # define TRUE (0 == 0)
21 # define FALSE (0 == 1)
26 char **fileNames = NULL;
27 int *dataBlockSizes = NULL;
28 void **dataBlocks = NULL;
32 struct eventHeaderStruct h;
33 struct eventHeaderStruct sh;
34 struct equipmentHeaderStruct eh;
37 fprintf( stderr, "Usage: %s [-s][-e][-#=N] FILE [FILE]...\n",
42 int handleArgs( const int argc, char * const * const argv ) {
46 if ( argc <= 1 ) return usage();
48 for ( arg = 1; arg < argc; arg++ ) {
50 if ( strcmp( argv[arg], "--" ) == 0 ) {
54 if ( strcmp( argv[arg], "-s" ) == 0 ) {
58 if ( strcmp( argv[arg], "-e" ) == 0 ) {
62 if ( strncmp( argv[arg], "-#=", 3 ) == 0 ) {
63 if ( sscanf( &argv[arg][3], "%d", &numLoops ) != 1 ) {
65 "Failed to scan \"%s\" (expected: -#=number)\n",
71 if ( argv[arg][0] == '-' ) return usage();
74 if ( (fileNames = realloc( fileNames,
75 ++numFiles * sizeof( char* ) )) == NULL ) {
76 perror( "malloc failed " );
79 if ( (fileNames[ numFiles-1 ] = strdup( argv[arg] )) == NULL ) {
80 perror( "strdup failed " );
85 if ( numFiles < 1 ) return usage();
92 if ( (dataBlockSizes = malloc( sizeof(int) * numFiles )) == NULL ) {
93 perror( "malloc failed " );
96 if ( (dataBlocks = malloc( sizeof( void * ) * numFiles )) == NULL ) {
97 perror( "malloc failed" );
100 for ( f = 0; f != numFiles; f++ ) {
102 struct stat statData;
104 if ( stat( fileNames[f], &statData ) != 0 ) {
105 fprintf( stderr, "Cannot stat file \"%s\"", fileNames[f] );
110 if ( (dataBlockSizes[f] = statData.st_size) < 0 ) {
112 "Stat for file \"%s\" returns size: %d\n",
113 fileNames[f], (int)statData.st_size );
116 if ( dataBlockSizes[f] == 0 ) {
117 dataBlocks[f] = NULL;
120 if ( (dataBlocks[f] = malloc( dataBlockSizes[f] )) == NULL ) {
122 "Failed to malloc for file \"%s\" size:%d",
123 fileNames[f], dataBlockSizes[f] );
127 if ( (in = fopen( fileNames[f], "r" )) == NULL ) {
128 fprintf( stderr, "Failed to open input file \"%s\"", fileNames[f] );
132 if ( fread( dataBlocks[f],
136 "Failed to read from file \"%s\"",
147 h.eventMagic = EVENT_MAGIC_NUMBER;
148 h.eventHeadSize = EVENT_HEAD_BASE_SIZE;
149 h.eventVersion = EVENT_CURRENT_VERSION;
151 ZERO_TRIGGER_PATTERN( h.eventTriggerPattern );
152 ZERO_DETECTOR_PATTERN( h.eventDetectorPattern );
153 RESET_ATTRIBUTES( h.eventTypeAttribute );
154 h.eventLdcId = h.eventGdcId = VOID_ID;
155 memcpy( &sh, &h, sizeof( h ) );
157 eh.equipmentType = 0;
159 RESET_ATTRIBUTES( eh.equipmentTypeAttribute );
160 eh.equipmentBasicElementSize = 1;
163 void dumpDummy( const int size ) {
167 for ( i = 0, pat = 0; i != size; i++, pat++ )
168 fwrite( &pat, 1, 1, stdout );
171 void createSorEor( eventTypeType eventType ) {
175 sh.eventSize = 10872;
176 h.eventType = sh.eventType = eventType;
177 LOAD_RAW_EVENT_ID( h.eventId, 1, 0, 1 );
178 LOAD_RAW_EVENT_ID( sh.eventId, 1, 0, 1 );
179 RESET_ATTRIBUTES( h.eventTypeAttribute );
180 RESET_ATTRIBUTES( sh.eventTypeAttribute );
181 SET_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_P_START );
182 SET_SYSTEM_ATTRIBUTE( sh.eventTypeAttribute, ATTR_P_START );
183 h.eventGdcId = sh.eventGdcId = 0;
185 for ( i = 0; i != 2; i++ ) {
186 h.eventSize = sh.eventSize;
187 CLEAR_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_SUPER_EVENT );
188 fwrite( &h, sizeof(h), 1, stdout );
189 dumpDummy( h.eventSize - EVENT_HEAD_BASE_SIZE );
191 SET_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_SUPER_EVENT );
192 h.eventSize = sh.eventSize + EVENT_HEAD_BASE_SIZE;
194 for ( l = 0; l != numFiles; l++ ) {
195 fwrite( &h, sizeof(h), 1, stdout );
197 fwrite( &sh, sizeof(sh), 1, stdout );
198 dumpDummy( sh.eventSize - EVENT_HEAD_BASE_SIZE );
200 CLEAR_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_P_START );
201 CLEAR_SYSTEM_ATTRIBUTE( sh.eventTypeAttribute, ATTR_P_START );
202 SET_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_P_END );
203 SET_SYSTEM_ATTRIBUTE( sh.eventTypeAttribute, ATTR_P_END );
208 createSorEor( START_OF_RUN );
212 createSorEor( END_OF_RUN );
215 void createData( const int loopNo ) {
220 for ( totSize = EVENT_HEAD_BASE_SIZE, l = 0;
223 totSize += EVENT_HEAD_BASE_SIZE + sizeof( eh ) + dataBlockSizes[l];
225 h.eventSize = totSize;
226 h.eventType = sh.eventType = PHYSICS_EVENT;
227 RESET_ATTRIBUTES( h.eventTypeAttribute );
228 SET_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_SUPER_EVENT );
229 RESET_ATTRIBUTES( sh.eventTypeAttribute );
230 h.eventGdcId = sh.eventGdcId = 0;
232 for ( n = 0; n != loopNo; n++ ) {
233 LOAD_RAW_EVENT_ID( h.eventId, n, 0, n );
234 LOAD_RAW_EVENT_ID( sh.eventId, n, 0, n );
235 fwrite( &h, sizeof(h), 1, stdout );
237 for ( l = 0; l != numFiles; l++ ) {
239 sh.eventSize = EVENT_HEAD_BASE_SIZE + sizeof( eh ) + dataBlockSizes[l];
240 eh.equipmentSize = dataBlockSizes[l];
241 fwrite( &sh, sizeof(sh), 1, stdout );
242 fwrite( &eh, sizeof(eh), 1, stdout );
243 fwrite( dataBlocks[l], dataBlockSizes[l], 1, stdout );
248 void createStream() {
249 if ( doSor ) createSor();
250 createData( numLoops );
251 if ( doEor ) createEor();
254 int main( int argc, char **argv ) {
257 if ( !handleArgs( argc, argv ) ) return 1;
258 if ( !readData() ) return 1;