7 #include "rtsSystems.h"
8 #include "daqFormats.h"
11 #include "evpSupport.h"
12 #include "emcReader.h"
15 #include "StMessMgr.h"
18 using namespace OLDEVP;
22 #define SWAP32(bk,x) ((bk->bh.byte_order==0x4030201)?(bk->x):swap32(bk->x))
30 #define swap16(x) bswap_16(x)
31 #define swap32(x) bswap_32(x)
35 extern inline unsigned short swap16(
unsigned short x)
37 return ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)) ;
40 extern inline unsigned int swap32(
unsigned int x)
42 return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
43 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) ;
48 #define qswap16(test,x) ((test)?swap16(x):(x))
49 #define qswap32(test,x) ((test)?swap32(x):(x))
51 static inline unsigned int bswap(
unsigned int x)
59 memset(
this,0,
sizeof(
emc_t));
60 fenceA=fenceB=fenceC=fenceD=fenceE=fenceF=fenceG=fenceH=1946;
63 bsmd_max_ch = EMC_FIBER_NUM*4800 ;
67 etow_max_ch = ETOW_MAXFEE*ETOW_DATSIZE ;
68 esmd_max_ch = ESMD_MAXFEE*ESMD_DATSIZE ;
102 static const char *id2char(
int id)
114 static const char *inst2char(
int inst)
128 char *getEmcTrgData(
DATAP *datap,
int index);
129 int readBTOW(u_short *_data,
int token);
130 int readETOW(u_short *_data,
int token);
131 int DAQemcReader(
char *m);
133 int OLDEVP::emcReader(
char *m)
135 if (m == NULL)
return EVP_DATA_ERR;
136 return DAQemcReader(m);
139 int OLDEVP::DAQemcReader(
char *m)
147 const char *p, *secp, *rbp, *adcr, *adcd ;
148 u_int local_token, token ;
162 if(datap->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapdatap = 1;
164 token = qswap32(swapdatap, datap->bh.token);
166 emc.btow_max_ch = 4800 ;
167 emc.bsmd_max_ch = 12*4800 ;
171 emc.etow_max_ch = ETOW_MAXFEE*ETOW_DATSIZE ;
172 emc.esmd_max_ch = ESMD_MAXFEE*ESMD_DATSIZE ;
183 emc.btow_in = emc.bsmd_in = 0 ;
185 emc.etow_in = emc.esmd_in = 0 ;
191 if(datap == NULL)
return 0 ;
198 char *trg_btow_data = 0;
200 LOG_INFO <<
"Getting BTOW data from trigger banks..." << endm;
201 readBTOW((u_short *)(trg_btow_data), token);
202 bytes += (64 + 2 + 30 * 164) * 2;
206 char *trg_etow_data = getEmcTrgData(datap, y8ETOW_INDEX);
208 LOG_INFO <<
"Getting ETOW data from trigger banks..." << endm;
209 readETOW((u_short *)(trg_etow_data), token);
210 bytes += (64 + 2 + 6 * 164) * 2;
215 for(type=0;type<2;type++) {
225 len = qswap32(swapdatap, datap->det[
id].len) * 4 ;
226 off = qswap32(swapdatap, datap->det[
id].off) ;
227 if((len == 0) || (off == 0)) {
240 len = qswap32(swapdatap, datap->det[EXT_ID].len) ;
241 off = qswap32(swapdatap, datap->det[EXT_ID].off) ;
242 if((len == 0) || (off == 0)) {
246 datapx = (
struct DATAPX *)(m + off*4) ;
249 if(checkBank(datapx->bh.bank_type, CHAR_DATAPX) < 0) {
253 if(datapx->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapdatapx = 1;
255 len = qswap32(swapdatapx, datapx->det[
id-10].len) * 4 ;
256 off = qswap32(swapdatapx, datapx->det[
id-10].off) ;
257 if((len == 0) || (off == 0)) {
267 LOG_DEBUG << Form(
"EMC %s: bytes %d, off %d",id2char(
id),len,off) << endm;
271 emcp = (
struct EMCP *)((u_int *)m + off) ;
273 if(checkBank(emcp->bh.bank_type,p) < 0) {
277 if(emcp->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapemcp = 1;
279 token = qswap32(swapemcp, emcp->bh.token) ;
286 len = qswap32(swapemcp, emcp->sec[i].len) ;
287 if(len == 0) continue ;
291 off = qswap32(swapemcp, emcp->sec[i].off) ;
293 emcsecp = (
struct EMCSECP *)((u_int *)emcp + off) ;
295 if(checkBank(emcsecp->bh.bank_type,secp) < 0) {
299 cou = (b2h32(emcsecp->bh.length) - 10) / 2 ;
302 LOG_DEBUG << Form(
"EMC %s: instance %s: %d fibers possible",id2char(
id),inst2char(instance),cou) << endm;
305 len = b2h32(emcsecp->fiber[j].len) ;
307 if(len == 0) continue ;
309 off = b2h32(emcsecp->fiber[j].off) ;
311 emcrbp = (
struct EMCRBP *)((u_int *)emcsecp + off) ;
313 LOG_DEBUG << Form(
"EMC %s: instance %s: fiber %d: len %d, off %d",id2char(
id),inst2char(instance),j+1,len,off) << endm;
315 if(checkBank(emcrbp->bh.bank_type,rbp) < 0) {
320 cou2 = (b2h32(emcrbp->bh.length) - 10) /2 ;
322 LOG_DEBUG << Form(
"EMC %s: instance %s: fiber %d: %d banks used",id2char(
id),inst2char(instance),j+1,cou2) << endm;
324 emcadcr = emcadcd = NULL ;
326 for(k=0;k<cou2;k++) {
327 len = b2h32(emcrbp->banks[k].len) ;
329 if(len == 0) continue ;
331 off = b2h32(emcrbp->banks[k].off) ;
337 emcadcr = (
struct DUMMYDATA *)((u_int *)emcrbp + off) ;
338 if(checkBank(emcadcr->bh.bank_type,adcr) < 0) {
344 emcadcd = (
struct DUMMYDATA *)((u_int *)emcrbp + off) ;
345 if(checkBank(emcadcr->bh.bank_type,adcd) < 0) {
351 LOG_ERROR << Form(
"Unknown subbank %d in EMCRBP!",k) << endm;
356 if(emcadcr == NULL) {
357 LOG_WARN << Form(
"EMC %d: instance %d, format %d is not implemented yet!",
358 id2char(
id),inst2char(instance), k) << endm;
363 if((type==0) && (i == EMC_B_TOW)) {
365 LOG_ERROR <<
"Reading BTOW data from DAQ banks but already read it from trigger banks" << endm;
367 readBTOW((u_short *)((
char *)emcadcr + 40), token);
369 else if((type==0) && (i == EMC_B_SMD)) {
377 data = (u_short *) ((
char *) emcadcr + 40 + 4 + 256) ;
380 emc.bsmd_cap[j] = *(u_char *)((
char *)emcadcr + 40 + 4 + 4*16) ;
381 for(l=0;l<4800;l++) {
382 emc.bsmd[j][l] = l2h16(*data++) ;
383 if(emc.bsmd[j][l] > 0) emc.bsmd_ch++ ;
384 LOG_DEBUG << Form(
"BSMD %d: %d",l,emc.bsmd[j][l]) << endm;
388 else if((type == 1) && (i == EMC_B_TOW)) {
391 LOG_ERROR <<
"Reading ETOW data in the DAQ banks, but already read it from the trigger banks." << endm;
394 readETOW((u_short *)((
char*)emcadcr + 40), token);
396 else if((type==1) && (i == EMC_B_SMD)) {
407 data = (u_short *) ((
char*) emcadcr + 40 + 4 + 4) ;
409 data = (u_short *) ((
char*) emcadcr + 40 + 4 + 6) ;
412 local_token = thi * 256 + tlo ;
414 if(token != local_token) {
415 LOG_ERROR << Form(
"ESMD: Token in bank %d different from token in data %d",token,local_token) << endm;
418 data = (u_short *) ((
char*) emcadcr + 40 + 4 + 128) ;
423 if(l2h32(emcadcr->bh.length) < 3000) {
424 emc.esmd_max_fee = 30 ;
427 emc.esmd_max_fee = 48 ;
430 emc.esmd_max_ch = emc.esmd_max_fee*ETOW_DATSIZE ;
434 for(m=0;m<ESMD_PRESIZE;m++) {
435 for(l=0;l<emc.esmd_max_fee;l++) {
436 emc.
esmd_pre[l][m] = l2h16(*data++) ;
440 for(m=0;m<ESMD_DATSIZE;m++) {
441 for(l=0;l<emc.esmd_max_fee;l++) {
442 emc.esmd[l][m] = l2h16(*data++) ;
443 if(emc.esmd[l][m] > 0) emc.esmd_ch++ ;
462 int OLDEVP::readBTOW(u_short *_data,
int token)
466 int thi, tlo, local_token;
471 data = (u_short *)((
char*)_data + 4 + 4);
473 data = (u_short *)((
char*)_data + 4 + 6);
475 data = (u_short *)((
char*)_data + 4 + 128);
477 local_token = thi * 256 + tlo ;
479 if(token != local_token) {
480 LOG_ERROR << Form(
"BTOW: Event token different from token in BTOW data %d vs %d (%d,%d)",token,local_token,thi,tlo) << endm;
485 data = (u_short *)((
char*)_data + 4 + 128);
486 emc.btow_raw = data ;
489 for(m=0;m<BTOW_PRESIZE;m++) {
490 for(l=0;l<BTOW_MAXFEE;l++) {
491 emc.btow_pre[l][m] = l2h16(*data++) ;;
496 for(m=0;m<BTOW_DATSIZE;m++) {
497 for(l=0;l<BTOW_MAXFEE;l++) {
498 emc.btow_new[l][m] = l2h16(*data++) ;
503 data = emc.btow_raw ;
506 for(l=0;l<4800;l++) {
507 emc.btow[l] = l2h16(*data++) ;
508 if(emc.btow[l] > 0) emc.btow_ch++ ;
521 int OLDEVP::readETOW(u_short *_data,
int token) {
534 data = (u_short *)((
char*)_data + 4 + 4);
536 data = (u_short *)((
char*)_data + 4 + 6);
538 data = (u_short *)((
char*)_data + 4 + 128);
540 local_token = thi * 256 + tlo ;
542 if(token != local_token) {
543 LOG_ERROR << Form(
"ETOW: Event token different from token in ETOW data %d vs %d (%d,%d)",token,local_token,thi,tlo) << endm;
546 emc.etow_raw = data ;
549 for(m=0;m<ETOW_PRESIZE;m++) {
550 for(l=0;l<ETOW_MAXFEE;l++) {
551 emc.etow_pre[l][m] = l2h16(*data++) ;;
556 for(m=0;m<ETOW_DATSIZE;m++) {
557 for(l=0;l<ETOW_MAXFEE;l++) {
558 emc.etow[l][m] = l2h16(*data++) ;
559 if(emc.etow[l][m] > 0) emc.etow_ch++ ;
569 char* OLDEVP::getEmcTrgData(
DATAP* datap,
int index)
571 LOG_INFO <<
"Starting EMC reader..." << endm;
574 LOG_INFO << Form(
"DATAP=08x%x, byte order=0x%08x", datap, datap->bh.byte_order) << endm;
575 if (!(datap->det[TRG_ID].off && datap->det[TRG_ID].len))
return 0;
576 bool swapdatap = datap->bh.byte_order != DAQ_RAW_FORMAT_ORDER;
577 int off = qswap32(swapdatap, datap->det[TRG_ID].off);
580 TRGP* trgp = (
TRGP*)((
int*)datap + off) ;
581 LOG_INFO << Form(
"TRGP=0x%08x, byte order=0x%08x", trgp, trgp->bh.byte_order) << endm;
582 if (checkBank(trgp->bh.bank_type,
"TRGP") < 0)
return 0;
584 if (!trgp->bh.token) {
585 LOG_INFO <<
"Token 0 - skipping..." << endm;
589 if (!(trgp->trgData.off && trgp->trgData.len))
return 0;
591 bool swaptrgp = trgp->bh.byte_order != DAQ_RAW_FORMAT_ORDER;
592 off = qswap32(swaptrgp, trgp->trgData.off);
595 TRGD* trgd = (
TRGD*)((
int*)trgp + off);
596 LOG_INFO << Form(
"TRGD=0x%08x, byte order=0x%08x", trgd, trgd->bh.byte_order) << endm;
599 if (checkBank(trgd->bh.bank_type,
"TRGD") < 0)
return 0;
601 bool swaptrgd = trgd->bh.byte_order != DAQ_RAW_FORMAT_ORDER;
602 int len = qswap32(swaptrgd, trgd->bh.length);
604 if (len == 10)
return 0;
609 unsigned int* p = (
unsigned int*)&trgd->tow;
610 std::transform(p, p + SIZE, p, bswap);
614 unsigned int byteCount_Version = qswap32(swaptrgd, trgtowertrnfer->byteCount_Version);
615 unsigned int byteCount = byteCount_Version >> 8 & 0xffffff;
616 unsigned char version = byteCount_Version & 0xff;
619 case y8TRANSFER_VERSION: {
620 int offset = qswap32(swaptrgd, trgtowertrnfer->OffsetBlock[index].offset);
621 int length = qswap32(swaptrgd, trgtowertrnfer->OffsetBlock[index].length);
623 if (!(offset && length))
return 0;
625 LOG_INFO << Form(
"TrgTowerTrnfer2008: byte count=%d, version=0x%02x, index=%d, offset=%d, length=%d",
626 byteCount, version, index, offset, length) << endm;
630 if (length != (64 + 2 + 30 * 164) * 2) {
631 LOG_ERROR << Form(
"Have BTOW in event, but data length incorrect: length=%d, not %d. Ignoring data...",
632 length, (64 + 2 + 30 * 164) * 2) << endm;
636 LOG_INFO << Form(
"Have BTOW data in trigger banks: offset=%d, length=%d", offset, length) << endm;
640 if (length != (64 + 2 + 6 * 164) * 2) {
641 LOG_ERROR << Form(
"Have ETOW in event, but data length incorrect: length=%d, not %d. Ignoring data...",
642 length, (64 + 2 + 6 * 164) * 2) << endm;
646 LOG_INFO << Form(
"Have ETOW data in trigger banks: offset=%d, length=%d", offset, length) << endm;
650 LOG_ERROR << Form(
"Unknown transfer data index=%d", index) << endm;
654 return (
char*)trgtowertrnfer + offset;
658 LOG_INFO << Form(
"Trigger transfer version 0x%02x. No EMC data in trigger banks.", version) << endm;
void version(std::ostream &os=std::cout)
print HepMC version
u_short * esmd_raw
ESMD preamble.
u_short esmd_pre[ESMD_MAXFEE][ESMD_PRESIZE]
ADC data.