4 #include <daqFormats.h>
5 #include <rtsSystems.h>
11 #define PADS_PER_ROW 182
12 #include <TPC/offsets.h>
13 #include <TPC/trans_table.hh>
16 #include <fcfClass.hh>
18 static struct tpc_t *tpc_cached;
21 static int unpackRaw(
int sec,
int what,
struct TPCPADK *padk,
struct TPCCPPR_l *cppr,
char *mem) ;
23 static int dumpGainr(
int sec,
int rb,
struct TPCGAINR *g) ;
31 int tpc_reader(
char *m,
struct tpc_t *tpc,
int sector,
int flags)
76 memset(tpc_cached->rdo_present,0,
sizeof(tpc_cached->rdo_present)) ;
78 memset((
char *)tpc_cached->counts,0,
sizeof(tpc_cached->counts)) ;
82 memset(tpc_cached->cl_counts,0,
sizeof(tpc_cached->cl_counts)) ;
83 memset(tpc_cached->cl_p,0,
sizeof(tpc_cached->cl_p)) ;
85 tpc_cached->mode = 0 ;
86 tpc_cached->max_channels_sector = 512*5692 ;
87 tpc_cached->max_channels_all = tpc_cached->max_channels_sector * 24 ;
88 tpc_cached->channels_sector = 0 ;
89 tpc_cached->has_clusters = 0 ;
91 if(m == NULL)
return 0 ;
93 datap = (
struct DATAP *)m ;
95 if(datap->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapdatap = 1;
97 len = qswap32(swapdatap, datap->det[TPC_ID].len) ;
105 off = qswap32(swapdatap, datap->det[TPC_ID].off) ;
111 LOG(DBG,
"TPCP len %d, off %d",len,off) ;
113 tpcp = (
struct TPCP *)((u_int *)m + off) ;
114 if(checkBank((
char *)tpcp,
"TPCP") < 0)
return 0 ;
115 if(tpcp->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swaptpcp = 1;
117 t = qswap32(swaptpcp, tpcp->bh.token) ;
121 LOG(DBG,
"Token %d...",t,0,0,0,0) ;
123 int found_something = 0 ;
128 if(qswap32(swaptpcp, tpcp->bh.format_number)==1) {
147 LOG(DBG,
"Wanted %d is in %d, first RB %d, fmt %d",sector,hsec,first,tpcp->bh.format_number,0) ;
149 LOG(DBG,
"Sector %d: len %d, offset %d, len+offset %d",
150 hsec,tpcp->sb[hsec].len,tpcp->sb[hsec].off,tpcp->sb[hsec].len+tpcp->sb[hsec].off,0) ;
153 if(tpcp->sb[hsec].len == 0) {
160 tot_bytes = qswap32(swaptpcp, tpcp->sb[hsec].len) * 4 ;
167 secp = (
struct TPCSECP *) ((
char *)tpcp + qswap32(swaptpcp, tpcp->sb[hsec].off)*4) ;
168 if(checkBank((
char *)secp,
"TPCSECP") < 0)
return 0 ;
171 if(t != b2h32(secp->bh.token)) {
172 LOG(CAUTION,
"Different token %d in sector %d - should be %d",
173 b2h32(secp->bh.token),hsec,t,0,0) ;
180 LOG(DBG,
"Sector %d: format number %d",hsec,b2h32(secp->bh.format_number),0,0,0) ;
183 if(b2h32(secp->bh.format_number)==2) {
185 seclp = (
struct TPCSECLP *)((u_int *)secp + b2h32(secp->bh.w9)) ;
186 if(checkBank((
char *)seclp,CHAR_TPCSECLP) < 0) {
195 for(rb=first;rb<last;rb++) {
205 if(seclp->rb[rb].len == 0) {
206 LOG(DBG,
"RB %d has no CLD data (in SECLP)...",rb,0,0,0,0) ;
210 rbclp = (
struct TPCRBCLP *) ((
char *)seclp + b2h32(seclp->rb[rb].off)*4) ;
211 if(checkBank((
char *)rbclp,CHAR_TPCRBCLP) < 0) {
217 if(t != l2h32(rbclp->bh.token)) {
218 LOG(CAUTION,
"Different token %d in rb %d - should be %d",
219 l2h32(rbclp->bh.token),rb,t,0,0) ;
223 for(mz=0;mz<3;mz++) {
224 if(rbclp->mz[mz].len == 0) {
225 LOG(DBG,
"MZ %d has no CLD data (in RBCLP)...",mz,0,0,0,0) ;
229 mzcld = (
struct TPCMZCLD_local *)((
char *)rbclp + l2h32(rbclp->mz[mz].off)*4) ;
230 if(checkBank((
char *)mzcld,CHAR_TPCMZCLD) < 0) {
231 LOG(ERR,
"Bad TPCMZCLD data bank in sector %d, RB %d, MZ %d (t %d)!",
232 sector+1,rrb+1,mz+1,t,0) ;
238 if(t != l2h32(mzcld->bh.token)) {
239 LOG(CAUTION,
"Different token %d in mz %d - should be %d",
240 l2h32(mzcld->bh.token),mz,t,0,0) ;
244 LOG(DBG,
"TPCMZCLD MZ %d, token %d",mz,l2h32(mzcld->bh.token),0,0,0) ;
246 ret = unpackCld(sector, mzcld) ;
248 tpc_cached->has_clusters = 1 ;
249 found_something = 1 ;
252 if(DAQ_RAW_FORMAT_WORD9 != l2h32(mzcld->bh.w9)) {
255 LOG(DBG,
"FCF timing: sec %d, mz %d, hits %d, ms %f",sector+1,rrb*3+mz,ret,((
double)l2h32(mzcld->bh.w9)*8.0)/33000.0) ;
266 for(rb=first;rb<last;rb++) {
276 if(secp->rb[rb].len == 0) {
277 LOG(DBG,
"Sector %d: RB %d not present in raw data...",sector,rb,0,0,0) ;
281 rbp = (
struct TPCRBP *) ((
char *)secp + b2h32(secp->rb[rb].off)*4) ;
282 if(checkBank((
char *)rbp,
"TPCRBP") < 0) {
288 tpc_cached->rdo_present[rrb] = 1;
292 if(t != l2h32(rbp->bh.token)) {
293 LOG(CAUTION,
"Different token %d in rb %d - should be %d",
294 l2h32(rbp->bh.token),rb,t,0,0) ;
298 for(mz=0;mz<3;mz++) {
299 LOG(DBG,
"RB %d, MZ %d: len %d, off %d",rb,mz,l2h32(rbp->mz[mz].len),l2h32(rbp->mz[mz].off)) ;
302 for(mz=0;mz<3;mz++) {
303 if(rbp->mz[mz].len == 0) continue ;
307 mzp = (
struct TPCMZP *)((
char *)rbp + l2h32(rbp->mz[mz].off)*4) ;
308 if(checkBank((
char *)mzp,
"TPCMZP") < 0) {
309 LOG(ERR,
"Bad TPCMZP data bank in sector %d, RB %d, MZ %d (t %d)!",
310 sector+1,rrb+1,mz+1,t,0) ;
316 if(t != l2h32(mzp->bh.token)) {
317 LOG(CAUTION,
"Different token %d in mz %d - should be %d",
318 l2h32(mzp->bh.token),mz,t,0,0) ;
322 LOG(DBG,
"TPCMZP token %d",l2h32(mzp->bh.token),0,0,0,0) ;
333 if(mzp->banks[TPC_MZCLD].len != 0) {
334 LOG(DBG,
"MZCLD len 0x%08X, off 0x%08X",l2h32(mzp->banks[TPC_MZCLD].len),l2h32(mzp->banks[TPC_MZCLD].off),0,0,0) ;
336 mzcld = (
struct TPCMZCLD_local *)((
char *)mzp + l2h32(mzp->banks[TPC_MZCLD].off)*4) ;
337 if(checkBank((
char *)mzcld,
"TPCMZCLD") < 0) ;
343 if(mzp->banks[TPC_SEQD].len != 0) {
344 seqd = (
struct TPCSEQD *)((
char *)mzp + l2h32(mzp->banks[TPC_SEQD].off)*4) ;
346 if(checkBank((
char *)seqd,CHAR_TPCSEQD) < 0) ;
351 if(mzp->banks[TPC_ADCX].len != 0) {
352 adcx = (
struct TPCADCX *)((
char *)mzp + l2h32(mzp->banks[TPC_ADCX].off)*4) ;
354 if(checkBank((
char *)adcx,CHAR_TPCADCX) < 0) ;
360 if((mzp->banks[TPC_ADCD].len != 0) && (tpc_cached->mode==0)) {
364 if(mzp->banks[TPC_SEQD].len == 0) {
365 LOG(WARN,
"SEQD has 0 length and ADCD doesn't???",0,0,0,0,0) ;
369 seqd = (
struct TPCSEQD *)((
char *)mzp + l2h32(mzp->banks[TPC_SEQD].off)*4) ;
370 adcd = (
struct TPCADCD *)((
char *)mzp + l2h32(mzp->banks[TPC_ADCD].off)*4) ;
372 if(checkBank((
char *)seqd,
"TPCSEQD") < 0) {
373 LOG(ERR,
"Bad TPCSEQD data bank in sector %d, RB %d, MZ %d!",
374 sector+1,rrb+1,mz+1,0,0) ;
379 if(checkBank((
char *)adcd,
"TPCADCD") < 0)
return 0 ;
381 int len = l2h32(seqd->bh.length) - 10 ;
390 for(jj=0;jj<len;jj++) {
391 int start, last, length, stop ;
395 ss = l2h16(seqd->seq[jj]) ;
400 pp = (ss & 0x7FFF) % 256 ;
401 rr = (ss & 0x7FFF) / 256 ;
403 if(pp == 0xff) break ;
408 start = (ss & 0x7FC0) >> 6 ;
409 stop = start + length ;
411 for(tbin=start;tbin<stop;tbin++) {
415 val = adcd->adc[adccou++] ;
417 if((rr > 45) || (pp > 182)) {
418 LOG(CRIT,
"rr = %d pp=%d",rr,pp);
420 counter = tpc_cached->counts[rr-1][pp-1] ;
423 LOG(CRIT,
"%d %d %d counter = %d",ss,rr,pp,counter);
427 tpc_cached->adc[rr-1][pp-1][counter] = val ;
428 tpc_cached->timebin[rr-1][pp-1][counter] = tbin ;
429 tpc_cached->counts[rr-1][pp-1] += 1 ;
430 tpc_cached->channels_sector++ ;
439 found_something = 1 ;
448 if((mzp->banks[TPC_PADK].len != 0)) {
449 padk = (
struct TPCPADK *)((
char *)mzp + l2h32(mzp->banks[TPC_PADK].off)*4) ;
450 if(checkBank((
char *)padk,
"TPCPADK") < 0)
return 0 ;
455 if(mzp->banks[TPC_CPPR].len != 0) {
457 cppr = (
struct TPCCPPR_l *)((
char *)mzp + l2h32(mzp->banks[TPC_CPPR].off)*4) ;
458 if(checkBank((
char *)cppr,
"TPCCPPR") < 0)
return 0 ;
463 if((mzp->banks[TPC_ADCR].len != 0) && (tpc_cached->mode==0)) {
464 adcr = (
struct TPCADCR_l *)((
char *)mzp + l2h32(mzp->banks[TPC_ADCR].off)*4) ;
466 if(checkBank((
char *)adcr,
"TPCADCR") < 0)
return 0 ;
468 if(unpackRaw(sector, 0, padk, cppr, (
char *)adcr) < 0) {
469 LOG(ERR,
"Problems in RAW data in sector %d, RB %d, MZ %d - skipping...",
470 sector+1,rrb+1,mz+1,0,0) ;
473 found_something = 1 ;
475 LOG(DBG,
"TPC Raw data bank in sector %d, RB %d, MZ %d!",
476 sector+1,rrb+1,mz+1,0,0) ;
480 if(mzp->banks[TPC_PEDR].len != 0) {
481 pedr = (
struct TPCPEDR *)((
char *)mzp + l2h32(mzp->banks[TPC_PEDR].off)*4) ;
483 if(checkBank((
char *)pedr,
"TPCPEDR") < 0)
return 0 ;
485 found_something = 1 ;
487 unpackRaw(sector, 1, padk, cppr, (
char *)pedr) ;
488 tpc_cached->mode = 1 ;
492 if(mzp->banks[TPC_RMSR].len != 0) {
493 rmsr = (
struct TPCRMSR *)((
char *)mzp + l2h32(mzp->banks[TPC_RMSR].off)*4) ;
495 if(checkBank((
char *)rmsr,
"TPCRMSR") < 0)
return 0 ;
497 unpackRaw(sector, 2, padk, cppr, (
char *)rmsr) ;
498 tpc_cached->mode = 1 ;
499 found_something = 1 ;
502 if(mzp->banks[TPC_GAINR].len != 0) {
503 gainr = (
struct TPCGAINR *)((
char *)mzp + l2h32(mzp->banks[TPC_GAINR].off)*4) ;
505 if(checkBank((
char *)gainr,CHAR_TPCGAINR) < 0) ;
507 dumpGainr(sector, rrb, gainr) ;
508 found_something = 1 ;
512 if(mzp->banks[TPC_BADR].len != 0) {
513 badr = (
struct TPCBADR *)((
char *)mzp + l2h32(mzp->banks[TPC_BADR].off)*4) ;
515 if(checkBank((
char *)badr,CHAR_TPCBADR) < 0) ;
516 found_something = 1 ;
525 #ifdef SOME_OTHER_DAY
526 if(tpc_cached->has_clusters) {
529 printf(
"Row %2d: ",i+1) ;
531 if(tpc_cached->cl_p[i][j]) {
532 printf(
" %d[%2d] ",j,l2h32(*tpc_cached->cl_p[i][j])) ;
542 if(tpc_cached->mode == 1) {
543 for(
int r=0;r<45;r++) {
544 for(
int p=0;p<182;p++) {
545 if(tpc_cached->counts[r][p] == 0) {
546 memset(tpc_cached->adc[r][p], 0,
sizeof(tpc_cached->adc[r][p]));
547 memset(tpc_cached->timebin[r][p], 0,
sizeof(tpc_cached->timebin[r][p]));
555 return found_something ;
564 static int unpackRaw(
int sec,
int what,
struct TPCPADK *padk,
struct TPCCPPR_l *cppr,
char *mem)
583 LOG(WARN,
"No PADK? - skipping...",0,0,0,0,0) ;
587 LOG(WARN,
"No DATA? - skipping...",0,0,0,0,0) ;
594 adcdata = (u_char *) mem +
sizeof(
struct TPCADCR_l);
596 LOG(WARN,
"No CPPR? - skipping...",0,0,0,0,0) ;
600 cppdata = (u_short *)((
char *)cppr +
sizeof(
struct TPCCPPR_l)) ;
603 adcdata = ((
struct TPCPEDR *)mem)->ped ;
606 adcdata = ((
struct TPCRMSR *)mem)->rms ;
609 LOG(ERR,
"Unknown case variable %d",what,0,0,0,0) ;
616 row = padk->rp[i].row ;
617 pad = padk->rp[i].pad ;
619 if((row==0xFF) || (pad==0xFF)) continue ;
625 adcseq = (u_char *) adcdata + timebins*i ;
630 cppseq = (u_short *)((
char *) cppdata + 2*2*32*i) ;
632 for(j=0;j<cpps;j++) {
633 u_short start, stop ;
645 start = l2h16(*cppseq++) ;
646 stop = l2h16(*cppseq++) ;
651 if(start & 0xFE00) break ;
652 if(start == 511) stop = 511 ;
657 if((stop < start) || (stop >= timebins)) {
658 LOG(WARN,
"Bad data stop<start %d<%d - skipping",stop,start,0,0,0) ;
663 for(t=start;t<=stop;t++) {
665 val = *(adcseq + t) ;
670 LOG(WARN,
"ADC==0, timebin %d, seq %d, lrow %d, row %d, pad %d",t,j,i,row+1,pad+1) ;
677 counter = tpc_cached->counts[row][pad] ;
678 tpc_cached->adc[row][pad][counter] = val ;
679 tpc_cached->timebin[row][pad][counter] = t ;
680 tpc_cached->counts[row][pad]++ ;
681 tpc_cached->channels_sector++ ;
689 for(j=0;j<timebins;j++) {
690 tpc_cached->adc[row][pad][j] = *adcseq++ ;
691 tpc_cached->channels_sector++ ;
692 tpc_cached->counts[row][pad]++;
696 for(j=0;j<timebins;j++) {
697 tpc_cached->timebin[row][pad][j] = *adcseq++ ;
698 tpc_cached->counts[row][pad]++;
715 u_int nrows, nclust, row ;
721 int bytes = l2h32(mzcld->bh.length)*4 ;
723 LOG(DBG,
"No data in TPCMZCLD...",0,0,0,0,0) ;
727 nrows = l2h32(mzcld->rows) ;
730 LOG(NOTE,
"No MZCLD rows?...",0,0,0,0,0) ;
734 u_int ticks = l2h32(mzcld->bh.w9) ;
737 rdata = (u_int *) mzcld->padrowFiller ;
739 LOG(DBG,
"unpackCld: 0x%08X 0x%08X %d: 0x%08X 0x%08X",mzcld,rdata,rdata-(u_int*)mzcld,*rdata,*(rdata+1),0) ;
743 for(i=0;i<nrows;i++) {
747 row = l2h32(*rdata++) ;
748 nclust = l2h32(*rdata++) ;
753 for(instance=0;instance<3;instance++) {
754 if(tpc_cached->cl_p[row-1][instance]) continue ;
755 tpc_cached->cl_p[row-1][instance] = rdata -2 ;
762 LOG(ERR,
"Too many row %d contributions!",row) ;
765 LOG(DBG,
"Row %d (%d of %d), %d clusters (tot bytes %d)...",row,i,nrows,nclust,bytes) ;
769 for(j=0;j<nclust;j++) {
770 u_short pad, tm, flags, charge, fl ;
771 u_int t1, t2, p1, p2 ;
774 pt = l2h32(*rdata++) ;
775 cf = l2h32(*rdata++) ;
778 tm = (pt >> 16) & 0x7FFF ;
784 if(pt & 0x8000) flags |= FCF_DOUBLE_PAD ;
785 if(pt & 0x4000) flags |= FCF_DEAD_EDGE ;
786 if(pt & (0x8000 << 16)) flags |= FCF_ONEPAD ;
787 if(fl & 0x8000) flags |= FCF_ROW_EDGE ;
788 if(fl & 0x4000) flags |= FCF_BROKEN_EDGE ;
790 t2 = (fl & 0x00F0) >> 4 ;
793 t2 = (tm >> 6) + t2 ;
794 t1 = (tm >> 6) - t1 ;
796 p2 = (fl & 0x3800) >> 11 ;
797 p1 = (fl & 0x0700) >> 8 ;
799 p2 = (pad >> 6) + p2 ;
800 p1 = (pad >> 6) - p1 ;
803 double dpad, dtimebin ;
810 dpad = (double)(pad)/64.0 ;
811 dtimebin = (double)(tm)/64.0 ;
813 LOG(DBG,
" pad %d, timebin %d, charge %d, flags 0x%04X, t1 %d, t2 %d, p1 %d, p2 %d",(
int)dpad,(
int)dtimebin,charge,flags,t1,t2,p1,p2) ;
815 u_int ix = tpc_cached->cl_counts[row-1] ;
816 if(ix < TPC_READER_MAX_CLUSTERS) {
817 tpc_cached->cl[row-1][ix].p = (float) dpad ;
818 tpc_cached->cl[row-1][ix].t = (float) dtimebin ;
819 tpc_cached->cl[row-1][ix].charge = charge ;
820 tpc_cached->cl[row-1][ix].flags = flags ;
821 tpc_cached->cl[row-1][ix].t1 = t1 ;
822 tpc_cached->cl[row-1][ix].t2 = t2 ;
823 tpc_cached->cl[row-1][ix].p1 = p1 ;
824 tpc_cached->cl[row-1][ix].p2 = p2 ;
829 tpc_cached->cl_counts[row-1]++ ;
833 LOG(WARN,
"Too many clusters (%d) in sector %d, row %d",ix,sec+1,row,0,0) ;
845 LOG(DBG,
"FCF: sec %d, mz %d (last row %d): %d clust in %u ticks",sec+1,l2h32(mzcld->bh.bank_id),row,tot_hits,ticks) ;
850 static int dumpGainr(
int sec,
int rrb,
struct TPCGAINR *g)
858 int rmz = l2h32(g->bh.bank_id) ;
867 u_int vram, row, pad ;
874 for(rb=1;rb<=6;rb++) {
875 for(mz=0;mz<3;mz++) {
876 for(i=0;i<MZ_MAX_PADS;i++) {
886 vram = (as+1)*0x8000 ;
889 vram += (i%64)*0x200 ;
895 vram |= (rb<<12) | (mz<<9);
899 for(row=1;row<=45;row++) {
900 for(pad=1;pad<=182;pad++) {
901 if((adc_offset[row][pad] & 0xFFFF) == (int)vram) {
902 seq[rb-1][mz][i].row = row ;
903 seq[rb-1][mz][i].pad = pad ;
915 for(i=0;i<MZ_MAX_PADS;i++) {
916 if(seq[rrb][rmz][i].row == 0) continue ;