13 #include <TPC/rowlen.h>
15 #include <TPX/tpx_altro_to_pad.h>
24 int tpx_odd_fee_count = 0 ;
27 static u_int histo_data[2000] ;
28 static void histo_init()
30 memset(histo_data,0,
sizeof(histo_data)) ;
33 static void histo_fill(
double gain)
35 int i_gain = (int) gain ;
37 if((i_gain>=0) && (i_gain<2000)) histo_data[i_gain]++ ;
42 static double histo_peak()
49 for(
int i=0;i<(2000-10);i++) {
51 for(
int j=0;j<10;j++) {
52 sum += histo_data[i+j] ;
63 for(
int i=start_i;i<(start_i+10);i++) {
64 sum += histo_data[i] ;
65 peak += i*histo_data[i] ;
84 c_run = c_date = c_time = 0 ;
95 memset(gains,0,
sizeof(gains)) ;
101 memset(bad_fee,0,
sizeof(bad_fee)) ;
102 tpx_odd_fee_count = 0 ;
104 memset(bad_rdo_mask,0,
sizeof(bad_rdo_mask)) ;
106 pulser_ped = TPX_PULSER_PED_START ;
108 pulser_ped_stop = TPX_TCU_LOC_PED_STOP ;
109 pulser_start = TPX_TCU_LOC_START ;
110 pulser_stop = TPX_TCU_LOC_STOP ;
111 pulser_time_0 = TPX_TCU_LOC_TIME_0 ;
113 clock_mode = TPX_CLOCK_TCU_LOC ;
125 void tpxGain::set_clock_mode(
int mode)
131 case TPX_CLOCK_TCU_LOC :
132 pulser_ped_stop = TPX_TCU_LOC_PED_STOP ;
133 pulser_start = TPX_TCU_LOC_START ;
134 pulser_stop = TPX_TCU_LOC_STOP ;
135 pulser_time_0 = TPX_TCU_LOC_TIME_0 ;
138 pulser_ped_stop = TPX_TCD_PED_STOP ;
139 pulser_start = TPX_TCD_START ;
140 pulser_stop = TPX_TCD_STOP ;
141 pulser_time_0 = TPX_TCD_TIME_0 ;
143 case TPX_CLOCK_TCU_RHIC :
144 pulser_ped_stop = TPX_TCU_RHIC_PED_STOP ;
145 pulser_start = TPX_TCU_RHIC_START ;
146 pulser_stop = TPX_TCU_RHIC_STOP ;
147 pulser_time_0 = TPX_TCU_RHIC_TIME_0 ;
149 case TPX_CLOCK_NEW_TCD :
150 pulser_ped_stop = TPX_NEW_TCD_PED_STOP ;
151 pulser_start = TPX_NEW_TCD_START ;
152 pulser_stop = TPX_NEW_TCD_STOP ;
153 pulser_time_0 = TPX_NEW_TCD_TIME_0 ;
163 void tpxGain::free_store()
180 for(
int i=0;i<24;i++) {
193 void tpxGain::init(
int sec)
203 bytes =
sizeof(
struct means) * 24 * 46 ;
205 means = (
struct means *) malloc(bytes) ;
207 memset(means,0,bytes) ;
209 bytes =
sizeof(
struct aux) * 24 * 46 * 182 ;
211 aux = (
struct aux *) malloc(bytes) ;
213 memset(aux,0,bytes) ;
215 bytes =
sizeof(
struct fee_found_t) * 25 * 7 ;
217 fee_found = (
struct fee_found_t *) malloc(bytes) ;
220 memset(fee_found,0,bytes) ;
222 for(
int i=0;i<24;i++) {
223 bytes =
sizeof(
struct gains) * 46 * 182 ;
225 gains[i] = (
struct gains *) malloc(bytes) ;
227 memset(gains[i],0,bytes) ;
231 memset(tpx_pulser_peak,0,
sizeof(tpx_pulser_peak)) ;
233 memset(bad_rdo_mask,0,
sizeof(bad_rdo_mask)) ;
242 void tpxGain::ev_done()
245 LOG(WARN,
"Using hardcoded tb range: ped at [%3d:%3d], peak at [%3d:%3d], T0 at %f!",pulser_ped, pulser_ped_stop, pulser_start,pulser_stop,pulser_time_0) ;
249 LOG(NOTE,
"After event %d",events) ;
258 void tpxGain::accum(
char *evbuff,
int bytes)
270 t = tpx_get_start(evbuff, bytes/4, &rdo, 0) ;
278 data_end = rdo.data_end ;
279 a.rdo = rdo.rdo - 1 ;
280 a.what = TPX_ALTRO_DO_ADC ;
282 a.sector = rdo.sector ;
286 struct fee_found_t *fee_f = get_fee_found(sec,rdo.rdo) ;
289 LOG(DBG,
"gain: evt %d (got_one %d), sector %d, rdo %d",events, fee_f->got_one,rdo.sector, rdo.rdo) ;
293 data_end = tpx_scan_to_next(data_end, rdo.data_start, &a) ;
295 fee_f->ch_count[a.id][a.ch]++ ;
297 if((a.row>45) || (a.pad>182)) {
298 LOG(TERR,
"Should not be here! row %d, pad %d, aid %3d:%02d",a.row,a.pad, a.id, a.ch) ;
302 gs = get_gains(sec,a.row,a.pad) ;
303 as = get_aux(sec,a.row,a.pad) ;
312 if(a.count) as->cou++ ;
314 for(i=0;i<a.count;i++) {
315 if(a.adc[i] == 0) continue ;
317 int adc_i = a.adc[i] ;
323 if((tb_i>=pulser_ped) && (tb_i<=pulser_stop)) {
324 as->adc_store[tb_i - pulser_ped] += adc_i ;
326 else if(tb_i < pulser_ped) {
332 if(cou) noise /= cou ;
335 if((noise > 40.0) || (cou>20)) {
339 }
while(data_end && (data_end > rdo.data_start)) ;
352 void tpxGain::if_file()
354 memset(bad_rdo_mask, 0,
sizeof(bad_rdo_mask)) ;
356 for(
int s=1;s<=24;s++) {
357 for(
int r=1;r<=6;r++) {
358 struct fee_found_t *fee_f = get_fee_found(s,r) ;
361 for(
int a=0;a<256;a+=2) {
364 if(tpx_altro_to_fee(r,a)<0) continue ;
368 for(
int w=a;w<(a+2);w++) {
369 for(
int c=0;c<16;c++) {
370 if(fee_f->ch_count[w][c]) {
379 int c = bad_fee[s][r][0] ;
381 bad_fee[s][r][c+1] = a ;
384 int fee = tpx_altro_to_fee(r,a) ;
386 LOG(WARN,
"Possible bad FEE %3d (ALTRO %3d) on sector %2d, RDO %d",fee,a,s,r) ;
387 LOG(WARN,
" to tpx_gains.txt: %d %d %d 0.000 0.000",-s,r,fee) ;
392 bad_rdo_mask[s] |= (1<<(r-1)) ;
393 LOG(WARN,
"Possible masked RDO %d on sector %2d",r,s) ;
414 double g_rms, t0_rms, c_rms ;
415 int t0_mean_count[25] ;
418 memset(t0_mean_count,0,
sizeof(t0_mean_count)) ;
419 memset(t0_mean,0,
sizeof(t0_mean)) ;
421 g_rms = t0_rms = c_rms = 0 ;
427 LOG(DBG,
"gain_calc: doing calculation with %d events",events) ;
433 int s_start, s_stop ;
439 else if(sector <= 24) {
444 s_start = (sector-24)*2-1 ;
445 s_stop = s_start + 1 ;
450 if(raw_gains_fname) {
451 sprintf(fname,
"%s_%02d",raw_gains_fname,sector) ;
452 ofile = fopen(fname,
"w") ;
456 for(s=s_start;s<=s_stop;s++) {
462 if(get_means(s,r)->g || get_means(s,r)->t0) {
463 LOG(ERR,
"%f %f",get_means(s,r)->g,get_means(s,r)->t0) ;
467 for(p=1;p<=tpc_rowlen[r];p++) {
468 c = get_aux(s,r,p)->cou ;
471 get_gains(s,r,p)->g = 0.0 ;
472 get_gains(s,r,p)->t0 = 9.999 ;
475 tpx_to_altro(r,p,rdo,aa,cc) ;
477 if(bad_rdo_mask[s] & (1<<rdo)) {
479 LOG(WARN,
"Masked Sector %d, RDO %d, row %d, pad %d",s,rdo+1,r,p) ;
480 get_gains(s,r,p)->t0 = -9.990 ;
486 int c = bad_fee[s][rdo][0] ;
488 for(
int i=0;i<c;i++) {
489 int al=bad_fee[s][rdo][i+1];
490 for(
int j=0;j<2;j++) {
494 get_gains(s,r,p)->t0 = -9.900 ;
505 struct aux *as = get_aux(s,r,p) ;
508 for(
int i=pulser_ped;i<=pulser_ped_stop;i++) {
509 ped += (double) as->adc_store[i-pulser_ped] / (
double) c ;
519 for(
int i=pulser_start;i<=pulser_stop;i++) {
520 double val = (double) as->adc_store[i-pulser_ped] / (
double) c - ped ;
532 get_gains(s,r,p)->g = charge ;
535 get_gains(s,r,p)->t0 = t0/charge ;
538 get_gains(s,r,p)->t0 = 0.0 ;
539 get_gains(s,r,p)->g = 0.0 ;
559 for(p=1;p<=tpc_rowlen[r];p++) {
560 struct aux *aux = get_aux(s,r,p) ;
563 aux->low_pulse = (100 * aux->low_pulse) / events ;
564 aux->high_pulse = (100 * aux->high_pulse) / events ;
565 aux->noise = (100 * aux->noise) / events ;
573 for(p=1;p<=tpc_rowlen[r];p++) {
574 struct aux *aux = get_aux(s,r,p) ;
575 int fired = aux->cou ;
579 else aux->low_pulse = 100 ;
583 if(get_gains(s,r,p)->g < 100.0) aux->low_pulse = 100 ;
584 if(get_gains(s,r,p)->g > 2000.0) aux->high_pulse = 100 ;
587 if((aux->low_pulse > 10) || (aux->noise > 10) || (aux->high_pulse > 10)) {
588 if(r != 0) LOG(NOTE,
"ROW %d, pad %d: lo %d, noise %d, hi %d -- skipping",r,p,
589 aux->low_pulse, aux->noise, aux->high_pulse) ;
594 if((p>=4) && (p<=(tpc_rowlen[r]-3))) {
595 histo_fill(get_gains(s,r,p)->g) ;
600 int accepted_pads_cou = c ;
602 double row_means = histo_peak() ;
613 tot_pads_cou = good_pads_cou = 0 ;
617 for(p=4;p<=(tpc_rowlen[r]-3);p++) {
618 double g = get_gains(s,r,p)->g ;
619 double t0 = get_gains(s,r,p)->t0 ;
623 if( (g>(row_means*0.9)) && (g<(row_means*1.1))) {
626 LOG(WARN,
"TO: %d %d %d = %f %f",s,r,p,g,t0) ;
630 get_means(s,r)->g += g ;
631 get_means(s,r)->g_rms += g * g ;
633 get_means(s,r)->t0 += get_gains(s,r,p)->t0 ;
634 get_means(s,r)->t0_rms += get_gains(s,r,p)->t0 * get_gains(s,r,p)->t0 ;
646 get_means(s,r)->t0 = tb_stop ;
649 get_means(s,r)->g /= c ;
650 get_means(s,r)->g_rms /= c ;
652 get_means(s,r)->g_rms = sqrt(get_means(s,r)->g_rms - get_means(s,r)->g * get_means(s,r)->g) ;
654 get_means(s,r)->t0 /= c ;
655 get_means(s,r)->t0_rms /= c ;
657 double tmp = get_means(s,r)->t0_rms - get_means(s,r)->t0 * get_means(s,r)->t0 ;
659 get_means(s,r)->t0_rms = 0.0 ;
662 get_means(s,r)->t0_rms = sqrt(tmp) ;
674 printf(
"%2d %2d %f %f %f %f\n",s,r,
675 get_means(s,r)->g, get_means(s,r)->g_rms,
676 get_means(s,r)->t0, get_means(s,r)->t0_rms) ;
684 for(p=1;p<=tpc_rowlen[r];p++) {
686 if(ofile) fprintf(ofile,
"%d %d %d %.3f %.3f ",s,r,p,get_gains(s,r,p)->g,get_gains(s,r,p)->t0) ;
688 if(get_gains(s,r,p)->g>0.0001) {
689 double t0 = get_gains(s,r,p)->t0 ;
692 LOG(WARN,
"T0 S%d %d %d = %f %f",s,r,p,get_gains(s,r,p)->g,t0) ;
697 get_gains(s,r,p)->g = get_means(s,r)->g / get_gains(s,r,p)->g ;
702 if((p==1) || (p==2) || (p==tpc_rowlen[r]) || (p==(tpc_rowlen[r]-1))) ;
704 t0_mean[s] += get_gains(s,r,p)->t0 ;
711 if(ofile) fprintf(ofile,
"%.3f %.3f\n",get_gains(s,r,p)->g, get_gains(s,r,p)->t0) ;
715 if(get_means(s,r)->g != 0.0) {
716 LOG(TERR,
"Sector %2d, row %2d: charge %.3f +- %.3f; t0 %.3f +- %.3f; rough mean %.3f; good/acc/all pads %d/%d/%d",
718 get_means(s,r)->g, get_means(s,r)->g_rms,
719 get_means(s,r)->t0, get_means(s,r)->t0_rms,
720 row_means,good_pads_cou, accepted_pads_cou, tot_pads_cou) ;
723 LOG(WARN,
"Sector %2d, row %2d: charge %f +- %f; t0 %f +- %f; rough_mean %.1f; good/acc/all pads %d/%d/%d",
725 get_means(s,r)->g, get_means(s,r)->g_rms,
726 get_means(s,r)->t0, get_means(s,r)->t0_rms,
727 row_means,good_pads_cou, accepted_pads_cou, tot_pads_cou) ;
732 if(get_means(s,r)->g) {
738 g_rms += get_means(s,r)->g_rms ;
739 t0_rms += get_means(s,r)->t0_rms ;
753 if(ofile) fclose(ofile) ;
755 double t0_all_mean = 0.0 ;
756 int t0_all_mean_count = 0 ;
757 for(
int s=s_start;s<=s_stop;s++) {
758 if(t0_mean_count[s]) {
759 t0_mean[s] /= (double) t0_mean_count[s] ;
761 t0_all_mean += t0_mean[s] ;
762 t0_all_mean_count++ ;
764 LOG(TERR,
"Sector %d: t0_mean %f, count was %d",s,t0_mean[s],t0_mean_count[s]) ;
769 if((s_start==16) && (s_stop==16)) {
770 LOG(WARN,
"Sector 16 -- special case") ;
771 t0_all_mean += t0_mean[16] ;
772 t0_all_mean_count = 1 ;
775 if(t0_all_mean_count) t0_all_mean /= (double) t0_all_mean_count ;
779 for(
int s=s_start;s<=s_stop;s++) {
780 for(
int r=1;r<=45;r++) {
781 for(
int p=1;p<=tpc_rowlen[r];p++) {
782 double t0 = get_gains(s,r,p)->t0 ;
784 if(get_gains(s,r,p)->g) {
785 get_gains(s,r,p)->t0 = t0_all_mean - t0 ;
792 LOG(TERR,
"gain_calc: sectors [%d:%d]: %d events used: Mean RMS: %f gain; T0 %f +- %f, diff from canonical %f",
797 pulser_time_0-t0_all_mean) ;
803 void tpxGain::do_default(
int sec)
809 memset(bad_fee,0,
sizeof(bad_fee)) ;
810 tpx_odd_fee_count = 0 ;
813 if(gains[sec-1] == 0) {
814 gains[sec-1] = (
struct gains *) malloc(
sizeof(
struct gains) * 46 * 182) ;
818 for(
int i=0;i<24;i++) {
820 gains[i] = (
struct gains *) malloc(
sizeof(
struct gains) * 46 * 182) ;
828 memset(gains[s-1],0,
sizeof(
struct gains) * 46 * 182) ;
835 for(p=1;p<=tpc_rowlen[r];p++) {
836 set_gains(s,r,p,1.0,0.0) ;
845 int tpxGain::from_file(
const char *fname,
int sec)
855 f = fopen(fname,
"r") ;
858 LOG(WARN,
"from_file: error in fopen \"%s\" [%s]",fname,strerror(errno)) ;
867 LOG(DBG,
"After stat %s %d %d",fname,buff.st_mtime,load_time) ;
869 if(load_time < buff.st_mtime) {
870 LOG(DBG,
"Will reload...") ;
871 LOG(INFO,
"Reloading \"%s\"",fname) ;
874 LOG(DBG,
"Wont relaod") ;
875 LOG(INFO,
"Keeping cached copy of gains...") ;
887 u_int f_date, f_time ;
890 localtime_r(&buff.st_mtime, &tm) ;
892 f_date = (tm.tm_year+1900) * 10000 + (tm.tm_mon+1) * 100 + tm.tm_mday ;
893 f_time = tm.tm_hour * 10000 + tm.tm_min * 100 + tm.tm_sec ;
897 load_time = time(NULL) ;
900 LOG(TERR,
"reading gains from \"%s\" for sector %d...",fname, sector) ;
907 if(fgets(str,
sizeof(str)-1,f)==0) continue ;
909 if(strlen(str)==0) continue ;
911 if((str[0]==
'#') || (str[0]==
'/')) {
913 if((cix = strstr(str,
"Run "))) {
914 sscanf(cix+4,
"%u",&c_run) ;
916 else if((cix = strstr(str,
"Date "))) {
917 sscanf(cix+5,
"%u",&c_date) ;
919 else if((cix = strstr(str,
"Time "))) {
920 sscanf(cix+5,
"%u",&c_time) ;
927 int ret = sscanf(str,
"%d %d %d %f %f\n",&s,&r,&p,&g,&t0) ;
929 if(ret != 5) continue ;
935 if(sector && (s != sector)) continue ;
940 int c = bad_fee[s][r][0] ;
945 altro = (p<<1) & 0xFF ;
946 bad_fee[s][r][c+1] = altro ;
951 c = tpx_odd_fee_count ;
952 tpx_odd_fee[c].sector = s ;
953 tpx_odd_fee[c].rdo = r ;
954 tpx_odd_fee[c].status = 2 ;
955 tpx_odd_fee[c].tpc_fee_padplane = p ;
956 tpx_odd_fee[c].altro_id_padplane = altro ;
958 tpx_odd_fee_count++ ;
960 LOG(INFO,
"Bad FEE %d: sector %2d, RB %d, ALTRO %3d (TPC-FEE %3d)",bad_fee[s][r][0],s,r,altro,p) ;
966 if(sector && (s != sector)) continue ;
968 set_gains(s,r,p,g,t0) ;
973 if(tpx_is_stgc)
goto skip_per_pad ;
978 if(sector && (s != sector)) continue ;
982 for(u_int c=0;c<bad_fee[s][r][0];c++) {
985 fee = bad_fee[s][r][c+1] ;
987 for(
int i=0;i<2;i++) {
988 for(
int ch=0;ch<16;ch++) {
991 tpx_from_altro(r-1,fee+i,ch, row, pad) ;
994 LOG(ERR,
"What %d: %d %d %d????",row,r,fee+i,ch) ;
998 set_gains(s,row,pad,0.0,-9.900) ;
1001 LOG(DBG,
"Killing rp %d:%d for bad FEE %3d in sector %2d",row,pad,fee,s) ;
1011 LOG(TERR,
"Gains read: run %08u, date %08u [%08u], time %06u [%06u]",c_run,c_date,f_date,c_time,f_time) ;
1016 int tpxGain::to_file(
const char *fname)
1022 time_t tim = time(NULL) ;
1024 if(strcmp(fname,
"stdout")==0) f = stdout ;
1026 f = fopen(fname,
"w") ;
1030 LOG(ERR,
"gains: fopen \"%s\" [%s]",fname,strerror(errno)) ;
1036 int s_start, s_stop ;
1048 localtime_r(&tim, &tm) ;
1050 c_date = (tm.tm_year+1900) * 10000 + (tm.tm_mon+1) * 100 + tm.tm_mday ;
1051 c_time = tm.tm_hour * 10000 + tm.tm_min * 100 + tm.tm_sec ;
1053 LOG(TERR,
"gains: writing to file \"%s\" for sectors %d..%d: run %u, date %u, time %u",fname,
1055 c_run, c_date, c_time) ;
1057 fprintf(f,
"# $Id: tpxGain.cxx,v 1.38 2024/04/10 12:01:00 tonko Exp $\n") ;
1058 fprintf(f,
"# Run %u\n",c_run) ;
1060 for(s=s_start;s<=s_stop;s++) {
1062 if(bad_rdo_mask[s]) {
1063 LOG(WARN,
"Sector %02d: bad rdo mask 0x%02X",s,bad_rdo_mask[s]) ;
1067 for(r=1;r<=45;r++) {
1068 for(p=1;p<=tpc_rowlen[r];p++) {
1069 float t0 = get_gains(s,r,p)->t0 ;
1070 float gg = get_gains(s,r,p)->g ;
1072 fprintf(f,
"%d %d %d %.3f %6.3f",s,r,p,
1077 fprintf(f,
" # RDO missing\n") ;
1079 else if(t0 < -9.0) {
1080 fprintf(f,
" # FEE missing\n") ;
1090 fprintf(f,
"# Date %u\n",c_date) ;
1091 fprintf(f,
"# Time %u\n",c_time) ;
1093 if(f != stdout) fclose(f) ;
1095 LOG(TERR,
"gains: written.") ;
1100 void tpxGain::compare(
char *fname,
int mysec)
1105 double dg_mean, dg_rms ;
1106 double dt_mean, dt_rms ;
1109 int both, old_only, new_only ;
1111 old_only = new_only = both = 0 ;
1113 f = fopen(fname,
"r") ;
1116 LOG(ERR,
"from_file: error in fopen \"%s\" [%s]",fname,strerror(errno)) ;
1120 dg_mean = dg_rms = dt_mean = dt_rms = 0.0 ;
1124 char (*f_bad)[46][183] = (char (*)[46][183]) malloc(25*46*183) ;
1127 LOG(ERR,
"Malloc failed? Wha? [%s]",strerror(errno)) ;
1136 if(fgets(str,
sizeof(str)-1,f)==0) continue ;
1138 if(strlen(str)==0) continue ;
1139 if((str[0]==
'#') || (str[0]==
'/')) continue ;
1142 int ret = sscanf(str,
"%d %d %d %f %f\n",&s,&r,&p,&g,&t0) ;
1144 if(ret != 5) continue ;
1145 if(s < 0) continue ;
1147 if(mysec && (s != mysec)) continue ;
1154 if((p==1) || (p==2)) continue ;
1155 if((p==tpc_rowlen[r]) || (p==(tpc_rowlen[r]-1))) continue ;
1157 if(g==0.0) f_bad[s][r][p] = 1 ;
1158 else f_bad[s][r][p] = 0 ;
1163 for(
int s=1;s<=24;s++) {
1164 if(mysec && (s != mysec)) continue ;
1166 for(
int r=1;r<=45;r++) {
1167 for(
int p=1;p<=tpc_rowlen[r];p++) {
1170 if((p==1) || (p==2)) continue ;
1171 if((p==tpc_rowlen[r]) || (p==(tpc_rowlen[r]-1))) continue ;
1175 if(f_bad[s][r][p]) {
1176 if(get_gains(s,r,p)->g == 0.0) both++ ;
1182 if(get_gains(s,r,p)->g == 0.0) {
1183 if(get_gains(s,r,p)->t0 < -9.0) {
1184 LOG(DBG,
"FEE/RDO was marked as bad (%d,%d,%d) -- skipping",s,r,p) ;
1192 double dg = get_gains(s,r,p)->g / g ;
1193 double dt0 = get_gains(s,r,p)->t0 - t0 ;
1199 dt_rms += dt0 * dt0 ;
1214 dg_mean /= (double) dg_count ;
1215 dg_rms /= (double) dg_count ;
1216 dt_mean /= (double) dg_count ;
1217 dt_rms /= (double) dg_count ;
1219 dg_rms = sqrt(dg_rms - dg_mean * dg_mean) ;
1220 dt_rms = sqrt(dt_rms - dt_mean * dt_mean) ;
1223 int max_allowed = 2 ;
1228 if(new_only>max_allowed) {
1229 LOG(ERR,
"gain_compare to %s: sector %d: seems to have new bad pads: both %3d, new_only %3d, old_only %d",
1231 mysec,both,new_only,old_only) ;
1234 LOG(INFO,
"gain_compare to %s: sector %d: both %3d, new_only %3d, old_only %d",
1235 fname,mysec,both,new_only,old_only) ;
1243 int tpxGain::summarize(
char *fname, FILE *log,
int gain_mode)
1254 if(fname==0) ofile = 0 ;
1255 else if(strcmp(fname,
"stdout")==0) ofile = stdout ;
1257 ofile = fopen(fname,
"w") ;
1259 getcwd(reason,100) ;
1260 LOG(ERR,
"summarize: error in fopen %s\"%s\" [%s] ",reason,fname,strerror(errno)) ;
1265 LOG(TERR,
"fname %s, logfile %p, gain_mode %d",fname,log,gain_mode) ;
1268 int s_start, s_stop ;
1270 s_start = s_stop = sector ;
1279 for(s=s_start;s<=s_stop;s++) {
1283 struct fee_found_t *fee_f = get_fee_found(s,r) ;
1286 LOG(ERR,
"FEE %d,%d -- not found",s,r) ;
1289 if(fee_f->got_one == 0) continue ;
1291 if(events != (
int)fee_f->got_one) {
1292 LOG(WARN,
"Run had sector %d, RB %d: expect %u events, got %u",s,r,events,fee_f->got_one) ;
1295 LOG(NOTE,
"Run had sector %d, RB %d: expect %u events, got %u",s,r,events,fee_f->got_one) ;
1298 for(
int i=0;i<36;i++) {
1299 int fee = tpx_rdo_fees(r,i) ;
1300 if(fee == 255) continue ;
1304 for(
int fch=0;fch<32;fch++) {
1306 int ch = tpx_old_to_new_ch[fch] ;
1310 a = ((fee << 1) | 1) & 0xFF ;
1313 a = (fee << 1) & 0xFF ;
1320 int seen = fee_f->ch_count[a][ch] ;
1325 tpx_from_altro(r-1,a,ch,row,pad) ;
1333 double g = get_gains(s,row,pad)->g ;
1334 double t0 = get_gains(s,row,pad)->t0 ;
1336 struct aux *aux = get_aux(s,row,pad) ;
1340 if(seen != events) {
1344 sprintf(reason+strlen(reason),
"[Bad - Missing]") ;
1349 sprintf(reason+strlen(reason),
"[Bad - Flaky readout: seen %d times in %d events]",seen,events) ;
1356 int bad_t0, bad_gain, bad_noise, bad_low ;
1357 bad_t0 = bad_gain = bad_noise = bad_low = 0 ;
1361 if((g<0.9) || (g>1.1)) {
1366 if((pad<=2) || (pad>=(tpc_rowlen[row]-1))) {
1374 if((t0<-0.40) || (t0>0.40)) {
1379 if((pad<=2) || (pad>=(tpc_rowlen[row]-1))) {
1388 if(aux->noise > 10) {
1389 bad_noise = aux->noise ;
1392 if((pad<=2) || (pad>=(tpc_rowlen[row]-1))) {
1400 if(aux->low_pulse > 1) {
1401 bad_low = aux->low_pulse ;
1404 if((pad<=2) || (pad>=(tpc_rowlen[row]-1))) {
1417 u_char j1 = tpx_altro_to_j1[a&1][ch] ;
1420 if(row && bad_gain) sprintf(reason+strlen(reason),
"[Bad gain %.1f]",g) ;
1421 if(row && bad_t0) sprintf(reason+strlen(reason),
"[Bad t0 %.1f]",t0) ;
1422 if(row && bad_noise) {
1423 if(gain_mode != GAIN_MODE_CORRECTED) {
1424 sprintf(reason+strlen(reason),
"[Bad noise %d%%]",bad_noise) ;
1427 sprintf(reason+strlen(reason),
"[JUST NOISE %d%%]",bad_noise) ;
1430 if(row && bad_low) sprintf(reason+strlen(reason),
"[Bad low %d%%]",bad_low) ;
1436 if(gain_mode != GAIN_MODE_CORRECTED) {
1437 fprintf(log,
"%2d %d %3d %2d %3d %2d %2d %3d %.3f %6.3f %d %s\n",s,r,fee,j1,a,ch,row,pad,
1440 else if(row && bad_noise) {
1441 fprintf(log,
"%2d %d %3d %2d %3d %2d %2d %3d %.3f %6.3f %d %s\n",s,r,fee,j1,a,ch,row,pad,
1447 if(gain_mode != GAIN_MODE_CORRECTED) {
1448 if((err>1) || bad_gain || bad_t0 || bad_noise || bad_low) {
1450 get_gains(s,row,pad)->g = 0.0 ;
1453 if(ofile) fprintf(ofile,
"%2d %d %3d %2d %3d %2d %2d %3d %.3f %6.3f %d %s\n",s,r,fee,j1,a,ch,row,pad,
1462 get_gains(s,row,pad)->g = 0.0 ;
1463 get_gains(s,row,pad)->t0 = 0.0 ;
1466 fprintf(ofile,
"%2d %d %3d %2d %3d %2d %2d %3d %.3f %6.3f %d %s\n",s,r,fee,j1,a,ch,row,pad,
1468 fprintf(ofile,
"%d %d %d 0.000 0.000 # ONLY NOISY\n",s,row,pad) ;
1473 get_gains(s,row,pad)->g = 1.0 ;
1474 get_gains(s,row,pad)->t0 = -10.0 ;
1478 fee_f->ch_count[a][ch] *= -1 ;
1482 for(a=0;a<256;a++) {
1483 for(
int ch=0;ch<16;ch++) {
1484 u_char j1 = tpx_altro_to_j1[a&1][ch] ;
1485 if(fee_f->ch_count[a][ch] > 0) {
1486 int fee = tpx_altro_to_fee(r,a) ;
1489 fprintf(log,
"%2d %d %3d %2d %3d %2d %2d %3d %.3f %6.3f %d %s\n",s,r,fee,j1,a,ch,-1,-1,
1490 0.0,0.0,3,
"[Bad - Spurious channel]") ;
1493 if(ofile) fprintf(ofile,
"%2d %d %3d %2d %3d %2d %2d %3d %.3f %6.3f %d %s\n",s,r,fee,j1,a,ch,-1,-1,
1494 0.0,0.0,3,
"[Bad - Spurious channel]") ;
1499 if(ofile) fflush(ofile) ;
1504 if(ofile && (ofile != stdout)) fclose(ofile) ;