27 EventDecoder::EventDecoder(
size_t nch,
size_t nsample)
42 pthread_mutex_init(&m_data_mutex, NULL);
49 EventDecoder::~EventDecoder()
52 pthread_mutex_destroy(&m_data_mutex);
70 unlock( m_data_mutex );
91 m_file.open(finname.c_str(), ios::in | ios::binary);
93 if( !m_file.is_open() )
95 msg_err<<
"Could not open "<<finname<<
" for reading"<<
endl;
96 unlock( m_data_mutex );
101 ReadBytes( m_RunHeadBuf );
105 m_pstart = m_file.tellg();
110 m_pend = m_file.tellg();
112 ReadBytes( m_EndFootBuf );
116 m_file.seekg( m_pstart );
121 m_totev = m_flf.num_events;
126 unlock(m_data_mutex);
131 unlock( m_data_mutex );
139 void EventDecoder::ReadBytes( std::vector<BYTE> &
bytes )
141 m_file.read( &bytes[0], bytes.size() );
148 void EventDecoder::ReadEvent( std::vector<adc16_t> &
adc,
bool headonly )
151 ReadBytes( m_EveHeadBuf );
164 size_t evsz = m_evh.ev_size;
167 m_file.seekg(evsz, std::ios::cur);
173 if(!m_EveDataBuf.empty()) m_EveDataBuf.clear();
175 m_EveDataBuf.resize(m_evh.ev_size,
'\0');
178 ReadBytes(m_EveDataBuf);
179 Decode( &m_EveDataBuf[0], m_EveDataBuf.size(),
GETDCFLAG(m_evh.dq_flag),
adc );
186 std::vector<adc16_t> &
adc)
189 if(!m_file.is_open())
return -1;
193 if(evnum < m_events.size())
195 m_file.seekg(m_events[evnum]);
201 size_t curev = m_events.size();
202 while( curev <= evnum )
205 streampos
pos = m_file.tellg();
206 m_events.push_back( pos );
210 ReadEvent(adc,
true);
212 ReadEvent(adc,
false);
221 unlock(m_data_mutex);
230 std::vector<adc16_t> &
adc )
235 return GetEvent( 0, eh, adc);
240 if( m_EveData.empty() )
return -1;
254 unlock(m_data_mutex);
261 ssize_t EventDecoder::Decode(
const char *buf,
size_t nb,
bool cflag,
262 std::vector<adc16_t> &
adc )
268 adc.resize( (nb*8)/m_nadc, 0 );
274 HuffDataCompressor::Instance().DecompressEventData(m_nadc, m_nch, m_nsample, buf, nb, byteidx, adc);
286 void EventDecoder::ReadBuffer(
const char *buf,
size_t nb)
290 msg_err<<
"Cannot use this function while reading data from a file"<<
endl;
297 if(!m_EveData.empty()) m_EveData.clear();
300 if( m_bytes_left <= 0 ||
303 m_EveDataBuf.clear();
304 if(IsFirstPacket(buf, nb))
308 const char *pdata = buf;
316 unlock(m_data_mutex);
327 unlock(m_data_mutex);
333 m_bytes_left = m_evh.ev_size;
334 if(m_bytes_left <= 0)
337 unlock(m_data_mutex);
344 m_EveDataBuf.insert(m_EveDataBuf.end(), pdata, pdata + nb-nb_read);
345 m_bytes_left -= (nb - nb_read);
351 unlock(m_data_mutex);
356 unlock(m_data_mutex);
362 m_EveDataBuf.insert(m_EveDataBuf.end(), buf, buf+
nb);
364 if( m_bytes_left < 0 )
369 unlock(m_data_mutex);
374 if( m_bytes_left == 0 )
378 Decode( &m_EveDataBuf[0], m_EveDataBuf.size(), cflag, m_EveData );
381 msg_info<<
"Recieved: run "<<m_rnh.run_num<<
" event "<<m_evh.ev_num<<
endl;
385 unlock(m_data_mutex);
392 bool EventDecoder::IsFirstPacket(
const char *buf,
size_t nb)
396 bool rval = ( ((k0 & 0xFF) ==
EVSKEY) && ((k1 & 0xFF) ==
EVSKEY) );
static constexpr double nb
static const size_t FileFootSz
ssize_t decode_evehead(const char *buf, evheader_t &eh)
static Timer & GetTimer()
ssize_t decode_runhead(const char *buf, runheader_t &rh)
static const size_t RunHeadSz
static const size_t EveHeadSz
ssize_t decode_filefoot(const char *buf, footer_t &rf)
static const short BitsADC
static LogStream msg_info(std::cout, _strinfo)
void unpack12into16(const void *in, void *out, size_t n)
static LogStream msg_err(std::cerr, _strerror)
byte bytes
Alias for common language habits.
QTextStream & endl(QTextStream &s)