StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tpx23.cxx
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <time.h>
4 #include <stdlib.h>
5 #include <ctype.h>
6 #include <pthread.h>
7 #include <sys/time.h>
8 
9 #include <rtsLog.h>
10 
11 #include <rtsSystems.h>
12 
13 #include <DAQ1000/ddl_struct.h>
14 #include <DAQ1000/rdo_cmds.h>
15 
16 #include <TPC/rowlen.h>
17 #include <TPX/tpx_altro_to_pad.h>
18 
19 #include <DAQ_TPX/tpxCore.h>
20 #include <DAQ_TPX/tpxPed.h>
21 #include <DAQ_TPX/tpxGain.h>
22 
23 #include <DAQ_ITPC/itpcPed.h> // only for itpcData!
24 
25 #ifdef THREAD_DBG_USE
26 
27 #include <MISC_LIBS/thread_dbg.h>
28 
29 #else
30 
31 #define TLOG()
32 #define TLOGX(x)
33 
34 #endif
35 
36 #include "tpx23.h"
37 
38 
39 static double mark(void)
40 {
41  struct timeval tmval ;
42 
43  gettimeofday(&tmval,0) ;
44 
45  return ((double)tmval.tv_sec*1000000.0 + (double)tmval.tv_usec) ;
46 }
47 
48 static double delta(double v)
49 {
50  return mark() - v ;
51 }
52 
53 
54 tpxPed *tpx23::peds ;
55 tpc23_base::row_pad_t (*tpx23::rp_gain_tpx)[ROW_MAX+1][PAD_MAX+1] ;
56 
57 
58 int tpx23::from22to23(char *c_addr, int words)
59 {
60  // does NOTHING yet
61  return words ;
62 }
63 
64 inline u_int tpx23::set_rdo(int s, int r)
65 {
66 
67  sector1 = s ;
68  rdo1 = r ;
69 
70  return 0 ; // should be fee_mask
71 }
72 
73 int tpx23::fee_scan()
74 {
75  u_int *h ;
76  err = 0 ; // in class
77  int id_pre = -1 ;
78  int ch_pre = -1 ;
79  int s_cou ;
80  char retry ;
81 // double s_tmx = mark() ;
82 
83  get_token((char *)d_start,words) ;
84 
85  TLOG() ;
86 
87  if(run_type==1 || run_type==5) {
88  int r0_logical = tpx36_from_real(subdet_id,sector1,rdo1) - 1 ;
89 
90 
91  peds->valid_evts[r0_logical]++ ;
92  peds->evts[r0_logical]++ ;
93 
94  peds->sector = subdet_id ; // this is the PC id 1..36!
95 
96  if(peds->evts[r0_logical]<=3) goto done ; // skip first 3 events
97  if(peds->valid_evts[r0_logical]>1000) goto done ; // enough...
98 
99 
100  }
101 
102  // first valid FEE word is at d_start+2 ;
103  // last valid FEE word is at d_end
104  h = d_end ;
105 
106  TLOGX(rdo1) ;
107 
108  if(log_level>0) LOG(TERR,"%d: fee_scan",rdo1) ;
109 
110  u_int *h_to_continue ;
111  retry = 0 ;
112 
113 
114  f_stat.evt_cou++ ;
115 
116  // NOTE: ALTRO scans from the end!!!
117  while(h>(d_start+2)) {
118  u_int hi, lo ;
119 
120 // double tmx ;
121 // tmx=mark() ;
122 
123  lo = *h-- ;
124  hi = *h-- ;
125 
126  // for intermediate hdr version
127  lo &= 0xFFFFF ;
128  hi &= 0xFFFFF ;
129 
130  int wc = ((hi&0x3F)<<4)|((lo&0xF0000)>>16) ; // altro's word count
131 
132 
133  int id = (lo&0xFF0) >> 4 ; // altro id
134  int ch = lo & 0xF ;
135 
136  // sanity checks: 0xAAA & 0xA
137  u_int aaa = hi>>6 ;
138  u_int a = (lo>>12)&0xF ;
139 
140 
141  if((aaa!= 0x2AAA)||(a!=0xA)||(wc>437)) {
142  run_errors++ ;
143  if(run_errors<20) {
144  if((online || mode) && retry==0) {
145  LOG(ERR,"S%02d:%d: aid %d:%d, %d:%d: aaa 0x%X, a 0x%X, wc %d, %d",
146  sector1,rdo1,id,ch,id_pre,ch_pre,aaa,a,wc,d_end-h) ;
147  }
148  }
149  if(1) {
150  h++ ;
151  //LOG(ERR,"Retry aaa") ;
152  retry = 1 ;
153  continue ;
154  }
155  }
156 
157  if(wc==0) {
158  id_pre = id ;
159  ch_pre = ch ;
160  continue ;
161  }
162 
163 
164  TLOGX(id) ;
165 
166 
167 // if(mode & 3) { // debugging!
168  u_int aa = ((h[1]&0xFFFFF)>>10) ;
169 
170  //LOG(TERR,"aid %d:%d: 0x%X 0x%X",id,ch,h[0]&0xFFFFF,h[1]&0xFFFFF) ;
171 
172  if(aa != 0x2AA) {
173  run_errors++ ;
174  if(run_errors<20) {
175  if((online || mode) && retry==0) {
176  LOG(ERR,"S%02d:%d: aid %d:%d, %d:%d: aaa 0x%X, a 0x%X, aa 0x%X, wc %d",
177  sector1,rdo1,id,ch,id_pre,ch_pre,aaa,a,aa,wc) ;
178  }
179  }
180 
181  if(1) {
182  h++ ;
183  //LOG(ERR,"Retry aa") ;
184  retry = 1 ;
185  continue ;
186  }
187  }
188  else {
189  //LOG(WARN,"S%02d:%d: aid %d:%d, %d:%d: aaa 0x%X, a 0x%X, aa 0x%X, wc %d",
190  // sector1,rdo1,id,ch,id_pre,ch_pre,aaa,a,aa,wc) ;
191 
192 
193  }
194 
195 // }
196 
197  if(retry) {
198  if(online || mode) {
199  if(run_errors<20) {
200  LOG(WARN,"S%02d:%d: aid %d:%d, %d:%d: aaa 0x%X, a 0x%X, wc %d, %d -- OK",
201  sector1,rdo1,id,ch,id_pre,ch_pre,aaa,a,wc,d_end-h) ;
202  }
203  }
204  }
205 
206  retry = 0 ;
207 
208  h_to_continue = h ; // h+1
209 
210  for(int i=0;i<tpx_fee_override_cou;i++) {
211  if(sector1 == tpx_fee_override[i].sector) {
212  if(rdo1==tpx_fee_override[i].rdo) {
213  int fee = id & 0xFE ;
214 
215  if(fee == tpx_fee_override[i].curr_altro) {
216  int should = tpx_fee_override[i].orig_altro ;
217 
218  if(id & 1) should |= 1 ;
219 
220  //LOG(WARN,"S%02d:%d overriding ALTRO id %d with %d",sector1,rdo1,id,should) ;
221  id = should ;
222  break ;
223  }
224  }
225  }
226  }
227 
228 
229  // this now needs to go into the canonical format!
230  int row ;
231  int pad ;
232 
233  // get row,pad & flags and skip the pad if there are flags
234  int flags = flags_row_pad(id,ch,row,pad) ;
235 
236 #if 0
237  // max wc in pedestal runs is 437
238  if(wc>437) { // garbage in the event... and now what???
239  run_errors++ ;
240  if(run_errors<10) {
241  if(online) LOG(ERR,"S%02d:%d: rp %d:%d (aid %d:%d, %d:%d) : wc %d",sector1,rdo1,row,pad,
242  id,ch,id_pre,ch_pre, wc) ;
243  }
244  //err |= 0x10000 ; // signal an error because I am breaking out
245  break ;
246  }
247 #endif
248 
249  while(wc%4) wc++ ;
250 
251 
252 // f_stat.tm[0] += delta(tmx) ;
253 
254  // if this is a physics run: skip pads which have flags
255  // hmm... is this right?
256  if(flags && run_type==3) {
257  if(log_level>0) {
258  LOG(TERR,"%d: rp %d:%d, flags 0x%X",rdo1,row,pad,flags) ;
259  }
260  h -= wc/2 ;
261  continue ;
262  }
263 
264 #if 0
265  // fixing a bug in fee_23a FY23 version!
266  altro_present[id][ch]++ ;
267 
268  if(altro_present[id][ch]>1) {
269  run_errors++ ;
270  if(run_errors<20) {
271  if(online) LOG(ERR,"S%02:%d: AID %d:%d already present %d",sector1,rdo1,id,ch,altro_present[id][ch]) ;
272  }
273  h -= wc/2 ;
274  continue ;
275  }
276 #endif
277 
278  u_short *d = s1_dta + last_ix ; // this is where the raw data goes...
279  //u_short d[512] ;
280 
281  if(log_level>0) {
282  LOG(TERR,"%d: rp %d:%d; last_ix %d %p",rdo1,row,pad,last_ix,d) ;
283  }
284 
285  int ix = 0 ;
286 
287  //TLOGX(row) ;
288 
289 
290 // tmx = mark() ;
291 
292  for(int i=0;i<wc;) { // NOTE: no increment!
293  lo = *h-- ;
294  hi = *h-- ;
295 
296  //lo &= 0xFFFFF ;
297  //hi &= 0xFFFFF ;
298 
299  if(ix==0) { // see if I need to skip the first dummies!!!
300  u_short dd[4] ;
301 
302  dd[0] = (hi>>10)&0x3FF ;
303  dd[1] = hi & 0x3FF ;
304  dd[2] = (lo>>10)&0x3FF ;
305  dd[3] = lo & 0x3FF ;
306 
307  int s = 0 ;
308  if(dd[0]==0x2AA) {
309  s = 1 ;
310  if(dd[1]==0x2AA) {
311  s = 2 ;
312  if(dd[2]==0x2AA) {
313  s = 3 ;
314  }
315  }
316  }
317 
318  for(;s<4;s++) {
319  d[ix++] = dd[s] ;
320  }
321  }
322  else {
323  d[ix++] = (hi>>10)&0x3FF ;
324  d[ix++] = hi & 0x3FF ;
325  d[ix++] = (lo>>10)&0x3FF ;
326  d[ix++] = lo & 0x3FF ;
327  }
328 
329  i += 4 ;
330  }
331 
332  u_short *dd = d ;
333 
334  int seq_ix = 0 ;
335  struct seq_t *seq = s1[row][pad].seq ;
336 
337  //TLOGX(row) ;
338 
339  //LOG(TERR,"Here 1") ;
340  struct sseq_t {
341  u_short t_lo ;
342  u_short t_hi ;
343  u_short d[512] ;
344  } sseq[SEQ_MAX] ;
345 
346 // f_stat.tm[1] += delta(tmx) ;
347 
348 // tmx = mark() ;
349 
350  while(dd<(d+ix)) {
351  u_short t_lo ;
352 
353 
354  u_short t_len = *dd++ - 2 ;
355  u_short t_hi = *dd++ ;
356 
357  if(seq_ix>=(SEQ_MAX-1)) { // break if too many: note that I need to leave space for the sentinel!
358  break ;
359  }
360 
361 
362  t_lo = t_hi - t_len + 1 ;
363 
364  if(t_len>440 || t_hi>440 || t_lo>440) {
365  run_errors++ ;
366  if(run_errors<20) {
367  if(online||mode) LOG(ERR,"S%02d:%d: rp %d:%d (aid %d:%d, %d:%d), t_len %d, t_lo %d, t_hi %d; wc %d, ix %d, seq %d, %d",
368  sector1,rdo1,row,pad,
369  id,ch,id_pre,ch_pre,
370  t_len,t_lo,t_hi,wc,ix,seq_ix,d_end-h) ;
371  }
372  if(t_len>440 || t_hi>440 || t_lo>440) {
373  //err |= 0x20000 ;
374  if(1) {
375  //LOG(ERR,"Retry rp") ;
376  h = h_to_continue ;
377  retry = 1 ;
378  goto end_loop ;
379  }
380  break ;
381  }
382 
383  //if(t_hi>510) break ;
384  //if(t_lo>510) break ;
385  }
386 
387  //printf("rp %d:%d: seq %d: t_len %d, t_lo:hi %d:%d\n",row,pad,seq_ix,t_len,t_lo,t_hi) ;
388 
389  sseq[seq_ix].t_lo = t_lo ;
390  sseq[seq_ix].t_hi = t_hi ;
391  //sseq[seq_ix].dta_p = (dd-d) ; // I'm at the data now
392 
393 
394  for(int i=0;i<t_len;i++) {
395  short adc = *dd++ ;
396  sseq[seq_ix].d[i] = adc ;
397  //printf(" adc %d = %d\n",i,adc) ;
398  }
399  seq_ix++ ;
400 
401 
402  //dd += t_len ; // skip over data...
403 
404  }
405 
406  //TLOG() ;
407  if(run_type==1 || run_type==5) {
408  tpx_altro_struct a ;
409 
410  memset(&a,0,sizeof(a)) ;
411  a.what = TPX_ALTRO_DO_ADC ;
412  a.t = 1 ; //invent token
413  a.row = row ;
414  a.pad = pad ;
415  a.id = id ;
416  a.ch = ch ;
417  a.rdo = rdo1-1 ;
418  a.sector = sector1 ;
419  a.count = 435 ; // matches tb from 0..434
420 
421  int aix = 0 ;
422 
423  for(int i=(seq_ix-1);i>=0;i--) {
424  int t_len = sseq[i].t_hi - sseq[i].t_lo + 1 ;
425 
426  int ii = 0 ;
427  for(int j=(t_len-1);j>=0;j--) {
428  int adc = sseq[i].d[j] ;
429  a.adc[aix] = adc ;
430  a.tb[aix] = sseq[i].t_lo + ii ;
431  ii++ ;
432  aix++ ;
433  }
434 
435  peds->accum(&a) ;
436  }
437 
438  }
439  else if(altro) {
440  altro[altro_cou].row = row ;
441  altro[altro_cou].pad = pad ;
442  altro[altro_cou].ch = ch ;
443  altro[altro_cou].id = id ;
444  altro[altro_cou].count = 0 ;
445 
446  int aix = 0 ;
447 
448  for(int i=(seq_ix-1);i>=0;i--) {
449  int t_len = sseq[i].t_hi - sseq[i].t_lo + 1 ;
450 
451  int ii = 0 ;
452  for(int j=(t_len-1);j>=0;j--) {
453  int adc = sseq[i].d[j] ;
454  altro[altro_cou].adc[aix] = adc ;
455  altro[altro_cou].tb[aix] = sseq[i].t_lo + ii ;
456  altro[altro_cou].count++ ;
457  ii++ ;
458  aix++ ;
459  }
460  }
461 
462  altro_cou++ ;
463  }
464  else if(tpx_d) {
465  tpx_d->sector = sector1 ;
466  tpx_d->rdo = rdo1 ;
467  tpx_d->row = row ;
468  tpx_d->pad = pad ;
469  tpx_d->altro = id ;
470 
471  LOG(NOTE,"%d:%d %d:%d %d:%d",sector1,rdo1,row,pad,id,ch) ;
472 
473  tpx_d->ch_start(ch) ; // sets tpx_d->ch within
474 
475  for(int i=(seq_ix-1);i>=0;i--) {
476  int t_len = sseq[i].t_hi - sseq[i].t_lo + 1 ;
477 
478  int ii = 0 ;
479  for(int j=(t_len-1);j>=0;j--) {
480  int adc = sseq[i].d[j] ;
481  int tb ;
482 
483 
484  //a.adc[aix] = adc ;
485 
486  tb = sseq[i].t_lo + ii ;
487  //a.tb[aix] = sseq[i].t_lo + ii ;
488  ii++ ;
489  //aix++ ;
490 
491  tpx_d->accum(tb,adc) ;
492  }
493 
494  }
495 
496  tpx_d->ch_done() ;
497  }
498 
499 
500 
501 // f_stat.tm[2] += delta(tmx) ;
502 
503 
504  //LOG(TERR,"Here 2") ;
505  s_cou = 0 ;
506  dd = d ;
507  seq = s1[row][pad].seq ;
508 
509 // printf("row %d, pad %d: seq_ix %d\n",row,pad,seq_ix) ;
510 
511 
512 // tmx = mark() ;
513 
514  for(int i=(seq_ix-1);i>=0;i--) {
515  seq[s_cou].t_lo = sseq[i].t_lo;
516  seq[s_cou].t_hi = sseq[i].t_hi ;
517  seq[s_cou].dta_p = (dd-d) ;
518  seq[s_cou].blob_id = 0 ;
519 
520  int t_len = sseq[i].t_hi - sseq[i].t_lo + 1 ;
521 
522  //printf("... new seq %d: lo %d, hi %d\n",s_cou,seq[s_cou].t_lo,seq[s_cou].t_hi) ;
523 
524  for(int j=(t_len-1);j>=0;j--) {
525  *dd++ = sseq[i].d[j] ;
526  }
527  s_cou++ ;
528  }
529 
530  //TLOG() ;
531 
532  ix = dd - d ;
533 
534 // seq[s_cou].t_hi = -1 ; // sentinel; marker
535 
536 
537  sequence_cou += s_cou ;
538 
539  seq[s_cou].t_hi = -1 ;
540  s1[row][pad].ix = last_ix ; // remember where this data is
541 
542  last_ix += ix ;
543 
544 
545 #if 0
546  seq = s1[row][pad].seq ;
547  while(seq->t_hi>=0) {
548  int t_len = seq->t_hi - seq->t_lo + 1 ;
549  printf("rp %d:%d, t_lo %d, t_hi %d\n",row,pad,seq->t_lo,seq->t_hi) ;
550 
551  dd = d + seq->dta_p ;
552  for(int j=0;j<t_len;j++) {
553  printf(" adc %d: %d\n",j,dd[j]) ;
554  }
555 
556  seq++ ;
557  }
558 #endif
559 
560  id_pre = id ;
561  ch_pre = ch ;
562 
563 
564 // f_stat.tm[3] += delta(tmx) ;
565 
566  end_loop:;
567  }
568 
569 
570 
571  done:;
572 
573 // f_stat.tm[4] += delta(s_tmx) ;
574 
575  TLOG() ;
576 
577  return err ;
578 }
579 
580 /*
581  While not strictly static, this thing should not set or use
582  any class members
583 */
584 
585 u_int tpx23::get_token_s(char *c_addr, int wds)
586 {
587  u_int *d = (u_int *)c_addr ;
588  int t, trg, daq ;
589  int trg_cou ;
590  struct trg_data *trg_d ;
591 
592  t = 4097 ;
593  trg = 0 ;
594  daq = 0 ;
595 
596  TLOGX(rdo1) ;
597 
598  int type = (d[0]>>0)&0xF ;
599  int hdr_type = (d[0]>>24)&0xF ; //0: pre-FY23 headers, 1:FY23 headers
600 // hdr_type = 1 ;
601 
602 
603  switch(type) {
604  case DDL_TYPE_LOG :
605 // LOG(WARN,"log event") ;
606  goto done ;
607  case DDL_TYPE_MSC :
608 // LOG(WARN,"msc event") ;
609  goto done ;
610  default :
611 // LOG(WARN,"%d: event %d: S%02d:%d: type %d:%d, words %d",rdo,d[1],sec,rdo,type,subtype,wds) ;
612 // goto done ;
613  break ;
614  }
615 
616 
617 // for(int i=0;i<16;i++) {
618 // LOG(TERR,"%2d = 0x%08X",i,d[i]) ;
619 // }
620 
621  TLOGX(wds) ;
622 
623  d += wds ;
624  d-- ; // at the last datum
625 
626  if(hdr_type) {
627  //0:tick,1:status,2:1,3:trg
628 
629  t = d[-2] & 0xFFF ;
630  daq = (d[-2] >> 12)&0xF ;
631  trg = (d[-2] >> 16)&0xF ;
632 
633  goto done ;
634 
635  }
636 
637  // here we are with the old, pre-FY23 header format
638  TLOGX(rdo1) ;
639 
640  d -= 2 ; // skip 2 words trailer
641 
642 
643  trg_cou = *d ;
644 
645  d -= trg_cou * (sizeof(struct trg_data)/4) ;
646 
647 
648  trg_d = (struct trg_data *)d ;
649 
650  TLOGX(trg_cou) ;
651 
652  for(int i=0;i<trg_cou;i++) {
653 // LOG(WARN,"trg_data %d: 0x%X 0x%X 0x%X",i,trg_d[i].rhic_counter, trg_d[i].csr, trg_d[i].data) ;
654 
655  switch(trg_d[i].csr & 0xFF000000) {
656  case 0xFF000000 : // FIFO stuff?
657  case 0xDD000000 : // FIFO stuff?
658  break ;
659  case 0xEE000000 : // prompt token
660  default :
661  t = trg_d[i].data & 0xFFF ;
662  daq = (trg_d[i].data >> 12)&0xF ;
663  trg = (trg_d[i].data >> 16)&0xF ;
664  goto done ;
665  }
666  }
667 
668  TLOGX(rdo1) ;
669 
670  done:;
671 
672  TLOGX(rdo1) ;
673 
674  return (trg<<16)|(daq<<12)|t ;
675 }
676 
677 u_int tpx23::get_token(char *c_addr, int wds)
678 {
679  u_int *d = (u_int *)c_addr ;
680  u_int *d_first ;
681  token = 4097 ;
682  trg_cmd = 0 ;
683  daq_cmd = 0 ;
684 
685  d_first = d ;
686 
687  err = 0 ;
688 
689 
690 // LOG(TERR,"get_token %u",d[1]) ;
691 
692  tdbg[0] = d[1] ; // RHIC counter
693 
694  d += wds ;
695  d-- ; // at the last datum
696 
697 
698 // LOG(TERR,"evt %d(hdr %d, wds %d): 0x%08X 0x%08X, 0x%08X 0x%08X 0x%08X 0x%08X",evt,hdr_version,wds,
699 // d_first[0],d_first[1],d[-3],d[-2],d[-1],d[0]) ;
700 
701  TLOGX(hdr_version) ;
702 
703  // for the new FY23 format!
704  if(hdr_version) {
705  tdbg[0] = d_first[1] ; // RHIC counter start
706  tdbg[1] = d[0] ; // RHIC counter end
707  tdbg[2] = d[-1] ; // event status
708  tdbg[3] = d[-2] ; // trg word
709 
710  tdbg[4] = d[-5] ; // at start-altro
711  tdbg[5] = d[-4] ; // at end-altr
712  tdbg[6] = d[-3] ; // before DDL
713 
714  token = d[-2] & 0xFFF ;
715  daq_cmd = (d[-2] >> 12)&0xF ;
716  trg_cmd = (d[-2] >> 16)&0xF ;
717 
718 
719  u_int evt_err = d[-1] ;
720  if(evt_err & 0xFF000000) {
721  int cou ;
722 
723  if(wds>20) cou = 20 ;
724  else cou = wds ;
725 
726  err |= 0x1 ;
727 
728  if(online) {
729  LOG(ERR,"evt_err %d:%d: 0x%08X: 0x%08X, wds %u",evt,rdo1,d_first[0],evt_err,wds) ;
730  for(int i=0;i<cou;i++) {
731  LOG(TERR," %d: 0x%08X",i,d_first[i]) ;
732  }
733  }
734  }
735 
736 
737 #if 0
738  printf(" delta evt %d: all %d: %d %d %d %d - token %d, wds %d\n",evt,tdbg[1]-tdbg[0],
739  tdbg[4]-tdbg[0],
740  tdbg[5]-tdbg[4],
741  tdbg[6]-tdbg[5],
742  tdbg[1]-tdbg[6],
743  token,wds) ;
744 
745 #endif
746 
747  d_end = d - 6 ; // last word of the ALTRO contribution
748 
749  return (trg_cmd<<16)|(daq_cmd<<12)|token ;
750 
751  }
752 
753  TLOGX(rdo1) ;
754 
755  d -= 2 ; // skip 2 words trailer to position myself at "trigger count"
756 
757  int trg_cou = *d ;
758 
759  d -= trg_cou * (sizeof(struct trg_data)/4) ; // move back 1
760 
761  TLOGX(trg_cou) ;
762 
763  struct trg_data *trg = (struct trg_data *)d ;
764  for(int i=0;i<trg_cou;i++) {
765 
766 // if(hdr_version) {
767 // LOG(TERR,"trg_data %d: 0x%X 0x%X 0x%X",i,trg[i].rhic_counter, trg[i].csr, trg[i].data) ;
768 // }
769 
770  switch(trg[i].csr & 0xFF000000) {
771  case 0xFF000000 : // FIFO stuff?
772  case 0xDD000000 : // FIFO stuff?
773  break ;
774  case 0xEE000000 : // prompt token
775  default :
776  token = trg[i].data & 0xFFF ;
777  daq_cmd = (trg[i].data >> 12)&0xF ;
778  trg_cmd = (trg[i].data >> 16)&0xF ;
779  goto done ;
780  }
781  }
782 
783  TLOG() ;
784 
785  done:;
786 
787  TLOG() ;
788 
789  d_end = d - 3 ; // very last ALTRO datum
790 
791  return (trg_cmd<<16)|(daq_cmd<<12)|token ;
792 }
793 
794 
795 // This unfortunately needs to be globally visible as per tpxCore.cxx
796 //struct tpx_rdo tpx_rdo[24][6] ;
797 
798 int tpx23::msc_dump(char *c_addr, int wds)
799 {
800 
801 
802  LOG(INFO,"msc_dump: %d bytes, should be %d: Sreal%d:%d",wds*4,sizeof(struct tpx_rdo),
803  sector1,rdo1) ;
804 
805  // modelled after tpxCore.cxx tpx_show_status
806  // struct tpx_rdo has
807  //
808  c_addr += 2*4 ; // skip 2 header words?
809 
810  memcpy(&(tpx_rdo[sector1-1][rdo1-1]),c_addr,sizeof(struct tpx_rdo)) ;
811 
812  // modify tpx_show_status to have a pointer to the data instead of this thread-unsafe tpx_rdo static!
813  err = tpx_show_status(sector1,1<<(rdo1-1),0) ;
814  if(err) {
815  if(online) LOG(ERR,"S%02d:%d: tpx_show_status %d",sector1,rdo1,err) ;
816  }
817 
818  return err ;
819 }
820 
821 int tpx23::log_dump(char *c_addr, int wds)
822 {
823  int len, cou ;
824 
825 
826 
827 
828  char *tmpbuff ;
829  int non_ascii ;
830  int do_log ;
831  u_int err_status = 0 ;
832  char *rdobuff ;
833 
834 
835  int s_real, r_real ;
836 // tpx36_to_real(subdet_id,rdo,s_real,r_real) ;
837  s_real = sector1 ;
838  r_real = rdo1 ;
839 
840  int rdo = rdo1 ;
841 
842 // do_log = (log_file) ? 0 : 1 ;
843  do_log = 1 ; // to tpx.log
844 
845 // LOG(TERR,"USing long_dump: rdo %d, log to LOG %d", rdo, do_log) ;
846 
847  non_ascii = 0 ;
848 
849  int max_cou = (words-4)*4 ;
850  rdobuff = (char *)(c_addr+2*4) ; // skip header
851 
852  // one liner or more?
853  len = strlen(rdobuff) ;
854  if(len>max_cou) len = max_cou ;
855 
856  cou = 0 ;
857 
858  tmpbuff = (char *) malloc(len+1) ;
859  memcpy(tmpbuff,rdobuff,len+1) ;
860 
861  // tokenize into strings
862  for(int i=0;i<len;i++) {
863  if(tmpbuff[i]=='\n') {
864  cou++ ;
865  tmpbuff[i] = 0 ;
866  }
867  }
868 
869 // LOG(TERR,"%d lines %s",cou,rdobuff) ;
870 
871  for(int i=0;i<(len);) { // was len+1?
872 
873  int st = i ;
874  int err = 0 ;
875 
876 
877  // check for non-printable chars; should be the same as the
878  // new SRAM check
879  for(int j=st;j<len;j++) {
880  if(tmpbuff[j] == 0) break ; // new line
881 
882  if(!isprint(tmpbuff[j])) {
883  if(tmpbuff[j] == 9) ; // skip tab
884  else {
885  LOG(WARN,"---> [%d LOG] Unprintable character 0x%02X? -- powercycle",rdo,(u_char)tmpbuff[j]) ;
886  //LOG(ERR,"But ignored for FY22") ;
887  err_status |= 1;
888  err = -1 ;
889  tmpbuff[j] = '?' ;
890  }
891  }
892 
893  }
894 
895 #if 1
896  if(strstr(tmpbuff+st,"RHIC clock: ")) {
897  if(strstr(tmpbuff+st,"EXTERNAL")) {
898  rhic_clock = 1 ;
899  }
900  else {
901  rhic_clock = 0 ; // internal
902  }
903  }
904 
905  if(strstr(tmpbuff+st,"JTAG dev ")) {
906  int ret, dev ;
907  u_int dev_id, user ;
908 
909  dev = -1 ;
910 
911 // LOG(WARN,"[S%02d:%d LOG]: JTAG:",s_real,r_real,tmpbuff+st) ;
912 
913  ret = sscanf(tmpbuff+st,"JTAG dev %d: ID 0x%X, USERcode 0x%X",&dev,&dev_id,&user) ;
914  LOG(WARN,"JTAG: ret %d, dev %d, dev_id 0x%08X, user 0x%08X",ret, dev, dev_id,user) ;
915 
916  if(ret==3 && dev>=0 && dev<5) {
917  fpga_usercode[dev] = user ;
918  }
919  }
920 #endif
921 
922  if(strstr(tmpbuff+st,"SPECIAL_0 code")) {
923  LOG(ERR,"---> SPECIAL code: RDO %d",rdo) ;
924  }
925 
926  // check for error string but just print a warning, we'll be more selective later
927  if(strstr(tmpbuff+st,"ERR")) {
928  if(strstr(tmpbuff+st,"FLASH Id")) {
929  LOG(WARN,"[S%02d:%d LOG]: contains ERR \"%s\"",s_real,r_real,tmpbuff+st) ;
930  }
931  else {
932  //err = -1 ;
933  //LOG(ERR,"[S%02d:%d LOG]: contains ERR \"%s\"",s_real,r_real,tmpbuff+st) ;
934  }
935  }
936 
937  // check for question mark in CPLD status
938  if(strstr(tmpbuff+st,"Status: Xil config")) {
939  if(strstr(tmpbuff+st,"!")) {
940  //err_status |= DET_ERR_LOCAL_PS ;
941  LOG(WARN,"---> [S%d:%d LOG] CPLD status has \"!\" -- ignored ",s_real,r_real) ;
942  //err = -1 ;
943  }
944 
945  if(strstr(tmpbuff+st,"FEE power BAD")) {
946  //err_status |= DET_ERR_OPER_PS ;
947  //LOG(ERR,"---> [S%d:%d LOG] FEE power BAD -- powercycle (ignored)",s_real,r_real) ;
948  //err = -1 ;
949  }
950  }
951 
952 
953  if(strstr(tmpbuff+st,"SRAM check failed")) {
954  err = -1 ;
955  err_status |= 2 ;
956  LOG(ERR,"---> [%d LOG] SRAM check failed -- powercycle",rdo) ;
957  }
958 
959  if(strstr(tmpbuff+st,"CPLD claims error")) {
960  err = -1 ;
961  LOG(ERR,"---> [%d LOG] CPLD claims error -- reconfig 0x300",rdo) ;
962  }
963 
964  if(strstr(tmpbuff+st,"can't configure RDO!")) {
965  LOG(ERR,"---> [%d LOG] \"can't configure RDO\" -- reconfig 0x300",rdo) ;
966 // err = -1 ;
967  }
968 
969 
970  // mostly run related
971  if(strstr(tmpbuff+st,"lost RHIC")) {
972  LOG(ERR,"---> [%d LOG] \"lost RHIC\" -- restart run",rdo) ;
973  err = -1 ;
974  }
975  if(strstr(tmpbuff+st,"NO RHIC CLOCK")) {
976  LOG(ERR,"---> [%d LOG] \"NO RHIC CLOCK\" -- restart run",rdo) ;
977  err = -1 ;
978  }
979 
980  if(strstr(tmpbuff+st,"DRIFT")) {
981  LOG(ERR,"---> [%d LOG] \"DRIFT/clock problems\" -- restart run",rdo) ;
982  err = -1 ;
983  }
984 
985 
986  if(strstr(tmpbuff+st,"CRIT")) {
987  err = -1 ;
988  LOG(ERR,"---> [%d LOG] CRIT string in log -- restart run",rdo) ;
989  }
990 
991  if(strstr(tmpbuff+st,"altro error")) {
992  err = -1 ;
993  err_status |= 3 ;
994  LOG(ERR,"---> [%d LOG] altro error -- restart run",rdo) ;
995  }
996 
997 
998  if(strstr(tmpbuff+st,"ERR ALTRO")) {
999  //err = -1 ;
1000  //LOG(WARN,"---> [%d LOG] ERR ALTRO -- CHECK THIS",rdo) ;
1001  }
1002 
1003  if(strstr(tmpbuff+st,"ERR: expired")) {
1004  err = -1 ;
1005  }
1006 
1007 
1008  if(err<0) {
1009  LOG(ERR,"[S%02d:%d %d]: %s",s_real,r_real,evt,tmpbuff+st) ;
1010  log_is_error = 1 ;
1011  }
1012  else if(do_log) {
1013  LOG(INFO,"[S%02d:%d %d]: %s",s_real,r_real,evt,tmpbuff+st) ;
1014  }
1015 
1016  while(tmpbuff[i]) {
1017  //LOG(WARN,"%d: %d [%c]",i,tmpbuff[i],tmpbuff[i]) ;
1018  i++ ;
1019  if(i>=len) break ;
1020  }
1021  i++ ;
1022 
1023  }
1024 
1025 // TLOG() ;
1026  free(tmpbuff) ;
1027 
1028 #if 0
1029 
1030  t = time(NULL) ;
1031  ctime_r(&t,tm) ;
1032  tm[strlen(tm)-1] = 0 ; // kill the new-line
1033 
1034  switch(cou) {
1035  case 0 : // no newline found
1036  term_char = "\n" ;
1037  buff = rdobuff ;
1038  break ;
1039  case 1 : // 1 newline aka 1 liner
1040  buff = rdobuff ;
1041  term_char = "" ;
1042  break ;
1043  default :
1044  buff = "" ;
1045  term_char = "\n" ;
1046  break ;
1047  }
1048 
1049  if(log_file == 0) {
1050  return err_status;
1051  }
1052 
1053 
1054 
1055  pthread_mutex_lock(&log_file_mutex) ;
1056 
1057 
1058  fprintf(log_file,"%s[RDO %d] (%s) %s%s%s",ANSI_BLUE,rdo,
1059  tm,
1060  buff,
1061  term_char,ANSI_RESET) ;
1062 
1063  if(cou > 1) {
1064  fprintf(log_file,"%s",rdobuff) ;
1065  }
1066 
1067  fflush(log_file) ;
1068 
1069  pthread_mutex_unlock(&log_file_mutex) ;
1070 #endif
1071 
1072  return err_status ;
1073 
1074 }
1075 
1076 
1077 int tpx23::rdo_scan(char *c_addr, int wds)
1078 {
1079  int ret = 0 ;
1080 
1081  u_int *d = (u_int *)c_addr ;
1082 
1083 
1084 
1085  int rdo = (d[0]>>8)&0xF ;
1086  int sec = (d[0]>>12)&0xFF ;
1087 
1088  subtype = (d[0]>>4)&0xF ; //class
1089  type = (d[0]>>0)&0xF ; //class
1090 
1091  hdr_version = (d[0]>>24)&0xF ;
1092 // hdr_version = 1 ;
1093 
1094  d_start = d ;
1095  token = 4096 ;
1096  trg_cmd = 0 ;
1097  daq_cmd = 0 ;
1098  err = 0 ;
1099 
1100  words = wds ;
1101 
1102  evt++ ;
1103 
1104 
1105  if((d[0]&0xF0000000)!=0xF0000000) {
1106  LOG(ERR,"%d:%d: bad header 0x%08X",evt,rdo1,d[0]) ;
1107  }
1108 
1109  if(rdo!=rdo1 || sec!=sector1) {
1110  LOG(ERR,"%d:%d: wrong sec,rdo: rdo expect %d is %d; sector expect %d is %d [0x%08X]",evt,rdo1,
1111  rdo1,rdo,sector1,sec,d[0]) ;
1112  }
1113 
1114 
1115  TLOG() ;
1116 
1117  switch(type) {
1118  case DDL_TYPE_LOG :
1119 
1120  TLOG() ;
1121  ret = log_dump(c_addr,wds) ;
1122  TLOG() ;
1123  break ;
1124  case DDL_TYPE_MSC :
1125  if(subtype==2) { // heartbeat; ignore
1126  break ;
1127  }
1128 
1129  LOG(WARN,"%d: MSC: event %d: S%02d:%d: type %d:%d, words %d",rdo1,d[1],sec,rdo,type,subtype,words) ;
1130 
1131  ret = msc_dump(c_addr, wds) ;
1132 
1133  break ;
1134  default : // ALTRO data -- and we're off
1135  evt_trgd++ ;
1136  TLOG() ;
1137  ret = fee_scan() ;
1138  TLOG() ;
1139  break ;
1140  }
1141 
1142  return ret ; // should be ret
1143 
1144 }
1145 
1146 
1147 tpx23::tpx23()
1148 {
1149 // LOG(TERR,"%s %d %p",__PRETTY_FUNCTION__,sizeof(rp_gain),rp_gain) ;
1150 
1151  rts_id = TPX_ID ;
1152 
1153 
1154  if(rp_gain_tpx==0) {
1155  rp_gain_tpx = (row_pad_t (*)[ROW_MAX+1][PAD_MAX+1]) malloc(sizeof(row_pad_t)*24*(ROW_MAX+1)*(PAD_MAX+1)) ;
1156 
1157  // initialize here!
1158  for(int s=0;s<24;s++) {
1159  for(int r=0;r<=ROW_MAX;r++) {
1160  for(int p=0;p<=PAD_MAX;p++) {
1161  rp_gain_tpx[s][r][p].gain = 1.0 ;
1162  rp_gain_tpx[s][r][p].t0 = 0.0 ;
1163  rp_gain_tpx[s][r][p].flags = 0 ;
1164  }}}
1165 
1166  }
1167 
1168  rp_gain = rp_gain_tpx ;
1169 
1170  if(peds==0) {
1171  peds = new class tpxPed ;
1172  }
1173 
1174  row_min = 14 ;
1175  row_max = 45 ;
1176  for(int row=1;row<=45;row++) rowlen[row] = tpc_rowlen[row] ;
1177 
1178  hdr_version = 0 ; // 0:pre FY23
1179 
1180  memset(fpga_usercode,0,sizeof(fpga_usercode)) ;
1181 
1182  altro = 0 ;
1183 
1184  tpx_d = 0 ;
1185 }
1186 
1187 
1188 #include <DAQ_TPX/tpxCore.h>
1189 
1190 u_char tpx23::flags_row_pad(int asic, int channel, int &row, int &pad)
1191 {
1192  row = 255 ;
1193  pad = 255 ;
1194 
1195  if(rdo1<1||rdo1>6) return 0xFF ;
1196 
1197  // I will rewrite this to make it super-fast
1198 
1199  tpx_from_altro(rdo1-1,asic,channel,row,pad) ; // from tpxCore!
1200  if(row==0 || row==255) return 0xFF ;
1201  if(pad==0 || pad==255) return 0xFF ;
1202 
1203 // row -= 13 ; // HACK: row 14 becomes row 1
1204 
1205  return rp_gain[sector1-1][row][pad].flags ;
1206 }
1207 
1208 #if 0
1209 int tpx23::run_start()
1210 {
1211 // LOG(WARN,"TPX23 run_start") ;
1212 
1213  rhic_clock = -1 ; // unknown
1214  log_is_error = 0 ;
1215 
1216  return 0 ;
1217 }
1218 #endif
Definition: tpxPed.h:14
Definition: tpx_rdo.h:4