StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tpxStat.cxx
1 #include <math.h>
2 #include <string.h>
3 #include <stdio.h>
4 
5 #include <rtsLog.h>
6 #include <daqModes.h>
7 
8 #include <TPX/tpx_altro_to_pad.h>
9 
10 #include "tpxCore.h"
11 #include "tpxStat.h"
12 
13 
14 //#include <thread_dbg.h>
15 #define TLOG()
16 
17 
18 #define QA_TB_START 147
19 #define QA_TB_STOP 153
20 
21 #define QA_CHARGE 640.0
22 
23 static const int fee_check[] = {
24 // 100,
25 // 101,
26 // 102,
27 // 103,
28 // 104,
29  107,
30  108,
31  109,
32  110,
33  111,
34  114,
35  115,
36  116,
37  117,
38  118,
39 // 121,
40 // 122,
41 // 123,
42 // 124
43 } ;
44 
45 static const int fee_check_count = sizeof(fee_check)/sizeof(fee_check[0]) ;
46 
47 static FILE *stripes_f ;
48 
49 void tpxStat::run_start(u_int rb_mask, int r_type)
50 {
51 
52  run_type = r_type ;
53 
54  memset(r,0,sizeof(r)) ;
55  stripes = 0 ;
56 
57  stripes_f = 0 ;
58  if(run_type != RUN_TYPE_PED) {
59 // stripes_f = fopen("/RTScache/stripes.txt","w") ;
60  }
61 
62  if((run_type == RUN_TYPE_DAQCHECK) && (rb_mask==4)) { // RDO #3
63  fee_check_on = 1 ;
64  memset(fee_check_altros,0,sizeof(fee_check_altros)) ;
65  memset(fee_check_data,0,sizeof(fee_check_data)) ;
66  }
67  else fee_check_on = 0 ;
68 
69  for(int i=0;i<6;i++) {
70  if(rb_mask & (1<<i)) {
71  int a, c ;
72 
73 
74  r[i].should = 1 ;
75 
76  if(fee_check_on) {
77  // figure out which altros need to be present
78  for(int j=0;j<fee_check_count;j++) {
79 
80  a = (fee_check[j]<<1) & 0xFF ;
81 
82  fee_check_altros[a] = 1 ;
83  fee_check_altros[a|1] = 1 ;
84 
85  r[i].a[a].should = 1;
86  r[i].a[a|1].should = 1 ;
87 
88  for(c=0;c<16;c++) {
89  r[i].a[a].c[c].min_adc = 0xFFFF ;
90  r[i].a[a|1].c[c].min_adc = 0xFFFF ;
91 
92  fee_check_data[j*2][c].aid = a ;
93  fee_check_data[j*2+1][c].aid = a|1 ;
94  }
95 
96  }
97  }
98  else {
99  int s_real, r_real ;
100  tpx36_to_real(sector,i+1,s_real,r_real) ;
101  r_real-- ;
102 
103  for(a=0;a<256;a++) {
104  for(c=0;c<16;c++) {
105  int row, pad ;
106 
107  tpx_from_altro(r_real,a,c,row,pad) ;
108 
109  if(row <= 45) { // expect him!
110  r[i].a[a].should = 1 ;
111 
112 
113  }
114  r[i].a[a].c[c].min_adc = 0xFFFF ;
115  }
116  }
117 
118  }
119  }
120  }
121 
122  return ;
123 }
124 
125 int tpxStat::run_stop(FILE *ofile, u_int rb_mask, int run_type, char *fname)
126 {
127 
128  int should ;
129  int err = 0 ;
130  int str_cou = 0 ;
131 
132  if(ofile==0) return 0 ;
133 
134  if(stripes_f) fclose(stripes_f) ;
135  stripes_f = 0 ;
136 
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++) {
140  int s_real, r_real ;
141 
142  if(rb_mask & (1<<i)) ;
143  else continue ;
144 
145  tpx36_to_real(sector,i+1,s_real,r_real) ;
146  r_real-- ;
147 
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) {
151  int row, pad ;
152 
153  str_cou++ ;
154 
155  if(str_cou > 4) continue ;
156 
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) ;
160 
161  if(str_cou == 4) {
162  LOG(WARN,"Stripes logging stopped, stripes %d",stripes) ;
163  }
164  }
165  }
166  }
167  }
168  }
169 
170  for(int i=0;i<6;i++) {
171 
172 
173 
174  int a, c ;
175  int s_real, r_real ;
176 
177 
178  if(rb_mask & (1<<i)) should = 1 ;
179  else should = 0 ;
180 
181 
182  if(r[i].errs) {
183  LOG(WARN,"[RDO %d] had %d ALTRO errors",i+1,r[i].errs) ;
184  }
185 
186  if(r[i].should != should) {
187  err++ ;
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) ;
190  }
191 
192 
193  if(r[i].count==0) {
194  if(r[i].should) {
195  err++ ;
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) ;
198  }
199  continue ;
200  }
201  else {
202  if(!r[i].should) {
203  err++ ;
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) ;
206  }
207  else {
208 // fprintf(ofile,"----- RDO %d: found events %u\n",i+1,r[i].count) ;
209  }
210  }
211 
212  tpx36_to_real(sector,i+1,s_real,r_real) ;
213  r_real-- ; // start from 0 ;
214 
215 
216  for(a=0;a<256;a++) {
217  int a_err = 0 ;
218  for(c=0;c<16;c++) {
219  u_int have, expect ;
220 
221  int warn = 0 ;
222  int p_err = 0 ;
223 
224  have = r[i].a[a].c[c].count ;
225  if(r[i].a[a].should) {
226  expect = r[i].count ;
227  }
228  else {
229  expect = 0 ;
230  }
231 
232  int row ;
233  int pad ;
234 
235  tpx_from_altro(r_real,a,c,row,pad) ;
236 
237  for(int j=0;j<tpx_odd_fee_count;j++) {
238 
239  if((tpx_odd_fee[j].sector != s_real) || (tpx_odd_fee[j].rdo != (r_real+1))) continue ;
240 
241  for(int k=0;k<2;k++) {
242  int aid = tpx_odd_fee[j].altro_id_padplane ;
243 
244  if(a == (aid+k)) {
245  expect = 0 ;
246  if(c==0) {
247  LOG(WARN,"[RDO %d] altro %3d was marked odd in the gain file...",i+1,a) ;
248  }
249  }
250  }
251  }
252 
253  if(have != expect) {
254  warn = 1 ;
255  a_err = 1 ;
256  p_err = 1 ;
257 
258  LOG(WARN,"[RDO %d] AID %3d:%2d (r:p %d:%d): expect %d counts, have %d",i+1,a,c,
259  row,pad,
260  expect,have) ;
261  }
262 
263 
264  if(have && expect) {
265  int ped_warn = 0 ;
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 ;
269  }
270 
271  if(ped_warn) {
272  LOG(WARN,"[RDO %d] AID %3d:%2d (r:p %d:%d) pedestal: min adc %3d, max adx %4d",
273  i+1,a,c,row,pad,
274  r[i].a[a].c[c].min_adc,
275  r[i].a[a].c[c].max_adc) ;
276  warn = 1 ;
277  }
278 
279 
280  }
281 
282 
283  if(warn) {
284  if(p_err) fprintf(ofile,"\t%sERROR: ",ANSI_RED) ;
285  else fprintf(ofile,"\t%sWARN: ",ANSI_CYAN) ;
286 
287  err++ ;
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) ;
291  }
292 
293  }
294  if(r[i].a[a].should && (a_err==0)) {
295 #ifdef TEST_RDO
296  fprintf(ofile,"\tAID %3d OK...\n",a) ;
297 #endif
298  }
299  }
300 
301 }
302 
303  fflush(ofile) ;
304 
305  if(fee_check_on) {
306  FILE *f = fopen(fname,"w") ;
307 
308  for(int i=0;i<fee_check_count;i++) {
309  int fee = fee_check[i] ;
310 
311  for(int j=0;j<2;j++) {
312  int ix = 2*i + j ;
313 
314 
315  for(int ch=0;ch<16;ch++) {
316  int err ;
317  char errstr[1024] ;
318  errstr[0] = 0 ;
319  err = 0 ;
320 
321  double cou = fee_check_data[ix][ch].count ;
322  int aid = fee_check_data[ix][ch].aid ;
323  int row, pad ;
324 
325  u_char j1 ;
326 
327  tpx_from_altro(2,aid,ch,row,pad) ;
328 
329  j1 = tpx_altro_to_j1[aid&1][ch] ;
330 
331  if(cou == 0.0) {
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") ;
335 
336  continue ;
337  }
338 
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 ;
343 
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 ;
348 
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) ;
353 
354 
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) ;
360 
361  double gain = fee_check_data[ix][ch].charge / QA_CHARGE ;
362  if((gain<0.9) || (gain>1.1)) {
363  strcat(errstr,"Bad gain,") ;
364  err = 1 ;
365  }
366 
367  double tmp = fee_check_data[ix][ch].ped ;
368  if((tmp > 100.0) || (tmp<5.0)) {
369  strcat(errstr,"Bad pedestal,") ;
370  err = 2 ;
371  }
372 
373  tmp = fee_check_data[ix][ch].rms ;
374  if((tmp < 0.4) || (tmp>1.2)) {
375  strcat(errstr,"Noisy,") ;
376  err = 2 ;
377  }
378 
379  if(r[2].count != cou) {
380  strcat(errstr,"Flaky readout,") ;
381  err = 3 ;
382  }
383 
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,
385  gain,
386  fee_check_data[ix][ch].t0,
387  err,errstr) ;
388 
389 
390  }
391  }
392  }
393 
394  fclose(f) ;
395  }
396 
397  return err ;
398 }
399 
400 void tpxStat::accum(char *rdobuff, int bytes)
401 {
402  int t ;
403  u_int *data_end ;
404  tpx_rdo_event rdo ;
405  tpx_altro_struct a ;
406  int errors = 0 ;
407  const u_int MAX_ERRORS = 10 ;
408  int r0_logical ;
409 
410  TLOG() ;
411  t = tpx_get_start(rdobuff, bytes/4, &rdo, 0) ;
412 
413  if(t <= 0) return ; // non data event or some error
414 
415  r0_logical = tpx36_from_real(sector, rdo.sector, rdo.rdo) ;
416  r0_logical-- ; // so it starts from 0
417 
418 // if(sector && (rdo.sector != sector)) {
419 // LOG(ERR,"sector mismatch: expect %d, in data %d",sector,rdo.sector) ;
420 // return ;
421 // }
422 
423 
424  if(rdo.rdo > 6) {
425  LOG(ERR,"rdo error: %d",rdo.rdo) ;
426  return ;
427  }
428 
429  a.what = TPX_ALTRO_DO_ADC ; // I need this for the stripes count
430  a.rdo = rdo.rdo - 1 ; // a.rdo counts from 0
431  a.t = t ;
432  a.sector = rdo.sector ;
433  a.ch = 0 ; // can't rely
434 
435 
436  if(r[r0_logical].errs >= MAX_ERRORS) {
437  a.log_err = 0 ;
438  }
439  else {
440  a.log_err = 1 ;
441  }
442 
443  r[r0_logical].count++ ;
444 
445  data_end = rdo.data_end ;
446 
447  TLOG() ;
448 
449  do {
450  TLOG() ;
451  data_end = tpx_scan_to_next(data_end, rdo.data_start, &a) ;
452  TLOG() ;
453  if(a.err || (data_end==0)) {
454  //LOG(ERR,"Got err %d:%d, log error = %d: data_end %p",a.id,a.ch,a.log_err,data_end) ;
455  errors = 1 ;
456  }
457  else {
458  TLOG() ;
459  if((a.rdo >= 6) || (a.ch >= 16)) {
460  LOG(ERR,"What !? %d %d",a.rdo,a.ch) ;
461  }
462  TLOG() ;
463  if(a.count >= 400) {
464  TLOG() ;
465  stripes++ ;
466  TLOG() ;
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) ;
470  }
471 
472  TLOG() ;
473  if(stripes_f) {
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]) ;
477  }
478  }
479 
480  }
481  TLOG() ;
482  }
483 
484  //LOG(TERR,"%d: AID %d:%d",data_end-rdo.data_start,a.id,a.ch) ;
485 
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) ;
488  continue ;
489  }
490 
491  r[r0_logical].a[a.id].c[a.ch].count++ ;
492 
493  TLOG() ;
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] ;
498  }
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] ;
501  }
502 
503  }
504  }
505 
506  if(fee_check_on) {
507 
508  int a_ok = -1 ;
509  for(int i=0;i<fee_check_count*2;i++) {
510  int a0 ;
511  a0 = fee_check_data[i][0].aid ;
512  if(a.id==a0) {
513  a_ok = i ;
514  break ;
515  }
516  }
517 
518  if(a_ok <0) {
519  LOG(WARN,"RDO %d: spurious ALTRO %3d",rdo.rdo,a.id) ;
520  continue ;
521  }
522 
523  fee_check_data[a_ok][a.ch].count++ ;
524 
525  double ped, rms, cou ;
526  ped = rms = cou = 0.0 ;
527  for(int i=0;i<a.count;i++) {
528  if(a.tb[i] < 100) {
529  ped += a.adc[i] ;
530  rms += a.adc[i] * a.adc[i];
531  cou++ ;
532  }
533  }
534 
535  if(cou == 0.0) cou = 1.0 ; // oh well...
536 
537  ped /= cou ;
538  rms /= cou ;
539  rms = sqrt(rms - ped*ped) ;
540 
541  fee_check_data[a_ok][a.ch].ped += ped ;
542  fee_check_data[a_ok][a.ch].ped_sigma += ped*ped ;
543 
544  fee_check_data[a_ok][a.ch].rms += rms ;
545  fee_check_data[a_ok][a.ch].rms_sigma += rms*rms ;
546 
547  double d_sum, d_t0 ;
548  d_sum = d_t0 = 0.0 ;
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 ;
552 
553  d_sum += d ;
554  d_t0 += d*a.tb[i] ;
555  }
556  }
557 
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 ;
560 
561  if(d_sum == 0.0) d_t0 = 100.0 ;
562  else d_t0 /= d_sum ;
563 
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 ;
566 
567  //LOG(TERR,"Got A %3d:%02d, count %d: ped %f, rms %f, charge %f, t0 %f",a.id,a.ch,a.count,ped,rms,d_sum,d_t0) ;
568 
569 
570  } // end of fee_check_on
571 
572  if(data_end == rdo.data_start) {
573  LOG(WARN,"Data end == data start...") ;
574  }
575 
576  } while(data_end && (data_end > rdo.data_start)) ;
577 
578  TLOG() ;
579 
580  if(errors) {
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) ;
584  }
585  } ;
586 
587  return ;
588 
589 }