00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 #include <Stiostream.h>
00127 #include <assert.h>
00128 #include "StDaqLib/GENERIC/EventReader.hh"
00129 #include "TPCV2P0.hh"
00130
00131 #define MAKE_THE_DAMNED_COMPILER_SILENT
00132
00133
00134 #include "fee_pin.h"
00135
00136 using namespace OLDEVP;
00137
00138 TPCV2P0_ZS_SR::TPCV2P0_ZS_SR(int s, TPCV2P0_Reader *det, char mergeSequences)
00139 {
00140 assert(mergeSequences==0||mergeSequences==1);
00141 mMergeSequences=mergeSequences;
00142
00143 sector = s-1;
00144 detector = det;
00145 if (detector->ercpy->verbose){
00146 cout << "Constructing TPCV2P0_ZS_SR" << endl
00147 << " MergeSequence is " << (mMergeSequences?"ON":"OFF") << endl;
00148 }
00149
00150
00151 memset((char *)adcd_p, 0, sizeof(adcd_p));
00152 memset((char *)adcx_p, 0, sizeof(adcx_p));
00153 memset((char *)seqd_p, 0, sizeof(seqd_p));
00154
00155 for(int ii=TPC_PADROWS-1;ii>=0;ii--) RowSpacePts[ii]=0;
00156
00157 }
00158
00159 int TPCV2P0_ZS_SR::initialize()
00160 {
00161 for (int row=0; row<TPC_PADROWS; row++) {
00162 for (int pad=0; pad<TPC_MAXPADS; pad++) {
00163 Pad_array[row][pad].nseq=0;
00164 Pad_array[row][pad].seq= (Sequence *)0;
00165 }
00166 }
00167
00168
00169
00170
00171 int rcb;
00172
00173 for(rcb = 0; rcb < 6; rcb++)
00174 {
00175 for(int mz = 0; mz < 3; mz++)
00176 {
00177 adcd_p[rcb][mz] = detector->getBankTPCADCD(sector,rcb,mz);
00178 if ((void *)adcd_p[rcb][mz] != NULL) {
00179
00180 fflush(stdout);
00181 }
00182 adcx_p[rcb][mz] = detector->getBankTPCADCX(sector,rcb,mz);
00183 if ((void *)adcx_p[rcb][mz] != NULL) {
00184
00185 fflush(stdout);
00186 }
00187 seqd_p[rcb][mz] = detector->getBankTPCSEQD(sector,rcb,mz);
00188 if ((void *)seqd_p[rcb][mz] != NULL) {
00189
00190 fflush(stdout);
00191 }
00192 }
00193 }
00194
00195
00196
00197
00198
00199
00200
00201 for(rcb = 0; rcb < 6; rcb++) {
00202 for(int mz = 0; mz < 3; mz++) {
00203 if (seqd_p[rcb][mz] == (classname(Bank_TPCSEQD) *)NULL) {
00204
00205
00206 TPCV2P0_PADK_SR *padkr = detector->getPADKReader(sector);
00207
00208 if (!padkr) continue;
00209 classname(Bank_TPCADCR) *adcr =
00210 detector->getBankTPCADCR(sector,rcb,mz) ;
00211
00212 if (!adcr) continue;
00213 classname(Bank_TPCCPPR) *cppr =
00214 detector->getBankTPCCPPR(sector,rcb,mz) ;
00215
00216 if (!cppr) continue;
00217
00218
00219 if (detector->ercpy->verbose)
00220 printf("reconstructing from RAW banks: SEC%d RB%d MZ%d\n",sector,rcb+1,mz+1);
00221 PADK_entry ent;
00222
00223 for (int row=1; row<=TPC_PADROWS; row++) {
00224 for (int pad=1; pad<=tpcRowLen[row-1]; pad++) {
00225 padkr->get(row, pad, &ent);
00226 if((ent.mz != mz+1) || (ent.rb != rcb+1)) continue;
00227 struct ASIC_Cluster *clusters =
00228 (ASIC_Cluster *)(cppr->entry + 32*ent.offset);
00229
00230 int lastbin = -2, len = 0;
00231 int i, start, stop=-1, newseq;
00232 for (i=0; i<TPC_MXSEQUENCE; i++) {
00233 start = clusters[i].start_time_bin;
00234 if ((start < 0)||(start==511)||(start<=stop)) break;
00235
00236 newseq = (start>lastbin+1);
00237 stop = clusters[i].stop_time_bin;
00238
00239
00240 if ( stop<start || start<0 || start>511 || stop<0 || stop>511) {
00241 struct EventInfo ei;
00242 ei = detector->ercpy->getEventInfo();
00243 printf("%s:%d: bad TPC cluster data detected\n",
00244 __FILE__,__LINE__);
00245 printf("evt# %d , sector %d, RB %d, MZ %d row %d, pad %d\n",
00246 ei.EventSeqNo,sector+1,rcb+1,mz+1,row,pad);
00247 printf("cluster %d: start=0x%x stop=0x%x\n",
00248 i,clusters[i].start_time_bin,clusters[i].stop_time_bin);
00249 break;
00250 }
00251 lastbin = stop;
00252 if (newseq) Pad_array[row-1][pad-1].nseq++;
00253
00254
00255 }
00256 if (i==TPC_MXSEQUENCE) {
00257
00258
00259 }
00260
00261
00262
00263 int nseq = Pad_array[row-1][pad-1].nseq;
00264 if (!nseq) continue;
00265 Pad_array[row-1][pad-1].seq=
00266 (Sequence *)malloc(nseq*sizeof(Sequence));
00267 if (Pad_array[row-1][pad-1].seq==NULL) {
00268 cout << "failed to malloc() Sequence structures " << endl;
00269 return FALSE;
00270 }
00271 lastbin = -2;
00272 len = 0;
00273 int seqCnt = 0;
00274
00275
00276 for (i=0; seqCnt<nseq; i++) {
00277 if (i<TPC_MXSEQUENCE) {
00278 start = clusters[i].start_time_bin;
00279 if ((start < 0)||(start==511)) break;
00280 newseq = (start>lastbin+1);
00281
00282 stop = clusters[i].stop_time_bin;
00283 len = stop - start + 1;
00284
00285 if ( stop<start || start<0 || start>511 || stop<0 || stop>511) {
00286 struct EventInfo ei;
00287 ei = detector->ercpy->getEventInfo();
00288 printf("%s:%d: bad TPC cluster data detected\n",
00289 __FILE__,__LINE__);
00290 printf("evt# %d , sector %d, RB %d, MZ %d row %d, pad %d\n",
00291 ei.EventSeqNo,sector+1,rcb+1,mz+1,row,pad);
00292 printf("cluster %d: start=0x%x stop=0x%x\n",
00293 i,clusters[i].start_time_bin,clusters[i].stop_time_bin);
00294 break;
00295 }
00296 lastbin = stop;
00297 if (newseq) {
00298 int offset = ent.offset * padkr->getADCBytes() + start;
00299 Pad_array[row-1][pad-1].seq[seqCnt].startTimeBin = start;
00300 Pad_array[row-1][pad-1].seq[seqCnt].Length = len;
00301 Pad_array[row-1][pad-1].seq[seqCnt].FirstAdc =
00302 (u_char *)(adcr->ADC + offset);
00303 seqCnt++;
00304 }
00305 else {
00306 assert( seqCnt>=1 && seqCnt<=nseq );
00307 Pad_array[row-1][pad-1].seq[seqCnt-1].Length += len;
00308 }
00309 }
00310 else {
00311
00312 }
00313 }
00314 }
00315 }
00316 }
00317
00318 else {
00319
00320
00321 int padrow=-1, pad=-1, lastbin=-2, oldstart=0;
00322 int len = seqd_p[rcb][mz]->header.BankLength - (sizeof(Bank_Header)/4);
00323 int numseq = (4*len)/sizeof(short);
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334 int i=0,iOld=-1;
00335 while (i<numseq) {
00336 if(i<=iOld) return FALSE;
00337 iOld=i;
00338 if (seqd_p[rcb][mz]->sequence[i]<0) {
00339 padrow = (seqd_p[rcb][mz]->sequence[i]>>8)& 0x7f;
00340 pad = (seqd_p[rcb][mz]->sequence[i])& 0xff;
00341 if (pad==255) break;
00342
00343 oldstart = 0;
00344 lastbin = -2;
00345 i++;
00346 }
00347 else {
00348 if (padrow <0 || pad <0) {
00349 printf("encountered bad SEQD bank RB %d, Mezz %d\n", rcb+1, mz+1);
00350 return FALSE;
00351 }
00352
00353 unsigned short work = seqd_p[rcb][mz]->sequence[i];
00354 int start = work>>6;
00355 int len = work & 0x1f;
00356 if (start >= oldstart) {
00357
00358 if(mMergeSequences) { if (start>lastbin+1) Pad_array[padrow-1][pad-1].nseq++; }
00359 else { Pad_array[padrow-1][pad-1].nseq++; }
00360
00361
00362 lastbin = start+len-1;
00363 oldstart = start;
00364 if (work & 0x20 ) {
00365 assert(padrow>=1&&pad>=1);
00366 int nseq = Pad_array[padrow-1][pad-1].nseq;
00367 if (nseq) {
00368
00369 if (Pad_array[padrow-1][pad-1].seq) {
00370 printf("ERROR DETECTED: Pad_array[%d][%d] already malloced\n",
00371 padrow-1,pad-1);
00372 return FALSE;
00373 }
00374 Pad_array[padrow-1][pad-1].seq=
00375 (Sequence *)malloc(nseq*sizeof(Sequence));
00376 if (Pad_array[padrow-1][pad-1].seq==NULL) {
00377 cout << "failed to malloc() Sequence structures " << endl;
00378 return FALSE;
00379 }
00380 }
00381 pad++;
00382 lastbin = -2;
00383 oldstart=0;
00384 }
00385 i++;
00386 continue;
00387 }
00388 else {
00389 if (detector->ercpy->verbose) printf("new pad detected with bit 5 clear!\n");
00390 fflush(stdout);
00391
00392
00393
00394 while (seqd_p[rcb][mz]->sequence[i]>0 && i<numseq) i++;
00395
00396 assert(padrow>=1&&pad>=1);
00397 int nseq = Pad_array[padrow-1][pad-1].nseq;
00398 if (nseq) {
00399
00400
00401 if (Pad_array[padrow-1][pad-1].seq==NULL) {
00402 Pad_array[padrow-1][pad-1].seq=
00403 (Sequence *)malloc(nseq*sizeof(Sequence));
00404 if (Pad_array[padrow-1][pad-1].seq==NULL) {
00405 cout << "failed to malloc() Sequence structures " << endl;
00406 return FALSE;
00407 }
00408 }
00409 }
00410 }
00411 }
00412 }
00413 int nseq;
00414
00415 if (padrow <1 || pad <1) {
00416 nseq = 0;
00417
00418
00419 } else {
00420 nseq = Pad_array[padrow-1][pad-1].nseq;
00421 }
00422
00423 if (nseq) {
00424
00425
00426 if (Pad_array[padrow-1][pad-1].seq==NULL) {
00427 Pad_array[padrow-1][pad-1].seq=
00428 (Sequence *)malloc(nseq*sizeof(Sequence));
00429 if (Pad_array[padrow-1][pad-1].seq==NULL) {
00430 cout << "failed to malloc() Sequence structures " << endl;
00431 return FALSE;
00432 }
00433 }
00434 }
00435
00436
00437 u_char *adc_locn = (u_char *)adcd_p[rcb][mz]->ADC;
00438 padrow=-1; pad=-1; lastbin=-2; oldstart = 0; i=0;
00439 int pad_seq;
00440
00441 while (i<numseq) {
00442 if (seqd_p[rcb][mz]->sequence[i]<0) {
00443 padrow = (seqd_p[rcb][mz]->sequence[i]>>8)& 0x7f;
00444 pad = (seqd_p[rcb][mz]->sequence[i])& 0xff;
00445 if (pad==255) break;
00446 pad_seq = 0;
00447 oldstart=0;
00448 lastbin = -2;
00449 i++;
00450 }
00451 else {
00452 unsigned short work = seqd_p[rcb][mz]->sequence[i];
00453 int start = work>>6;
00454 int len = work & 0x1f;
00455
00456 if (start >= oldstart) {
00457
00458
00459 if(mMergeSequences) {
00460 if (start>lastbin+1) {
00461 if (pad_seq>=Pad_array[padrow-1][pad-1].nseq) {
00462 printf("sequence overrun %s %d row %d pad %d seq %d\n",
00463 __FILE__,__LINE__,padrow,pad,pad_seq);
00464 }
00465
00466 Pad_array[padrow-1][pad-1].seq[pad_seq].startTimeBin = start;
00467 Pad_array[padrow-1][pad-1].seq[pad_seq].Length = len;
00468 Pad_array[padrow-1][pad-1].seq[pad_seq].FirstAdc = adc_locn;
00469 adc_locn +=len;
00470 pad_seq++;
00471 }
00472 else {
00473 assert ( pad_seq>=1 && pad_seq<=Pad_array[padrow-1][pad-1].nseq);
00474
00475 if (pad_seq>Pad_array[padrow-1][pad-1].nseq)
00476 printf("sequence overrun %s %d row %d pad %d seq %d\n",
00477 __FILE__,__LINE__,padrow,pad,pad_seq);
00478 Pad_array[padrow-1][pad-1].seq[pad_seq-1].Length += len;
00479 adc_locn +=len;
00480 }
00481 } else {
00482
00483 if (pad_seq>=Pad_array[padrow-1][pad-1].nseq) {
00484 printf("sequence overrun %s %d row %d pad %d seq %d\n",
00485 __FILE__,__LINE__,padrow,pad,pad_seq);
00486 }
00487
00488 Pad_array[padrow-1][pad-1].seq[pad_seq].startTimeBin = start;
00489 Pad_array[padrow-1][pad-1].seq[pad_seq].Length = len;
00490 Pad_array[padrow-1][pad-1].seq[pad_seq].FirstAdc = adc_locn;
00491 adc_locn +=len;
00492 pad_seq++;
00493 }
00494
00495 lastbin = start+len-1;
00496 oldstart = start;
00497 if (work & 0x20) {
00498 pad++;
00499 pad_seq = 0;
00500 lastbin = -2;
00501 oldstart=0;
00502 }
00503 i++;
00504 }
00505 else {
00506 if (detector->ercpy->verbose) {
00507 printf("new pad detected with bit 5 clear!\n");
00508 fflush(stdout);
00509 }
00510
00511 while (seqd_p[rcb][mz]->sequence[i]>0 && i<numseq) i++;
00512 }
00513 }
00514 }
00515 }
00516 }
00517 }
00518
00519 return TRUE;
00520 }
00521
00522 TPCV2P0_ZS_SR::~TPCV2P0_ZS_SR()
00523 {
00524
00525
00526
00527
00528
00529
00530
00531
00532 for (int row=0; row<TPC_PADROWS; row++) {
00533 for (int pad=0; pad<tpcRowLen[row]; pad++) {
00534 void *memaddr = Pad_array[row][pad].seq;
00535 if (memaddr) free(memaddr);
00536 }
00537 }
00538 for (int rrow=0; rrow<TPC_PADROWS; rrow++) {
00539 if (RowSpacePts[rrow]) free(RowSpacePts[rrow]);
00540 }
00541 }
00542
00543 int TPCV2P0_ZS_SR::getPadList(int PadRow, u_char **padList)
00544 {
00545
00546 int pad;
00547 if (PadRow == 0 || PadRow > TPC_PADROWS) return -1;
00548
00549
00550 int npad=0;
00551 for (pad=1; pad<=tpcRowLen[PadRow-1]; pad++)
00552 {
00553 if (Pad_array[PadRow-1][pad-1].nseq) padlist[PadRow-1][npad++] = pad;
00554 }
00555
00556 *padList = &padlist[PadRow-1][0];
00557
00558 return npad;
00559 }
00560
00561 int TPCV2P0_ZS_SR::getSequences(int PadRow, int Pad, int *nSeq,
00562 Sequence **SeqData)
00563 {
00564 *nSeq = Pad_array[PadRow-1][Pad-1].nseq;
00565 *SeqData = Pad_array[PadRow-1][Pad-1].seq;
00566 return 0;
00567 }
00568
00569 int TPCV2P0_ZS_SR::getFeeSequences(int Fee, int Pin, int *nSeq,
00570 Sequence **SeqData)
00571 {
00572 unsigned short PadRow = row_vs_fee[Fee][Pin];
00573 unsigned short Pad = pad_vs_fee[Fee][Pin];
00574 *nSeq = Pad_array[PadRow-1][Pad-1].nseq;
00575 *SeqData = Pad_array[PadRow-1][Pad-1].seq;
00576 return 0;
00577 }
00578
00579
00580
00581 int TPCV2P0_ZS_SR::getSpacePts(int PadRow, int *nSpacePts, SpacePt **SpacePts)
00582 {
00583 int rb, row;
00584 classname(Bank_TPCMZCLD) *cld;
00585 int numSpPts = 0;
00586
00587 for (row=0; row<TPC_PADROWS; nsptrow[row++]=0);
00588
00589 for (rb=0; rb<6; rb++) {
00590 for (int mz=0; mz<3; mz++) {
00591
00592 cld_p[rb][mz] = detector->getBankTPCMZCLD(sector,rb,mz) ;
00593 cld = cld_p[rb][mz];
00594 if (!cld) continue;
00595 int *ptr = (int *)&cld->stuff;
00596 for (int ir=0; ir<cld->NumRows; ir++){
00597 int row = *ptr++;
00598 int nsp = *ptr++;
00599 nsptrow[row-1] += nsp;
00600 ptr += 2*nsp;
00601 }
00602 }
00603 }
00604 if (detector->ercpy->verbose) cout << "sector "<<sector<<": found "
00605 <<numSpPts<<" space pts" <<endl;
00606
00607 for (row=0; row<TPC_PADROWS; row++) {
00608 RowSpacePts[row] = (SpacePt *)malloc(nsptrow[row]*sizeof(struct SpacePt));
00609 if (RowSpacePts[row]==NULL) {
00610 cout << "failed to malloc() Space Point structures " << endl;
00611 return FALSE;
00612 }
00613 }
00614
00615 for (rb=0; rb<6; rb++) {
00616 for (int mz=0; mz<3; mz++) {
00617 cld = cld_p[rb][mz];
00618 if (!cld) continue;
00619 int *ptr = &cld->stuff[0];
00620 for (int ir=0; ir<cld->NumRows; ir++){
00621 int row = *ptr++;
00622 int nsp = *ptr++;
00623 for (int isp=0; isp<nsp; isp++, ptr+=2) {
00624 RowSpacePts[row-1][isp] = *(SpacePt *)ptr;
00625 }
00626 }
00627 }
00628 }
00629 *nSpacePts = nsptrow[PadRow-1];
00630 *SpacePts = RowSpacePts[PadRow-1];
00631 return TRUE;
00632 }
00633
00634
00635 int TPCV2P0_ZS_SR::MemUsed()
00636 {
00637 return 0;
00638 }
00639