00001 #include <string.h>
00002 #include <stdio.h>
00003
00004 #include <daqFormats.h>
00005 #include <rtsSystems.h>
00006 #include <rtsLog.h>
00007 #include <rts.h>
00008
00009
00010 #define ROWS 45
00011 #define PADS_PER_ROW 182
00012 #include <TPC/offsets.h>
00013 #include <TPC/trans_table.hh>
00014
00015 #include "daq_tpc.h"
00016 #include <fcfClass.hh>
00017
00018 static struct tpc_t *tpc_cached;
00019
00020
00021 static int unpackRaw(int sec, int what, struct TPCPADK *padk, struct TPCCPPR_l *cppr, char *mem) ;
00022 static int unpackCld(int sec, struct TPCMZCLD_local *mzcld) ;
00023 static int dumpGainr(int sec, int rb, struct TPCGAINR *g) ;
00024
00025
00026
00027
00028
00029
00030
00031 int tpc_reader(char *m, struct tpc_t *tpc, int sector, int flags)
00032 {
00033 u_int rb, mz ;
00034 u_int len ;
00035 u_int off ;
00036 u_int t ;
00037 u_int first, last ;
00038 u_int hsec ;
00039 int ret ;
00040 u_int tot_bytes ;
00041
00042
00043 int retval = 0;
00044
00045 int swapdatap=0;
00046 int swaptpcp=0;
00047
00048 tot_bytes = 0 ;
00049
00050 struct DATAP *datap ;
00051 struct TPCP *tpcp ;
00052 struct TPCSECP *secp ;
00053 struct TPCRBP *rbp ;
00054 struct TPCMZP *mzp ;
00055 struct TPCSEQD *seqd ;
00056 struct TPCADCD *adcd ;
00057 struct TPCADCX *adcx ;
00058 struct TPCPADK *padk ;
00059 struct TPCADCR_l *adcr ;
00060 struct TPCCPPR_l *cppr ;
00061 struct TPCPEDR *pedr ;
00062 struct TPCRMSR *rmsr ;
00063 struct TPCGAINR *gainr ;
00064 struct TPCBADR *badr ;
00065 struct TPCMZCLD_local *mzcld ;
00066 struct TPCSECLP *seclp ;
00067 struct TPCRBCLP *rbclp ;
00068
00069
00070
00071
00072
00073
00074 tpc_cached = tpc ;
00075
00076 memset(tpc_cached->rdo_present,0,sizeof(tpc_cached->rdo_present)) ;
00077
00078 memset((char *)tpc_cached->counts,0,sizeof(tpc_cached->counts)) ;
00079
00080
00081
00082 memset(tpc_cached->cl_counts,0,sizeof(tpc_cached->cl_counts)) ;
00083 memset(tpc_cached->cl_p,0,sizeof(tpc_cached->cl_p)) ;
00084
00085 tpc_cached->mode = 0 ;
00086 tpc_cached->max_channels_sector = 512*5692 ;
00087 tpc_cached->max_channels_all = tpc_cached->max_channels_sector * 24 ;
00088 tpc_cached->channels_sector = 0 ;
00089 tpc_cached->has_clusters = 0 ;
00090
00091 if(m == NULL) return 0 ;
00092
00093 datap = (struct DATAP *)m ;
00094
00095 if(datap->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapdatap = 1;
00096
00097 len = qswap32(swapdatap, datap->det[TPC_ID].len) ;
00098 if(len==0) {
00099 retval = 0 ;
00100 return retval;
00101 }
00102
00103 len *= 4 ;
00104
00105 off = qswap32(swapdatap, datap->det[TPC_ID].off) ;
00106 if(off==0) {
00107 retval = 0 ;
00108 return retval;
00109 }
00110
00111 LOG(DBG,"TPCP len %d, off %d",len,off) ;
00112
00113 tpcp = (struct TPCP *)((u_int *)m + off) ;
00114 if(checkBank((char *)tpcp,"TPCP") < 0) return 0 ;
00115 if(tpcp->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swaptpcp = 1;
00116
00117 t = qswap32(swaptpcp, tpcp->bh.token) ;
00118
00119
00120
00121 LOG(DBG,"Token %d...",t,0,0,0,0) ;
00122
00123 int found_something = 0 ;
00124
00125
00126
00127
00128 if(qswap32(swaptpcp, tpcp->bh.format_number)==1) {
00129 if((sector%2)==0) {
00130 first = 0 ;
00131 last = 6 ;
00132 hsec = sector ;
00133 }
00134 else {
00135 first = 6 ;
00136 last = 12 ;
00137 hsec = sector -1 ;
00138 }
00139 }
00140 else {
00141 first = 0 ;
00142 last = 6 ;
00143 hsec = sector ;
00144 }
00145
00146
00147 LOG(DBG,"Wanted %d is in %d, first RB %d, fmt %d",sector,hsec,first,tpcp->bh.format_number,0) ;
00148
00149 LOG(DBG,"Sector %d: len %d, offset %d, len+offset %d",
00150 hsec,tpcp->sb[hsec].len,tpcp->sb[hsec].off,tpcp->sb[hsec].len+tpcp->sb[hsec].off,0) ;
00151
00152
00153 if(tpcp->sb[hsec].len == 0) {
00154 retval = 0 ;
00155 return retval;
00156 }
00157
00158
00159
00160 tot_bytes = qswap32(swaptpcp, tpcp->sb[hsec].len) * 4 ;
00161
00162
00163
00164 tot_bytes /= 2 ;
00165
00166
00167 secp = (struct TPCSECP *) ((char *)tpcp + qswap32(swaptpcp, tpcp->sb[hsec].off)*4) ;
00168 if(checkBank((char *)secp,"TPCSECP") < 0) return 0 ;
00169
00170
00171 if(t != b2h32(secp->bh.token)) {
00172 LOG(CAUTION,"Different token %d in sector %d - should be %d",
00173 b2h32(secp->bh.token),hsec,t,0,0) ;
00174 return 0 ;
00175 }
00176
00177
00178
00179
00180 LOG(DBG,"Sector %d: format number %d",hsec,b2h32(secp->bh.format_number),0,0,0) ;
00181
00182 seclp = NULL ;
00183 if(b2h32(secp->bh.format_number)==2) {
00184 if(secp->bh.w9) {
00185 seclp = (struct TPCSECLP *)((u_int *)secp + b2h32(secp->bh.w9)) ;
00186 if(checkBank((char *)seclp,CHAR_TPCSECLP) < 0) {
00187 seclp = NULL ;
00188 }
00189 }
00190 }
00191
00192
00193 if(seclp) {
00194
00195 for(rb=first;rb<last;rb++) {
00196 int rrb ;
00197
00198 if(rb >= 6) {
00199 rrb = rb - 6 ;
00200 }
00201 else {
00202 rrb = rb ;
00203 }
00204
00205 if(seclp->rb[rb].len == 0) {
00206 LOG(DBG,"RB %d has no CLD data (in SECLP)...",rb,0,0,0,0) ;
00207 continue ;
00208 }
00209
00210 rbclp = (struct TPCRBCLP *) ((char *)seclp + b2h32(seclp->rb[rb].off)*4) ;
00211 if(checkBank((char *)rbclp,CHAR_TPCRBCLP) < 0) {
00212 break ;
00213 }
00214
00215
00216
00217 if(t != l2h32(rbclp->bh.token)) {
00218 LOG(CAUTION,"Different token %d in rb %d - should be %d",
00219 l2h32(rbclp->bh.token),rb,t,0,0) ;
00220 continue ;
00221 }
00222
00223 for(mz=0;mz<3;mz++) {
00224 if(rbclp->mz[mz].len == 0) {
00225 LOG(DBG,"MZ %d has no CLD data (in RBCLP)...",mz,0,0,0,0) ;
00226 continue ;
00227 }
00228
00229 mzcld = (struct TPCMZCLD_local *)((char *)rbclp + l2h32(rbclp->mz[mz].off)*4) ;
00230 if(checkBank((char *)mzcld,CHAR_TPCMZCLD) < 0) {
00231 LOG(ERR,"Bad TPCMZCLD data bank in sector %d, RB %d, MZ %d (t %d)!",
00232 sector+1,rrb+1,mz+1,t,0) ;
00233
00234 break ;
00235
00236 }
00237
00238 if(t != l2h32(mzcld->bh.token)) {
00239 LOG(CAUTION,"Different token %d in mz %d - should be %d",
00240 l2h32(mzcld->bh.token),mz,t,0,0) ;
00241 continue ;
00242 }
00243
00244 LOG(DBG,"TPCMZCLD MZ %d, token %d",mz,l2h32(mzcld->bh.token),0,0,0) ;
00245
00246 ret = unpackCld(sector, mzcld) ;
00247 if(ret > 0) {
00248 tpc_cached->has_clusters = 1 ;
00249 found_something = 1 ;
00250 }
00251
00252 if(DAQ_RAW_FORMAT_WORD9 != l2h32(mzcld->bh.w9)) {
00253
00254
00255 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) ;
00256 }
00257
00258 }
00259
00260 }
00261 }
00262
00263
00264
00265
00266 for(rb=first;rb<last;rb++) {
00267 int rrb ;
00268
00269 if(rb >= 6) {
00270 rrb = rb - 6 ;
00271 }
00272 else {
00273 rrb = rb ;
00274 }
00275
00276 if(secp->rb[rb].len == 0) {
00277 LOG(DBG,"Sector %d: RB %d not present in raw data...",sector,rb,0,0,0) ;
00278 continue ;
00279 }
00280
00281 rbp = (struct TPCRBP *) ((char *)secp + b2h32(secp->rb[rb].off)*4) ;
00282 if(checkBank((char *)rbp,"TPCRBP") < 0) {
00283 continue ;
00284 }
00285
00286
00287
00288 tpc_cached->rdo_present[rrb] = 1;
00289
00290
00291
00292 if(t != l2h32(rbp->bh.token)) {
00293 LOG(CAUTION,"Different token %d in rb %d - should be %d",
00294 l2h32(rbp->bh.token),rb,t,0,0) ;
00295 continue ;
00296 }
00297
00298 for(mz=0;mz<3;mz++) {
00299 LOG(DBG,"RB %d, MZ %d: len %d, off %d",rb,mz,l2h32(rbp->mz[mz].len),l2h32(rbp->mz[mz].off)) ;
00300 }
00301
00302 for(mz=0;mz<3;mz++) {
00303 if(rbp->mz[mz].len == 0) continue ;
00304
00305
00306
00307 mzp = (struct TPCMZP *)((char *)rbp + l2h32(rbp->mz[mz].off)*4) ;
00308 if(checkBank((char *)mzp,"TPCMZP") < 0) {
00309 LOG(ERR,"Bad TPCMZP data bank in sector %d, RB %d, MZ %d (t %d)!",
00310 sector+1,rrb+1,mz+1,t,0) ;
00311
00312 continue ;
00313
00314 }
00315
00316 if(t != l2h32(mzp->bh.token)) {
00317 LOG(CAUTION,"Different token %d in mz %d - should be %d",
00318 l2h32(mzp->bh.token),mz,t,0,0) ;
00319 continue ;
00320 }
00321
00322 LOG(DBG,"TPCMZP token %d",l2h32(mzp->bh.token),0,0,0,0) ;
00323
00324 adcr = NULL ;
00325 cppr = NULL ;
00326 padk = NULL ;
00327 rmsr = NULL ;
00328 pedr = NULL ;
00329 adcd = NULL ;
00330 seqd = NULL ;
00331
00332
00333 if(mzp->banks[TPC_MZCLD].len != 0) {
00334 LOG(DBG,"MZCLD len 0x%08X, off 0x%08X",l2h32(mzp->banks[TPC_MZCLD].len),l2h32(mzp->banks[TPC_MZCLD].off),0,0,0) ;
00335
00336 mzcld = (struct TPCMZCLD_local *)((char *)mzp + l2h32(mzp->banks[TPC_MZCLD].off)*4) ;
00337 if(checkBank((char *)mzcld,"TPCMZCLD") < 0) ;
00338
00339
00340
00341 }
00342
00343 if(mzp->banks[TPC_SEQD].len != 0) {
00344 seqd = (struct TPCSEQD *)((char *)mzp + l2h32(mzp->banks[TPC_SEQD].off)*4) ;
00345
00346 if(checkBank((char *)seqd,CHAR_TPCSEQD) < 0) ;
00347
00348
00349 }
00350
00351 if(mzp->banks[TPC_ADCX].len != 0) {
00352 adcx = (struct TPCADCX *)((char *)mzp + l2h32(mzp->banks[TPC_ADCX].off)*4) ;
00353
00354 if(checkBank((char *)adcx,CHAR_TPCADCX) < 0) ;
00355 }
00356
00357
00358
00359
00360 if((mzp->banks[TPC_ADCD].len != 0) && (tpc_cached->mode==0)) {
00361 int rr, pp ;
00362
00363
00364 if(mzp->banks[TPC_SEQD].len == 0) {
00365 LOG(WARN,"SEQD has 0 length and ADCD doesn't???",0,0,0,0,0) ;
00366 continue ;
00367 }
00368
00369 seqd = (struct TPCSEQD *)((char *)mzp + l2h32(mzp->banks[TPC_SEQD].off)*4) ;
00370 adcd = (struct TPCADCD *)((char *)mzp + l2h32(mzp->banks[TPC_ADCD].off)*4) ;
00371
00372 if(checkBank((char *)seqd,"TPCSEQD") < 0) {
00373 LOG(ERR,"Bad TPCSEQD data bank in sector %d, RB %d, MZ %d!",
00374 sector+1,rrb+1,mz+1,0,0) ;
00375
00376 break ;
00377 }
00378
00379 if(checkBank((char *)adcd,"TPCADCD") < 0) return 0 ;
00380
00381 int len = l2h32(seqd->bh.length) - 10 ;
00382 len *= 2 ;
00383
00384 int adccou = 0 ;
00385 int jj ;
00386
00387
00388 rr = pp = 1 ;
00389
00390 for(jj=0;jj<len;jj++) {
00391 int start, last, length, stop ;
00392 u_short ss, f8 ;
00393 int tbin ;
00394
00395 ss = l2h16(seqd->seq[jj]) ;
00396 f8 = ss & 0x8000 ;
00397
00398
00399 if(f8) {
00400 pp = (ss & 0x7FFF) % 256 ;
00401 rr = (ss & 0x7FFF) / 256 ;
00402
00403 if(pp == 0xff) break ;
00404 }
00405 else {
00406 last = ss & 0x0020 ;
00407 length = ss & 0x1F ;
00408 start = (ss & 0x7FC0) >> 6 ;
00409 stop = start + length ;
00410
00411 for(tbin=start;tbin<stop;tbin++) {
00412 u_char val ;
00413 int counter ;
00414
00415 val = adcd->adc[adccou++] ;
00416
00417 if((rr > 45) || (pp > 182)) {
00418 LOG(CRIT, "rr = %d pp=%d",rr,pp);
00419 }
00420 counter = tpc_cached->counts[rr-1][pp-1] ;
00421
00422 if(counter>512) {
00423 LOG(CRIT, "%d %d %d counter = %d",ss,rr,pp,counter);
00424 }
00425
00426
00427 tpc_cached->adc[rr-1][pp-1][counter] = val ;
00428 tpc_cached->timebin[rr-1][pp-1][counter] = tbin ;
00429 tpc_cached->counts[rr-1][pp-1] += 1 ;
00430 tpc_cached->channels_sector++ ;
00431 }
00432
00433 if(last) pp++ ;
00434 }
00435
00436
00437 }
00438
00439 found_something = 1 ;
00440
00441
00442
00443 continue ;
00444 }
00445
00446
00447 padk = NULL ;
00448 if((mzp->banks[TPC_PADK].len != 0)) {
00449 padk = (struct TPCPADK *)((char *)mzp + l2h32(mzp->banks[TPC_PADK].off)*4) ;
00450 if(checkBank((char *)padk,"TPCPADK") < 0) return 0 ;
00451
00452 }
00453
00454 cppr = NULL ;
00455 if(mzp->banks[TPC_CPPR].len != 0) {
00456
00457 cppr = (struct TPCCPPR_l *)((char *)mzp + l2h32(mzp->banks[TPC_CPPR].off)*4) ;
00458 if(checkBank((char *)cppr,"TPCCPPR") < 0) return 0 ;
00459 }
00460
00461
00462
00463 if((mzp->banks[TPC_ADCR].len != 0) && (tpc_cached->mode==0)) {
00464 adcr = (struct TPCADCR_l *)((char *)mzp + l2h32(mzp->banks[TPC_ADCR].off)*4) ;
00465
00466 if(checkBank((char *)adcr,"TPCADCR") < 0) return 0 ;
00467
00468 if(unpackRaw(sector, 0, padk, cppr, (char *)adcr) < 0) {
00469 LOG(ERR,"Problems in RAW data in sector %d, RB %d, MZ %d - skipping...",
00470 sector+1,rrb+1,mz+1,0,0) ;
00471 }
00472
00473 found_something = 1 ;
00474
00475 LOG(DBG,"TPC Raw data bank in sector %d, RB %d, MZ %d!",
00476 sector+1,rrb+1,mz+1,0,0) ;
00477 continue ;
00478 }
00479
00480 if(mzp->banks[TPC_PEDR].len != 0) {
00481 pedr = (struct TPCPEDR *)((char *)mzp + l2h32(mzp->banks[TPC_PEDR].off)*4) ;
00482
00483 if(checkBank((char *)pedr,"TPCPEDR") < 0) return 0 ;
00484
00485 found_something = 1 ;
00486
00487 unpackRaw(sector, 1, padk, cppr, (char *)pedr) ;
00488 tpc_cached->mode = 1 ;
00489
00490 }
00491
00492 if(mzp->banks[TPC_RMSR].len != 0) {
00493 rmsr = (struct TPCRMSR *)((char *)mzp + l2h32(mzp->banks[TPC_RMSR].off)*4) ;
00494
00495 if(checkBank((char *)rmsr,"TPCRMSR") < 0) return 0 ;
00496
00497 unpackRaw(sector, 2, padk, cppr, (char *)rmsr) ;
00498 tpc_cached->mode = 1 ;
00499 found_something = 1 ;
00500 }
00501
00502 if(mzp->banks[TPC_GAINR].len != 0) {
00503 gainr = (struct TPCGAINR *)((char *)mzp + l2h32(mzp->banks[TPC_GAINR].off)*4) ;
00504
00505 if(checkBank((char *)gainr,CHAR_TPCGAINR) < 0) ;
00506
00507 dumpGainr(sector, rrb, gainr) ;
00508 found_something = 1 ;
00509
00510 }
00511
00512 if(mzp->banks[TPC_BADR].len != 0) {
00513 badr = (struct TPCBADR *)((char *)mzp + l2h32(mzp->banks[TPC_BADR].off)*4) ;
00514
00515 if(checkBank((char *)badr,CHAR_TPCBADR) < 0) ;
00516 found_something = 1 ;
00517 }
00518
00519
00520
00521 }
00522
00523 }
00524
00525 #ifdef SOME_OTHER_DAY
00526 if(tpc_cached->has_clusters) {
00527 int i, j ;
00528 for(i=0;i<45;i++) {
00529 printf("Row %2d: ",i+1) ;
00530 for(j=0;j<3;j++) {
00531 if(tpc_cached->cl_p[i][j]) {
00532 printf(" %d[%2d] ",j,l2h32(*tpc_cached->cl_p[i][j])) ;
00533 }
00534 }
00535 printf("\n") ;
00536 }
00537 }
00538 #endif
00539
00540
00541
00542 if(tpc_cached->mode == 1) {
00543 for(int r=0;r<45;r++) {
00544 for(int p=0;p<182;p++) {
00545 if(tpc_cached->counts[r][p] == 0) {
00546 memset(tpc_cached->adc[r][p], 0, sizeof(tpc_cached->adc[r][p]));
00547 memset(tpc_cached->timebin[r][p], 0, sizeof(tpc_cached->timebin[r][p]));
00548 }
00549 }
00550 }
00551 }
00552
00553
00554
00555 return found_something ;
00556 }
00557
00558
00559
00560
00561
00562
00563
00564 static int unpackRaw(int sec, int what, struct TPCPADK *padk, struct TPCCPPR_l *cppr, char *mem)
00565 {
00566 int i, j, t ;
00567 u_char row, pad ;
00568 u_short *cppseq ;
00569 u_char *adcseq ;
00570 u_char *adcdata ;
00571 u_short *cppdata ;
00572
00573 int timebins, cpps ;
00574
00575 timebins = 512 ;
00576 cpps = 31 ;
00577
00578 adcdata = NULL ;
00579 cppdata = NULL ;
00580
00581
00582 if(padk == NULL) {
00583 LOG(WARN,"No PADK? - skipping...",0,0,0,0,0) ;
00584 return -1 ;
00585 }
00586 if(mem == NULL) {
00587 LOG(WARN,"No DATA? - skipping...",0,0,0,0,0) ;
00588 return -1 ;
00589 }
00590
00591
00592 switch(what) {
00593 case 0 :
00594 adcdata = (u_char *) mem + sizeof(struct TPCADCR_l);
00595 if(cppr == NULL) {
00596 LOG(WARN,"No CPPR? - skipping...",0,0,0,0,0) ;
00597 return -1 ;
00598 }
00599
00600 cppdata = (u_short *)((char *)cppr + sizeof(struct TPCCPPR_l)) ;
00601 break ;
00602 case 1 :
00603 adcdata = ((struct TPCPEDR *)mem)->ped ;
00604 break ;
00605 case 2 :
00606 adcdata = ((struct TPCRMSR *)mem)->rms ;
00607 break ;
00608 default :
00609 LOG(ERR,"Unknown case variable %d",what,0,0,0,0) ;
00610 return -1 ;
00611 }
00612
00613
00614
00615 for(i=0;i<384;i++) {
00616 row = padk->rp[i].row ;
00617 pad = padk->rp[i].pad ;
00618
00619 if((row==0xFF) || (pad==0xFF)) continue ;
00620
00621
00622 row-- ;
00623 pad-- ;
00624
00625 adcseq = (u_char *) adcdata + timebins*i ;
00626
00627
00628 switch(what) {
00629 case 0 :
00630 cppseq = (u_short *)((char *) cppdata + 2*2*32*i) ;
00631
00632 for(j=0;j<cpps;j++) {
00633 u_short start, stop ;
00634 u_char val ;
00635 int counter ;
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645 start = l2h16(*cppseq++) ;
00646 stop = l2h16(*cppseq++) ;
00647
00648
00649
00650
00651 if(start & 0xFE00) break ;
00652 if(start == 511) stop = 511 ;
00653
00654
00655
00656
00657 if((stop < start) || (stop >= timebins)) {
00658 LOG(WARN,"Bad data stop<start %d<%d - skipping",stop,start,0,0,0) ;
00659 return -1 ;
00660 }
00661
00662
00663 for(t=start;t<=stop;t++) {
00664
00665 val = *(adcseq + t) ;
00666
00667 if(val == 0) {
00668
00669 if(t!=511) {
00670 LOG(WARN,"ADC==0, timebin %d, seq %d, lrow %d, row %d, pad %d",t,j,i,row+1,pad+1) ;
00671 break ;
00672
00673 }
00674 }
00675
00676
00677 counter = tpc_cached->counts[row][pad] ;
00678 tpc_cached->adc[row][pad][counter] = val ;
00679 tpc_cached->timebin[row][pad][counter] = t ;
00680 tpc_cached->counts[row][pad]++ ;
00681 tpc_cached->channels_sector++ ;
00682
00683
00684 }
00685
00686 }
00687 break ;
00688 case 1 :
00689 for(j=0;j<timebins;j++) {
00690 tpc_cached->adc[row][pad][j] = *adcseq++ ;
00691 tpc_cached->channels_sector++ ;
00692 tpc_cached->counts[row][pad]++;
00693 }
00694 break ;
00695 case 2 :
00696 for(j=0;j<timebins;j++) {
00697 tpc_cached->timebin[row][pad][j] = *adcseq++ ;
00698 tpc_cached->counts[row][pad]++;
00699 }
00700 break ;
00701 }
00702
00703
00704 }
00705
00706
00707 return 0 ;
00708
00709 }
00710
00711
00712 static int unpackCld(int sec, struct TPCMZCLD_local *mzcld)
00713 {
00714 u_int i, j ;
00715 u_int nrows, nclust, row ;
00716 u_int *rdata ;
00717 u_int tot_hits ;
00718
00719 tot_hits = 0 ;
00720
00721 int bytes = l2h32(mzcld->bh.length)*4 ;
00722 if(bytes == sizeof(struct bankHeader)) {
00723 LOG(DBG,"No data in TPCMZCLD...",0,0,0,0,0) ;
00724 return 0 ;
00725 }
00726
00727 nrows = l2h32(mzcld->rows) ;
00728
00729 if(nrows == 0) {
00730 LOG(NOTE,"No MZCLD rows?...",0,0,0,0,0) ;
00731 return tot_hits ;
00732 }
00733
00734 u_int ticks = l2h32(mzcld->bh.w9) ;
00735
00736
00737 rdata = (u_int *) mzcld->padrowFiller ;
00738
00739 LOG(DBG,"unpackCld: 0x%08X 0x%08X %d: 0x%08X 0x%08X",mzcld,rdata,rdata-(u_int*)mzcld,*rdata,*(rdata+1),0) ;
00740
00741 row = 0 ;
00742
00743 for(i=0;i<nrows;i++) {
00744
00745
00746
00747 row = l2h32(*rdata++) ;
00748 nclust = l2h32(*rdata++) ;
00749
00750
00751
00752 int instance ;
00753 for(instance=0;instance<3;instance++) {
00754 if(tpc_cached->cl_p[row-1][instance]) continue ;
00755 tpc_cached->cl_p[row-1][instance] = rdata -2 ;
00756 break ;
00757 }
00758
00759
00760
00761 if(instance >= 3) {
00762 LOG(ERR,"Too many row %d contributions!",row) ;
00763 }
00764
00765 LOG(DBG,"Row %d (%d of %d), %d clusters (tot bytes %d)...",row,i,nrows,nclust,bytes) ;
00766
00767
00768
00769 for(j=0;j<nclust;j++) {
00770 u_short pad, tm, flags, charge, fl ;
00771 u_int t1, t2, p1, p2 ;
00772 u_int pt, cf ;
00773
00774 pt = l2h32(*rdata++) ;
00775 cf = l2h32(*rdata++) ;
00776
00777
00778 tm = (pt >> 16) & 0x7FFF ;
00779 pad = pt & 0x3FFF ;
00780 charge = cf >> 16 ;
00781 fl = cf & 0xFFFF ;
00782
00783 flags = 0 ;
00784 if(pt & 0x8000) flags |= FCF_DOUBLE_PAD ;
00785 if(pt & 0x4000) flags |= FCF_DEAD_EDGE ;
00786 if(pt & (0x8000 << 16)) flags |= FCF_ONEPAD ;
00787 if(fl & 0x8000) flags |= FCF_ROW_EDGE ;
00788 if(fl & 0x4000) flags |= FCF_BROKEN_EDGE ;
00789
00790 t2 = (fl & 0x00F0) >> 4 ;
00791 t1 = fl & 0x000F ;
00792
00793 t2 = (tm >> 6) + t2 ;
00794 t1 = (tm >> 6) - t1 ;
00795
00796 p2 = (fl & 0x3800) >> 11 ;
00797 p1 = (fl & 0x0700) >> 8 ;
00798
00799 p2 = (pad >> 6) + p2 ;
00800 p1 = (pad >> 6) - p1 ;
00801
00802
00803 double dpad, dtimebin ;
00804
00805
00806
00807
00808
00809
00810 dpad = (double)(pad)/64.0 ;
00811 dtimebin = (double)(tm)/64.0 ;
00812
00813 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) ;
00814
00815 u_int ix = tpc_cached->cl_counts[row-1] ;
00816 if(ix < TPC_READER_MAX_CLUSTERS) {
00817 tpc_cached->cl[row-1][ix].p = (float) dpad ;
00818 tpc_cached->cl[row-1][ix].t = (float) dtimebin ;
00819 tpc_cached->cl[row-1][ix].charge = charge ;
00820 tpc_cached->cl[row-1][ix].flags = flags ;
00821 tpc_cached->cl[row-1][ix].t1 = t1 ;
00822 tpc_cached->cl[row-1][ix].t2 = t2 ;
00823 tpc_cached->cl[row-1][ix].p1 = p1 ;
00824 tpc_cached->cl[row-1][ix].p2 = p2 ;
00825
00826
00827
00828
00829 tpc_cached->cl_counts[row-1]++ ;
00830 tot_hits++ ;
00831 }
00832 else {
00833 LOG(WARN,"Too many clusters (%d) in sector %d, row %d",ix,sec+1,row,0,0) ;
00834 return -1 ;
00835 }
00836
00837
00838
00839
00840 }
00841
00842 }
00843
00844
00845 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) ;
00846
00847 return tot_hits ;
00848 }
00849
00850 static int dumpGainr(int sec, int rrb, struct TPCGAINR *g)
00851 {
00852 static int inited ;
00853 static struct blah {
00854 u_short row, pad ;
00855 } seq[6][3][6*256] ;
00856
00857 int i ;
00858 int rmz = l2h32(g->bh.bank_id) ;
00859
00860
00861
00862
00863
00864 rmz -= 1 ;
00865
00866 if(!inited) {
00867 u_int vram, row, pad ;
00868
00869 inited = 1 ;
00870
00871
00872 int rb, mz ;
00873
00874 for(rb=1;rb<=6;rb++) {
00875 for(mz=0;mz<3;mz++) {
00876 for(i=0;i<MZ_MAX_PADS;i++) {
00877 int as = i/64 ;
00878
00879 if(as > 5) break ;
00880
00881
00882 if(as <= 2) {
00883 vram = as * 0x8000 ;
00884 }
00885 else {
00886 vram = (as+1)*0x8000 ;
00887 }
00888
00889 vram += (i%64)*0x200 ;
00890
00891
00892 vram >>= 9 ;
00893
00894
00895 vram |= (rb<<12) | (mz<<9);
00896
00897
00898
00899 for(row=1;row<=45;row++) {
00900 for(pad=1;pad<=182;pad++) {
00901 if((adc_offset[row][pad] & 0xFFFF) == (int)vram) {
00902 seq[rb-1][mz][i].row = row ;
00903 seq[rb-1][mz][i].pad = pad ;
00904 break ;
00905 }
00906 }
00907 }
00908
00909 }
00910 }
00911 }
00912
00913 }
00914
00915 for(i=0;i<MZ_MAX_PADS;i++) {
00916 if(seq[rrb][rmz][i].row == 0) continue ;
00917
00918 }
00919
00920
00921 return 0 ;
00922 }