11 #include <TPC/rowlen.h>
13 #include <TPX/tpx_altro_to_pad.h>
14 #include <SFS/sfs_index.h>
20 static const int MIN_EVENTS = 100 ;
22 #define STGC_PED_FILENAME "/RTScache/pedestals"
30 memset(evts,0,
sizeof(evts)) ;
31 memset(valid_evts,0,
sizeof(valid_evts)) ;
53 memset(peds,0,
sizeof(peds)) ;
58 void stgcPed::init(
int sec,
int active_rbs)
64 rb_mask = active_rbs ;
67 memset(evts,0,
sizeof(evts)) ;
68 memset(valid_evts,0,
sizeof(valid_evts)) ;
69 memset(altro_found,0,
sizeof(altro_found)) ;
75 void stgcPed::smooth()
77 LOG(TERR,
"Running smooth...") ;
79 LOG(TERR,
"I have %f",peds[2][0].ped[0]) ;
81 for(
int a=0;a<256;a++) {
82 for(
int c=0;c<16;c++) {
89 for(
int t=0;t<512;t++) {
90 double ped = peds[a][c].ped[t] ;
91 double p_rms = peds[a][c].rms[t] ;
93 if(a==2 && c==0) LOG(TERR,
"tb %d = %f %f",t,ped,p_rms) ;
95 if(ped==0.0 && p_rms==0.0) break ;
96 if(ped>1022 && p_rms>9) break ;
105 for(
int t=0;t<16;t++) {
106 double ped = peds[a][c].ped[t] ;
107 double p_rms = peds[a][c].rms[t] ;
111 if(ped==0.0 && p_rms==0.0) break ;
112 if(ped>1022 && p_rms>9) break ;
120 if(cou==0) continue ;
125 rms = sqrt(rms-sum*sum) ;
127 for(
int t=0;t<512;t++) {
128 peds[a][c].ped[t] = sum ;
129 peds[a][c].rms[t] = rms ;
132 LOG(TERR,
"A%d:%d - %d %d",a,c,(
int)cou,(
int)peds[a][c].ped[0]) ;
141 void stgcPed::accum(
char *evbuff,
int bytes)
148 t = tpx_get_start(evbuff, bytes/4, &rdo, 0) ;
152 a.what = TPX_ALTRO_DO_ADC ;
153 a.rdo = rdo.rdo - 1 ;
155 a.sector = rdo.sector ;
162 if(evts[a.rdo] <= 3) {
163 LOG(NOTE,
"RDO %d: skipping event %d < 3",rdo.rdo,evts[a.rdo]) ;
167 valid_evts[a.rdo]++ ;
170 data_end = rdo.data_end ;
173 data_end = tpx_scan_to_next(data_end, rdo.data_start, &a) ;
175 }
while(data_end && (data_end > rdo.data_start)) ;
189 for(
int i=0;i<aa->count;i++) {
195 peds[a][ch].ped[tb] += (double) adc ;
196 peds[a][ch].rms[tb] += (double) (adc*adc) ;
197 peds[a][ch].cou[tb]++ ;
209 LOG(NOTE,
"Calculating pedestals for sector %2d",sector) ;
213 for(
int a=0;a<256;a++) {
214 if(!altro_found[a]) continue ;
216 for(
int c=0;c<16;c++) {
217 for(
int t=0;t<512;t++) {
218 if(peds[a][c].cou[t] == 0) {
219 peds[a][c].ped[t] = 1023.0 ;
220 peds[a][c].rms[t] = 9.999 ;
225 pp = peds[a][c].ped[t] / (double) peds[a][c].cou[t] ;
226 rr = peds[a][c].rms[t] / (double) peds[a][c].cou[t] ;
229 if(rr < (pp*pp)) rr = 0.0 ;
230 else rr = sqrt(rr - pp*pp) ;
232 peds[a][c].ped[t] = pp ;
233 peds[a][c].rms[t] = rr ;
236 LOG(TERR,
"AID %d:%d = %f +- %f",a,c,peds[a][c].ped[15],peds[a][c].rms[15]) ;
241 for(
int r=0;r<6;r++) {
242 if(rb_mask & (1<<r)) {
243 if(valid_evts[r] < MIN_EVENTS) {
245 LOG(ERR,
"RDO %d: not enough valid events (%d < %d) [%d]",r+1,valid_evts[r],MIN_EVENTS,evts[r]) ;
255 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]) ;
258 LOG(ERR,
"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]) ;
265 int stgcPed::to_altro(
char *buff,
int rb,
int timebins)
273 LOG(ERR,
"ped::to_altro peds are bad: RDO %d: valid %d",rb+1,valid) ;
284 if(!altro_found[a]) continue ;
286 LOG(TERR,
"ALTRO %d: found %d",a,altro_found[a]) ;
288 for(ch=0;ch<16;ch++) {
291 u_int *addr = (u_int *) rbuff ;
295 u_short *ptr = (u_short *) rbuff ;
301 struct stgcPed::peds *ped = &(peds[a][ch]) ;
308 for(t=15;t<timebins+15;t++) {
309 *ptr++ = (u_short) ped->ped[t] ;
313 if((row==42)&&(pad==140)) {
322 for(;t<(TPX_MAX_TB+15);t++) {
323 u_short val = (u_short) 1023 ;
326 if((row==42)&&(pad==140)) {
339 u_short val = (u_short) ped->ped[t] ;
341 if((row==42)&&(pad==140)) {
349 u_short val = (u_short) ped->ped[0] ;
351 if((row==42)&&(pad==140)) {
362 LOG(WARN,
"tcou %d is odd, adding ped of tb %d?",tcou,t) ;
363 *ptr++ = (u_short) ped->ped[0];
369 *addr = (aid << 24) | (ch << 16) | tcou ;
377 LOG(NOTE,
"Pedestals prepared for RDO %d, bytes %d",rb+1,rbuff-buff) ;
378 return rbuff - buff ;
381 int stgcPed::to_evb(
char *buff)
386 int stgcPed::from_cache(
char *fname, u_int rb_msk)
397 pn =
"/RTScache/pedestals" ;
403 memset(peds,0,
sizeof(peds)) ;
404 memset(altro_found,0,
sizeof(altro_found)) ;
406 for(
int rdo=1;rdo<=6;rdo++) {
407 if(rb_mask & (1<<(rdo-1))) ;
410 sprintf(fn,
"%s_s%02d_r%d.txt",pn,sector,rdo) ;
414 LOG(ERR,
"ped::from_cache can't open output file \"%s\" [%s]",fn,strerror(errno)) ;
420 LOG(NOTE,
"Loading pedestals from cache \"%s\"...",fn) ;
429 if(fgets(buff,
sizeof(buff),f)==0) continue ;
437 int ret = sscanf(buff,
"%d %d %d %f %f",&r,&p,&t,&pp,&rr) ;
438 if(ret != 5) continue ;
445 LOG(ERR,
"WHA %d %d",r,p) ;
453 peds[r][p].ped[t] = pp ;
454 peds[r][p].rms[t] = rr ;
456 if(p==0 && t==20) LOG(TERR,
"S%d:%d: %f %f",r,p,pp,rr) ;
464 LOG(TERR,
"Pedestals loaded from cache (last was \"%s\"): sector %2d [0x%02X].",fn,sector,rb_mask) ;
468 LOG(ERR,
"Pedestals failed from cache (last was \"%s\"): sector %2d [0x%02X].",fn,sector,rb_mask) ;
478 int stgcPed::to_cache(
char *fname, u_int run)
487 LOG(ERR,
"ped::to_cache peds are bad: valid %d -- not caching",valid) ;
492 time_t tm = time(0) ;
493 asc_date = ctime(&tm) ;
499 pn =
"/RTScache/pedestals" ;
504 for(
int rdo=1;rdo<=6;rdo++) {
507 if(rb_mask & (1<<(rdo-1))) ;
512 if(valid_evts[rdo-1] < MIN_EVENTS) {
513 LOG(ERR,
"Sector %2d, RDO %d has %d events -- not caching!",sector,rdo,valid_evts[rdo-1]) ;
517 sprintf(fn,
"%s_s%02d_r%d.txt",pn,sector,rdo) ;
521 LOG(ERR,
"ped::to_cache can't open output file \"%s\" [%s]",fn,strerror(errno)) ;
526 LOG(NOTE,
"Writing pedestals to cache \"%s\"...",fn) ;
529 fprintf(f,
"# Detector %s\n",
"STGC") ;
530 fprintf(f,
"# Run %08u\n",run) ;
531 fprintf(f,
"# Date %s",asc_date) ;
532 fprintf(f,
"# Logical sector %d, logical RDO %d\n",sector,rdo) ;
535 for(
int a=0;a<256;a++) {
536 if(!altro_found[a]) continue ;
538 for(
int c=0;c<16;c++) {
540 for(
int t=0;t<512;t++) {
541 fprintf(f,
"%d %d %d %.3f %.3f\n",a,c,t,peds[a][c].ped[t],peds[a][c].rms[t]) ;
549 LOG(TERR,
"Pedestals written to cache \"%s\", for sector %2d...",fn,sector) ;
556 int stgcPed::kill_bad(
int rdo0,
int a,
int ch)
558 for(
int t=0;t<512;t++) {
559 peds[a][ch].ped[t] = 1023.0 ;
560 peds[a][ch].rms[t] = 9.999 ;