8 #include <TPX/tpx_altro_to_pad.h>
18 #define QA_TB_START 147
19 #define QA_TB_STOP 153
21 #define QA_CHARGE 640.0
23 static const int fee_check[] = {
45 static const int fee_check_count =
sizeof(fee_check)/
sizeof(fee_check[0]) ;
47 static FILE *stripes_f ;
49 void tpxStat::run_start(u_int rb_mask,
int r_type)
54 memset(r,0,
sizeof(r)) ;
58 if(run_type != RUN_TYPE_PED) {
62 if((run_type == RUN_TYPE_DAQCHECK) && (rb_mask==4)) {
64 memset(fee_check_altros,0,
sizeof(fee_check_altros)) ;
65 memset(fee_check_data,0,
sizeof(fee_check_data)) ;
67 else fee_check_on = 0 ;
69 for(
int i=0;i<6;i++) {
70 if(rb_mask & (1<<i)) {
78 for(
int j=0;j<fee_check_count;j++) {
80 a = (fee_check[j]<<1) & 0xFF ;
82 fee_check_altros[a] = 1 ;
83 fee_check_altros[a|1] = 1 ;
86 r[i].a[a|1].should = 1 ;
89 r[i].a[a].c[c].min_adc = 0xFFFF ;
90 r[i].a[a|1].c[c].min_adc = 0xFFFF ;
92 fee_check_data[j*2][c].aid = a ;
93 fee_check_data[j*2+1][c].aid = a|1 ;
100 tpx36_to_real(sector,i+1,s_real,r_real) ;
107 tpx_from_altro(r_real,a,c,row,pad) ;
110 r[i].a[a].should = 1 ;
114 r[i].a[a].c[c].min_adc = 0xFFFF ;
125 int tpxStat::run_stop(FILE *ofile, u_int rb_mask,
int run_type,
char *fname)
132 if(ofile==0)
return 0 ;
134 if(stripes_f) fclose(stripes_f) ;
137 if(stripes && (run_type != RUN_TYPE_PED)) {
138 LOG(WARN,
"saw %d occurences of more than 400 timebins",stripes) ;
139 for(
int i=0;i<6;i++) {
142 if(rb_mask & (1<<i)) ;
145 tpx36_to_real(sector,i+1,s_real,r_real) ;
148 for(
int a=0;a<256;a++) {
149 for(
int c=0;c<16;c++) {
150 if(r[i].a[a].c[c].stripes) {
155 if(str_cou > 4) continue ;
157 tpx_from_altro(r_real,a,c,row,pad) ;
158 LOG(WARN,
"Stripes %d/%d: RDO %d: AID %3d:%02d, r:p %2d:%03d",
159 r[i].a[a].c[c].stripes,r[i].a[a].c[c].count,i+1,a,c,row,pad) ;
162 LOG(WARN,
"Stripes logging stopped, stripes %d",stripes) ;
170 for(
int i=0;i<6;i++) {
178 if(rb_mask & (1<<i)) should = 1 ;
183 LOG(WARN,
"[RDO %d] had %d ALTRO errors",i+1,r[i].errs) ;
186 if(r[i].should != should) {
188 fprintf(ofile,
"%sERROR: [RDO %d] in_RC %d, in_data %d -- MISMATCH\n%s",ANSI_RED,i+1,should,r[i].should,ANSI_RESET) ;
189 LOG(ERR,
"[RDO %d] in_RC %d, in_data %d -- MISMATCH",i+1,should,r[i].should) ;
196 fprintf(ofile,
"%sERROR: [RDO %d] expect data, found none!\n%s",ANSI_RED,i+1,ANSI_RESET) ;
197 LOG(WARN,
"[RDO %d] expect data, found none!",i+1) ;
204 fprintf(ofile,
"%sERROR: [RDO %d] expect none, found events %u\n%s",ANSI_RED,i+1,r[i].count,ANSI_RESET) ;
205 LOG(ERR,
"[RDO %d] expect none, found events %u",i+1,r[i].count) ;
212 tpx36_to_real(sector,i+1,s_real,r_real) ;
224 have = r[i].a[a].c[c].count ;
225 if(r[i].a[a].should) {
226 expect = r[i].count ;
235 tpx_from_altro(r_real,a,c,row,pad) ;
237 for(
int j=0;j<tpx_odd_fee_count;j++) {
239 if((tpx_odd_fee[j].sector != s_real) || (tpx_odd_fee[j].rdo != (r_real+1))) continue ;
241 for(
int k=0;k<2;k++) {
242 int aid = tpx_odd_fee[j].altro_id_padplane ;
247 LOG(WARN,
"[RDO %d] altro %3d was marked odd in the gain file...",i+1,a) ;
258 LOG(WARN,
"[RDO %d] AID %3d:%2d (r:p %d:%d): expect %d counts, have %d",i+1,a,c,
266 if(run_type == RUN_TYPE_PED) {
267 if(r[i].a[a].c[c].min_adc < 10) ped_warn = 1 ;
268 if(r[i].a[a].c[c].max_adc > 200) ped_warn = 1 ;
272 LOG(WARN,
"[RDO %d] AID %3d:%2d (r:p %d:%d) pedestal: min adc %3d, max adx %4d",
274 r[i].a[a].c[c].min_adc,
275 r[i].a[a].c[c].max_adc) ;
284 if(p_err) fprintf(ofile,
"\t%sERROR: ",ANSI_RED) ;
285 else fprintf(ofile,
"\t%sWARN: ",ANSI_CYAN) ;
288 fprintf(ofile,
"AID %3d:%2d: expect %d counts, have %d; min %4u, max %4u\n%s",a,c,expect,have,
289 r[i].a[a].c[c].min_adc,
290 r[i].a[a].c[c].max_adc,ANSI_RESET) ;
294 if(r[i].a[a].should && (a_err==0)) {
296 fprintf(ofile,
"\tAID %3d OK...\n",a) ;
306 FILE *f = fopen(fname,
"w") ;
308 for(
int i=0;i<fee_check_count;i++) {
309 int fee = fee_check[i] ;
311 for(
int j=0;j<2;j++) {
315 for(
int ch=0;ch<16;ch++) {
321 double cou = fee_check_data[ix][ch].count ;
322 int aid = fee_check_data[ix][ch].aid ;
327 tpx_from_altro(2,aid,ch,row,pad) ;
329 j1 = tpx_altro_to_j1[aid&1][ch] ;
332 LOG(DBG,
" ALTRO %3d:%02d: %4d counts",aid,ch,(
int)fee_check_data[ix][ch].count) ;
333 fprintf(f,
"%2d %d %3d %2d %3d %2d %2d %3d %.3f %6.3f %d %s\n",24,3,fee,j1,aid,ch,row,pad,
334 0.0,0.0,3,
"No data") ;
339 fee_check_data[ix][ch].ped /= cou ;
340 fee_check_data[ix][ch].rms /= cou ;
341 fee_check_data[ix][ch].charge /= cou ;
342 fee_check_data[ix][ch].t0 /= cou ;
344 fee_check_data[ix][ch].ped_sigma /= cou ;
345 fee_check_data[ix][ch].rms_sigma /= cou ;
346 fee_check_data[ix][ch].charge_sigma /= cou ;
347 fee_check_data[ix][ch].t0_sigma /= cou ;
349 fee_check_data[ix][ch].ped_sigma = sqrt(fee_check_data[ix][ch].ped_sigma - fee_check_data[ix][ch].ped*fee_check_data[ix][ch].ped) ;
350 fee_check_data[ix][ch].rms_sigma = sqrt(fee_check_data[ix][ch].rms_sigma - fee_check_data[ix][ch].rms*fee_check_data[ix][ch].rms) ;
351 fee_check_data[ix][ch].charge_sigma = sqrt(fee_check_data[ix][ch].charge_sigma - fee_check_data[ix][ch].charge*fee_check_data[ix][ch].charge) ;
352 fee_check_data[ix][ch].t0_sigma = sqrt(fee_check_data[ix][ch].t0_sigma - fee_check_data[ix][ch].t0*fee_check_data[ix][ch].t0) ;
355 LOG(TERR,
" A %3d:%02d: %4d cou: ped %.2f +- %.2f, rms %.2f +- %.2f, cha %.2f +- %.2f, t0 %.2f +- %.2f",aid,ch,(
int)cou,
356 fee_check_data[ix][ch].ped, fee_check_data[ix][ch].ped_sigma,
357 fee_check_data[ix][ch].rms, fee_check_data[ix][ch].rms_sigma,
358 fee_check_data[ix][ch].charge, fee_check_data[ix][ch].charge_sigma,
359 fee_check_data[ix][ch].t0, fee_check_data[ix][ch].t0_sigma) ;
361 double gain = fee_check_data[ix][ch].charge / QA_CHARGE ;
362 if((gain<0.9) || (gain>1.1)) {
363 strcat(errstr,
"Bad gain,") ;
367 double tmp = fee_check_data[ix][ch].ped ;
368 if((tmp > 100.0) || (tmp<5.0)) {
369 strcat(errstr,
"Bad pedestal,") ;
373 tmp = fee_check_data[ix][ch].rms ;
374 if((tmp < 0.4) || (tmp>1.2)) {
375 strcat(errstr,
"Noisy,") ;
379 if(r[2].count != cou) {
380 strcat(errstr,
"Flaky readout,") ;
384 fprintf(f,
"%2d %d %3d %2d %3d %2d %2d %3d %.3f %6.3f %d %s\n",24,3,fee,j1,aid,ch,row,pad,
386 fee_check_data[ix][ch].t0,
400 void tpxStat::accum(
char *rdobuff,
int bytes)
407 const u_int MAX_ERRORS = 10 ;
411 t = tpx_get_start(rdobuff, bytes/4, &rdo, 0) ;
415 r0_logical = tpx36_from_real(sector, rdo.sector, rdo.rdo) ;
425 LOG(ERR,
"rdo error: %d",rdo.rdo) ;
429 a.what = TPX_ALTRO_DO_ADC ;
430 a.rdo = rdo.rdo - 1 ;
432 a.sector = rdo.sector ;
436 if(r[r0_logical].errs >= MAX_ERRORS) {
443 r[r0_logical].count++ ;
445 data_end = rdo.data_end ;
451 data_end = tpx_scan_to_next(data_end, rdo.data_start, &a) ;
453 if(a.err || (data_end==0)) {
459 if((a.rdo >= 6) || (a.ch >= 16)) {
460 LOG(ERR,
"What !? %d %d",a.rdo,a.ch) ;
467 r[r0_logical].a[a.id].c[a.ch].stripes++ ;
468 if((stripes % 100)==0) {
469 LOG(NOTE,
"A lot of stripes: %d",stripes) ;
474 fprintf(stripes_f,
"==> RDO %d, cou %d: %d %d\n",a.rdo,r[r0_logical].count,a.row,a.pad) ;
475 for(
int i=0;i<a.count;i++) {
476 fprintf(stripes_f,
"%d %d\n",a.tb[i],a.adc[i]) ;
486 if((data_end==0) || (a.ch>15) || (a.rdo >= 6)) {
487 LOG(ERR,
"Bad boy: rdo %d, AID %d:%d -- %p",a.rdo,a.id,a.ch,data_end) ;
491 r[r0_logical].a[a.id].c[a.ch].count++ ;
494 if(run_type == RUN_TYPE_PED) {
495 for(
int i=0;i<a.count;i++) {
496 if(a.adc[i] > r[r0_logical].a[a.id].c[a.ch].max_adc) {
497 r[r0_logical].a[a.id].c[a.ch].max_adc = a.adc[i] ;
499 if(a.adc[i] < r[r0_logical].a[a.id].c[a.ch].min_adc) {
500 r[r0_logical].a[a.id].c[a.ch].min_adc = a.adc[i] ;
509 for(
int i=0;i<fee_check_count*2;i++) {
511 a0 = fee_check_data[i][0].aid ;
519 LOG(WARN,
"RDO %d: spurious ALTRO %3d",rdo.rdo,a.id) ;
523 fee_check_data[a_ok][a.ch].count++ ;
525 double ped, rms, cou ;
526 ped = rms = cou = 0.0 ;
527 for(
int i=0;i<a.count;i++) {
530 rms += a.adc[i] * a.adc[i];
535 if(cou == 0.0) cou = 1.0 ;
539 rms = sqrt(rms - ped*ped) ;
541 fee_check_data[a_ok][a.ch].ped += ped ;
542 fee_check_data[a_ok][a.ch].ped_sigma += ped*ped ;
544 fee_check_data[a_ok][a.ch].rms += rms ;
545 fee_check_data[a_ok][a.ch].rms_sigma += rms*rms ;
549 for(
int i=0;i<a.count;i++) {
550 if((a.tb[i] >= QA_TB_START) && (a.tb[i] <= QA_TB_STOP)) {
551 double d = a.adc[i] - ped ;
558 fee_check_data[a_ok][a.ch].charge += d_sum ;
559 fee_check_data[a_ok][a.ch].charge_sigma += d_sum*d_sum ;
561 if(d_sum == 0.0) d_t0 = 100.0 ;
564 fee_check_data[a_ok][a.ch].t0 += d_t0 ;
565 fee_check_data[a_ok][a.ch].t0_sigma += d_t0*d_t0 ;
572 if(data_end == rdo.data_start) {
573 LOG(WARN,
"Data end == data start...") ;
576 }
while(data_end && (data_end > rdo.data_start)) ;
581 r[r0_logical].errs++ ;
582 if(r[r0_logical].errs == MAX_ERRORS) {
583 LOG(WARN,
"RDO %d has %d errors -- stopping logging",r0_logical+1,r[r0_logical].errs) ;