12 #include <rtsSystems.h>
14 #include <DAQ_READER/daq_dta.h>
20 fgtPed::fgtPed(
int rts)
25 memset(fgt_stat,0,
sizeof(fgt_stat)) ;
36 memset(ch_status,0,
sizeof(ch_status)) ;
42 for(
int r=0;r<6;r++) {
43 for(
int ar=0;ar<6;ar++) {
44 for(
int ap=0;ap<24;ap++) {
45 for(
int c=0;c<128;c++) {
46 ch_status[r][ar][ap][c] = FGT_CH_STAT_SHOULD ;
55 for(
int ar=0;ar<2;ar++) {
56 u_int ap_mask = 0x00F00F ;
57 for(
int ap=0;ap<24;ap++) {
58 if(ap_mask & (1<<ap)) ;
61 for(
int c=0;c<128;c++) {
62 ch_status[0][ar][ap][c] = FGT_CH_STAT_SHOULD ;
71 for(
int r=0;r<2;r++) {
72 for(
int ar=0;ar<6;ar++) {
74 u_int ap_mask = 0x3FF3FF ;
75 for(
int ap=0;ap<24;ap++) {
76 if(ap_mask & (1<ap)) ;
79 for(
int c=0;c<128;c++) {
80 ch_status[r][ar][ap][c] = FGT_CH_STAT_SHOULD ;
97 static int fgt_bad_heuristics(
int rts_id,
double ped,
double rms)
102 if((rms < 5.0)||(rms>50.0)||(ped>2000.0)||(ped<5.0))
return -1 ;
105 if((rms<=0.0) || (ped<0.0))
return -1 ;
113 int fgtPed::run_stop()
115 for(
int r=0;r<FGT_RDO_COU;r++) {
116 if(rb_mask & (1<<r)) ;
119 int evts = fgt_stat[r].evts ;
121 if(fgt_stat[r].err) {
122 LOG(ERR,
"RDO %d: %d errors in %d events",r+1,fgt_stat[r].err,evts) ;
125 for(
int arm=0;arm<FGT_ARM_COU;arm++) {
126 if(fgt_stat[r].arm_mask & (1<<arm)) ;
129 for(
int apv=0;apv<FGT_APV_COU;apv++) {
130 int errs = fgt_stat[r].err_apv[arm][apv] ;
131 int cou = fgt_stat[r].cou_apv[arm][apv] ;
133 if(errs || (cou && (cou!=evts))) {
134 LOG(ERR,
"RDO %d: ARM %d, APV %2d: %d errors, in %d/%d events",
135 r+1,arm,apv,errs,cou,evts) ;
145 void fgtPed::init(
int active_rbs)
147 memset(fgt_stat,0,
sizeof(fgt_stat)) ;
149 rb_mask = active_rbs ;
154 int fgtPed::do_zs(
char *src,
int in_bytes,
char *dst,
int rdo1)
160 short *d16 = (
short *) dst ;
161 u_int *d32 = (u_int *)dst ;
163 fgt_stat[rdo1-1].evts++ ;
165 daq_dta *dd = fgt_rdr[rdo1-1]->handle_adc(0, rdo1, src) ;
172 memset(&meta_zs,0,
sizeof(meta_zs)) ;
174 int t_xpect = tb_cou_ped ;
180 for(
int arm=0;arm<FGT_ARM_COU;arm++) {
181 for(
int apv=0;apv<FGT_APV_COU;apv++) {
183 printf(
"ARC %d, ARM %d, APV %d: %d %d %d %d\n",rdo1,arm,apv,
184 meta->arc[rdo1].arm[arm].apv[apv].present,
185 meta->arc[rdo1].arm[arm].apv[apv].ntim,
186 meta->arc[rdo1].arm[arm].apv[apv].error,
187 meta->arc[rdo1].arm[arm].error) ;
189 if(meta->arc[rdo1].arm[arm].apv[apv].present) {
192 meta_zs.status[arm][apv] |= 1 ;
194 int t_cou = meta->arc[rdo1].arm[arm].apv[apv].ntim ;
196 if(t_cou != t_xpect) {
197 if(fgt_stat[rdo1-1].err < 12) {
198 LOG(WARN,
"wrong tb_cou %d(expect %d): ARC %d, ARM %d, APV %d",t_cou,t_xpect,rdo1,arm,apv) ;
200 fgt_stat[rdo1-1].err++ ;
204 if(t_cou > max_tb) max_tb = t_cou ;
208 meta_zs.status[arm][apv] |= err ;
212 if(meta->arc[rdo1].arm[arm].apv[apv].error || meta->arc[rdo1].arm[arm].error) {
213 meta_zs.status[arm][apv] |= 4 ;
216 if(meta_zs.status[arm][apv] & 1) {
217 fgt_stat[rdo1-1].cou_apv[arm][apv]++ ;
220 if(meta_zs.status[arm][apv] & 0xFE) {
221 fgt_stat[rdo1-1].err_apv[arm][apv]++ ;
224 switch(meta_zs.status[arm][apv]) {
230 if(fgt_stat[rdo1-1].err < 100) {
231 LOG(ERR,
"ARC %d, ARM %d, APV %d: meta_zs 0x%X",rdo1,arm,apv,meta_zs.status[arm][apv]) ;
233 fgt_stat[rdo1-1].err++ ;
240 if(max_tb > tb_cou_ped) max_tb = tb_cou_ped ;
242 meta_zs.tb_cou = max_tb ;
245 int meta_bytes =
sizeof(meta_zs) ;
246 while(meta_bytes % 2) meta_bytes++ ;
248 *d32++ = 0xFEEDBEEF ;
254 *d32++ = META_ZS_VERSION ;
259 *d32++ = META_PED_ZS_VERSION ;
263 *d32++ = META_PED_ZS_VERSION ;
267 u_int *dta_bytes = d32++ ;
268 *d32++ = meta_bytes ;
270 memcpy(d32,&meta_zs,meta_bytes) ;
271 d16 = (
short *)d32 + meta_bytes/2 ;
273 while(dd && dd->iterate()) {
285 struct peds_t *p_thr = &(peds[arc-1]) ;
294 *d16++ = 0xAB00 | arc ;
295 *d16++ = (arm << 8) | apv ;
298 for(u_int i=0;i<dd->ncontent;i++) {
305 if(tb >= max_tb) continue ;
311 *d16++ = (cou_tb << 8) | ch ;
314 for(
int i=0;i<cou_tb;i++) {
317 short adc = (short) ((
float)f[i_save+i].adc - p_thr->ped[arm][apv][ch][i] + 0.5);
320 if(adc > 0) total_charge++ ;
324 *d16++ = f[i_save+i].adc ;
337 thr = p_thr->thr[arm][apv][ch] ;
365 *d16++ = (cou_tb <<8) | ch ;
368 for(
int i=0;i<cou_tb;i++) {
372 short adc = (short) ((
float)f[i_save+i].adc - p_thr->ped[arm][apv][ch][i] + 0.5);
376 *d16++ = f[i_save+i].adc ;
384 *d16++ = dumped_cou ;
387 int out_bytes = (
char *)d16 - (
char *)dst ;
389 *dta_bytes = out_bytes ;
392 while(out_bytes % 4) {
407 void fgtPed::accum(
char *evbuff,
int bytes,
int rdo1)
414 fgt_stat[rdo].evts++ ;
421 if(fgt_stat[rdo].evts > 0xFF00) return ;
423 struct peds_t *p = peds + rdo ;
426 dd = fgt_rdr[rdo1-1]->handle_adc(0,rdo1, evbuff) ;
429 int t_xpect = tb_cou_xpect ;
431 char need[FGT_ARM_COU][FGT_APV_COU] ;
432 memset(need,0,
sizeof(need)) ;
437 for(
int arm=0;arm<FGT_ARM_COU;arm++) {
438 for(
int apv=0;apv<FGT_APV_COU;apv++) {
439 if(meta->arc[rdo1].arm[arm].apv[apv].present == 0) continue ;
442 if(meta->arc[rdo1].arm[arm].apv[apv].ntim != t_xpect) {
443 if(fgt_stat[rdo1-1].err < 10) {
444 LOG(WARN,
"evt %d: RDO %d, ARM %d, APV %d: ntim %d, expect %d??",fgt_stat[rdo].evts,rdo1,arm,apv,meta->arc[rdo1].arm[arm].apv[apv].ntim,t_xpect) ;
446 fgt_stat[rdo1-1].err++ ;
449 need[arm][apv] |= 1 ;
451 if(meta->arc[rdo1].arm[arm].apv[apv].apv_id != apv) {
452 LOG(ERR,
"RDO %d, ARM %d, APV %d: %d",rdo1,arm,apv,meta->arc[rdo1].arm[arm].apv[apv].apv_id) ;
453 need[arm][apv] |= 2 ;
461 while(dd && dd->iterate()) {
462 if(dd->rdo != rdo1) continue ;
469 need[arm][apv] |= 4 ;
471 for(u_int i=0;i<dd->ncontent;i++) {
480 p->ped[arm][apv][ch][tb] += (float) adc ;
481 p->rms[arm][apv][ch][tb] += (float) (adc * adc) ;
482 if(tb==0) p->cou[arm][apv][ch]++ ;
488 for(
int arm=0;arm<FGT_ARM_COU;arm++) {
489 for(
int apv=0;apv<FGT_APV_COU;apv++) {
490 if(need[arm][apv] == 0) continue ;
492 switch(need[arm][apv]) {
497 LOG(ERR,
"ARC %d, ARM %d, APV %d: need is 0x%X",rdo1,arm,apv,need[arm][apv]) ;
510 double fgtPed::do_thresh(
double ns,
int k,
int do_log)
521 LOG(INFO,
"do_thresh: n-sigma %f, k-seq %d",n_sigma, k_seq) ;
524 for(
int r=0;r<FGT_RDO_COU;r++) {
525 struct peds_t *p = peds + r ;
527 for(
int arm=0;arm<FGT_ARM_COU;arm++) {
528 for(
int apv=0;apv<FGT_APV_COU;apv++) {
529 for(
int c=0;c<FGT_CH_COU;c++) {
531 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_SHOULD) ;
539 for(
int t=0;t<tb_cou_ped;t++) {
540 double pp = p->ped[arm][apv][c][t] ;
541 double rm = p->rms[arm][apv][c][t] ;
543 if(rm < 0.0) continue ;
551 p->thr[arm][apv][c] = 0xFFFD ;
558 p->thr[arm][apv][c] = (u_short) (ped + rms * n_sigma + 0.5) ;
569 for(
int r=0;r<FGT_RDO_COU;r++) {
570 struct peds_t *p = peds + r ;
575 int b_misconfigured = 0 ;
579 for(
int arm=0;arm<FGT_ARM_COU;arm++) {
580 for(
int apv=0;apv<FGT_APV_COU;apv++) {
581 for(
int c=0;c<FGT_CH_COU;c++) {
582 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_SHOULD) ;
584 p->thr[arm][apv][c] = 0xFFFE ;
591 if(ch_status[r][arm][apv][c] & 0xFE) {
592 p->thr[arm][apv][c] = 0xFFFF ;
595 else if(p->thr[arm][apv][c] >= 0xFFFD) {
601 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_NO_CONFIG) {
604 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_NO_RESPONSE) {
607 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_BAD) {
610 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_PED_UNKNOWN) {
620 LOG(WARN,
"ARC %d: masked %d, misconfigd %d, bad %d, bad ped %d, unknown %d of %d all",
621 r+1,b_masked,b_misconfigured,b_bad,b_ped_0,b_unknown,b_all) ;
625 double perc = (double)b_bad_all/(
double)b_all_all ;
635 const int MIN_EVENTS = 1000 ;
638 LOG(NOTE,
"Calculating pedestals") ;
646 for(
int r=0;r<FGT_RDO_COU;r++) {
647 if(rb_mask & (1<<r)) ;
652 struct peds_t *ped = peds + r ;
654 for(
int arm=0;arm<FGT_ARM_COU;arm++) {
655 for(
int apv=0;apv<FGT_APV_COU;apv++) {
656 for(
int ch=0;ch<FGT_CH_COU;ch++) {
658 int cou = ped->cou[arm][apv][ch] ;
660 for(
int t=0;t<tb_cou_xpect;t++) {
663 ped->ped[arm][apv][ch][t] = 0 ;
664 ped->rms[arm][apv][ch][t] = -1.0 ;
670 pp = ped->ped[arm][apv][ch][t] / (double) cou ;
671 rr = ped->rms[arm][apv][ch][t] / (double) cou ;
674 if(rr < (pp*pp)) rr = 0.0 ;
675 else rr = sqrt(rr - pp*pp) ;
677 ped->ped[arm][apv][ch][t] = pp ;
678 ped->rms[arm][apv][ch][t] = rr ;
680 if(t > tb_cou_ped) tb_cou_ped = t ;
696 for(
int r=0;r<FGT_RDO_COU;r++) {
697 if(rb_mask & (1<<r)) ;
700 if(fgt_stat[r].evts < MIN_EVENTS) {
707 LOG(TERR,
"Pedestals calculated. tb_count %d, RDO counts: %u",tb_cou_ped,fgt_stat[0].evts) ;
710 if(not_enough) valid = 0 ;
714 LOG(ERR,
"FGT pedestals not good: APVs bad %d, events %d",bad,fgt_stat[0].evts) ;
721 int fgtPed::to_evb(
char *buff)
723 int r, arm, apv, c, t ;
724 u_short *dta = (u_short *) buff ;
729 LOG(ERR,
"ped::to_evb peds are bad: valid %d",valid) ;
732 LOG(NOTE,
"Preparing pedestals for later EVB...") ;
736 *dta++ = FGT_ARM_COU ;
737 *dta++ = FGT_APV_COU ;
738 *dta++ = FGT_CH_COU ;
739 *dta++ = tb_cou_ped ;
742 for(r=0;r<FGT_RDO_COU;r++) {
743 if(rb_mask && (1<<r)) ;
746 struct peds_t *ped = peds + r ;
749 u_short *apv_cou = dta++ ;
754 for(arm=0;arm<FGT_ARM_COU;arm++) {
755 for(apv=0;apv<FGT_APV_COU;apv++) {
763 for(c=0;c<FGT_CH_COU;c++) {
764 for(t=0;t<tb_cou_ped;t++) {
768 pp = (u_short)(ped->ped[arm][apv][c][t]*16.0 + 0.5) ;
771 pp = (u_short)(ped->rms[arm][apv][c][t]*16.0 + 0.5) ;
781 LOG(TERR,
"Pedestals prepared for later EVB, %d bytes",(
char *)dta-buff) ;
783 return ((
char *)dta-buff) ;
788 memset(peds,0,
sizeof(peds)) ;
791 void fgtPed::clear_from_cache()
793 for(
int r=0;r<FGT_RDO_COU;r++) {
794 struct peds_t *p = peds_from_cache + r ;
796 for(
int arm=0;arm<FGT_ARM_COU;arm++) {
797 for(
int apv=0;apv<FGT_APV_COU;apv++) {
798 for(
int c=0;c<FGT_CH_COU;c++) {
799 for(
int t=0;t<FGT_TB_COU;t++) {
800 p->ped[arm][apv][c][t] = 0.0 ;
801 p->rms[arm][apv][c][t] = -2.0 ;
803 p->cou[arm][apv][c] = 0 ;
804 p->thr[arm][apv][c] = 0xFFFF ;
813 int fgtPed::from_cache(
char *fname)
828 f = fopen(fname,
"r") ;
831 fn =
"/RTScache/pedestals.txt" ;
836 LOG(U_TONKO,
"ped::from_cache can't open input file \"%s\" [%s]",fn,strerror(errno)) ;
841 LOG(NOTE,
"Loading pedestals from cache \"%s\"...",fn) ;
846 int r, arm, apv, ch, tb ;
850 if(fgets(buff,
sizeof(buff),f) == 0) continue ;
862 int ret = sscanf(buff,
"%d %d %d %d %d %f %f",&r,&arm,&apv,&ch,&tb,&pp,&rr) ;
863 if(ret != 7) continue ;
866 struct peds_t *ped = peds + (r-1) ;
868 ped->ped[arm][apv][ch][tb] = pp ;
869 ped->rms[arm][apv][ch][tb] = rr ;
871 struct peds_t *ped_fc = peds_from_cache + (r-1) ;
873 ped_fc->ped[arm][apv][ch][tb] = pp ;
874 ped_fc->rms[arm][apv][ch][tb] = rr ;
876 if(tb > tb_cou_ped) {
882 if(pp<=0.0 || rr<=0.0) {
883 ch_status[r-1][arm][apv][ch] |= FGT_CH_STAT_PED_UNKNOWN ;
892 if(tb_cou_ped != tb_cou_xpect) {
893 LOG(ERR,
"Pedestals loaded from cache \"%s\" but have %d timebins != expect %d!",fn,
894 tb_cou_ped,tb_cou_xpect) ;
897 LOG(INFO,
"Pedestals loaded from cache \"%s\", %d timebins OK",fn,tb_cou_ped) ;
905 int fgtPed::to_cache(
char *fname, u_int run,
int dont_cache)
912 LOG(ERR,
"ped::to_cache peds are bad: valid %d -- caching anyway...",valid) ;
918 strcpy(f_fname,fname) ;
922 sprintf(f_fname,
"/RTScache/%s_pedestals_%08u.txt",rts2name(rts_id),run) ;
926 f = fopen(f_fname,
"w") ;
928 LOG(ERR,
"ped::to_cache can't open output file \"%s\" [%s]",f_fname,strerror(errno)) ;
933 if(tb_cou_ped != tb_cou_xpect) {
934 LOG(CAUTION,
"Writing pedestals to cache \"%s\" [valid %d] but data has %d timebins != expect %d!",f_fname,valid,
935 tb_cou_ped,tb_cou_xpect) ;
938 LOG(TERR,
"Writing pedestals to cache \"%s\" [valid %d], ntimebins %d",f_fname,valid,tb_cou_ped) ;
941 time_t tim = time(0) ;
942 fprintf(f,
"# Detector %s\n",rts2name(rts_id)) ;
943 fprintf(f,
"# Run %08u\n",run) ;
944 fprintf(f,
"# Date %s",ctime(&tim)) ;
945 fprintf(f,
"# Timebins %d\n",tb_cou_ped) ;
948 for(
int r=0;r<FGT_RDO_COU;r++) {
949 if(rb_mask & (1<<r)) ;
953 struct peds_t *ped = peds + r ;
955 for(
int arm=0;arm<FGT_ARM_COU;arm++) {
956 for(
int apv=0;apv<FGT_APV_COU;apv++) {
959 for(
int c=0;c<FGT_CH_COU;c++) {
962 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_SHOULD) ;
965 for(
int t=0;t<tb_cou_ped;t++) {
967 fprintf(f,
"%d %d %2d %3d %2d %7.3f %.3f\n",r+1,arm,apv,c,t,
968 ped->ped[arm][apv][c][t],
969 ped->rms[arm][apv][c][t]) ;
980 LOG(CAUTION,
"Pedestals NOT cached for run %08u: bad run conditions 0x%X!",run,dont_cache) ;
985 f = fopen(
"/RTScache/pedestals.txt",
"w") ;
987 LOG(ERR,
"ped::to_cache can't open output file \"%s\" [%s]",
"/RTScache/pedestals.txt",strerror(errno)) ;
991 fprintf(f,
"# Detector %s\n",rts2name(rts_id)) ;
992 fprintf(f,
"# Run %08u\n",run) ;
993 fprintf(f,
"# Date %s",ctime(&tim)) ;
994 fprintf(f,
"# Timebins %d\n",tb_cou_ped) ;
998 int non_cached_cou = 0 ;
1000 for(
int r=0;r<FGT_RDO_COU;r++) {
1001 if(rb_mask & (1<<r)) ;
1005 struct peds_t *ped = peds + r ;
1007 for(
int arm=0;arm<FGT_ARM_COU;arm++) {
1008 for(
int apv=0;apv<FGT_APV_COU;apv++) {
1011 for(
int c=0;c<FGT_CH_COU;c++) {
1014 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_SHOULD) ;
1017 struct peds_t *use_ped ;
1021 if((ch_status[r][arm][apv][c] & 0x0E) || (ped->cou[arm][apv][c]==0)) {
1024 use_ped = peds_from_cache + r ;
1031 for(
int t=0;t<tb_cou_ped;t++) {
1033 fprintf(f,
"%d %d %2d %3d %2d %7.3f %.3f\n",r+1,arm,apv,c,t,
1034 use_ped->ped[arm][apv][c][t],
1035 use_ped->rms[arm][apv][c][t]) ;
1047 sprintf(cmd,
"/bin/cp /RTScache/pedestals.txt /RTScache/%s_pedestals_%08u_loaded.txt",rts2name(rts_id),run) ;
1059 double perc = 100.0 * (double)non_cached_cou / (
double)all_cou ;
1061 LOG(U_TONKO,
"Pedestals cached for run %08u: skipped %d/%d (%d%%) channels.",run,non_cached_cou,all_cou,(
int)perc) ;
1064 LOG(TERR,
"Pedestals cached for run %08u: skipped %d/%d channels.",run,non_cached_cou,all_cou) ;
1072 int fgtPed::bad_from_cache(
char *fname)
1083 f = fopen(fname,
"r") ;
1088 fn =
"/RTS/conf/gmt/gmt_bad_channels.txt" ;
1091 fn =
"/RTS/conf/ist/ist_bad_channels.txt" ;
1094 fn =
"/RTS/conf/fst/fst_bad_channels.txt" ;
1098 fn =
"/RTS/conf/fgt/fgt_bad_channels.txt" ;
1106 LOG(ERR,
"ped::bad_from_cache can't open input file \"%s\" [%s]",fn,strerror(errno)) ;
1111 LOG(NOTE,
"Loading bad from cache \"%s\"...",fn) ;
1116 int r, arm, apv, ch ;
1119 if(fgets(buff,
sizeof(buff),f) == 0) continue ;
1131 int ret = sscanf(buff,
"%d %d %d %d",&r,&arm,&apv,&ch) ;
1132 if(ret != 4) continue ;
1137 memset(n,0,
sizeof(n)) ;
1138 sscanf(buff,
"%s %s %s %s",ca[0],ca[1],ca[2],ca[3]) ;
1139 for(
int i=0;i<4;i++) {
1141 if(sscanf(ca[i],
"%d",&dummy)!=1) continue ;
1143 if(index(ca[i],
'-')) n[i] =
'-' ;
1147 if(dummy<0) n[i] =
'-' ;
1153 if(arm < 0) arm *= -1 ;
1154 if(apv < 0) apv *= -1 ;
1155 if(ch < 0) ch *= -1 ;
1160 for(
int a=0;a<FGT_APV_COU;a++) {
1161 for(
int c=0;c<FGT_CH_COU;c++) {
1162 ch_status[r-1][arm][a][c] |= FGT_CH_STAT_BAD ;
1166 else if(n[2]==
'-') {
1167 for(
int c=0;c<FGT_CH_COU;c++) {
1168 ch_status[r-1][arm][apv][c] |= FGT_CH_STAT_BAD ;
1172 ch_status[r-1][arm][apv][ch] |= FGT_CH_STAT_BAD ;
1180 for(
int r=0;r<FGT_RDO_COU;r++) {
1181 for(
int m=0;m<FGT_ARM_COU;m++) {
1182 for(
int a=0;a<FGT_APV_COU;a++) {
1183 for(
int c=0;c<FGT_CH_COU;c++) {
1184 if(ch_status[r][m][a][c] & FGT_CH_STAT_BAD) b_cou++ ;
1190 LOG(INFO,
"Loaded %d bad channels from \"%s\"",b_cou,fn) ;