13 #include "fcs_data_c.h"
15 static inline u_int sw16(u_int d)
21 d |= (tmp & 0xFFFF)<<16 ;
32 u_char fcs_data_c::rdo_map_loaded ;
34 u_char fcs_data_c::fcs_bad_ch[8][34] ;
35 u_char fcs_data_c::fcs_bad_ch_all[FCS_SECTOR_COU][8][34] ;
37 u_int fcs_data_c::run_number ;
38 u_int fcs_data_c::run_type ;
41 float fcs_data_c::n_sigma ;
42 float fcs_data_c::n_sigma_hcal ;
43 float fcs_data_c::n_sigma_epd ;
44 short fcs_data_c::n_pre ;
45 short fcs_data_c::n_post ;
46 short fcs_data_c::n_cou ;
47 char fcs_data_c::n_mode ;
50 u_short fcs_data_c::ht_threshold ;
51 u_short fcs_data_c::tb_pre ;
52 u_short fcs_data_c::tb_all ;
54 u_char fcs_data_c::ascii_no ;
56 pthread_mutex_t fcs_data_c::ped_mutex ;
58 fcs_data_c::statistics_t fcs_data_c::statistics[8] ;
60 int fcs_data_c::stage_params_txt[32] ;
63 long fcs_data_c::dep_to_char(int det, int ns, int dep)
66 char *ctmp = (
char *)&ret ;
87 if(ns==0) ctmp[1] =
'N' ;
90 sprintf(ctmp+2,
"%02d",dep) ;
99 const char *fcs_data_c::stage_labels[] = {
100 "FCS_HAD-HERATIO-THR",
101 "FCS_EM-HERATIO-THR",
123 0 2x uint8 (had_ratio_thr << 8) || em_ratio_thr
133 10 2x uint8 (jp_a_thr_1 << 8) || jp_a_thr_0
134 11 2x uint8 (0x00 << 8) || jp_a_thr_2
135 12 2x uint8 (jp_bc_thr_1 << 8) || jp_bc_thr_0
136 13 2x uint8 (jp_bc_thr_d << 8) || jp_bc_thr_2
137 14 2x uint8 (jp_de_thr_1 << 8) || jp_de_thr_0
138 15 2x uint8 (jp_de_thr_d << 8) || jp_de_thr_2
141 18 2x uint8 (hcal_ht_thr << 8) || ecal_ht_thr
148 const char *fcs_data_c::stage_labels[32] = {
149 "FCS_HAD-HERATIO-THR",
150 "FCS_EM-HERATIO-THR",
190 int fcs_data_c::load_stage_params(
int sec1,
const char *fname)
193 fname=
"/RTS/conf/fcs/stage_params.txt" ;
196 FILE *f = fopen(fname,
"r") ;
198 LOG(ERR,
"sector %2d: %s: [%s]",sec1,fname,strerror(errno)) ;
202 LOG(INFO,
"sector %2d: stage_params %s opened",sec1,fname) ;
213 if(fgets(buff,
sizeof(buff),f)==0) continue ;
215 if(buff[0]==
'#') continue ;
216 if(buff[0]==
'\n') continue ;
222 int ret = sscanf(buff,
"%d %d %d %s %d",&dummy,&dummy,&ix,name,&val) ;
223 if(ret != 5) continue ;
228 for(u_int i=0;i<
sizeof(stage_labels)/
sizeof(stage_labels[0]);i++) {
229 if(stage_labels[i]==0) continue ;
231 if(strcasecmp(stage_labels[i],name)==0) {
232 stage_params_txt[i] = val ;
234 if(i>max_i) max_i = i ;
241 LOG(ERR,
"stage_param_txt [%s]=%d NOT coded locally",name,val) ;
244 LOG(INFO,
"stage_param_txt [%s]=%d, index %d, ix %d",name,val,got_it,ix) ;
250 for(u_int i=0;i<max_i;i++) {
251 LOG(TERR,
"stage_params_txt: %d/%d = %d",i,max_i,stage_params_txt[i]) ;
261 int fcs_data_c::zs_start(u_short *buff)
270 if(hdr_det==2) sigma = n_sigma_epd ;
271 else if(hdr_det==1) sigma = n_sigma_hcal ;
272 else sigma = n_sigma ;
275 if(ch >= 32) is_trg = 1 ;
276 if(hdr_det >= 3) is_trg = 1;
286 i_ped = (int)(ped[sector-1][rdo-1].mean[ch]+0.5) ;
288 LOG(DBG,
"S%d:%d:%d mean %f, n_sigma %f, rms %f",
290 (
float)ped[sector-1][rdo-1].mean[ch],
292 (
float)ped[sector-1][rdo-1].rms[ch]) ;
297 thr = (int)(ped[sector-1][rdo-1].mean[ch] + sigma * ped[sector-1][rdo-1].rms[ch] + 0.5) ;
300 thr = (int)(ped[sector-1][rdo-1].mean[ch] + sigma) ;
316 for(
int i=0;i<tb_cou;i++) {
317 short i_adc = adc[i] & 0xFFF ;
327 t_stop = t_start + t_cou ;
330 if(t_start < 0) t_start = 0 ;
333 if(t_stop > tb_cou) t_stop = tb_cou ;
336 memset(mark,0,tb_cou) ;
340 for(;t_start<t_stop;t_start++) {
359 LOG(DBG,
"RDO %d, ch %d, q_ped %d, i_ped %d",rdo,ch,q_ped,i_ped) ;
363 statistics[rdo-1].odd_ped[ch]++ ;
370 t_stop = t_start + t_cou ;
373 if(t_start < 0) t_start = 0 ;
376 if(t_stop > tb_cou) t_stop = tb_cou ;
379 memset(mark,0,tb_cou) ;
384 for(;t_start<t_stop;t_start++) {
391 LOG(DBG,
"RDO %d, ch %d: thr %d: got %d",rdo,ch,thr,got_one) ;
393 if(got_one==0)
return 0 ;
397 dp = (u_short *)buff ;
403 u_short *dstart = dp ;
410 u_short *t_cou_p = dp + 1 ;
413 for(
int i=0;i<tb_cou;i++) {
424 short i_adc = adc[i] & 0xFFF ;
425 short fla = adc[i] >> 12 ;
431 if(i_adc < 0) i_adc = 0 ;
457 dstart[1] = seq_cou ;
462 if(seq_cou == 0)
return 0 ;
471 int fcs_data_c::start(u_short *d16,
int shorts)
480 dta_start = dta_p = d16 ;
481 dta_stop = d16 + shorts ;
482 dta_shorts = shorts ;
501 if(d[0]==0xDDDDDDDD) {
510 version = sw16(d[2]) ;
514 version = 0x18040000 ;
520 version = 0x18050000 ;
522 dta_p = ((u_short *)d)+6 ;
525 for(
int i=0;i<16;i++) {
526 LOG(TERR,
"...data9801: %d = 0x%04X",i,dta_p[i]) ;
533 version = 0x18110000 ;
540 version = 0x30050000 ;
548 LOG(ERR,
"uknown version 0x%04X",d16[2]) ;
556 dta_p = (u_short *) d ;
559 for(
int i=0;i<8;i++) {
560 LOG(TERR,
"...data: %d = 0x%04X",i,dta_p[i]) ;
570 while(dta_p < dta_stop) {
571 if(*dta_p++ == 0xFD06) {
591 int fcs_data_c::hdr_event()
593 u_short *start_p = dta_p ;
604 hdr_board_id = dta_p[3] ;
607 hdr_sector = ((hdr_board_id >> 11) & 0x1F)+1 ;
608 hdr_rdo = ((hdr_board_id >> 8) & 0x7)+1 ;
610 hdr_det = (hdr_board_id >> 6) & 0x3 ;
611 hdr_ns = (hdr_board_id >> 5) & 1 ;
612 hdr_dep = hdr_board_id & 0x1F ;
618 if((sector != hdr_sector) || (rdo != hdr_rdo)) {
620 LOG(ERR,
"%d: sector %d:%d expected, received %d:%d [0x%X]",
id,sector,rdo,hdr_sector,hdr_rdo,hdr_board_id) ;
624 if(realtime && (hdr_board_id != board_id)) {
626 LOG(ERR,
"%d: evt %d: board_id: expected 0x%04X, received 0x%04X",
id,events,board_id,hdr_board_id) ;
630 hdr_trg_word = ((dta_p[5]&0xF)<<16) | dta_p[4] ;
631 hdr_rhic_counter = (dta_p[7]<<16)|dta_p[6] ;
634 LOG(NOTE,
"HDR S%d:%d: trg_word 0x%05X, RHIC %u, upper 0x%03X",hdr_sector,hdr_rdo, hdr_trg_word,hdr_rhic_counter,dta_p[5]>>4) ;
636 trg_cmd = hdr_trg_word & 0xF ;
637 daq_cmd = (hdr_trg_word>>4) & 0xF ;
638 token = ((hdr_trg_word>>8)&0xF)<<8 ;
639 token |= ((hdr_trg_word>>12)&0xF)<<4 ;
640 token |= ((hdr_trg_word>>16)&0xF) ;
642 LOG(NOTE,
"HDR: token %d, trg_cmd %d, daq_cmd %d",token,trg_cmd,daq_cmd) ;
644 if(version == 0x30050000) {
660 if(dta_p[0]==0xEEEE && dta_p[1]==0xEEEE) {
664 u_int *d32 = (u_int *)dta_p ;
666 int words = (dta_shorts - 8 - 2)/2 ;
669 ascii_p = (
char *)dta_p ;
670 ascii_words = words ;
672 if(ascii_no==0) LOG(TERR,
"ASCII contribution - words %d[%d]: sector %d, rdo %d, hdr_trg_word 0x%X, hdr_board 0x%X",words,dta_shorts,sector,rdo,hdr_trg_word,hdr_board_id) ;
676 for(
int i=0;i<words;i++) {
679 if((asc&0xFF00FFFF)==0xF5009800) {
680 char c = (asc>>16)&0xFF ;
682 if(cou>
sizeof(ctmp)) ;
691 if(ascii_no==0) LOG(TERR,
"S%d:%d:%d: \"%s\"",sector,rdo,events,ctmp) ;
695 if((c=strstr(ctmp,
"r0 7"))) {
696 sscanf(c,
"r0 7 %f",&f_val) ;
699 statistics[rdo-1].ht_rate = (int) f_val ;
704 else if((c=strstr(ctmp,
"t W "))) {
705 sscanf(c,
"t W %f",&f_val) ;
708 statistics[rdo-1].temperature = f_val ;
711 else if((c=strstr(ctmp,
"tb "))) {
712 sscanf(c,
"tb 0x%X",&i_val) ;
714 f_val = 100.0*(double)(i_val & 0x3FF)/1023.0 ;
717 statistics[rdo-1].deadtime = f_val ;
727 else if((c=strstr(ctmp,
"b "))) {
728 sscanf(c,
"b %f",&f_val) ;
731 statistics[rdo-1].deadtime = f_val ;
734 else if((c=strstr(ctmp,
"rg 7 "))) {
735 sscanf(c,
"rg 7 0x%X",&i_val) ;
737 f_val = 100.0*(double)(i_val & 0x3ff)/1023.0 ;
741 statistics[rdo-1].rx_throttle = f_val ;
760 else if(asc != 0xFFFFFFFF) {
762 LOG(ERR,
"ASCII wha %d: 0x%08X",i,asc) ;
767 if(asc==0xFFFFFFFF) {
777 LOG(ERR,
"S%d:%d:%d: ASCII[%d] but no end-marker \"%s\"",sector,rdo,events,cou,ctmp) ;
781 LOG(ERR,
"S%d:%d:%d: ASCII[%d] \"%s\"",sector,rdo,events,cou,ctmp) ;
785 else if(dta_p[0]==0xFFFF && dta_p[1]==0xFFFF) {
787 LOG(ERR,
"S%d:%d:%d: ASCII bug: 0x%X, 0x%X",sector,rdo,events,hdr_trg_word,dta_p[2]) ;
788 for(
int i=0;i<32;i++) {
789 LOG(TERR,
"... %d = 0x%04X",i,start_p[i]) ;
794 else if(dta_p[0]==0xFFFF) {
797 LOG(ERR,
"BAD 0xFFFF bug -- 0x%08X",*((u_int *)dta_p)) ;
799 u_int *d32 = (u_int *)start_p ;
801 for(
int i=0;i<32;i++) {
802 LOG(ERR,
"... %d = 0x%04X",i,d32[i]) ;
812 if(dta_p[0]==0xE800) {
827 int fcs_data_c::event_end(
int how)
829 if(!trgd_event)
return 0 ;
831 if(rdo_map_loaded && (ch_mask_seen != rdo_map[sector-1][rdo-1].ch_mask)) {
833 LOG(ERR,
"%d: event_end: %d: S%02d:%d: mask not-complete 0x%llX (T %d)",
id,events,sector,rdo,ch_mask_seen,token) ;
840 int fcs_data_c::event_stream()
842 u_int *d = (u_int *)dta_p ;
843 u_int *d_stop = (u_int *)dta_stop ;
847 u_char want_log = 0 ;
848 u_char end_seen = 0 ;
857 if(d_stop[0]==0x5C || d_stop[0]==0xAAAABBBB) ;
861 for(
int i=-8;i<=8;i++) {
862 LOG(ERR,
"%d: d_stop 0x%08X",i,d_stop[i]) ;
868 u_int old_slice = d[2] ;
869 u_int pkt_counter = d[5] ;
871 u_int end_slice = 0xdeadbeef ;
872 u_int pkt_status = 0xdeadbeef ;
879 memset(mean,0,
sizeof(mean)) ;
880 memset(rms,0,
sizeof(rms)) ;
881 memset(cou,0,
sizeof(cou)) ;
885 memset(ch_dead,0,
sizeof(ch_dead)) ;
896 u_int *adc32 = (u_int *)adc ;
903 u_int got_adc_end = 0 ;
919 if((dta & 0xF)==0xC) {
920 sync = (dta>>20) & 3 ;
926 adc_ch = dta & 0x1F ;
928 if(got_adc_end & (1<<adc_ch)) {
929 LOG(ERR,
"Ch %d -- adc-slice_end already received",adc_ch) ;
931 got_adc_end |= (1<<adc_ch) ;
933 LOG(WARN,
"ADC slice-end 0x%08X at RHIC %u, sync %d, ch %d",dta,(dta>>8)&0xFFFF,(dta>>24)&3,
937 adc_ch = dta & 0x1F ;
939 if(((dta>>24)&0x3)!= sync) {
947 if(adc_cou%4 || adc_cou==0) {
948 LOG(ERR,
"ADC ch %d, adc_cou %d??",adc_ch,adc_cou) ;
949 for(
int i=2;i>=-20;i--) {
950 LOG(ERR,
" prev %d 0x%08X",i,d[i]) ;
956 xings += adc_cou / 4 ;
958 for(
int i=0;i<adc_cou;i++) {
959 u_int d_lo = adc32[i] & 0xFFF ;
960 u_int d_hi = (adc32[i]>>16) & 0xFFF ;
962 mean[adc_ch] += d_lo ;
963 rms[adc_ch] += d_lo * d_lo ;
966 mean[adc_ch] += d_hi ;
967 rms[adc_ch] += d_hi * d_hi ;
971 if(((adc32[i]>>13)&3)!=sync) {
972 LOG(ERR,
"ADC ch %d: 0x%08X: bad sync: expect %d",adc_ch,adc32[i],sync) ;
974 if(((adc32[i]>>(13+16))&3)!=sync) {
975 LOG(ERR,
"ADC ch %d: 0x%08X: bad sync: expect %d",adc_ch,adc32[i],sync) ;
987 LOG(WARN,
"DEAD but unfinished ADCs %d on ch %d",adc_cou,adc_ch) ;
999 u_int d_end = (dta>>8) & 0xFFFF ;
1000 u_int ch = dta & 0x1F ;
1005 ch_dead[ch] += d_end ;
1017 LOG(ERR,
"Unknown packet 0x%X",t) ;
1021 LOG(ERR,
"Too many ADCs %d after ch %d",adc_cou,adc_ch) ;
1024 adc32[adc_cou] = dta ;
1036 if(got_adc_end != 0xFFFFFFFF) {
1037 LOG(ERR,
"ADCs got end 0x%08X",got_adc_end) ;
1042 LOG(ERR,
"ADCs with bad sync %d",bad_sync) ;
1046 if(want_log || !end_seen || deadtime || pkt_status || (slice==old_slice) || (slice != end_slice)) {
1048 LOG(ERR,
"Packet 0x%08X:0x%08X:0x%08X:%d, shorts %d - status 0x%08X: xings %d, trgs %d, deadtime %d",
1049 slice,old_slice,end_slice,pkt_counter,dta_shorts,
1051 xings,trgs,deadtime) ;
1054 for(
int i=0;i<32;i++) {
1056 LOG(ERR,
"\t deadtime %d=%d",i,ch_dead[i]) ;
1063 for(
int i=0;i<32;i++) {
1064 if(cou[i]==0) continue ;
1069 rms[i] = sqrt(rms[i]-mean[i]*mean[i]) ;
1071 if(cou[i]>10000) LOG(TERR,
"Ch %d: %f +- %f, cou %d -- %d",i,mean[i],rms[i],cou[i],(
int)(mean[i]*8.0+0.5)) ;
1073 if(run_type==1) printf(
"PED %d %f %f %d %d\n",i,mean[i],rms[i],cou[i],(
int)(mean[i]*8.0+0.5)) ;
1082 int fcs_data_c::event()
1085 if(version != 0x18110000) {
1086 if(version == 0x30050000) {
1087 return event_stream() ;
1089 return event_pre_fy19() ;
1097 if(dta_p[0]==0xE800) {
1104 while(*dta_p == 0xFFFF) {
1109 LOG(ERR,
"S%d:%d: events %d: BUG 0xFFFF: ch %d, bytes left %d",sector,rdo,events,ch_count,dta_stop-dta_p) ;
1110 LOG(ERR,
" 0x%X 0x%X 0x%X",dta_p[1],dta_p[2],dta_p[3]) ;
1129 u_int rhic_cou_xpect = hdr_rhic_counter & 0x7F ;
1130 u_int board_id_xpect = board_id & 0xFF ;
1139 while(dta_p<dta_stop) {
1144 u_char complain = 0 ;
1145 u_short *dbg_h = dta_p ;
1152 board = (h[0] >> 6) ;
1154 trg_word = ((h[2]&0xFF)<<12)|(h[1]) ;
1155 rhic_cou = h[2]>>8 ;
1161 if(board_id_xpect != board) complain = 1 ;
1163 if(ch>36) complain = 1 ;
1165 if(ch_mask_seen & (1LL<<ch)) {
1166 if(realtime) LOG(ERR,
"event %d: ch duplicate %d",events,ch) ;
1169 ch_mask_seen |= (1LL<<ch) ;
1172 if((hdr_trg_word!=trg_word)|(rhic_cou_xpect!=rhic_cou)) {
1179 if(realtime && err_count<10) {
1180 LOG(ERR,
"%d: S%d:%d: Evt %d, ch %d[%d]: 0x%X 0x%05X %d expected: 0x%X 0x%05X %d seen",
id,sector,rdo,
1182 board_id_xpect,hdr_trg_word,rhic_cou_xpect,
1183 board,trg_word,rhic_cou) ;
1185 LOG(ERR,
"%d: 0x%04X 0x%04X 0x%04X 0x%04X",
id,dbg_h[-1],dbg_h[0],dbg_h[1],dbg_h[2]) ;
1192 while(dta_p<dta_stop) {
1193 u_short d = *dta_p++ ;
1201 else if(d & 0x8000) {
1203 if(realtime) LOG(ERR,
"... ch %d: tb_cou %d: 0x%04X",ch,tb_cou,d) ;
1207 if((u_int)tb_cou>=(
sizeof(adc)/
sizeof(adc[0]))) {
1209 LOG(ERR,
"Event too big, ch %d, tb %d",ch,tb_cou) ;
1224 if(first_tb_cou==0) {
1225 first_tb_cou = tb_cou ;
1227 else if(tb_cou != first_tb_cou) {
1229 if(complain) LOG(ERR,
"%d: ch length mismatch: expect %d, is %d: ch %d(%d)",rdo,first_tb_cou,tb_cou,ch,ch_count) ;
1250 int fcs_data_c::ana_ch()
1263 if(run_type==2 && sector==11 && trg_cmd==4) {
1267 static u_char expect[3][37] ;
1275 expect[1][34]=0xC1 ;
1276 expect[1][35]=0xC1 ;
1277 expect[1][36]=0x66 ;
1279 expect[2][34]=0xE1 ;
1280 expect[2][35]=0xE1 ;
1281 expect[2][36]=0x77 ;
1283 for(
int i=0;i<20;i++) {
1285 expect[2][i]=(1<<5)|i ;
1288 for(
int i=20;i<28;i++) {
1289 expect[1][i] = (1<<6)|(i-20) ;
1290 expect[2][i] = (1<<6)|(1<<5)|(i-20) ;
1293 for(
int i=28;i<34;i++) {
1294 expect[1][i] = (2<<6)|(i-28) ;
1295 expect[2][i] = (2<<6)|(1<<5)|(i-28) ;
1302 for(
int tb=0;tb<tb_cou;tb++) {
1304 int d = adc[tb] & 0xFF ;
1307 if(d && (expect[r][ch] != d)) {
1311 else if(expect[r][ch] != d) {
1319 ped[sector-1][rdo-1].cou[ch]++ ;
1320 ped[sector-1][rdo-1].bad_4[ch] += errs ;
1328 if(ch>=32 || sector==11)
return 0 ;
1337 if((adc[0] & 0xFFF)==0x555) {
1342 for(
int tb=0;tb<tb_cou;tb++) {
1343 u_int iadc = adc[tb] & 0xFFF ;
1346 if(aaa[tb%2] != iadc) {
1347 ped[sector-1][rdo-1].bad_4[ch]++ ;
1351 double sadc = (double)iadc ;
1353 ped[sector-1][rdo-1].mean[ch] += sadc ;
1354 ped[sector-1][rdo-1].rms[ch] += sadc * sadc ;
1355 ped[sector-1][rdo-1].cou[ch]++ ;
1358 ped[sector-1][rdo-1].tmp_val_8[ch] += sadc ;
1359 ped[sector-1][rdo-1].tmp_cou_8[ch]++ ;
1362 if(ped[sector-1][rdo-1].tmp_cou_8[ch]==8) {
1363 double d = ped[sector-1][rdo-1].tmp_val_8[ch] ;
1365 ped[sector-1][rdo-1].mean_8[ch] += d ;
1366 ped[sector-1][rdo-1].rms_8[ch] += d*d ;
1367 ped[sector-1][rdo-1].cou_8[ch]++ ;
1369 ped[sector-1][rdo-1].tmp_val_8[ch] = 0.0 ;
1370 ped[sector-1][rdo-1].tmp_cou_8[ch] = 0 ;
1381 int fcs_data_c::accum_pre_fy19(u_int ch, u_int tb, u_short sadc)
1386 if(tb>=(
sizeof(adc)/
sizeof(adc[0]))) {
1393 if(ch>=32)
return 0 ;
1404 ped[sector-1][rdo-1].mean[ch] += (double)sadc ;
1405 ped[sector-1][rdo-1].rms[ch] += (double)sadc * (
double)sadc ;
1406 ped[sector-1][rdo-1].cou[ch]++ ;
1409 ped[sector-1][rdo-1].tmp_val_8[ch] += (double)sadc ;
1410 ped[sector-1][rdo-1].tmp_cou_8[ch]++ ;
1412 if(ped[sector-1][rdo-1].tmp_cou_8[ch]==8) {
1413 double d = ped[sector-1][rdo-1].tmp_val_8[ch] ;
1415 ped[sector-1][rdo-1].mean_8[ch] += d ;
1416 ped[sector-1][rdo-1].rms_8[ch] += d*d ;
1417 ped[sector-1][rdo-1].cou_8[ch]++ ;
1419 ped[sector-1][rdo-1].tmp_val_8[ch] = 0.0 ;
1420 ped[sector-1][rdo-1].tmp_cou_8[ch] = 0 ;
1434 void fcs_data_c::run_start(u_int run,
int type)
1449 memset(&statistics,0,
sizeof(statistics)) ;
1459 void fcs_data_c::run_stop(
int bad_ped)
1472 void fcs_data_c::ped_start()
1477 memset(ped[s][r].mean,0,
sizeof(ped[s][r].mean)) ;
1478 memset(ped[s][r].rms,0,
sizeof(ped[s][r].rms)) ;
1479 memset(ped[s][r].cou,0,
sizeof(ped[s][r].cou)) ;
1481 memset(ped[s][r].bad_4,0,
sizeof(ped[s][r].bad_4)) ;
1483 memset(ped[s][r].mean_8,0,
sizeof(ped[s][r].mean_8)) ;
1484 memset(ped[s][r].rms_8,0,
sizeof(ped[s][r].rms_8)) ;
1485 memset(ped[s][r].cou_8,0,
sizeof(ped[s][r].cou_8)) ;
1487 memset(ped[s][r].tmp_val_8,0,
sizeof(ped[s][r].tmp_val_8)) ;
1488 memset(ped[s][r].tmp_cou_8,0,
sizeof(ped[s][r].tmp_cou_8)) ;
1493 void fcs_data_c::ped_stop(
int bad_ped)
1497 int s = sector - 1 ;
1502 if(sector==11 && run_type==1) return ;
1511 for(
int c=0;c<32;c++) {
1512 if(ped[s][r].cou[c] > max_c) {
1513 max_c = ped[s][r].cou[c] ;
1517 if(max_c < 500) bad_ped |= 4 ;
1519 for(
int c=0;c<32;c++) {
1521 if(ped[s][r].cou[c]) {
1522 ped[s][r].mean[c] /= ped[s][r].cou[c] ;
1523 ped[s][r].rms[c] /= ped[s][r].cou[c] ;
1525 ped[s][r].rms[c] -= ped[s][r].mean[c] * ped[s][r].mean[c] ;
1527 if(ped[s][r].rms[c] < 0.0) ped[s][r].rms[c] = 0.0 ;
1529 ped[s][r].rms[c] = sqrt(ped[s][r].rms[c]) ;
1532 ped[s][r].mean[c] = -1.0 ;
1533 ped[s][r].rms[c] = 0.0 ;
1537 if(ped[s][r].cou_8[c]) {
1538 ped[s][r].mean_8[c] /= ped[s][r].cou_8[c] ;
1539 ped[s][r].rms_8[c] /= ped[s][r].cou_8[c] ;
1541 ped[s][r].rms_8[c] -= ped[s][r].mean_8[c] * ped[s][r].mean_8[c] ;
1543 if(ped[s][r].rms_8[c] < 0.0) ped[s][r].rms_8[c] = 0.0 ;
1545 ped[s][r].rms_8[c] = sqrt(ped[s][r].rms_8[c]) ;
1550 ped[s][r].mean_8[c] = -1.0 ;
1551 ped[s][r].rms_8[c] = 0.0 ;
1558 strcpy(status,
" -- Bad: ") ;
1559 if(bad_ped & 1) strcat(status,
"wrong_run_type,") ;
1560 if(bad_ped & 2) strcat(status,
"beam_in_rhic,") ;
1561 if(bad_ped & 4) strcat(status,
"not_enough_events") ;
1563 LOG(ERR,
"S%d:%d pedestals %s",sector,rdo,status) ;
1566 strcpy(status,
" -- Status OK") ;
1572 time_t now = time(0) ;
1573 struct tm *tm = localtime(&now) ;
1578 sprintf(fname,
"/RTScache/fcs_pedestals_s%02d_r%d_t%d_%08u_f%u.txt",sector,rdo,run_type,run_number,rhic_freq) ;
1581 sprintf(fname,
"/RTScache/fcs_pedestals_%d_%d_%d_%d_%d.txt",
1589 pedf = fopen(fname,
"w") ;
1591 LOG(ERR,
"Can't open %s [%s]",fname,strerror(errno)) ;
1595 int d = rdo_map[s][r].det ;
1596 int n = rdo_map[s][r].ns ;
1597 int p = rdo_map[s][r].dep ;
1599 fprintf(pedf,
"# Sector %2d, RDO %d\n",sector,rdo) ;
1600 fprintf(pedf,
"# Det %d, NS %d, DEP %d\n",d,n,p) ;
1601 fprintf(pedf,
"# RUN %08u, type %d %s\n",run_number,run_type,status) ;
1602 fprintf(pedf,
"# TIME %u\n",(
unsigned int)now) ;
1603 char *ctm = ctime(&now) ;
1604 fprintf(pedf,
"# DATE %s",ctm) ;
1605 fprintf(pedf,
"# RHIC %u, FEE state %d\n",rhic_freq,fee_state) ;
1607 fprintf(pedf,
"\n") ;
1612 if((r+1)==5) c_max = 4 ;
1617 for(
int c=0;c<c_max;c++) {
1619 double m = ped[s][r].mean[c] ;
1620 double rms = ped[s][r].rms[c] ;
1624 if((m<3.0)||(m>200.0)||(rms<0.3)||(rms>2.2)) err = 1 ;
1627 if(ped[s][r].bad_4[c]) {
1635 const char *masked ;
1637 if(ped[s][r].i_gain[c]==0) masked =
" -- MASKED" ;
1640 LOG(ERR,
"S%02d:%d ch %02d: ped %.1f, rms %.1f: bad cou %u%s",sector,rdo,c,m,rms,ped[s][r].bad_4[c],masked) ;
1648 double rms8 = ped[s][r].rms_8[c] ;
1650 if(run_type==2) rms8 = ped[s][r].bad_4[c] ;
1652 fprintf(pedf,
"%d %d %d %d %d %d %f %f %f %f\n",sector,rdo,d,n,p,c,
1653 ped[s][r].mean[c],ped[s][r].rms[c],
1654 ped[s][r].mean_8[c],rms8) ;
1661 if(!bad_ped && run_type==1) {
1663 sprintf(fname,
"/RTScache/fcs_pedestals_s%02d_r%d_f%u.txt",sector,rdo,rhic_freq) ;
1665 LOG(WARN,
"also making pedestals formal to [%s]",fname) ;
1667 pedf = fopen(fname,
"w") ;
1669 LOG(ERR,
"Can't open %s [%s]",fname,strerror(errno)) ;
1674 int d = rdo_map[s][r].det ;
1675 int n = rdo_map[s][r].ns ;
1676 int p = rdo_map[s][r].dep ;
1678 fprintf(pedf,
"# Sector %2d, RDO %d\n",sector,rdo) ;
1679 fprintf(pedf,
"# Det %d, NS %d, DEP %d\n",d,n,p) ;
1680 fprintf(pedf,
"# RUN %08u, type %d %s\n",run_number,run_type,status) ;
1681 fprintf(pedf,
"# TIME %u\n",(
unsigned int)now) ;
1682 char *ctm = ctime(&now) ;
1683 fprintf(pedf,
"# DATE %s",ctm) ;
1684 fprintf(pedf,
"# RHIC %u, FEE state %d\n",rhic_freq,fee_state) ;
1686 fprintf(pedf,
"\n") ;
1691 if((r+1)==5) c_max = 4 ;
1696 for(
int c=0;c<c_max;c++) {
1700 double rms8 = ped[s][r].rms_8[c] ;
1702 if(run_type==2) rms8 = ped[s][r].bad_4[c] ;
1704 fprintf(pedf,
"%d %d %d %d %d %d %f %f %f %f\n",sector,rdo,d,n,p,c,
1705 ped[s][r].mean[c],ped[s][r].rms[c],
1706 ped[s][r].mean_8[c],rms8) ;
1713 LOG(ERR,
"S%d:%d: not caching pedestals 0x%X",sector,rdo,bad_ped) ;
1719 int fcs_data_c::gain_from_cache(
const char *fname)
1722 const char *file_name ;
1726 if(!rdo_map_loaded) {
1727 LOG(ERR,
"You must load the rdo map before!") ;
1731 for(
int s=0;s<FCS_SECTOR_COU;s++) {
1732 for(
int i=0;i<8;i++) {
1733 for(
int c=0;c<32;c++) {
1734 ped[s][i].el_gain[c] = 1.0 ;
1735 ped[s][i].et_gain[c] = 1.0 ;
1741 file_name =
"/RTS/conf/fcs" ;
1747 ret = stat(file_name,&sstat) ;
1749 LOG(ERR,
"gain_from_cache: %s: [%s]",file_name,strerror(errno)) ;
1753 if(sstat.st_mode & S_IFDIR) {
1756 else if(sstat.st_mode & S_IFREG) {
1760 LOG(ERR,
"gain_from_cache: %s: incorrect file type",file_name) ;
1764 for(
int det_ix=0;det_ix<3;det_ix++) {
1765 for(
int v=0;v<2;v++) {
1769 strncpy(ff,file_name,
sizeof(ff)-1) ;
1772 const char *c_det, *c_typ ;
1786 if(v==0) c_typ =
"electronics" ;
1789 sprintf(ff,
"%s/fcs_%s_%s_gains.txt",file_name,c_det,c_typ) ;
1792 FILE *f = fopen(ff,
"r") ;
1794 LOG(ERR,
"gain_from_cache: %s [%s] (perhaps not an error)",ff,strerror(errno)) ;
1795 if(!is_dir)
goto read_done ;
1799 LOG(INFO,
"gain_from_cache: Opened gains[%s] %s",v==0?
"electronics":
"Et",ff) ;
1805 if(fgets(buff,
sizeof(buff),f)==0) continue ;
1807 if(buff[0]==
'#') continue ;
1808 if(buff[0]==0) continue ;
1814 int ret = sscanf(buff,
"%d %d %d %d %f",&det,&ns,&dep,&ch,&gain) ;
1816 if(ret!=5) continue ;
1818 if(is_dir && (det_ix != det)) {
1819 LOG(WARN,
"det expect %d, det in file %d",det_ix,det) ;
1825 int s = det_map[det][ns][dep].sector - 1 ;
1826 int r = det_map[det][ns][dep].rdo - 1 ;
1828 if(s<0 || r<0) continue ;
1829 if(ch < 0) continue ;
1832 ped[s][r].el_gain[ch] = gain ;
1835 ped[s][r].et_gain[ch] = gain ;
1840 if(!is_dir)
goto read_done ;
1845 for(
int s=0;s<FCS_SECTOR_COU;s++) {
1846 for(
int i=0;i<8;i++) {
1847 for(
int c=0;c<32;c++) {
1849 double d = ped[s][i].el_gain[c] * ped[s][i].et_gain[c] ;
1854 ped[s][i].i_gain[c] = (u_int)(d*256.0+0.5) ;
1856 if(ped[s][i].i_gain[c]>4095) {
1857 LOG(NOTE,
"S%d:%d: ch %d -- gain correction too big",s+1,i+1,c,ped[s][i].i_gain[c]) ;
1874 int fcs_data_c::ped_from_cache(
const char *ff)
1876 FILE *f = fopen(ff,
"r") ;
1878 LOG(ERR,
"ped_from_cache: can't open %s [%s]",ff,strerror(errno)) ;
1882 LOG(INFO,
"ped_from_cache: opened %s",ff) ;
1887 if(fgets(buff,
sizeof(buff),f)==0) continue ;
1889 if(buff[0]==
'#') continue ;
1890 if(buff[0]==0) continue ;
1894 int ss,rrd,dd,nn,ppp ;
1896 int ret = sscanf(buff,
"%d %d %d %d %d %d %f %f %f %f",&ss,&rrd,&dd,&nn,&ppp,&c,&p,&r,&pp,&rr) ;
1898 if(ret!=10) continue ;
1902 ped[ss-1][rrd-1].mean[c] = p ;
1903 ped[ss-1][rrd-1].rms[c] = r ;
1904 ped[ss-1][rrd-1].cou[c] = 0 ;
1907 ped[ss-1][rrd-1].mean_8[c] = pp ;
1908 ped[ss-1][rrd-1].rms_8[c] = rr ;
1909 ped[ss-1][rrd-1].cou_8[c] = 0 ;
1911 u_short pppp = (u_short)(pp+0.5) ;
1917 ped[ss-1][rrd-1].i_ped[c] = pppp ;
1927 int fcs_data_c::event_pre_fy19()
1935 LOG(TERR,
"event() version 0x%08X",version) ;
1938 while(dta_p<dta_stop) {
1944 for(
int i=0;i<128;i++) printf(
"%d 0x%04X\n",i,dta_p[i]) ;
1950 if(h[0]==0xFD07 || h[0]==0x5800) {
1954 if(version==0x28010518) dta_p++ ;
1962 if((dta_p[0]==0xFD07) || (dta_p[0]==0x5800)) break ;
1963 if((dta_p[0]==0x0066) && (dta_p[1]==0x7788)) break ;
1974 while(dta_p<dta_stop) {
1975 u_short d = *dta_p++ ;
1988 if(first_rhic_strobe_tick < 0) {
1989 first_rhic_strobe_tick = tb_cou ;
1994 if(trigger_tick < 0) {
1995 trigger_tick = tb_cou ;
2002 if(accum_pre_fy19(ch,tb_cou,d)<0) {
2003 LOG(ERR,
"Event too big, ch %d, tb %d",ch,tb_cou) ;
2023 int fcs_data_c::load_readout_map(
const char *fname)
2028 if(rdo_map_loaded==0) {
2029 LOG(ERR,
"rdo_map not loaded!") ;
2033 for(u_int dd=0;dd<4;dd++) {
2038 fn =
"/RTS/conf/fcs/fcs_ecal_readout_map.csv" ;
2041 fn =
"/RTS/conf/fcs/fcs_hcal_readout_map.csv" ;
2044 fn =
"/RTS/conf/fcs/fcs_fpre_readout_map.csv" ;
2047 fn =
"/RTS/conf/fcs/fcs_main_readout_map.csv" ;
2054 FILE *f = fopen(fn,
"r") ;
2056 if(f) LOG(INFO,
"load_readout_map: opened %s",fn) ;
2058 LOG(ERR,
"load_readout_map: %s [%s]",fn,strerror(errno)) ;
2065 u_int det, ns, dep, ch ;
2068 if(fgets(buff,
sizeof(buff),f)==0) continue ;
2070 if(buff[0]==
'#') continue ;
2071 if(buff[0]==
'\n') continue ;
2072 if(buff[0]==0) continue ;
2074 int ret = sscanf(buff,
"%d %d %d %d %d %d %d %d %d %d",
2075 &adet,&
id,&row,&col,
2076 &det,&ns,&crt,&slt,&dep,&ch) ;
2078 if(ret!=10) continue ;
2082 LOG(ERR,
"expect det %d, not %d",dd,det) ;
2087 LOG(ERR,
"bad ns %d",ns) ;
2092 LOG(ERR,
"bad dep %d",dep) ;
2097 LOG(ERR,
"bad ch %d",ch) ;
2101 int sec = det_map[det][ns][dep].sector ;
2102 int rdo = det_map[det][ns][dep].rdo ;
2104 rdo_map[sec-1][rdo-1].ch[ch].id = id ;
2105 rdo_map[sec-1][rdo-1].ch[ch].row = row ;
2106 rdo_map[sec-1][rdo-1].ch[ch].col = col ;
2107 rdo_map[sec-1][rdo-1].crt = crt ;
2108 rdo_map[sec-1][rdo-1].slt = slt ;
2119 int fcs_data_c::load_sc_map(
const char *fname)
2124 if(rdo_map_loaded==0) {
2125 LOG(ERR,
"rdo_map not loaded!") ;
2129 for(
int s=0;s<10;s++) {
2130 for(
int r=0;r<8;r++) {
2131 for(
int c=0;c<32;c++) {
2132 rdo_map[s][r].ch[c].sc_sipm = 0xFF ;
2135 for(u_int dd=0;dd<3;dd++) {
2140 fn =
"/RTS/conf/fcs/fcs_ecal_sc_map.csv" ;
2143 fn =
"/RTS/conf/fcs/fcs_hcal_sc_map.csv" ;
2146 fn =
"/RTS/conf/fcs/fcs_pres_sc_map.csv" ;
2149 fn =
"/RTS/conf/fcs/fcs_main_sc_map.csv" ;
2155 LOG(INFO,
"load_sc_map: constructing FPRE map") ;
2157 for(
int s=0;s<10;s++) {
2158 for(
int r=0;r<8;r++) {
2159 if(rdo_map[s][r].det != 2) continue ;
2161 int dep = rdo_map[s][r].dep ;
2163 for(
int c=0;c<32;c++) {
2164 rdo_map[s][r].ch[c].sc_sipm = 0 ;
2165 rdo_map[s][r].ch[c].sc_dep = dep ;
2166 rdo_map[s][r].ch[c].sc_add = 0 ;
2167 rdo_map[s][r].ch[c].sc_bra = 0 ;
2170 int ns = rdo_map[s][r].ns ;
2176 rdo_map[s][r].ch[16].sc_sipm= 0xFF ;
2179 rdo_map[s][r].ch[0].sc_sipm = 0xFF ;
2183 for(
int c=0;c<16;c++) {
2184 rdo_map[s][r].ch[c].sc_sipm = 0xFF ;
2188 for(
int c=16;c<32;c++) {
2189 rdo_map[s][r].ch[c].sc_sipm = 0xFF ;
2201 FILE *f = fopen(fn,
"r") ;
2203 if(f) LOG(INFO,
"load_sc_map: opened %s",fn) ;
2205 LOG(ERR,
"load_sc_map: %s [%s]",fn,strerror(errno)) ;
2212 u_int det, ns, dep ;
2213 u_int bra, add, sipm ;
2216 if(fgets(buff,
sizeof(buff),f)==0) continue ;
2218 if(buff[0]==
'#') continue ;
2219 if(buff[0]==
'\n') continue ;
2220 if(buff[0]==0) continue ;
2222 int ret = sscanf(buff,
"%d %d %d %d %d %d %d %d %d %d",
2223 &adet,&
id,&row,&col,
2224 &det,&ns,&dep,&bra,&add,&sipm) ;
2226 if(ret!=10) continue ;
2230 LOG(ERR,
"expect det %d, not %d",dd,det) ;
2235 LOG(ERR,
"bad ns %d",ns) ;
2240 LOG(ERR,
"bad dep %d",dep) ;
2245 LOG(ERR,
"bad bra %d",bra) ;
2251 for(
int s=0;s<10;s++) {
2252 for(
int r=0;r<8;r++) {
2253 if(rdo_map[s][r].det != dd) continue ;
2254 if(rdo_map[s][r].ns != ns) continue ;
2256 for(
int c=0;c<32;c++) {
2257 if(rdo_map[s][r].ch[c].
id !=
id) continue ;
2259 rdo_map[s][r].ch[c].sc_dep = dep ;
2260 rdo_map[s][r].ch[c].sc_bra = bra ;
2261 rdo_map[s][r].ch[c].sc_add = add ;
2273 rdo_map[s][r].ch[c].sc_sipm = sipm ;
2283 LOG(ERR,
"DET %d: can't find id %d",dd,
id) ;
2294 int fcs_data_c::load_rdo_map(
const char *fname)
2300 rdo_map_loaded = 0 ;
2301 memset(rdo_map,0,
sizeof(rdo_map)) ;
2302 memset(det_map,0,
sizeof(det_map)) ;
2307 for(
int s=0;s<12;s++) {
2308 for(
int r=0;r<8;r++) {
2309 rdo_map[s][r].det = 3 ;
2310 rdo_map[s][r].ns = 0 ;
2311 rdo_map[s][r].dep = 10 ;
2315 for(
int r=0;r<8;r++) {
2318 rdo_map[0][r].crate = 0 ;
2319 rdo_map[0][r].slot = r ;
2321 rdo_map[1][r].crate = 0 ;
2322 rdo_map[1][r].slot = r+8 ;
2325 rdo_map[2][r].crate = 0 ;
2326 rdo_map[2][r].slot = r+16 ;
2329 rdo_map[2][r].crate = 1 ;
2330 rdo_map[2][r].slot = r+16-4 ;
2335 rdo_map[3][r].crate = 1 ;
2336 rdo_map[3][r].slot = r ;
2338 rdo_map[4][r].crate = 1 ;
2339 rdo_map[4][r].slot = r+8 ;
2346 rdo_map[10][r].crate = 2 ;
2347 rdo_map[10][r].slot = r ;
2353 rdo_map[10][r].crate = 2 ;
2354 rdo_map[10][r].slot = r - 4 ;
2357 rdo_map[5][r].crate = 4 ;
2358 rdo_map[5][r].slot = r ;
2360 rdo_map[6][r].crate = 4 ;
2361 rdo_map[6][r].slot = r+8 ;
2364 rdo_map[7][r].crate = 4 ;
2365 rdo_map[7][r].slot = r+16 ;
2368 rdo_map[7][r].crate = 3 ;
2369 rdo_map[7][r].slot = r+16-4 ;
2374 rdo_map[8][r].crate = 3 ;
2375 rdo_map[8][r].slot = r ;
2377 rdo_map[9][r].crate = 3 ;
2378 rdo_map[9][r].slot = r+8 ;
2384 rdo_map[5-1][2-1] = rdo_map[7-1][8-1] ;
2385 rdo_map[10-1][2-1] = rdo_map[10-1][8-1] ;
2389 fname =
"/RTS/conf/fcs/fcs_daq_map.txt" ;
2392 FILE *f = fopen(fname,
"r") ;
2394 LOG(ERR,
"Can't open map file %s [%s]",fname,strerror(errno)) ;
2398 LOG(INFO,
"load_rdo_map: opened %s",fname) ;
2405 unsigned long long mask ;
2409 if(fgets(buff,
sizeof(buff),f)==0) continue ;
2411 if(buff[0]==
'#') continue ;
2412 if(buff[0]==
'\n') continue ;
2413 if(buff[0]==0) continue ;
2415 int ret = sscanf(buff,
"%d %d %d %d %d 0x%llX",&s,&r,&d,&n,&b,&mask) ;
2417 if(ret != 6) continue ;
2425 if((s<0) || (r<0)) {
2427 LOG(ERR,
"Mapping S%d:%d --> %d,%d,%d, mask 0x%llX",s+1,r+1,d,n,b,mask) ;
2432 rdo_map[s][r].det = d ;
2433 rdo_map[s][r].ns = n ;
2434 rdo_map[s][r].dep = b ;
2435 rdo_map[s][r].ch_mask = mask ;
2437 det_map[d][n][b].sector = s+1 ;
2438 det_map[d][n][b].rdo = r+1 ;
2441 rdo_map_loaded = 1 ;
2448 u_short fcs_data_c::set_rdo(
int rdo1)
2452 return set_board_id() ;
2456 u_short fcs_data_c::set_board_id()
2458 int sec = sector - 1 ;
2462 if((sec<0)||(r<0)) {
2463 LOG(ERR,
"bad %d %d",sec,r) ;
2466 int det = rdo_map[sec][r].det ;
2467 int ns = rdo_map[sec][r].ns ;
2468 int dep = rdo_map[sec][r].dep ;
2471 board_id = (sec<<11)|(r<<8)|(det<<6)|(ns<<5)|dep ;
2478 int fcs_data_c::load_bad_ch(
const char *fname,
int sector)
2480 memset(fcs_bad_ch,0,
sizeof(fcs_bad_ch)) ;
2481 memset(fcs_bad_ch_all,0,
sizeof(fcs_bad_ch_all)) ;
2484 fname =
"/RTS/conf/fcs/bad_channels.txt" ;
2487 FILE *f = fopen(fname,
"r") ;
2489 LOG(ERR,
"fcs_load_bad_ch: %s [%s]",fname,strerror(errno)) ;
2493 LOG(INFO,
"fcs_load_bad_ch: %s",fname) ;
2500 if(fgets(buff,
sizeof(buff),f)==0) continue ;
2504 if(buff[0]==
'#') continue ;
2505 if(buff[0]==
'\n') continue ;
2507 int s,r,det,ns,dep,ch,flag ;
2510 int ret = sscanf(buff,
"%d %d %d %d %d %d %f %f 0x%X",&s,&r,&det,&ns,&dep,&ch,&ped,&rms,&flag) ;
2514 if(ret != 9) continue ;
2516 if(sector==0) LOG(WARN,
"Bad ch: S%02d:%d:%02d",s,r,ch) ;
2518 if((r<1)||(r>8)) continue ;
2519 if((ch<0)||(ch>33)) continue ;
2520 if((s<1)||(s>10)) continue ;
2522 fcs_bad_ch_all[s-1][r-1][ch] = 1 ;
2524 if(s != sector) continue ;
2526 LOG(WARN,
"Bad ch: S%02d:%d:%02d",s,r,ch) ;
2528 fcs_bad_ch[r-1][ch] = 1 ;