source: fact/tools/FAD/fadtoraw/fadtoraw.cpp@ 15446

Last change on this file since 15446 was 9904, checked in by neise, 14 years ago
FAD dat-file to M0-fileformat (raw) converter initial commit
File size: 12.9 KB
Line 
1#include "fadtoraw.h"
2
3#define NUM_OF_CHANNELS_PER_CHIP 9
4#define NUM_OF_DRS_PER_FAD 4
5#define CPC NUM_OF_CHANNELS_PER_CHIP
6#define DPF NUM_OF_DRS_PER_FAD
7#define MAX_NUM_OF_FADS 50
8
9// converts FAD -> .RAW
10int fadtoraw(char *inputfilename, timeval starttime, timeval endtime, int runnum){
11
12
13 FILE *faddatafile;
14 FILE* RAWfile;
15 char rawfilename[PATH_MAX];
16
17 EventHeader RawEventHeader;
18 RunHeader RawRunHeader;
19 RawRunHeader.NChips = DPF;
20 RawRunHeader.NBoards = 1; //fix
21 BoardStructure rawBoard[RawRunHeader.NBoards];
22 EVNT FadEvent;
23 unsigned short board_ids[MAX_NUM_OF_FADS];
24 short convert; // buffer to convert adc data
25 float temperature=0;
26
27 // open input data file which is fad-formatted
28 if ((faddatafile = fopen (inputfilename, "r")) == NULL)
29 {
30 printf ("Error: Inputfile %s not found\n", inputfilename);
31 return -1;
32 }
33 // Open outputfile, which will be RAW formatted
34 char dummy[PATH_MAX];
35 strncpy(dummy,inputfilename,strlen(inputfilename)-4);
36 sprintf(rawfilename, "%s.raw", dummy);
37 if ((RAWfile = fopen (rawfilename, "w")) == NULL)
38 {
39 printf ("Error: Could not open outputfile %s\n", rawfilename);
40 return -1;
41 }
42
43 //RAW file starts with a Runheader, but not all information is known at this
44 // point, so an empty header is written, and filled after the inputfile was processed.
45 write_RAWheader (rawBoard, RawRunHeader, RAWfile);
46
47 // reading FAD input file. counting number of events in file.
48 // Write FAD-Data to FAD-struct
49 int Number_of_Events_in_File =0;
50 for (int evnt_cnt = 0;; evnt_cnt++)
51 {
52
53 // reading package header
54 // try to read START PACKAGE FLAG = 0xFB01 ... if reading fails, EOF found.
55 if (fread (&FadEvent.evnt_header.start_package_flag, sizeof (FadEvent.evnt_header.start_package_flag), 1, faddatafile) != 1)
56 {
57 // end of file reached
58 Number_of_Events_in_File = evnt_cnt;
59 std::cout << evnt_cnt << " events counted.\n";
60 break;
61 }
62
63
64
65 if(ntohs (FadEvent.evnt_header.start_package_flag) != 0xFB01){
66 printf ("Error: Wrong start package flag in event %d\n", evnt_cnt);
67 exit (1);
68 }
69 fread (&FadEvent.evnt_header.package_length, sizeof (FadEvent.evnt_header.package_length), 1, faddatafile);
70 fread (&FadEvent.evnt_header.version_no, sizeof (FadEvent.evnt_header.version_no), 1, faddatafile);
71 fread (&FadEvent.evnt_header.trigger_id, sizeof (FadEvent.evnt_header.trigger_id), 1, faddatafile);
72 fread (&FadEvent.evnt_header.trigger_type, sizeof (FadEvent.evnt_header.trigger_type), 1, faddatafile);
73 fread (&FadEvent.evnt_header.trigger_crc, sizeof (FadEvent.evnt_header.trigger_crc), 1, faddatafile);
74 fread (&FadEvent.evnt_header.local_trigger_id, sizeof (FadEvent.evnt_header.local_trigger_id), 1, faddatafile);
75 fread (&FadEvent.evnt_header.local_trigger_type, sizeof (FadEvent.evnt_header.local_trigger_type), 1, faddatafile);
76 fread (&FadEvent.evnt_header.local_trigger_crc, sizeof (FadEvent.evnt_header.local_trigger_crc), 1, faddatafile);
77 fread (&FadEvent.evnt_header.board_id, sizeof (FadEvent.evnt_header.board_id), 1, faddatafile);
78
79
80 RawEventHeader.EventNumber = ntohl(FadEvent.evnt_header.local_trigger_id);
81 RawEventHeader.TriggerType = ntohs(FadEvent.evnt_header.local_trigger_type);
82 RawEventHeader.Second = starttime.tv_sec; // Event time stamp (result of gettimeofday())
83 RawEventHeader.Microsecond = starttime.tv_usec;
84 RawEventHeader.TriggerType = FadEvent.evnt_header.local_trigger_type;
85
86 // read out temperatures and sum them up
87
88 // i need some dummy variables here
89 long SumOfRois =0;
90 int ThisRoi=0;
91 int start_cell_dummy =0;
92
93 for (int i = 0; i < DPF; i++)
94 {
95 fread (&FadEvent.evnt_header.drs_temperature[i], sizeof (FadEvent.evnt_header.drs_temperature[i]), 1, faddatafile);
96
97 // if MSB = sign bit is set.
98 if ((ntohs (FadEvent.evnt_header.drs_temperature[i]) & 0x8000) == 0) // temp is positive
99 {
100 temperature += float(ntohs (FadEvent.evnt_header.drs_temperature[i]) >> 3)/16;
101 }
102 else // temp is negative
103 {
104 temperature += float(0xE000 | (ntohs (FadEvent.evnt_header.drs_temperature[i])) >> 3)/16;
105 }
106 }
107
108 // DAC Values, only if version > 0x0101
109 if (ntohs (FadEvent.evnt_header.version_no) > 0x0101)
110 {
111 for (int i = 0; i < 8; i++)
112 {
113 fread (&FadEvent.evnt_header.dac[i], sizeof (FadEvent.evnt_header.dac[i]), 1, faddatafile);
114 }
115 }
116
117
118 SumOfRois = 0;
119 ThisRoi = 0;
120 for (int i = 0; i < (DPF * CPC); i++) // for loop over DPFxCPC channels .. i.e. 36 channels in our case.
121 {
122 // read channel header
123 // ID
124 fread (&FadEvent.channel[i].channel_id, sizeof (FadEvent.channel[i].channel_id), 1, faddatafile);
125 // start cell
126 fread (&FadEvent.channel[i].channel_start_cell, sizeof (FadEvent.channel[i].channel_start_cell), 1, faddatafile);
127 // region of interest
128 fread (&FadEvent.channel[i].channel_roi, sizeof (FadEvent.channel[i].channel_roi), 1, faddatafile);
129 ThisRoi = ntohs(FadEvent.channel[i].channel_roi);
130
131 SumOfRois += ThisRoi;
132
133 // allocate memory and read channel data
134 FadEvent.channel[i].channel_adc_data = (unsigned short*) calloc (ThisRoi, sizeof (unsigned short));
135 fread (FadEvent.channel[i].channel_adc_data, sizeof (unsigned short), ThisRoi, faddatafile);
136
137 }
138
139 // now I know everything about this Event and can write the run header.
140 // write RAW Event Header
141 RawEventHeader.EventSize = SumOfRois * sizeof(short) + DPF * sizeof(int) * RawRunHeader.NBoards; // THE TRIGGER CELLS ARE PART OF THE EVENT SIZE (+16)!!!!!!!
142
143 fwrite(&RawEventHeader.EventNumber, (size_t) sizeof(RawEventHeader.EventNumber), 1, RAWfile);
144 fwrite(&RawEventHeader.Second, (size_t) sizeof(RawEventHeader.Second), 1, RAWfile);
145 fwrite(&RawEventHeader.Microsecond, (size_t) sizeof(RawEventHeader.Microsecond), 1, RAWfile);
146 fwrite(&RawEventHeader.TriggerType, (size_t) sizeof(RawEventHeader.TriggerType), 1, RAWfile);
147 fwrite(&RawEventHeader.EventSize, (size_t) sizeof(RawEventHeader.EventSize), 1, RAWfile);
148
149 // write trigger cells
150 // these triger cells are the so called channel_start cells ...I read out of the fad
151 // input data file.
152 // I read out the channels in this order: 00 10 20 30 01 11 21 31 .. 28 38.
153 // so the four trigger cell I need are stored in the first
154 // four channels of each event.
155
156
157 for(unsigned int l=0; l < RawRunHeader.NBoards*RawRunHeader.NChips; l++){
158 start_cell_dummy = int(ntohs(FadEvent.channel[l].channel_start_cell));
159 fwrite(&start_cell_dummy, sizeof(int), 1, RAWfile);
160 }
161
162 // okay now i can write the 36 channels into the data file.
163 // but they were stored in a different manner ... so we have
164 // to do this carefully
165 //
166 // oh and in addition. the data is not yet in a nice PC readable format.
167 // its still in 12bit signed twis complement + out-of-range-bit with leading zeroes.
168 // we have to transform it into nice signed shorts.
169
170
171 // for loop over DPFxCPC channels .. i.e. 36 channels in our case.
172 for (int chip =0 ; chip < DPF ; ++chip) {
173 for (int ch = 0; ch < CPC; ++ch) {
174
175 // we want to walk like this: 0 4 8 12 .. 32 1 5 9 .. 33 2 6 10 ... 34 3 7 11 ... 35
176
177 for (int b =0 ; b < ntohs(FadEvent.channel[chip+DPF*ch].channel_roi) ; b++) {
178 convert = ntohs(FadEvent.channel[chip+DPF*ch].channel_adc_data[b]);
179 //if(convert == 0x1FFF) convert = 0x7FFF; //set value for overflow
180 //else if(convert == 0x1000)convert = 0x8000; //set value for underflow
181 (convert <<= 4) >>= 4; //delete the sign-bit by shifting left and shift back
182 fwrite (&convert , sizeof(unsigned short) , 1 , RAWfile);
183 }
184 }
185 }
186
187
188 for (int i = 0; i < (DPF * CPC); i++) // for loop over DPFxCPC channels .. i.e. 36 channels in our case.
189 {
190 // free memory of channel data
191 free (FadEvent.channel[i].channel_adc_data);
192 }
193
194 // CRC, only if version > 0x0100
195 if (ntohs (FadEvent.evnt_header.version_no) > 0x0100)
196 {
197 fread (&FadEvent.package_crc, sizeof (FadEvent.package_crc), 1, faddatafile);
198 }
199
200 // end package flag
201 fread (&FadEvent.end_package_flag, sizeof (FadEvent.end_package_flag), 1, faddatafile);
202 if(ntohs (FadEvent.end_package_flag) != 0x04FE){
203 printf ("Error: Wrong end package flag (%#x) in event %d\n", ntohs (FadEvent.end_package_flag), evnt_cnt);
204 return -1;
205 }
206
207
208
209
210 }
211
212 //FAD Data to RAW-struct
213 //RunHeader
214 RawRunHeader.MagicNum = 0xe0e0;
215 RawRunHeader.DataFormat = 1;
216 RawRunHeader.RunHeaderSize = sizeof(RawRunHeader);
217 RawRunHeader.EventHeaderSize = sizeof(RawEventHeader);
218 RawRunHeader.BoardStructureSize = sizeof(BoardStructure);
219 RawRunHeader.SoftwareRevision = 0xFFFF;
220 RawRunHeader.Identification = 0xFFFF;
221 RawRunHeader.Type = 0; // Run type: 0=data, 1=pedestal, 3=test
222 RawRunHeader.Events = Number_of_Events_in_File;
223
224 RawRunHeader.RunNumber = runnum;
225 RawRunHeader.FileNumber = 0;
226 memset (RawRunHeader.Description, 0x00, sizeof (RawRunHeader.Description));
227 sprintf(RawRunHeader.Description,"FAD BOARD TEST");
228
229 RawRunHeader.NChips = DPF; // Number of DRS chips per board
230 RawRunHeader.NChannels = CPC; // Number of channels per chip
231 RawRunHeader.Samples = 1024; // Number of samples _only for test_
232 RawRunHeader.Offset= 0; // ??? Offset from trigger
233 RawRunHeader.NBytes = 2; // Bytes per sample
234 RawRunHeader.StartSecond = starttime.tv_sec; // Opening and closing time of the file
235 RawRunHeader.StartMicrosecond = starttime.tv_usec;
236 RawRunHeader.EndSecond = endtime.tv_sec;
237 RawRunHeader.EndMicrosecond = endtime.tv_usec;
238
239
240 for (unsigned int i=0; i < RawRunHeader.NBoards; i++) {
241 rawBoard[i].SerialNo = board_ids[i]; // Board serial number
242 rawBoard[i].NomFreq = 2; // Nominal sampling frequency [GHz]
243 rawBoard[i].BoardTemp = temperature/(4*RawRunHeader.Events); // Board temperature [deg C]
244 rawBoard[i].ScaleFactor = 1/2.048; // Factor for conversion to mV
245 }
246
247
248 // now all necessary information
249 // to write RAW file header is known.
250 // jump back to the beginning and
251 rewind(RAWfile);
252 // write the Raw header
253 write_RAWheader (rawBoard, RawRunHeader, RAWfile);
254
255 if(RawRunHeader.Events > 0){
256 printf("Could not find any events.");
257 return -1;
258 }
259
260 fclose(RAWfile);
261 fclose(faddatafile);
262
263return 0;
264}
265
266
267
268int write_RAWheader (BoardStructure *rawBoard, RunHeader& RawRunHeader, FILE *RAWfile) {
269
270 // write header of rawfile
271 fwrite (&RawRunHeader.MagicNum, (size_t) sizeof(RawRunHeader.MagicNum), 1, RAWfile);
272 fwrite (&RawRunHeader.DataFormat, (size_t) sizeof(RawRunHeader.DataFormat), 1, RAWfile);
273 fwrite (&RawRunHeader.RunHeaderSize, (size_t) sizeof(RawRunHeader.RunHeaderSize), 1, RAWfile);
274 fwrite (&RawRunHeader.EventHeaderSize, (size_t) sizeof(RawRunHeader.EventHeaderSize), 1, RAWfile);
275 fwrite (&RawRunHeader.BoardStructureSize, (size_t) sizeof(RawRunHeader.BoardStructureSize), 1, RAWfile);
276 fwrite (&RawRunHeader.SoftwareRevision, (size_t) sizeof(RawRunHeader.SoftwareRevision), 1, RAWfile);
277 fwrite (&RawRunHeader.Identification, (size_t) sizeof(RawRunHeader.Identification), 1, RAWfile);
278 fwrite (&RawRunHeader.Type, (size_t) sizeof(RawRunHeader.Type), 1, RAWfile);
279 fwrite (&RawRunHeader.RunNumber, (size_t) sizeof(RawRunHeader.RunNumber), 1, RAWfile);
280 fwrite (&RawRunHeader.FileNumber, (size_t) sizeof(RawRunHeader.FileNumber), 1, RAWfile);
281 fwrite (&RawRunHeader.Description, (size_t) sizeof(RawRunHeader.Description), 1, RAWfile);
282 fwrite (&RawRunHeader.NBoards, (size_t) sizeof(RawRunHeader.NBoards), 1, RAWfile);
283 fwrite (&RawRunHeader.NChips, (size_t) sizeof(RawRunHeader.NChips), 1, RAWfile);
284 fwrite (&RawRunHeader.NChannels, (size_t) sizeof(RawRunHeader.NChannels), 1, RAWfile);
285 fwrite (&RawRunHeader.Samples, (size_t) sizeof(RawRunHeader.Samples), 1, RAWfile);
286 fwrite (&RawRunHeader.Offset, (size_t) sizeof(RawRunHeader.Offset), 1, RAWfile);
287 fwrite (&RawRunHeader.Events, (size_t) sizeof(RawRunHeader.Events), 1, RAWfile);
288 fwrite (&RawRunHeader.NBytes, (size_t) sizeof(RawRunHeader.NBytes), 1, RAWfile);
289 fwrite (&RawRunHeader.StartSecond, (size_t) sizeof(RawRunHeader.StartSecond), 1, RAWfile);
290 fwrite (&RawRunHeader.StartMicrosecond, (size_t) sizeof(RawRunHeader.StartMicrosecond), 1, RAWfile);
291 fwrite (&RawRunHeader.EndSecond, (size_t) sizeof(RawRunHeader.EndSecond), 1, RAWfile);
292 fwrite (&RawRunHeader.EndMicrosecond, (size_t) sizeof(RawRunHeader.EndMicrosecond), 1, RAWfile);
293 // write boardstructure to RAW file
294 for (unsigned int i=0; i < RawRunHeader.NBoards; i++) {
295 fwrite (&rawBoard[i].SerialNo, (size_t) sizeof(rawBoard[i].SerialNo), 1, RAWfile);
296 fwrite (&rawBoard[i].NomFreq, (size_t) sizeof(rawBoard[i].NomFreq), 1, RAWfile);
297 fwrite (&rawBoard[i].BoardTemp, (size_t) sizeof(rawBoard[i].BoardTemp), 1, RAWfile);
298 fwrite (&rawBoard[i].ScaleFactor, (size_t) sizeof(rawBoard[i].ScaleFactor), 1, RAWfile);
299 }
300
301 return 0;
302}
Note: See TracBrowser for help on using the repository browser.