7 #include <rtsSystems.h>
11 #include <SFS/sfs_index.h>
12 #include <DAQ_READER/daqReader.h>
13 #include <DAQ_READER/daq_dta.h>
19 const char *daq_fgt::help_string =
"FGT\n\
21 raw returns raw data\n" ;
28 daq_det_factory::det_factories[FGT_ID] = this ;
44 daq_det_factory::det_factories[IST_ID] = this ;
50 ((
daq_fgt *)dd)->set_flavor(IST_ID) ;
63 daq_det_factory::det_factories[FST_ID] = this ;
69 ((
daq_fgt *)dd)->set_flavor(FST_ID) ;
82 daq_det_factory::det_factories[GMT_ID] = this ;
88 ((
daq_fgt *)dd)->set_flavor(GMT_ID) ;
98 void daq_fgt::set_flavor(
int id)
103 name = rts2name(rts_id) ;
108 name = rts2name(rts_id) ;
113 name = rts2name(rts_id) ;
118 name = rts2name(rts_id) ;
123 LOG(DBG,
"set_flavor %d [%s]",
id,name) ;
131 name = rts2name(rts_id) ;
133 caller = rts_caller ;
135 if(caller) caller->insert(
this, rts_id) ;
143 adc->meta = (
void *) &apv_meta ;
144 zs->meta = (
void *) &apv_meta ;
146 LOG(DBG,
"%s: constructor: caller %p",name,rts_caller) ;
152 LOG(DBG,
"%s: DEstructor",name) ;
164 daq_dta *daq_fgt::get(
const char *bank,
int sec,
int rdo,
int pad,
void *p1,
void *p2)
169 if(present == 0)
return 0 ;
175 if(strcasecmp(bank,
"raw")==0) {
176 return handle_raw(sec,rdo) ;
178 else if(strcasecmp(bank,
"adc")==0) {
179 return handle_adc(sec,rdo,0) ;
181 else if(strcasecmp(bank,
"pedrms")==0) {
182 return handle_ped(sec,rdo) ;
184 else if(strcasecmp(bank,
"zs")==0) {
185 return handle_zs(sec,rdo) ;
189 LOG(ERR,
"%s: unknown bank type \"%s\"",name,bank) ;
195 daq_dta *daq_fgt::handle_raw(
int sec,
int rdo)
198 int r_start, r_stop ;
200 int s_start, s_stop ;
208 LOG(DBG,
"%s: present %d",name,present) ;
220 r_start = r_stop = rdo ;
232 s_start = s_stop = sec ;
236 raw->create(8*1024,
"fgt_raw",rts_id,DAQ_DTA_STRUCT(
char)) ;
238 for(
int s=s_start;s<=s_stop;s++) {
239 for(
int r=r_start;r<=r_stop;r++) {
240 sprintf(str,
"%s/sec%02d/rb%02d/raw",sfs_name, s, r) ;
241 full_name = caller->get_sfs_name(str) ;
243 if(!full_name) continue ;
244 bytes = caller->sfs->fileSize(full_name) ;
247 st = (
char *) raw->request(bytes) ;
249 int ret = caller->sfs->read(str, st, bytes) ;
251 LOG(ERR,
"ret is %d") ;
255 raw->finalize(bytes,s,r,0) ;
266 daq_dta *daq_fgt::handle_zs(
int sec,
int rdo,
char *rdobuff,
int inbytes)
268 int r_start, r_stop ;
271 zs->create(1000,
"fgt_zs",rts_id,DAQ_DTA_STRUCT(
fgt_adc_t)) ;
273 LOG(NOTE,
"FGT: doing ZS") ;
274 memset(&apv_meta,0,
sizeof(apv_meta)) ;
276 if((rdo <= 0) || (rdo > FGT_RDO_COU)){
278 r_stop = FGT_RDO_COU ;
281 r_start = r_stop = rdo ;
286 for(
int s=1;s<=2;s++) {
287 for(
int r=r_start;r<=r_stop;r++) {
295 sprintf(str,
"%s/sec%02d/rb%02d/zs",sfs_name, s, r) ;
296 full_name = caller->get_sfs_name(str) ;
298 if(!full_name) continue ;
299 bytes = caller->sfs->fileSize(full_name) ;
301 d = (u_short *) malloc(bytes) ;
303 int ret = caller->sfs->read(str, (
char *)d, bytes) ;
305 LOG(ERR,
"ret is %d") ;
310 d = (u_short *) rdobuff ;
314 if(d == 0) continue ;
317 int *d32 = (
int *) d ;
319 if(d32[0] != (
int)0xFEEDBEEF) {
320 LOG(ERR,
"Bad signature 0x%04X",d32[0]) ;
326 case META_ZS_VERSION :
327 case META_PED_ZS_VERSION :
330 LOG(ERR,
"Unknown version 0x%04X",d32[1]) ;
338 int meta_bytes = d32[3] ;
343 int dumped_chs = d[bytes/2-1] ;
351 for(
int arm=0;arm<FGT_ARM_COU;arm++) {
352 for(
int apv=0;apv<FGT_APV_COU;apv++) {
353 if(meta_zs->status[arm][apv]) {
354 apv_meta.arc[r].arm[arm].present = 1 ;
355 apv_meta.arc[r].present = 1 ;
357 apv_meta.arc[r].arm[arm].apv[apv].present = 1 ;
358 apv_meta.arc[r].arm[arm].apv[apv].ntim = meta_zs->tb_cou ;
360 if(meta_zs->status[arm][apv] != 1) {
361 apv_meta.arc[r].arm[arm].apv[apv].error = 1 ;
368 LOG(NOTE,
"Expect %d dumped CHs in %d bytes, meta_bytes %d, tb_cou %d",dumped_chs,bytes,meta_bytes,meta_zs->tb_cou) ;
374 int ix = 2*4 + meta_bytes/2 ;
377 bytes -= meta_bytes ;
393 while((d[ix] & 0xFF00) == 0xAB00) {
398 zs->finalize(cou,arm,arc,apv) ;
414 if(bytes <= 0) continue ;
420 fgt_d = (
fgt_adc_t *) zs->request(FGT_CH_COU*FGT_TB_COU) ;
423 int ch = d[ix] & 0xFF ;
424 int tb_cou = d[ix] >> 8 ;
430 bytes -= 2*(1+tb_cou) ;
431 for(
int i=0;i<tb_cou;i++) {
434 fgt_d[cou].adc = d[ix] & 0xFFF ;
435 fgt_d[cou].flags = d[ix]>>12 ;
446 zs->finalize(cou,arm,arc,apv) ;
450 if(got_chs != dumped_chs) {
451 LOG(ERR,
"Mismatch: got %d, expect %d",got_chs,dumped_chs) ;
454 if(rdobuff == 0) free(d) ;
462 zs->meta = (
void *) &apv_meta ;
472 daq_dta *daq_fgt::handle_adc(
int sec,
int rdo,
char *rdobuff)
474 int r_start, r_stop ;
475 int s_start, s_stop ;
477 adc->create(1000,
"fgt_adc",rts_id,DAQ_DTA_STRUCT(
fgt_adc_t)) ;
479 memset(&apv_meta,0,
sizeof(apv_meta)) ;
481 LOG(NOTE,
"FGT: doing ADC") ;
483 if((rdo <= 0) || (rdo > FGT_RDO_COU)){
485 r_stop = FGT_RDO_COU ;
488 r_start = r_stop = rdo ;
500 s_start = s_stop = sec ;
505 for(
int s=s_start;s<=s_stop;s++) {
506 for(
int r=r_start;r<=r_stop;r++) {
510 daq_dta *dd = handle_raw(s,r) ;
511 if(dd == 0) continue ;
513 if(dd->iterate() == 0)
continue ;
515 d = (u_int *) dd->Void ;
516 int words = dd->ncontent/4 ;
518 if(words <= 0) continue ;
522 d = (u_int *) rdobuff ;
528 int format_code = (d[2] >> 8) & 0xFF ;
533 int arm_mask = (d[3] >> 8) & 0x3F ;
534 LOG(NOTE,
"[evt %d]: RDO %d: ARC Header: format %d, ARM mask 0x%02x",get_global_event_num(),r,format_code,arm_mask);
536 apv_meta.arc[r].present = 1 ;
537 apv_meta.arc[r].format_code = format_code ;
538 apv_meta.arc[r].arm_mask = arm_mask ;
543 for(
int arm=0;arm<FGT_ARM_COU;arm++) {
544 if(arm_mask & (1<<arm)) ;
547 LOG(NOTE,
"[evt %d]: RDO %d: Handling ARM %d",get_global_event_num(),r,arm) ;
550 int arm_id = *dta & 0x7 ;
551 int arm_seq = (*dta >> 20) & 0xfff;
552 int arm_err = (*dta >>16) & 0xf;
555 u_int apv_mask = *dta & 0x00FFFFFF ;
560 apv_meta.arc[r].arm[arm].present = 1 ;
561 apv_meta.arc[r].arm[arm].arm_id = arm_id ;
562 apv_meta.arc[r].arm[arm].arm_seq = arm_seq ;
563 apv_meta.arc[r].arm[arm].arm_err = arm_err ;
564 apv_meta.arc[r].arm[arm].apv_mask = apv_mask ;
566 LOG(NOTE,
"[evt %d]: ARM_ID %d SEQ %d ERR %1x APV_MASK %06x",get_global_event_num(),
567 arm_id,arm_seq,arm_err,apv_mask);
570 apv_meta.arc[r].arm[arm].error = 1 ;
571 LOG(ERR,
"[evt %d]: RDO %d ARM %d: Bad ARM ID is %d",get_global_event_num(),r,arm,arm_id) ;
572 goto unrecoverable_error ;
576 apv_meta.arc[r].arm[arm].error = 1 ;
577 LOG(ERR,
"[evt %d]: RDO %d ARM %d: Error code 0x%x",get_global_event_num(),r,arm,arm_err) ;
582 for(
int apv=0;apv<FGT_APV_COU;apv++) {
583 if(apv_mask & (1<<apv)) ;
586 LOG(NOTE,
"[evt %d]: Handling APV %d",get_global_event_num(),apv) ;
588 int apv_id = *dta & 0x1F ;
589 int fmt = (*dta >> 16) & 0xf ;
591 if ((fmt==1)||(fmt==2))
592 length = (*dta >> 5) & 0x3FF ;
594 length = (*dta >> 5) & 0x7ff ;
596 int seq = (*dta >> 20) & 0xfff;
600 LOG(NOTE,
" ID %d, length %d, fmt %d",apv_id,length,fmt) ;
602 int capid = *dta & 0xFF ;
603 int nhits = (*dta >> 8) & 0x7F ;
604 int is_error = (*dta >> 15) & 1 ;
606 int refadc,ntim,is_0;
607 if ((fmt==1)||(fmt==2)) {
608 refadc = (*dta >> 16) & 0xFFF ;
609 ntim = (*dta >> 28) & 0x7 ;
610 is_0 = *dta & 0x80000000 ;
613 refadc = (*dta >> 16) & 0x7FF ;
614 ntim = (*dta >> 27) & 0x1f ;
621 nhits = ( ntim>0 ? nhits+1 : 0 );
623 LOG(NOTE,
" capid %d, nhits %d, is_error 0x%X, refadc %d, ntim %d, is_0 0x%X",
624 capid, nhits, is_error, refadc, ntim, is_0) ;
626 apv_meta.arc[r].arm[arm].apv[apv].present = 1 ;
628 apv_meta.arc[r].arm[arm].apv[apv].apv_id = apv_id ;
629 apv_meta.arc[r].arm[arm].apv[apv].fmt = fmt ;
630 apv_meta.arc[r].arm[arm].apv[apv].length = length ;
631 apv_meta.arc[r].arm[arm].apv[apv].seq = seq ;
632 apv_meta.arc[r].arm[arm].apv[apv].capid = capid ;
633 apv_meta.arc[r].arm[arm].apv[apv].nhits = nhits ;
634 apv_meta.arc[r].arm[arm].apv[apv].is_error = is_error ;
635 apv_meta.arc[r].arm[arm].apv[apv].refadc = refadc ;
636 apv_meta.arc[r].arm[arm].apv[apv].ntim = ntim ;
640 apv_meta.arc[r].arm[arm].apv[apv].error = 1 ;
641 LOG(ERR,
"[evt %d]: RDO %d ARM %d APV %d: Bad APV ID, got %d",get_global_event_num(),r,arm,apv,apv_id) ;
642 goto unrecoverable_error ;
648 LOG(WARN,
"[evt %d]: RDO %d ARM %d APV %d: Sequence number mismatch, expect %d have %d",get_global_event_num(),r,arm,apv,arm_seq,seq);
650 apv_meta.arc[r].arm[arm].apv[apv].error = 1 ;
651 LOG(ERR,
"[evt %d]: RDO %d ARM %d APV %d: Sequence number mismatch, expect %d have %d",get_global_event_num(),r,arm,apv,arm_seq,seq);
652 goto unrecoverable_error ;
656 if((ntim < 0) || (ntim > 31)) {
657 apv_meta.arc[r].arm[arm].apv[apv].error = 1 ;
658 LOG(ERR,
"Ntim %d ?!",ntim) ;
661 if((fmt != 1)&&(fmt != 2)&&(fmt != 3)) {
662 apv_meta.arc[r].arm[arm].apv[apv].error = 1 ;
663 LOG(ERR,
"Invalid FMT %d (evt %d)",fmt,get_global_event_num()) ;
681 u_long expect0 = *(dta-2);
682 expect0 = (expect0 & 0xffffffe0)|((expect0+1) & 0x0000001f);
683 u_long expect1 = *(dta-1);
686 if((*(dta+3)==expect0) && (*(dta+4)==expect1)) {
698 u_short *d16 = (u_short *) dta ;
700 u_short wfm[((2047-2)/3)*8] ;
704 for(
int j=0;j<((length-2)/3)*2;j++) {
707 wfm[4*j] = 0x0fff & d16[i] ;
709 wtmp = 0x000f & (d16[i++] >> 12) ;
711 wfm[4*j + 1] = (0x0ff0 & (d16[i]<<4)) | wtmp ;
713 wtmp = 0x00ff & (d16[i++] >> 8) ;
715 wfm[4*j+2] = (0x0f00 & (d16[i]<<8)) | wtmp ;
716 wfm[4*j+3] = 0x0fff & (d16[i++]>>4) ;
719 dta += (length - 2) ;
728 if ((ntim>0) && ( 27+127+(ntim-1)*140 >= ((length-2)/3)*8 )) {
729 apv_meta.arc[r].arm[arm].apv[apv].error = 1 ;
730 LOG(ERR,
"[evt %d]: RDO %d ARM %d APV %d: Trouble in APV block content, it's shorter than required to unpack %d timebins",
731 get_global_event_num(),r,arm,apv,ntim);
734 for(
int ch=0;ch<128;ch++) {
735 int rch = 32*(ch%4) + 8*(ch/4) - 31*(ch/16) ;
736 for(
int tb=0;tb<ntim;tb++) {
738 int adc = wfm[27+ch+tb*140] ;
739 fgt_d[cou].ch = rch ;
741 fgt_d[cou].adc = adc ;
742 fgt_d[cou].flags = 0 ;
749 adc->finalize(cou, arm, r, apv) ;
756 apv_meta.arc[r].error = 1 ;
757 LOG(WARN,
"[evt %d]: RDO %d: Cannot reliably recover pointer to next item, dropping the rest of this event on this rdo",
758 get_global_event_num(),r);
765 adc->meta = (
void *) &apv_meta ;
777 daq_dta *daq_fgt::handle_ped(
int sec,
int rdo)
780 ped->create(1000,
"fgt_pedrms",rts_id,DAQ_DTA_STRUCT(
fgt_pedrms_t)) ;
788 sprintf(str,
"%s/sec%02d/pedrms",sfs_name, 1) ;
789 full_name = caller->get_sfs_name(str) ;
792 LOG(TERR,
"full_name %s",full_name) ;
795 if(!full_name)
return 0 ;
796 bytes = caller->sfs->fileSize(full_name) ;
798 LOG(TERR,
"bytes %d",bytes) ;
800 d = (u_short *) malloc(bytes) ;
802 int ret = caller->sfs->read(str, (
char *)d, bytes) ;
804 LOG(ERR,
"ret is %d") ;
809 LOG(ERR,
"Bad pedestal version") ;
821 LOG(ERR,
"Bad pedestal version 0x%X",d[1]) ;
831 int max_ix = (bytes/2) ;
843 f_ped = (
fgt_pedrms_t *) ped->request(ch_cou * tb_cou) ;
846 for(
int ch=0;ch<ch_cou;ch++) {
847 for(
int t=0;t<tb_cou;t++) {
848 u_short ped = d[ix++] ;
849 u_short rms = d[ix++] ;
859 f_ped[cou].ped = ((float)ped) / 16.0 ;
860 f_ped[cou].rms = ((float)rms) / 16.0 ;
861 f_ped[cou].cmn_rms = ((float)cmn_rms) / 16.0 ;
866 ped->finalize(cou,arm,arc,apv) ;
887 int daq_fgt::get_l2(
char *buff,
int words,
struct daq_trg_word *trg,
int rdo)
889 const int FGT_BYTES_MIN = ((6)*4) ;
890 const int FGT_BYTES_MAX = (512*1024) ;
892 const u_int FGT_SIGNATURE = 0x46475420 ;
896 u_int *d32 = (u_int *)buff ;
897 int id_check_failed = 0 ;
901 int buff_bytes = 4 * words ;
903 if((buff_bytes < FGT_BYTES_MIN) || (buff_bytes > FGT_BYTES_MAX)) {
904 LOG(ERR,
"RDO %d: expect %d bytes, received %d",rdo,FGT_BYTES_MIN,buff_bytes) ;
912 if(d32[1] != FGT_SIGNATURE) {
913 LOG(ERR,
"RDO %d: bad header sig 0x%08X, expect 0x%08X",rdo,d32[1], FGT_SIGNATURE) ;
917 if(d32[5] != 0xFEEDBEEF) {
918 LOG(ERR,
"RDO %d: bad FEEDBEEF 0x%08X",rdo,d32[5]) ;
931 if((d32[3] & 0xFFFF0000)) {
932 LOG(ERR,
"RDO %d: error flags 0x%04X",rdo,d32[3]>>16) ;
940 int rdo_in_dta = d32[3] & 0xFF ;
941 if(rdo_id[rdo] != 0xFF) {
942 if(rdo_id[rdo] != rdo_in_dta) {
948 LOG(NOTE,
"RDO %d: expect %d, in data %d",rdo,rdo_id[rdo],rdo_in_dta) ;
951 int format_code = (d32[2] >> 8) & 0xFF ;
952 if(format_code == 0x02) {
953 LOG(WARN,
"RDO %d: format code 0x%X? (null event)",rdo,format_code) ;
965 #define G_CONST 0x04C11DB7
967 u_int crc_in_data = d32[last_ix] ;
968 register u_int crc = 0xFFFFFFFF ;
970 for(
int i=0;i<last_ix;i++) {
974 register u_int data_j ;
975 register u_int crc_31 ;
977 for(
register int j=31;j>=0;j--) {
978 data_j = (datum >> j) & 1 ;
979 crc_31 = (crc & 0x80000000) ? 1 : 0 ;
981 if(crc_31 == data_j) {
982 crc = (crc<<1) ^ G_CONST ;
990 if(crc != crc_in_data) {
991 LOG(ERR,
"RDO %d: CRC in data 0x%08X, CRC calculated 0x%08X",rdo,crc_in_data,crc) ;
996 LOG(DBG,
"RDO %d: CRC in data 0x%08X, CRC calculated 0x%08X",rdo,crc_in_data,crc) ;
1002 trg[t_cou].t = d32[0] & 0xFFF ;
1003 trg[t_cou].daq = d32[2] & 0xF ;
1004 trg[t_cou].trg = (d32[2] >> 4) & 0xF ;
1005 trg[t_cou].rhic = d32[4] ;
1009 LOG(NOTE,
"RDO %d: words %d: token %d, trg %d, daq %d: rhic %u: rdo_in_data %d, format_code 0x%X",rdo,words,
1010 trg[0].t, trg[0].trg, trg[0].daq, trg[0].rhic,
1011 rdo_in_dta,format_code) ;
1016 LOG(ERR,
"RDO %d: token 0?",rdo) ;
1021 switch(trg[0].trg) {
1025 LOG(ERR,
"RDO %d: bad trg_cmd %d",rdo, trg[0].trg) ;
1032 #if 0 // skip for the temporary 0x8129 V
1035 int mesg_length = d32[last_ix-1] & 0xFFF ;
1036 if(mesg_length > 30) {
1037 LOG(ERR,
"RDO %d: bad trigger length %d",rdo,mesg_length) ;
1043 for(
int i=0;i<mesg_length;i++) {
1044 u_int
trg_data = d32[last_ix - 2 - i] ;
1048 trg[t_cou].t = (trg_data >> 8) & 0xFFF ;
1049 trg[t_cou].trg = (trg_data >> 4) & 0xF ;
1050 trg[t_cou].daq = trg_data & 0xF ;
1051 trg[t_cou].rhic = (trg_data >> 20) & 0x7FF ;
1055 if(trg_data & 0x80000000) {
1056 LOG(ERR,
"RDO %d: FIFO limit 0x%08X",rdo,trg_data) ;
1063 LOG(ERR,
"RDO %d: too many triggers %d",rdo,t_cou) ;
1084 LOG(WARN,
"RDO %d: words %d: bad %d:",rdo,words,bad) ;
1086 for(
int i=0;i<10;i++) {
1087 LOG(WARN,
"\tRDO %d: %4d: 0x%08X",rdo,i,d32[i]) ;
1090 for(
int i=(words-4);i<words;i++) {
1091 LOG(WARN,
"\tRDO %d: %4d: 0x%08X",rdo,i,d32[i]) ;
1095 else if(trg[0].trg==11) {
1098 for(
int i=10;i<2410;i++) {
1100 u_int b31, b21, b1, b0 ;
1102 b31 = (t_data >> 31) & 1 ;
1103 b21 = (t_data >> 21) & 1 ;
1104 b1 = (t_data >> 1) & 1 ;
1105 b0 = (t_data >> 0) & 1 ;
1107 should = !(b31 ^ b21 ^ b1 ^b0) ;
1108 should = (t_data << 1) | should ;
1110 if(d32[i] != t_data) {
1111 if(!shutup) LOG(WARN,
"word %4d: should 0x%08X, is 0x%08X",i,t_data,d32[i]) ;
1115 if(bad_cou > 2) shutup = 1 ;
1117 should = !(b31 ^ b21 ^ b1 ^b0) ;
1118 should = (t_data << 1) | should ;
1124 if(bad_cou) LOG(ERR,
"RDO %d: bad locations %d",rdo,bad_cou) ;
1132 if(id_check_failed) {
1134 if(rdo_warns[rdo] < 15) {
1135 LOG(CAUTION,
"RDO %d: rdo check failed: expect 0x%02X, found 0x%02X",
1136 rdo,rdo_id[rdo],rdo_in_dta) ;