6 #include <Event/packet.h>
9 #define coutfl cout << __FILE__<< " " << __LINE__ << " "
10 #define cerrfl cerr << __FILE__<< " " << __LINE__ << " "
19 _required_depth = 4000000;
20 cout <<
" Adjusted the depth to the minimum value " << endl;
24 _required_depth =
depth;
27 if ( low_mark > _required_depth)
29 _low_mark = _required_depth;
30 cout <<
" Adjusted the low mark to not exceed minimum depth " << endl;
38 _last_requested_element = -1;
48 for (
auto itr = waveforms.begin() ; itr != waveforms.end() ; ++itr)
55 for (
auto itr = gtm_data.begin() ; itr != gtm_data.end() ; ++itr)
66 for (
auto itr = waveforms.begin() ; itr != waveforms.end() ; ++itr)
73 for (
auto itr = gtm_data.begin() ; itr != gtm_data.end() ; ++itr)
79 for (
int fee_id = 0; fee_id < MAX_FEECOUNT; fee_id++)
81 fee_data[fee_id].clear();
85 _thebuffer.erase(_thebuffer.begin(), _thebuffer.begin() + _progress_index );
95 return ( min_depth() >= _required_depth);
115 for (
int i = 0;
i <
l2;
i++)
117 unsigned short x = b[
i] & 0xffff;
118 _thebuffer.push_back(x);
119 x = (b[
i] >> 16) & 0xffff;
120 _thebuffer.push_back(x);
131 if ( n < 0)
return 0;
132 if ( _last_requested_element == n)
return 1;
136 if ( i >= waveforms.size() )
138 _last_requested_element = -1;
144 _last_requested_element =
i;
150 unsigned char *gtm = (
unsigned char *)dat;
153 payload->
pkt_type = gtm[0] | ((
unsigned short)gtm[1] << 8);
154 if (payload->
pkt_type != GTM_LVL1_ACCEPT_MAGIC_KEY && payload->
pkt_type != GTM_ENDAT_MAGIC_KEY)
163 payload->
bco = ((
unsigned long long)gtm[2] << 0)
164 | ((
unsigned long long)gtm[3] << 8)
165 | ((
unsigned long long)gtm[4] << 16)
166 | ((
unsigned long long)gtm[5] << 24)
167 | ((
unsigned long long)gtm[6] << 32)
168 | (((
unsigned long long)gtm[7]) << 40);
169 payload->
lvl1_count = ((
unsigned int)gtm[8] << 0)
170 | ((
unsigned int)gtm[9] << 8)
171 | ((
unsigned int)gtm[10] << 16)
172 | ((
unsigned int)gtm[11] << 24);
173 payload->
endat_count = ((
unsigned int)gtm[12] << 0)
174 | ((
unsigned int)gtm[13] << 8)
175 | ((
unsigned int)gtm[14] << 16)
176 | ((
unsigned int)gtm[15] << 24);
177 payload->
last_bco = ((
unsigned long long)gtm[16] << 0)
178 | ((
unsigned long long)gtm[17] << 8)
179 | ((
unsigned long long)gtm[18] << 16)
180 | ((
unsigned long long)gtm[19] << 24)
181 | ((
unsigned long long)gtm[20] << 32)
182 | (((
unsigned long long)gtm[21]) << 40);
185 this->gtm_data.push_back(payload);
194 unsigned long long old_bco = 0;
197 if (_is_decoded )
return 0;
201 unsigned int payload_length = _thebuffer.size();
203 unsigned int index = 0;
207 while (index < payload_length)
213 const unsigned int datalength = 0xf;
215 if ((_thebuffer[index] & 0xFF00) == FEE_MAGIC_KEY)
218 unsigned int fee_id = _thebuffer[
index] & 0xff;
221 if (fee_id < MAX_FEECOUNT)
223 for (
unsigned int i = 0;
i < datalength;
i++)
226 if (index >= payload_length)
break;
228 fee_data[fee_id].push_back(_thebuffer[index++]);
233 else if ((_thebuffer[index] & 0xFF00) == GTM_MAGIC_KEY)
238 if ( (_thebuffer[index] & 0xFFFF) == GTM_LVL1_ACCEPT_MAGIC_KEY)
243 old_bco = (_thebuffer[index+3] & 0xff);
245 old_bco |= (_thebuffer[index+2] & 0xffff);
247 old_bco |= (_thebuffer[index+1] & 0xffff);
251 unsigned long long this_bco = 0;
252 this_bco = (_thebuffer[index+3] & 0xff);
254 this_bco |= (_thebuffer[index+2] & 0xffff);
256 this_bco |= (_thebuffer[index+1] & 0xffff);
258 if ( this_bco != old_bco )
264 _progress_index =
index;
269 unsigned short buf[16];
272 for (
unsigned int i = 0;
i < 16;
i++)
274 buf[
i] = _thebuffer[index++];
277 decode_gtm_data(buf);
282 index += datalength + 1;
287 _progress_index =
index;
290 for (
int ifee = 0 ; ifee < MAX_FEECOUNT ; ifee++)
296 for ( pos = 0; pos < fee_data[ifee].size() ; )
299 int skip_amount = find_header (pos, fee_data[ifee]);
300 if ( skip_amount < 0)
break;
305 unsigned int startpos =
pos;
308 if ( startpos +
HEADER_LENGTH >= fee_data[ifee].
size() || startpos + fee_data[ifee][startpos] > fee_data[ifee].
size())
310 pos = fee_data[ifee].size() + 1;
325 sw->
channel = header[1] & 0x1ff;
327 | ((header[2] & 0x3ff) << 1)
331 unsigned short data_size = header[5] -1 ;
338 for (
int i = 0 ;
i < data_size ;
i++)
340 sw->
waveform.push_back( fee_data[ifee][pos++]);
344 unsigned short crc = crc16(ifee, startpos, header[0]-1);
352 sw->
valid = ( crc == fee_data[ifee][
pos]);
354 waveforms.insert(sw);
372 if (strcmp(what,
"N_TAGGER") == 0)
374 return gtm_data.size();
377 else if (strcmp(what,
"TAGGER_TYPE") == 0 )
379 if (i < gtm_data.size())
381 return gtm_data[
i]->pkt_type;
385 else if (strcmp(what,
"IS_ENDAT") == 0 )
387 if (i < gtm_data.size())
389 return gtm_data[
i]->is_endat;
393 else if (strcmp(what,
"IS_LEVEL1_TRIGGER") == 0 )
395 if (i < gtm_data.size())
397 return gtm_data[
i]->is_lvl1;
401 else if (strcmp(what,
"BCO") == 0 )
403 if (i < gtm_data.size())
405 return gtm_data[
i]->bco;
409 else if (strcmp(what,
"LEVEL1_COUNT") == 0 )
411 if (i < gtm_data.size())
413 return gtm_data[
i]->lvl1_count;
417 else if (strcmp(what,
"ENDAT_COUNT") == 0 )
419 if (i < gtm_data.size())
421 return gtm_data[
i]->endat_count;
425 else if (strcmp(what,
"LAST_BCO") == 0 )
427 if (i < gtm_data.size())
429 return gtm_data[
i]->last_bco;
433 else if (strcmp(what,
"MODEBITS") == 0 )
435 if (i < gtm_data.size())
437 return gtm_data[
i]->modebits;
446 if ( n < 0)
return 0;
450 if ( cacheIterator(n) )
452 unsigned int m = sample;
453 if ( m >= (*_cached_iter)->waveform.size() )
return 0;
454 return (*_cached_iter)->waveform[m];
468 if ( strcmp(what,
"NR_WF") == 0 )
470 return waveforms.size();
473 else if ( strcmp(what,
"MAX_FEECOUNT") == 0 )
479 else if ( strcmp(what,
"SAMPLES") == 0 )
481 if ( cacheIterator(n) )
483 return (
int) (*_cached_iter)->waveform.size();
488 else if ( strcmp(what,
"FEE") == 0 )
490 if ( cacheIterator(n) )
492 return (
int) (*_cached_iter)->fee;
497 else if ( strcmp(what,
"SAMPAADDRESS") == 0 )
499 if ( cacheIterator(n) )
501 return (
int) (*_cached_iter)->sampa_address;
506 else if ( strcmp(what,
"SAMPACHANNEL") == 0 )
508 if ( cacheIterator(n) )
510 return (
int) (*_cached_iter)->sampa_channel;
515 else if ( strcmp(what,
"CHANNEL") == 0 )
517 if ( cacheIterator(n) )
519 return (
int) (*_cached_iter)->channel;
524 else if ( strcmp(what,
"BCO") == 0 )
526 if ( cacheIterator(n) )
528 return (
int) (*_cached_iter)->bx_timestamp;
533 else if ( strcmp(what,
"CHECKSUM") == 0 )
535 if ( cacheIterator(n) )
537 return (
int) (*_cached_iter)->checksum;
542 else if ( strcmp(what,
"CHECKSUMERROR") == 0 )
544 if ( cacheIterator(n) )
546 if ( (*_cached_iter)->valid )
return 0;
560 unsigned int pos = yy;
561 std::vector<unsigned short> header_candidate;
571 if ( pos >= orig.size())
576 header_candidate.push_back(orig[pos]);
585 if (header_candidate[4] == MAGIC_KEY_0 && header_candidate[6] == MAGIC_KEY_1 && (header_candidate[0] - header_candidate[5] == HEADER_LENGTH))
593 if ( pos >= orig.size())
599 header_candidate.erase(header_candidate.begin());
600 header_candidate.push_back(orig[pos]);
611 if (lValue(0,
"N_TAGGER") == 0)
612 os <<
" No lvl1 and Endat taggers" << endl;
615 os <<
" TAGGER_TYPE BCO LEVEL1 CNT ENDAT CNT LAST_BCO MODEBITS" << endl;
617 for (
int i = 0;
i < lValue(0,
"N_TAGGER"); ++
i)
619 os <<
" 0x" << setw(4) << hex << lValue(
i,
"TAGGER_TYPE") << dec
620 <<
" (" << (lValue(
i,
"IS_ENDAT") ?
"ENDAT" :
"") << (lValue(
i,
"IS_LEVEL1_TRIGGER") ?
"LVL1 " :
"")
622 << setw(12) << lValue(
i,
"BCO") <<
" "
623 << setw(10) << lValue(
i,
"LEVEL1_COUNT") <<
" "
624 << setw(10) << lValue(
i,
"ENDAT_COUNT") <<
" "
625 << setw(12) << lValue(
i,
"LAST_BCO")
626 <<
" 0x" << std::setfill(
'0') << setw(2) << hex << lValue(
i,
"MODEBITS") << std::setfill(
' ')<< dec
632 waveform_set::const_iterator wf_iter;
634 for (
int i = 0;
i < iValue(0,
"NR_WF") ;
i++)
636 os <<
" FEE Channel Sampachannel Samples BCO CRC_ERR" << endl;
638 os << setw(5) << iValue(
i,
"FEE") <<
" "
639 << setw(9) << iValue(
i,
"CHANNEL") <<
" "
640 << setw(9) << iValue(
i,
"SAMPACHANNEL") <<
" "
641 << setw(12) << iValue(
i,
"SAMPLES") <<
" "
642 <<
" 0x" << setw(5) << hex << iValue(
i,
"BCO") << dec
644 << setw(4) << iValue(
i,
"CHECKSUMERROR")
647 for (
int j = 0;
j < iValue(
i,
"SAMPLES") ;
j += 10)
650 for (
int k = 0;
k < 10 ;
k++)
652 os << setw(4) << iValue(
i,
j+
k) <<
" ";
664 unsigned short n =
x;
665 n = ((n >> 1) & 0x55555555) | ((n << 1) & 0xaaaaaaaa);
666 n = ((n >> 2) & 0x33333333) | ((n << 2) & 0xcccccccc);
667 n = ((n >> 4) & 0x0f0f0f0f) | ((n << 4) & 0xf0f0f0f0);
668 n = ((n >> 8) & 0x00ff00ff) | ((n << 8) & 0xff00ff00);
678 unsigned short crc = 0xffff;
680 for (
int i = 0;
i < l;
i++)
682 unsigned short x = fee_data[fee][index+
i];
684 crc ^= reverseBits( x);
685 for (
unsigned short k = 0;
k < 16;
k++)
687 crc = crc & 1 ? (crc >> 1) ^ 0xa001 : crc >> 1;
690 crc = reverseBits(crc);