StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
daq_bsmd.cxx
1 #include <sys/types.h>
2 #include <errno.h>
3 #include <assert.h>
4 
5 #include <rtsLog.h>
6 #include <rtsSystems.h>
7 
8 
9 
10 #include <SFS/sfs_index.h>
11 #include <DAQ_READER/daqReader.h>
12 #include <DAQ_READER/daq_dta.h>
13 
14 
15 #include "daq_bsmd.h"
16 
17 const char *daq_bsmd::help_string = "BSMD\n\
18 adc returns bsmd_t;\n\
19 raw returns raw data\n" ;
20 
22 {
23 public:
25  daq_det_factory::det_factories[BSMD_ID] = this ;
26  }
27 
28  daq_det *create() {
29  return new daq_bsmd ;
30  }
31 } ;
32 
33 static daq_det_bsmd_factory bsmd_factory ;
34 
35 static const struct bsmd_old_to_new_map_t {
36  char new_sec ;
37  char new_rdo ;
38 } bsmd_old_to_new_map[13] = {
39  { 0, 0 }, // we start from 1 so this a dummy!
40  { 1, 1 },
41  { 1, 2 },
42  { 1, 3 },
43  { 1, 4 },
44  { 1, 5 },
45  { 1, 6 },
46  { 2, 1 },
47  { 2, 2 },
48  { 2, 3 },
49  { 2, 4 },
50  { 2, 5 },
51  { 2, 6 }
52 
53 } ;
54 
55 
56 
57 daq_bsmd::daq_bsmd(daqReader *rts_caller)
58 {
59  rts_id = BSMD_ID ;
60  name = rts2name(rts_id) ;
61  sfs_name = "bsmd" ;
62  caller = rts_caller ;
63  t_data = 0 ;
64 
65  if(caller) caller->insert(this, rts_id) ;
66 
67 
68  adc = new daq_dta ;
69  adc_non_zs = new daq_dta ;
70  ped = new daq_dta ;
71  rms = new daq_dta ;
72  raw = new daq_dta ;
73 
74  LOG(DBG,"%s: constructor: caller %p",name,rts_caller) ;
75  return ;
76 }
77 
78 daq_bsmd::~daq_bsmd()
79 {
80  LOG(DBG,"%s: DEstructor",name) ;
81 
82 
83  delete adc ;
84  delete ped ;
85  delete rms ;
86  delete adc_non_zs ;
87  delete raw ;
88 
89  return ;
90 }
91 
92 
93 
94 daq_dta *daq_bsmd::get(const char *bank, int sec, int row, int pad, void *p1, void *p2)
95 {
96  Make() ;
97  if(!present) return 0 ;
98 
99  LOG(DBG,"got something") ;
100 
101  if(strcasecmp(bank,"adc")==0) {
102  return handle_adc(row) ;
103  }
104  else if(strcasecmp(bank,"adc_non_zs")==0) {
105  return handle_adc_non_zs(row) ;
106  }
107  else if(strcasecmp(bank,"ped")==0) {
108  return handle_ped_rms(row,1) ;
109  }
110  else if(strcasecmp(bank,"rms")==0) {
111  return handle_ped_rms(row,0) ;
112  }
113  else if(strcasecmp(bank,"raw")==0) {
114  return handle_raw(row) ;
115  }
116 
117 
118  LOG(ERR,"%s: unknown bank type \"%s\"",name,bank) ;
119  return 0 ;
120 }
121 
122 // this is the zero-suppressed reader!
123 daq_dta *daq_bsmd::handle_adc(int rdo)
124 {
125  struct bsmd_desc bsmd_d ;
126  int start_r, stop_r ;
127  int bytes ;
128 
129  assert(caller) ; // sanity...
130 
131  LOG(DBG,"%s: present 0x%X",name,present) ;
132 
133  if(!present) return 0 ;
134 
135  if(rdo <= 0) {
136  start_r = 1 ;
137  stop_r = BSMD_FIBERS ;
138  }
139  else {
140  start_r = stop_r = rdo ;
141  }
142 
143  bytes = 0 ;
144  memset(&bsmd_d,0,sizeof(bsmd_d)) ;
145 
146  if(present & DET_PRESENT_DATAP) { // in datap!
147  char *emcp = (char *)legacyDetp(rts_id, caller->mem) ;
148  //LOG(NOTE,"EMCP %p?",emcp) ;
149  if(bsmd_reader(emcp, &bsmd_d)==0) return 0 ;
150 
151  for(int r=start_r;r<=stop_r;r++) {
152  bytes += bsmd_d.bytes[r-1][1] ;
153  }
154 
155  }
156  else {
157  int s_new, r_new ;
158  char str[256] ;
159  char *full_name ;
160 
161  for(int r=start_r;r<=stop_r;r++) {
162  int l_bytes ;
163 
164  s_new = bsmd_old_to_new_map[r].new_sec ;
165  r_new = bsmd_old_to_new_map[r].new_rdo ;
166 
167  sprintf(str,"%s/sec%02d/rb%02d/adc",sfs_name,s_new,r_new) ;
168 
169  full_name = caller->get_sfs_name(str) ;
170 
171  if(!full_name) continue ;
172 
173  l_bytes = caller->sfs->fileSize(full_name) ;
174  if(!l_bytes) continue ;
175 
176  bsmd_d.bytes[r-1][1] = l_bytes ;
177 
178  bytes += l_bytes ;
179  }
180 
181  }
182 
183 
184 
185  LOG(DBG,"rdo %d: bytes %d",rdo,bytes) ;
186 
187  if(bytes==0) return 0 ; // odd, nothing found...
188 
189  adc->create(bytes,"adc",rts_id,DAQ_DTA_STRUCT(bsmd_t)) ;
190 
191  u_short *data_alloc ;
192  int malloced_bytes ;
193 
194  if(present & DET_PRESENT_DATAP) {
195  malloced_bytes = 0 ;
196  data_alloc = 0 ;
197  }
198  else {
199  // ZS _could_ be larger than raw by factors of 2!
200  malloced_bytes = (2400+10+3+100)*4 *2 ;
201  data_alloc = (u_short *)malloc(malloced_bytes) ;
202  assert(data_alloc) ;
203  }
204 
205 
206  for(int r=start_r;r<=stop_r;r++) {
207  int count ;
208  int version, fiber ;
209  u_short *data ;
210 
211  if(bsmd_d.bytes[r-1][1] == 0) continue ;
212 
213  if(present & DET_PRESENT_DATAP) {
214 
215  data = (u_short *)(bsmd_d.dta[r-1][1]) ; // move to data start
216  }
217  else {
218  int s_new, r_new ;
219  char str[256] ;
220  char *full_name ;
221 
222  s_new = bsmd_old_to_new_map[r].new_sec ;
223  r_new = bsmd_old_to_new_map[r].new_rdo ;
224 
225  sprintf(str,"%s/sec%02d/rb%02d/adc",sfs_name,s_new,r_new) ;
226 
227  full_name = caller->get_sfs_name(str) ;
228 
229  if(!full_name) continue ;
230 
231  bytes = caller->sfs->fileSize(full_name) ;
232 
233  if(bytes > malloced_bytes) {
234  LOG(ERR,"Too big %s is %d",str,bytes) ;
235  continue ;
236  }
237 
238  data = data_alloc ;
239  caller->sfs->read(str, (char *)data, bytes) ;
240 
241  bsmd_d.endian[r-1][1] = 0 ; // little endian
242  }
243 
244  bsmd_t *bsmd = (bsmd_t *) adc->request(1) ;
245 
246  memset(bsmd,0,sizeof(bsmd_t)) ;
247 
248 
249  if(bsmd_d.endian[r-1][1]) { // big!
250  version = b2h16(data[0]) ;
251  count = b2h16(data[1]) ;
252  bsmd->cap = b2h16(data[2]) ;
253  fiber = b2h16(data[3]) ;
254 
255  bsmd->cap &= 0x7F ;
256  LOG(NOTE,"%s: fiber %d[%d]: count %d, cap %d, version 0x%04X",name,r,fiber,count,bsmd->cap,version) ;
257 
258  data += 4 ;
259 
260  for(int c=0;c<count;c++) {
261  int ch = b2h16(*data++) ;
262  int adc = b2h16(*data++) ;
263 
264  bsmd->adc[ch] = adc ;
265  }
266  }
267  else {
268  version = l2h16(data[0]) ;
269  count = l2h16(data[1]) ;
270  bsmd->cap = l2h16(data[2]) ;
271  fiber = l2h16(data[3]) ;
272 
273  bsmd->cap &= 0x7F ;
274  LOG(NOTE,"%s: fiber %d[%d]: count %d, cap %d, version 0x%04X",name,r,fiber,count,bsmd->cap,version) ;
275 
276  data += 4 ;
277 
278  for(int c=0;c<count;c++) {
279  int ch = l2h16(*data++) ;
280  int adc = l2h16(*data++) ;
281 
282  bsmd->adc[ch] = adc ;
283  }
284  }
285 
286 
287 
288  adc->finalize(1,0,r,bsmd->cap) ;
289  }
290 
291  adc->rewind() ;
292 
293  if(malloced_bytes) free(data_alloc) ;
294 
295  return adc ;
296 
297 }
298 
299 daq_dta *daq_bsmd::handle_ped_rms(int rdo, int is_ped)
300 {
301  int start_r, stop_r ;
302  int bytes ;
303  struct bsmd_desc bsmd_d ;
304  daq_dta *dta_use ;
305 
306  if(rdo <= 0) {
307  start_r = 1 ;
308  stop_r = BSMD_FIBERS ;
309  }
310  else {
311  start_r = stop_r = rdo ;
312  }
313 
314  // for SFS based
315  int want_sec[3] = { 0, 0, 0 } ;
316  u_short *pedrms[3] = { 0, 0, 0 } ;
317 
318  for(int r=start_r;r<=stop_r;r++) {
319 
320  int s_new, r_new ;
321  s_new = bsmd_old_to_new_map[r].new_sec ;
322  r_new = bsmd_old_to_new_map[r].new_rdo ;
323 
324  want_sec[s_new] = 1 ;
325  }
326 
327 
328  bytes = 0 ;
329 
330  if(present & DET_PRESENT_DATAP) { // in datap!
331  char *emcp = (char *)legacyDetp(rts_id, caller->mem) ;
332  //LOG(NOTE,"EMCP %p?",emcp) ;
333  if(bsmd_reader(emcp, &bsmd_d)==0) return 0 ;
334 
335 
336  for(int r=start_r;r<=stop_r;r++) {
337  bytes += bsmd_d.bytes[r-1][2] ;
338  }
339 
340 
341  }
342  else {
343  for(int s=1;s<=2;s++) {
344  char str[256] ;
345  char *full_name ;
346  int l_bytes ;
347 
348  if(want_sec[s] == 0) continue ;
349 
350  sprintf(str,"%s/sec%02d/pedrms",sfs_name,s) ;
351 
352  full_name = caller->get_sfs_name(str) ;
353 
354  if(!full_name) continue ;
355 
356  l_bytes = caller->sfs->fileSize(full_name) ;
357 
358  bytes += l_bytes ;
359 
360 
361  pedrms[s] = (u_short *) malloc(l_bytes) ;
362 
363  caller->sfs->read(str, (char *)(pedrms[s]), l_bytes) ;
364 
365  }
366 #if 0
367  // try to fix FY13 bug!
368  if(bytes==0) { // not found
369  char str[256] ;
370  char *full_name ;
371 
372 
373  sprintf(str,"%s/pedrms",sfs_name) ;
374 
375  full_name = caller->get_sfs_name(str) ;
376 
377  LOG(TERR,"FY13 bug -- Got %s for RDO%d, ped %d?!",str,rdo,is_ped) ;
378  }
379 #endif
380  }
381 
382  LOG(DBG,"rdo %d: bytes %d",rdo,bytes) ;
383 
384  if(bytes==0) return 0 ;
385 
386 
387  if(is_ped) {
388  dta_use = ped ;
389  dta_use->create(1,"bsmd_ped",rts_id,DAQ_DTA_STRUCT(bsmd_t)) ;
390  }
391  else {
392  dta_use = rms ;
393  dta_use->create(1,"bsmd_rms",rts_id,DAQ_DTA_STRUCT(bsmd_t)) ;
394  }
395 
396 
397 
398 
399  LOG(DBG,"doing rdos: %d-%d",start_r,stop_r) ;
400 
401  for(int r=start_r;r<=stop_r;r++) {
402  u_short *data ;
403 
404  if(present & DET_PRESENT_DATAP) {
405  if(bsmd_d.bytes[r-1][2] == 0) continue ;
406 
407  data = (u_short *)(bsmd_d.dta[r-1][2]) ; // move to data start
408  }
409  else { // SFS
410  int s_new, r_new ;
411 
412  s_new = bsmd_old_to_new_map[r].new_sec ;
413  r_new = bsmd_old_to_new_map[r].new_rdo ;
414  if(pedrms[s_new]==0) continue ;
415 
416  data = pedrms[s_new] + ((4+128*4800)*(r_new-1)) ;
417 
418  bsmd_d.endian[r-1][2] = 0 ;// little endian
419 
420  }
421 
422  LOG(DBG,"BSMD PEDR: rdo %d: 0x%04X 0x%04X 0x%04X 0x%04X",r,data[0],data[1],data[2],data[3]) ;
423 
424 
425  data += 4 ; // skip 4 shorts...
426 
427 
428  for(int c=0;c<128;c++) {
429  bsmd_t *bsmd = (bsmd_t *) dta_use->request(1) ;
430  bsmd->cap = c ;
431  for(int ch=0;ch<4800;ch++) {
432  int adc ;
433  if(bsmd_d.endian[r-1][2]) {
434  adc = b2h16(*data++) ;
435  }
436  else {
437  adc = l2h16(*data++) ;
438  }
439 
440  if(is_ped) adc &= 0x3FF ; // ped is lower 10 bits
441  else {
442  adc >>= 10 ; // rms is upper 6
443  }
444 
445  bsmd->adc[ch] = adc ;
446  }
447  dta_use->finalize(1,0,r,c) ;
448  }
449 
450 
451  }
452 
453  for(int s=1;s<=2;s++) {
454  if(pedrms[s]) free(pedrms[s]) ;
455  }
456 
457  dta_use->rewind() ;
458 
459  return dta_use ;
460 
461 }
462 
463 
464 daq_dta *daq_bsmd::handle_adc_non_zs(int rdo)
465 {
466  struct bsmd_desc bsmd_d ;
467  int start_r, stop_r ;
468  int bytes ;
469 
470  assert(caller) ; // sanity...
471 
472  if(!present) return 0 ;
473 
474  if(rdo <= 0) {
475  start_r = 1 ;
476  stop_r = BSMD_FIBERS ;
477  }
478  else {
479  start_r = stop_r = rdo ;
480  }
481 
482  bytes = 0 ;
483 
484  if(present & DET_PRESENT_DATAP) { // in datap!
485  char *emcp = (char *)legacyDetp(rts_id, caller->mem) ;
486 
487  if(bsmd_reader(emcp, &bsmd_d)==0) return 0 ;
488 
489 
490  for(int r=start_r;r<=stop_r;r++) {
491  //LOG(NOTE,"BSMD: adc_non_zs: fiber %d, bytes %d",r,bsmd_d.bytes[r-1][0]) ;
492  bytes += bsmd_d.bytes[r-1][0] ;
493  }
494 
495  if(bytes==0) return 0 ;
496 
497  adc_non_zs->create(bytes,"adc_nzs",rts_id,DAQ_DTA_STRUCT(bsmd_t)) ;
498 
499  for(int r=start_r;r<=stop_r;r++) {
500 
501  if(bsmd_d.bytes[r-1][0] == 0) continue ;
502 
503  bsmd_t *bsmd = (bsmd_t *) adc_non_zs->request(1) ;
504 
505 // memset(bsmd,0,sizeof(bsmd_t)) ;
506 
507  // cap is 64 bytes after the start
508  bsmd->cap = *(char *)((char *)bsmd_d.dta[r-1][0] + 4 + 4*16) ;
509 
510  LOG(DBG,"Found cap %d",bsmd->cap) ;
511 
512  u_short *data = (u_short *)((char *)bsmd_d.dta[r-1][0] + 4 + 256) ; // move to data start
513 
514  for(int c=0;c<BSMD_DATSIZE;c++) {
515  bsmd->adc[c] = l2h16(*data++) ;
516  }
517 
518  adc_non_zs->finalize(1,0,r,bsmd->cap) ;
519  }
520 
521 
522  }
523  else {
524 
525  for(int r=start_r;r<=stop_r;r++) {
526  bytes += sizeof(bsmd_t) ; // approx
527  }
528 
529 
530  adc_non_zs->create(bytes,"adc_nzs",rts_id,DAQ_DTA_STRUCT(bsmd_t)) ;
531 
532  for(int r=start_r;r<=stop_r;r++) {
533  daq_dta *dd = handle_raw(r) ;
534 
535  if(dd==0) continue ;
536 
537  if(dd->iterate() == 0) continue ;
538 
539 
540  u_int *d = (u_int *)dd->Void ;
541  u_int rdo_words = dd->ncontent ;
542 
543  if(rdo_words == 0) continue ;
544 
545  bsmd_t *bsmd = (bsmd_t *) adc_non_zs->request(1) ;
546 
547 // memset(bsmd,0,sizeof(bsmd_t)) ;
548 
549  // cap is 64 bytes after the start
550  bsmd->cap = d[8] & 0x7F ;
551 
552 
553  LOG(DBG,"Found cap %d",bsmd->cap) ;
554 
555  u_short *data = (u_short *)(d+10) ; // move to data start which is 10 words after
556 
557  for(int c=0;c<BSMD_DATSIZE;c++) {
558  bsmd->adc[c] = l2h16(*data++) ;
559  }
560 
561  adc_non_zs->finalize(1,0,r,bsmd->cap) ;
562  }
563 
564 
565 
566 
567 
568  }
569 
570  //LOG(NOTE,"BSMD: rdo %d: start %d, stop %d",rdo,start_r, stop_r) ;
571 
572 
573  adc_non_zs->rewind() ;
574 
575  return adc_non_zs ;
576 
577 }
578 
579 
580 daq_dta *daq_bsmd::handle_raw(int rdo)
581 {
582  struct bsmd_desc bsmd_d ;
583  int start_r, stop_r ;
584  int bytes ;
585 
586  assert(caller) ; // sanity...
587 
588  if(!present) return 0 ;
589 
590  if(rdo <= 0) {
591  start_r = 1 ;
592  stop_r = BSMD_FIBERS ;
593  }
594  else {
595  start_r = stop_r = rdo ;
596  }
597 
598  bytes = 0 ;
599 
600  if(present & DET_PRESENT_DATAP) { // in datap!
601  char *emcp = (char *)legacyDetp(rts_id, caller->mem) ;
602 
603  if(bsmd_reader(emcp, &bsmd_d)==0) return 0 ;
604 
605 
606  for(int r=start_r;r<=stop_r;r++) {
607  //LOG(NOTE,"BSMD: adc_non_zs: fiber %d, bytes %d",r,bsmd_d.bytes[r-1][0]) ;
608  bytes += bsmd_d.bytes[r-1][0] ;
609  }
610 
611  if(bytes==0) return 0 ;
612 
613  raw->create(bytes,"raw",rts_id,DAQ_DTA_STRUCT(char)) ;
614 
615  for(int r=start_r;r<=stop_r;r++) {
616  bytes = bsmd_d.bytes[r-1][0] ;
617 
618  if(bytes == 0) continue ;
619 
620  char *st = (char *) raw->request(bytes) ;
621 
622  memcpy(st,(char *)bsmd_d.dta[r-1][0],bytes) ;
623 
624  raw->finalize(bytes,0,r,0) ;
625  }
626  }
627  else if(present & DET_PRESENT_SFS) {
628  int s_new, r_new ;
629  char str[256] ;
630  char *full_name ;
631 
632 
633  for(int r=start_r;r<=stop_r;r++) {
634 
635  s_new = bsmd_old_to_new_map[r].new_sec ;
636  r_new = bsmd_old_to_new_map[r].new_rdo ;
637 
638  sprintf(str,"%s/sec%02d/rb%02d/raw",sfs_name,s_new,r_new) ;
639 
640  full_name = caller->get_sfs_name(str) ;
641 
642  if(!full_name) continue ;
643 
644  bytes += caller->sfs->fileSize(full_name) ;
645 
646  }
647 
648 
649  LOG(NOTE,"BSMD raw: total of %d bytes",bytes) ;
650  if(bytes == 0) return 0 ;
651 
652  raw->create(bytes,"bsmd_raw",rts_id,DAQ_DTA_STRUCT(char)) ;
653 
654 
655  for(int r=start_r;r<=stop_r;r++) {
656 
657  s_new = bsmd_old_to_new_map[r].new_sec ;
658  r_new = bsmd_old_to_new_map[r].new_rdo ;
659 
660  sprintf(str,"%s/sec%02d/rb%02d/raw",sfs_name,s_new,r_new) ;
661 
662  full_name = caller->get_sfs_name(str) ;
663 
664  if(!full_name) continue ;
665 
666  bytes = caller->sfs->fileSize(full_name) ;
667 
668  char *st = (char *) raw->request(bytes) ;
669  int ret = caller->sfs->read(str, st, bytes) ;
670  if(ret != bytes) {
671  LOG(ERR,"ret is %d != %d",ret,bytes) ;
672  }
673 
674  raw->finalize(bytes,s_new,r) ; // add the sector but keep the old "RDO"!
675  }
676 
677 
678 
679  }
680  else return 0 ;
681 
682 
683 
684 
685  raw->rewind() ;
686 
687  return raw ;
688 
689 }
690 
691 
692 
693 
694 int daq_bsmd::get_l2(char *buff, int words, struct daq_trg_word *trg, int rdo)
695 {
696  const int BSMD_BYTES_MIN = ((2400+10+3)*4) ; // this is the minimum
697  const int BSMD_BYTES_MAX = ((2400+10+3+30)*4) ;
698 // const u_int BSMD_VERSION = 0x8035 ; // Nov 09
699  const u_int BSMD_SIGNATURE = 0x42534D44 ; // "BSMD"
700  const u_int BSMD_HDR_ID = 5 ; // by some definiton somewhere...
701 
702  int t_cou = 0 ;
703  int bad = 0 ;
704  u_int *d32 = (u_int *)buff ;
705  int id_check_failed = 0 ;
706 
707  //HACKINTOSH!
708  words = 2413 ;
709 
710  // FIRST we check the length
711  int buff_bytes = 4 * words ;
712 
713  if((buff_bytes < BSMD_BYTES_MIN) || (buff_bytes > BSMD_BYTES_MAX)) {
714  LOG(ERR,"RDO %d: expect %d bytes, received %d",rdo,BSMD_BYTES_MIN,buff_bytes) ;
715  bad |= 1 ;
716  }
717 
718 
719  // grab crc from the last word
720  int last_ix = words - 1 ;
721  // misc signatures and errors from the header
722  if(d32[1] != BSMD_SIGNATURE) { // "BSMD"
723  LOG(ERR,"RDO %d: bad header sig 0x%08X, expect 0x%08X",rdo,d32[1], BSMD_SIGNATURE) ;
724  bad |= 1 ;
725  }
726 
727  if(d32[9] != 0xDEADFACE) { // deadface
728  LOG(ERR,"RDO %d: bad deadface 0x%08X",rdo,d32[9]) ;
729  bad |= 1 ;
730  }
731 
732 
733 /* wait for it to stabilize
734  if((d32[2] >> 16) != BSMD_VERSION) {
735  LOG(ERR,"RDO %d: bad version 0x%04X, expect 0x%04X",rdo,d32[2] >> 16, BSMD_VERSION) ;
736  bad |= 2 ; // soft error
737 
738  }
739 */
740 
741  if((d32[3] & 0xFFFF0000)) { // error_flags
742  LOG(ERR,"RDO %d: error flags 0x%04X",rdo,d32[3]>>16) ;
743  bad |= 2 ;
744  }
745 
746  if(((d32[3]>>8) & 0xFF) != BSMD_HDR_ID) { // det_id
747  LOG(ERR,"RDO %d: bad det_id 0x%02X",rdo,(d32[3]>>8)&0xFF) ;
748  bad |= 1 ;
749  }
750 
751  int rdo_in_dta = d32[3] & 0xFF ; // fiber ID via jumpers...
752  if(rdo_id[rdo] != 0xFF) { // skip the check!
753  if(rdo_id[rdo] != rdo_in_dta) {
754  id_check_failed++ ;
755  }
756  }
757 
758 
759 
760  // compare to what?
761  LOG(DBG,"RDO %d: in data %d",rdo,rdo_id) ;
762 
763 
764  int format_code = (d32[2] >> 8) & 0xFF ;
765 
766 // LOG(TERR,"%d: sig 0x%08X, dead 0x%08X, 3 0x%08X, 2 0x%08X: format code 0x%X",rdo,d32[1],d32[9],d32[3],d32[2],format_code) ;
767 
768  if(format_code == 0x02) { // null event
769  LOG(NOTE,"RDO %d: null event",rdo) ;
770 
771  trg[0].t = 4097 ;
772  trg[0].daq = 0 ;
773  trg[0].trg = 0 ;
774  trg[0].rhic = d32[4] ;
775 
776  return 1 ;
777 
778  }
779 
780 #if 1
781 #define G_CONST 0x04C11DB7
782 
783  u_int crc_in_data = d32[last_ix] ;
784  register u_int crc = 0xFFFFFFFF ;
785  if(crc_in_data) {
786  for(int i=0;i<last_ix;i++) {
787  u_int datum ;
788 
789  datum = d32[i] ;
790  register u_int data_j ;
791  register u_int crc_31 ;
792 
793  for(register int j=31;j>=0;j--) {
794  data_j = (datum >> j) & 1 ;
795  crc_31 = (crc & 0x80000000) ? 1 : 0 ;
796 
797  if(crc_31 == data_j) {
798  crc = (crc<<1) ^ G_CONST ;
799  }
800  else {
801  crc = (crc<<1) ;
802  }
803  }
804  }
805 
806  if(crc != crc_in_data) {
807  LOG(ERR,"RDO %d: CRC in data 0x%08X, CRC calculated 0x%08X",rdo,crc_in_data,crc) ;
808  //bad |= 1 ;
809  }
810  }
811  else {
812 // LOG(WARN,"RDO %d: no CRC in data",rdo) ;
813  }
814 
815  LOG(DBG,"RDO %d: CRC in data 0x%08X, CRC calculated 0x%08X",rdo,crc_in_data,crc) ;
816 #endif
817 
818 
819  // L0 part
820  t_cou = 0 ;
821  trg[0].t = d32[0] & 0xFFF ;
822  trg[0].daq = d32[2] & 0xF ;
823  trg[0].trg = (d32[2] >> 4) & 0xF ;
824  trg[0].rhic = d32[4] ;
825 
826  t_cou++ ;
827 
828 
829 // LOG(TERR,"RDO %d: token %d, trg %d, daq %d: rhic %u; words %d",rdo,
830 // trg[0].t, trg[0].trg, trg[0].daq, trg[0].rhic,words) ;
831 
832 
833  // check token and trg_cmd sanity...
834  if(trg[0].t == 0) {
835  LOG(ERR,"RDO %d: token 0?",rdo) ;
836  trg[0].t = 4097 ; // turn it to sanity!
837  bad |= 2 ;
838  }
839 
840 
841 #if 1 // skip for the temporary 0x8129 V
842 
843  // get mesg_length
844  int mesg_length = d32[last_ix-1] & 0xFFF ; // 12 bits only
845  if(mesg_length > 30) {
846  LOG(ERR,"RDO %d: bad trigger length %d",rdo,mesg_length) ;
847  // kill it! this will make the main length bad too
848  mesg_length = 0 ;
849  bad |= 2 ;
850  }
851 
852  for(int i=0;i<mesg_length;i++) {
853  u_int trg_data = d32[last_ix - 2 - i] ;
854 
855 
856 
857  trg[t_cou].t = (trg_data >> 8) & 0xFFF ;
858  trg[t_cou].trg = (trg_data >> 4) & 0xF ;
859  trg[t_cou].daq = trg_data & 0xF ;
860  trg[t_cou].rhic = (trg_data >> 20) & 0x7FF ;
861 
862 
863  // check the triggers here!
864  if(trg_data & 0x80000000) {
865  LOG(ERR,"RDO %d: FIFO limit 0x%08X",rdo,trg_data) ;
866  bad |= 2 ;
867  }
868 
869 
870  // need some sane limit here on t_cou
871  if(t_cou >= 120) {
872  LOG(ERR,"RDO %d: too many triggers %d",rdo,t_cou) ;
873  bad |= 2 ;
874  break ;
875  }
876 
877  t_cou++ ;
878  }
879 
880 
881 // int trailer_event = d32[last_ix - 2 - mesg_length] & 0xFFFF ;
882 
883 
884 // if(trailer_event != d32[0]) {
885 // LOG(ERR,"RDO %d: bad trailer event 0x%08X != header 0x%08X",rdo,trailer_event,d32[0]) ;
886 // bad |= 2 ;
887 // }
888 #endif
889 
890 
891  // check trigger command sanity...
892  switch(trg[0].trg) {
893  case 2 :
894  LOG(WARN,"RDO %d: T %04d: odd trg_cmd %d (daq_cmd %d) -- will ignore this event even if it had errors...",
895  rdo, trg[0].t, trg[0].trg, trg[0].daq) ;
896  trg[0].t = 4097 ; // sanitize...
897  bad = 0 ; // and force-clear any error....
898  break ;
899  case 4 :
900  case 8 :
901  case 11 :
902  case 12 :
903  break ;
904  default :
905  LOG(ERR,"RDO %d: T %04d: bad trg_cmd %d (daq_cmd %d)",rdo, trg[0].t, trg[0].trg, trg[0].daq) ;
906  // sanitize
907  trg[0].t = 4097 ;
908  bad |= 2 ;
909  break ;
910  }
911 
912 
913  if(bad) {
914  LOG(WARN,"RDO %d: words %d: bad %d:",rdo,words,bad) ;
915  // dump the whole header
916  for(int i=0;i<10;i++) {
917  LOG(WARN,"\tRDO %d: header %4d: 0x%08X",rdo,i,d32[i]) ;
918  }
919  // dump last 4 words of the trailer as well
920  for(int i=(words-4);i<words;i++) {
921  LOG(WARN,"\tRDO %d: trailer %4d: 0x%08X",rdo,i,d32[i]) ;
922  }
923 
924  }
925  else if(trg[0].trg==11) { // special test pattern!
926  int bad_cou = 0;
927  int shutup = 0 ;
928  for(int i=10;i<2410;i++) {
929  u_int should ;
930  u_int b31, b21, b1, b0 ;
931 
932  b31 = (t_data >> 31) & 1 ;
933  b21 = (t_data >> 21) & 1 ;
934  b1 = (t_data >> 1) & 1 ;
935  b0 = (t_data >> 0) & 1 ;
936 
937  should = !(b31 ^ b21 ^ b1 ^b0) ;
938  should = (t_data << 1) | should ;
939 
940  if(d32[i] != t_data) {
941  if(!shutup) LOG(WARN,"word %4d: should 0x%08X, is 0x%08X",i,t_data,d32[i]) ;
942  bad_cou++ ;
943  }
944 
945  if(bad_cou > 2) shutup = 1 ;
946 
947  should = !(b31 ^ b21 ^ b1 ^b0) ;
948  should = (t_data << 1) | should ;
949 
950  t_data = should ;
951 
952  }
953 
954  if(bad_cou) LOG(ERR,"RDO %d: bad locations %d",rdo,bad_cou) ;
955  }
956 
957  if(bad & 1) { // critical errors
958  //return 0 ; // no trigger!
959 
960  trg[0].t = 4097 ;
961  trg[0].trg = 0 ;
962  trg[0].daq = 0 ;
963  trg[0].rhic = 0 ;
964 
965  return 1 ;
966  }
967  else {
968 
969  if(id_check_failed) {
970  rdo_warns[rdo]++ ;
971  if(rdo_warns[rdo] < 5) {
972  if(rdo_id[rdo]==0x5 || rdo_id[rdo]==0xD) {
973  LOG(ERR,"RDO %d: rdo check failed: expect 0x%02X, found 0x%02X",
974  rdo,rdo_id[rdo],rdo_in_dta) ;
975  }
976  else {
977  LOG(CAUTION,"RDO %d: rdo check failed: expect 0x%02X, found 0x%02X",
978  rdo,rdo_id[rdo],rdo_in_dta) ;
979  }
980  }
981  }
982 
983 /*
984  rdo_warns[rdo]++ ;
985  if(rdo_warns[rdo]<2) {
986  LOG(TERR,"RDO %d: rdo check: expect 0x%02X, found 0x%02X",
987  rdo,rdo_id[rdo],rdo_in_dta) ;
988  }
989 */
990 
991  return t_cou ;
992  }
993 }
void version(std::ostream &os=std::cout)
print HepMC version
Definition: Version.h:27