dlardaq.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////////
2 //
3 // declarations / primitives / functions to handle raw data from DAQ
4 // ADC resolution is 12bit
5 //
6 // Created: vgalymov, Sat Jul 2 15:25:46 CEST 2016
7 // Modified:
8 //
9 ////////////////////////////////////////////////////////////////////////////////
10 
11 #ifndef __DLARDAQ_H__
12 #define __DLARDAQ_H__
13 
14 #include <exception>
15 #include <vector>
16 #include <ctime>
17 #include <stdint.h>
18 
19 // key for raw data
20 #define EVSKEY 0xFF
21 #define ENDKEY 0xF0
22 
23 // bit check
24 #define SETBYTEBIT(var, pos) ( var |= 1 << pos )
25 #define CLEARBYTEBIT(var, pos) ( var &= ~(1 << pos) )
26 #define CHECKBYTEBIT(var, pos) ( (var) & (1<<pos) )
27 
28 // flag bit for compression
29 #define DCBITFLAG 0x6 // 0x0 LSB -> 0x7 MSB
30 #define GETDCFLAG(info) (CHECKBYTEBIT(info, DCBITFLAG)>0)
31 #define SETDCFLAG(info) (SETBYTEBIT(info, DCBITFLAG))
32 
33 // event data quality flag
34 #define EVDQFLAG(info) ( (info & 0x3F ) == 0 )
35 
36 ///
37 namespace dlardaq
38 {
39  typedef char BYTE;
40  typedef uint16_t adc16_t;
41 
42  //
43  // formatting exception
45  {
46  virtual const char* what() const throw()
47  {
48  return "Bad file format";
49  }
50  };
51 
53 
54  // specify sizes of different headers
55  // run header in bytes
56  static const size_t RunHeadSz = 5;
57  // event header in bytes
58  static const size_t EveHeadSz = 35;
59  // run footer in bytes
60  static const size_t FileFootSz = 4;
61 
62  // adc bits
63  static const short BitsADC = 12;
64 
65  // trigger structure from WR trig handler
66  typedef struct trigger_t
67  {
68  uint8_t type;
69  uint32_t num;
70  struct timespec ts; //{ time_t ts.tv_sec, long tv_nsec }
71  } trigger_t;
72 
73  // structure to hold decoded run header
74  typedef struct runheader_t
75  {
76  uint32_t run_num;
77  uint8_t run_flags;
78  } runheader_t;
79 
80  // strucutre to hold decoded event header
81  typedef struct evheader_t
82  {
83  trigger_t trig_info; // trigger info
84  uint8_t dq_flag; // data quality flag
85  uint32_t ev_num; // event number
86  uint32_t ev_size; // size of event in bytes
87  } evheader_t;
88 
89  // structure to hold footer information
90  typedef struct footer_t
91  {
92  uint16_t num_events;
93  } footer_t;
94 
95  // pack 16 bits 12 bits
96  void pack16into12(const void *in, void *out, size_t n);
97 
98  // unpack 12 bits into 16 bits
99  void unpack12into16(const void *in, void *out, size_t n);
100  // pack 16 bits 12 bit
101 
102  // write binary file with 12 bit words
103  void write12(const char *fname, std::vector<adc16_t> &adc);
104  // read binary file from 12 bit words
105  void read12(const char *fname, std::vector<adc16_t> &adc);
106 
107 
108  // get byte content for a given data type
109  // NOTE: cast assumes host byte order
110  template<typename T> T ConvertToValue(const void *in)
111  {
112  const T *ptr = static_cast<const T*>(in);
113  return *ptr;
114  }
115 
116  // functions to decode header informations from binary stream
117  ssize_t decode_runhead(const char* buf, runheader_t &rh);
118  ssize_t decode_evehead(const char* buf, evheader_t &eh);
119  ssize_t decode_filefoot(const char* buf, footer_t &rf);
120 }
121 
122 #endif
uint16_t adc16_t
Definition: dlardaq.h:40
struct dlardaq::trigger_t trigger_t
static const size_t FileFootSz
Definition: dlardaq.h:60
trigger_t trig_info
Definition: dlardaq.h:83
uint32_t ev_num
Definition: dlardaq.h:85
uint8_t type
Definition: dlardaq.h:68
virtual const char * what() const
Definition: dlardaq.h:46
int16_t adc
Definition: CRTFragment.hh:202
ssize_t decode_evehead(const char *buf, evheader_t &eh)
ssize_t decode_runhead(const char *buf, runheader_t &rh)
static const size_t RunHeadSz
Definition: dlardaq.h:56
uint8_t dq_flag
Definition: dlardaq.h:84
static const size_t EveHeadSz
Definition: dlardaq.h:58
struct dlardaq::footer_t footer_t
std::void_t< T > n
char BYTE
Definition: dlardaq.h:39
uint32_t ev_size
Definition: dlardaq.h:86
ssize_t decode_filefoot(const char *buf, footer_t &rf)
uint32_t num
Definition: dlardaq.h:69
static const short BitsADC
Definition: dlardaq.h:63
struct dlardaq::runheader_t runheader_t
struct dlardaq::evheader_t evheader_t
T ConvertToValue(const void *in)
Definition: dlardaq.h:110
void unpack12into16(const void *in, void *out, size_t n)
void pack16into12(const void *in, void *out, size_t n)
void read12(const char *fname, std::vector< adc16_t > &adc)
uint16_t num_events
Definition: dlardaq.h:92
void write12(const char *fname, std::vector< adc16_t > &adc)
uint8_t run_flags
Definition: dlardaq.h:77
static formatexception fex
Definition: dlardaq.h:52
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
uint32_t run_num
Definition: dlardaq.h:76