12 #include <rtsSystems.h>
14 #include <DAQ_READER/daq_dta.h>
25 clock_gettime(CLOCK_MONOTONIC,&ts) ;
27 return (u_int)(ts.tv_sec*1000000+ts.tv_nsec/1000) ;
31 static u_int t_delta(u_int mark)
33 return (
unsigned int)((t_mark() - mark)) ;
37 char fstPed::cmnGroup[24][128] ;
42 int fstPed::load_group(
const char *fname)
44 if(fname==0) fname =
"/RTS/conf/fst/cmnGroup.txt" ;
46 memset(cmnGroup,0xFF,
sizeof(cmnGroup)) ;
48 FILE *f = fopen(fname,
"r") ;
50 LOG(ERR,
"Group file %s [%s]",fname,strerror(errno)) ;
58 if(fgets(buff,
sizeof(buff),f)==0) continue ;
60 int ret = sscanf(buff,
"%d %d %d",&apv,&ch,&grp) ;
61 if(ret != 3) continue ;
63 cmnGroup[apv][ch] = grp ;
71 LOG(INFO,
"cmnGroup %s loaded.",fname) ;
78 fstPed::fstPed(
int rts)
83 memset(fgt_stat,0,
sizeof(fgt_stat)) ;
93 memset(ch_status,0,
sizeof(ch_status)) ;
98 for(
int r=0;r<6;r++) {
99 for(
int ar=0;ar<6;ar++) {
100 for(
int ap=0;ap<24;ap++) {
101 for(
int c=0;c<128;c++) {
102 ch_status[r][ar][ap][c] = FGT_CH_STAT_SHOULD ;
108 for(
int t=0;t<8;t++) {
129 int fstPed::run_stop()
131 for(
int r=0;r<FGT_RDO_COU;r++) {
132 if(rb_mask & (1<<r)) ;
135 int evts = fgt_stat[r].evts ;
137 if(fgt_stat[r].err) {
138 LOG(WARN,
"RDO %d: %d errors in %d events [but this is questionable]",r+1,fgt_stat[r].err,evts) ;
141 for(
int arm=0;arm<FGT_ARM_COU;arm++) {
142 if(fgt_stat[r].arm_mask & (1<<arm)) ;
145 for(
int apv=0;apv<FGT_APV_COU;apv++) {
146 int errs = fgt_stat[r].err_apv[arm][apv] ;
147 int cou = fgt_stat[r].cou_apv[arm][apv] ;
149 if(errs || (cou && (cou!=evts))) {
150 LOG(ERR,
"RDO %d: ARM %d, APV %2d: %d errors, in %d/%d events",
151 r+1,arm,apv,errs,cou,evts) ;
161 void fstPed::init(
int active_rbs)
163 memset(fgt_stat,0,
sizeof(fgt_stat)) ;
165 rb_mask = active_rbs ;
170 int fstPed::do_zs(
char *src,
int in_bytes,
char *dst,
int rdo1,
int id)
176 u_int mark = t_mark() ;
178 short *d16 = (
short *) dst ;
179 u_int *d32 = (u_int *)dst ;
181 fgt_stat[rdo1-1].evts++ ;
184 daq_dta *dd = fgt_rdr[id]->handle_adc(sector, rdo1, src) ;
191 memset(&meta_zs,0,
sizeof(meta_zs)) ;
193 int t_xpect = tb_cou_ped ;
199 for(
int arm=0;arm<FST_ARM_COU;arm++) {
200 for(
int apv=0;apv<FGT_APV_COU;apv++) {
201 if(meta->arc[rdo1].arm[arm].apv[apv].present) {
204 meta_zs.status[arm][apv] |= 1 ;
206 int t_cou = meta->arc[rdo1].arm[arm].apv[apv].ntim ;
208 if(t_cou != t_xpect) {
209 if(fgt_stat[rdo1-1].err < 12) {
210 LOG(WARN,
"wrong tb_cou %d(expect %d): ARC %d, ARM %d, APV %d",t_cou,t_xpect,rdo1,arm,apv) ;
212 fgt_stat[rdo1-1].err++ ;
216 if(t_cou > max_tb) max_tb = t_cou ;
220 meta_zs.status[arm][apv] |= err ;
224 if(meta->arc[rdo1].arm[arm].apv[apv].error || meta->arc[rdo1].arm[arm].error) {
225 meta_zs.status[arm][apv] |= 4 ;
228 if(meta_zs.status[arm][apv] & 1) {
229 fgt_stat[rdo1-1].cou_apv[arm][apv]++ ;
232 if(meta_zs.status[arm][apv] & 0xFE) {
233 fgt_stat[rdo1-1].err_apv[arm][apv]++ ;
236 switch(meta_zs.status[arm][apv]) {
242 if(fgt_stat[rdo1-1].err < 100) {
243 LOG(ERR,
"ARC %d, ARM %d, APV %d: meta_zs 0x%X",rdo1,arm,apv,meta_zs.status[arm][apv]) ;
245 fgt_stat[rdo1-1].err++ ;
252 if(max_tb > tb_cou_ped) max_tb = tb_cou_ped ;
254 meta_zs.tb_cou = max_tb ;
257 int meta_bytes =
sizeof(meta_zs) ;
258 while(meta_bytes % 2) meta_bytes++ ;
260 *d32++ = 0xFEEDBEEF ;
265 *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 = &(peds[arc-1]) ;
294 *d16++ = 0xAB00 | arc ;
295 *d16++ = (arm << 8) | apv ;
303 memset(grp,0,
sizeof(grp)) ;
306 u_short dta[128][9] ;
312 for(u_int i=0;i<dd->ncontent;i++) {
315 int g = cmnGroup[apv][ch] ;
324 if(tb >= max_tb) continue ;
330 double mean = p->ped[arm][apv][ch][tb] ;
331 double rms = p->rms[arm][apv][ch][tb] ;
338 if(adc < (mean+nSigmaCut*rms)) {
339 grp[g][tb].mean += adc - mean ;
350 for(
int g=0;g<4;g++) {
351 for(
int tb=0;tb<9;tb++) {
352 if(grp[g][tb].cou) grp[g][tb].mean /= grp[g][tb].cou ;
355 grp[g][tb].mean = 10000000.0 ;
364 for(
int ch=0;ch<128;ch++) {
365 int g = cmnGroup[apv][ch] ;
371 tb_cou[0] = tb_cou[1] = tb_cou[2] = 0 ;
376 for(
int tb=0;tb<9;tb++) {
377 if(tb>=max_tb) continue ;
379 if(grp[g][tb].mean > 10000.0) {
385 for(
int tb=0;tb<9;tb++) {
388 if(tb >= max_tb) continue ;
392 double cmn_mean = grp[g][tb].mean ;
393 double mean = p->ped[arm][apv][ch][tb] ;
394 double cmn_rms = p->cmn_rms[arm][apv][ch][tb] ;
397 dd = dta[ch][tb] - mean ;
400 dd = dta[ch][tb] - mean - cmn_mean ;
404 if(dd > nSeedsCut*cmn_rms) tb_cou[0]++ ;
405 if(dd > nPedsCut1*cmn_rms) tb_cou[1]++ ;
406 if(dd > nPedsCut2*cmn_rms) tb_cou[2]++ ;
412 if(tb_cou[0]>=nSeedsTbs) pass |= 1 ;
413 if(tb_cou[1]>=nPedsTbs1) pass |= 2 ;
414 if(tb_cou[2]>=nPedsTbs2) pass |= 4 ;
418 *d16++ = (t_cou<<8)|ch ;
421 for(
int tb=0;tb<t_cou;tb++) {
425 else id = ((int)(d[tb]+0.5))&0xFFF ;
427 *d16++ = (pass<<12)|
id ;
443 *d16++ = dumped_cou ;
446 int out_bytes = (
char *)d16 - (
char *)dst ;
448 *dta_bytes = out_bytes ;
451 while(out_bytes % 4) {
468 void fstPed::accum(
char *evbuff,
int bytes,
int rdo1)
475 fgt_stat[rdo].evts++ ;
477 int evt = fgt_stat[rdo].evts ;
479 if(fgt_stat[rdo].evts < evts_for_rms) do_cmn = 0 ;
480 else if(fgt_stat[rdo].evts==evts_for_rms) do_cmn = 1 ;
488 if(fgt_stat[rdo].evts > 0xFF00) return ;
490 struct peds_t *p = peds + rdo ;
493 dd = fgt_rdr[rdo1-1]->handle_adc(sector,rdo1, evbuff) ;
496 int t_xpect = tb_cou_xpect ;
498 char need[FST_ARM_COU][FGT_APV_COU] ;
499 memset(need,0,
sizeof(need)) ;
504 for(
int arm=0;arm<FST_ARM_COU;arm++) {
505 for(
int apv=0;apv<FGT_APV_COU;apv++) {
506 if(meta->arc[rdo1].arm[arm].apv[apv].present == 0) continue ;
508 if(meta->arc[rdo1].arm[arm].apv[apv].ntim != t_xpect) {
509 if(fgt_stat[rdo1-1].err <= 10) {
510 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) ;
511 if(fgt_stat[rdo1-1].err==10) {
512 LOG(WARN,
"evt %d: RDO %d: NO more warnings",fgt_stat[rdo].evts,rdo1) ;
515 fgt_stat[rdo1-1].err++ ;
518 need[arm][apv] |= 1 ;
520 if(meta->arc[rdo1].arm[arm].apv[apv].apv_id != apv) {
521 LOG(ERR,
"RDO %d, ARM %d, APV %d: %d",rdo1,arm,apv,meta->arc[rdo1].arm[arm].apv[apv].apv_id) ;
522 need[arm][apv] |= 2 ;
531 LOG(INFO,
"%d: calculating pedestals at event %d",rdo1,fgt_stat[rdo].evts) ;
539 u_short grp_dta[128][9] ;
541 memset(fst_grp,0,
sizeof(fst_grp)) ;
545 while(dd && dd->iterate()) {
546 if(dd->rdo != rdo1) continue ;
553 need[arm][apv] |= 4 ;
562 if(do_cmn==2) memset(fst_grp,0,
sizeof(fst_grp)) ;
564 for(u_int i=0;i<dd->ncontent;i++) {
575 p->ped[arm][apv][ch][tb] += adc ;
576 p->rms[arm][apv][ch][tb] += (adc * adc) ;
577 p->cou[arm][apv][ch][tb]++ ;
585 int cou = p->cou[arm][apv][ch][tb] ;
593 mean = p->ped[arm][apv][ch][tb] / cou ;
594 rms = p->rms[arm][apv][ch][tb] / cou ;
596 rms = sqrt(rms-mean*mean) ;
599 p->ped[arm][apv][ch][tb] = mean ;
600 p->rms[arm][apv][ch][tb] = rms ;
609 p->cou[arm][apv][ch][tb] = 0 ;
612 int g = cmnGroup[apv][ch] ;
615 double mean = p->ped[arm][apv][ch][tb] ;
616 double rms = p->rms[arm][apv][ch][tb] ;
618 grp_dta[ch][tb] = adc ;
622 if(mean<0.0) continue ;
624 if(adc < (mean+nSigmaCut*rms)) {
626 fst_grp[g][tb].mean += adc - mean ;
627 fst_grp[g][tb].cou++ ;
636 if(for_cmn < 10) LOG(WARN,
"%d: evt %d: ARM %d, APV %d: for cmn %d",rdo1,evt,arm,apv,for_cmn) ;
640 for(
int g=0;g<4;g++) {
641 for(
int tb=0;tb<9;tb++) {
642 if(fst_grp[g][tb].cou) fst_grp[g][tb].mean /= fst_grp[g][tb].cou ;
645 fst_grp[g][tb].mean = 1000000.0 ;
649 for(
int ch=0;ch<128;ch++) {
650 int g = cmnGroup[apv][ch] ;
654 for(
int tb=0;tb<9;tb++) {
655 int d = grp_dta[ch][tb] ;
657 double g_mean = fst_grp[g][tb].mean ;
659 if(g_mean > 1024) continue ;
661 double mean = p->ped[arm][apv][ch][tb] ;
662 double rms = p->rms[arm][apv][ch][tb] ;
664 if(mean<0.0) continue ;
670 if(d < (mean+nSigmaCut*rms)) {
671 p->cmn_ped[arm][apv][ch][tb] += d-mean-g_mean ;
672 p->cmn_rms[arm][apv][ch][tb] += (d-mean-g_mean)*(d-mean-g_mean) ;
673 p->cou[arm][apv][ch][tb]++ ;
684 if(do_cmn==2 && good_cmn<40000) LOG(WARN,
"%d: evt %d: good_cmn %d",rdo1,evt,good_cmn) ;
687 for(
int arm=0;arm<FST_ARM_COU;arm++) {
688 for(
int apv=0;apv<FGT_APV_COU;apv++) {
689 if(need[arm][apv] == 0) continue ;
691 switch(need[arm][apv]) {
696 LOG(ERR,
"ARC %d, ARM %d, APV %d: need is 0x%X",rdo1,arm,apv,need[arm][apv]) ;
709 double fstPed::do_thresh(
double ns,
int k,
int do_log)
720 LOG(INFO,
"do_thresh: n-sigma %f, k-seq %d",n_sigma, k_seq) ;
723 for(
int r=0;r<FGT_RDO_COU;r++) {
724 struct peds_t *p = peds + r ;
726 for(
int arm=0;arm<FST_ARM_COU;arm++) {
727 for(
int apv=0;apv<FGT_APV_COU;apv++) {
728 for(
int c=0;c<FGT_CH_COU;c++) {
730 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_SHOULD) ;
738 for(
int t=0;t<tb_cou_ped;t++) {
739 double pp = p->ped[arm][apv][c][t] ;
740 double rm = p->rms[arm][apv][c][t] ;
742 if(rm < 0.0) continue ;
768 for(
int r=0;r<FGT_RDO_COU;r++) {
771 if(rb_mask & (1<<r)) ;
777 int b_misconfigured = 0 ;
783 for(
int arm=0;arm<3;arm++) {
784 for(
int apv=0;apv<FGT_APV_COU;apv++) {
785 for(
int c=0;c<FGT_CH_COU;c++) {
786 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_SHOULD) ;
797 if(ch_status[r][arm][apv][c] & 0xFE) {
807 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_NO_CONFIG) {
810 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_NO_RESPONSE) {
814 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_BAD) {
818 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_PED_UNKNOWN) {
830 LOG(WARN,
"ARC %d: masked %d, misconfigd %d, bad %d, bad ped %d, unknown %d of %d all",
831 r+1,b_masked,b_misconfigured,b_bad,b_ped_0,b_unknown,b_all) ;
835 LOG(WARN,
"ARC %d: masked %d, misconfigd %d, bad %d, bad ped %d, unknown %d of %d all",
836 r+1,b_masked,b_misconfigured,b_bad,b_ped_0,b_unknown,b_all) ;
841 double perc = (double)b_bad_all/(
double)b_all_all ;
851 const int MIN_EVENTS = 1000 ;
854 LOG(NOTE,
"Calculating pedestals") ;
862 for(
int r=0;r<FGT_RDO_COU;r++) {
863 if(rb_mask & (1<<r)) ;
868 struct peds_t *ped = peds + r ;
870 for(
int arm=0;arm<FST_ARM_COU;arm++) {
871 for(
int apv=0;apv<FGT_APV_COU;apv++) {
872 for(
int ch=0;ch<FGT_CH_COU;ch++) {
876 for(
int t=0;t<tb_cou_xpect;t++) {
877 int cou = ped->cou[arm][apv][ch][t] ;
880 ped->cmn_ped[arm][apv][ch][t] = -1.0 ;
881 ped->cmn_rms[arm][apv][ch][t] = 0 ;
887 pp = ped->cmn_ped[arm][apv][ch][t] / cou ;
888 rr = ped->cmn_rms[arm][apv][ch][t] / cou ;
892 if(rr < (pp*pp)) rr = 0.0 ;
893 else rr = sqrt(rr - pp*pp) ;
895 ped->cmn_ped[arm][apv][ch][t] = pp ;
896 ped->cmn_rms[arm][apv][ch][t] = rr ;
898 if(t > tb_cou_ped) tb_cou_ped = t ;
914 for(
int r=0;r<FGT_RDO_COU;r++) {
915 if(rb_mask & (1<<r)) ;
918 if(fgt_stat[r].evts < MIN_EVENTS) {
925 LOG(TERR,
"Pedestals calculated. tb_count %d, RDO counts: %u",tb_cou_ped,fgt_stat[0].evts) ;
928 if(not_enough) valid = 0 ;
932 LOG(ERR,
"FGT pedestals not good: APVs bad %d, events %d",bad,fgt_stat[0].evts) ;
939 int fstPed::to_evb(
char *buff)
941 int r, arm, apv, c, t ;
942 u_short *dta = (u_short *) buff ;
947 LOG(ERR,
"ped::to_evb peds are bad: valid %d",valid) ;
950 LOG(NOTE,
"Preparing pedestals for later EVB...") ;
954 *dta++ = FGT_ARM_COU ;
955 *dta++ = FGT_APV_COU ;
956 *dta++ = FGT_CH_COU ;
957 *dta++ = tb_cou_ped ;
960 for(r=0;r<FGT_RDO_COU;r++) {
961 if(rb_mask && (1<<r)) ;
964 struct peds_t *ped = peds + r ;
967 u_short *apv_cou = dta++ ;
972 for(arm=0;arm<FST_ARM_COU;arm++) {
973 for(apv=0;apv<FGT_APV_COU;apv++) {
975 if(cmnGroup[apv][0]<0) continue ;
981 for(c=0;c<FGT_CH_COU;c++) {
982 for(t=0;t<tb_cou_ped;t++) {
986 pp = (u_short)(ped->ped[arm][apv][c][t]*16.0 + 0.5) ;
989 pp = (u_short)(ped->rms[arm][apv][c][t]*16.0 + 0.5) ;
992 pp = (u_short)(ped->cmn_rms[arm][apv][c][t]*16.0 + 0.5) ;
1002 LOG(TERR,
"Pedestals prepared for later EVB, %d bytes",(
char *)dta-buff) ;
1004 return ((
char *)dta-buff) ;
1007 void fstPed::clear()
1009 memset(peds,0,
sizeof(peds)) ;
1012 void fstPed::clear_from_cache()
1016 for(
int r=0;r<FGT_RDO_COU;r++) {
1017 struct peds_t *p = peds_from_cache + r ;
1019 for(
int arm=0;arm<FST_ARM_COU;arm++) {
1020 for(
int apv=0;apv<FGT_APV_COU;apv++) {
1021 for(
int c=0;c<FGT_CH_COU;c++) {
1022 for(
int t=0;t<FST_TB_COU;t++) {
1023 p->ped[arm][apv][c][t] = 0.0 ;
1024 p->rms[arm][apv][c][t] = -2.0 ;
1025 p->cmn_rms[arm][apv][c][t] = -2.0 ;
1026 p->cou[arm][apv][c][t] = 0 ;
1038 int fstPed::from_cache(
char *fname)
1047 clear_from_cache() ;
1054 sprintf(file,
"/RTScache/fst_s%d_pedestals.txt",sector) ;
1060 LOG(U_TONKO,
"ped::from_cache can't open input file \"%s\" [%s]",fn,strerror(errno)) ;
1065 LOG(INFO,
"Loading pedestals from cache \"%s\"...",fn) ;
1070 int r, arm, apv, ch, tb ;
1071 float pp, rr, cmn_rms ;
1074 if(fgets(buff,
sizeof(buff),f) == 0) continue ;
1086 int ret = sscanf(buff,
"%d %d %d %d %d %f %f %f",&r,&arm,&apv,&ch,&tb,&pp,&rr,&cmn_rms) ;
1087 if(ret != 8) continue ;
1090 struct peds_t *ped = peds + (r-1) ;
1092 ped->ped[arm][apv][ch][tb] = pp ;
1093 ped->rms[arm][apv][ch][tb] = rr ;
1094 ped->cmn_rms[arm][apv][ch][tb] = cmn_rms ;
1096 struct peds_t *ped_fc = peds_from_cache + (r-1) ;
1098 ped_fc->ped[arm][apv][ch][tb] = pp ;
1099 ped_fc->rms[arm][apv][ch][tb] = rr ;
1100 ped_fc->cmn_rms[arm][apv][ch][tb] = cmn_rms ;
1102 if(tb > tb_cou_ped) {
1108 if(pp<=0.0 || rr<=0.0) {
1109 ch_status[r-1][arm][apv][ch] |= FGT_CH_STAT_PED_UNKNOWN ;
1118 if(tb_cou_ped != tb_cou_xpect) {
1119 LOG(ERR,
"Pedestals loaded from cache \"%s\" but have %d timebins != expect %d!",fn,
1120 tb_cou_ped,tb_cou_xpect) ;
1123 LOG(INFO,
"Pedestals loaded from cache \"%s\", %d timebins OK",fn,tb_cou_ped) ;
1131 int fstPed::to_cache(
char *fname, u_int run,
int dont_cache)
1137 for(
int r=0;r<FGT_RDO_COU;r++) {
1138 if(rb_mask & (1<<r)) ;
1142 if(fgt_stat[r].evts < 1000) {
1143 LOG(ERR,
"%d: not enough events %d",r+1,fgt_stat[r].evts) ;
1148 if(bad || dont_cache || !valid) bad = 1 ;
1151 LOG(ERR,
"ped::to_cache peds are bad: valid %d %d %d-- caching anyway...",bad,dont_cache,valid) ;
1156 strcpy(f_fname,fname) ;
1159 sprintf(f_fname,
"/RTScache/fst_s%d_pedestals_%08u_%s.txt",sector,run,bad?
"BAD":
"GOOD") ;
1163 f = fopen(f_fname,
"w") ;
1165 LOG(ERR,
"ped::to_cache can't open output file \"%s\" [%s]",f_fname,strerror(errno)) ;
1170 if(tb_cou_ped != tb_cou_xpect) {
1171 LOG(ERR,
"Writing pedestals to cache \"%s\" [valid %d] but data has %d timebins != expect %d!",f_fname,valid,
1172 tb_cou_ped,tb_cou_xpect) ;
1175 LOG(INFO,
"Writing pedestals to cache \"%s\" [valid %d], ntimebins %d",f_fname,valid,tb_cou_ped) ;
1178 time_t tim = time(0) ;
1179 fprintf(f,
"# Detector FST%d\n",sector) ;
1180 fprintf(f,
"# Run %08u\n",run) ;
1181 fprintf(f,
"# Date %s",ctime(&tim)) ;
1182 fprintf(f,
"# Timebins %d\n",tb_cou_ped) ;
1186 for(
int r=0;r<FGT_RDO_COU;r++) {
1187 if(rb_mask & (1<<r)) ;
1191 struct peds_t *ped = peds + r ;
1193 for(
int arm=0;arm<3;arm++) {
1194 for(
int apv=0;apv<FGT_APV_COU;apv++) {
1195 for(
int c=0;c<FGT_CH_COU;c++) {
1198 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_SHOULD) ;
1201 if(cmnGroup[apv][c]<0) continue ;
1203 for(
int t=0;t<tb_cou_ped;t++) {
1205 fprintf(f,
"%d %d %2d %3d %2d %7.3f %.3f %.3f\n",r+1,arm,apv,c,t,
1206 ped->ped[arm][apv][c][t],
1207 ped->rms[arm][apv][c][t],
1208 ped->cmn_rms[arm][apv][c][t]) ;
1218 strcpy(f_fname,fname) ;
1221 sprintf(f_fname,
"/RTScache/fst_s%d_pedestals_cmn_%08u_%s.txt",sector,run,bad?
"BAD":
"GOOD") ;
1225 f = fopen(f_fname,
"w") ;
1227 LOG(ERR,
"ped::to_cache can't open output file \"%s\" [%s]",f_fname,strerror(errno)) ;
1232 if(tb_cou_ped != tb_cou_xpect) {
1233 LOG(ERR,
"Writing pedestals to cache \"%s\" [valid %d] but data has %d timebins != expect %d!",f_fname,valid,
1234 tb_cou_ped,tb_cou_xpect) ;
1237 LOG(INFO,
"Writing pedestals to cache \"%s\" [valid %d], ntimebins %d",f_fname,valid,tb_cou_ped) ;
1241 fprintf(f,
"# Detector FST%d\n",sector) ;
1242 fprintf(f,
"# Run %08u\n",run) ;
1243 fprintf(f,
"# Date %s",ctime(&tim)) ;
1244 fprintf(f,
"# Timebins %d\n",tb_cou_ped) ;
1248 for(
int r=0;r<FGT_RDO_COU;r++) {
1249 if(rb_mask & (1<<r)) ;
1253 struct peds_t *ped = peds + r ;
1255 for(
int arm=0;arm<3;arm++) {
1256 for(
int apv=0;apv<FGT_APV_COU;apv++) {
1257 for(
int c=0;c<FGT_CH_COU;c++) {
1260 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_SHOULD) ;
1263 if(cmnGroup[apv][c]<0) continue ;
1265 for(
int t=0;t<tb_cou_ped;t++) {
1267 fprintf(f,
"%d %d %2d %3d %2d %7.3f %.3f\n",r+1,arm,apv,c,t,
1268 ped->cmn_ped[arm][apv][c][t],
1269 ped->cmn_rms[arm][apv][c][t]) ;
1281 if(bad || dont_cache || !valid) {
1282 LOG(CAUTION,
"Pedestals NOT cached for run %08u",run) ;
1283 LOG(ERR,
"Pedestals NOT cached for run %08u: %d:%d:0x%X!",run,bad,valid,dont_cache) ;
1288 sprintf(f_fname,
"/RTScache/fst_s%d_pedestals.txt",sector) ;
1289 f = fopen(f_fname,
"w") ;
1291 LOG(ERR,
"ped::to_cache can't open output file \"%s\" [%s]",f_fname,strerror(errno)) ;
1295 fprintf(f,
"# Detector FST%d\n",sector) ;
1296 fprintf(f,
"# Run %08u\n",run) ;
1297 fprintf(f,
"# Date %s",ctime(&tim)) ;
1298 fprintf(f,
"# Timebins %d\n",tb_cou_ped) ;
1302 int non_cached_cou = 0 ;
1304 for(
int r=0;r<FGT_RDO_COU;r++) {
1305 if(rb_mask & (1<<r)) ;
1309 struct peds_t *ped = peds + r ;
1311 for(
int arm=0;arm<3;arm++) {
1312 for(
int apv=0;apv<FGT_APV_COU;apv++) {
1315 for(
int c=0;c<FGT_CH_COU;c++) {
1318 if(ch_status[r][arm][apv][c] & FGT_CH_STAT_SHOULD) ;
1321 if(cmnGroup[apv][c]<0) continue ;
1323 struct peds_t *use_ped = ped ;
1328 if((ch_status[r][arm][apv][c] & 0x0E) || (ped->cou[arm][apv][c]==0)) {
1331 use_ped = peds_from_cache + r ;
1338 for(
int t=0;t<tb_cou_ped;t++) {
1340 fprintf(f,
"%d %d %2d %3d %2d %7.3f %.3f %.3f\n",r+1,arm,apv,c,t,
1341 use_ped->ped[arm][apv][c][t],
1342 use_ped->rms[arm][apv][c][t],
1343 use_ped->cmn_rms[arm][apv][c][t]) ;
1356 sprintf(cmd,
"/bin/cp /RTScache/pedestals.txt /RTScache/%s_pedestals_%08u_loaded.txt",
"fst",run) ;
1361 double perc = 100.0 * (double)non_cached_cou / (
double)all_cou ;
1363 LOG(U_TONKO,
"Pedestals cached for run %08u: skipped %d/%d (%d%%) channels.",run,non_cached_cou,all_cou,(
int)perc) ;
1366 LOG(TERR,
"Pedestals cached for run %08u: skipped %d/%d channels.",run,non_cached_cou,all_cou) ;
1374 int fstPed::bad_from_cache(
char *fname)
1385 f = fopen(fname,
"r") ;
1388 fn =
"/RTS/conf/fst/fst_bad_channels.txt" ;
1393 LOG(ERR,
"ped::bad_from_cache can't open input file \"%s\" [%s]",fn,strerror(errno)) ;
1398 LOG(NOTE,
"Loading bad from cache \"%s\"...",fn) ;
1403 int r, arm, apv, ch ;
1406 if(fgets(buff,
sizeof(buff),f) == 0) continue ;
1418 int ret = sscanf(buff,
"%d %d %d %d",&r,&arm,&apv,&ch) ;
1419 if(ret != 4) continue ;
1424 memset(n,0,
sizeof(n)) ;
1425 sscanf(buff,
"%s %s %s %s",ca[0],ca[1],ca[2],ca[3]) ;
1426 for(
int i=0;i<4;i++) {
1428 if(sscanf(ca[i],
"%d",&dummy)!=1) continue ;
1430 if(index(ca[i],
'-')) n[i] =
'-' ;
1434 if(dummy<0) n[i] =
'-' ;
1440 if(arm < 0) arm *= -1 ;
1441 if(apv < 0) apv *= -1 ;
1442 if(ch < 0) ch *= -1 ;
1447 for(
int a=0;a<FGT_APV_COU;a++) {
1448 for(
int c=0;c<FGT_CH_COU;c++) {
1449 ch_status[r-1][arm][a][c] |= FGT_CH_STAT_BAD ;
1453 else if(n[2]==
'-') {
1454 for(
int c=0;c<FGT_CH_COU;c++) {
1455 ch_status[r-1][arm][apv][c] |= FGT_CH_STAT_BAD ;
1459 ch_status[r-1][arm][apv][ch] |= FGT_CH_STAT_BAD ;
1467 for(
int r=0;r<FGT_RDO_COU;r++) {
1468 for(
int m=0;m<FST_ARM_COU;m++) {
1469 for(
int a=0;a<FGT_APV_COU;a++) {
1470 for(
int c=0;c<FGT_CH_COU;c++) {
1471 if(ch_status[r][m][a][c] & FGT_CH_STAT_BAD) b_cou++ ;
1477 LOG(INFO,
"Loaded %d bad channels from \"%s\"",b_cou,fn) ;