50 #include "Stl3Util/ftf/FtfSl3.h"
52 #include "Stl3Util/base/L3swap.h"
57 int FtfSl3::canItBeMerged (
FtfTrack* tTrack ) {
58 double rTpcMin = 50. ;
59 double rTpcMax = 210. ;
61 if ( nHits < 15 )
return 0 ;
68 double x0 = tTrack->r0 * cos (tTrack->phi0);
69 double y0 = tTrack->r0 * sin (tTrack->phi0);
70 double rc = tTrack->pt / (bFactor * para.bField);
71 double trackPhi0 = tTrack->psi + tTrack->q * 0.5 * M_PI / fabs ((
double) tTrack->q);
72 double xc = x0 - rc * cos (trackPhi0);
73 double yc = y0 - rc * sin (trackPhi0);
78 localPhi[0] = para.phiMin ;
79 localPhi[1] = para.phiMax ;
82 double sinPhi, cosPhi, tanPhi ;
83 double a, b, c, b2minus4ac ;
84 double x1, y1, x2, y2, r1, r2 ;
85 c = xc * xc + yc * yc - rc * rc ;
86 for (
int i = 0 ; i < 2 ; i++ ) {
87 sinPhi = sin(localPhi[i]);
88 cosPhi = cos(localPhi[i]);
89 tanPhi = tan(localPhi[i]);
90 a = 1. + tanPhi * tanPhi ;
91 b = -2. * xc - 2. * yc * tanPhi ;
93 b2minus4ac = b * b - 4. * a * c ;
94 if ( b2minus4ac > 0 ) {
95 double rootB2Minus4ac = ::sqrt(b2minus4ac);
97 x1 = 0.5 * (-b + rootB2Minus4ac) / a ;
99 r1 = ::sqrt(x1*x1+y1*y1);
103 if ( cosPhi != 0 ) ratiox = x1/cosPhi ;
105 if ( sinPhi != 0 ) ratiox = y1/sinPhi ;
107 if ( ratiox >= 0 && ratioy >= 0 &&
108 r1 > rTpcMin && r1 < rTpcMax ) {
113 x2 = 0.5 * (-b - rootB2Minus4ac) / a ;
115 r2 = ::sqrt(x2*x2+y2*y2);
117 if ( cosPhi != 0 ) ratiox = x2/cosPhi ;
119 if ( sinPhi != 0 ) ratioy = y2/sinPhi ;
122 if ( ratiox >= 0 && ratioy >= 0 &&
123 r2 > rTpcMin && r2 < rTpcMax ) {
133 double zMembrane = 0 ;
134 double sToMembrane = (zMembrane - tTrack->z0 ) / tTrack->tanl ;
135 double dangle = sToMembrane / rc ;
136 double angle = dangle + trackPhi0 ;
137 double xMembrane = xc + rc * cos(angle) ;
138 double yMembrane = yc + rc * sin(angle) ;
139 double rMembrane = ::sqrt(xMembrane*xMembrane+yMembrane*yMembrane);
140 if ( rMembrane > rTpcMin && rMembrane < rTpcMax ) {
150 int FtfSl3::fillHits (
unsigned int maxBytes,
char* buff,
unsigned int token ) {
155 ftfLog (
"FtfSl3::fillHits: buffer with %d bytes too small \n", maxBytes ) ;
163 head->nrClusters_in_sector = nHits ;
165 memcpy(head->bh.bank_type,CHAR_L3_SECCD,8);
166 head->bh.bank_id = sectorNr;
167 head->bh.format_ver = DAQ_RAW_FORMAT_VERSION ;
168 head->bh.byte_order = DAQ_RAW_FORMAT_ORDER ;
169 head->bh.format_number = 0;
170 head->bh.token = token;
171 head->bh.w9 = DAQ_RAW_FORMAT_WORD9;
181 for ( i = 0 ; i < nTracks ; i++ ) {
184 nextHit = (
FtfHit *)nextHit->nextTrackHit) {
185 hitP->padrow = nextHit->row ;
186 hitP->pad = nextHit->buffer1 ;
187 hitP->time = nextHit->buffer2 ;
188 hitP->flags = nextHit->flags ;
189 hitP->charge = (short)nextHit->q ;
190 hitP->RB_MZ = (
short)nextHit->hardwareId ;
191 hitP->trackId =
track[i].id ;
192 if ( !(nextHit->track) ) {
193 ftfLog (
"FtfSl3:fillHits: we got a problem track %d row hit %d \n",
194 track[i].
id, hitP->padrow ) ;
203 for ( i = 0 ; i < nHits ; i++ ) {
204 nextHit = &(
hit[i]) ;
205 if ( nextHit->track ) continue ;
206 hitP->padrow = nextHit->row ;
207 hitP->pad = nextHit->buffer1 ;
208 hitP->time = nextHit->buffer2 ;
209 hitP->flags = nextHit->flags ;
210 hitP->charge = (short)nextHit->q ;
211 hitP->RB_MZ = (
short)nextHit->hardwareId ;
217 if ( counter != nHits ) {
218 ftfLog (
"FtfSl3:fillHits: Warning! Actual number of hits written %d total nHits %d \n",
222 head->bh.length = ((
char *)hitP-buff) / 4 ;
224 return ((
char *)hitP-buff) ;
229 int FtfSl3::fillTracks (
int maxBytes,
char* buff,
unsigned int token ) {
238 unsigned int headSize;
246 char *pointer = buff +
sizeof(
struct L3_SECTP) ;
248 char *endOfData = pointer + headSize
251 int nBytes = (endOfData-buff) ;
252 if ( nBytes > maxBytes ) {
253 ftfLog (
"FtfSl3::fillTracks: %d bytes needed, %d max, too short a buffer \n ", nBytes, maxBytes ) ;
264 memcpy(head->bh.bank_type,CHAR_L3_SECTP,8);
265 head->bh.length =
sizeof(
struct L3_SECTP) / 4 ;
266 head->bh.bank_id = sectorNr;
267 head->bh.format_ver = DAQ_RAW_FORMAT_VERSION ;
268 head->bh.byte_order = DAQ_RAW_FORMAT_ORDER ;
269 head->bh.format_number = 1;
270 head->bh.token = token;
271 head->bh.w9 = DAQ_RAW_FORMAT_WORD9;
274 head->nHits = (
unsigned int)nHits ;
275 head->nTracks = (
unsigned int)nTracks ;
276 head->cpuTime = (
unsigned int)rint(cpuTime * 1000000);
277 head->realTime = (
unsigned int)rint(realTime * 1000000) ;
278 head->xVert = (int)rint(para.xVertex * 1000000);
279 head->yVert = (int)rint(para.yVertex * 1000000);
280 head->zVert = (int)rint(para.zVertex * 1000000);
282 head->banks[0].off = (pointer-buff) / 4;
283 head->banks[0].len = (endOfData-pointer)/ 4 ;
284 head->banks[1].off = 0 ;
285 head->banks[1].len = 0 ;
286 head->banks[2].off = 0 ;
287 head->banks[2].len = 0 ;
301 memcpy(LTD->bh.bank_type, CHAR_L3_LTD, 8);
304 LTD->bh.bank_id = sectorNr ;
305 LTD->bh.format_ver = DAQ_RAW_FORMAT_VERSION ;
306 LTD->bh.byte_order = DAQ_RAW_FORMAT_ORDER ;
307 LTD->bh.format_number = 0 ;
308 LTD->bh.token = token ;
309 LTD->bh.w9 = DAQ_RAW_FORMAT_WORD9 ;
313 for (
int i = 0 ; i < nTracks ; i++ ) {
315 uSTrack->id =
track[i].id ;
316 if ( canItBeMerged ( &(
track[i]) ) ) {
319 uSTrack->nHits =
track[i].nHits ;
320 uSTrack->ndedx =
track[i].nDedx;
321 uSTrack->innerMostRow =
track[i].innerMostRow ;
322 uSTrack->outerMostRow =
track[i].outerMostRow ;
323 uSTrack->xy_chisq = (
unsigned short)rint(10 *
track[i].chi2[0]
324 /
float(
track[i].nHits));
325 uSTrack->sz_chisq = (
unsigned short)rint(10 *
track[i].chi2[1]
326 /
float(
track[i].nHits));
327 uSTrack->dedx =
track[i].dedx ;
328 uSTrack->pt =
track[i].pt * float(
track[i].q * para.bFieldPolarity) ;
329 uSTrack->psi =
track[i].psi ;
330 uSTrack->tanl =
track[i].tanl ;
331 uSTrack->r0 =
track[i].r0 ;
332 uSTrack->phi0 =
track[i].phi0 ;
333 uSTrack->z0 =
track[i].z0 ;
334 uSTrack->trackLength =
track[i].length ;
335 uSTrack->dpt = (short)(32768. *
track[i].dpt /
track[i].pt ) ;
337 uSTrack->dtanl =
track[i].CompressOver1(64.*
track[i].dtanl,
track[i].tanl);
338 uSTrack->dz0 = (short)(1024. *
track[i].dz0 ) ;
350 int FtfSl3::getNrTracks (
void) {
356 int FtfSl3::setup (
int maxHitsIn,
int maxTracksIn ) {
361 para.setDefaults ( ) ;
369 double toRad = acos(-1.)/180. ;
371 sectorG[ 0].phiMin = 45. * toRad ;
372 sectorG[ 1].phiMin = 15. * toRad ;
373 sectorG[ 2].phiMin = 345. * toRad ;
374 sectorG[ 3].phiMin = 315. * toRad ;
375 sectorG[ 4].phiMin = 285. * toRad ;
376 sectorG[ 5].phiMin = 255. * toRad ;
377 sectorG[ 6].phiMin = 225. * toRad ;
378 sectorG[ 7].phiMin = 195. * toRad ;
379 sectorG[ 8].phiMin = 165. * toRad ;
380 sectorG[ 9].phiMin = 135. * toRad ;
381 sectorG[10].phiMin = 105. * toRad ;
382 sectorG[11].phiMin = 75. * toRad ;
383 sectorG[12].phiMin = 105. * toRad ;
384 sectorG[13].phiMin = 135. * toRad ;
385 sectorG[14].phiMin = 165. * toRad ;
386 sectorG[15].phiMin = 195. * toRad ;
387 sectorG[16].phiMin = 225. * toRad ;
388 sectorG[17].phiMin = 255. * toRad ;
389 sectorG[18].phiMin = 285. * toRad ;
390 sectorG[19].phiMin = 315. * toRad ;
391 sectorG[20].phiMin = 345. * toRad ;
392 sectorG[21].phiMin = 15. * toRad ;
393 sectorG[22].phiMin = 45. * toRad ;
394 sectorG[23].phiMin = 75. * toRad ;
396 sectorG[ 0].phiMax = 75. * toRad ;
397 sectorG[ 1].phiMax = 45. * toRad ;
398 sectorG[ 2].phiMax = 15. * toRad ;
399 sectorG[ 3].phiMax = 345. * toRad ;
400 sectorG[ 4].phiMax = 315. * toRad ;
401 sectorG[ 5].phiMax = 285. * toRad ;
402 sectorG[ 6].phiMax = 255. * toRad ;
403 sectorG[ 7].phiMax = 225. * toRad ;
404 sectorG[ 8].phiMax = 195. * toRad ;
405 sectorG[ 9].phiMax = 165. * toRad ;
406 sectorG[10].phiMax = 135. * toRad ;
407 sectorG[11].phiMax = 105. * toRad ;
408 sectorG[12].phiMax = 135. * toRad ;
409 sectorG[13].phiMax = 165. * toRad ;
410 sectorG[14].phiMax = 195. * toRad ;
411 sectorG[15].phiMax = 225. * toRad ;
412 sectorG[16].phiMax = 255. * toRad ;
413 sectorG[17].phiMax = 285. * toRad ;
414 sectorG[18].phiMax = 315. * toRad ;
415 sectorG[19].phiMax = 345. * toRad ;
416 sectorG[20].phiMax = 15. * toRad ;
417 sectorG[21].phiMax = 45. * toRad ;
418 sectorG[22].phiMax = 75. * toRad ;
419 sectorG[23].phiMax = 105. * toRad ;
421 double etaMin = 0.4 ;
422 double etaMax = 2.3 ;
424 for (
int sector = 0 ; sector < NSECTORS ; sector++ ) {
425 sectorG[sector].phiShift = 0 ;
427 sectorG[sector].etaMin = -1. * etaMin ;
428 sectorG[sector].etaMax = etaMax ;
431 sectorG[sector].etaMin = -1. * etaMax ;
432 sectorG[sector].etaMax = etaMin ;
435 sectorG[ 2].phiShift = 16. * toRad ;
436 sectorG[20].phiShift = 16. * toRad ;
442 maxHits = maxHitsIn ;
443 maxTracks = maxTracksIn ;
447 para.phiMin = 0.F * pi / 180.F ;
448 para.phiMax =360.F * pi / 180.F ;
452 para.trackDebug = 24 ;
453 para.debugLevel = 1 ;
469 int FtfSl3::readMezzanine (
int sector,
int readOutBoard,
487 if ( !checkByteOrder(mzcld->bh.byte_order) ) swapByte = 1 ;
489 tmp32 = mzcld->padrowFiller ;
492 if ( swapByte ) rows = swap32(rows);
496 for (i=0; i<rows; i++) {
505 for ( j=0; j<len; j++) {
508 unsigned short time ;
515 unsigned short flags ;
516 unsigned short charge ;
519 xt = (
struct xt *) tmp32++ ;
520 c = (
struct c *) tmp32++ ;
525 if ( (c->flags & 1) == 1)
continue;
534 pad = swap16(xt->x) ;
535 time = swap16(xt->t) ;
537 fp = (double)pad / 64. ;
538 ft = (double)time/ 64. ;
541 if ( ft < minTimeBin ) continue ;
542 if ( ft > maxTimeBin ) continue ;
543 if ( c->charge < minClusterCharge ) continue ;
544 if ( c->charge > maxClusterCharge ) continue ;
546 ftfLog (
"FtfSl3:readMezzaninne row %d \n", row ) ;
547 ftfLog (
"ft %f min max %d %d \n", ft, minTimeBin, maxTimeBin ) ;
548 ftfLog (
"charge %d min max %d %d \n", c->charge, minClusterCharge, maxClusterCharge ) ;
552 PTRS.Setptrs((
double)fp, (
double)ft,(
double)row, (
double)sector) ;
559 hitP->id = nHits+counter ;
561 hitP->sector = sector ;
563 hitP->x = (float) XYZ.Getx();
564 hitP->y = (float) XYZ.Gety();
565 hitP->z = (float) XYZ.Getz();
571 hitP->buffer1 = pad ;
572 hitP->buffer2 = time ;
574 hitP->flags = (c->flags | (1<<7) );
576 hitP->flags = c->flags ;
577 hitP->q = c->charge ;
578 hitP->hardwareId = readOutBoard * 16 + mezzanninneNr ;
584 if ( (nHits+counter)>maxHits ) {
585 ftfLog (
"Error - FtfSl3:read: Hit array too small: counter %d maxHits %d \n",
601 int FtfSl3::readSector (
struct TPCSECLP *seclp ) {
616 if ( !checkByteOrder(seclp->bh.byte_order) ) swapByte = 1 ;
620 sector = (
unsigned int)seclp->bh.bank_id ;
621 if ( swapByte ) sector = swap32(sector) ;
625 para.phiMin = sectorG[sector-1].phiMin ;
626 para.phiMax = sectorG[sector-1].phiMax ;
627 para.etaMin = sectorG[sector-1].etaMin ;
628 para.etaMax = sectorG[sector-1].etaMax ;
631 if ( sectorNr < 1 && sectorNr > 24 ) {
632 ftfLog (
"Error - FtfSl3::readSector: Wrong sector %d!\n",sectorNr);
637 for (iRb=0; iRb<SB_RB_NUM; iRb++) {
644 para.phiShift = 0.8 ;
646 else if ( sector == 22 ) {
649 para.phiShift = 0.27 ;
652 if ( sectorG[sector-1].phiMin < para.phiMin )
653 para.phiMin = sectorG[sector-1].phiMin ;
654 if ( sectorG[sector-1].phiMax > para.phiMax )
655 para.phiMax = sectorG[sector-1].phiMax ;
658 if ( sectorG[sector-1].etaMin < para.etaMin )
659 para.etaMin = sectorG[sector-1].etaMin ;
660 if ( sectorG[sector-1].etaMax > para.etaMax )
661 para.etaMax = sectorG[sector-1].etaMax ;
664 if ( !(
unsigned int)seclp->rb[iRb].off) continue ;
666 int off = (
unsigned int)seclp->rb[iRb].off;
667 if ( swapByte ) off = swap32(off);
668 rbclp = (
struct TPCRBCLP *)((
char *)seclp + off * 4) ;
669 int swapByteMezzaninne = 0 ;
670 if ( !checkByteOrder(rbclp->bh.byte_order) ) swapByteMezzaninne = 1;
673 for (kMz=0; kMz<RB_MZ_NUM; kMz++) {
675 if(rbclp->mz[kMz].off) {
677 if ( debugLevel > 1 ) {
678 ftfLog (
"FtfSl3::readSector: MZCLD %d exists!\n",
685 off = rbclp->mz[kMz].off ;
686 if ( swapByteMezzaninne ) off = swap32(off);
692 nHitsOfMz = readMezzanine (sector, iRb, kMz, mzcld);
694 ftfLog (
"FtfSl3:readSector: wrong reading mezzanine \n" ) ;
727 int FtfSl3::readSector (
struct TPCSECLP *seclp1,
struct TPCSECLP *seclp2 ) {
732 int sector,tmpsector;
742 short swapByte1 = 0 ;
744 if ( !checkByteOrder(seclp1->bh.byte_order) ) swapByte1 = 1 ;
745 if ( !checkByteOrder(seclp2->bh.byte_order) ) swapByte2 = 1 ;
749 sector = (
unsigned int)seclp1->bh.bank_id ;
750 if ( swapByte1 ) sector = swap32(sector) ;
751 tmpsector = (
unsigned int)seclp2->bh.bank_id ;
752 if ( swapByte2 ) tmpsector = swap32(tmpsector) ;
754 if(tmpsector != sector){
755 ftfLog(
"Error in embedding got different sectors to read in\n");
762 para.phiMin = sectorG[sector-1].phiMin ;
763 para.phiMax = sectorG[sector-1].phiMax ;
764 para.etaMin = sectorG[sector-1].etaMin ;
765 para.etaMax = sectorG[sector-1].etaMax ;
769 if ( sectorNr < 0 && sectorNr > 24 ) {
770 ftfLog (
"Error - FtfSl3::readSector: Wrong sector %d!\n",sectorNr);
775 swapByte = swapByte1;
778 for(
int embed=0;embed<2;embed++){
784 for (iRb=0; iRb<SB_RB_NUM; iRb++) {
791 para.phiShift = 0.8 ;
793 else if ( sector == 22 ) {
796 para.phiShift = 0.27 ;
799 if ( sectorG[sector-1].phiMin < para.phiMin ) para.phiMin = sectorG[sector-1].phiMin ;
800 if ( sectorG[sector-1].phiMax > para.phiMax ) para.phiMax = sectorG[sector-1].phiMax ;
803 if ( sectorG[sector-1].etaMin < para.etaMin ) para.etaMin = sectorG[sector-1].etaMin ;
804 if ( sectorG[sector-1].etaMax > para.etaMax ) para.etaMax = sectorG[sector-1].etaMax ;
809 if ( !(
unsigned int)seclp->rb[iRb].off) continue ;
811 int off = (
unsigned int)seclp->rb[iRb].off;
812 if ( swapByte ) off = swap32(off);
813 rbclp = (
struct TPCRBCLP *)((
char *)seclp + off * 4) ;
814 int swapByteMezzaninne = 0 ;
815 if ( !checkByteOrder(rbclp->bh.byte_order) ) swapByteMezzaninne = 1 ;
818 for (kMz=0; kMz<RB_MZ_NUM; kMz++) {
820 if(rbclp->mz[kMz].off) {
822 if ( debugLevel > 1 ) {
823 ftfLog (
"FtfSl3::readSector: MZCLD %d exists!\n", kMz+1) ;
829 off = rbclp->mz[kMz].off ;
830 if ( swapByteMezzaninne ) off = swap32(off);
836 nHitsOfMz = readMezzanine (sector, iRb, kMz, mzcld);
838 ftfLog (
"FtfSl3:readSector: wrong reading mezzanine \n" ) ;
848 swapByte = swapByte2;
871 int FtfSl3::processSector ( ){
875 for (
int h = 0 ; h < nHits ; h++ ) {
878 para.eventReset = 1 ;
881 if (para.dEdx) dEdx();
883 if ( debugLevel > 0 )
884 ftfLog (
" FtfSl3::process: tracks %i Time: real %f cpu %f\n",
885 nTracks, realTime, cpuTime ) ;
894 int FtfSl3::dEdx ( ) {
896 for (
int i = 0 ; i<nTracks ; i++ ){
897 if (
track[i].nHits<para.minHitsForDedx) {
902 fDedx->TruncatedMean(&
track[i]);
909 int FtfSl3::setParameters ( ) {
915 para.hitChi2Cut = 50.F ;
916 para.goodHitChi2 = 10.F ;
917 para.trackChi2Cut = 30.F ;
918 para.maxChi2Primary = 0. ;
919 para.segmentRowSearchRange = 2 ;
920 para.trackRowSearchRange = 3 ;
923 para.dphiMerge = 0.01F ;
924 para.detaMerge = 0.02F ;
925 para.etaMinTrack = -2.2F ;
926 para.etaMaxTrack = 2.2F ;
931 para.goBackwards = 1 ;
932 para.goodDistance = 5.F ;
933 para.mergePrimaries = 0 ;
934 para.maxDistanceSegment = 50.F ;
935 para.minHitsPerTrack = 5 ;
936 para.nHitsForSegment = 2 ;
938 para.nEtaTrack = 40 ;
940 para.nPhiTrack = 40 ;
941 para.nPrimaryPasses = 1 ;
942 para.nSecondaryPasses = 0 ;
943 para.xyErrorScale = 1.0F ;
944 para.szErrorScale = 1.0F ;
948 para.ptMinHelixFit = 0. ;
953 para.dxVertex = 0.005F ;
954 para.dyVertex = 0.005F ;
955 para.phiVertex = 0.F ;