6 #include <rtsSystems.h>
10 #include <SFS/sfs_index.h>
11 #include <DAQ_READER/daqReader.h>
12 #include <DAQ_READER/daq_dta.h>
17 const char *daq_bsmd::help_string =
"BSMD\n\
18 adc returns bsmd_t;\n\
19 raw returns raw data\n" ;
25 daq_det_factory::det_factories[BSMD_ID] = this ;
35 static const struct bsmd_old_to_new_map_t {
38 } bsmd_old_to_new_map[13] = {
60 name = rts2name(rts_id) ;
65 if(caller) caller->insert(
this, rts_id) ;
74 LOG(DBG,
"%s: constructor: caller %p",name,rts_caller) ;
80 LOG(DBG,
"%s: DEstructor",name) ;
94 daq_dta *daq_bsmd::get(
const char *bank,
int sec,
int row,
int pad,
void *p1,
void *p2)
97 if(!present)
return 0 ;
99 LOG(DBG,
"got something") ;
101 if(strcasecmp(bank,
"adc")==0) {
102 return handle_adc(row) ;
104 else if(strcasecmp(bank,
"adc_non_zs")==0) {
105 return handle_adc_non_zs(row) ;
107 else if(strcasecmp(bank,
"ped")==0) {
108 return handle_ped_rms(row,1) ;
110 else if(strcasecmp(bank,
"rms")==0) {
111 return handle_ped_rms(row,0) ;
113 else if(strcasecmp(bank,
"raw")==0) {
114 return handle_raw(row) ;
118 LOG(ERR,
"%s: unknown bank type \"%s\"",name,bank) ;
123 daq_dta *daq_bsmd::handle_adc(
int rdo)
126 int start_r, stop_r ;
131 LOG(DBG,
"%s: present 0x%X",name,present) ;
133 if(!present)
return 0 ;
137 stop_r = BSMD_FIBERS ;
140 start_r = stop_r = rdo ;
144 memset(&bsmd_d,0,
sizeof(bsmd_d)) ;
146 if(present & DET_PRESENT_DATAP) {
147 char *emcp = (
char *)legacyDetp(rts_id, caller->mem) ;
149 if(bsmd_reader(emcp, &bsmd_d)==0)
return 0 ;
151 for(
int r=start_r;r<=stop_r;r++) {
152 bytes += bsmd_d.bytes[r-1][1] ;
161 for(
int r=start_r;r<=stop_r;r++) {
164 s_new = bsmd_old_to_new_map[r].new_sec ;
165 r_new = bsmd_old_to_new_map[r].new_rdo ;
167 sprintf(str,
"%s/sec%02d/rb%02d/adc",sfs_name,s_new,r_new) ;
169 full_name = caller->get_sfs_name(str) ;
171 if(!full_name) continue ;
173 l_bytes = caller->sfs->fileSize(full_name) ;
174 if(!l_bytes) continue ;
176 bsmd_d.bytes[r-1][1] = l_bytes ;
185 LOG(DBG,
"rdo %d: bytes %d",rdo,bytes) ;
187 if(bytes==0)
return 0 ;
189 adc->create(bytes,
"adc",rts_id,DAQ_DTA_STRUCT(
bsmd_t)) ;
191 u_short *data_alloc ;
194 if(present & DET_PRESENT_DATAP) {
200 malloced_bytes = (2400+10+3+100)*4 *2 ;
201 data_alloc = (u_short *)malloc(malloced_bytes) ;
206 for(
int r=start_r;r<=stop_r;r++) {
211 if(bsmd_d.bytes[r-1][1] == 0) continue ;
213 if(present & DET_PRESENT_DATAP) {
215 data = (u_short *)(bsmd_d.dta[r-1][1]) ;
222 s_new = bsmd_old_to_new_map[r].new_sec ;
223 r_new = bsmd_old_to_new_map[r].new_rdo ;
225 sprintf(str,
"%s/sec%02d/rb%02d/adc",sfs_name,s_new,r_new) ;
227 full_name = caller->get_sfs_name(str) ;
229 if(!full_name) continue ;
231 bytes = caller->sfs->fileSize(full_name) ;
233 if(bytes > malloced_bytes) {
234 LOG(ERR,
"Too big %s is %d",str,bytes) ;
239 caller->sfs->read(str, (
char *)data, bytes) ;
241 bsmd_d.endian[r-1][1] = 0 ;
246 memset(bsmd,0,
sizeof(
bsmd_t)) ;
249 if(bsmd_d.endian[r-1][1]) {
250 version = b2h16(data[0]) ;
251 count = b2h16(data[1]) ;
252 bsmd->cap = b2h16(data[2]) ;
253 fiber = b2h16(data[3]) ;
256 LOG(NOTE,
"%s: fiber %d[%d]: count %d, cap %d, version 0x%04X",name,r,fiber,count,bsmd->cap,version) ;
260 for(
int c=0;c<count;c++) {
261 int ch = b2h16(*data++) ;
262 int adc = b2h16(*data++) ;
264 bsmd->adc[ch] = adc ;
268 version = l2h16(data[0]) ;
269 count = l2h16(data[1]) ;
270 bsmd->cap = l2h16(data[2]) ;
271 fiber = l2h16(data[3]) ;
274 LOG(NOTE,
"%s: fiber %d[%d]: count %d, cap %d, version 0x%04X",name,r,fiber,count,bsmd->cap,version) ;
278 for(
int c=0;c<count;c++) {
279 int ch = l2h16(*data++) ;
280 int adc = l2h16(*data++) ;
282 bsmd->adc[ch] = adc ;
288 adc->finalize(1,0,r,bsmd->cap) ;
293 if(malloced_bytes) free(data_alloc) ;
299 daq_dta *daq_bsmd::handle_ped_rms(
int rdo,
int is_ped)
301 int start_r, stop_r ;
308 stop_r = BSMD_FIBERS ;
311 start_r = stop_r = rdo ;
315 int want_sec[3] = { 0, 0, 0 } ;
316 u_short *pedrms[3] = { 0, 0, 0 } ;
318 for(
int r=start_r;r<=stop_r;r++) {
321 s_new = bsmd_old_to_new_map[r].new_sec ;
322 r_new = bsmd_old_to_new_map[r].new_rdo ;
324 want_sec[s_new] = 1 ;
330 if(present & DET_PRESENT_DATAP) {
331 char *emcp = (
char *)legacyDetp(rts_id, caller->mem) ;
333 if(bsmd_reader(emcp, &bsmd_d)==0)
return 0 ;
336 for(
int r=start_r;r<=stop_r;r++) {
337 bytes += bsmd_d.bytes[r-1][2] ;
343 for(
int s=1;s<=2;s++) {
348 if(want_sec[s] == 0) continue ;
350 sprintf(str,
"%s/sec%02d/pedrms",sfs_name,s) ;
352 full_name = caller->get_sfs_name(str) ;
354 if(!full_name) continue ;
356 l_bytes = caller->sfs->fileSize(full_name) ;
361 pedrms[s] = (u_short *) malloc(l_bytes) ;
363 caller->sfs->read(str, (
char *)(pedrms[s]), l_bytes) ;
373 sprintf(str,
"%s/pedrms",sfs_name) ;
375 full_name = caller->get_sfs_name(str) ;
377 LOG(TERR,
"FY13 bug -- Got %s for RDO%d, ped %d?!",str,rdo,is_ped) ;
382 LOG(DBG,
"rdo %d: bytes %d",rdo,bytes) ;
384 if(bytes==0)
return 0 ;
389 dta_use->create(1,
"bsmd_ped",rts_id,DAQ_DTA_STRUCT(
bsmd_t)) ;
393 dta_use->create(1,
"bsmd_rms",rts_id,DAQ_DTA_STRUCT(
bsmd_t)) ;
399 LOG(DBG,
"doing rdos: %d-%d",start_r,stop_r) ;
401 for(
int r=start_r;r<=stop_r;r++) {
404 if(present & DET_PRESENT_DATAP) {
405 if(bsmd_d.bytes[r-1][2] == 0) continue ;
407 data = (u_short *)(bsmd_d.dta[r-1][2]) ;
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 ;
416 data = pedrms[s_new] + ((4+128*4800)*(r_new-1)) ;
418 bsmd_d.endian[r-1][2] = 0 ;
422 LOG(DBG,
"BSMD PEDR: rdo %d: 0x%04X 0x%04X 0x%04X 0x%04X",r,data[0],data[1],data[2],data[3]) ;
428 for(
int c=0;c<128;c++) {
431 for(
int ch=0;ch<4800;ch++) {
433 if(bsmd_d.endian[r-1][2]) {
434 adc = b2h16(*data++) ;
437 adc = l2h16(*data++) ;
440 if(is_ped) adc &= 0x3FF ;
445 bsmd->adc[ch] = adc ;
447 dta_use->finalize(1,0,r,c) ;
453 for(
int s=1;s<=2;s++) {
454 if(pedrms[s]) free(pedrms[s]) ;
464 daq_dta *daq_bsmd::handle_adc_non_zs(
int rdo)
467 int start_r, stop_r ;
472 if(!present)
return 0 ;
476 stop_r = BSMD_FIBERS ;
479 start_r = stop_r = rdo ;
484 if(present & DET_PRESENT_DATAP) {
485 char *emcp = (
char *)legacyDetp(rts_id, caller->mem) ;
487 if(bsmd_reader(emcp, &bsmd_d)==0)
return 0 ;
490 for(
int r=start_r;r<=stop_r;r++) {
492 bytes += bsmd_d.bytes[r-1][0] ;
495 if(bytes==0)
return 0 ;
497 adc_non_zs->create(bytes,
"adc_nzs",rts_id,DAQ_DTA_STRUCT(
bsmd_t)) ;
499 for(
int r=start_r;r<=stop_r;r++) {
501 if(bsmd_d.bytes[r-1][0] == 0) continue ;
508 bsmd->cap = *(
char *)((
char *)bsmd_d.dta[r-1][0] + 4 + 4*16) ;
510 LOG(DBG,
"Found cap %d",bsmd->cap) ;
512 u_short *data = (u_short *)((
char *)bsmd_d.dta[r-1][0] + 4 + 256) ;
514 for(
int c=0;c<BSMD_DATSIZE;c++) {
515 bsmd->adc[c] = l2h16(*data++) ;
518 adc_non_zs->finalize(1,0,r,bsmd->cap) ;
525 for(
int r=start_r;r<=stop_r;r++) {
530 adc_non_zs->create(bytes,
"adc_nzs",rts_id,DAQ_DTA_STRUCT(
bsmd_t)) ;
532 for(
int r=start_r;r<=stop_r;r++) {
537 if(dd->iterate() == 0)
continue ;
540 u_int *d = (u_int *)dd->Void ;
541 u_int rdo_words = dd->ncontent ;
543 if(rdo_words == 0) continue ;
550 bsmd->cap = d[8] & 0x7F ;
553 LOG(DBG,
"Found cap %d",bsmd->cap) ;
555 u_short *data = (u_short *)(d+10) ;
557 for(
int c=0;c<BSMD_DATSIZE;c++) {
558 bsmd->adc[c] = l2h16(*data++) ;
561 adc_non_zs->finalize(1,0,r,bsmd->cap) ;
573 adc_non_zs->rewind() ;
580 daq_dta *daq_bsmd::handle_raw(
int rdo)
583 int start_r, stop_r ;
588 if(!present)
return 0 ;
592 stop_r = BSMD_FIBERS ;
595 start_r = stop_r = rdo ;
600 if(present & DET_PRESENT_DATAP) {
601 char *emcp = (
char *)legacyDetp(rts_id, caller->mem) ;
603 if(bsmd_reader(emcp, &bsmd_d)==0)
return 0 ;
606 for(
int r=start_r;r<=stop_r;r++) {
608 bytes += bsmd_d.bytes[r-1][0] ;
611 if(bytes==0)
return 0 ;
613 raw->create(bytes,
"raw",rts_id,DAQ_DTA_STRUCT(
char)) ;
615 for(
int r=start_r;r<=stop_r;r++) {
616 bytes = bsmd_d.bytes[r-1][0] ;
618 if(bytes == 0) continue ;
620 char *st = (
char *) raw->request(bytes) ;
622 memcpy(st,(
char *)bsmd_d.dta[r-1][0],bytes) ;
624 raw->finalize(bytes,0,r,0) ;
627 else if(present & DET_PRESENT_SFS) {
633 for(
int r=start_r;r<=stop_r;r++) {
635 s_new = bsmd_old_to_new_map[r].new_sec ;
636 r_new = bsmd_old_to_new_map[r].new_rdo ;
638 sprintf(str,
"%s/sec%02d/rb%02d/raw",sfs_name,s_new,r_new) ;
640 full_name = caller->get_sfs_name(str) ;
642 if(!full_name) continue ;
644 bytes += caller->sfs->fileSize(full_name) ;
649 LOG(NOTE,
"BSMD raw: total of %d bytes",bytes) ;
650 if(bytes == 0)
return 0 ;
652 raw->create(bytes,
"bsmd_raw",rts_id,DAQ_DTA_STRUCT(
char)) ;
655 for(
int r=start_r;r<=stop_r;r++) {
657 s_new = bsmd_old_to_new_map[r].new_sec ;
658 r_new = bsmd_old_to_new_map[r].new_rdo ;
660 sprintf(str,
"%s/sec%02d/rb%02d/raw",sfs_name,s_new,r_new) ;
662 full_name = caller->get_sfs_name(str) ;
664 if(!full_name) continue ;
666 bytes = caller->sfs->fileSize(full_name) ;
668 char *st = (
char *) raw->request(bytes) ;
669 int ret = caller->sfs->read(str, st, bytes) ;
671 LOG(ERR,
"ret is %d != %d",ret,bytes) ;
674 raw->finalize(bytes,s_new,r) ;
694 int daq_bsmd::get_l2(
char *buff,
int words,
struct daq_trg_word *trg,
int rdo)
696 const int BSMD_BYTES_MIN = ((2400+10+3)*4) ;
697 const int BSMD_BYTES_MAX = ((2400+10+3+30)*4) ;
699 const u_int BSMD_SIGNATURE = 0x42534D44 ;
700 const u_int BSMD_HDR_ID = 5 ;
704 u_int *d32 = (u_int *)buff ;
705 int id_check_failed = 0 ;
711 int buff_bytes = 4 * words ;
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) ;
720 int last_ix = words - 1 ;
722 if(d32[1] != BSMD_SIGNATURE) {
723 LOG(ERR,
"RDO %d: bad header sig 0x%08X, expect 0x%08X",rdo,d32[1], BSMD_SIGNATURE) ;
727 if(d32[9] != 0xDEADFACE) {
728 LOG(ERR,
"RDO %d: bad deadface 0x%08X",rdo,d32[9]) ;
741 if((d32[3] & 0xFFFF0000)) {
742 LOG(ERR,
"RDO %d: error flags 0x%04X",rdo,d32[3]>>16) ;
746 if(((d32[3]>>8) & 0xFF) != BSMD_HDR_ID) {
747 LOG(ERR,
"RDO %d: bad det_id 0x%02X",rdo,(d32[3]>>8)&0xFF) ;
751 int rdo_in_dta = d32[3] & 0xFF ;
752 if(rdo_id[rdo] != 0xFF) {
753 if(rdo_id[rdo] != rdo_in_dta) {
761 LOG(DBG,
"RDO %d: in data %d",rdo,rdo_id) ;
764 int format_code = (d32[2] >> 8) & 0xFF ;
768 if(format_code == 0x02) {
769 LOG(NOTE,
"RDO %d: null event",rdo) ;
774 trg[0].rhic = d32[4] ;
781 #define G_CONST 0x04C11DB7
783 u_int crc_in_data = d32[last_ix] ;
784 register u_int crc = 0xFFFFFFFF ;
786 for(
int i=0;i<last_ix;i++) {
790 register u_int data_j ;
791 register u_int crc_31 ;
793 for(
register int j=31;j>=0;j--) {
794 data_j = (datum >> j) & 1 ;
795 crc_31 = (crc & 0x80000000) ? 1 : 0 ;
797 if(crc_31 == data_j) {
798 crc = (crc<<1) ^ G_CONST ;
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) ;
815 LOG(DBG,
"RDO %d: CRC in data 0x%08X, CRC calculated 0x%08X",rdo,crc_in_data,crc) ;
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] ;
835 LOG(ERR,
"RDO %d: token 0?",rdo) ;
841 #if 1 // skip for the temporary 0x8129 V
844 int mesg_length = d32[last_ix-1] & 0xFFF ;
845 if(mesg_length > 30) {
846 LOG(ERR,
"RDO %d: bad trigger length %d",rdo,mesg_length) ;
852 for(
int i=0;i<mesg_length;i++) {
853 u_int
trg_data = d32[last_ix - 2 - i] ;
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 ;
864 if(trg_data & 0x80000000) {
865 LOG(ERR,
"RDO %d: FIFO limit 0x%08X",rdo,trg_data) ;
872 LOG(ERR,
"RDO %d: too many triggers %d",rdo,t_cou) ;
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) ;
905 LOG(ERR,
"RDO %d: T %04d: bad trg_cmd %d (daq_cmd %d)",rdo, trg[0].t, trg[0].trg, trg[0].daq) ;
914 LOG(WARN,
"RDO %d: words %d: bad %d:",rdo,words,bad) ;
916 for(
int i=0;i<10;i++) {
917 LOG(WARN,
"\tRDO %d: header %4d: 0x%08X",rdo,i,d32[i]) ;
920 for(
int i=(words-4);i<words;i++) {
921 LOG(WARN,
"\tRDO %d: trailer %4d: 0x%08X",rdo,i,d32[i]) ;
925 else if(trg[0].trg==11) {
928 for(
int i=10;i<2410;i++) {
930 u_int b31, b21, b1, b0 ;
932 b31 = (t_data >> 31) & 1 ;
933 b21 = (t_data >> 21) & 1 ;
934 b1 = (t_data >> 1) & 1 ;
935 b0 = (t_data >> 0) & 1 ;
937 should = !(b31 ^ b21 ^ b1 ^b0) ;
938 should = (t_data << 1) | should ;
940 if(d32[i] != t_data) {
941 if(!shutup) LOG(WARN,
"word %4d: should 0x%08X, is 0x%08X",i,t_data,d32[i]) ;
945 if(bad_cou > 2) shutup = 1 ;
947 should = !(b31 ^ b21 ^ b1 ^b0) ;
948 should = (t_data << 1) | should ;
954 if(bad_cou) LOG(ERR,
"RDO %d: bad locations %d",rdo,bad_cou) ;
969 if(id_check_failed) {
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) ;
977 LOG(CAUTION,
"RDO %d: rdo check failed: expect 0x%02X, found 0x%02X",
978 rdo,rdo_id[rdo],rdo_in_dta) ;
void version(std::ostream &os=std::cout)
print HepMC version