14 #include <GenFit/AbsFitterInfo.h>
15 #include <GenFit/AbsHMatrix.h>
16 #include <GenFit/AbsMeasurement.h>
17 #include <GenFit/AbsTrackRep.h>
18 #include <GenFit/DetPlane.h>
19 #include <GenFit/Exception.h>
20 #include <GenFit/FitStatus.h>
21 #include <GenFit/KalmanFittedStateOnPlane.h>
22 #include <GenFit/KalmanFitterInfo.h>
23 #include <GenFit/MeasuredStateOnPlane.h>
24 #include <GenFit/MeasurementOnPlane.h>
25 #include <GenFit/SharedPlanePtr.h>
28 #include <GenFit/TrackPoint.h>
30 #include <TMatrixDfwd.h>
31 #include <TMatrixDSymfwd.h>
33 #include <TMatrixTSym.h>
34 #include <TVectorDfwd.h>
45 #define LogDebug(exp) std::cout << "DEBUG: " << __FILE__ << ": " << __LINE__ << ": " << exp << std::endl
46 #define LogError(exp) std::cout << "ERROR: " << __FILE__ << ": " << __LINE__ << ": " << exp << std::endl
47 #define LogWarning(exp) std::cout << "WARNING: " << __FILE__ << ": " << __LINE__ << ": " << exp << std::endl
49 #define WILD_DOUBLE -999999
57 ofstream fout_matrix(
"matrix.txt");
62 Track::Track(genfit::AbsTrackRep* rep, TVector3 seed_pos, TVector3 seed_mom, TMatrixDSym seed_cov,
const int v)
68 genfit::MeasuredStateOnPlane seedMSoP(rep);
69 seedMSoP.setPosMomCov(seed_pos, seed_mom, seed_cov);
72 TVectorD seedState(6);
73 TMatrixDSym seedCov(6);
74 seedMSoP.get6DStateCov(seedState, seedCov);
76 _track =
new genfit::Track(rep, seedState, seedCov);
90 std::vector<genfit::AbsMeasurement*> msmts;
92 _track->insertPoint(
new genfit::TrackPoint(msmts,
_track));
106 std::vector<genfit::AbsMeasurement*> msmts;
107 msmts.push_back(measurement->getMeasurement());
109 new genfit::TrackPoint(msmts,
_track));
112 _clusterIDs.push_back(measurement->get_cluster_ID());
152 genfit::SharedPlanePtr destPlane(
new genfit::DetPlane(O, n));
154 genfit::AbsTrackRep* rep =
_track->getCardinalRep();
155 genfit::TrackPoint*
tp =
_track->getPointWithMeasurementAndFitterInfo(
159 std::cout <<
"Track has no TrackPoint with fitterInfo! \n";
162 std::unique_ptr<genfit::KalmanFittedStateOnPlane> kfsop(
new genfit::KalmanFittedStateOnPlane(
163 *(static_cast<genfit::KalmanFitterInfo*>(tp->getFitterInfo(rep))->getBackwardUpdate())));
167 pathlenth = rep->extrapolateToPlane(*kfsop, destPlane);
169 catch (genfit::Exception&
e)
171 std::cerr <<
"Exception, next track" << std::endl;
172 std::cerr << e.what();
177 state = *
dynamic_cast<genfit::MeasuredStateOnPlane*
>(kfsop.get());
186 genfit::MeasuredStateOnPlane* state =
new genfit::MeasuredStateOnPlane();
197 double Track::extrapolateToLine(genfit::MeasuredStateOnPlane& state, TVector3 line_point, TVector3 line_direction,
const int tr_point_id)
const
201 genfit::AbsTrackRep* rep =
_track->getCardinalRep();
202 genfit::TrackPoint*
tp =
_track->getPointWithMeasurementAndFitterInfo(
206 std::cout <<
"Track has no TrackPoint with fitterInfo! \n";
209 std::unique_ptr<genfit::KalmanFittedStateOnPlane> kfsop(
new genfit::KalmanFittedStateOnPlane(
210 *(static_cast<genfit::KalmanFitterInfo*>(tp->getFitterInfo(rep))->getBackwardUpdate())));
214 pathlenth = rep->extrapolateToLine(*kfsop, line_point, line_direction);
216 catch (genfit::Exception&
e)
218 std::cerr <<
"Exception, next track" << std::endl;
219 std::cerr << e.what();
224 state = *
dynamic_cast<genfit::MeasuredStateOnPlane*
>(kfsop.get());
233 genfit::MeasuredStateOnPlane* state =
new genfit::MeasuredStateOnPlane();
234 double pathlenth = this->
extrapolateToLine(*state, line_point, line_direction, tr_point_id);
244 double Track::extrapolateToCylinder(genfit::MeasuredStateOnPlane& state,
double radius, TVector3 line_point, TVector3 line_direction,
const int tr_point_id,
const int direction)
const
247 std::cout << __LINE__ << std::endl;
250 <<
": tr_point_id: " << tr_point_id
251 <<
": direction: " << direction
254 assert(direction == 1 or direction == -1);
258 genfit::AbsTrackRep* rep =
_track->getCardinalRep();
267 bool have_tp_with_fit_info =
false;
268 std::unique_ptr<genfit::MeasuredStateOnPlane> kfsop = NULL;
269 if (
_track->getNumPointsWithMeasurement() > 0)
274 genfit::TrackPoint*
tp =
_track->getPointWithMeasurement(tr_point_id);
280 if (dynamic_cast<genfit::KalmanFitterInfo*>(tp->getFitterInfo(rep)))
284 if (static_cast<genfit::KalmanFitterInfo*>(tp->getFitterInfo(
286 ->getForwardUpdate())
288 have_tp_with_fit_info =
true;
290 std::unique_ptr<genfit::MeasuredStateOnPlane>(
new genfit::KalmanFittedStateOnPlane(
291 *(static_cast<genfit::KalmanFitterInfo*>(tp->getFitterInfo(
293 ->getForwardUpdate())));
298 if (static_cast<genfit::KalmanFitterInfo*>(tp->getFitterInfo(
300 ->getBackwardUpdate())
302 have_tp_with_fit_info =
true;
304 std::unique_ptr<genfit::MeasuredStateOnPlane>(
new genfit::KalmanFittedStateOnPlane(
305 *(static_cast<genfit::KalmanFitterInfo*>(tp->getFitterInfo(
307 ->getBackwardUpdate())));
313 if (!have_tp_with_fit_info)
316 std::cout << __LINE__ <<
": !have_tp_with_fit_info" << std::endl;
318 kfsop = std::unique_ptr<genfit::MeasuredStateOnPlane>(
new genfit::MeasuredStateOnPlane(rep));
319 rep->setPosMomCov(*kfsop,
_track->getStateSeed(),
_track->getCovSeed());
322 if (!kfsop)
return pathlenth;
327 pathlenth = rep->extrapolateToCylinder(*kfsop, radius, line_point, line_direction);
329 catch (genfit::Exception&
e)
334 std::cerr << e.what();
339 state = *
dynamic_cast<genfit::MeasuredStateOnPlane*
>(kfsop.get());
348 assert(direction == 1 or direction == -1);
349 genfit::MeasuredStateOnPlane* state =
new genfit::MeasuredStateOnPlane();
350 double pathlenth = this->
extrapolateToCylinder(*state, radius, line_point, line_direction, tr_point_id, direction);
361 const std::vector<PHGenFit::Measurement*>& measurements,
362 std::map<
double, std::shared_ptr<PHGenFit::Track> >& incr_chi2s_new_tracks,
363 const int base_tp_idx,
365 const float blowup_factor,
366 const bool use_fitted_state)
const
371 <<
" : base_tp_idx: " << base_tp_idx
372 <<
" : direction: " << direction
373 <<
" : blowup_factor: " << blowup_factor
374 <<
" : use_fitted_state: " << use_fitted_state
378 if (measurements.size() == 0)
return -1;
382 std::shared_ptr<PHGenFit::Track> new_track = NULL;
384 new_track = std::shared_ptr<PHGenFit::Track>(
new PHGenFit::Track(*
this));
391 genfit::Track*
track = new_track->getGenFitTrack();
392 genfit::AbsTrackRep* rep = track->getCardinalRep();
395 genfit::TrackPoint* tp_base = NULL;
396 std::unique_ptr<genfit::MeasuredStateOnPlane> currentState = NULL;
397 genfit::SharedPlanePtr
plane = NULL;
399 if (track->getNumPointsWithMeasurement() > 0)
401 tp_base = track->getPointWithMeasurement(base_tp_idx);
402 newFi = !(tp_base->hasFitterInfo(rep));
406 std::cout << __LINE__ <<
": "
407 <<
"newFi: " << newFi << std::endl;
411 currentState = std::unique_ptr<genfit::MeasuredStateOnPlane>(
new genfit::MeasuredStateOnPlane(rep));
412 rep->setPosMomCov(*currentState, track->getStateSeed(),
413 track->getCovSeed());
419 genfit::KalmanFitterInfo* kfi =
static_cast<genfit::KalmanFitterInfo*
>(tp_base->getFitterInfo(rep));
432 if (use_fitted_state)
434 const genfit::MeasuredStateOnPlane* tempFS = &(kfi->getFittedState(
true));
442 currentState = std::unique_ptr<genfit::MeasuredStateOnPlane>(
new genfit::MeasuredStateOnPlane(*tempFS));
446 genfit::MeasuredStateOnPlane* tempUpdate = kfi->getUpdate(direction);
454 currentState = std::unique_ptr<genfit::MeasuredStateOnPlane>(
new genfit::MeasuredStateOnPlane(*tempUpdate));
467 if (blowup_factor > 1)
469 currentState->blowUpCov(blowup_factor,
true, 1
e6);
472 catch (genfit::Exception &
e)
477 <<
": Fitted state not found!"
479 std::cerr << e.what() << std::endl;
481 currentState = std::unique_ptr<genfit::MeasuredStateOnPlane>(
new genfit::MeasuredStateOnPlane(rep));
482 rep->setPosMomCov(*currentState, track->getStateSeed(),
483 track->getCovSeed());
487 std::cout << __LINE__ << std::endl;
499 new_track->addMeasurement(measurement);
502 std::cout << __LINE__ <<
": clusterIDs size: " << new_track->get_cluster_IDs().size() << std::endl;
503 std::cout << __LINE__ <<
": clusterkeyss size: " << new_track->get_cluster_keys().size() << std::endl;
507 genfit::TrackPoint*
tp = new_track->getGenFitTrack()->getPoint(-1);
509 std::cout << __LINE__ << std::endl;
511 genfit::KalmanFitterInfo* fi =
new genfit::KalmanFitterInfo(tp, rep);
512 tp->setFitterInfo(fi);
516 <<
": track->getPointWithMeasurement(): " << track->getPointWithMeasurement(-1)
526 std::cout << __LINE__ << std::endl;
528 const std::vector<genfit::AbsMeasurement*>& rawMeasurements =
529 tp->getRawMeasurements();
531 plane = rawMeasurements[0]->constructPlane(*currentState);
537 rep->extrapolateToPlane(*currentState, plane);
543 LogWarning(
"Can not extrapolate to measuremnt with cluster_ID and cluster key: ") << measurement->get_cluster_ID()
544 <<
" " << measurement->get_cluster_key()
550 std::cout << __LINE__ << std::endl;
552 fi->setPrediction(currentState->clone(), direction);
553 genfit::MeasuredStateOnPlane* state = fi->getPrediction(direction);
555 std::cout << __LINE__ << std::endl;
557 TVectorD stateVector(state->getState());
558 TMatrixDSym cov(state->getCov());
561 std::cout << __LINE__ << std::endl;
569 for (std::vector<genfit::AbsMeasurement*>::const_iterator
it =
570 rawMeasurements.begin();
571 it != rawMeasurements.end(); ++
it)
573 fi->addMeasurementsOnPlane(
574 (*it)->constructMeasurementsOnPlane(*state));
580 std::cout << __LINE__ << std::endl;
583 const std::vector<genfit::MeasurementOnPlane*>& measurements_on_plane = fi->getMeasurementsOnPlane();
587 <<
": size of fi's MeasurementsOnPlane: " << measurements_on_plane.size()
590 for (std::vector<genfit::MeasurementOnPlane*>::const_iterator
it =
591 measurements_on_plane.begin();
592 it != measurements_on_plane.end(); ++
it)
594 const genfit::MeasurementOnPlane& mOnPlane = **
it;
597 const TVectorD& measurement(mOnPlane.getState());
598 const genfit::AbsHMatrix*
H(mOnPlane.getHMatrix());
600 const TMatrixDSym& V(mOnPlane.getCov());
602 TVectorD res(measurement -
H->Hv(stateVector));
605 std::cout << __LINE__ << std::endl;
615 TMatrixDSym covSumInv(cov);
620 genfit::tools::invertMatrix(covSumInv);
622 catch (genfit::Exception &
e)
630 TMatrixD CHt(
H->MHt(cov));
631 #ifdef _PRINT_MATRIX_
632 std::cout << __LINE__ <<
": V_{k}:" << std::endl;
634 std::cout << __LINE__ <<
": R_{k}^{-1}:" << std::endl;
636 std::cout << __LINE__ <<
": C_{k|k-1}:" << std::endl;
638 std::cout << __LINE__ <<
": C_{k|k-1} H_{k}^{T} :" << std::endl;
640 std::cout << __LINE__ <<
": K_{k} :" << std::endl;
641 TMatrixD Kk(CHt, TMatrixD::kMult, covSumInv);
643 std::cout << __LINE__ <<
": res:" << std::endl;
647 TMatrixD(CHt, TMatrixD::kMult, covSumInv) * res);
651 covSumInv.Similarity(CHt);
655 std::cout << __LINE__ << std::endl;
666 TVectorD resNew(measurement -
H->Hv(stateVector));
669 TMatrixDSym HCHt(cov);
676 genfit::tools::invertMatrix(HCHt);
678 catch (genfit::Exception &
e)
685 chi2inc += HCHt.Similarity(resNew);
687 ndfInc += measurement.GetNrows();
689 #ifdef _PRINT_MATRIX_
690 std::cout << __LINE__ <<
": V - HCHt:" << std::endl;
692 std::cout << __LINE__ <<
": resNew:" << std::endl;
697 std::cout << __LINE__ <<
": ndfInc: " << ndfInc << std::endl;
698 std::cout << __LINE__ <<
": chi2inc: " << chi2inc << std::endl;
701 currentState->setStateCovPlane(stateVector, cov, plane);
702 currentState->setAuxInfo(state->getAuxInfo());
704 genfit::KalmanFittedStateOnPlane* updatedSOP =
705 new genfit::KalmanFittedStateOnPlane(*currentState, chi2inc,
707 fi->setUpdate(updatedSOP, direction);
712 incr_chi2s_new_tracks.insert(std::make_pair(chi2inc, new_track));
722 genfit::AbsTrackRep* rep =
_track->getCardinalRep();
723 genfit::TrackPoint*
tp =
_track->getPointWithMeasurementAndFitterInfo(
727 std::cout <<
"Track has no TrackPoint with fitterInfo! \n";
730 std::unique_ptr<genfit::KalmanFittedStateOnPlane> kfsop(
new genfit::KalmanFittedStateOnPlane(
731 *(static_cast<genfit::KalmanFitterInfo*>(tp->getFitterInfo(rep))->getBackwardUpdate())));
735 pathlenth = rep->extrapolateToPoint(*kfsop, P);
737 catch (genfit::Exception&
e)
739 std::cerr <<
"Exception, next track" << std::endl;
740 std::cerr << e.what();
745 state = *
dynamic_cast<genfit::MeasuredStateOnPlane*
>(kfsop.get());
754 genfit::MeasuredStateOnPlane* state =
new genfit::MeasuredStateOnPlane();
767 double chi2 = std::numeric_limits<double>::quiet_NaN();
769 genfit::AbsTrackRep* rep =
_track->getCardinalRep();
772 genfit::FitStatus*
fs =
_track->getFitStatus(rep);
774 chi2 = fs->getChi2();
781 double ndf = std::numeric_limits<double>::quiet_NaN();
783 genfit::AbsTrackRep* rep =
_track->getCardinalRep();
786 genfit::FitStatus*
fs =
_track->getFitStatus(rep);
795 double charge = std::numeric_limits<double>::quiet_NaN();
801 genfit::TrackPoint* tp_base =
nullptr;
803 if (
_track->getNumPointsWithMeasurement() > 0)
805 tp_base =
_track->getPointWithMeasurement(0);
808 if (!tp_base)
return charge;
810 genfit::AbsTrackRep* rep =
_track->getCardinalRep();
813 genfit::KalmanFitterInfo* kfi =
static_cast<genfit::KalmanFitterInfo*
>(tp_base->getFitterInfo(rep));
817 const genfit::MeasuredStateOnPlane* state = &(kfi->getFittedState(
true));
822 charge = rep->getCharge(*state);
828 std::cerr <<
"Track::get_charge - Error - obtaining charge failed. Returning NAN as charge." << std::endl;
836 TVector3
mom(0, 0, 0);
840 genfit::TrackPoint* tp_base =
nullptr;
842 if (
_track->getNumPointsWithMeasurement() > 0)
844 tp_base =
_track->getPointWithMeasurement(0);
847 if (!tp_base)
return mom;
849 genfit::AbsTrackRep* rep =
_track->getCardinalRep();
852 genfit::KalmanFitterInfo* kfi =
static_cast<genfit::KalmanFitterInfo*
>(tp_base->getFitterInfo(rep));
854 if (!kfi)
return mom;
856 const genfit::MeasuredStateOnPlane* state = &(kfi->getFittedState(
true));
859 return state->getMom();