3 ** Take a raw data file and produce a DATE-formatted data file
12 #include <sys/types.h>
21 # define TRUE (0 == 0)
24 # define FALSE (0 == 1)
29 char **fileNames = NULL;
30 int *dataBlockSizes = NULL;
31 void **dataBlocks = NULL;
35 struct eventHeaderStruct h;
36 struct eventHeaderStruct sh;
37 struct equipmentHeaderStruct eh;
40 fprintf( stderr, "Usage: %s [-s][-e][-#=N] FILE [FILE]...\n",
45 int handleArgs( const int argc, char * const * const argv ) {
49 if ( argc <= 1 ) return usage();
51 for ( arg = 1; arg < argc; arg++ ) {
53 if ( strcmp( argv[arg], "--" ) == 0 ) {
57 if ( strcmp( argv[arg], "-s" ) == 0 ) {
61 if ( strcmp( argv[arg], "-e" ) == 0 ) {
65 if ( strncmp( argv[arg], "-#=", 3 ) == 0 ) {
66 if ( sscanf( &argv[arg][3], "%d", &numLoops ) != 1 ) {
68 "Failed to scan \"%s\" (expected: -#=number)\n",
74 if ( argv[arg][0] == '-' ) return usage();
77 if ( (fileNames = realloc( fileNames,
78 ++numFiles * sizeof( char* ) )) == NULL ) {
79 perror( "malloc failed " );
82 if ( (fileNames[ numFiles-1 ] = malloc( strlen(argv[arg])+1 )) == NULL ) {
83 perror( "malloc failed " );
86 if ( (strcpy( fileNames[ numFiles-1 ], argv[arg] )) == NULL ) {
87 perror( "strcpy failed " );
92 if ( numFiles < 1 ) return usage();
99 if ( (dataBlockSizes = malloc( sizeof(int) * numFiles )) == NULL ) {
100 perror( "malloc failed " );
103 if ( (dataBlocks = malloc( sizeof( void * ) * numFiles )) == NULL ) {
104 perror( "malloc failed" );
107 for ( f = 0; f != numFiles; f++ ) {
109 struct stat statData;
111 if ( stat( fileNames[f], &statData ) != 0 ) {
112 fprintf( stderr, "Cannot stat file \"%s\"", fileNames[f] );
117 if ( (dataBlockSizes[f] = statData.st_size) < 0 ) {
119 "Stat for file \"%s\" returns size: %d\n",
120 fileNames[f], (int)statData.st_size );
123 if ( dataBlockSizes[f] == 0 ) {
124 dataBlocks[f] = NULL;
127 if ( (dataBlocks[f] = malloc( dataBlockSizes[f] )) == NULL ) {
129 "Failed to malloc for file \"%s\" size:%d",
130 fileNames[f], dataBlockSizes[f] );
134 if ( (in = fopen( fileNames[f], "r" )) == NULL ) {
135 fprintf( stderr, "Failed to open input file \"%s\"", fileNames[f] );
139 if ( fread( dataBlocks[f],
143 "Failed to read from file \"%s\"",
154 h.eventMagic = EVENT_MAGIC_NUMBER;
155 h.eventHeadSize = EVENT_HEAD_BASE_SIZE;
156 h.eventVersion = EVENT_CURRENT_VERSION;
158 ZERO_TRIGGER_PATTERN( h.eventTriggerPattern );
159 ZERO_DETECTOR_PATTERN( h.eventDetectorPattern );
160 RESET_ATTRIBUTES( h.eventTypeAttribute );
161 h.eventLdcId = h.eventGdcId = VOID_ID;
162 memcpy( &sh, &h, sizeof( h ) );
164 eh.equipmentType = 0;
166 RESET_ATTRIBUTES( eh.equipmentTypeAttribute );
167 eh.equipmentBasicElementSize = 1;
170 void dumpDummy( const int size ) {
174 for ( i = 0, pat = 0; i != size; i++, pat++ )
175 fwrite( &pat, 1, 1, stdout );
178 void createSorEor( eventTypeType eventType ) {
182 sh.eventSize = 10872;
183 h.eventType = sh.eventType = eventType;
184 LOAD_RAW_EVENT_ID( h.eventId, 1, 0, 1 );
185 LOAD_RAW_EVENT_ID( sh.eventId, 1, 0, 1 );
186 RESET_ATTRIBUTES( h.eventTypeAttribute );
187 RESET_ATTRIBUTES( sh.eventTypeAttribute );
188 SET_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_P_START );
189 SET_SYSTEM_ATTRIBUTE( sh.eventTypeAttribute, ATTR_P_START );
190 h.eventGdcId = sh.eventGdcId = 0;
192 for ( i = 0; i != 2; i++ ) {
193 h.eventSize = sh.eventSize;
194 CLEAR_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_SUPER_EVENT );
195 fwrite( &h, sizeof(h), 1, stdout );
196 dumpDummy( h.eventSize - EVENT_HEAD_BASE_SIZE );
198 SET_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_SUPER_EVENT );
199 h.eventSize = sh.eventSize + EVENT_HEAD_BASE_SIZE;
201 for ( l = 0; l != numFiles; l++ ) {
202 fwrite( &h, sizeof(h), 1, stdout );
204 fwrite( &sh, sizeof(sh), 1, stdout );
205 dumpDummy( sh.eventSize - EVENT_HEAD_BASE_SIZE );
207 CLEAR_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_P_START );
208 CLEAR_SYSTEM_ATTRIBUTE( sh.eventTypeAttribute, ATTR_P_START );
209 SET_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_P_END );
210 SET_SYSTEM_ATTRIBUTE( sh.eventTypeAttribute, ATTR_P_END );
215 createSorEor( START_OF_RUN );
219 createSorEor( END_OF_RUN );
222 void createData( const int loopNo ) {
227 for ( totSize = EVENT_HEAD_BASE_SIZE, l = 0;
230 totSize += EVENT_HEAD_BASE_SIZE + sizeof( eh ) + dataBlockSizes[l];
232 h.eventSize = totSize;
233 h.eventType = sh.eventType = PHYSICS_EVENT;
234 RESET_ATTRIBUTES( h.eventTypeAttribute );
235 SET_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_SUPER_EVENT );
236 RESET_ATTRIBUTES( sh.eventTypeAttribute );
237 h.eventGdcId = sh.eventGdcId = 0;
239 for ( n = 0; n != loopNo; n++ ) {
240 LOAD_RAW_EVENT_ID( h.eventId, n, 0, n );
241 LOAD_RAW_EVENT_ID( sh.eventId, n, 0, n );
242 fwrite( &h, sizeof(h), 1, stdout );
244 for ( l = 0; l != numFiles; l++ ) {
246 sh.eventSize = EVENT_HEAD_BASE_SIZE + sizeof( eh ) + dataBlockSizes[l];
247 eh.equipmentSize = dataBlockSizes[l];
248 fwrite( &sh, sizeof(sh), 1, stdout );
249 fwrite( &eh, sizeof(eh), 1, stdout );
250 fwrite( dataBlocks[l], dataBlockSizes[l], 1, stdout );
255 void createStream() {
256 if ( doSor ) createSor();
257 createData( numLoops );
258 if ( doEor ) createEor();
261 int main( int argc, char **argv ) {
264 if ( !handleArgs( argc, argv ) ) return 1;
265 if ( !readData() ) return 1;
273 int main( int argc, char **argv ) {
274 fprintf( stderr, "%s was compiled without DATE\n", argv[0] );