| 1 | /*********************************************************************************************\ | 
|---|
| 2 | former Version of VIEWEVENT ist now called printdatay | 
|---|
| 3 |  | 
|---|
| 4 | print content of FAD raw-data in an Excel readable format :-) | 
|---|
| 5 | dn, 08.06.10 | 
|---|
| 6 |  | 
|---|
| 7 | should work with data format version no. 0x0100, 0x0101 and 0x0102 | 
|---|
| 8 |  | 
|---|
| 9 | all 16 and 32 bit values must be converted from big to little endian!!! (ntohs (), ntohl ()) | 
|---|
| 10 |  | 
|---|
| 11 | kw, 05.10 | 
|---|
| 12 |  | 
|---|
| 13 | \*********************************************************************************************/ | 
|---|
| 14 | #ifndef PATH_MAX | 
|---|
| 15 | #define PATH_MAX 1000 | 
|---|
| 16 | #endif | 
|---|
| 17 | #include <stdlib.h> | 
|---|
| 18 | #include <stdio.h> | 
|---|
| 19 | #include <string.h> | 
|---|
| 20 | #include <getopt.h> | 
|---|
| 21 | #include <arpa/inet.h> | 
|---|
| 22 |  | 
|---|
| 23 |  | 
|---|
| 24 | #include "printdata.h" | 
|---|
| 25 |  | 
|---|
| 26 | int main(int argc, char **argv) | 
|---|
| 27 | { | 
|---|
| 28 | int opt, evnt_cnt; | 
|---|
| 29 | int max_roi=0; | 
|---|
| 30 |  | 
|---|
| 31 | // static array to store data, for printing coloumn wise. | 
|---|
| 32 | unsigned short evnt_data[36][1024]; | 
|---|
| 33 | for (int col=0; col<1024; col++) | 
|---|
| 34 | { | 
|---|
| 35 | for (int row=0; row<36; row++) | 
|---|
| 36 | { | 
|---|
| 37 | evnt_data[row][col]=0; | 
|---|
| 38 | } | 
|---|
| 39 | } | 
|---|
| 40 | unsigned short ch_id[36]; | 
|---|
| 41 | unsigned short ch_start_cell[36]; | 
|---|
| 42 | unsigned short ch_roi[36]; | 
|---|
| 43 |  | 
|---|
| 44 | const char *optstring = "h"; | 
|---|
| 45 | static struct option long_options[] = | 
|---|
| 46 | { | 
|---|
| 47 | {"no-data", 0, 0, 1}, | 
|---|
| 48 | {"dec",0,0,2}, | 
|---|
| 49 | {"offsbin",0,0,3}, | 
|---|
| 50 | {"twoscompl",0,0,4}, | 
|---|
| 51 | {0, 0, 0, 0} | 
|---|
| 52 | }; | 
|---|
| 53 |  | 
|---|
| 54 | char fname[PATH_MAX]; | 
|---|
| 55 | int print_data = 1;     //1 | 
|---|
| 56 | int print_hex=1; | 
|---|
| 57 | int print_dec=0;        //2 | 
|---|
| 58 | int print_offsbin=0;    //3 | 
|---|
| 59 | int print_twoscompl=0;  //4 | 
|---|
| 60 |  | 
|---|
| 61 | if (argc <= 1) | 
|---|
| 62 | { | 
|---|
| 63 | usage (argv[0]); | 
|---|
| 64 | exit (1); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | while ((opt = getopt_long (argc, argv, optstring, long_options, 0)) != -1) | 
|---|
| 68 | { | 
|---|
| 69 | switch (opt) | 
|---|
| 70 | { | 
|---|
| 71 | // no channel data | 
|---|
| 72 | case 1: | 
|---|
| 73 | print_data = 0; | 
|---|
| 74 | break; | 
|---|
| 75 | case 2: | 
|---|
| 76 | print_hex = 0; | 
|---|
| 77 | print_dec=1; | 
|---|
| 78 | break; | 
|---|
| 79 | case 3: | 
|---|
| 80 | print_hex = 0; | 
|---|
| 81 | print_offsbin=1; | 
|---|
| 82 | break; | 
|---|
| 83 | case 4: | 
|---|
| 84 | print_hex = 0; | 
|---|
| 85 | print_twoscompl=1; | 
|---|
| 86 | break; | 
|---|
| 87 | default: | 
|---|
| 88 | usage (argv[0]); | 
|---|
| 89 | exit (1); | 
|---|
| 90 | break; | 
|---|
| 91 | } | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | if (optind < argc) | 
|---|
| 95 | { | 
|---|
| 96 | strncpy (fname, argv[optind], PATH_MAX); | 
|---|
| 97 | } | 
|---|
| 98 | else | 
|---|
| 99 | { | 
|---|
| 100 | usage (argv[0]); | 
|---|
| 101 | exit (1); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | FILE *fhandle; | 
|---|
| 105 | size_t fresult; | 
|---|
| 106 |  | 
|---|
| 107 | EVNT *evnt; | 
|---|
| 108 |  | 
|---|
| 109 | // open input file | 
|---|
| 110 | if ((fhandle = fopen (fname, "r")) == NULL) | 
|---|
| 111 | { | 
|---|
| 112 | // Oops... | 
|---|
| 113 | printf ("Error: File %s not found\n", fname); | 
|---|
| 114 | exit (1); | 
|---|
| 115 | } | 
|---|
| 116 | else | 
|---|
| 117 | { | 
|---|
| 118 | for (evnt_cnt = 0;; evnt_cnt++) | 
|---|
| 119 | { | 
|---|
| 120 | // allocate memory for evnt structure | 
|---|
| 121 | evnt =( EVNT*) calloc (1, sizeof (EVNT)); | 
|---|
| 122 |  | 
|---|
| 123 | // read and print package header | 
|---|
| 124 | if ((fresult = fread (&evnt->evnt_header.start_package_flag, sizeof (evnt->evnt_header.start_package_flag), 1, fhandle)) == 1) | 
|---|
| 125 | { | 
|---|
| 126 | printf ("--------------\nEvent No.: %d\n--------------\n", evnt_cnt); | 
|---|
| 127 |  | 
|---|
| 128 | printf ("Start Package Flag: 0x%.4X\n", ntohs (evnt->evnt_header.start_package_flag)); | 
|---|
| 129 |  | 
|---|
| 130 | fread (&evnt->evnt_header.package_length, sizeof (evnt->evnt_header.package_length), 1, fhandle); | 
|---|
| 131 | printf ("Package Length: 0x%.4X\n", ntohs (evnt->evnt_header.package_length)); | 
|---|
| 132 |  | 
|---|
| 133 | fread (&evnt->evnt_header.version_no, sizeof (evnt->evnt_header.version_no), 1, fhandle); | 
|---|
| 134 | printf ("Version Number: 0x%.4X\n", ntohs (evnt->evnt_header.version_no)); | 
|---|
| 135 |  | 
|---|
| 136 | fread (&evnt->evnt_header.trigger_id, sizeof (evnt->evnt_header.trigger_id), 1, fhandle); | 
|---|
| 137 | printf ("Trigger-ID: 0x%.8X\n", ntohl (evnt->evnt_header.trigger_id)); | 
|---|
| 138 |  | 
|---|
| 139 | fread (&evnt->evnt_header.trigger_type, sizeof (evnt->evnt_header.trigger_type), 1, fhandle); | 
|---|
| 140 | printf ("Trigger Type: 0x%.2X\n", evnt->evnt_header.trigger_type); | 
|---|
| 141 |  | 
|---|
| 142 | fread (&evnt->evnt_header.trigger_crc, sizeof (evnt->evnt_header.trigger_crc), 1, fhandle); | 
|---|
| 143 | printf ("Trigger CRC: 0x%.2X\n", evnt->evnt_header.trigger_crc); | 
|---|
| 144 |  | 
|---|
| 145 | fread (&evnt->evnt_header.local_trigger_id, sizeof (evnt->evnt_header.local_trigger_id), 1, fhandle); | 
|---|
| 146 | printf ("Local Trigger-ID: 0x%.8X\n", ntohl (evnt->evnt_header.local_trigger_id)); | 
|---|
| 147 |  | 
|---|
| 148 | fread (&evnt->evnt_header.local_trigger_type, sizeof (evnt->evnt_header.local_trigger_type), 1, fhandle); | 
|---|
| 149 | printf ("Local Trigger Type: 0x%.2X\n", evnt->evnt_header.local_trigger_type); | 
|---|
| 150 |  | 
|---|
| 151 | fread (&evnt->evnt_header.local_trigger_crc, sizeof (evnt->evnt_header.local_trigger_crc), 1, fhandle); | 
|---|
| 152 | printf ("Local Trigger CRC: 0x%.2X\n", evnt->evnt_header.local_trigger_crc); | 
|---|
| 153 |  | 
|---|
| 154 | fread (&evnt->evnt_header.board_id, sizeof (evnt->evnt_header.board_id), 1, fhandle); | 
|---|
| 155 | printf ("Board-ID: 0x%.4X\n", ntohs (evnt->evnt_header.board_id)); | 
|---|
| 156 |  | 
|---|
| 157 | for (int i = 0; i < 4; i++) | 
|---|
| 158 | { | 
|---|
| 159 | fread (&evnt->evnt_header.drs_temperature[i], sizeof (evnt->evnt_header.drs_temperature[i]), 1, fhandle); | 
|---|
| 160 | if ((ntohs (evnt->evnt_header.drs_temperature[i]) & 0x8000) == 0x8000) | 
|---|
| 161 | { | 
|---|
| 162 | printf ("Temperature %d: %d\n", i, 0xE000 | (ntohs (evnt->evnt_header.drs_temperature[i])) >> 3); | 
|---|
| 163 | } | 
|---|
| 164 | else | 
|---|
| 165 | { | 
|---|
| 166 | printf ("Temperature %d: %d\n", i, ntohs (evnt->evnt_header.drs_temperature[i]) >> 3); | 
|---|
| 167 | } | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | // DAC Values, only if version > 0x0101 | 
|---|
| 171 | if (ntohs (evnt->evnt_header.version_no) > 0x0101) | 
|---|
| 172 | { | 
|---|
| 173 | for (int i = 0; i < 8; i++) | 
|---|
| 174 | { | 
|---|
| 175 | fread (&evnt->evnt_header.dac[i], sizeof (evnt->evnt_header.dac[i]), 1, fhandle); | 
|---|
| 176 | printf ("DAC %d: %d\n", i, ntohs (evnt->evnt_header.dac[i])); | 
|---|
| 177 | } | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | // read channels, no error checking... | 
|---|
| 181 | for (int i = 0; i < (4 * 9); i++) | 
|---|
| 182 | { | 
|---|
| 183 | // read channel header | 
|---|
| 184 | fread (&ch_id[i], sizeof (evnt->channel[i].channel_id), 1, fhandle); | 
|---|
| 185 | fread (&ch_start_cell[i], sizeof (evnt->channel[i].channel_start_cell), 1, fhandle); | 
|---|
| 186 | fread (&ch_roi[i], sizeof (evnt->channel[i].channel_roi), 1, fhandle); | 
|---|
| 187 |  | 
|---|
| 188 |  | 
|---|
| 189 | if (max_roi < ntohs (ch_roi[i])) | 
|---|
| 190 | {max_roi = ntohs (ch_roi[i]);} | 
|---|
| 191 |  | 
|---|
| 192 | fread ((evnt_data[i]), sizeof (unsigned short), ntohs (ch_roi[i]), fhandle); | 
|---|
| 193 |  | 
|---|
| 194 |  | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | // loop over event_data to adjust endianess! | 
|---|
| 198 | for (int row=0; row<36; row++) | 
|---|
| 199 | { | 
|---|
| 200 | ch_id[row]=ntohs(ch_id[row]); | 
|---|
| 201 | ch_start_cell[row]=ntohs(ch_start_cell[row]); | 
|---|
| 202 | ch_roi[row]=ntohs(ch_roi[row]); | 
|---|
| 203 | for (int col=0; col<1024; col++) | 
|---|
| 204 | { | 
|---|
| 205 | evnt_data[row][col] = ntohs(evnt_data[row][col]); | 
|---|
| 206 | } | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | printf("Channel IDs:\n"); | 
|---|
| 210 | for (int ch=0; ch<36; ch++){ | 
|---|
| 211 | printf("0x%.2X ",ch_id[ch]); | 
|---|
| 212 | } | 
|---|
| 213 | printf("\n"); | 
|---|
| 214 |  | 
|---|
| 215 | printf("Channel Start Cells:\n"); | 
|---|
| 216 | for (int ch=0; ch<36; ch++){ | 
|---|
| 217 | printf("%4d ",ch_start_cell[ch]); | 
|---|
| 218 | } | 
|---|
| 219 | printf("\n"); | 
|---|
| 220 |  | 
|---|
| 221 | printf("Channel ROIs:\n"); | 
|---|
| 222 | for (int ch=0; ch<36; ch++){ | 
|---|
| 223 | printf("%4d ",ch_roi[ch]); | 
|---|
| 224 | } | 
|---|
| 225 | printf("\n"); | 
|---|
| 226 | printf ("maximal ROI was %d \n\n", max_roi); | 
|---|
| 227 |  | 
|---|
| 228 | // print channel data in hex format | 
|---|
| 229 | if (print_data && print_hex) | 
|---|
| 230 | { | 
|---|
| 231 | for (int col = 0; col < max_roi; col++) | 
|---|
| 232 | { | 
|---|
| 233 | for (int row = 0; row < 36; row++) | 
|---|
| 234 | { | 
|---|
| 235 | printf ("0x%.4X ", evnt_data[row][col] ); | 
|---|
| 236 | } | 
|---|
| 237 | printf ("\n"); | 
|---|
| 238 | } | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 |  | 
|---|
| 242 | // print channel data in decimal format | 
|---|
| 243 | if (print_data && print_dec) | 
|---|
| 244 | { | 
|---|
| 245 | for (int col = 0; col < max_roi; col++) | 
|---|
| 246 | { | 
|---|
| 247 | for (int row = 0; row < 36; row++) | 
|---|
| 248 | { | 
|---|
| 249 | printf ("%.4d ", evnt_data[row][col] ); | 
|---|
| 250 | } | 
|---|
| 251 | printf ("\n"); | 
|---|
| 252 | } | 
|---|
| 253 | } | 
|---|
| 254 |  | 
|---|
| 255 | // print channel data in ad9238 offset binary format | 
|---|
| 256 | // only the first lowest 13bits should contain data, | 
|---|
| 257 | // if this is not true and some data 1s sit in the upper 3positions | 
|---|
| 258 | // strange things might happen | 
|---|
| 259 | if (print_data && print_offsbin) | 
|---|
| 260 | { | 
|---|
| 261 | short sd; // just a dummy to calculate signed adc value | 
|---|
| 262 | for (int col = 0; col < max_roi; col++) | 
|---|
| 263 | { | 
|---|
| 264 | for (int row = 0; row < 36; row++) | 
|---|
| 265 | { | 
|---|
| 266 | // the overflow bit is the 13th bit (when you say LSB = 1st bit) | 
|---|
| 267 | // to shift a 1 to this position one has to use (1<<12), not (1<<13) | 
|---|
| 268 | // 1<<0 is 0x0001 | 
|---|
| 269 | // 1<<1 is 0x0002 | 
|---|
| 270 | // 1<<12 is 0x1000 or 0001.0000.0000.0000 | 
|---|
| 271 | if ((evnt_data[row][col] & (1<<12))) // the overflow bit is set | 
|---|
| 272 | { | 
|---|
| 273 | if ((evnt_data[row][col] & ~(1<<12)) > 0 ) // this was an OVERflow | 
|---|
| 274 | { | 
|---|
| 275 | sd = 0x7FFF; // dec=32767 highest possible value of signed short! | 
|---|
| 276 | }else { // this was an UNDERflow | 
|---|
| 277 | sd = 0x8000; // dec=-32768 lowest possible value of signed short | 
|---|
| 278 | } | 
|---|
| 279 | }else{ // data is okay, no overflow or underflow | 
|---|
| 280 | // since ADC data format is 12bit offset binary | 
|---|
| 281 | // I shift the numbers from 0..4095 to -2048 .. +2047 | 
|---|
| 282 | sd = evnt_data[row][col] - (1<<11); | 
|---|
| 283 | } | 
|---|
| 284 | printf ("%.4d ", sd ); | 
|---|
| 285 | } | 
|---|
| 286 | printf ("\n"); | 
|---|
| 287 | } | 
|---|
| 288 | } | 
|---|
| 289 |  | 
|---|
| 290 | // print channel data in ad9238 in twos complement format | 
|---|
| 291 | // only the first lowest 13bits should contain data, | 
|---|
| 292 | // if this is not true and some data 1s sit in the upper 3positions | 
|---|
| 293 | // strange things might happen | 
|---|
| 294 | if (print_data && print_twoscompl) | 
|---|
| 295 | { | 
|---|
| 296 | short sd; // just a dummy to calculate signed adc value | 
|---|
| 297 | for (int col = 0; col < max_roi; col++) | 
|---|
| 298 | { | 
|---|
| 299 | for (int row = 0; row < 36; row++) | 
|---|
| 300 | { | 
|---|
| 301 | // the overflow bit is the 13th bit (when you say LSB = 1st bit) | 
|---|
| 302 | // to shift a 1 to this position one has to use (1<<12), not (1<<13) | 
|---|
| 303 | // 1<<0 is 0x0001 | 
|---|
| 304 | // 1<<1 is 0x0002 | 
|---|
| 305 | // 1<<12 is 0x1000 or 0001.0000.0000.0000 | 
|---|
| 306 | if ((evnt_data[row][col] & (1<<12))) // the overflow bit is set | 
|---|
| 307 | { | 
|---|
| 308 | if ((evnt_data[row][col] & ~(1<<12)) > 0 ) // this was an OVERflow | 
|---|
| 309 | { | 
|---|
| 310 | sd = 0x7FFF; // dec=32767 highest possible value of signed short! | 
|---|
| 311 | }else { // this was an UNDERflow | 
|---|
| 312 | sd = 0x8000; // dec=-32768 lowest possible value of signed short | 
|---|
| 313 | } | 
|---|
| 314 | }else{ // data is okay, no overflow or underflow | 
|---|
| 315 | // AD9238 twos complement | 
|---|
| 316 | // positive numbers will look like: 0000.0xxx.xxxx.xxxx | 
|---|
| 317 | // negative numbers will look like: 0000.1xxx.xxxx.xxxx | 
|---|
| 318 | // the bit shift operators are smart for signed integer data types. | 
|---|
| 319 | // shifting back and forth should do the trick. | 
|---|
| 320 |  | 
|---|
| 321 | sd = (evnt_data[row][col]<<4); | 
|---|
| 322 | sd = (sd>>4); | 
|---|
| 323 | } | 
|---|
| 324 | printf ("%.4d ", sd ); | 
|---|
| 325 | } | 
|---|
| 326 | printf ("\n"); | 
|---|
| 327 | } | 
|---|
| 328 | } | 
|---|
| 329 | // CRC, only if version > 0x0100 | 
|---|
| 330 | if (ntohs (evnt->evnt_header.version_no) > 0x0100) | 
|---|
| 331 | { | 
|---|
| 332 | fread (&evnt->package_crc, sizeof (evnt->package_crc), 1, fhandle); | 
|---|
| 333 | printf ("\nPackage CRC: 0x%.4X\n", ntohs (evnt->package_crc)); | 
|---|
| 334 | } | 
|---|
| 335 |  | 
|---|
| 336 | // end package flag | 
|---|
| 337 | fread (&evnt->end_package_flag, sizeof (evnt->end_package_flag), 1, fhandle); | 
|---|
| 338 | printf ("\nEnd Package Flag: 0x%.4X\n\n", ntohs (evnt->end_package_flag)); | 
|---|
| 339 |  | 
|---|
| 340 | } | 
|---|
| 341 | else | 
|---|
| 342 | { | 
|---|
| 343 | // end of file reached ... or something else ... | 
|---|
| 344 | printf ("\nCould not read event no.: %d (end of file?)\n", evnt_cnt); | 
|---|
| 345 | return (1); | 
|---|
| 346 | } | 
|---|
| 347 |  | 
|---|
| 348 | // free memory of evnt structure | 
|---|
| 349 | free (evnt); | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 | // close file | 
|---|
| 353 | fclose (fhandle); | 
|---|
| 354 | } | 
|---|
| 355 |  | 
|---|
| 356 | exit (0); | 
|---|
| 357 | } | 
|---|
| 358 |  | 
|---|
| 359 | void usage(char *argv) | 
|---|
| 360 | { | 
|---|
| 361 | printf ("\nUsage: %s [--no-data] FILE\n", argv); | 
|---|
| 362 | } | 
|---|