303 {
304
305
306
307
308
309
310
311
312
315 {
316 cout << "Before pickHits";
317 if ( is2d ) cout << " 2d:";
318 else cout << " 3d:";
319 cout << endl;
320 }
321
322 int nFound = 0;
323 int nCand = 0;
324 double rMin, rMax;
325 double rEnter, rExit;
326 BesAngle phiEnter, phiExit;
327 int wireLow, wireHigh;
328 double phiLow, phiHigh;
329 int nCell;
330 MdcHit* thisHit;
331 HepAList<MdcHitOnTrack> localHistory;
332
333 double cellWidth;
334 double goodDriftCut;
335
336 double aresid = 0.;
337 int firstInputHit = -1;
338 int lCurl = 0;
339
340
341 const MdcLayer* firstInputLayer = trk->
firstLayer();
342 const MdcLayer* lastInputLayer = trk->
lastLayer();
343
346 assert( tkFit != 0 );
347 const TrkFitStatus* tkStatus = trk->
track().
status();
348 assert( tkStatus != 0 );
349 TrkHitList* hitList = trk->
track().
hits();
350 assert( hitList != 0 );
351 TrkExchangePar par = tkFit->
helix( 0.0 );
352 double d0 = par.
d0();
353 double curv = 0.5 * par.
omega();
354 double sinPhi0 =
sin( par.
phi0() );
355 double cosPhi0 =
cos( par.
phi0() );
356
357
359 double absd0 = fabs( d0 );
361
362 bool willCurl = false;
363 double rCurl = fabs( d0 + 1. / curv );
365
366 if ( rCurl < rMax )
367 {
368 willCurl = true;
370 }
371
372 bool reachedLastInput = false;
373 bool hasCurled = false;
374
375
376 bool isHotOnLayer[43];
377 if (
tkParam.pickSkipExistLayer )
378 {
379 for ( int ii = 0; ii < 43; ii++ ) isHotOnLayer[ii] = false;
381 { isHotOnLayer[ihit->layerNumber()] = true; }
382 }
383
384
385
386 for (
const MdcLayer* layer = gm->
firstLayer(); layer != 0;
387 layer =
389 : layer ) )
390 {
391
392 if ( lCurl )
393 {
394 lCurl = 0;
395 hasCurled = true;
396 }
397 if ( tkStatus->
is2d() && layer->view() != 0 )
continue;
398
399 if (
tkParam.pickSkipExistLayer && isHotOnLayer[layer->layNum()] )
400 continue;
401
402
403
404 bool lContinue = true;
405 if (
tkParam.pickUitlLastLayer )
406 {
407 if ( layer == lastInputLayer ) reachedLastInput = true;
408 }
409
410
411 if ( hasCurled )
412 {
413 rEnter = layer->rOut();
414 if ( rEnter < rMin )
415 {
416
417
418 break;
419 }
420 rExit = layer->rIn();
421 if ( rExit < rMin ) rExit = rMin;
422 if ( rEnter > rMax ) rEnter = rMax;
423
424
425
426 }
427 else
428 {
429 rEnter = layer->rIn();
430 rExit = layer->rOut();
431
432
433 if ( rExit < rMin ) continue;
434
435 if ( willCurl )
436 {
437 if ( rEnter > rMax )
438 {
439
440
441 hasCurled = 1;
442 continue;
443 }
444 if ( rExit > rMax )
445 {
446 lCurl = 1;
447 rExit = rMax;
448 }
449 }
450 else
451 {
452
453 if ( rEnter > rMax )
454 {
455
456 break;
457 }
458 if ( rExit > rMax ) rExit = rMax;
459 }
460 }
461
462 nCell = layer->nWires();
464
465 goodDriftCut = 0.5 * cellWidth * M_SQRT2 +
tkParam.pickHitMargin;
466
467 double deltaPhiCellWidth = 0.5 * ( cellWidth * M_SQRT2 ) / layer->rMid();
468
469
470 BesAngle phiTemp( 0.0 );
471 int ierror = trk->
projectToR( rEnter, phiTemp, hasCurled );
472 phiEnter = phiTemp.
posRad();
473 if ( ierror != 0 )
474 {
476 std::cout << " ErrMsg(warning) "
477 << "Error in MdcTrackList::pickHits projection, ierror " << ierror
478 << std::endl;
479 continue;
480 }
481 ierror = trk->
projectToR( rExit, phiTemp, hasCurled );
482 phiExit = phiTemp.
posRad();
483 if ( ierror != 0 )
484 {
486 std::cout << " ErrMsg(warning) "
487 << "Error in MdcTrackList::pickHits projection, ierror " << ierror
488 << std::endl;
489 continue;
490 }
491
493 {
494 std::cout << endl << "--pickHit of layer " << layer->layNum() << "--" << std::endl;
495 std::cout <<
" track phiEnter " << phiEnter.
deg() <<
" phiExit " << phiExit.
deg()
496 << " degree" << std::endl;
497 std::cout << " cell width " << 360. / layer->nWires() << " dPhiz "
500 << std::endl;
501 std::cout <<
" maxInactiveResid " <<
tkParam.maxInactiveResid <<
" pickHitPhiFactor "
502 <<
tkParam.pickHitPhiFactor << std::endl;
503 std::cout << " goodDriftCut " << goodDriftCut << "=sqrt(2)*0.5*" << cellWidth << "+"
504 <<
tkParam.pickHitMargin << std::endl;
505 }
506
508
509
510 double t_phiHit = -999.;
511 if ( curv * Bz <= 0.0 )
512 {
513
514 phiLow = phiEnter;
515 phiHigh = phiExit;
516
517 phiLow -=
tkParam.maxInactiveResid / rEnter;
518 phiHigh +=
tkParam.maxInactiveResid / rExit;
519 }
520 else
521 {
522 phiLow = phiExit;
523 phiHigh = phiEnter;
524 phiLow -=
tkParam.maxInactiveResid / rExit;
525 phiHigh +=
tkParam.maxInactiveResid / rEnter;
526 }
527
530
531 double lowFloat = nCell /
Constants::twoPi * ( phiLow - layer->phiOffset() ) + 0.5;
532 double highFloat = nCell /
Constants::twoPi * ( phiHigh - layer->phiOffset() ) + 0.5;
533 wireLow = ( lowFloat >= 0.0 ) ? int( lowFloat ) : int( floor( lowFloat ) );
534 wireHigh = ( highFloat >= 0.0 ) ? int( highFloat ) : int( floor( highFloat ) );
535
537 {
538 std::cout << " wireLow " << wireLow << " wireHigh " << wireHigh << " phiLow "
541 }
542
543 int tempDiff = 0;
545 {
547 if ( wireLow > tempN / 2. && wireHigh < tempN / 2. )
548 {
550 tempDiff = wireHigh + tempN - wireLow + 1;
551 }
552 else
553 {
555 tempDiff = wireHigh - wireLow + 1;
556 }
557 }
558
559 if ( wireLow > wireHigh ) wireLow -= nCell;
560 long t_iHit = 0;
561 for ( int thisWire = wireLow; thisWire <= wireHigh; thisWire++ )
562 {
564 thisHit = map->
hitWire( layer->layNum(), corrWire );
565
567 {
568 if ( thisHit != 0 )
569 {
570 cout << endl << "test hit ";
571 thisHit->
print( std::cout );
572 }
573 else
574 std::cout << endl
575 << "test hit (" << layer->layNum() << "," << corrWire << ")" << std::endl;
576 }
577
578 double tof = 0.;
579 double z = 0.;
580 double driftDist = 0.;
581
582 double delx, dely;
583 double resid = 0., predDrift = 0.;
584 int ambig = 0;
585 const MdcHitOnTrack* alink = 0;
586 double mcTkId = -9999;
587 if ( thisHit == 0 )
588 {
589 delx = -d0 * sinPhi0 - layer->xWire( corrWire );
590 dely = d0 * cosPhi0 - layer->yWire( corrWire );
591 predDrift = cosPhi0 * dely - sinPhi0 * delx + curv * ( delx * delx + dely * dely );
592 if ( 6 ==
tkParam.lPrint ) cout <<
"No hit. predDrift=" << predDrift << endl;
593 continue;
594 }
595 else
596 {
598
600 {
601 alink = 0;
602 }
603 else
604 {
606 if ( 6 ==
tkParam.lPrint ) { cout <<
" existing hot; "; }
607 }
608
609 if ( alink == 0 || pickAm )
610 {
611 if ( ( !tkStatus->
is2d() ) && layer->view() != 0 )
612 {
613 double rc = 9999.;
615 double rw = layer->rMid();
616 double alpha = acos( 1 - rw * rw / ( 2 * rc * rc ) );
617 double fltLen = rw;
618 if ( fabs( 1 - rw * rw / ( 2 * rc * rc ) ) < 1 ) fltLen = rc *
alpha;
619 z = par.
z0() + fltLen * par.
tanDip();
621 double x = layer->getWire( corrWire )->xWireDC( z );
622 double y = layer->getWire( corrWire )->yWireDC( z );
623 delx = -d0 * sinPhi0 -
x;
624 dely = d0 * cosPhi0 - y;
626 }
627 else
628 {
629 delx = -d0 * sinPhi0 - thisHit->
x();
630 dely = d0 * cosPhi0 - thisHit->
y();
632 }
633 predDrift = cosPhi0 * dely - sinPhi0 * delx + curv * ( delx * delx + dely * dely );
634
635
636 ambig = ( predDrift >= 0 ) ? 1 : -1;
637 if ( hasCurled ) ambig = -ambig;
638 double entranceAngle = 0.;
639 driftDist = thisHit->
driftDist( tof + bunchTime, ambig, entranceAngle, 0., z );
640 if ( alink == 0 )
641 {
642
643 resid = ambig * fabs( driftDist ) - predDrift;
644 aresid = fabs( resid );
645
646 }
647 }
648 else
649 {
650 ambig = alink->
ambig();
651 if ( 6 ==
tkParam.lPrint ) cout <<
" pick ambig for hot" << endl;
653 }
654 }
655
656
657 double zGuess = par.
z0() + layer->rMid() * par.
tanDip();
658 double phiDCz = layer->getWire( corrWire )->phiDC( zGuess );
659 BesAngle phiDCzMax( phiDCz + deltaPhiCellWidth );
660 BesAngle phiDCzMin( phiDCz - deltaPhiCellWidth );
661
663 {
664 double sigma = 999.;
665 if ( thisHit != 0 && alink == 0 )
666 {
667 double entranceAngle = 0.;
668 sigma = thisHit->
sigma( driftDist, ambig, entranceAngle, atan( par.
tanDip() ), z );
669 }
674 if ( curv * Bz <= 0. )
675 {
676
677 if ( ( phiDCzMin - phiExit > 0 ) || ( phiDCzMax - phiEnter < 0 ) )
680 }
681 else
682 {
683 if ( ( phiDCzMin - phiEnter > 0 ) || ( phiDCzMax - phiExit < 0 ) )
686 }
692 double t_phiLowCut = -999.;
693 double t_phiHighCut = -999.;
694 if ( t_phiHit > -998. )
695 {
696 t_phiLowCut = ( phiEnter - t_phiHit ) * rEnter;
697 t_phiHighCut = ( phiExit - t_phiHit ) * rExit;
698 }
705 t_iHit++;
706 }
707
708 if ( curv * Bz <= 0. )
709 {
710
711 if ( ( phiDCzMin - phiExit > 0 ) || ( phiDCzMax - phiEnter < 0 ) )
712 {
714 { std::cout << " CUT by phiDCz not in phi En Ex range, curv>=0" << std::endl; }
715 continue;
716 }
717 }
718 else
719 {
720
721 if ( ( phiDCzMin - phiEnter > 0 ) || ( phiDCzMax - phiExit < 0 ) )
722 {
724 { std::cout << " CUT by phiDCz not in phi En Ex range, curv<0" << std::endl; }
725 continue;
726 }
727 }
728
729
730
731 if ( ambig != 0 && fabs( predDrift ) > goodDriftCut )
732 {
734 { cout << " predDrift " << predDrift << ">goodDriftCut " << goodDriftCut << endl; }
735 continue;
736 }
737
738
739 if ( ambig == 0 && fabs( predDrift ) > goodDriftCut )
740 {
742 {
743 cout << " ambig==0 && |predDirft| " << fabs( predDrift ) << "> goodDriftCut "
744 << goodDriftCut << endl;
745 cout << " No good hit, but track near cell-edge " << endl;
746 }
747 continue;
748 }
749
750
751 if ( ambig != 0 )
752 {
753
754
755 double entranceAngle = 0.;
756 double sigma =
757 thisHit->
sigma( driftDist, ambig, entranceAngle, atan( par.
tanDip() ), z );
758 double factor =
tkParam.pickHitFactor;
759
760
761 double residCut =
tkParam.maxActiveSigma * factor * sigma;
762
763
764
765
766
767
768 if ( alink == 0 && ( aresid <= residCut ) )
769 {
771 {
772 cout << " (2) New hit found " << endl;
773 }
774
775 int isActive = 1;
776
777
778
779
780
781 MdcRecoHitOnTrack tempHot( *thisHit, ambig, bunchTime );
782 tempHot.setActivity( isActive );
783
785 {
786 tempHot.setUsability( false );
788 std::cout << " thisHit used, setUsability false " << std::endl;
789 }
790
791 double flt = layer->rMid();
793 flt += 0.000001 * ( thisHit->
x() + thisHit->
y() );
794 tempHot.setFltLen( flt );
796 {
797 std::cout << " aresid " << aresid << "<=residCut " << residCut << " nSig "
798 << aresid / sigma <<
" nSigCut " << (
tkParam.maxActiveSigma * factor )
799 <<
" factor " << factor <<
" maxActiveSigma " <<
tkParam.maxActiveSigma
800 <<
" tanDip " << par.
tanDip() << std::endl;
801 std::cout << " Append Hot " << std::endl;
802 }
803 alink = (MdcHitOnTrack*)hitList->
appendHot( &tempHot );
804 }
805 else
806 {
808 {
809 if ( alink != 0 ) { std::cout << "Exist hot found" << std::endl; }
810 else if ( aresid > residCut )
811 {
812 thisHit->
print( std::cout );
813 std::cout << " Drop hit. aresid " << aresid << ">residCut " << residCut
814 << " nSig " << aresid / sigma << " nSigCut "
815 << (
tkParam.maxActiveSigma * factor ) <<
" factor " << factor
816 <<
" maxActiveSigma " <<
tkParam.maxActiveSigma <<
" tanDip "
817 << par.
tanDip() << std::endl;
818 }
819 }
820 }
821 if ( !localHistory.member( const_cast<MdcHitOnTrack*>( alink ) ) )
822 {
823 localHistory.append( const_cast<MdcHitOnTrack*>( alink ) );
825 nFound++;
827 std::cout << " nFound=" << nFound << " nCand " << nCand << std::endl;
828 if ( layer == firstInputLayer && firstInputHit < 0 ) { firstInputHit = nCand; }
829 }
830 else
831 {
833 std::cout << "ErrMsg(warning) "
834 << "would have inserted identical HOT "
835 "twice in history buffer"
836 << std::endl;
837 }
838 }
839
840
841 else if ( ambig == 0 && reachedLastInput )
842 {
843 if ( 6 ==
tkParam.lPrint ) std::cout <<
"ambig==0 " << std::endl;
844 lContinue = false;
845 int nSuccess = 0;
846 int last = 8;
847 if ( nCand < 8 ) last = nCand;
848 for ( int i = 0; i < last; i++ )
849 {
850 int j = nCand - 1 - i;
851 if ( localHistory[j] != 0 )
852 {
853
854 nSuccess++;
855 }
856 if ( i == 2 && nSuccess >= 2 ) lContinue = true;
857 if ( i == 4 && nSuccess >= 3 ) lContinue = true;
858 if ( i == 7 && nSuccess >= 5 ) lContinue = true;
860 cout << i << " (3) No hit found; if beyond known good region "
861 << endl;
862 if ( lContinue )
863 {
865 std::cout << " pickHits break by lContinue. i=" << i << " nSuccess=" << nSuccess
866 << std::endl;
867 break;
868 }
869 }
870
872 cout << " (3) No hit found; if beyond known good region " << endl;
873
874 if ( !lContinue )
875 {
876
877 break;
878 }
879 localHistory.append( 0 );
880 }
881
882 nCand++;
883
884 if ( ambig != 0 && reachedLastInput )
885 {
887 {
890 }
891 else
892 {
895 }
896 }
897
898 }
900 {
906 }
907 if ( !lContinue && reachedLastInput )
908 {
909
910 break;
911 }
912 }
913
914
915 bool lContinue = true;
916 for ( int ihit = firstInputHit; ihit >= 0; ihit-- )
917 {
918 if ( localHistory[ihit] != 0 )
919 {
920 if ( lContinue )
921 {
922
923 const MdcHitOnTrack* mdcHit = localHistory[ihit]->mdcHitOnTrack();
924 if ( mdcHit != 0 )
925 {
928 }
929 continue;
930 }
931 else
932 {
933
934 if ( 6 ==
tkParam.lPrint ) { std::cout <<
" gap found; delete hits. "; }
935 if ( !localHistory[ihit]->isUsable() )
936 {
938 {
939 cout << "about to delete hit for unusable HOT:" << endl;
940 localHistory[ihit]->print( std::cout );
941 }
942 hitList->
removeHit( localHistory[ihit]->hit() );
943 }
945 {
946 cout << " current contents of localHistory: " << localHistory.length() << "hot"
947 << endl;
948
949
950
951
952 }
953 nFound--;
954 }
955 }
956 else if ( localHistory[ihit] == 0 )
957 {
958 if ( 6 ==
tkParam.lPrint ) { cout <<
" localHistory= 0 " << endl; }
959 int nSuccess = 0;
960 lContinue = false;
961 int last = 8;
962 if ( nCand < 8 ) last = nCand;
963 for ( int i = 0; i < last; i++ )
964 {
965 int j = ihit + 1 + i;
966 if ( localHistory[j] != 0 ) nSuccess++;
967 if ( i == 2 && nSuccess >= 2 ) lContinue = true;
968 if ( i == 4 && nSuccess >= 3 ) lContinue = true;
969
970 if ( lContinue ) break;
971 }
972 }
973 }
975 {
976 std::cout <<
"nFound=" << nFound <<
" < " <<
tkParam.pickHitFract * nCand
977 <<
" pickHitFract? " <<
tkParam.pickHitFract <<
"*" << nCand << std::endl;
978 }
979 if ( nFound <
tkParam.pickHitFract * nCand )
980 nFound = 0;
981 if ( 6 ==
tkParam.lPrint ) { cout <<
" localHistory= 0 " << endl; }
982
984 {
985 cout << "After pickHits found " << nFound << " hit(s)" << endl;
987 std::cout << std::endl;
988 }
989
990 return nFound;
991}
double sin(const BesAngle a)
double cos(const BesAngle a)
NTuple::Array< long > m_pickIs2D
NTuple::Array< double > m_pickPt
NTuple::Item< long > m_pickLayer
NTuple::Array< long > m_pickMcTk
AIDA::IHistogram1D * g_pickHitWire
NTuple::Item< long > m_pickNCellWithDigi
NTuple::Array< double > m_pickPhiLowCut
double haveDigiDrift[43][288]
NTuple::Array< double > m_pickDriftCut
NTuple::Array< double > m_pickCurv
NTuple::Array< double > m_pickDrift
NTuple::Array< double > m_pickResid
NTuple::Array< int > m_pickPhizOk
NTuple::Array< double > m_pickSigma
NTuple::Array< long > m_pickWire
NTuple::Tuple * m_tuplePick
NTuple::Array< double > m_pickDriftTruth
NTuple::Array< double > m_pickZ
NTuple::Array< double > m_pickPredDrift
NTuple::Item< long > m_pickNCell
NTuple::Array< double > m_pickPhiHighCut
int mdcWrapWire(int wireIn, int nCell)
static const double twoPi
static const int maxCell[43]
static const double radToDegrees
const MdcLayer * prevLayer(int lay) const
const MdcLayer * lastLayer() const
const MdcLayer * firstLayer() const
const MdcLayer * nextLayer(int lay) const
MdcHit * hitWire(int lay, int wire) const
const MdcLayer * layer() const
const MdcDigi * digi() const
void print(std::ostream &o) const
double sigma(double, int, double, double, double) const
double driftDist(double, int, double, double, double) const
const MdcLayer * layer() const
void setFirstLayer(const MdcLayer *l)
int projectToR(double radius, BesAngle &phiIntersect, int lCurl=0) const
void setHasCurled(bool c=true)
const MdcLayer * firstLayer() const
const MdcLayer * lastLayer() const
void setLastLayer(const MdcLayer *l)
int getTrackIndex() const
const TrkHitOnTrk * getHitOnTrack(const TrkRecoTrk *trk) const
hot_iterator begin() const
TrkHitOnTrk * appendHot(const TrkHitOnTrk *theHot)
bool removeHit(const TrkFundHit *theHit)
TrkHotList::hot_iterator hot_iterator
void print(std::ostream &o) const
const MdcPatRec::BField & bField() const