26 #include "PlanarMeasurement.h"
34 #include <TDatabasePDG.h>
46 cardinalRep_(0), fitStatuses_(), mcTrackId_(-1), timeSeed_(0), stateSeed_(6), covSeed_(6)
53 cardinalRep_(0), fitStatuses_(), stateSeed_(6), covSeed_(6)
78 std::vector <AbsMeasurement*> factoryHits = factory.
createMany(trackCand);
80 if (factoryHits.size() != trackCand.
getNHits()) {
81 Exception exc(
"Track::Track ==> factoryHits.size() != trackCand->getNHits()",__LINE__,__FILE__);
87 for (
unsigned int i=0;
i<factoryHits.size(); ++
i){
96 cardinalRep_(0), fitStatuses_(), mcTrackId_(-1), timeSeed_(0), stateSeed_(stateSeed),
97 covSeed_(TMatrixDSym::kUnit, TMatrixDSym(6))
104 cardinalRep_(0), fitStatuses_(), mcTrackId_(-1), timeSeed_(0), stateSeed_(6),
105 covSeed_(TMatrixDSym::kUnit, TMatrixDSym(6))
113 cardinalRep_(0), fitStatuses_(), mcTrackId_(-1), timeSeed_(0), stateSeed_(stateSeed),
122 cardinalRep_(rhs.cardinalRep_), mcTrackId_(rhs.mcTrackId_), timeSeed_(rhs.timeSeed_),
123 stateSeed_(rhs.stateSeed_), covSeed_(rhs.covSeed_)
127 std::map<const AbsTrackRep*, AbsTrackRep*> oldRepNewRep;
132 oldRepNewRep[(*it)] = newRep;
232 if ((*it)->hasFitterInfo(rep)) {
242 if ((*it)->hasFitterInfo(rep)) {
262 if ((*it)->hasFitterInfo(rep)) {
272 if ((*it)->hasFitterInfo(rep)) {
290 if (point ==
nullptr) {
291 Exception exc(
"Track::getFittedState ==> no trackPoint with fitterInfo for rep",__LINE__,__FILE__);
305 Exception exc(
"Track::getIdForRep ==> cannot find TrackRep in Track",__LINE__,__FILE__);
329 return (dynamic_cast<KalmanFitStatus*>(
fitStatuses_.at(rep)) !=
nullptr);
365 debugOut <<
"Track::insertPoint at position " <<
id <<
"\n";
423 int n = points.size();
432 for (std::vector<TrackPoint*>::iterator
p = points.begin();
p != points.end(); ++
p)
433 (*p)->setTrack(
this);
473 debugOut <<
"Track::deletePoint at position " <<
id <<
"\n";
517 if(trackPoint->hasFitterInfo(rep)) {
518 trackPoint->deleteFitterInfo(rep);
533 std::map<const AbsTrackRep*, AbsTrackRep*> otherRepThisRep;
534 std::vector<const AbsTrackRep*> otherRepsToRemove;
536 for (std::vector<AbsTrackRep*>::const_iterator otherRep=other->
trackReps_.begin(); otherRep!=other->
trackReps_.end(); ++otherRep) {
538 for (std::vector<AbsTrackRep*>::const_iterator thisRep=
trackReps_.begin(); thisRep!=
trackReps_.end(); ++thisRep) {
539 if ((*thisRep)->isSame(*otherRep)) {
540 otherRepThisRep[*otherRep] = *thisRep;
542 debugOut <<
" map other rep " << *otherRep <<
" to " << (*thisRep) <<
"\n";
545 Exception exc(
"Track::mergeTrack ==> more than one matching rep.",__LINE__,__FILE__);
554 otherRepsToRemove.push_back(*otherRep);
556 debugOut <<
" remove other rep " << *otherRep <<
"\n";
562 std::vector<TrackPoint*> points;
565 for (std::vector<TrackPoint*>::const_iterator otherTp=other->
trackPoints_.begin(); otherTp!=other->
trackPoints_.end(); ++otherTp) {
566 points.push_back(
new TrackPoint(**otherTp, otherRepThisRep, &otherRepsToRemove));
592 for (std::vector<TrackPoint*>::const_iterator pointIt =
trackPoints_.begin(); pointIt !=
trackPoints_.end(); ++pointIt) {
593 (*pointIt)->deleteFitterInfo(rep);
611 if (
id >= 0 && (
unsigned int)
id <
trackReps_.size())
615 errorOut <<
"Track::setCardinalRep: Attempted to set cardinalRep_ to a value out of bounds. Resetting cardinalRep_ to 0." << std::endl;
627 double minChi2(9.E99);
631 if (
it->second->isFitConverged()) {
632 if (
it->second->getChi2() < minChi2) {
633 minChi2 =
it->second->getChi2();
639 if (bestRep !=
nullptr) {
658 int equalUntil(-1), equalFrom(nPoints);
659 for (
int i = 0;
i<nPoints; ++
i) {
666 if (equalUntil == nPoints-1)
672 for (
int i = nPoints-1;
i>equalUntil; --
i) {
680 debugOut <<
"Track::sort. Equal up to (including) hit " << equalUntil <<
" and from (including) hit " << equalFrom <<
" \n";
701 double fittedCharge = fittedState.
getCharge();
704 if (
trackReps_[
i]->getPDGCharge() * fittedCharge < 0) {
730 if (rep !=
nullptr) {
751 debugOut <<
"Track::deleteForwardInfo from position " << startId <<
" to " << endId <<
"\n";
762 assert (endId >= startId);
764 for (std::vector<TrackPoint*>::const_iterator pointIt =
trackPoints_.begin() + startId; pointIt !=
trackPoints_.begin() + endId; ++pointIt) {
765 if (rep !=
nullptr) {
766 if ((*pointIt)->hasFitterInfo(rep))
767 (*pointIt)->getFitterInfo(rep)->deleteForwardInfo();
770 const std::vector<AbsFitterInfo*> fitterInfos = (*pointIt)->getFitterInfos();
771 for (std::vector<AbsFitterInfo*>::const_iterator fitterInfoIt = fitterInfos.begin(); fitterInfoIt != fitterInfos.end(); ++fitterInfoIt) {
772 (*fitterInfoIt)->deleteForwardInfo();
781 debugOut <<
"Track::deleteBackwardInfo from position " << startId <<
" to " << endId <<
"\n";
792 assert (endId >= startId);
795 for (std::vector<TrackPoint*>::const_iterator pointIt =
trackPoints_.begin() + startId; pointIt !=
trackPoints_.begin() + endId; ++pointIt) {
796 if (rep !=
nullptr) {
797 if ((*pointIt)->hasFitterInfo(rep))
798 (*pointIt)->getFitterInfo(rep)->deleteBackwardInfo();
801 const std::vector<AbsFitterInfo*> fitterInfos = (*pointIt)->getFitterInfos();
802 for (std::vector<AbsFitterInfo*>::const_iterator fitterInfoIt = fitterInfos.begin(); fitterInfoIt != fitterInfos.end(); ++fitterInfoIt) {
803 (*fitterInfoIt)->deleteBackwardInfo();
812 debugOut <<
"Track::deleteReferenceInfo from position " << startId <<
" to " << endId <<
"\n";
823 assert (endId >= startId);
825 for (std::vector<TrackPoint*>::const_iterator pointIt =
trackPoints_.begin() + startId; pointIt !=
trackPoints_.begin() + endId; ++pointIt) {
826 if (rep !=
nullptr) {
827 if ((*pointIt)->hasFitterInfo(rep))
828 (*pointIt)->getFitterInfo(rep)->deleteReferenceInfo();
831 std::vector<AbsFitterInfo*> fitterInfos = (*pointIt)->getFitterInfos();
832 for (std::vector<AbsFitterInfo*>::const_iterator fitterInfoIt = fitterInfos.begin(); fitterInfoIt != fitterInfos.end(); ++fitterInfoIt) {
833 (*fitterInfoIt)->deleteReferenceInfo();
842 debugOut <<
"Track::deleteMeasurementInfo from position " << startId <<
" to " << endId <<
"\n";
853 assert (endId >= startId);
855 for (std::vector<TrackPoint*>::const_iterator pointIt =
trackPoints_.begin() + startId; pointIt !=
trackPoints_.begin() + endId; ++pointIt) {
856 if (rep !=
nullptr) {
857 if ((*pointIt)->hasFitterInfo(rep))
858 (*pointIt)->getFitterInfo(rep)->deleteMeasurementInfo();
861 std::vector<AbsFitterInfo*> fitterInfos = (*pointIt)->getFitterInfos();
862 for (std::vector<AbsFitterInfo*>::const_iterator fitterInfoIt = fitterInfos.begin(); fitterInfoIt != fitterInfos.end(); ++fitterInfoIt) {
863 (*fitterInfoIt)->deleteMeasurementInfo();
872 debugOut <<
"Track::deleteFitterInfo from position " << startId <<
" to " << endId <<
"\n";
883 assert (endId >= startId);
885 for (std::vector<TrackPoint*>::const_iterator pointIt =
trackPoints_.begin() + startId; pointIt !=
trackPoints_.begin() + endId; ++pointIt) {
886 if (rep !=
nullptr) {
887 if ((*pointIt)->hasFitterInfo(rep))
888 (*pointIt)->deleteFitterInfo(rep);
891 for (std::vector<AbsTrackRep*>::const_iterator repIt =
trackReps_.begin(); repIt !=
trackReps_.end(); ++repIt) {
892 if ((*pointIt)->hasFitterInfo(*repIt))
893 (*pointIt)->deleteFitterInfo(*repIt);
907 bool backwards(
false);
908 if (startId > endId) {
909 double temp = startId;
923 for (std::vector<TrackPoint*>::const_iterator pointIt =
trackPoints_.begin() + startId; pointIt !=
trackPoints_.begin() + endId; ++pointIt) {
924 if (! (*pointIt)->hasFitterInfo(rep)) {
925 Exception e(
"Track::getTracklength: trackPoint has no fitterInfo", __LINE__,__FILE__);
933 state = (*pointIt)->getFitterInfo(rep)->getFittedState();
954 for (
unsigned int j = 0;
j < measurements.size(); ++
j) {
959 if (dynamic_cast<const PlanarMeasurement*>(m)) {
986 if (startId > endId) {
1002 Exception e(
"Track::getTOF: trackPoint has no fitterInfo", __LINE__,__FILE__);
1020 assert(startId <= endId);
1023 std::vector< AbsFitterInfo* > fis;
1027 if (rep ==
nullptr) {
1028 fis = (*tp)->getFitterInfos();
1030 else if ((*tp)->hasFitterInfo(rep)) {
1031 fis.push_back((*tp)->getFitterInfo(rep));
1034 for (std::vector< AbsFitterInfo* >::iterator fi = fis.begin(); fi != fis.end(); ++fi) {
1051 it->second->getPruneFlags().setFlags(option);
1087 std::vector< AbsFitterInfo* > fis =
trackPoints_[
i]->getFitterInfos();
1088 for (
unsigned int j = 0;
j<fis.size(); ++
j) {
1121 TString opt = option;
1123 if (opt.Contains(
"C")) {
1184 for (std::vector<AbsTrackRep*>::const_iterator rep =
trackReps_.begin(); rep !=
trackReps_.end(); ++rep) {
1257 printOut <<
"=======================================================================================\n";
1264 printOut <<
" (This is the cardinal rep)";
1269 printOut <<
"---------------------------------------------------------------------------------------\n";
1272 printOut <<
"TrackPoint Nr. " <<
i <<
"\n";
1274 printOut <<
"..........................................................................\n";
1278 it->second->Print();
1281 printOut <<
"=======================================================================================\n";
1289 bool consistent =
true;
1290 std::stringstream failures;
1292 std::map<const AbsTrackRep*, const KalmanFitterInfo*> prevFis;
1296 failures <<
"Track::checkConsistency(): stateSeed_ dimension != 6" << std::endl;
1302 failures <<
"Track::checkConsistency(): covSeed_ dimension != 6" << std::endl;
1310 failures <<
"Track::checkConsistency(): Warning: covSeed_ is zero" << std::endl;
1315 failures <<
"Track::checkConsistency(): Number of fitStatuses is != number of TrackReps " << std::endl;
1322 failures <<
"Track::checkConsistency(): cardinalRep id " <<
cardinalRep_ <<
" out of bounds" << std::endl;
1327 for (std::vector<AbsTrackRep*>::const_iterator rep =
trackReps_.begin(); rep !=
trackReps_.end(); ++rep) {
1329 if ((*rep) ==
nullptr) {
1330 failures <<
"Track::checkConsistency(): TrackRep is nullptr" << std::endl;
1336 TParticlePDG*
particle = TDatabasePDG::Instance()->GetParticle((*rep)->getPDG());
1337 if (particle ==
nullptr) {
1338 failures <<
"Track::checkConsistency(): TrackRep pdg ID " << (*rep)->getPDG() <<
" is not valid" << std::endl;
1345 failures <<
"Track::checkConsistency(): No FitStatus for Rep or FitStatus is nullptr" << std::endl;
1354 if ((*
tp) ==
nullptr) {
1355 failures <<
"Track::checkConsistency(): TrackPoint is nullptr" << std::endl;
1360 if ((*tp)->getTrack() !=
this) {
1361 failures <<
"Track::checkConsistency(): TrackPoint does not point back to this track" << std::endl;
1367 const std::vector<AbsMeasurement*>& rawMeasurements = (*tp)->getRawMeasurements();
1368 for (std::vector<AbsMeasurement*>::const_iterator
m = rawMeasurements.begin();
m != rawMeasurements.end(); ++
m) {
1370 if ((*
m) ==
nullptr) {
1371 failures <<
"Track::checkConsistency(): Measurement is nullptr" << std::endl;
1376 if ((*m)->getTrackPoint() != *
tp) {
1377 failures <<
"Track::checkConsistency(): Measurement does not point back to correct TrackPoint" << std::endl;
1384 std::vector<AbsFitterInfo*> fitterInfos = (*tp)->getFitterInfos();
1385 for (std::vector<AbsFitterInfo*>::const_iterator fi = fitterInfos.begin(); fi != fitterInfos.end(); ++fi) {
1387 if ((*fi) ==
nullptr) {
1388 failures <<
"Track::checkConsistency(): FitterInfo is nullptr. TrackPoint: " << *
tp << std::endl;
1395 for (std::vector<AbsTrackRep*>::const_iterator rep =
trackReps_.begin(); rep !=
trackReps_.end(); ++rep) {
1396 if ( (*rep) == (*fi)->getRep() ) {
1401 failures <<
"Track::checkConsistency(): fitterInfo points to TrackRep which is not in Track" << std::endl;
1406 if (!( (*fi)->checkConsistency(&(
this->getFitStatus((*fi)->getRep())->getPruneFlags())) ) ) {
1407 failures <<
"Track::checkConsistency(): FitterInfo not consistent. TrackPoint: " << *
tp << std::endl;
1412 if (dynamic_cast<KalmanFitterInfo*>(*fi) !=
nullptr) {
1413 if (prevFis[(*fi)->getRep()] !=
nullptr &&
1415 prevFis[(*fi)->getRep()]->hasReferenceState() ) {
1417 double prevLen = prevFis[(*fi)->getRep()]->getReferenceState()->getBackwardSegmentLength();
1418 if (fabs(prevLen + len) > 1
E-10 ) {
1419 failures <<
"Track::checkConsistency(): segment lengths of reference states for rep " << (*fi)->getRep() <<
" (id " <<
getIdForRep((*fi)->getRep()) <<
") at TrackPoint " << (*
tp) <<
" don't match" << std::endl;
1420 failures << prevLen <<
" + " << len <<
" = " << prevLen + len << std::endl;
1421 failures <<
"TrackPoint " << *
tp <<
", FitterInfo " << *fi <<
", rep " <<
getIdForRep((*fi)->getRep()) << std::endl;
1430 prevFis[(*fi)->getRep()] =
nullptr;
1438 std::vector<TrackPoint*> trackPointsWithMeasurement;
1441 if ((*it)->hasRawMeasurements()) {
1442 trackPointsWithMeasurement.push_back(*
it);
1447 failures <<
"Track::checkConsistency(): trackPointsWithMeasurement_ has incorrect size" << std::endl;
1452 for (
unsigned int i = 0;
i < trackPointsWithMeasurement.size(); ++
i) {
1454 failures <<
"Track::checkConsistency(): trackPointsWithMeasurement_ is not correct" << std::endl;
1456 failures <<
"should have id " << i <<
", address " << trackPointsWithMeasurement[
i] << std::endl;
1462 if (not consistent) {
1471 debugOut <<
"Track::trackHasChanged \n";
1478 it->second->setHasTrackChanged();
1488 if ((*it)->hasRawMeasurements()) {
1495 void Track::Streamer(TBuffer &R__b)
1498 const bool streamTrackPoints =
true;
1502 if (R__b.IsReading()) {
1504 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
if (R__v) { }
1505 TObject::Streamer(R__b);
1507 std::vector<AbsTrackRep*> &R__stl =
trackReps_;
1511 Error(
"trackReps_ streamer",
"Missing the TClass object for genfit::AbsTrackRep!");
1516 R__stl.reserve(R__n);
1517 for (R__i = 0; R__i < R__n; R__i++) {
1520 R__stl.push_back(R__t);
1524 if (streamTrackPoints)
1530 Error(
"trackPoints_ streamer",
"Missing the TClass object for genfit::TrackPoint!");
1535 R__stl.reserve(R__n);
1536 for (R__i = 0; R__i < R__n; R__i++) {
1541 R__stl.push_back(R__t);
1545 std::map<const AbsTrackRep*,FitStatus*> &R__stl =
fitStatuses_;
1549 Error(
"fitStatuses_ streamer",
"Missing the TClass object for genfit::AbsTrackRep!");
1554 Error(
"fitStatuses_ streamer",
"Missing the TClass object for genfit::FitStatus!");
1559 for (R__i = 0; R__i < R__n; R__i++) {
1577 R__b.CheckByteCount(R__s, R__c, thisClass::IsA());
1581 R__c = R__b.WriteVersion(thisClass::IsA(), kTRUE);
1582 TObject::Streamer(R__b);
1584 std::vector<AbsTrackRep*> &R__stl =
trackReps_;
1585 int R__n=int(R__stl.size());
1590 Error(
"trackReps_ streamer",
"Missing the TClass object for genfit::AbsTrackRep!");
1593 std::vector<AbsTrackRep*>::iterator R__k;
1594 for (R__k = R__stl.begin(); R__k != R__stl.end(); ++R__k) {
1600 if (streamTrackPoints)
1603 int R__n=int(R__stl.size());
1606 std::vector<TrackPoint*>::iterator R__k;
1607 for (R__k = R__stl.begin(); R__k != R__stl.end(); ++R__k) {
1613 std::map<const AbsTrackRep*,FitStatus*> &R__stl =
fitStatuses_;
1614 int R__n=int(R__stl.size());
1619 Error(
"fitStatuses_ streamer",
"Missing the TClass object for genfit::AbsTrackRep!");
1624 Error(
"fitStatuses_ streamer",
"Missing the TClass object for genfit::FitStatus!");
1627 std::map<const AbsTrackRep*,FitStatus*>::iterator R__k;
1628 for (R__k = R__stl.begin(); R__k != R__stl.end(); ++R__k) {
1631 R__b << ((*R__k).second);
1638 R__b.SetByteCount(R__c, kTRUE);