Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
dlardaq::EventDecoder Class Reference

#include <EventDecoder.h>

Public Member Functions

 EventDecoder (size_t nch, size_t nsample)
 
 ~EventDecoder ()
 
void SetNCh (size_t val)
 
void SetNSample (size_t val)
 
size_t GetNCh () const
 
size_t GetNSample () const
 
ssize_t Open (std::string finname)
 
void Close ()
 
ssize_t GetEvent (size_t evnum, dlardaq::evheader_t &eh, std::vector< adc16_t > &adc)
 
ssize_t GetEvent (dlardaq::evheader_t &eh, std::vector< adc16_t > &adc)
 
void ReadBuffer (const char *buf, size_t nb)
 
bool Compressed () const
 
size_t GetTotEvents () const
 
runheader_t GetRunHeader ()
 
evheader_t GetEventHeader ()
 
footer_t GetFileFooter ()
 

Public Attributes

std::ifstream m_file
 

Protected Member Functions

int lock (pthread_mutex_t &mutex)
 
int trylock (pthread_mutex_t &mutex)
 
int unlock (pthread_mutex_t &mutex)
 
void ReadEvent (std::vector< adc16_t > &adc, bool headonly=false)
 
void ReadBytes (std::vector< BYTE > &bytes)
 
ssize_t Decode (const char *buf, size_t nb, bool cflag, std::vector< adc16_t > &adc)
 
bool IsFirstPacket (const char *buf, size_t nb)
 

Protected Attributes

int m_Verbosity
 
std::vector< BYTEm_RunHeadBuf
 
std::vector< BYTEm_EveHeadBuf
 
std::vector< BYTEm_EndFootBuf
 
std::vector< BYTEm_EveDataBuf
 
std::vector< adc16_tm_EveData
 
ssize_t m_bytes_left
 
runheader_t m_rnh
 
evheader_t m_evh
 
footer_t m_flf
 
std::streampos m_pstart
 
std::streampos m_pend
 
std::vector< std::streampos > m_events
 
size_t m_totev
 
short m_nadc
 
size_t m_nch
 
size_t m_nsample
 
bool m_dcflag
 
pthread_mutex_t m_data_mutex
 

Detailed Description

Definition at line 25 of file EventDecoder.h.

Constructor & Destructor Documentation

EventDecoder::EventDecoder ( size_t  nch,
size_t  nsample 
)

Definition at line 27 of file EventDecoder_service.cc.

28 {
29  // this is needed to read compressed data
30  m_nadc = dlardaq::BitsADC; // bits for ADC resolution
31  m_nch = nch; // total number of channels
32  m_nsample = nsample; // number of samples in each channel
33 
37 
38  //
39  m_bytes_left = 0;
40 
41  // data mutex initialization
42  pthread_mutex_init(&m_data_mutex, NULL);
43 }
pthread_mutex_t m_data_mutex
Definition: EventDecoder.h:118
static const size_t FileFootSz
Definition: dlardaq.h:60
std::vector< BYTE > m_EndFootBuf
Definition: EventDecoder.h:87
static const size_t RunHeadSz
Definition: dlardaq.h:56
static const size_t EveHeadSz
Definition: dlardaq.h:58
static const short BitsADC
Definition: dlardaq.h:63
std::vector< BYTE > m_EveHeadBuf
Definition: EventDecoder.h:86
std::vector< BYTE > m_RunHeadBuf
Definition: EventDecoder.h:85
EventDecoder::~EventDecoder ( )

Definition at line 49 of file EventDecoder_service.cc.

50 {
51  Close();
52  pthread_mutex_destroy(&m_data_mutex);
53 }
pthread_mutex_t m_data_mutex
Definition: EventDecoder.h:118

Member Function Documentation

void EventDecoder::Close ( )

Definition at line 59 of file EventDecoder_service.cc.

60 {
61  // lock mutex
62  lock( m_data_mutex );
63 
64  if(m_file.is_open())
65  m_file.close();
66 
67  m_totev = 0;
68 
69  // unlock mutex
71 }
pthread_mutex_t m_data_mutex
Definition: EventDecoder.h:118
int lock(pthread_mutex_t &mutex)
Definition: EventDecoder.h:67
std::ifstream m_file
Definition: EventDecoder.h:64
int unlock(pthread_mutex_t &mutex)
Definition: EventDecoder.h:69
bool dlardaq::EventDecoder::Compressed ( ) const
inline

Definition at line 55 of file EventDecoder.h.

55 { return m_dcflag; }
ssize_t EventDecoder::Decode ( const char *  buf,
size_t  nb,
bool  cflag,
std::vector< adc16_t > &  adc 
)
protected

Definition at line 261 of file EventDecoder_service.cc.

263 {
264  adc.clear();
265 
266  if( !cflag ) //not compressed
267  {
268  adc.resize( (nb*8)/m_nadc, 0 );
269  unpack12into16( buf, &adc[0], nb );
270  }
271  else
272  {
273  size_t byteidx = 0;
275 
276  }
277 
278  return adc.size();
279 }
static constexpr double nb
Definition: Units.h:81
static HuffDataCompressor & Instance()
void DecompressEventData(short nbadc, size_t nch, size_t seqlen, const char *buf, size_t bufsize, size_t &byteidx, std::vector< adc16_t > &adc)
void unpack12into16(const void *in, void *out, size_t n)
ssize_t EventDecoder::GetEvent ( size_t  evnum,
dlardaq::evheader_t eh,
std::vector< adc16_t > &  adc 
)

Definition at line 185 of file EventDecoder_service.cc.

187 {
188  adc.clear();
189  if(!m_file.is_open()) return -1;
190  //if(evnum >= m_totev) return -1; //no-can-do
192 
193  if(evnum < m_events.size()) // already know where it is
194  {
195  m_file.seekg(m_events[evnum]);
196  ReadEvent( adc );
197  eh = m_evh;
198  }
199  else
200  {
201  size_t curev = m_events.size();
202  while( curev <= evnum )
203  {
204  // our event begins at this position
205  streampos pos = m_file.tellg();
206  m_events.push_back( pos );
207 
208  // readonly header to get event data size
209  if( curev < evnum )
210  ReadEvent(adc, true);
211  else // event we want
212  ReadEvent(adc, false);
213 
214  curev++;
215  }
216 
217  // last event is the one with want ...
218  eh = m_evh;
219  }
220 
222 
223  return evnum;
224 }
pthread_mutex_t m_data_mutex
Definition: EventDecoder.h:118
std::vector< std::streampos > m_events
Definition: EventDecoder.h:104
int lock(pthread_mutex_t &mutex)
Definition: EventDecoder.h:67
std::ifstream m_file
Definition: EventDecoder.h:64
void ReadEvent(std::vector< adc16_t > &adc, bool headonly=false)
int unlock(pthread_mutex_t &mutex)
Definition: EventDecoder.h:69
ssize_t EventDecoder::GetEvent ( dlardaq::evheader_t eh,
std::vector< adc16_t > &  adc 
)

Definition at line 229 of file EventDecoder_service.cc.

231 {
232  if(m_file.is_open())
233  {
234  // return first event
235  return GetEvent( 0, eh, adc);
236  }
237 
238  ssize_t rval = -1;
239  //else // we have an event buffer
240  if( m_EveData.empty() ) return -1;
241 
242  // lock mutex
244 
245  eh = m_evh;
246  adc = m_EveData;
247 
248  // clear the internal data buffer
249  m_EveData.clear();
250 
251  rval = eh.ev_num;
252 
253  // unlock mutex
255  return rval;
256 }
pthread_mutex_t m_data_mutex
Definition: EventDecoder.h:118
uint32_t ev_num
Definition: dlardaq.h:85
int lock(pthread_mutex_t &mutex)
Definition: EventDecoder.h:67
std::ifstream m_file
Definition: EventDecoder.h:64
ssize_t GetEvent(size_t evnum, dlardaq::evheader_t &eh, std::vector< adc16_t > &adc)
int unlock(pthread_mutex_t &mutex)
Definition: EventDecoder.h:69
std::vector< adc16_t > m_EveData
Definition: EventDecoder.h:90
evheader_t dlardaq::EventDecoder::GetEventHeader ( )
inline

Definition at line 61 of file EventDecoder.h.

61 { return m_evh; }
footer_t dlardaq::EventDecoder::GetFileFooter ( )
inline

Definition at line 62 of file EventDecoder.h.

62 { return m_flf; }
size_t dlardaq::EventDecoder::GetNCh ( ) const
inline

Definition at line 34 of file EventDecoder.h.

34 { return m_nch; }
size_t dlardaq::EventDecoder::GetNSample ( ) const
inline

Definition at line 35 of file EventDecoder.h.

35 { return m_nsample; }
runheader_t dlardaq::EventDecoder::GetRunHeader ( )
inline

Definition at line 60 of file EventDecoder.h.

60 { return m_rnh; }
size_t dlardaq::EventDecoder::GetTotEvents ( ) const
inline

Definition at line 58 of file EventDecoder.h.

58 { return m_totev; }
bool EventDecoder::IsFirstPacket ( const char *  buf,
size_t  nb 
)
protected

Definition at line 392 of file EventDecoder_service.cc.

393 {
394  BYTE k0 = buf[dlardaq::RunHeadSz];
395  BYTE k1 = buf[dlardaq::RunHeadSz+1];
396  bool rval = ( ((k0 & 0xFF) == EVSKEY) && ((k1 & 0xFF) == EVSKEY) );
397 
398  return rval;
399 }
static const size_t RunHeadSz
Definition: dlardaq.h:56
char BYTE
Definition: dlardaq.h:39
#define EVSKEY
Definition: dlardaq.h:20
int dlardaq::EventDecoder::lock ( pthread_mutex_t &  mutex)
inlineprotected

Definition at line 67 of file EventDecoder.h.

67 { return pthread_mutex_lock(&mutex); }
ssize_t EventDecoder::Open ( std::string  finname)

Definition at line 78 of file EventDecoder_service.cc.

79 {
80  // attempt to close any previously opened files
81  Close();
82 
83  lock( m_data_mutex );
84 
85  m_EveData.clear(); // not used when reading from file
86 
87  // clear event bookmarks
88  m_events.clear();
89 
90  //
91  m_file.open(finname.c_str(), ios::in | ios::binary);
92 
93  if( !m_file.is_open() )
94  {
95  msg_err<<"Could not open "<<finname<<" for reading"<<endl;
97  return -1;
98  }
99 
100  // read run header bytes
103 
104 
105  m_pstart = m_file.tellg();
106 
107  // read footer
108  // fast forward to the end
109  m_file.seekg(-dlardaq::FileFootSz, m_file.end );
110  m_pend = m_file.tellg();
111 
114 
115  // rewind back to the begining
116  m_file.seekg( m_pstart );
117 
118 
119  //
120 
122 
123  if( m_totev == 0 )
124  {
125  msg_err<<"File "<<finname<<" is empty"<<endl;
127  Close();
128  return 0;
129  }
130 
131  unlock( m_data_mutex );
132 
133  return m_totev;
134 }
pthread_mutex_t m_data_mutex
Definition: EventDecoder.h:118
static const size_t FileFootSz
Definition: dlardaq.h:60
std::streampos m_pstart
Definition: EventDecoder.h:102
std::streampos m_pend
Definition: EventDecoder.h:103
std::vector< std::streampos > m_events
Definition: EventDecoder.h:104
std::vector< BYTE > m_EndFootBuf
Definition: EventDecoder.h:87
ssize_t decode_runhead(const char *buf, runheader_t &rh)
int lock(pthread_mutex_t &mutex)
Definition: EventDecoder.h:67
std::ifstream m_file
Definition: EventDecoder.h:64
ssize_t decode_filefoot(const char *buf, footer_t &rf)
void ReadBytes(std::vector< BYTE > &bytes)
int unlock(pthread_mutex_t &mutex)
Definition: EventDecoder.h:69
uint16_t num_events
Definition: dlardaq.h:92
static LogStream msg_err(std::cerr, _strerror)
std::vector< BYTE > m_RunHeadBuf
Definition: EventDecoder.h:85
std::vector< adc16_t > m_EveData
Definition: EventDecoder.h:90
QTextStream & endl(QTextStream &s)
void EventDecoder::ReadBuffer ( const char *  buf,
size_t  nb 
)

Definition at line 286 of file EventDecoder_service.cc.

287 {
288  if(m_file.is_open())
289  {
290  msg_err<<"Cannot use this function while reading data from a file"<<endl;
291  return;
292  }
293 
294  // lock mutex
296  m_totev = 0;
297  if(!m_EveData.empty()) m_EveData.clear();
298 
299  // we have finished reading previous event
300  if( m_bytes_left <= 0 ||
301  Timer::GetTimer().splittime(false, false) > TMAXWAIT)
302  {
303  m_EveDataBuf.clear();
304  if(IsFirstPacket(buf, nb)) //check we have a correct packet
305  {
306  // decode header
307  size_t nb_read = 0;
308  const char *pdata = buf;
309 
310  // decode run header
311  ssize_t ret;
312  ret = dlardaq::decode_runhead(pdata, m_rnh);
313  if( ret <= 0)
314  {
315  // unlock mutex
317  return;
318  }
319  pdata += ret;
320  nb_read += ret;
321 
322  // decode event header
323  ret = dlardaq::decode_evehead(pdata, m_evh);
324  if( ret <= 0)
325  {
326  // unlock mutex
328  return;
329  }
330  pdata += ret;
331  nb_read += ret;
332 
334  if(m_bytes_left <= 0)
335  {
336  // unlock mutex
338  return;
339  }
340 
341  //msg_info<<"Start recieving event "<<m_evh.ev_num<<endl;
342 
343  // copy the data
344  m_EveDataBuf.insert(m_EveDataBuf.end(), pdata, pdata + nb-nb_read);
345  m_bytes_left -= (nb - nb_read);
346 
347  // start timer
349 
350  //
352  return;
353  }
354  else
355  {
357  return;
358  }
359  }
360 
361  // append to buffer
362  m_EveDataBuf.insert(m_EveDataBuf.end(), buf, buf+nb);
363  m_bytes_left -= nb;
364  if( m_bytes_left < 0 )
365  {
366  msg_err<<"Byte packet mismatch"<<endl;
367 
368  // unlock mutex
370  return;
371  }
372 
373  // we have full event
374  if( m_bytes_left == 0 )
375  {
376  // decode data
377  bool cflag = GETDCFLAG(m_evh.dq_flag);
378  Decode( &m_EveDataBuf[0], m_EveDataBuf.size(), cflag, m_EveData );
379  m_totev = 1;
380 
381  msg_info<<"Recieved: run "<<m_rnh.run_num<<" event "<<m_evh.ev_num<<endl;
382  }
383 
384  // unlock mutex
386 }
pthread_mutex_t m_data_mutex
Definition: EventDecoder.h:118
static constexpr double nb
Definition: Units.h:81
uint32_t ev_num
Definition: dlardaq.h:85
ssize_t decode_evehead(const char *buf, evheader_t &eh)
static Timer & GetTimer()
Definition: Timer.h:35
ssize_t decode_runhead(const char *buf, runheader_t &rh)
uint8_t dq_flag
Definition: dlardaq.h:84
std::vector< BYTE > m_EveDataBuf
Definition: EventDecoder.h:88
#define GETDCFLAG(info)
Definition: dlardaq.h:30
int lock(pthread_mutex_t &mutex)
Definition: EventDecoder.h:67
#define TMAXWAIT
Definition: EventDecoder.h:21
std::ifstream m_file
Definition: EventDecoder.h:64
uint32_t ev_size
Definition: dlardaq.h:86
void start()
Definition: Timer.h:41
static LogStream msg_info(std::cout, _strinfo)
int unlock(pthread_mutex_t &mutex)
Definition: EventDecoder.h:69
ssize_t Decode(const char *buf, size_t nb, bool cflag, std::vector< adc16_t > &adc)
static LogStream msg_err(std::cerr, _strerror)
std::vector< adc16_t > m_EveData
Definition: EventDecoder.h:90
bool IsFirstPacket(const char *buf, size_t nb)
QTextStream & endl(QTextStream &s)
uint32_t run_num
Definition: dlardaq.h:76
void EventDecoder::ReadBytes ( std::vector< BYTE > &  bytes)
protected

Definition at line 139 of file EventDecoder_service.cc.

140 {
141  m_file.read( &bytes[0], bytes.size() );
142 }
std::ifstream m_file
Definition: EventDecoder.h:64
void EventDecoder::ReadEvent ( std::vector< adc16_t > &  adc,
bool  headonly = false 
)
protected

Definition at line 148 of file EventDecoder_service.cc.

149 {
150  // first read the header
153  //dlardaq::msg_info<<">>> Event number "<<m_evh.ev_num<<endl;
154  //dlardaq::msg_info<<">>> Event size "<<m_evh.ev_size<<endl;
155  //dlardaq::msg_info<<">>> Trig number "<<m_evh.trig_info.num<<endl;
156  //dlardaq::msg_info<<">>> Time stamp "<<m_evh.trig_info.ts.tv_sec<<" s "
157  //<<m_evh.trig_info.ts.tv_nsec<<" ns"<<endl;
158  //dlardaq::msg_info<<">>> Flags "<<bitset<8>(m_evh.dq_flag)<<endl;
159 
160 
161  if(headonly) // read only header and skip to next event
162  {
163  // get event size
164  size_t evsz = m_evh.ev_size;
165 
166  // move to next event
167  m_file.seekg(evsz, std::ios::cur);
168  return;
169  }
170 
171  //
172  // decode event data
173  if(!m_EveDataBuf.empty()) m_EveDataBuf.clear();
174 
175  m_EveDataBuf.resize(m_evh.ev_size, '\0');
176 
177  // read content of the file
180 }
int16_t adc
Definition: CRTFragment.hh:202
ssize_t decode_evehead(const char *buf, evheader_t &eh)
uint8_t dq_flag
Definition: dlardaq.h:84
std::vector< BYTE > m_EveDataBuf
Definition: EventDecoder.h:88
#define GETDCFLAG(info)
Definition: dlardaq.h:30
std::ifstream m_file
Definition: EventDecoder.h:64
uint32_t ev_size
Definition: dlardaq.h:86
void ReadBytes(std::vector< BYTE > &bytes)
std::vector< BYTE > m_EveHeadBuf
Definition: EventDecoder.h:86
ssize_t Decode(const char *buf, size_t nb, bool cflag, std::vector< adc16_t > &adc)
void dlardaq::EventDecoder::SetNCh ( size_t  val)
inline

Definition at line 31 of file EventDecoder.h.

31 { m_nch = val; }
void dlardaq::EventDecoder::SetNSample ( size_t  val)
inline

Definition at line 32 of file EventDecoder.h.

32 { m_nsample = val; }
int dlardaq::EventDecoder::trylock ( pthread_mutex_t &  mutex)
inlineprotected

Definition at line 68 of file EventDecoder.h.

68 { return pthread_mutex_trylock(&mutex); }
int dlardaq::EventDecoder::unlock ( pthread_mutex_t &  mutex)
inlineprotected

Definition at line 69 of file EventDecoder.h.

69 { return pthread_mutex_unlock(&mutex); }

Member Data Documentation

ssize_t dlardaq::EventDecoder::m_bytes_left
protected

Definition at line 93 of file EventDecoder.h.

pthread_mutex_t dlardaq::EventDecoder::m_data_mutex
protected

Definition at line 118 of file EventDecoder.h.

bool dlardaq::EventDecoder::m_dcflag
protected

Definition at line 115 of file EventDecoder.h.

std::vector<BYTE> dlardaq::EventDecoder::m_EndFootBuf
protected

Definition at line 87 of file EventDecoder.h.

std::vector<adc16_t> dlardaq::EventDecoder::m_EveData
protected

Definition at line 90 of file EventDecoder.h.

std::vector<BYTE> dlardaq::EventDecoder::m_EveDataBuf
protected

Definition at line 88 of file EventDecoder.h.

std::vector<BYTE> dlardaq::EventDecoder::m_EveHeadBuf
protected

Definition at line 86 of file EventDecoder.h.

std::vector<std::streampos> dlardaq::EventDecoder::m_events
protected

Definition at line 104 of file EventDecoder.h.

evheader_t dlardaq::EventDecoder::m_evh
protected

Definition at line 98 of file EventDecoder.h.

std::ifstream dlardaq::EventDecoder::m_file

Definition at line 64 of file EventDecoder.h.

footer_t dlardaq::EventDecoder::m_flf
protected

Definition at line 99 of file EventDecoder.h.

short dlardaq::EventDecoder::m_nadc
protected

Definition at line 110 of file EventDecoder.h.

size_t dlardaq::EventDecoder::m_nch
protected

Definition at line 111 of file EventDecoder.h.

size_t dlardaq::EventDecoder::m_nsample
protected

Definition at line 112 of file EventDecoder.h.

std::streampos dlardaq::EventDecoder::m_pend
protected

Definition at line 103 of file EventDecoder.h.

std::streampos dlardaq::EventDecoder::m_pstart
protected

Definition at line 102 of file EventDecoder.h.

runheader_t dlardaq::EventDecoder::m_rnh
protected

Definition at line 97 of file EventDecoder.h.

std::vector<BYTE> dlardaq::EventDecoder::m_RunHeadBuf
protected

Definition at line 85 of file EventDecoder.h.

size_t dlardaq::EventDecoder::m_totev
protected

Definition at line 107 of file EventDecoder.h.

int dlardaq::EventDecoder::m_Verbosity
protected

Definition at line 83 of file EventDecoder.h.


The documentation for this class was generated from the following files: