18#include "TrkFitter/TrkHelixFitter.h"
19#include "TrkBase/TrkSimpTraj.h"
20#include "TrkBase/TrkParams.h"
21#include "TrkBase/TrkHitOnTrk.h"
22#include "CLHEP/Matrix/Vector.h"
23#include "CLHEP/Geometry/Point3D.h"
24#include "CLHEP/Matrix/Matrix.h"
25#include "CLHEP/Matrix/SymMatrix.h"
26#include "TrkBase/TrkEnums.h"
27#include "TrkBase/TrkErrCode.h"
28#include "TrkBase/TrkHotList.h"
29#include "CgemData/CgemHitOnTrack.h"
38 10.,5.,5.,10., 10.,5.,5.,10.,
39 10.,5.,5.,5., 5.,5.,5.,5., 5.,5.,5.,10.,
40 10.,5.,5.,5., 5.,5.,5.,5., 5.,5.,5.,5., 5.,5.,5.,10.,
41 10.,5.,5.,5., 5.,5.,10.
52 _allowFlips = allowFlips;
53 _allowDrops = allowDrops;
62 _allowFlips = right._allowFlips;
63 _allowDrops = right._allowDrops;
72 if (&right ==
this)
return *
this;
73 _allowFlips = right._allowFlips;
74 _allowDrops = right._allowDrops;
75 _lastChisq = right._lastChisq;
84 _allowFlips = allowFlips;
85 _allowDrops = allowDrops;
108 bool permitFlips = _allowFlips;
109 bool lPickHits = _allowDrops;
116 register double chisqold;
117 double chisqnew, chichange;
118 double chitest = 0.01;
156 int nhits = hitlist.
nHit();
157 std::vector<double> delChi(nhits,0);
158 std::vector<double> XdelChi(ncluster,0);
159 std::vector<double> VdelChi(ncluster,0);
160 std::vector<std::vector<double> > deriv(nhits);
161 std::vector<std::vector<double> > Xderiv(ncluster);
162 std::vector<std::vector<double> > Vderiv(ncluster);
166 int npar = theTraj.
nPar();
171 bool l3d = (npar > 3);
172 const int minZ = l3d ? 2 : 0;
173 const int minXY = npar - minZ;
174 const int minAct = minZ + minXY;
176 HepSymMatrix vparam(npar,0);
177 HepVector diffsum(npar);
178 HepVector delpar(npar);
180 std::vector<std::vector<double> >::iterator ideriv = deriv.begin();
181 std::vector<std::vector<double> >::iterator iXderiv = Xderiv.begin();
182 std::vector<std::vector<double> >::iterator iVderiv = Vderiv.begin();
183 std::vector<double>::iterator idelChi = delChi.begin();
184 std::vector<double>::iterator iXdelChi = XdelChi.begin();
185 std::vector<double>::iterator iVdelChi = VdelChi.begin();
186 assert(((
int)deriv.size()) ==(hitlist.
end()-hitlist.
begin()));
188 ideriv->resize(npar);
189 if (ihit->isActive()) {
200 iXderiv->resize(npar);
201 iVderiv->resize(npar);
210 if (nXY < minXY ||
nZ < minZ || nActive < minAct) {
211 status.setFailure(11,
"Not enough hits in TrkHelixFitter! ");
227 HepVector derivs(npar);
228 HepVector Xderivs(npar);
229 HepVector Vderivs(npar);
236 bool mustIterate(
false);
238 for (i = 0; i < npar; i++) diffsum[i] = 0.0;
242 std::vector<std::vector<double> >::iterator ideriv = deriv.begin();
243 std::vector<std::vector<double> >::iterator iXderiv = Xderiv.begin();
244 std::vector<std::vector<double> >::iterator iVderiv = Vderiv.begin();
245 std::vector<double>::iterator idelChi = delChi.begin();
246 std::vector<double>::iterator iXdelChi = XdelChi.begin();
247 std::vector<double>::iterator iVdelChi = VdelChi.begin();
248 assert(((
int)deriv.size())==(hitlist.
end()-hitlist.
begin()));
260 calcResult = cgemHot->
getFitStuff(Xderivs, XdeltaChiNew, Vderivs, VdeltaChiNew);
261 for (i=0; i<npar; ++i){
262 (*iXderiv)[i] = Xderivs[i];
263 (*iVderiv)[i] = Vderivs[i];
266 calcResult = ihit->getFitStuff(derivs, deltaChiNew);
267 for (i=0; i<npar; ++i) (*ideriv)[i] = derivs[i];
279 calcResult = cgemHot->
getFitStuff(XdeltaChiNew, VdeltaChiNew);
281 calcResult = ihit->getFitStuff(deltaChiNew);
287 cout<<
"ErrMsg(warning) TrkHelixFitter:"
288 <<
"unable to getFitStuff for hit " << *ihit << endl;
290 ihit->setUsability(
false);
299 mustIterate = (mustIterate || (calcResult.
success() != 1));
301 *iXdelChi = XdeltaChiNew;
302 *iVdelChi = VdeltaChiNew;
304 *idelChi = deltaChiNew;
308 cout << (ihit-hitlist.
begin());
309 ihit->
print(std::cout);
310 cout <<
" dChi " << *idelChi
311 <<
" amb " << ihit->ambig()
312 <<
" resid " << ihit->resid()
313 <<
" rms " << ihit->hitRms()
314 <<
" hitlen " << ihit->hitLen()
315 <<
" fltlen " << ihit->fltLen() << endl;
318 if (ihit->isActive() ==
false) {
319 if(
m_debug) std::cout<<
"SKIP not active hit"<< std::endl;
329 chisqold += XdeltaChiNew * XdeltaChiNew;
330 chisqold += VdeltaChiNew * VdeltaChiNew;
331 for (i = 0; i < npar; ++i) {
332 diffsum[i] += (*iXderiv)[i] * XdeltaChiNew;
333 diffsum[i] += (*iVderiv)[i] * VdeltaChiNew;
334 for (
int j = 0; j < i+1; ++j) {
335 vparam.fast(i+1,j+1) += (*iXderiv)[i] * (*iXderiv)[j];
336 vparam.fast(i+1,j+1) += (*iVderiv)[i] * (*iVderiv)[j];
340 chisqold += deltaChiNew * deltaChiNew;
341 for (i = 0; i < npar; ++i) {
342 diffsum[i] += (*ideriv)[i] * deltaChiNew;
343 for (
int j = 0; j < i+1; ++j) {
344 vparam.fast(i+1,j+1) += (*ideriv)[i] * (*ideriv)[j];
356 cout<<ihit->layerNumber()<<
" "<<setw(12)<<XdeltaChiNew<<
" || "<<setw(12)<<(Xderivs)[0]<<
" "<<setw(12)<<(Xderivs)[1]<<
" "<<setw(12)<<(Xderivs)[2]<<
" "<<setw(12)<<(Xderivs)[3]<<
" "<<setw(12)<<(Xderivs)[4]<<endl;
357 cout<<ihit->layerNumber()<<
" "<<setw(12)<<VdeltaChiNew<<
" || "<<setw(12)<<(Vderivs)[0]<<
" "<<setw(12)<<(Vderivs)[1]<<
" "<<setw(12)<<(Vderivs)[2]<<
" "<<setw(12)<<(Vderivs)[3]<<
" "<<setw(12)<<(Vderivs)[4]<<endl;
358 }
else cout<<ihit->layerNumber()<<
" "<<setw(12)<<deltaChiNew<<
" || "<<setw(12)<<(*ideriv)[0]<<
" "<<setw(12)<<(*ideriv)[1]<<
" "<<setw(12)<<(*ideriv)[2]<<
" "<<setw(12)<<(*ideriv)[3]<<
" "<<setw(12)<<(*ideriv)[4]<<endl;
368 cout<<
"ErrMsg(warning) TrkHelixFitter:"
369 <<
"Matrix inversion failed " << endl;
371 status.setFailure(12,
"Matrix inversion failed in TrkHelixFitter");
374 delpar = vparam * (-diffsum);
376 cout <<
" delpar = "<<delpar << endl;
380 if (fabs(delpar[1]) > 1.) {
382 cout<<
"ErrMsg(warning) TrkHelixFitter:"
383 <<
"Pathological fit " << endl;
385 status.setFailure(13,
"Pathological fit in TrkHelixFitter.");
389 for (i = 0; i < npar; ++i) params.
parameter()[i] += delpar[i];
391 cout <<
" params "<<params.
parameter() << endl;
400 double bigDelChi = 0.0;
403 mustIterate = (mustIterate || (
iter <= 2 && lPickHits));
404 ideriv = deriv.begin();
405 iXderiv = Xderiv.begin();
406 iVderiv = Vderiv.begin();
407 idelChi = delChi.begin();
408 iXdelChi = XdelChi.begin();
409 iVdelChi = VdelChi.begin();
412 ihit->print(std::cout);
414 if(!ihit->isUsable()){
415 if(
m_debug) { std::cout<<
"hit NOT usable "<< std::endl; }
426 for (i = 0; i < npar; i++) {
427 *iXdelChi += (*iXderiv)[i] * delpar[i];
428 *iVdelChi += (*iVderiv)[i] * delpar[i];
430 if (ihit->isActive()){
431 chisqnew += *iXdelChi * *iXdelChi;
432 chisqnew += *iVdelChi * *iVdelChi;
435 for (i = 0; i < npar; i++) {
436 *idelChi += (*ideriv)[i] * delpar[i];
438 if (ihit->isActive()) chisqnew += *idelChi * *idelChi;
441 if (!mustIterate && lPickHits) {
443 if(
typeid(*ihit)==
typeid(
CgemHitOnTrack)) abDelChi = fabs(*iXdelChi + *iVdelChi)/2;
444 else abDelChi = fabs(*idelChi);
446 if (abDelChi <=
nSigmaCut[ihit->layerNumber()] ) {
448 std::cout<<
"abDelChi "<<abDelChi
449 <<
"<?"<<
nSigmaCut[ihit->layerNumber()] << std::endl;
451 if (ihit->isActive() == 0) {
452 ihit->setActivity(1);
453 if(
m_debug){ cout <<
"set ACTIVE, Added " << endl; }
464 if (ihit->isActive()) {
465 if (abDelChi > bigDelChi) {
467 std::cout<<
"bigest set INACTIVE, abDelChi = "<<abDelChi
468 <<
">"<<
nSigmaCut[ihit->layerNumber()] <<
" bigDelChi=" <<bigDelChi<< std::endl; }
469 bigDelChi = abDelChi;
486 if (bigHit != hitlist.
end() && (nActive > minAct)) {
502 bigHit->setActivity(0);
504 std::cout<<
"---deactivate hit!! delChi2="<<bigDelChi<< std::endl;
506 bigHit->print(std::cout);
507 std::cout<<
"--------------------!! "<< std::endl;
514 chichange = chisqold - chisqnew;
516 cout <<
"chisq from "<<chisqold <<
" -> " << chisqnew << endl;
518 if (chichange < -0.5 && !mustIterate && lPicked == 0) {
520 cout<<
"ErrMsg(warning)" <<
" blowing up: " << chichange << endl;
523 setLastChisq(chisqnew);
524 status.setFailure(1);
526 if(
m_debug) std::cout<<
"failure 1 "<< std::endl;
528 }
else if (chichange < chitest && !mustIterate && lPicked ==0){
530 status.setSuccess(1);
531 setLastChisq(chisqnew);
532 if(
m_debug) std::cout<<
"success 1 "<< std::endl;
536 if (
iter == itermax) {
537 setLastChisq(chisqnew);
538 status.setSuccess(2);
539 if(
m_debug) std::cout<<
"success 2 "<< std::endl;
TrkErrCode getFitStuff(HepVector &derivs, HepVector &derivs2, double &sigma1, double &sigma2, double &deltaChi1, double &deltaChi2) const
void print(std::ostream &ostr) const
virtual ~TrkHelixFitter()
static double nSigmaCut[43]
TrkHelixFitter & operator=(const TrkHelixFitter &right)
TrkErrCode fit(TrkHotList &hitList, TrkSimpTraj &)
TrkHelixFitter(bool allowFlips=false, bool allowDrops=false)
void setFittingPar(bool allowFlips, bool allowDrops)
TrkErrCode updateMeasurement(TrkHitOnTrk &hot, const TrkDifTraj *traj=0, bool maintainAmbiguity=false) const
hot_iterator begin() const
virtual int nHit(TrkEnums::TrkViewInfo view=TrkEnums::bothView) const =0
HepSymMatrix & covariance()