ADCM Data Format

Stream Structure

The file contains packets of data with header and payload. Header specifies the type of data block and packet size.

Packet Header

offset

size, bytes

type

name

description

0

2

int

ID

block type

2

2

int

size

packet size, bytes, including header

4

size-4

struct

data

data block, contents defined by type

Data Blocks

ID 0x504D, CMAP

offset

size, bytes

type

name

description

0

4

int

N

number of map entries

4

1

int

map1

map for channel 1

5

1

int

map2

map for channel 2

...

*

1

int

mapN

map for channel N

map bits:

bit

description

0

reserved

1

channel type is Master, γ

2

channel type is Slave, α

3

baseline calibration enabled

7:4

reserved

ID 0x5645, EVNT

offset

size, bytes

type

name

description

0

1

int

N

number of entries

1

1

int

-

reserved

2

2

int

-

reserved

4

4

int

ts

event time-stamp

8

14

struct

pulse1

pulse data block 1

...

*

14

struct

pulseN

pulse data block N

pulse data block structure:

offset

size, bytes

type

name

description

0

1

int

ch

channel number

1

1

int

flags

flags

2

4

float

a

pulse amplitude (integral), baseline subtracted

6

4

float

t

pulse time

10

4

float

w

pulse width

flags:

bit

description

0

reserved

1

channel type is Master, γ

2

channel type is Slave, α

3

baseline calibration enabled

7:4

reserved

ID 0x5443, CNTR

offset

size, bytes

type

name

description

0

4

int

N

number of entries

4

8

double

time

measurement period

12

4

int

cnt1

input pulse count for channel 1

...

*

4

int

cntN

input pulse count for channel N

Decoder Example

adcm_16.h

   1 /*
   2  *
   3  * ADCM-16 hardware settings and registers definition
   4  *
   5  */
   6 
   7 #ifndef ADCM16_H
   8 #define ADCM16_H 1
   9 
  10 #include <sys/types.h>
  11 
  12 #define ADCM_BOARD_NCH 16
  13 
  14 #define ADCM_MIN_N_SAMP 4
  15 #define ADCM_MAX_N_SAMP 254 /* hardware limit is 8192 */
  16 #define ADCM_MIN_FRAGM_LEN ADCM_MIN_N_SAMP
  17 
  18 #define ADCM_N_CH 32 /* maximum number of channels in system */
  19 #define ADC_NCH ADCM_N_CH
  20 //#define ADCM_BITS 10
  21 //#define ADCM_BASELINE (1<<(ADCM_BITS-1))
  22 
  23 #define ADC_REALSTEP 10e-9
  24 
  25 #define ADC_TS_FREQ 1e8         /* timestamp counter clock, Hz */
  26 
  27 //#define ADC_PREWIN_MIN 0
  28 //#define ADC_PREWIN_MAX 31 -- this does not allow baseline reconstruction
  29 //#define ADC_PREWIN_MAX 20
  30 
  31 //#define ADC_MATCHWIN_MIN 1
  32 //#define ADC_MATCHWIN_MAX 63
  33 
  34 //#define ADC_LATENCY_MIN 0
  35 //#define ADC_LATENCY_MAX 30
  36 //#define ADC_LATENCY_OFFSET 0
  37 
  38 #define ADC_MAX_GAIN 4095
  39 #define ADC_DEFAULT_GAIN 700
  40 #define ADC_GAIN_0dB 70
  41 //#define ADC_MAX_DATA ((1<<ADCM_BITS)-1)
  42 //#define ADC_MIN_DATA 0
  43 //#define ADC_MAX_THR ADC_MAX_DATA
  44 //#define ADC_MIN_THR ADC_MIN_DATA
  45 
  46 #define ADCM_TRIG_RUN  0x8000
  47 #define ADCM_TRIG_TEST 0x4000
  48 
  49 #define ADCM_TRIGBIT_EXT1 0x1
  50 #define ADCM_TRIGBIT_EXT2 0x2
  51 //#define ADCM_TRIGBIT_EXT3 0x4
  52 #define ADCM_TRIGBIT_INT  0x8
  53 
  54 #define ADCM_CCTRL_BIT_CMPEN 0x0001
  55 #define ADCM_CCTRL_BIT_S     0x0002
  56 #define ADCM_CCTRL_BIT_M     0x0004
  57 #define ADCM_CCTRL_BIT_TEST  0x0008
  58 #define ADCM_CCTRL_BIT_ZERO  0x0010
  59 #define ADCM_CCTRL_BIT_INV   0x0020
  60 
  61 #endif /* ADCM16_H */
  62 
adcm_16.h

adcm_df.h

   1 #ifndef ADCM_DF_H
   2 #define ADCM_DF_H 1
   3 
   4 // ISO C99
   5 #include <inttypes.h>
   6 #include <sys/types.h>
   7 
   8 #include "adcm16.h"
   9 
  10 #define STOR_ID_CMAP  0x504D    /* 'MP' */
  11 #define STOR_ID_EVNT  0x5645    /* 'EV' */
  12 #define STOR_ID_CNTR  0x5443    /* 'CT' */
  13 
  14 struct stor_packet_hdr_t {
  15   u_int16_t id;                 // data block ID
  16   u_int16_t size;               // block size, bytes
  17 } __attribute__ ((packed));
  18 
  19 struct stor_ev_hdr_t {
  20   u_int8_t np;                  // number of pulses following
  21   u_int8_t reserved1;
  22   u_int16_t reserved2;
  23   u_int32_t ts;                 // timestamp, 10 ns step
  24 } __attribute__ ((packed));
  25 
  26 struct stor_puls_t {
  27   u_int8_t ch;
  28   u_int8_t flags;
  29   float a;
  30   float t;
  31   float w;
  32 } __attribute__ ((packed));
  33 
  34 struct adcm_cmap_t {
  35   u_int32_t n;
  36   u_int8_t map[ADC_NCH];
  37 };
  38 
  39 struct adcm_counters_t {
  40   u_int32_t n;
  41   double time;
  42   u_int32_t rawhits[ADC_NCH];
  43 };
  44 
  45 #endif /* ADCM_DF_H */
  46 
adcm_df.h

df_decode.c

   1 /****************************************************************************
   2  *                                                                          *
   3  * df_decode.c: Sample decode program                                       *
   4  *                                                                          *
   5  * Compile: gcc -Wall -O2 df_decode.c -o df_decode                          *
   6  *                                                                          *
   7  ****************************************************************************/
   8 
   9 
  10 #define _LARGEFILE64_SOURCE
  11 #define _FILE_OFFSET_BITS 64
  12 
  13 #define _GNU_SOURCE
  14 
  15 #include <stdio.h>
  16 #include <stdlib.h>
  17 #include <string.h>
  18 #include <unistd.h>
  19 #include <sys/types.h>
  20 #include <sys/stat.h>
  21 #include <fcntl.h>
  22 #include <errno.h>
  23 #include <time.h>
  24 #include <sys/time.h>
  25 #include <sys/mman.h>
  26 
  27 #include "adcm_df.h"
  28 
  29 
  30 void print_adcm_counters (struct adcm_counters_t * cnt)
  31 {
  32 
  33   int i;
  34 
  35   if (!cnt)
  36     return;
  37   printf ("Counters run time:%e\n", cnt->time);
  38   for (i = 0; i < ADC_NCH; i++)
  39     if (cnt->rawhits[i])
  40       printf ("Hits [%2d]: %10u\n", i, cnt->rawhits[i]);
  41 }
  42 
  43 /*****************************************************************************/
  44 int process_raw_buf (unsigned char *buf, size_t len)
  45 {
  46 
  47   size_t p = 0, dp = 0;
  48   struct adcm_cmap_t cmap;
  49   struct adcm_counters_t counters;
  50   int nev = 0, nmap = 0, ncnt = 0;
  51 
  52   memset (&counters, 0, sizeof (struct adcm_counters_t));
  53   memset (&cmap, 0, sizeof (struct adcm_cmap_t));
  54 
  55   while (((dp = p + sizeof (struct stor_packet_hdr_t)) < len)) {
  56 
  57     struct stor_packet_hdr_t *hdr = (struct stor_packet_hdr_t *) & buf[p];
  58 
  59     switch (hdr->id) {
  60     case STOR_ID_CMAP:
  61       nmap++;
  62       memcpy (&cmap, &buf[dp], sizeof (struct adcm_cmap_t));
  63       break;
  64     case STOR_ID_EVNT:{
  65         struct stor_ev_hdr_t *eh = (struct stor_ev_hdr_t *) & buf[dp];
  66         int n;
  67 //        float t1 = 0, t2 = 0;
  68         nev++;
  69         printf ("TS %08X\n", eh->ts);
  70         for (n = 0; n < eh->np; n++) {
  71           struct stor_puls_t *hit = (struct stor_puls_t *) & buf[dp + sizeof (struct stor_ev_hdr_t) + n * sizeof (struct stor_puls_t)];
  72           printf ("%d\t%f\t%f\t%f\n", hit->ch, hit->a, hit->t, hit->w);
  73 /*
  74           if (hit->ch == 0)
  75             t1 = hit->t;
  76           if (hit->ch == 1)
  77             t2 = hit->t;
  78 */
  79         }
  80 /*
  81         if ((t1 > 0) && (t2 > 0))
  82           printf ("%f\n", t2 - t1);
  83 */
  84         break;
  85       }
  86     case STOR_ID_CNTR:
  87       ncnt++;
  88       memcpy (&counters, &buf[dp], sizeof (struct adcm_counters_t));
  89       break;
  90     default:
  91       fprintf (stderr, "Bad block ID %04X at pos %zu\n", hdr->id, p);
  92       exit (1);
  93     }
  94     p += hdr->size;
  95   }
  96   fprintf (stderr, "Complete %zu bytes, %u events, %u counters, %u maps\n", p, nev, ncnt, nmap);
  97   print_adcm_counters (&counters);
  98   return (0);
  99 }
 100 
 101 /*****************************************************************************/
 102 
 103 int main (int argc, char *argv[])
 104 {
 105   int fd;
 106   struct stat statbuf;
 107   size_t maplen, maxmaplen;
 108   off_t nb, mapoff;
 109   void *mapaddr;
 110   double partdone;
 111   char dumpfilename[1024];
 112 
 113 
 114   if (argc == 2) {
 115     strncpy (dumpfilename, argv[1], 1023);
 116   } else {
 117     fprintf (stderr, "USAGE: %s datafile\n", argv[0]);
 118     exit (1);
 119   }
 120 
 121   if ((stat (dumpfilename, &statbuf))) {
 122     fprintf (stderr, "stat('%s') failed: %s\n", dumpfilename, strerror (errno));
 123     exit (1);
 124   }
 125   fd = open (dumpfilename, O_RDONLY);
 126   if (fd < 0) {
 127     fprintf (stderr, "open('%s') failed: %s\n", dumpfilename, strerror (errno));
 128     exit (1);
 129   }
 130   nb = statbuf.st_size;
 131 
 132   mapoff = 0;
 133 // maxmaplen = 1073741824L; // 1G
 134   maxmaplen = 10485760L;        // 10M
 135   while (mapoff < nb) {
 136     maplen = nb - mapoff;
 137     if (maplen > maxmaplen)
 138       maplen = maxmaplen;
 139     mapaddr = mmap (0, maplen, PROT_READ, MAP_SHARED, fd, mapoff);
 140     if (mapaddr == MAP_FAILED) {
 141       fprintf (stderr, "mmap('%s') failed: %s\n", dumpfilename, strerror (errno));
 142       exit (1);
 143     }
 144     printf ("Mapped %zu bytes from %lld to addr %p\n", maplen, mapoff, mapaddr);
 145     process_raw_buf ((unsigned char *)mapaddr, maplen);
 146     munmap (mapaddr, maplen);
 147     mapoff += maplen;
 148     partdone = 1. * mapoff / nb;
 149     printf ("%lld MB done\n", mapoff / 1048576L);
 150 //break;
 151   }
 152   close (fd);
 153   return 0;
 154 }
df_decode.c


ADCM RawData

AdcmDataFormat (last edited 2011-05-20 07:42:41 by slepnev)