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