26 #include "FtfFinder.h"
29 #include <sys/types.h>
34 FtfFinder::FtfFinder ( )
48 FtfFinder::~FtfFinder ( )
51 if ( mcTrack )
delete[] mcTrack ;
52 if ( volumeC )
delete[] volumeC ;
53 if ( rowC )
delete[] rowC ;
54 if ( trackC )
delete[] trackC ;
59 double FtfFinder::process ( ) {
64 if ( para.infoLevel > 2 ) LOG(ERR,
"fft: Hit structure is empty \n " ) ;
68 initialCpuTime = CpuTime ( );
69 initialRealTime = RealTime ( );
73 if ( para.init == 0 ) {
74 if ( reset ( ) )
return 1 ;
79 if ( para.eventReset && setPointers ( ) )
return 1 ;
85 for ( i = 0 ; i < para.nPrimaryPasses ; i++ ) {
86 int ret = getTracks();
94 for ( i = 0 ; i < para.nSecondaryPasses ; i++ ) {
95 int ret = getTracks();
102 cpuTime = CpuTime ( ) - initialCpuTime ;
103 realTime = RealTime ( ) - initialRealTime ;
105 if ( para.infoLevel > 0 )
106 LOG(ERR,
"FtfFinder::process: cpu %7.3f real %f7.2 \n", cpuTime, realTime ) ;
113 void FtfFinder::dEdx ( ) {
114 for (
int i = 0 ; i<nTracks ; i++ ){
122 int FtfFinder::getTracks ( ) {
126 int nHitsSegment = (short)para.nHitsForSegment;
127 if ( para.primaries ) {
128 setConformalCoordinates ( ) ;
129 para.minHitsForFit = 1 ;
130 para.nHitsForSegment = max(2,nHitsSegment);
133 para.minHitsForFit = 2 ;
134 para.nHitsForSegment = max(3,nHitsSegment);
139 for (
int ir = para.nRowsPlusOne - 1 ; ir>=para.minHitsPerTrack ; ir--) {
143 if ( rowC[ir].first && (((
FtfHit *)rowC[ir].first)->row) < para.rowEnd )
148 firstHit = (
FtfHit *)(firstHit->nextRowHit) ) {
152 if ( firstHit->track != 0 ) continue ;
159 if ( nTracks > maxTracks ){
160 LOG(OPER,
"Event too larget to track! Ignoring event!") ;
161 nTracks = maxTracks ;
168 thisTrack->para = ¶ ;
169 thisTrack->id = nTracks ;
170 thisTrack->firstHit = thisTrack->lastHit = firstHit ;
171 thisTrack->innerMostRow = thisTrack->outerMostRow = firstHit->row ;
172 thisTrack->xRefHit = firstHit->x ;
173 thisTrack->yRefHit = firstHit->y ;
174 thisTrack->xLastHit = firstHit->x ;
175 thisTrack->yLastHit = firstHit->y ;
177 thisTrack->debugNew ( ) ;
182 thisTrack->reset ( ) ;
186 if ( thisTrack->buildTrack ( firstHit, volumeC ) ) {
190 if ( para.primaries &&
191 para.mergePrimaries == 1 &&
193 thisTrack->mergePrimary( trackC ) ) {
195 thisTrack->deleteCandidate ( ) ;
202 thisTrack->deleteCandidate ( ) ;
213 if ( CpuTime() - initialCpuTime > para.maxTime ) {
214 LOG(ERR,
"FtfFinder::getTracks: tracker time out after %f\n",
215 CpuTime() - initialCpuTime ) ;
220 para.nHitsForSegment = nHitsSegment ;
226 void FtfFinder::mergePrimaryTracks ( ) {
230 memset ( trackC, 0, para.nPhiTrackPlusOne*para.nEtaTrackPlusOne*
sizeof(
FtfContainer) ) ;
235 for (
int i = 0 ; i < nTracks ; i++ ) {
236 currentTrack = &(
track[i]);
237 if ( currentTrack->flag < 0 ) continue ;
241 currentTrack->nxatrk = 0 ;
247 if ( currentTrack->mergePrimary ( trackC ) ) {
248 currentTrack->flag = -1 ;
255 int FtfFinder::reset (
void) {
264 para.nRowsPlusOne = ( para.rowOuterMost - para.rowInnerMost ) / para.modRow + 2 ;
265 if ( para.nRowsPlusOne < 1 ) {
266 LOG(ERR,
" Rows: Outer Most Inner Most %d % d \n",
267 para.rowOuterMost, para.rowInnerMost ) ;
270 para.nPhiPlusOne = para.nPhi + 1 ;
271 para.nEtaPlusOne = para.nEta + 1 ;
272 para.nPhiEtaPlusOne = para.nPhiPlusOne * para.nEtaPlusOne ;
273 if ( para.mergePrimaries ) {
274 para.nPhiTrackPlusOne = para.nPhiTrack + 1 ;
275 para.nEtaTrackPlusOne = para.nEtaTrack + 1 ;
280 if (volumeC != NULL)
delete []volumeC;
282 LOG(ERR,
"Allocating %d bytes of memory for volume\n",
285 para.nEtaPlusOne*
sizeof(VOLUME));
287 int nVolumes = para.nRowsPlusOne*para.nPhiPlusOne *
290 if(volumeC == NULL) {
291 LOG(ERR,
"Problem with memory allocation... exiting\n" ) ;
297 if ( rowC != NULL )
delete[] rowC ;
299 LOG(ERR,
"Allocating %d bytes of memory for rowC\n",
300 para.nRowsPlusOne*
sizeof(ROW));
304 LOG(ERR,
"Problem with memory allocation... exiting\n" ) ;
310 if ( para.mergePrimaries ) {
311 if (trackC != NULL)
delete []trackC ;
313 LOG(ERR,
"Allocating %d bytes of memory for track_area\n",
314 para.nPhiTrackPlusOne*
315 para.nEtaTrackPlusOne*
sizeof(AREA));
317 int nTrackVolumes = para.nPhiTrackPlusOne*
318 para.nEtaTrackPlusOne ;
321 LOG(ERR,
"Problem with memory allocation... exiting\n" ) ;
329 LOG(ERR,
"FtfFinder::reset: Merging option not available \n " ) ;
330 LOG(ERR,
" when option was not used the first time \n " ) ;
338 phiDiff = para.phiMax - para.phiMin ;
339 if ( phiDiff > 2. * pi + 0.1 ) {
340 LOG(ERR,
"FtfFinder::reset: Wrong phi range %f, %f ",
341 para.phiMin*toDeg, para.phiMax*toDeg ) ;
344 if ( fabs(phiDiff-twoPi ) < pi / 36. ) para.phiClosed = 1 ;
350 para.phiSlice = (para.phiMax - para.phiMin)/para.nPhi ;
351 para.etaSlice = (para.etaMax - para.etaMin)/para.nEta ;
355 para.phiSliceTrack = (para.phiMaxTrack - para.phiMinTrack)/para.nPhiTrack ;
356 para.etaSliceTrack = (para.etaMaxTrack - para.etaMinTrack)/para.nEtaTrack ;
360 if ( para.xVertex != 0 || para.yVertex != 0 ) {
361 para.rVertex = (double)sqrt (para.xVertex*para.xVertex +
362 para.yVertex*para.yVertex) ;
363 para.phiVertex = (double)atan2(para.yVertex,para.xVertex);
367 para.phiVertex = 0.F ;
370 if ( para.dxVertex != 0 || para.dyVertex != 0 )
371 para.xyWeightVertex = 1.F / ((double)sqrt(para.dxVertex*para.dxVertex+
372 para.dyVertex*para.dyVertex) ) ;
373 else para.xyWeightVertex = 1.0F ;
389 int FtfFinder::setConformalCoordinates ( )
395 double x, y, r2, invR2 ;
396 for (
int ihit = 0 ; ihit<nHits ; ihit++ )
401 thisHit = &(
hit[ihit]) ;
403 uint *v1 = (uint *)volumeC;
404 uint *v2 = (uint *)&volumeC[20746];
405 uint *h = (uint *)thisHit;
407 if((h>v1) && (h<v2)) {
408 printf(
"hit: 0x%p v1=0x%p v2=0x%p ihit=%d nHits=%d hit=0x%p\n",
409 h,v1,v2,ihit,nHits,
hit);
413 x = thisHit->x - para.xVertex ;
414 y = thisHit->y - para.yVertex ;
418 thisHit->xp = x * invR2 ;
419 thisHit->yp = - y * invR2 ;
420 thisHit->wxy = r2 * r2 / ( square(para.xyErrorScale)
421 * ( square(thisHit->dx) + square(thisHit->dy) ) ) ;
429 int FtfFinder::setPointers ( )
432 register int volumeIndex;
433 double r, r2, phi, eta ;
436 nHitsOutOfRange = 0 ;
440 memset ( rowC, 0, para.nRowsPlusOne*
sizeof(
FtfContainer) ) ;
441 int n = para.nRowsPlusOne*para.nEtaPlusOne*para.nPhiPlusOne ;
443 if ( para.mergePrimaries ){
444 memset ( trackC, 0, para.nPhiTrackPlusOne*para.nEtaTrackPlusOne*
sizeof(
FtfContainer) ) ;
449 for ( ihit = 0 ; ihit<nHits ; ihit++ )
454 thisHit = &(
hit[ihit]) ;
455 localRow = thisHit->row - para.rowInnerMost ;
456 if ( fmod((
double)localRow,(
double)para.modRow) != 0 )
continue ;
458 if ( thisHit->row < para.rowInnerMost ) continue ;
459 if ( thisHit->row > para.rowOuterMost ) continue ;
463 localRow = localRow / para.modRow + 1 ;
469 r2 = thisHit->x * thisHit->x + thisHit->y * thisHit->y ;
470 r = (double)sqrt ( r2 ) ;
471 phi = (double)atan2(thisHit->y,thisHit->x) + para.phiShift ;
472 if ( phi < 0 ) phi = phi + twoPi ;
474 eta = (double)seta(r,thisHit->z) ;
476 if ( para.szFitFlag ) {
478 thisHit->wz = (double)(1./ square ( para.szErrorScale * thisHit->dz ));
487 thisHit->nextVolumeHit =
488 thisHit->nextRowHit = 0 ;
493 thisHit->phiIndex = (int)( (thisHit->phi-para.phiMin)/para.phiSlice + 1.);
494 if ( thisHit->phiIndex < 1 || thisHit->phiIndex > para.nPhi ) {
495 if ( para.infoLevel > 2 ) {
496 LOG(ERR,
" === > Hit %d has Phi = %f \n", (
int)thisHit->id,
497 thisHit->phi*toDeg ) ;
498 LOG(ERR,
" Phi index %d out of range \n", thisHit->phiIndex ) ;
508 thisHit->etaIndex = (int)((thisHit->eta - para.etaMin)/para.etaSlice + 1.);
509 if ( thisHit->etaIndex < 1 || thisHit->etaIndex > para.nEta ) {
510 if ( para.infoLevel > 2 ) {
511 LOG(ERR,
" \n === > Hit %d has Eta = %f z %f ", (
int)thisHit->id,
512 thisHit->eta, thisHit->z ) ;
513 LOG(ERR,
" \n Eta min/max %f %f ", para.etaMin, para.etaMax ) ;
514 LOG(ERR,
" \n Eta slice %f ", para.etaSlice ) ;
515 LOG(ERR,
" \n Eta index %d out of range ", thisHit->etaIndex ) ;
523 thisHit->nextTrackHit = 0 ;
530 volumeIndex = localRow * para.nPhiEtaPlusOne +
531 thisHit->phiIndex * para.nEtaPlusOne + thisHit->etaIndex ;
534 if (volumeC[volumeIndex].first == 0 )
535 volumeC[volumeIndex].first = (
void *)thisHit ;
537 ((
FtfHit *)(volumeC[volumeIndex].last))->nextVolumeHit = thisHit ;
538 volumeC[volumeIndex].last = (
void *)thisHit ;
544 if ( rowC[localRow].first == NULL ){
545 rowC [localRow].first = (
void *)thisHit ;
548 ((
FtfHit *)(rowC[localRow].last))->nextRowHit = thisHit ;
549 rowC[localRow].last = (
void *)thisHit ;
560 double FtfFinder::CpuTime(
void ) {
561 return (
double)(clock()) / CLOCKS_PER_SEC;
566 double FtfFinder::RealTime (
void) {
567 const long nClicks = 400000000 ;
568 unsigned long eax, edx;
569 asm volatile(
"rdtsc":
"=a" (eax),
"=d" (edx));
570 double realTime = (double)(eax)/ nClicks;
574 double FtfFinder::RealTime (
void) {