21 memset(evts,0,
sizeof(evts)) ;
22 memset(valid_evts,0,
sizeof(valid_evts)) ;
24 sizeof_ped =
sizeof(
struct peds) * 6 ;
37 if(ped_store) free(ped_store) ;
43 void bsmdPed::init(
int active_rbs)
47 memset(evts,0,
sizeof(evts)) ;
48 memset(valid_evts,0,
sizeof(valid_evts)) ;
50 rb_mask = active_rbs ;
53 ped_store = (
struct peds *) malloc(sizeof_ped) ;
56 memset(ped_store,0,sizeof_ped) ;
58 LOG(TERR,
"Pedestals zapped: sector %2d, rb_mask 0x%02X.", sector, rb_mask) ;
62 int bsmdPed::do_zs(
char *src,
int in_bytes,
char *dst,
int rdo1, u_int *adc_sum)
64 u_short *d_out = (u_short *) dst ;
68 u_int *d32 = (u_int *) src ;
69 double sum_adc = 0.0 ;
71 LOG(NOTE,
"BSMD ZS: rdo %d: in bytes %d",rdo1,in_bytes) ;
73 d = (u_short *)(src + 10*4) ;
75 cap = (u_short) (d32[8] & 0x7F) ;
77 fiber = (sector-1)*2 + (rdo1 - 1) ;
80 u_short *count = d_out++ ;
81 *d_out++ = d32[8] & 0xFFFF ;
84 u_short *tmp = d_out ;
86 double *ped = (ped_store + rdo1 - 1)->ped[cap] ;
87 u_short *thr = (ped_store + rdo1 - 1)->thr[cap] ;
89 for(
int ii=0;ii<4800;ii++) {
93 sum_adc += (double)dta - (*ped) ;
96 *d_out++ = dta - (int)(*ped + 0.5) ;
102 *count = (d_out - tmp)/2 ;
104 int out_bytes = (
char *)d_out - dst ;
106 if(out_bytes > in_bytes) {
107 valid_evts[rdo1-1]++ ;
108 LOG(NOTE,
"BSMD ZS: rdo %d: in bytes %d, out bytes %d",rdo1,in_bytes,out_bytes) ;
111 LOG(NOTE,
"BSMD ZS: rdo %d: in bytes %d, out bytes %d",rdo1,in_bytes,out_bytes) ;
117 *adc_sum = (u_int) (sum_adc + 0.5) ;
128 void bsmdPed::accum(
char *evbuff,
int bytes,
int rdo1)
135 d_in = (u_short *) evbuff ;
136 d32 = (u_int *) evbuff ;
139 cap = d32[8] & 0x7F ;
147 LOG(NOTE,
"RDO %d: skipping event %d < 3",rdo,evts[rdo]) ;
155 LOG(NOTE,
"RDO %d: event %d",rdo,evts[rdo]) ;
157 struct peds *p = ped_store + rdo ;
159 if(p->cou[cap] > 0xFFF0) return ;
163 d_in = (u_short *)(evbuff + 10*4) ;
165 for(
int j=0;j<4800;j++) {
169 p->ped[cap][j] += (double) adc ;
170 p->rms[cap][j] += (double) (adc*adc) ;
180 void bsmdPed::do_thresh(
double thr_sm,
double thr_pre)
184 if(!ped_store || !valid) {
185 LOG(ERR,
"bsmd:do_thresh invalid") ;
190 for(
int r=0;r<6;r++) {
191 if((sector==2) && (r>2)) n_sigma = thr_pre ;
192 else n_sigma = thr_sm ;
194 struct peds *p = ped_store + r ;
195 for(
int c=0;c<128;c++) {
196 for(
int t=0;t<4800;t++) {
197 p->thr[c][t] = (u_short) (p->ped[c][t] + p->rms[c][t] * n_sigma + 0.5) ;
210 const u_int MIN_EVENTS = 20 ;
213 LOG(NOTE,
"Calculating pedestals for sector %2d",sector) ;
217 if(rb_mask & (1<<r)) ;
220 struct peds *ped = ped_store + r ;
222 for(cap=0;cap<128;cap++) {
223 for(ch=0;ch<4800;ch++) {
225 if(ped->cou[cap] == 0) {
226 ped->ped[cap][ch] = 0xFFFF ;
227 ped->rms[cap][ch] = 9.999 ;
232 pp = ped->ped[cap][ch] / (double) ped->cou[cap] ;
233 rr = ped->rms[cap][ch] / (
double) ped->cou[cap] ;
236 if(rr < (pp*pp)) rr = 0.0 ;
237 else rr = sqrt(rr - pp*pp) ;
239 ped->ped[cap][ch] = pp ;
240 ped->rms[cap][ch] = rr ;
251 if(rb_mask & (1<<r)) ;
254 struct peds *ped = ped_store + r ;
256 for(cap=0;cap<128;cap++) {
257 if(ped->cou[cap] < MIN_EVENTS) {
261 LOG(WARN,
"RDO %d: cap %3d: only %d events!",r+1,cap,ped->cou[cap]) ;
264 LOG(WARN,
"Stopping detailed bad cap logging...") ;
267 if(ped->cou[cap] == 0) real_bad++ ;
273 LOG(TERR,
"Pedestals calculated. RDO counts: %u %u %u %u %u %u",valid_evts[0],valid_evts[1],valid_evts[2],valid_evts[3],valid_evts[4],valid_evts[5]) ;
281 LOG(ERR,
"BSMD pedestals not good (%d caps not good, %d missing)",bad,real_bad) ;
283 LOG(WARN,
"But since no real bad I will allow it!") ;
292 int bsmdPed::to_evb(
char *buff)
297 u_short *dta = (u_short *) buff ;
303 LOG(WARN,
"ped::to_evb peds are bad: valid %d",valid) ;
306 LOG(NOTE,
"Preparing pedestals for later EVB...") ;
309 struct peds *ped = ped_store + r ;
311 fiber = (sector-1)*6 + r ;
319 for(t=0;t<4800;t++) {
323 rr = (u_int)(ped->rms[p][t] * 8.0 + 0.5) ;
324 if(rr > 0x3F) rr = 0x3F ;
327 pp = (u_int)(ped->ped[p][t] + 0.5) ;
328 if(pp > 0x3FF) pp = 0x3FF ;
330 *dta++ = (rr<<10)|pp ;
338 LOG(TERR,
"Pedestals prepared for later EVB, sector %2d: %d bytes",sector,(
char *)dta-buff) ;
339 return ((
char *)dta-buff) ;
342 int bsmdPed::from_cache(
const char *fname)
352 f = fopen(fname,
"r") ;
355 fn =
"/RTScache/pedestals.txt" ;
360 LOG(ERR,
"ped::from_cache can't open input file \"%s\" [%s]",fn,strerror(errno)) ;
365 LOG(NOTE,
"Loading pedestals from cache \"%s\"...",fn) ;
372 if(fgets(buff,
sizeof(buff),f)==0) continue ;
374 if(buff[0]==
'#' || buff[0]==
'/') continue ;
376 int ret = sscanf(buff,
"%d %d %d %f %f",&r,&p,&t,&pp,&rr) ;
377 if(ret != 5) continue ;
379 struct peds *peds = ped_store + (r-1) ;
381 peds->ped[p][t] = pp ;
382 peds->rms[p][t] = rr ;
386 LOG(TERR,
"Pedestals loaded from cache \"%s\": sector %2d.",fn,sector) ;
394 int bsmdPed::to_cache(
const char *fname, u_int run)
402 LOG(ERR,
"ped::to_cache peds are bad: valid %d -- not caching",valid) ;
410 fn =
"/RTScache/pedestals.txt" ;
416 LOG(ERR,
"ped::to_cache can't open output file \"%s\" [%s]",fn,strerror(errno)) ;
421 LOG(NOTE,
"Writing pedestals to cache \"%s\"...",fn) ;
422 time_t tim = time(0) ;
423 fprintf(f,
"# Detector %s\n",
"BSMD") ;
424 fprintf(f,
"# Sector %2d\n",sector) ;
425 fprintf(f,
"# Run %08u\n",run) ;
426 fprintf(f,
"# Date %s",ctime(&tim)) ;
430 struct peds *peds = ped_store + r ;
433 for(t=0;t<4800;t++) {
434 fprintf(f,
"%d %d %d %8.3f %.3f\n",r+1,p,t,peds->ped[p][t],peds->rms[p][t]) ;
441 LOG(TERR,
"Pedestals written to cache \"%s\", for sector %2d...",fn,sector) ;
446 int bsmdPed::special_setup(
int run_type,
int sub_type)
452 case RUN_TYPE_PULSER_A :
453 case RUN_TYPE_PED_A :
454 case RUN_TYPE_PED_B :
455 LOG(WARN,
"Special Pedestal setup: %d, %d",run_type, sub_type) ;
462 struct peds *ped = ped_store + r ;
466 case RUN_TYPE_PULSER_A :
467 for(t=100;t<110;t++) ped->ped[p][t] = 0.0 ;
468 for(t=400;t<415;t++) ped->ped[p][t] = 0.0 ;
470 case RUN_TYPE_PED_A :
473 for(
int i=0;i<16;i++) {
474 ped->ped[p][t+i] = m * 1023.0 ;
481 case RUN_TYPE_PED_B :
484 for(
int i=0;i<16;i++) {
485 ped->ped[p][t+i] = m * 1023.0 ;
493 for(t=0;t<512;t++) ped->ped[p][t] = 1023.0 ;
494 for(t=p;t<(p+10);t++) ped->ped[p][t] = 0 ;