128 #include "StDaqLib/GENERIC/EventReader.hh"
129 #include "TPCV2P0.hh"
131 #define MAKE_THE_DAMNED_COMPILER_SILENT
136 using namespace OLDEVP;
138 TPCV2P0_ZS_SR::TPCV2P0_ZS_SR(
int s,
TPCV2P0_Reader *det,
char mergeSequences)
140 assert(mergeSequences==0||mergeSequences==1);
141 mMergeSequences=mergeSequences;
145 if (detector->ercpy->verbose){
146 cout <<
"Constructing TPCV2P0_ZS_SR" << endl
147 <<
" MergeSequence is " << (mMergeSequences?
"ON":
"OFF") << endl;
151 memset((
char *)adcd_p, 0,
sizeof(adcd_p));
152 memset((
char *)adcx_p, 0,
sizeof(adcx_p));
153 memset((
char *)seqd_p, 0,
sizeof(seqd_p));
155 for(
int ii=TPC_PADROWS-1;ii>=0;ii--) RowSpacePts[ii]=0;
159 int TPCV2P0_ZS_SR::initialize()
161 for (
int row=0; row<TPC_PADROWS; row++) {
162 for (
int pad=0; pad<TPC_MAXPADS; pad++) {
163 Pad_array[row][pad].nseq=0;
164 Pad_array[row][pad].seq= (
Sequence *)0;
173 for(rcb = 0; rcb < 6; rcb++)
175 for(
int mz = 0; mz < 3; mz++)
177 adcd_p[rcb][mz] = detector->getBankTPCADCD(sector,rcb,mz);
178 if ((
void *)adcd_p[rcb][mz] != NULL) {
182 adcx_p[rcb][mz] = detector->getBankTPCADCX(sector,rcb,mz);
183 if ((
void *)adcx_p[rcb][mz] != NULL) {
187 seqd_p[rcb][mz] = detector->getBankTPCSEQD(sector,rcb,mz);
188 if ((
void *)seqd_p[rcb][mz] != NULL) {
201 for(rcb = 0; rcb < 6; rcb++) {
202 for(
int mz = 0; mz < 3; mz++) {
203 if (seqd_p[rcb][mz] == (classname(Bank_TPCSEQD) *)NULL) {
208 if (!padkr)
continue;
209 classname(Bank_TPCADCR) *adcr =
210 detector->getBankTPCADCR(sector,rcb,mz) ;
213 classname(Bank_TPCCPPR) *cppr =
214 detector->getBankTPCCPPR(sector,rcb,mz) ;
219 if (detector->ercpy->verbose)
220 printf(
"reconstructing from RAW banks: SEC%d RB%d MZ%d\n",sector,rcb+1,mz+1);
223 for (
int row=1; row<=TPC_PADROWS; row++) {
224 for (
int pad=1; pad<=tpcRowLen[row-1]; pad++) {
225 padkr->get(row, pad, &ent);
226 if((ent.mz != mz+1) || (ent.rb != rcb+1))
continue;
230 int lastbin = -2, len = 0;
231 int i, start, stop=-1, newseq;
232 for (i=0; i<TPC_MXSEQUENCE; i++) {
233 start = clusters[i].start_time_bin;
234 if ((start < 0)||(start==511)||(start<=stop))
break;
236 newseq = (start>lastbin+1);
237 stop = clusters[i].stop_time_bin;
240 if ( stop<start || start<0 || start>511 || stop<0 || stop>511) {
242 ei = detector->ercpy->getEventInfo();
243 printf(
"%s:%d: bad TPC cluster data detected\n",
245 printf(
"evt# %d , sector %d, RB %d, MZ %d row %d, pad %d\n",
246 ei.EventSeqNo,sector+1,rcb+1,mz+1,row,pad);
247 printf(
"cluster %d: start=0x%x stop=0x%x\n",
248 i,clusters[i].start_time_bin,clusters[i].stop_time_bin);
252 if (newseq) Pad_array[row-1][pad-1].nseq++;
256 if (i==TPC_MXSEQUENCE) {
263 int nseq = Pad_array[row-1][pad-1].nseq;
265 Pad_array[row-1][pad-1].seq=
267 if (Pad_array[row-1][pad-1].seq==NULL) {
268 cout <<
"failed to malloc() Sequence structures " << endl;
276 for (i=0; seqCnt<nseq; i++) {
277 if (i<TPC_MXSEQUENCE) {
278 start = clusters[i].start_time_bin;
279 if ((start < 0)||(start==511))
break;
280 newseq = (start>lastbin+1);
282 stop = clusters[i].stop_time_bin;
283 len = stop - start + 1;
285 if ( stop<start || start<0 || start>511 || stop<0 || stop>511) {
287 ei = detector->ercpy->getEventInfo();
288 printf(
"%s:%d: bad TPC cluster data detected\n",
290 printf(
"evt# %d , sector %d, RB %d, MZ %d row %d, pad %d\n",
291 ei.EventSeqNo,sector+1,rcb+1,mz+1,row,pad);
292 printf(
"cluster %d: start=0x%x stop=0x%x\n",
293 i,clusters[i].start_time_bin,clusters[i].stop_time_bin);
298 int offset = ent.offset * padkr->getADCBytes() + start;
299 Pad_array[row-1][pad-1].seq[seqCnt].startTimeBin = start;
300 Pad_array[row-1][pad-1].seq[seqCnt].Length = len;
301 Pad_array[row-1][pad-1].seq[seqCnt].FirstAdc =
302 (u_char *)(adcr->ADC + offset);
306 assert( seqCnt>=1 && seqCnt<=nseq );
307 Pad_array[row-1][pad-1].seq[seqCnt-1].Length += len;
321 int padrow=-1, pad=-1, lastbin=-2, oldstart=0;
322 int len = seqd_p[rcb][mz]->header.BankLength - (
sizeof(
Bank_Header)/4);
323 int numseq = (4*len)/
sizeof(
short);
336 if(i<=iOld)
return FALSE;
338 if (seqd_p[rcb][mz]->sequence[i]<0) {
339 padrow = (seqd_p[rcb][mz]->sequence[i]>>8)& 0x7f;
340 pad = (seqd_p[rcb][mz]->sequence[i])& 0xff;
348 if (padrow <0 || pad <0) {
349 printf(
"encountered bad SEQD bank RB %d, Mezz %d\n", rcb+1, mz+1);
353 unsigned short work = seqd_p[rcb][mz]->sequence[i];
355 int len = work & 0x1f;
356 if (start >= oldstart) {
358 if(mMergeSequences) {
if (start>lastbin+1) Pad_array[padrow-1][pad-1].nseq++; }
359 else { Pad_array[padrow-1][pad-1].nseq++; }
362 lastbin = start+len-1;
365 assert(padrow>=1&&pad>=1);
366 int nseq = Pad_array[padrow-1][pad-1].nseq;
369 if (Pad_array[padrow-1][pad-1].seq) {
370 printf(
"ERROR DETECTED: Pad_array[%d][%d] already malloced\n",
374 Pad_array[padrow-1][pad-1].seq=
376 if (Pad_array[padrow-1][pad-1].seq==NULL) {
377 cout <<
"failed to malloc() Sequence structures " << endl;
389 if (detector->ercpy->verbose) printf(
"new pad detected with bit 5 clear!\n");
394 while (seqd_p[rcb][mz]->sequence[i]>0 && i<numseq) i++;
396 assert(padrow>=1&&pad>=1);
397 int nseq = Pad_array[padrow-1][pad-1].nseq;
401 if (Pad_array[padrow-1][pad-1].seq==NULL) {
402 Pad_array[padrow-1][pad-1].seq=
404 if (Pad_array[padrow-1][pad-1].seq==NULL) {
405 cout <<
"failed to malloc() Sequence structures " << endl;
415 if (padrow <1 || pad <1) {
420 nseq = Pad_array[padrow-1][pad-1].nseq;
426 if (Pad_array[padrow-1][pad-1].seq==NULL) {
427 Pad_array[padrow-1][pad-1].seq=
429 if (Pad_array[padrow-1][pad-1].seq==NULL) {
430 cout <<
"failed to malloc() Sequence structures " << endl;
437 u_char *adc_locn = (u_char *)adcd_p[rcb][mz]->ADC;
438 padrow=-1; pad=-1; lastbin=-2; oldstart = 0; i=0;
442 if (seqd_p[rcb][mz]->sequence[i]<0) {
443 padrow = (seqd_p[rcb][mz]->sequence[i]>>8)& 0x7f;
444 pad = (seqd_p[rcb][mz]->sequence[i])& 0xff;
452 unsigned short work = seqd_p[rcb][mz]->sequence[i];
454 int len = work & 0x1f;
456 if (start >= oldstart) {
459 if(mMergeSequences) {
460 if (start>lastbin+1) {
461 if (pad_seq>=Pad_array[padrow-1][pad-1].nseq) {
462 printf(
"sequence overrun %s %d row %d pad %d seq %d\n",
463 __FILE__,__LINE__,padrow,pad,pad_seq);
466 Pad_array[padrow-1][pad-1].seq[pad_seq].startTimeBin = start;
467 Pad_array[padrow-1][pad-1].seq[pad_seq].Length = len;
468 Pad_array[padrow-1][pad-1].seq[pad_seq].FirstAdc = adc_locn;
473 assert ( pad_seq>=1 && pad_seq<=Pad_array[padrow-1][pad-1].nseq);
475 if (pad_seq>Pad_array[padrow-1][pad-1].nseq)
476 printf(
"sequence overrun %s %d row %d pad %d seq %d\n",
477 __FILE__,__LINE__,padrow,pad,pad_seq);
478 Pad_array[padrow-1][pad-1].seq[pad_seq-1].Length += len;
483 if (pad_seq>=Pad_array[padrow-1][pad-1].nseq) {
484 printf(
"sequence overrun %s %d row %d pad %d seq %d\n",
485 __FILE__,__LINE__,padrow,pad,pad_seq);
488 Pad_array[padrow-1][pad-1].seq[pad_seq].startTimeBin = start;
489 Pad_array[padrow-1][pad-1].seq[pad_seq].Length = len;
490 Pad_array[padrow-1][pad-1].seq[pad_seq].FirstAdc = adc_locn;
495 lastbin = start+len-1;
506 if (detector->ercpy->verbose) {
507 printf(
"new pad detected with bit 5 clear!\n");
511 while (seqd_p[rcb][mz]->sequence[i]>0 && i<numseq) i++;
522 TPCV2P0_ZS_SR::~TPCV2P0_ZS_SR()
532 for (
int row=0; row<TPC_PADROWS; row++) {
533 for (
int pad=0; pad<tpcRowLen[row]; pad++) {
534 void *memaddr = Pad_array[row][pad].seq;
535 if (memaddr) free(memaddr);
538 for (
int rrow=0; rrow<TPC_PADROWS; rrow++) {
539 if (RowSpacePts[rrow]) free(RowSpacePts[rrow]);
543 int TPCV2P0_ZS_SR::getPadList(
int PadRow, u_char **padList)
547 if (PadRow == 0 || PadRow > TPC_PADROWS)
return -1;
551 for (pad=1; pad<=tpcRowLen[PadRow-1]; pad++)
553 if (Pad_array[PadRow-1][pad-1].nseq) padlist[PadRow-1][npad++] = pad;
556 *padList = &padlist[PadRow-1][0];
561 int TPCV2P0_ZS_SR::getSequences(
int PadRow,
int Pad,
int *nSeq,
564 *nSeq = Pad_array[PadRow-1][Pad-1].nseq;
565 *SeqData = Pad_array[PadRow-1][Pad-1].seq;
569 int TPCV2P0_ZS_SR::getFeeSequences(
int Fee,
int Pin,
int *nSeq,
572 unsigned short PadRow = row_vs_fee[Fee][Pin];
573 unsigned short Pad = pad_vs_fee[Fee][Pin];
574 *nSeq = Pad_array[PadRow-1][Pad-1].nseq;
575 *SeqData = Pad_array[PadRow-1][Pad-1].seq;
581 int TPCV2P0_ZS_SR::getSpacePts(
int PadRow,
int *nSpacePts,
SpacePt **SpacePts)
587 for (row=0; row<TPC_PADROWS; nsptrow[row++]=0);
589 for (rb=0; rb<6; rb++) {
590 for (
int mz=0; mz<3; mz++) {
592 cld_p[rb][mz] = detector->getBankTPCMZCLD(sector,rb,mz) ;
595 int *ptr = (
int *)&cld->stuff;
596 for (
int ir=0; ir<cld->NumRows; ir++){
599 nsptrow[row-1] += nsp;
604 if (detector->ercpy->verbose) cout <<
"sector "<<sector<<
": found "
605 <<numSpPts<<
" space pts" <<endl;
607 for (row=0; row<TPC_PADROWS; row++) {
608 RowSpacePts[row] = (
SpacePt *)malloc(nsptrow[row]*
sizeof(
struct SpacePt));
609 if (RowSpacePts[row]==NULL) {
610 cout <<
"failed to malloc() Space Point structures " << endl;
615 for (rb=0; rb<6; rb++) {
616 for (
int mz=0; mz<3; mz++) {
619 int *ptr = &cld->stuff[0];
620 for (
int ir=0; ir<cld->NumRows; ir++){
623 for (
int isp=0; isp<nsp; isp++, ptr+=2) {
624 RowSpacePts[row-1][isp] = *(
SpacePt *)ptr;
629 *nSpacePts = nsptrow[PadRow-1];
630 *SpacePts = RowSpacePts[PadRow-1];
635 int TPCV2P0_ZS_SR::MemUsed()