Removing memory leaks
[u/mrichter/AliRoot.git] / RAW / raw2date.c
CommitLineData
5b7dab70 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*/
721230b0 8
5b7dab70 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
721230b0 16#ifdef ALI_DATE
17
18#include "event.h"
5b7dab70 19
20#ifndef TRUE
21# define TRUE (0 == 0)
22#endif
23#ifndef FALSE
24# define FALSE (0 == 1)
25#endif
26
27const char *whoAmI;
28int numFiles = 0;
29char **fileNames = NULL;
30int *dataBlockSizes = NULL;
31void **dataBlocks = NULL;
32int doSor = FALSE;
33int doEor = FALSE;
34int numLoops = 1;
35struct eventHeaderStruct h;
36struct eventHeaderStruct sh;
37struct equipmentHeaderStruct eh;
38
39int usage() {
40 fprintf( stderr, "Usage: %s [-s][-e][-#=N] FILE [FILE]...\n",
41 whoAmI );
42 return FALSE;
43}
44
45int 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 }
1e54e9ae 82 if ( (fileNames[ numFiles-1 ] = malloc( strlen(argv[arg])+1 )) == NULL ) {
83 perror( "malloc failed " );
84 return FALSE;
85 }
86 if ( (strcpy( fileNames[ numFiles-1 ], argv[arg] )) == NULL ) {
87 perror( "strcpy failed " );
5b7dab70 88 return FALSE;
89 }
90 }
91
92 if ( numFiles < 1 ) return usage();
93 return TRUE;
94}
95
96int readData() {
97 int f;
98
99 if ( (dataBlockSizes = malloc( sizeof(int) * numFiles )) == NULL ) {
100 perror( "malloc failed " );
101 return FALSE;
102 }
103 if ( (dataBlocks = malloc( sizeof( void * ) * numFiles )) == NULL ) {
104 perror( "malloc failed" );
105 return FALSE;
106 }
107 for ( f = 0; f != numFiles; f++ ) {
108 FILE *in;
109 struct stat statData;
110
111 if ( stat( fileNames[f], &statData ) != 0 ) {
112 fprintf( stderr, "Cannot stat file \"%s\"", fileNames[f] );
113 perror( " " );
114 return FALSE;
115 }
116
117 if ( (dataBlockSizes[f] = statData.st_size) < 0 ) {
118 fprintf( stderr,
119 "Stat for file \"%s\" returns size: %d\n",
120 fileNames[f], (int)statData.st_size );
121 return FALSE;
122 }
123 if ( dataBlockSizes[f] == 0 ) {
124 dataBlocks[f] = NULL;
125 continue;
126 }
127 if ( (dataBlocks[f] = malloc( dataBlockSizes[f] )) == NULL ) {
128 fprintf( stderr,
129 "Failed to malloc for file \"%s\" size:%d",
130 fileNames[f], dataBlockSizes[f] );
131 perror( " " );
132 return FALSE;
133 }
134 if ( (in = fopen( fileNames[f], "r" )) == NULL ) {
135 fprintf( stderr, "Failed to open input file \"%s\"", fileNames[f] );
136 perror( " " );
137 return FALSE;
138 }
139 if ( fread( dataBlocks[f],
140 dataBlockSizes[f],
141 1, in ) != 1 ) {
142 fprintf( stderr,
143 "Failed to read from file \"%s\"",
144 fileNames[f] );
145 perror( " " );
146 return FALSE;
147 }
148 fclose( in );
149 }
150 return TRUE;
151}
152
153void initStructs() {
154 h.eventMagic = EVENT_MAGIC_NUMBER;
155 h.eventHeadSize = EVENT_HEAD_BASE_SIZE;
156 h.eventVersion = EVENT_CURRENT_VERSION;
157 h.eventRunNb = 1;
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 ) );
163
164 eh.equipmentType = 0;
165 eh.equipmentId = 0;
166 RESET_ATTRIBUTES( eh.equipmentTypeAttribute );
167 eh.equipmentBasicElementSize = 1;
168}
169
170void dumpDummy( const int size ) {
171 int i;
172 char pat;
173
174 for ( i = 0, pat = 0; i != size; i++, pat++ )
175 fwrite( &pat, 1, 1, stdout );
176}
177
178void createSorEor( eventTypeType eventType ) {
179 int l;
180 int i;
181
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;
191
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 );
197
198 SET_SYSTEM_ATTRIBUTE( h.eventTypeAttribute, ATTR_SUPER_EVENT );
199 h.eventSize = sh.eventSize + EVENT_HEAD_BASE_SIZE;
200
201 for ( l = 0; l != numFiles; l++ ) {
202 fwrite( &h, sizeof(h), 1, stdout );
203 sh.eventLdcId = l;
204 fwrite( &sh, sizeof(sh), 1, stdout );
205 dumpDummy( sh.eventSize - EVENT_HEAD_BASE_SIZE );
206 }
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 );
211 }
212}
213
214void createSor() {
215 createSorEor( START_OF_RUN );
216}
217
218void createEor() {
219 createSorEor( END_OF_RUN );
220}
221
222void createData( const int loopNo ) {
223 int n;
224 int l;
225 int totSize;
226
227 for ( totSize = EVENT_HEAD_BASE_SIZE, l = 0;
228 l != numFiles;
229 l++ )
230 totSize += EVENT_HEAD_BASE_SIZE + sizeof( eh ) + dataBlockSizes[l];
231
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;
238
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 );
243
244 for ( l = 0; l != numFiles; l++ ) {
245 sh.eventLdcId = 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 );
251 }
252 }
253}
254
255void createStream() {
256 if ( doSor ) createSor();
257 createData( numLoops );
258 if ( doEor ) createEor();
259}
260
261int main( int argc, char **argv ) {
262 whoAmI = argv[0];
263
264 if ( !handleArgs( argc, argv ) ) return 1;
265 if ( !readData() ) return 1;
266 initStructs();
267 createStream();
268 return 0;
269}
721230b0 270
271#else
272
273int main( int argc, char **argv ) {
274 fprintf( stderr, "%s was compiled without DATE\n", argv[0] );
275 return 1;
276}
277
278#endif