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 ] = strdup( argv[arg] )) == NULL ) {
83 perror( "strdup failed " );
88 if ( numFiles < 1 ) return usage();
95 if ( (dataBlockSizes = malloc( sizeof(int) * numFiles )) == NULL ) {
96 perror( "malloc failed " );
99 if ( (dataBlocks = malloc( sizeof( void * ) * numFiles )) == NULL ) {
100 perror( "malloc failed" );
103 for ( f = 0; f != numFiles; f++ ) {
105 struct stat statData;
107 if ( stat( fileNames[f], &statData ) != 0 ) {
108 fprintf( stderr, "Cannot stat file \"%s\"", fileNames[f] );
113 if ( (dataBlockSizes[f] = statData.st_size) < 0 ) {
115 "Stat for file \"%s\" returns size: %d\n",
116 fileNames[f], (int)statData.st_size );
119 if ( dataBlockSizes[f] == 0 ) {
120 dataBlocks[f] = NULL;
123 if ( (dataBlocks[f] = malloc( dataBlockSizes[f] )) == NULL ) {
125 "Failed to malloc for file \"%s\" size:%d",
126 fileNames[f], dataBlockSizes[f] );
130 if ( (in = fopen( fileNames[f], "r" )) == NULL ) {
131 fprintf( stderr, "Failed to open input file \"%s\"", fileNames[f] );
135 if ( fread( dataBlocks[f],
139 "Failed to read from file \"%s\"",
150 h.eventMagic = EVENT_MAGIC_NUMBER;
151 h.eventHeadSize = EVENT_HEAD_BASE_SIZE;
152 h.eventVersion = EVENT_CURRENT_VERSION;
154 ZERO_TRIGGER_PATTERN( h.eventTriggerPattern );
155 ZERO_DETECTOR_PATTERN( h.eventDetectorPattern );
156 RESET_ATTRIBUTES( h.eventTypeAttribute );
157 h.eventLdcId = h.eventGdcId = VOID_ID;
158 memcpy( &sh, &h, sizeof( h ) );
160 eh.equipmentType = 0;
162 RESET_ATTRIBUTES( eh.equipmentTypeAttribute );
163 eh.equipmentBasicElementSize = 1;
166 void dumpDummy( const int size ) {
170 for ( i = 0, pat = 0; i != size; i++, pat++ )
171 fwrite( &pat, 1, 1, stdout );
174 void createSorEor( eventTypeType eventType ) {
178 sh.eventSize = 10872;
179 h.eventType = sh.eventType = eventType;
180 LOAD_RAW_EVENT_ID( h.eventId, 1, 0, 1 );
181 LOAD_RAW_EVENT_ID( sh.eventId, 1, 0, 1 );
182 RESET_ATTRIBUTES( h.eventTypeAttribute );
183 RESET_ATTRIBUTES( sh.eventTypeAttribute );
184 SET_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_P_START );
185 SET_SYSTEM_ATTRIBUTE( sh.eventTypeAttribute, ATTR_P_START );
186 h.eventGdcId = sh.eventGdcId = 0;
188 for ( i = 0; i != 2; i++ ) {
189 h.eventSize = sh.eventSize;
190 CLEAR_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_SUPER_EVENT );
191 fwrite( &h, sizeof(h), 1, stdout );
192 dumpDummy( h.eventSize - EVENT_HEAD_BASE_SIZE );
194 SET_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_SUPER_EVENT );
195 h.eventSize = sh.eventSize + EVENT_HEAD_BASE_SIZE;
197 for ( l = 0; l != numFiles; l++ ) {
198 fwrite( &h, sizeof(h), 1, stdout );
200 fwrite( &sh, sizeof(sh), 1, stdout );
201 dumpDummy( sh.eventSize - EVENT_HEAD_BASE_SIZE );
203 CLEAR_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_P_START );
204 CLEAR_SYSTEM_ATTRIBUTE( sh.eventTypeAttribute, ATTR_P_START );
205 SET_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_P_END );
206 SET_SYSTEM_ATTRIBUTE( sh.eventTypeAttribute, ATTR_P_END );
211 createSorEor( START_OF_RUN );
215 createSorEor( END_OF_RUN );
218 void createData( const int loopNo ) {
223 for ( totSize = EVENT_HEAD_BASE_SIZE, l = 0;
226 totSize += EVENT_HEAD_BASE_SIZE + sizeof( eh ) + dataBlockSizes[l];
228 h.eventSize = totSize;
229 h.eventType = sh.eventType = PHYSICS_EVENT;
230 RESET_ATTRIBUTES( h.eventTypeAttribute );
231 SET_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_SUPER_EVENT );
232 RESET_ATTRIBUTES( sh.eventTypeAttribute );
233 h.eventGdcId = sh.eventGdcId = 0;
235 for ( n = 0; n != loopNo; n++ ) {
236 LOAD_RAW_EVENT_ID( h.eventId, n, 0, n );
237 LOAD_RAW_EVENT_ID( sh.eventId, n, 0, n );
238 fwrite( &h, sizeof(h), 1, stdout );
240 for ( l = 0; l != numFiles; l++ ) {
242 sh.eventSize = EVENT_HEAD_BASE_SIZE + sizeof( eh ) + dataBlockSizes[l];
243 eh.equipmentSize = dataBlockSizes[l];
244 fwrite( &sh, sizeof(sh), 1, stdout );
245 fwrite( &eh, sizeof(eh), 1, stdout );
246 fwrite( dataBlocks[l], dataBlockSizes[l], 1, stdout );
251 void createStream() {
252 if ( doSor ) createSor();
253 createData( numLoops );
254 if ( doEor ) createEor();
257 int main( int argc, char **argv ) {
260 if ( !handleArgs( argc, argv ) ) return 1;
261 if ( !readData() ) return 1;
269 int main( int argc, char **argv ) {
270 fprintf( stderr, "%s was compiled without DATE\n", argv[0] );