]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/raw2date.c
extended check
[u/mrichter/AliRoot.git] / RAW / raw2date.c
1 /* raw2date.c
2 **
3 ** Take a raw data file and produce a DATE-formatted data file
4 **
5 ** Revision history:
6 **  19/03/03 RD         Created
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15
16 #ifdef ALI_DATE
17
18 #include "event.h"
19
20 #ifndef TRUE
21 # define TRUE (0 == 0)
22 #endif
23 #ifndef FALSE
24 # define FALSE (0 == 1)
25 #endif
26
27 const char *whoAmI;
28 int numFiles = 0;
29 char **fileNames = NULL;
30 int   *dataBlockSizes = NULL;
31 void **dataBlocks = NULL;
32 int doSor = FALSE;
33 int doEor = FALSE;
34 int numLoops = 1;
35 struct eventHeaderStruct h;
36 struct eventHeaderStruct sh;
37 struct equipmentHeaderStruct eh;
38
39 int usage() {
40   fprintf( stderr, "Usage: %s [-s][-e][-#=N] FILE [FILE]...\n",
41            whoAmI );
42   return FALSE;
43 }
44
45 int handleArgs( const int argc, char * const * const argv ) {
46   int inFile = FALSE;
47   int arg;
48   
49   if ( argc <= 1 ) return usage();
50
51   for ( arg = 1; arg < argc; arg++ ) {
52     if ( !inFile ) {
53       if ( strcmp( argv[arg], "--" ) == 0 ) {
54         inFile = TRUE;
55         continue;
56       }
57       if ( strcmp( argv[arg], "-s" ) == 0 ) {
58         doSor = TRUE;
59         continue;
60       }
61       if ( strcmp( argv[arg], "-e" ) == 0 ) {
62         doEor = TRUE;
63         continue;
64       }
65       if ( strncmp( argv[arg], "-#=", 3 ) == 0 ) {
66         if ( sscanf( &argv[arg][3], "%d", &numLoops ) != 1 ) {
67           fprintf( stderr,
68                    "Failed to scan \"%s\" (expected: -#=number)\n",
69                    argv[arg] );
70           return usage();
71         }
72         continue;
73       }
74       if ( argv[arg][0] == '-' ) return usage();
75       inFile = TRUE;
76     }
77     if ( (fileNames = realloc( fileNames,
78                                ++numFiles * sizeof( char* ) )) == NULL ) {
79       perror( "malloc failed " );
80       return FALSE;
81     }
82     if ( (fileNames[ numFiles-1 ] = strdup( argv[arg] )) == NULL ) {
83       perror( "strdup failed " );
84       return FALSE;
85     }
86   }
87
88   if ( numFiles < 1 ) return usage();
89   return TRUE;
90 }
91
92 int readData() {
93   int f;
94
95   if ( (dataBlockSizes = malloc( sizeof(int) * numFiles )) == NULL ) {
96     perror( "malloc failed " );
97     return FALSE;
98   }
99   if ( (dataBlocks = malloc( sizeof( void * ) * numFiles )) == NULL ) {
100     perror( "malloc failed" );
101     return FALSE;
102   }
103   for ( f = 0; f != numFiles; f++ ) {
104     FILE *in;
105     struct stat statData;
106
107     if ( stat( fileNames[f], &statData ) != 0 ) {
108       fprintf( stderr, "Cannot stat file \"%s\"", fileNames[f] );
109       perror( " " );
110       return FALSE;
111     }
112
113     if ( (dataBlockSizes[f] = statData.st_size) < 0 ) {
114       fprintf( stderr,
115                "Stat for file \"%s\" returns size: %d\n",
116                fileNames[f], (int)statData.st_size );
117       return FALSE;
118     }
119     if ( dataBlockSizes[f] == 0 ) {
120       dataBlocks[f] = NULL;
121       continue;
122     }
123     if ( (dataBlocks[f] = malloc( dataBlockSizes[f] )) == NULL ) {
124       fprintf( stderr,
125                "Failed to malloc for file \"%s\" size:%d",
126                fileNames[f], dataBlockSizes[f] );
127       perror( " " );
128       return FALSE;
129     }
130     if ( (in = fopen( fileNames[f], "r" )) == NULL ) {
131       fprintf( stderr, "Failed to open input file \"%s\"", fileNames[f] );
132       perror( " " );
133       return FALSE;
134     }
135     if ( fread( dataBlocks[f],
136                 dataBlockSizes[f],
137                 1, in ) != 1 ) {
138       fprintf( stderr,
139                "Failed to read from file \"%s\"",
140                fileNames[f] );
141       perror( " " );
142       return FALSE;
143     }
144     fclose( in );
145   }
146   return TRUE;
147 }
148
149 void initStructs() {
150   h.eventMagic = EVENT_MAGIC_NUMBER;
151   h.eventHeadSize = EVENT_HEAD_BASE_SIZE;
152   h.eventVersion = EVENT_CURRENT_VERSION;
153   h.eventRunNb = 1;
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 ) );
159
160   eh.equipmentType = 0;
161   eh.equipmentId = 0;
162   RESET_ATTRIBUTES( eh.equipmentTypeAttribute );
163   eh.equipmentBasicElementSize = 1;
164 }
165
166 void dumpDummy( const int size ) {
167   int i;
168   char pat;
169
170   for ( i = 0, pat = 0; i != size; i++, pat++ )
171     fwrite( &pat, 1, 1, stdout );
172 }
173
174 void createSorEor( eventTypeType eventType ) {
175   int l;
176   int i;
177
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;
187
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 );
193
194     SET_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_SUPER_EVENT );
195     h.eventSize = sh.eventSize + EVENT_HEAD_BASE_SIZE;
196   
197     for ( l = 0; l != numFiles; l++ ) {
198       fwrite( &h, sizeof(h), 1, stdout );
199       sh.eventLdcId = l;
200       fwrite( &sh, sizeof(sh), 1, stdout );
201       dumpDummy( sh.eventSize - EVENT_HEAD_BASE_SIZE );
202     }
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 );
207   }
208 }
209
210 void createSor() {
211   createSorEor( START_OF_RUN );
212 }
213
214 void createEor() {
215   createSorEor( END_OF_RUN );
216 }
217
218 void createData( const int loopNo ) {
219   int n;
220   int l;
221   int totSize;
222
223   for ( totSize = EVENT_HEAD_BASE_SIZE, l = 0;
224         l != numFiles;
225         l++ )
226     totSize += EVENT_HEAD_BASE_SIZE + sizeof( eh ) + dataBlockSizes[l];
227   
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;
234   
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 );
239
240     for ( l = 0; l != numFiles; l++ ) {
241       sh.eventLdcId = 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 );
247     }
248   }
249 }
250
251 void createStream() {
252   if ( doSor ) createSor();
253   createData( numLoops );
254   if ( doEor ) createEor();
255 }
256
257 int main( int argc, char **argv ) {
258   whoAmI = argv[0];
259
260   if ( !handleArgs( argc, argv ) ) return 1;
261   if ( !readData() ) return 1;
262   initStructs();
263   createStream();
264   return 0;
265 }
266
267 #else
268
269 int main( int argc, char **argv ) {
270   fprintf( stderr, "%s was compiled without DATE\n", argv[0] );
271   return 1;
272 }
273
274 #endif