10 #include "stgc_data_c.h"
12 stgc_data_c::feb_t stgc_data_c::feb[STGC_SECTOR_COU][4][6] ;
13 stgc_data_c::errs_t stgc_data_c::errs[4] ;
15 u_int stgc_data_c::run_type ;
16 u_int stgc_data_c::run_number ;
18 static u_int gray2dec(u_int gray)
24 for(
int i=0;i<11;i++) {
33 stgc_data_c::stgc_data_c()
52 event_any = event_data = 0 ;
57 int stgc_data_c::run_start()
66 const char *stgc_data_c::type_c(u_short type)
85 int stgc_data_c::hdr_check(u_short *d,
int shorts)
99 trg_cmd = daq_cmd = 0 ;
108 LOG(ERR,
"%d: evt %d: corrupt header 0x%04X, shorts %d",rdo1,event_any,d[1],shorts) ;
118 c_type = type_c(evt_type) ;
123 int t_hi = (d[4]>>4)&0xF ;
124 int t_mid = (d[4]>>8)&0xF ;
125 int t_lo = (d[4]>>12)&0xF ;
127 token = (t_hi<<8)|(t_mid<<4)|t_lo ;
134 trg_counter = d[5]>>6 ;
145 mhz_start_evt_marker = (((
unsigned long)d[5]&0x3F)<<32)|((
unsigned long)d[6]<<16)|(
unsigned long)d[7] ;
151 d16_last = d + shorts - 2 ;
154 for(
int i=0;i<shorts;i++) {
156 if(d16_last[-i] != 0xFEED) {
157 d16_last = d + shorts - 2 - i ;
164 if(d16_last[0] != 0xFEEC) {
165 if(realtime) LOG(ERR,
"%d: evt %d: last is 0x%04X",rdo1,event_any,d16_last[0]) ;
170 mhz_stop_evt_marker = (d16_last[-2]<<16)|d16_last[-1] ;
180 adc_cou = d16_last - d16_data ;
184 if((evt_type==0x4544 && (adc_cou%4)) || shorts>40000) {
185 if(realtime) LOG(ERR,
"%d: evt %d: %s: adc_cou is %d (%d), shorts %d!?",rdo1,event_any,c_type,adc_cou,adc_cou/4,shorts) ;
187 for(
int i=0;i<16;i++) {
188 LOG(TERR,
"%d/%d = 0x%04X",i,shorts,d[i]) ;
189 LOG(TERR,
"last %d = 0x%04X",i,d16_last[-i]) ;
210 for(
int i=0;i<4;i++) {
211 response |= (
unsigned long)d[13+i]<<(sh*16) ;
215 u_int feb = (response>>56)&0xF ;
216 if((feb<1)||(feb>6)) sh |= 1 ;
217 if(((response>>48)&0xF0F0)!=0x1000) {
221 if(echo==0 || echo==0x00C0) {
225 if((response & 0xFFFFFFFFFFFFFl) != 0x2C00000000000l) sh |= 8;
229 LOG(ERR,
"S%d:%d: RESPONSE of cmd 0x%04X = 0x%lX: sh 0x%X",sector1,rdo1,echo,response,sh) ;
232 if(realtime>1) LOG(TERR,
"S%d:%d: RESPONSE of cmd 0x%04X",sector1,rdo1,echo) ;
237 trg_cmd = daq_cmd = 0 ;
240 if(realtime>1) LOG(TERR,
"S%d:%d: ECHO of cmd 0x%04X",sector1,rdo1,echo) ;
243 trg_cmd = daq_cmd = 0 ;
247 LOG(NOTE,
"S%d:%d: %d: VERSION 0x%04X: T %d, trg %d, daq %d; shorts %d, ADCs %d; start_mhz %ul, delta %d",sector1,rdo1,
id,version,token,trg_cmd,daq_cmd,
249 mhz_start_evt_marker,mhz_stop_evt_marker-mhz_start_evt_marker) ;
250 LOG(NOTE,
"%d: evts %d: T %d: trg_counter %d",rdo1,event_any,token,trg_counter) ;
254 if(realtime>1) LOG(TERR,
"S%d:%d: %d: T %d, trg %d, daq %d; shorts %d (timer evt)",sector1,rdo1,
id,token,trg_cmd,daq_cmd,shorts) ;
257 trg_cmd = daq_cmd = 0 ;
262 trg_cmd = daq_cmd = 0 ;
265 LOG(ERR,
"S%d:%d: evt %d: T %d, trg %d, daq %d, UNKNOWN type 0x%04X; shorts %d",sector1,rdo1,event_any,token,trg_cmd,daq_cmd,evt_type,shorts) ;
269 trg_cmd = daq_cmd = 0 ;
274 if(evt_type != 0x5244) {
275 if((d[3] & (1<<14))==0) {
276 LOG(ERR,
"%d: %s: RESPONSE FIFO",rdo1,c_type) ;
282 if((d[3] & (1<<13))==0) {
283 LOG(ERR,
"%d: %s: GTP not ready",rdo1,c_type) ;
285 if((d[3] & (1<<12))==0) {
289 if(echo != 0xF010) LOG(ERR,
"%d: %s of echo 0x%04X: RHICx5 PLL not locked",rdo1,c_type,echo) ;
293 if((d[3] & (1<<8))) {
294 if(realtime && errs[rdo1-1].fifo==0) {
295 LOG(ERR,
"%d: %s: EVENT FIFO FULL latched",rdo1,c_type) ;
297 errs[rdo1-1].fifo = 1 ;
299 if((d[3] & (1<<4))==0) {
300 LOG(ERR,
"%d: %s: FEB Config FIFO",rdo1,c_type) ;
304 for(
int i=0;i<6;i++) {
305 fee_status |= (((
unsigned long)st[i]&0xFF)<<(i*8)) ;
314 if(realtime>10) LOG(ERR,
"%d: evt %d: %s: FEB %d status: 0x%X",rdo1,event_any,c_type,i,st[i]) ;
326 int stgc_data_c::start_0001(u_short *d,
int shorts)
332 int stgc_data_c::event_0001()
340 if(adc_cou<=0)
return 0 ;
344 for(
int i=0;i<4;i++) {
348 u_int dd = ((u_int)d[0]<<16)|(u_int)d[1] ;
358 LOG(WARN,
"%d: FEB %d\n",rdo1,feb_id) ;
369 u_short t_hi, t_mid, t_lo ;
372 u_char t_cmd, d_cmd ;
374 unsigned long mhz_trg ;
377 d_cmd = (d[1]>>4) & 0xF ;
378 t_hi = (d[1]>>8)& 0xF ;
379 t_mid = (d[1]>>12)&0xF ;
380 t_lo = (d[0]>>0) & 0xF ;
382 t = (t_hi<<8)|(t_mid<<4)|t_lo ;
384 mhz_hi = (d[0]>>4)&0x1FF ;
385 mhz_trg = ((
unsigned long)mhz_hi<<32)|((
unsigned long)d[2]<<16)|(
unsigned long)d[3] ;
392 if(trg_cou) err |= 4 ;
396 if(t_cmd==0) err |= 8 ;
400 if(trg_cou==1 && datum_ix==2 && realtime<=1) {
409 if(realtime && err) {
410 LOG(ERR,
"%d: evt %d: err 0x%X: 0x%04X 0x%04X 0x%04X 0x%04X at adc_cou %d/%d",rdo1,event_any,err,d[0],d[1],d[2],d[3],datum_ix,adc_cou) ;
411 LOG(ERR,
"%d: evt %d: data: trg_cou %d: T %d, trg %d, daq %d; trg_mhz %lu",rdo1,event_any,trg_cou,t,
412 t_cmd,d_cmd,mhz_trg_marker) ;
413 LOG(ERR,
"%d: evt %d: hdr : trg_counter %d: T %d, trg %d, daq %d; evt_mhz %lu",rdo1,event_any,trg_counter,token,
414 trg_cmd,daq_cmd,mhz_start_evt_marker) ;
427 mhz_trg_marker = mhz_trg ;
465 u_int mhz_adc_marker = dd & 0x1FFFFFFF ;
467 u_int vmm_id = (d[2]>>13)&0x7 ;
468 int crc_ok = (d[2]>>12)&1 ;
469 int channel = (d[2]>>6)&0x3F ;
471 int pdo = (d[2]&0x3F)<<4 ;
472 pdo |= (d[3]>>12)&0xF ;
474 int bcid = d[3]&0xFFF ;
475 bcid = gray2dec(bcid) ;
477 if(crc_ok==0) evt_err |= 0x80 ;
479 if(vmm_id<4) evt_err |= 0x40 ;
483 if(evt_err==0x20 && datum_ix==2 && realtime<=1) {
491 if(evt_err&0x20) strcat(c_err,
"FEBerr ") ;
492 if(evt_err&0x40) strcat(c_err,
"VMMerr ") ;
493 if(evt_err&0x80) strcat(c_err,
"CRCerr ") ;
503 bad_error |= evt_err ;
505 if(realtime>1 && evt_err) {
506 LOG(ERR,
"S%d:%d: evt %d: FEB %d, VMM %d, ch %d: evt_err [%s]0x%X at adc_cou %d/%d, trg_cou %d",sector1,rdo1,event_any,feb_id,
507 vmm_id,channel,c_err,evt_err,datum_ix,adc_cou,trg_cou) ;
510 LOG(ERR,
" 0x%04X 0x%04X 0x%04X 0x%04X",d[0],d[1],d[2],d[3]) ;
520 vmm.feb_vmm = ((feb_id)<<2)|(vmm_id-4) ;
525 int delta = bcid - (mhz_trg_marker%4096) ;
527 if(delta<0) delta += 4096 ;
529 vmm.bcid_delta = delta ;
535 long tb = (long)mhz_adc_marker - (
long)(mhz_trg_marker&0x1FFFFFFF) ;
538 if(tb<-0x8000) vmm.tb = 0x8000 ;
539 else if(tb>0x7FFF) vmm.tb = 0x7FFF ;
544 if(vmm.tb<xing_min || vmm.tb>xing_max) {
559 int stgc_data_c::start(u_short *d,
int shorts)
566 if(hdr_check(d,shorts))
return -1 ;
581 return start_0001(d,shorts) ;
587 LOG(ERR,
"OLD CODE") ;
590 u_short *d16_last = d + shorts - 2 ;
592 for(
int i=0;i<shorts;i++) {
595 if(d16_last[-i] != 0xFEED) {
596 d16_last = d + shorts - 2 - i ;
610 int len = d16_last - d ;
612 LOG(NOTE,
"%d: type 0x%04X: token %d, trg_cmd %d, daq_cmd %d - len %d",rdo1,d[0],token,trg_cmd,daq_cmd,len) ;
627 LOG(NOTE,
"%d: data %d: chs %d: T %d, trg %d, daq %d",rdo1,event_data,len/4,token,trg_cmd,daq_cmd) ;
629 LOG(ERR,
"%d: data len odd %d",rdo1,len) ;
633 LOG(DBG,
"%d: timer",rdo1) ;
635 if((d16_data[0]&0xFF00)==0x0300) feb[sector1-1][rdo1-1][5].present = 1 ;
636 if((d16_data[0]&0xFF)==0x03) feb[sector1-1][rdo1-1][4].present = 1 ;
637 if((d16_data[1]&0xFF00)==0x0300) feb[sector1-1][rdo1-1][3].present = 1 ;
638 if((d16_data[1]&0xFF)==0x03) feb[sector1-1][rdo1-1][2].present = 1 ;
639 if((d16_data[2]&0xFF00)==0x0300) feb[sector1-1][rdo1-1][1].present = 1 ;
640 if((d16_data[2]&0xFF)==0x03) feb[sector1-1][rdo1-1][0].present = 1 ;
643 LOG(DBG,
"%d: echo",rdo1) ;
646 LOG(DBG,
"%d: FEB response",rdo1) ;
649 LOG(ERR,
"%d: unknown event type 0x%04X",rdo1,event_type) ;
656 LOG(NOTE,
"ch_count %d",ch_count) ;
658 if(len<=0)
return 0 ;
665 int stgc_data_c::event()
678 if(version!=0)
return event_0001() ;
692 return event_0001() ;
697 if(ch_count <= 0)
return 0 ;
698 if(event_type != 0x4544)
return 0 ;
700 for(
int i=0;i<4;i++) {
705 if((d[0]&0xF000) != 0x1000) err |= 1 ;
707 rod_id = (d[0]>>4)&0xFF ;
708 feb_id = d[0] & 0xF ;
711 if(rod_id != 1) err |= 2 ;
714 if(feb_id==0 || feb_id>6) err |= 2 ;
718 vmm_id = (d[2] >> 13) & 0x7 ;
720 threshold = (d[2]>>12) & 1 ;
722 channel = (d[2]>>6) & 0x3F ;
724 pdo = (d[2] & 0x3F)<<4 ;
725 pdo |= (d[3]>>12)&0xF ;
728 bcid = gray2dec(bcid) ;
731 if(vmm_id<4) err |= 4 ;
740 LOG(ERR,
"event %d: pkt %3d: rod_id %d, feb_id %d, vmm_id %d, ch %d, trigger %d, threshold %d, pdo %d, bcid %d: error %d",event_data,
741 ch_count,rod_id,feb_id,vmm_id,channel,trigger_id,
742 threshold,pdo,bcid,err) ;
749 vmm.feb_vmm = ((feb_id-1)<<2)|(vmm_id-4) ;
762 int stgc_data_c::event_end(
int flag)