7 #include <TMatrixDSym.h>
9 #include <GenFit/MeasuredStateOnPlane.h>
10 #include <GenFit/MeasurementOnPlane.h>
11 #include <GenFit/WireMeasurement.h>
12 #include <GenFit/PlanarMeasurement.h>
13 #include <GenFit/RKTrackRep.h>
14 #include <GenFit/AbsFitterInfo.h>
15 #include <GenFit/KalmanFitterInfo.h>
16 #include <GenFit/StateOnPlane.h>
25 static bool inited =
false;
27 static double Z_TARGET;
29 static double Z_UPSTREAM;
33 static double SIGX_BEAM;
34 static double SIGY_BEAM;
37 void initGlobalVariables()
59 GFTrack::GFTrack(): _track(nullptr), _trkrep(nullptr), _propState(nullptr), _virtMeas(nullptr), _trkcand(nullptr), _pdg(0)
61 initGlobalVariables();
64 GFTrack::GFTrack(
SRecTrack& recTrack): _track(nullptr), _trkrep(nullptr), _propState(nullptr), _virtMeas(nullptr), _trkcand(nullptr), _pdg(0)
66 _pdg = recTrack.
getCharge() > 0 ? -13 : 13;
67 _trkrep =
new genfit::RKTrackRep(_pdg);
72 TVectorD seed_state(6);
73 seed_state[0] = seed_pos.X();
74 seed_state[1] = seed_pos.Y();
75 seed_state[2] = seed_pos.Z();
76 seed_state[3] = seed_mom.Px();
77 seed_state[4] = seed_mom.Py();
78 seed_state[5] = seed_mom.Pz();
80 TMatrixDSym seed_cov(6);
81 double uncertainty[6] = {10., 10., 10., 3., 3., 10.};
82 for(
int i = 0; i < 6; i++)
84 for(
int j = 0; j < 6; j++)
86 seed_cov[i][j] = uncertainty[i]*uncertainty[j];
89 _track =
new genfit::Track(_trkrep, seed_state, seed_cov);
93 for(
int i = 0; i < nHits; ++i)
101 GFTrack::GFTrack(
Tracklet& tracklet): _track(nullptr), _trkrep(nullptr), _propState(nullptr), _virtMeas(nullptr), _trkcand(nullptr), _pdg(0)
108 if(_track !=
nullptr)
delete _track;
113 if(_trkrep !=
nullptr) _trkrep->setDebugLvl(v);
118 for(
auto iter = measurements.begin(); iter != measurements.end(); ++iter)
120 if(!(*iter)->isEnabled())
continue;
129 genfit::TrackPoint* tp =
new genfit::TrackPoint(measurement, _track);
130 tp->setSortingParameter(measurement->
getZ());
131 _track->insertPoint(tp);
136 genfit::AbsTrackRep* rep = _track->getCardinalRep();
139 genfit::FitStatus* fs = _track->getFitStatus(rep);
140 if(fs)
return fs->getChi2();
148 genfit::AbsTrackRep* rep = _track->getCardinalRep();
151 genfit::FitStatus* fs = _track->getFitStatus(rep);
152 if(fs)
return fs->getNdf();
160 double z = meas->
getZ();
161 if(z < _measurements.front()->getZ())
165 else if(z > _measurements.back()->getZ())
167 return _measurements.size() - 1;
171 auto iter = std::lower_bound(_measurements.begin(), _measurements.end(), meas, [](
GFMeasurement* a,
GFMeasurement* b) {
return (a->getZ() < b->getZ()); });
172 int idx = std::distance(_measurements.begin(), iter);
173 return fabs(z - _measurements[idx]->getZ()) < fabs(z - _measurements[idx-1]->getZ()) ? idx : idx-1;
179 TVector3 linePoint = (endPoint2 + endPoint1);
180 TVector3 lineDir = endPoint2 - endPoint1;
182 if(!setInitialStateForExtrap(startPtID))
return -9999.;
184 genfit::AbsTrackRep* rep = _track->getCardinalRep();
185 double len = rep->extrapolateToLine(*_propState, linePoint, lineDir);
187 TVectorD hitcoord(7);
188 hitcoord[0] = endPoint1.X();
189 hitcoord[1] = endPoint1.Y();
190 hitcoord[2] = endPoint1.Z();
191 hitcoord[3] = endPoint2.X();
192 hitcoord[4] = endPoint2.Y();
193 hitcoord[5] = endPoint2.Z();
195 TMatrixDSym tempcov(7);
197 _virtMeas.reset(
new genfit::WireMeasurement(hitcoord, tempcov, 999, 999,
nullptr));
204 genfit::SharedPlanePtr destPlane(
new genfit::DetPlane(pO, pU, pV));
206 if(!setInitialStateForExtrap(startPtID))
return -9999.;
208 genfit::AbsTrackRep* rep = _track->getCardinalRep();
209 double len = rep->extrapolateToPlane(*_propState, destPlane);
211 TVectorD hitcoord(2);
212 hitcoord[0] = pO.X();
213 hitcoord[1] = pO.Y();
214 TMatrixDSym tempcov(2);
215 tempcov.UnitMatrix();
217 genfit::PlanarMeasurement* pMeas =
new genfit::PlanarMeasurement(hitcoord, tempcov, 998, 998,
nullptr);
218 pMeas->setPlane(destPlane);
219 _virtMeas.reset(pMeas);
226 if(!setInitialStateForExtrap(startPtID))
return -9999.;
228 genfit::AbsTrackRep* rep = _track->getCardinalRep();
229 double len = rep->extrapolateToPoint(*_propState, point);
239 std::unique_ptr<const genfit::AbsHMatrix> H(_virtMeas->constructHMatrix(_propState->getRep()));
242 TVectorD stateVec(_propState->getState());
243 TMatrixDSym cov(_propState->getCov());
245 double ndf = meas.GetNrows();
247 TVectorD res(meas - H->Hv(stateVec));
248 TMatrixDSym covSumInv(cov);
251 genfit::tools::invertMatrix(covSumInv);
253 TMatrixD CHt(H->MHt(cov));
254 TVectorD update(TMatrixD(CHt, TMatrixD::kMult, covSumInv)*res);
257 covSumInv.Similarity(CHt);
259 _propState->setStateCov(stateVec, cov);
262 TVectorD resNew(meas - H->Hv(stateVec));
263 TMatrixDSym HCHt(cov);
267 genfit::tools::invertMatrix(HCHt);
268 chi2 = HCHt.Similarity(resNew);
273 bool GFTrack::setInitialStateForExtrap(
const int startPtID)
275 genfit::AbsTrackRep* rep = _track->getCardinalRep();
276 if(_track->getNumPoints() > 0)
278 genfit::TrackPoint* tp = _track->getPointWithMeasurementAndFitterInfo(startPtID, rep);
279 if(tp ==
nullptr)
return false;
281 genfit::KalmanFitterInfo* info =
static_cast<genfit::KalmanFitterInfo*
>(tp->getFitterInfo(rep));
282 _propState.reset(
new genfit::MeasuredStateOnPlane(info->getFittedState()));
286 _propState.reset(
new genfit::MeasuredStateOnPlane(_fitstates[startPtID]));
295 TVector3 pU(1., 0., 0.);
296 TVector3 pV(0., 1., 0.);
297 TVector3 pO(0., 0., z );
299 TVectorD beamCenter(2);
300 beamCenter[0] = X_BEAM; beamCenter[1] = Y_BEAM;
301 TMatrixDSym beamCov(2);
303 beamCov(0, 0) = SIGX_BEAM*SIGX_BEAM; beamCov(1, 1) = SIGY_BEAM*SIGY_BEAM;
309 if(fabs(len) > 6000.)
throw len;
316 else if(mom !=
nullptr)
321 catch(genfit::Exception& e)
323 std::cerr << __FILE__ <<
" " << __LINE__ <<
": hypo test failed vertex @Z=" << z <<
": " << e.what() << std::endl;
328 std::cerr << __FILE__ <<
" " << __LINE__ <<
": hypo test failed vertex @Z=" << z <<
": " << len << std::endl;
337 _trkcand = &tracklet;
338 _pdg = tracklet.
getCharge() > 0 ? -13 : 13;
339 _trkrep =
new genfit::RKTrackRep(_pdg);
341 TVectorD seed_state(6);
345 seed_state[0] = seed_pos.X();
346 seed_state[1] = seed_pos.Y();
347 seed_state[2] = seed_pos.Z();
348 seed_state[3] = seed_mom.Px();
349 seed_state[4] = seed_mom.Py();
350 seed_state[5] = seed_mom.Pz();
352 TMatrixDSym seed_cov(6);
353 double uncertainty[6] = {10., 10., 10., 3., 3., 10.};
354 for(
int i = 0; i < 6; i++)
356 for(
int j = 0; j < 6; j++)
358 seed_cov[i][j] = uncertainty[i]*uncertainty[j];
366 _track =
new genfit::Track(_trkrep, seed_state, seed_cov);
368 _measurements.clear();
369 for(
auto iter = tracklet.
hits.begin(); iter != tracklet.
hits.end(); ++iter)
371 if(iter->hit.index < 0)
continue;
374 _measurements.push_back(meas);
384 if(!updateMeasurements)
return;
385 for(
auto iter = _measurements.begin(); iter != _measurements.end(); ++iter)
387 (*iter)->postFitUpdate();
397 for(
auto iter = _measurements.begin(); iter != _measurements.end(); ++iter)
401 const genfit::MeasuredStateOnPlane& fitstate = (*iter)->getTrackPoint()->getKalmanFitterInfo()->getFittedState(
true);
405 fitstate.getPosMom(pos, mom);
406 TMatrixD stateVec(5, 1);
408 stateVec[1][0] = mom.Px()/mom.Pz();
409 stateVec[2][0] = mom.Py()/mom.Pz();
410 stateVec[3][0] = pos.X();
411 stateVec[4][0] = pos.Y();
415 TMatrixD cov(fitstate.getCov());
418 strack.
insertChisq((*iter)->getTrackPoint()->getKalmanFitterInfo()->getSmoothedChi2());
425 TVector3 pO(0., 0., Z_UPSTREAM);
426 TVector3 pU(1., 0., 0.);
427 TVector3 pV(0., 1., 0.);
428 TVectorD beamCenter(2);
429 beamCenter[0] = X_BEAM; beamCenter[1] = Y_BEAM;
430 TMatrixDSym beamCov(2);
432 beamCov(0, 0) = SIGX_BEAM*SIGX_BEAM; beamCov(1, 1) = SIGY_BEAM*SIGY_BEAM;
472 std::cout <<
"=============== SGTrack ===============" << std::endl;
473 std::cout <<
"------------ Track candidate ----------" << std::endl;
476 std::cout <<
"------------ Fit status ---------------" << std::endl;
477 _track->getFitStatus(_trkrep)->Print();
479 std::cout <<
"------------ Fit Result ---------------" << std::endl;
480 _track->getFittedState().Print();
482 if(debugLvl < 1)
return;
483 for(
auto iter = _measurements.begin(); iter != _measurements.end(); ++iter)
485 (*iter)->print(debugLvl-1);
488 if(debugLvl < 20)
return;
489 std::cout <<
"------------ GenFit Track ---------------" << std::endl;
void insertHitIndex(Int_t index)
void insertChisq(Double_t chisq)
void setTrackPtr(GFTrack *trackPtr)
double updatePropState(const TVectorD &meas, const TMatrixDSym &V)
double swimToVertex(double z, TVector3 *pos=nullptr, TVector3 *mom=nullptr, TMatrixDSym *cov=nullptr)
double extrapolateToPoint(TVector3 &point, bool update=false, const int startPtID=0)
void setTracklet(Tracklet &tracklet, double z_reference=590., bool wildseedcov=false)
void setChisqUpstream(Double_t chisq)
void setChisqDump(Double_t chisq)
virtual double get_DoubleFlag(const std::string &name) const
void insertStateVector(TMatrixD state)
TVector3 getGFPlaneO(Int_t i)
void setChisqVertex(Double_t chisq)
TMatrixDSym getGFCov(Int_t i)
void setKalmanStatus(Int_t status)
void postFitUpdate(bool updateMeasurements=true)
static recoConsts * instance()
int getNearestMeasurementID(GFMeasurement *meas)
double getExpPositionY(double z) const
void print(unsigned int debugLvl=0)
void insertCovariance(TMatrixD covar)
void print(std::ostream &os=std::cout)
double extrapolateToPlane(TVector3 &pO, TVector3 &pU, TVector3 &pV, const int startPtID=0)
void getExtrapPosMomCov(TVector3 &pos, TVector3 &mom, TMatrixDSym &cov)
void addMeasurement(GFMeasurement *measurement)
void setVerbosity(unsigned int v)
std::list< SignedHit > hits
void getExtrapPosMom(TVector3 &pos, TVector3 &mom)
void addMeasurements(std::vector< GFMeasurement * > &measurements)
TVectorD getGFState(Int_t i)
TVector3 getPositionVecSt1() const
TVector3 getMomentumVecSt1() const
TVectorD getGFAuxInfo(Int_t i)
TVector3 getExpMomentum(double z)
void insertGFState(const genfit::MeasuredStateOnPlane &msop)
TVector3 getGFPlaneV(Int_t i)
void setChisqTarget(Double_t chisq)
double extrapolateToLine(TVector3 &endPoint1, TVector3 &endPoint2, const int startPtID=0)
Int_t getCharge() const
Gets.
double getExpPositionX(double z) const
TVector3 getGFPlaneU(Int_t i)
void setChisq(Double_t chisq)
Sets.
void swimToVertex(TVector3 *pos=nullptr, TVector3 *mom=nullptr, bool hyptest=true)
Simple swim to vertex.