7 #include <rtsSystems.h>
10 #define TRG_VERSION 0x32
11 #include <daqFormats.h>
12 #include <trgStructures.h>
25 static int readBTOW(u_short *_data,
int token);
26 static int readETOW(u_short *_data,
int token);
62 static struct emc_describe_t {
74 { BTOW_ID, CHAR_EMCP, CHAR_EMCSECP, CHAR_EMCRBP, CHAR_EMCADCR, CHAR_EMCADCD, BTOW_INDEX, 0, 0 },
75 { BSMD_ID, CHAR_EMCP, CHAR_EMCSECP, CHAR_EMCRBP, CHAR_EMCADCR, CHAR_EMCADCD, -1 , 0, 1},
76 { ETOW_ID, CHAR_EECP, CHAR_EECSECP, CHAR_EECRBP, CHAR_EECADCR, CHAR_EECADCD, ETOW_INDEX, 1, 0 },
77 { ESMD_ID, CHAR_EECP, CHAR_EECSECP, CHAR_EECRBP, CHAR_EECADCR, CHAR_EECADCD, -1, 1, 1 },
81 int emc_reader(
char *m,
struct emc_t *emc, u_int driver,
int rts_id,
char *ptrs[12],
int f_bytes[12])
100 if(datap == 0)
return 0 ;
101 if(f_bytes) memset(f_bytes,0,
sizeof(f_bytes)) ;
104 u_char emc_wanted[4] ;
107 for(
int i=0;i<4;i++) {
108 if(emc_describe[i].
id == rts_id) {
117 memset(emc,0,
sizeof(
emc_t)) ;
119 emc_p->btow_max_ch = BTOW_MAXFEE*BTOW_DATSIZE ;
120 emc_p->bsmd_max_ch = 12*4800 ;
121 emc_p->etow_max_ch = ETOW_MAXFEE*ETOW_DATSIZE ;
122 emc_p->esmd_max_ch = ESMD_MAXFEE*ESMD_DATSIZE ;
124 memset(emc_wanted,0x1,
sizeof(emc_wanted)) ;
130 for(
int i=0;i<4;i++) {
132 if(emc_wanted[i] == 0) continue ;
138 dta_ptr = getEmcTrgData((
char *)datap, emc_describe[i].trg_index, &bytes);
145 goto found_specific_emc ;
151 if(emc_describe[i].type==0) {
154 len = qswap32(swapdatap, datap->det[
id].len) * 4 ;
155 off = qswap32(swapdatap, datap->det[
id].off) ;
156 if((len == 0) || (off == 0)) {
164 len = qswap32(swapdatap, datap->det[EXT_ID].len) ;
165 off = qswap32(swapdatap, datap->det[EXT_ID].off) ;
167 LOG(DBG,
"Checking for datapx: len=%d off=%d",len,off);
169 if((len == 0) || (off == 0)) {
173 datapx = (
struct DATAPX *)(m + off*4) ;
176 if(checkBank(datapx->bh.bank_type, CHAR_DATAPX) < 0) {
181 if(datapx->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapdatapx = 1;
183 len = qswap32(swapdatapx, datapx->det[
id-10].len) * 4 ;
184 off = qswap32(swapdatapx, datapx->det[
id-10].off) ;
186 LOG(DBG,
"Checking for EEC: len=%d off=%d",len,off);
188 if((len == 0) || (off == 0)) {
197 emcp = (
struct EMCP *)((u_int *)m + off) ;
199 if(checkBank(emcp->bh.bank_type,emc_describe[i].p) < 0) {
204 if(emcp->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapemcp = 1;
206 int token = qswap32(swapemcp, emcp->bh.token) ;
208 char *name = rts2name(emc_describe[i].
id) ;
209 int sub = emc_describe[i].sub ;
214 len = qswap32(swapemcp, emcp->sec[sub].len) ;
215 off = qswap32(swapemcp, emcp->sec[sub].off) ;
217 if(!len || !off) continue ;
219 emcsecp = (
struct EMCSECP *)((u_int *)emcp + off) ;
221 if(checkBank(emcsecp->bh.bank_type,emc_describe[i].secp) <= 0) {
226 int fibers = (b2h32(emcsecp->bh.length) - 10) / 2 ;
229 LOG(ERR,
"%s: too many fibers %d > %d",name,fibers,12) ;
234 LOG(DBG,
"EMC %s: %d fibers possible",name,fibers) ;
236 for(
int j=0;j<fibers;j++) {
237 len = b2h32(emcsecp->fiber[j].len) ;
238 off = b2h32(emcsecp->fiber[j].off) ;
240 if(!len || !off) continue ;
242 emcrbp = (
struct EMCRBP *)((u_int *)emcsecp + off) ;
244 if(checkBank(emcrbp->bh.bank_type,emc_describe[i].rbp) <= 0) {
251 int banks = (b2h32(emcrbp->bh.length) - 10) /2 ;
254 LOG(ERR,
"%s: fiber %d: too many banks %d > %d",name,j,banks,3) ;
259 LOG(DBG,
"EMC %s: fiber %d: %d banks used",name,j,banks) ;
269 memset(b_ptrs,0,
sizeof(b_ptrs)) ;
270 memset(b_bytes,0,
sizeof(b_bytes)) ;
272 for(
int k=0;k<banks;k++) {
273 len = b2h32(emcrbp->banks[k].len) ;
274 off = b2h32(emcrbp->banks[k].off) ;
276 if(!len || !off) continue ;
279 dta_ptr = 40 + (
char *)emcrbp + off*4 ;
281 dummy = (
DUMMYDATA *) ((u_int *)emcrbp + off) ;
284 b_ptrs[k] = dta_ptr ;
289 if(checkBank(dummy->bh.bank_type, emc_describe[i].adcr)<0) {
296 if(checkBank(dummy->bh.bank_type,
"EMCADCD")<0) {
303 if(checkBank(dummy->bh.bank_type,
"EMCPEDR")<0) {
310 LOG(ERR,
"%s: fiber %d: unknown bank at %d",name,j,k) ;
318 if(asdasd & (1<<2)) {
319 f_bytes[j] = b_bytes[1] ;
320 ptrs[j] = b_ptrs[1] ;
322 else if(b_bytes[2]) {
323 f_bytes[j] = b_bytes[2] ;
324 ptrs[j] = b_ptrs[1] ;
327 f_bytes[j] = b_bytes[0] ;
328 ptrs[j] = b_ptrs[0] ;
336 if(found <0) continue ;
354 if(found<0)
return 0 ;
366 emcadcr = (
struct DUMMYDATA *)((u_int *)emcrbp + off) ;
367 if(checkBank(emcadcr->bh.bank_type,adcr) < 0) {
373 emcadcd = (
struct DUMMYDATA *)((u_int *)emcrbp + off) ;
374 if(checkBank(emcadcd->bh.bank_type,adcd) < 0) {
380 LOG(ERR,
"Unknown subbank %d in EMCRBP!",k,0,0,0,0) ;
394 if((type==0) && (i == EMC_B_TOW)) {
396 LOG(ERR,
"Reading BTOW data from DAQ banks but already read it from trigger banks");
400 if((rts_id == BTOW_ID) && (emc==0)) {
401 ptrs[0] = (
char *) emcadcr + 40 ;
402 f_bytes[0] = l2h32(emcadcr->bh.length)*4 - 40 ;
406 readBTOW((u_short *)((u_int)emcadcr + 40), token);
409 else if((type==0) && (i == EMC_B_SMD)) {
421 data = (u_short *) ((u_int) emcadcr + 40 + 4 + 256) ;
424 emc_p->bsmd_cap[j] = *(u_char *)((u_int)emcadcr + 40 + 4 + 4*16) ;
426 emc_p->bsmd_raw_in = 1 ;
427 for(l=0;l<4800;l++) {
428 emc_p->bsmd_raw[j][l] = l2h16(*data++) ;
432 for(l=0;l<4800;l++) {
433 emc_p->bsmd[j][l] = l2h16(*data++) ;
434 if(emc_p->bsmd[j][l] > 0) emc_p->bsmd_ch++ ;
439 LOG(DBG,
"BSMD raw data present. Sent to the bsmd_raw bank? -- %s",(emc_p->bsmd_raw_in?
"Yes":
"No")) ;
444 int datums = b2h32(emcadcd->bh.length)-10-1 ;
446 LOG(DBG,
"BSMD ZS data present, %d hits",datums) ;
448 data = (u_short *) ((uint) emcadcd + 40) ;
449 LOG(DBG,
"local fiber %d, channels %d [== %d]",b2h16(data[0]),b2h16(data[1]),datums) ;
452 for(l=0;l<datums;l++) {
453 int ch = b2h16(*data++) ;
454 int adc = b2h16(*data++) ;
456 emc_p->bsmd[j][ch] = adc ;
464 emc_p->bsmd_ch += datums ;
468 else if((type==1) && (i == EMC_B_TOW)) {
471 LOG(ERR,
"Reading ETOW data in the DAQ banks, but already read it from the trigger banks.");
476 if((rts_id == ETOW_ID) && (emc == 0)) {
477 ptrs[0] = (
char *) emcadcr + 40 ;
478 f_bytes[0] = l2h32(emcadcr->bh.length)*4 - 40 ;
482 readETOW((u_short *)((u_int)emcadcr + 40), token);
485 else if((type==1) && (i == EMC_B_SMD)) {
496 data = (u_short *) ((u_int) emcadcr + 40 + 4 + 4) ;
498 data = (u_short *) ((u_int) emcadcr + 40 + 4 + 6) ;
501 local_token = thi * 256 + tlo ;
503 if(token != local_token) {
504 LOG(ERR,
"ESMD: Token in bank %d different from token in data %d",token,local_token,0,0,0) ;
507 ptrs[0] = (
char *) emcadcr + 40 ;
508 f_bytes[0] = l2h32(emcadcr->bh.length)*4 - 40 ;
509 if((rts_id == ESMD_ID) && (emc == 0))
return 1 ;
512 data = (u_short *) ((u_int) emcadcr + 40 + 4 + 128) ;
517 if(l2h32(emcadcr->bh.length) < 3000) {
518 emc_p->esmd_max_fee = 30 ;
521 emc_p->esmd_max_fee = 48 ;
524 emc_p->esmd_max_ch = emc_p->esmd_max_fee*ETOW_DATSIZE ;
528 for(m=0;m<ESMD_PRESIZE;m++) {
529 for(l=0;l<emc_p->esmd_max_fee;l++) {
530 emc_p->
esmd_pre[l][m] = l2h16(*data++) ;
534 for(m=0;m<ESMD_DATSIZE;m++) {
535 for(l=0;l<emc_p->esmd_max_fee;l++) {
536 emc_p->esmd[l][m] = l2h16(*data++) ;
537 if(emc_p->esmd[l][m] > 0) emc_p->esmd_ch++ ;
558 int readBTOW(u_short *_data,
int token)
562 int thi, tlo, local_token;
566 data = (u_short *)((
char *)_data + 4 + 4);
568 data = (u_short *)((
char *)_data + 4 + 6);
570 data = (u_short *)((
char *)_data + 4 + 128);
572 local_token = thi * 256 + tlo ;
574 if(token != local_token) {
575 LOG(ERR,
"BTOW: Event token different from token in BTOW data %d vs %d (%d,%d)",token,local_token,thi,tlo,0) ;
579 data = (u_short *)((
char *)_data + 4 + 128);
580 emc_p->btow_raw = data ;
583 for(m=0;m<BTOW_PRESIZE;m++) {
584 for(l=0;l<BTOW_MAXFEE;l++) {
585 emc_p->btow_pre[l][m] = l2h16(*data++) ;;
589 for(m=0;m<BTOW_DATSIZE;m++) {
590 for(l=0;l<BTOW_MAXFEE;l++) {
591 emc_p->btow_new[l][m] = l2h16(*data++) ;
596 data = emc_p->btow_raw ;
599 for(l=0;l<4800;l++) {
600 emc_p->btow[l] = l2h16(*data++) ;
601 if(emc_p->btow[l] > 0) emc_p->btow_ch++ ;
614 int readETOW(u_short *_data,
int token) {
627 data = (u_short *)((
char *)_data + 4 + 4);
629 data = (u_short *)((
char *)_data + 4 + 6);
631 data = (u_short *)((
char *)_data + 4 + 128);
633 local_token = thi * 256 + tlo ;
635 if(token != local_token) {
636 LOG(ERR,
"ETOW: Event token different from token in ETOW data %d vs %d (%d,%d)",token,local_token,thi,tlo,0) ;
639 emc_p->etow_raw = data ;
642 for(m=0;m<ETOW_PRESIZE;m++) {
643 for(l=0;l<ETOW_MAXFEE;l++) {
644 emc_p->etow_pre[l][m] = l2h16(*data++) ;;
649 for(m=0;m<ETOW_DATSIZE;m++) {
650 for(l=0;l<ETOW_MAXFEE;l++) {
651 emc_p->etow[l][m] = l2h16(*data++) ;
652 if(emc_p->etow[l][m] > 0) emc_p->etow_ch++ ;
662 char *getEmcTrgData(
char *input,
int idx,
int *bytes)
672 if(idx < 0)
return 0 ;
673 if(datap == 0)
return 0 ;
680 LOG(DBG,
"Starting getEmcTrgData 0x%x",datap);
682 if(datap->bh.byte_order != DAQ_RAW_FORMAT_ORDER)
685 len = qswap32(swapdatap, datap->det[TRG_ID].len);
686 if(len == 0)
return NULL;
689 off = qswap32(swapdatap, datap->det[TRG_ID].off);
690 if(off == 0)
return NULL;
693 ptr = (UINT32 *)datap;
694 trgp = (
struct TRGP *)(ptr + off) ;
695 if(checkBank(trgp->bh.bank_type,
"TRGP") < 0) {
699 if(trgp->bh.byte_order != DAQ_RAW_FORMAT_ORDER)
702 if(trgp->bh.token == 0) {
703 LOG(DBG,
"Token 0 - skipping...",0,0,0,0,0) ;
707 if(trgp->trgData.len == 0)
return NULL;
708 if(trgp->trgData.off == 0)
return NULL;
710 off = qswap32(swaptrgp, trgp->trgData.off);
712 trgd = (
struct TRGD *)(ptr + off) ;
714 if(trgd->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swaptrgd = 1;
717 if(checkBank(trgd->bh.bank_type,
"TRGD") < 0) {
721 LOG(DBG,
"TRGD len %d",
722 qswap32(swaptrgd, trgd->bh.length),0,0,0) ;
724 if(qswap32(swaptrgd, trgd->bh.length) == 10)
return NULL;
727 u_char trg_version = trgd->desc.TrgDataFmtVer ;
731 switch(trg_version) {
734 LOG(NOTE,
"TRG transverse version 0x%02x, EMC data supported", trg_version);
742 off = qswap32(swaptrgd, trgtowertrnfer->OffsetBlock[idx].offset);
743 len = qswap32(swaptrgd, trgtowertrnfer->OffsetBlock[idx].length);
747 LOG(NOTE,
"IDX = %d offset = %d length = %d",
751 if(idx == BTOW_INDEX) {
753 if(len != (64 + 2 + 30*164)*2) {
754 LOG(ERR,
"Have BTOW in event, but data length incorrect: len=%d not %d, ignoring data...",
755 len, (64 + 2 + 30 * 164) * 2);
759 LOG(NOTE,
"Have BTOW data in trigger banks: off=%d, len=%d", off, len);
761 else if (idx == ETOW_INDEX) {
763 if(len != (64 + 2 + 6*164)*2) {
764 LOG(ERR,
"Have ETOW in event, but data length incorrect: len=%d not %d. Ignoring data...",len,
769 LOG(NOTE,
"Have ETOW data in trigger banks: off=%d, len=%d", off, len);
779 LOG(NOTE,
"Trigger transfer version 0x%x... no EMC data in trigger banks.", trg_version);
u_short * esmd_raw
ESMD preamble.
u_short esmd_pre[ESMD_MAXFEE][ESMD_PRESIZE]
ADC data.