10 #include <DAQ_ITPC/itpcCore.h>
17 memset(ped_p,0,
sizeof(ped_p)) ;
18 memset(padplane_id,0,
sizeof(padplane_id)) ;
20 pulser_peak_timebin = 90 ;
27 for(
int s=0;s<24;s++) {
28 for(
int r=0;r<4;r++) {
29 for(
int p=0;p<16;p++) {
30 for(
int c=0;c<64;c++) {
31 if(ped_p[s][r][p][c]) {
32 free(ped_p[s][r][p][c]) ;
33 ped_p[s][r][p][c] = 0 ;
42 int itpcPed::kill_non_phys()
46 for(
int s=0;s<24;s++) {
47 for(
int r=0;r<4;r++) {
48 for(
int p=0;p<16;p++) {
49 for(
int c=0;c<64;c++) {
50 if(ped_p[s][r][p][c]) ;
53 int row, pad, fee_id ;
54 fee_id = padplane_id[s][r][p] ;
57 itpc_ifee_to_rowpad(fee_id,c,row,pad) ;
61 LOG(NOTE,
"Non-phys ch %d: %d %d %d %d: fee_id %d, rp %d:%d",cou,s+1,r+1,p+1,c,row,pad) ;
63 ped_p[s][r][p][c]->c_ped = 1023.0 ;
75 void itpcPed::init(
int sector,
int rdo, u_int mask)
77 LOG(NOTE,
"%s: sector %d, RDO %d, mask 0x%04X",__PRETTY_FUNCTION__,sector,rdo,mask) ;
83 for(
int p=0;p<16;p++) {
84 for(
int c=0;c<64;c++) {
85 if(ped_p[sector][rdo][p][c]) continue ;
87 ped_p[sector][rdo][p][c] = (ped_t *)malloc(
sizeof(ped_t)) ;
92 fee_mask[sector][rdo] = mask ;
97 void itpcPed::set_padplane_id(
int sector,
int rdo,
int port,
int id)
103 padplane_id[sector][rdo][port] = id ;
107 void itpcPed::clear()
109 memset(evts,0,
sizeof(evts)) ;
111 for(
int s=0;s<24;s++) {
112 for(
int r=0;r<4;r++) {
113 for(
int p=0;p<16;p++) {
114 for(
int c=0;c<64;c++) {
115 if(ped_p[s][r][p][c]) {
116 memset(ped_p[s][r][p][c],0,
sizeof(ped_t)) ;
123 memset(fee_err,0,
sizeof(fee_err)) ;
128 void itpcPed::accum(
int sector,
int rdo,
int port,
int fee_id,
int ch,
int tb,
int adc_val)
130 ped_t *pt = ped_p[sector][rdo][port][ch] ;
134 if(evts[sector][rdo]<250) {
135 evts[sector][rdo]++ ;
139 pt->ped[tb] += adc_val ;
140 pt->rms[tb] += adc_val*adc_val ;
143 pt->c_ped += adc_val ;
144 pt->c_rms += adc_val*adc_val ;
152 for(
int s=0;s<24;s++) {
153 for(
int r=0;r<4;r++) {
154 if(ped_p[s][r][0][0]==0) continue ;
158 for(
int p=0;p<16;p++) {
159 if(fee_mask[s][r] & (1<<p)) ;
162 for(
int c=0;c<64;c++) {
163 ped_t *pt = ped_p[s][r][p][c] ;
173 pt->c_ped /= pt->c_cou ;
174 pt->c_rms /= pt->c_cou ;
175 pt->c_rms = pt->c_rms - pt->c_ped*pt->c_ped ;
177 if(pt->c_rms <= 0.0) pt->c_rms = 0.0 ;
178 else pt->c_rms = sqrt(pt->c_rms) ;
180 for(
int t=0;t<512;t++) {
182 pt->ped[t] = 1023.0 ;
183 pt->rms[t] = 1022.0 ;
187 pt->ped[t] /= pt->cou[t] ;
188 pt->rms[t] /= pt->cou[t] ;
190 pt->rms[t] = pt->rms[t] - pt->ped[t]*pt->ped[t] ;
192 if(pt->rms[t] <= 0.0) pt->rms[t] = 0.0 ;
193 else pt->rms[t] = sqrt(pt->rms[t]) ;
204 int itpcPed::from_cache(
const char *fname,
int sec1,
int rdo1)
210 sprintf(fn,
"/RTScache/itpc_pedestals_s%02d_r%d.txt",sec1,rdo1) ;
212 else strcpy(fn,fname) ;
217 LOG(ERR,
"%s: [%s]",fn,strerror(errno)) ;
224 int sec, rdo, fee_port, fee_ch, tb, fee_id, dummy ;
225 float fd, fped, frms ;
227 if(fgets(buff,
sizeof(buff),f)==0) continue ;
229 int ret = sscanf(buff,
"%d %d %d %d %d %d %d %d %d %f %f %f %f",
230 &sec,&rdo,&fee_port,&fee_ch,&fee_id,&dummy,&dummy,&dummy,&dummy,&fd,&fd,&fped,&frms) ;
240 if(ped_p[sec][rdo][fee_port][fee_ch]) ;
243 padplane_id[sec][rdo][fee_port] = fee_id ;
245 ped_p[sec][rdo][fee_port][fee_ch]->c_ped = fped ;
246 ped_p[sec][rdo][fee_port][fee_ch]->c_rms = frms ;
249 ret = sscanf(buff,
"%d %d %d %d %d %f %f",
250 &sec,&rdo,&fee_port,&fee_ch,&tb,&fped,&frms) ;
262 if(ped_p[sec][rdo][fee_port][fee_ch]) ;
265 ped_p[sec][rdo][fee_port][fee_ch]->ped[tb] = fped ;
266 ped_p[sec][rdo][fee_port][fee_ch]->rms[tb] = frms ;
279 int itpcPed::sanity(
int mode)
285 sprintf(fname,
"/log/itpc/itpc_log_%02d.txt",sector_id) ;
286 FILE *f = fopen(fname,
"a") ;
288 LOG(ERR,
"sanity: %s [%s]",fname,strerror(errno)) ;
291 time_t now = time(0) ;
293 fprintf(f,
"Run %08u, Sector %2d, Run-type %d. Date %s",run_number,sector_id,run_type,ctime(&now)) ;
297 LOG(INFO,
"Using pulser peak timebin %d, in STAR %c",
298 pulser_peak_timebin, pulser_in_star?
'Y':
'N') ;
301 for(
int s=0;s<24;s++) {
302 for(
int r=0;r<4;r++) {
303 if(ped_p[s][r][0][0]==0) continue ;
309 for(
int p=0;p<16;p++) {
310 if(fee_mask[s][r] & (1<<p)) ;
313 for(
int c=0;c<64;c++) {
314 ped_t *pt = ped_p[s][r][p][c] ;
317 int row, pad, fee_id ;
319 fee_id = padplane_id[s][r][p] ;
325 itpc_ifee_to_rowpad(fee_id,c,row,pad) ;
328 itpc_rowpad_to_id(row,pad,fee_id,pin) ;
337 for(
int t=0;t<=20;t++) {
338 m_ped += pt->ped[t] ;
339 m_rms += pt->rms[t] ;
348 double pulser = pt->ped[pulser_peak_timebin] - m_ped ;
353 if((m_ped<20)||(m_ped>150)) bad |= 1 ;
354 if(m_rms < 0.5) bad |= 2 ;
358 if(mode==1 && pulser_in_star) {
365 if(m_rms>1.8) bad |= 4 ;
368 if(mode==1 && pulser < 200) bad |= 8 ;
373 fee_err[s][r][p][c] |= bad ;
376 fprintf(f,
" Bad FEE Ch: RDO %d, Port #%d, Ch %d (Padplane %d, rp %d:%d): flag 0x%X: %.1f +- %.1f, %.1f\n",
377 r+1,p+1,c,fee_id,row,pad,bad,m_ped,m_rms,pulser) ;
380 LOG(WARN,
"Bad FEE Ch: RDO %d, Port #%d, Ch %d (Padplane %d, rp %d:%d): flag 0x%X: %.1f +- %.1f, %.1f",
381 r+1,p+1,c,fee_id,row,pad,bad,m_ped,m_rms,pulser) ;
395 fprintf(f,
"Bad channels: %d/%d\n",bad_cou,bad_cou+good_cou) ;
399 LOG(INFO,
"Bad channels: %d/%d",bad_cou,bad_cou+good_cou) ;
406 int itpcPed::to_cache(
const char *fname,
int sec1,
int rdo1)
409 int s_start, s_stop ;
410 int r_start, r_stop ;
419 outf = fopen(fname,
"w") ;
421 LOG(ERR,
"%s: %s [%s]",__PRETTY_FUNCTION__,fname,strerror(errno)) ;
432 if(evts[sec1-1][rdo1-1]<100) {
433 LOG(ERR,
"S%02d:%d -- not enough ped events %d",sec1,rdo1,evts[sec1-1][rdo1-1]) ;
443 sprintf(fn,
"/RTScache/itpc_pedestals_s%02d_r%d.txt",sec1,rdo1) ;
445 outf = fopen(fn,
"w") ;
447 LOG(ERR,
"%s: %s [%s]",__PRETTY_FUNCTION__,fn,strerror(errno)) ;
454 for(
int sx=s_start;sx<=s_stop;sx++) {
456 for(
int rx=r_start;rx<=r_stop;rx++) {
459 if(ped_p[s][r][0][0]==0) continue ;
463 for(
int p=0;p<16;p++) {
464 if(fee_mask[s][r] & (1<<p)) ;
467 for(
int c=0;c<64;c++) {
468 ped_t *pt = ped_p[s][r][p][c] ;
471 int row, pad, fee_id ;
473 fee_id = padplane_id[s][r][p] ;
479 itpc_ifee_to_rowpad(fee_id,c,row,pad) ;
482 itpc_rowpad_to_id(row,pad,fee_id,pin) ;
491 for(
int t=0;t<=20;t++) {
492 m_ped += pt->ped[t] ;
493 m_rms += pt->rms[t] ;
501 fprintf(outf,
"%2d %d %2d %2d %2d %2d %2d %3d %3d %.3f %.3f %.3f %.3f \n",s+1,r+1,p+1,c,fee_id,pin,row,pad,-1,pt->c_ped,pt->c_rms, m_ped, m_rms) ;
515 for(
int sx=s_start;sx<=s_stop;sx++) {
517 for(
int rx=r_start;rx<=r_stop;rx++) {
520 if(ped_p[s][r][0][0]==0) continue ;
524 for(
int p=0;p<16;p++) {
525 if(fee_mask[s][r] & (1<<p)) ;
528 for(
int c=0;c<64;c++) {
529 ped_t *pt = ped_p[s][r][p][c] ;
531 for(
int t=0;t<512;t++) {
532 double ped = pt->ped[t] ;
539 if(ped<2.0) ped = 0.0 ;
541 if(ped==0.0) continue ;
543 if(pt->rms[t]>1000.0) continue ;
546 fprintf(outf,
"%2d %d %2d %2d %3d %.3f %.3f\n",s+1,r+1,p+1,c,t,ped,pt->rms[t]) ;
554 if(outf != stdout) fclose(outf) ;