5 #include <daqFormats.h>
6 #include <rtsSystems.h>
10 #include <DAQ_READER/daq_det.h>
13 #include <SVT/key_map.h>
25 static int unpackRaw(
int sec,
int what,
struct SVTANODK *padk,
struct TPCCPPR_l *cppr,
char *mem,
svt_t *svt) ;
32 int svt_reader(
char *m,
struct svt_t *svt, u_int driver)
38 static int first = 1 ;
60 if(m==NULL)
return 0 ;
62 datap = (
struct DATAP *) m ;
64 len = ntohl(datap->det[SVT_ID].len) ;
65 if(len == 0)
return 0 ;
68 off = ntohl(datap->det[SVT_ID].off) ;
69 if(off == 0)
return 0 ;
71 svtp = (
struct TPCP *)((u_int *)m+off) ;
72 if(checkBank((
char *)svtp,
"SVTP") < 0)
return -1 ;
79 svt->max_channels = 24*3*6*240*128 ;
85 svt->B[i][j][k] = (svt_pad_key[i][j][k] & 0xFF000000) >> 24 ;
86 svt->L[i][j][k] = (svt_pad_key[i][j][k] & 0x00FF0000) >> 16 ;
87 svt->W[i][j][k] = (svt_pad_key[i][j][k] & 0x00000F00) >> 8 ;
88 svt->H[i][j][k] = (svt_pad_key[i][j][k] & 0x0000F000) >> 12 ;
98 memset((
char *)svt->counts,0,
sizeof(svt->counts)) ;
100 for(sec=0;sec<4;sec++) {
103 if((b2h32(svtp->bh.format_number)==1) && (sec%2)) continue ;
105 if(svtp->sb[sec].len == 0) continue ;
107 LOG(DBG,
"SVT sector %d: len %d, off %d",sec+1,svtp->sb[sec].len,svtp->sb[sec].off) ;
109 secp = (
struct TPCSECP *) ((
char *)svtp + b2h32(svtp->sb[sec].off)*4) ;
110 if(checkBank((
char *)secp,
"SVTSECP") < 0)
return -1 ;
113 if(b2h32(svtp->bh.format_number)==1) {
120 for(rb=0;rb<last;rb++) {
126 rrb = rsec*6 + (rb - 6) ;
133 if(secp->rb[rb].len == 0) continue ;
135 rbp = (
struct TPCRBP *) ((
char *)secp + b2h32(secp->rb[rb].off)*4) ;
136 if(checkBank((
char *)rbp,
"SVTRBP") < 0)
return -1 ;
140 for(mz=0;mz<3;mz++) {
141 if(rbp->mz[mz].len == 0) continue ;
143 mzp = (
struct TPCMZP *)((
char *)rbp + l2h32(rbp->mz[mz].off)*4) ;
144 if(checkBank((
char *)mzp,
"SVTMZP") < 0)
return -1 ;
148 if((mzp->banks[TPC_ADCD].len != 0) && (svt->mode==0)) {
153 seqd = (
struct TPCSEQD *)((
char *)mzp + l2h32(mzp->banks[TPC_SEQD].off)*4) ;
154 adcd = (
struct TPCADCD *)((
char *)mzp + l2h32(mzp->banks[TPC_ADCD].off)*4) ;
177 if(checkBank((
char *)seqd,
"SVTSEQD") < 0)
return -1 ;
178 if(checkBank((
char *)adcd,
"SVTADCD") < 0)
return -1 ;
180 int len = l2h32(seqd->bh.length) - 10 ;
188 for(jj=0;jj<len;jj++) {
189 int start, last, length, stop ;
195 ss = l2h16(seqd->seq[jj]) ;
196 f8 = (ss & 0x8000) ? 1 : 0 ;
200 pp = (ss & 0x7FFF) % 256 ;
201 hy = (ss & 0x7FFF) / 256 ;
208 if((svt_pad_key[rrb][mz][k] & 0x7F) == hy) {
218 LOG(DBG,
"Hybrid 0: 0x%04X, seq %d of %d",
222 LOG(ERR,
"Can't find hybrid 0x%02X in the svt_key for RB %d, MZ %d (seq %d of %d)!",
223 hy,rrb+1,mz+1,jj,len) ;
227 LOG(DBG,
"Hy %d %d %d 0x%02X %d",rrb+1,mz+1,rr,hy,pp) ;
230 LOG(WARN,
"Anode is 0xFF!",0,0,0,0,0) ;
236 last = (ss & 0x0020) ? 1 : 0 ;
238 start = (ss & 0x7FC0) >> 6 ;
239 stop = start + length ;
246 for(tbin=start;tbin<stop;tbin++) {
248 val = adcd->adc[adccou++] ;
250 int counter = svt->counts[rrb][mz][rr-1][pp-1] ;
251 svt->adc[rrb][mz][rr-1][pp-1][counter] = val ;
252 svt->timebin[rrb][mz][rr-1][pp-1][counter] = tbin ;
253 svt->counts[rrb][mz][rr-1][pp-1] += 1 ;
268 if(mzp->banks[SVT_ANODK].len != 0) {
269 padk = (
struct SVTANODK *)((
char *)mzp + l2h32(mzp->banks[SVT_ANODK].off)*4) ;
271 if(checkBank((
char *)padk,
"SVTANODK") < 0)
return -1 ;
276 if(mzp->banks[TPC_CPPR].len != 0) {
278 cppr = (
struct TPCCPPR_l *)((
char *)mzp + l2h32(mzp->banks[TPC_CPPR].off)*4) ;
279 if(checkBank((
char *)cppr,
"SVTCPPR") < 0)
return -1 ;
283 if((mzp->banks[TPC_ADCR].len != 0) && (svt->mode==0)) {
284 adcr = (
struct TPCADCR_l *)((
char *)mzp + l2h32(mzp->banks[TPC_ADCR].off)*4) ;
286 if(checkBank((
char *)adcr,
"SVTADCR") < 0)
return -1 ;
290 if(unpackRaw(rrb*3+mz, 0, padk, cppr, (
char *)adcr, svt) < 0) {
291 LOG(ERR,
"Problems in RAW data in sector %d, RB %d, MZ %d - skipping...",
292 rsec+1,rrb+1,mz+1,0,0) ;
295 LOG(DBG,
"SVT Raw data bank in sector %d, RB %d, MZ %d [sec %d, rb %d]!",
296 rsec+1,rrb+1,mz+1,sec,rb) ;
300 if(mzp->banks[TPC_PEDR].len != 0) {
301 pedr = (
struct TPCPEDR *)((
char *)mzp + l2h32(mzp->banks[TPC_PEDR].off)*4) ;
303 if(checkBank((
char *)pedr,
"SVTPEDR") < 0)
return -1 ;
305 unpackRaw(rrb*3+mz, 1, padk, cppr, (
char *)pedr, svt) ;
310 if(mzp->banks[TPC_RMSR].len != 0) {
311 rmsr = (
struct TPCRMSR *)((
char *)mzp + l2h32(mzp->banks[TPC_RMSR].off)*4) ;
313 if(checkBank((
char *)rmsr,
"SVTRMSR") < 0)
return -1 ;
315 unpackRaw(rrb*3+mz, 2, padk, cppr, (
char *)rmsr, svt) ;
338 static int unpackRaw(
int sec,
int what,
struct SVTANODK *padk,
struct TPCCPPR_l *cppr,
char *mem,
svt_t *svt)
347 static u_char svtindex[128] ;
354 LOG(WARN,
"No ANODK? - skipping...",0,0,0,0,0) ;
358 LOG(WARN,
"No DATA? - skipping...",0,0,0,0,0) ;
366 int ver = l2h32((((
struct TPCRMSR *)mem)->bh.format_number)) ;
369 LOG(WARN,
"Can't work with pre-2001 data - sorry (ver %d)",ver,0,0,0,0) ;
374 svt->pre = l2h32(padk->pre) ;
375 svt->post = l2h32(padk->post) ;
376 svt->pedoffset = l2h32(padk->pedOff) ;
378 LOG(DBG,
"SVT: pre %d, post %d, pedOff %d",svt->pre,svt->post,svt->pedoffset) ;
387 adcdata = (u_char *) mem +
sizeof(
struct TPCADCR_l);
390 LOG(WARN,
"No CPPR? - skipping...",0,0,0,0,0) ;
394 cppdata = (u_short *)((
char *)cppr +
sizeof(
struct TPCCPPR_l)) ;
397 adcdata = ((
struct TPCPEDR *)mem)->ped ;
400 adcdata = ((
struct TPCRMSR *)mem)->rms ;
411 for(pad=0;pad<240;pad++) {
415 u_int anode_offset = (pad % 0x40) * 128 * 4 + (pad/64) ;
417 adcseq = (u_char *) adcdata + i*0x8000 + anode_offset ;
421 cppseq = (u_short *)((
char *) cppdata + 2*2*8*(i*256+pad)) ;
424 memset(svtindex,0,
sizeof(svtindex)) ;
426 for(j=0;j<cpps;j++) {
427 u_short start, stop ;
431 start = l2h16(*cppseq++) ;
432 stop = l2h16(*cppseq++) ;
436 if(start & 0xFE00) break ;
438 if(j==7) stop &= 0x7F ;
439 if(start == 127) stop = 127 ;
441 if((stop < start) || (stop >= timebins)) {
442 LOG(WARN,
"Bad data stop<start %d<%d, cpp %d - skipping",stop,start,j,0,0) ;
446 pre = (int)start - svt->pre ;
447 if(pre < 0) start = 0 ;
450 post = stop + svt->post ;
451 if(post > 127) stop = 127 ;
454 for(t=start;t<=stop;t++) {
465 val = *(adcseq + t*4) ;
472 counter = svt->counts[rb][mz][i][pad] ;
473 svt->adc[rb][mz][i][pad][counter] = val ;
474 svt->timebin[rb][mz][i][pad][counter] = t ;
475 svt->counts[rb][mz][i][pad]++ ;
483 for(j=0;j<timebins;j++) {
484 svt->adc[rb][mz][i][pad][j] = *(adcseq + j*4) ;
489 for(j=0;j<timebins;j++) {
490 svt->timebin[rb][mz][i][pad][j] = *(adcseq + j*4) ;