BOSS 7.0.6
BESIII Offline Software System
Loading...
Searching...
No Matches
TrkHelixFitter Class Reference

#include <TrkHelixFitter.h>

+ Inheritance diagram for TrkHelixFitter:

Public Member Functions

 TrkHelixFitter (bool allowFlips=false, bool allowDrops=false)
 
virtual ~TrkHelixFitter ()
 
TrkHelixFitteroperator= (const TrkHelixFitter &right)
 
 TrkHelixFitter (const TrkHelixFitter &)
 
TrkErrCode fit (TrkHotList &hitList, TrkSimpTraj &)
 
void setFittingPar (bool allowFlips, bool allowDrops)
 
double lastChisq () const
 
- Public Member Functions inherited from TrkHitOnTrkUpdater
virtual ~TrkHitOnTrkUpdater ()=0
 

Static Public Attributes

static bool m_debug = false
 
static double nSigmaCut [43]
 

Additional Inherited Members

- Protected Member Functions inherited from TrkHitOnTrkUpdater
TrkErrCode updateMeasurement (TrkHitOnTrk &hot, const TrkDifTraj *traj=0, bool maintainAmbiguity=false) const
 
void setActivity (TrkHitOnTrk &hot, bool active) const
 
void setParent (TrkHitOnTrk &hot, TrkRep *parent) const
 
TrkBase::Functors::updateMeasurement updateMeasurement (const TrkDifTraj *traj=0, bool maintainAmbiguity=false) const
 
TrkBase::Functors::setParent setParent (TrkRep *parent) const
 
TrkBase::Functors::setActive setActive (bool active) const
 

Detailed Description

Definition at line 29 of file TrkHelixFitter.h.

Constructor & Destructor Documentation

◆ TrkHelixFitter() [1/2]

TrkHelixFitter::TrkHelixFitter ( bool  allowFlips = false,
bool  allowDrops = false 
)

Definition at line 45 of file TrkHelixFitter.cxx.

45 :
46 //------------------------------------------------------------------------
48{
49 _allowFlips = allowFlips;
50 _allowDrops = allowDrops;
51 _lastChisq = -1.;
52}

◆ ~TrkHelixFitter()

TrkHelixFitter::~TrkHelixFitter ( )
virtual

Definition at line 41 of file TrkHelixFitter.cxx.

41{}

◆ TrkHelixFitter() [2/2]

TrkHelixFitter::TrkHelixFitter ( const TrkHelixFitter right)

Definition at line 55 of file TrkHelixFitter.cxx.

55 :
56 //------------------------------------------------------------------------
58{
59 _allowFlips = right._allowFlips;
60 _allowDrops = right._allowDrops;
61 _lastChisq = -1.;
62}

Member Function Documentation

◆ fit()

TrkErrCode TrkHelixFitter::fit ( TrkHotList hitList,
TrkSimpTraj theTraj 
)

Definition at line 87 of file TrkHelixFitter.cxx.

88 {
89 //------------------------------------------------------------------------
90 // Assumes that weight matrix is diagonal. */
91 /* Least-squares fit; the measured
92 quantity is the residual. The fit is accomplished by linearizing
93 the equation, using the derivatives of the residual w/r/t the
94 track parameters; because of this approximation, the fit may be iterated.
95 The fitted parameters are given by:
96 delta-param() = Vparam * Atran * Vyinv * delChi
97 where Vyinv = covariance matrix for the measurements
98 Atran = transpose of A
99 A = matrix of derivatives of delChi wrt trk params
100 (size = no. of params x no. of hits)
101 Vparam = covariance (error)" matrix of the fitted parameters
102 = (Atran * Vyinv * A)**-1
103 */
104
105 bool permitFlips = _allowFlips;
106 bool lPickHits = _allowDrops;
107 // permitFlips = 1 => permit state changes like ambiguity flips
108 // lPickHits = 1 => choose the best set of active hits on each iteration
109 int i;
111 int lPicked = 0; // = 1 => have either picked up or dropped an active hit
112 // on this iteration
113 register double chisqold;
114 double chisqnew, chichange;
115 double chitest = 0.01; //delta(chi2) < chitest => fit has converged
116 int nZ = 0, nXY = 0; // # active hits in each view
117 int nActive = 0;
118
119 // vparam = Vparam defined above ( = symmetric matrix)
120 // diffsum = Atran * Vyinv * delChi defined above (column vector)
121 // iter = iteration loop index
122 // itermax = max number of iterations
123 // delpar = change in parameters during this iteration
124 // chisqold, chisqnew = chisq before and after latest iteration
125
126 /***************************************************************************/
127 setLastChisq(-1.);
128 //bool shiftRef = false;//yzhang FIXME
129 // HepPoint3D storePoint;
130
131 // Change reference point of trajectory to be at first hit -- reduces
132 // numerical problems
133 // double oldT0 = hitlist[0]->parentTrack()->trackT0();
134 //if (shiftRef) {
135 // double firstFlight = hitlist[0]->fltLen();
136 // double newTime = hitlist[0]->parentTrack()->fitResult()->arrivalTime(firstFlight);
137 // hitlist[0]->parentTrack()->resetT0(newTime);
138 // Point3D.here = theTraj.position(firstFlight);
139 //
140 // storePoint = here;
141 // DifPoint dfPos;
142 // DifVector dfDir, dfDelDir;
143 // theTraj.getDFInfo(firstFlight, dfPos, dfDir, dfDelDir);
144 //
145 // theTraj.changePoint(here, fltOffset);
146 //}
147
148 //*** Things that don't change with each iteration
149 int nhits = hitlist.nHit();
150 std::vector<double> delChi(nhits,0);
151 std::vector<std::vector<double> > deriv(nhits);
152
153 TrkParams &params = *(theTraj.parameters());
154 // int npar = params.nPar();//yzhang temp
155 int npar = theTraj.nPar();//yzhang temp
156
157 // Decide minimum numbers of hits required. This could turn out to be wrong
158 // someday.
159
160 bool l3d = (npar > 3); // I hope always true
161 const int minZ = l3d ? 2 : 0;
162 const int minXY = npar - minZ;
163 const int minAct = minZ + minXY;
164
165 HepSymMatrix vparam(npar,0);
166 HepVector diffsum(npar);
167 HepVector delpar(npar);
168
169 std::vector<std::vector<double> >::iterator ideriv = deriv.begin();
170 std::vector<double>::iterator idelChi = delChi.begin();
171 assert(((int)deriv.size()) ==(hitlist.end()-hitlist.begin()));
172 for (TrkHotList::nc_hot_iterator ihit = hitlist.begin(); ihit != hitlist.end(); ++ihit,++ideriv,++idelChi) {
173 ideriv->resize(npar);
174 if (ihit->isActive()) {
175 nActive++;
176 if (ihit->whatView() == TrkEnums::xyView) nXY++;
177 else if (ihit->whatView() == TrkEnums::zView) nZ++;
178 else if (ihit->whatView() == TrkEnums::bothView) {
179 nZ++;
180 nXY++;
181 }
182 }
183
184 // Update the Hots to reflect new reference point
185 //if (shiftRef) {
186 // ihit->setFltLen( ihit->fltLen() - fltOffset );
187 //}
188 } //end loop over hits
189 if (nXY < minXY || nZ < minZ || nActive < minAct) {
190 status.setFailure(11,"Not enough hits in TrkHelixFitter! ");
191 return status;
192 }
193
194
195 //if (shiftRef) {
196 // double firstFlight = hitlist[0]->fltLen();
197 // Point3D.here = theTraj.position(firstFlight);
198 // DifPoint dfPos;
199 // DifVector dfDir, dfDelDir;
200 // theTraj.getDFInfo(firstFlight, dfPos, dfDir, dfDelDir);
201 // double dummy = 0.;
202 // hitlist[0]->updateFitStuff(dummy, 0, !permitFlips);
203 // hitlist[0]->updateMeasurement();
204 // hitlist[0]->updateFitStuff(dummy, 0, !permitFlips);
205 //}
206 // static HepVector derivs(npar);//yzhang temp
207 HepVector derivs(npar);//zhang change
208 // HepVector derivs(npar);//yzhang temp
209 TrkErrCode calcResult;
210 //**** Iterate fit.
211 size_t itermax = 12;
212 for (size_t iter = 1; iter <= itermax; iter++) {
213 bool mustIterate(false); // flag to force another iteration
214 chisqold = 0.0;
215 for (i = 0; i < npar; i++) diffsum[i] = 0.0;
216 vparam *= 0.0; // dumb way of clearing matrix
217
218 /* Loop over hits, accumulate sums, calculate chisq for current params. */
219 std::vector<std::vector<double> >::iterator ideriv = deriv.begin();
220 std::vector<double>::iterator idelChi = delChi.begin();
221 assert(((int)deriv.size())==(hitlist.end()-hitlist.begin()));
222 for (TrkHotList::nc_hot_iterator ihit = hitlist.begin(); ihit != hitlist.end(); ++ihit,++ideriv,++idelChi) {
223
224 // Ask the hit to do the calculations
225 calcResult = updateMeasurement(*ihit,0,!permitFlips);
226 double deltaChiNew;
227 if (calcResult.success()) {
228 if (iter < 2) { // FIXME? only update derivatives at first iteration...
229 calcResult = ihit->getFitStuff(derivs, deltaChiNew);
230 for (i=0; i<npar; ++i) (*ideriv)[i] = derivs[i];
231 //if(m_debug){
232 // std::cout<<"in TrkHelixFitter " <<std::endl;//yzhang deubg
233 // cout << "deriv: ";
234 // for (i=0;i<npar;++i) cout << (*ideriv)[i] << " " ;
235 // cout << endl;
236 //}
237 } else {
238 calcResult = ihit->getFitStuff(deltaChiNew);
239 }
240 }
241 if (calcResult.failure()) {
242 if(m_debug){
243 cout<<"ErrMsg(warning) TrkHelixFitter:"
244 << "unable to getFitStuff for hit " << *ihit << endl;
245 }
246 ihit->setUsability(false); // something bombed
247 continue;
248 }
249 mustIterate = (mustIterate || (calcResult.success() != 1));
250 *idelChi = deltaChiNew;
251 if(m_debug){
252 cout << (ihit-hitlist.begin());
253 ihit->print(std::cout);
254 cout << " dChi " << *idelChi
255 << " amb " << ihit->ambig()
256 << " resid " << ihit->resid()
257 << " rms " << ihit->hitRms()
258 << " hitlen " << ihit->hitLen()
259 << " fltlen " << ihit->fltLen() << endl;
260 }
261 if (ihit->isActive() == false) {
262 if(m_debug) std::cout<<"SKIP not active hit"<< std::endl;
263 continue;
264 }
265 chisqold += deltaChiNew * deltaChiNew;
266
267 for (i = 0; i < npar; ++i) {
268 diffsum[i] += (*ideriv)[i] * deltaChiNew;
269 for (int j = 0; j < i+1; ++j) {
270 vparam.fast(i+1,j+1) += (*ideriv)[i] * (*ideriv)[j];
271 }
272 }
273 } // end loop over hits
274
275
276 //**** Calculate new paramters
277 int ierr;
278 vparam.invert(ierr);
279 if (ierr) {
280 if(m_debug){
281 cout<<"ErrMsg(warning) TrkHelixFitter:"
282 << "Matrix inversion failed " << endl;
283 }
284 status.setFailure(12, "Matrix inversion failed in TrkHelixFitter");
285 //break;
286 }
287 delpar = vparam * (-diffsum);
288 if(m_debug){
289 cout << " delpar = "<<delpar << endl;
290 }
291 // The following test relies on having a fixed location for phi0 in
292 // all simple params; it should be made robust somehow!!!
293 if (fabs(delpar[1]) > 1.) {
294 if(m_debug){
295 cout<<"ErrMsg(warning) TrkHelixFitter:"
296 << "Pathological fit " << endl;
297 }
298 status.setFailure(13, "Pathological fit in TrkHelixFitter.");
299 //break;
300 }
301
302 for (i = 0; i < npar; ++i) params.parameter()[i] += delpar[i];
303 if(m_debug){
304 cout << " params "<<params.parameter() << endl;
305 }
306
307 //***** Loop through the hits again, calculating the approx change
308 // in residuals and chisq., and picking which hits should be active
309 // for next iteration.
310
311 chisqnew = 0.0;
312 lPicked = 0;
313 double bigDelChi = 0.0;
314 TrkHotList::nc_hot_iterator bigHit = hitlist.end();
315
316 mustIterate = (mustIterate || (iter <= 2 && lPickHits)); // iterate until hit-dropping allowed
317 ideriv = deriv.begin();
318 idelChi = delChi.begin();
319 for (TrkHotList::nc_hot_iterator ihit = hitlist.begin(); ihit != hitlist.end(); ++ihit,++ideriv,++idelChi) {
320 if(m_debug) {
321 ihit->print(std::cout);
322 }
323 if(!ihit->isUsable()){
324 if(m_debug) { std::cout<<"hit NOT usable "<< std::endl; }
325 continue;
326 }
327 //double weight = ihit->weight(); // FIXME: why isn't weight used???
328 for (i = 0; i < npar; i++) {
329 *idelChi += (*ideriv)[i] * delpar[i];
330 }
331 if (ihit->isActive()) chisqnew += *idelChi * *idelChi;
332
333 // Hit-picking
334 if (!mustIterate && lPickHits) {
335 double abDelChi = fabs(*idelChi);
336 //yzhang fix nSigmaCut for each layers 2010-04-13
337 if (abDelChi <= nSigmaCut[ihit->layerNumber()] ) {
338 if(m_debug){
339 std::cout<< "abDelChi "<<abDelChi
340 <<"<?"<<nSigmaCut[ihit->layerNumber()] << std::endl;//yzhang debug
341 }
342 if (ihit->isActive() == 0) {
343 ihit->setActivity(1); // reactivate hit
344 if(m_debug){ cout << "set ACTIVE, Added " << endl; }
345 lPicked = 1;
346 nActive++;
347 if (ihit->whatView() == TrkEnums::xyView) nXY++;
348 else if (ihit->whatView() == TrkEnums::zView) nZ++;
349 else if (ihit->whatView() == TrkEnums::bothView) {
350 nZ++;
351 nXY++;
352 }
353 }
354 } else {
355 if (ihit->isActive()) {
356 if (abDelChi > bigDelChi) {
357 if(m_debug){
358 std::cout<<"bigest set INACTIVE, abDelChi = "<<abDelChi
359 <<">"<<nSigmaCut[ihit->layerNumber()] <<" bigDelChi=" <<bigDelChi<< std::endl; }
360 bigDelChi = abDelChi;
361 bigHit = ihit;
362 }
363 }
364 }
365 } // end if iter > 2 (hit-picking)
366 } //end loop over hits
367
368 // Drop hit with worst residual
369 if (lPickHits) {
370 int lDrop = 0;
371 if (bigHit != hitlist.end() && (nActive > minAct)) {
372 if ( bigHit->whatView() == TrkEnums::xyView && nXY > minXY) {
373 nXY--;
374 lDrop = 1;
375 } else if ( bigHit->whatView() == TrkEnums::zView && nZ > minZ) {
376 nZ--;
377 lDrop = 1;
378 } else if ( bigHit->whatView() == TrkEnums::bothView && nZ > minZ &&
379 nXY > minXY) {
380 nZ--;
381 nXY--;
382 lDrop = 1;
383 }
384 if (lDrop == 1) {
385 lPicked = 1;
386 nActive--;
387 bigHit->setActivity(0); // deactivate hit
388 if(m_debug){
389 std::cout<<"---deactivate hit!! delChi2="<<bigDelChi<< std::endl;
390 std::cout<<"---";
391 bigHit->print(std::cout);
392 std::cout<<"--------------------!! "<< std::endl;
393 }
394 }
395 }
396 }// end if lPickHits
397
398 /* Test for convergence. */
399 chichange = chisqold - chisqnew;
400 if(m_debug){
401 cout << "chisq from "<<chisqold << " -> " << chisqnew << endl;
402 }
403 if (chichange < -0.5 && !mustIterate && lPicked == 0) {
404 if(m_debug){
405 cout<<"ErrMsg(warning)" << " blowing up: " << chichange << endl;
406 }
407 /* It's blowing up. */
408 setLastChisq(chisqnew);
409 status.setFailure(1);
410
411 if(m_debug) std::cout<<"failure 1 "<< std::endl;
412 break;
413 } else if (chichange < chitest && !mustIterate && lPicked ==0){
414 // We converged.
415 status.setSuccess(1);
416 setLastChisq(chisqnew);
417 if(m_debug) std::cout<<"success 1 "<< std::endl;
418 break;
419 }
420
421 if (iter == itermax) {
422 setLastChisq(chisqnew);
423 status.setSuccess(2);
424 if(m_debug) std::cout<<"success 2 "<< std::endl;
425 }
426 } /* end iteration loop */
427
428 // store the error matrix
429 params.covariance() = vparam;
430
431 // Attempt to calculate deltaChisq for this hit (compared to leaving hit
432 // out of the fit).
433 /* chisqnew = 0;
434 if (status.success()) {
435 HepVector deltaAlpha(npar);
436 for (ihit = 0; ihit < nhits; ihit++) {
437 thisHot = hitlist(ihit);
438 if (!thisHot->isActive()) continue;
439 HepVector derivs(npar, 0);
440 for (i = 0; i < npar; i++) {
441 derivs[i] = deriv[ihit][i];
442 }
443 double weight = thisHot->weight();
444 double resid = thisHot->resid();
445 deltaAlpha = vparam * derivs;
446 deltaAlpha *= (resid * weight);
447 // cout << resid * resid * weight
448 // << " " << resid * weight * dot(derivs, deltaAlpha) << endl;
449 // thisHot->chi2Contrib = -dot(deltaAlpha, temp) + resid * resid * weight
450 // + 2. * resid * weight * dot(derivs, deltaAlpha);
451 }
452 }
453 */
454
455 // Change reference point back to origin
456 //if (shiftRef) {
457 // Point3D.home(0.,0.,0.);
458 // theTraj.changePoint(home, fltOffset);
459 // hitlist[0]->parentTrack()->resetT0(oldT0);
460 //for (ihit = 0; ihit < nhits; ihit++) {
461 // thisHot = hitlist[ihit];
462 // thisHot->setFltLen( thisHot->fltLen() - fltOffset );
463 // }
464 //}
465 return status;
466}
const int nZ
EvtStreamInputIterator< typename Generator::result_type > iter(Generator gen, int N=0)
HepVector & parameter()
Definition: DifIndepPar.h:51
void print(std::ostream &ostr) const
Definition: TrkErrCode.cxx:79
int success() const
Definition: TrkErrCode.h:62
int failure() const
Definition: TrkErrCode.h:61
static bool m_debug
static double nSigmaCut[43]
TrkErrCode updateMeasurement(TrkHitOnTrk &hot, const TrkDifTraj *traj=0, bool maintainAmbiguity=false) const
HepSymMatrix & covariance()
Definition: TrkParams.h:54
TrkParams * parameters()
Definition: TrkSimpTraj.h:80
virtual int nPar() const
Definition: TrkSimpTraj.h:88
@ xyView
Definition: TrkEnums.h:22
@ zView
Definition: TrkEnums.h:22
@ bothView
Definition: TrkEnums.h:22
int nhits

Referenced by TrkSimpleRep::fit().

◆ lastChisq()

double TrkHelixFitter::lastChisq ( ) const
inline

Definition at line 39 of file TrkHelixFitter.h.

39{return _lastChisq;}

Referenced by TrkSimpleRep::fit().

◆ operator=()

TrkHelixFitter & TrkHelixFitter::operator= ( const TrkHelixFitter right)

Definition at line 66 of file TrkHelixFitter.cxx.

68{
69 if (&right == this) return *this;
70 _allowFlips = right._allowFlips;
71 _allowDrops = right._allowDrops;
72 _lastChisq = right._lastChisq;
73
74 return *this;
75}

◆ setFittingPar()

void TrkHelixFitter::setFittingPar ( bool  allowFlips,
bool  allowDrops 
)

Definition at line 79 of file TrkHelixFitter.cxx.

79 {
80 //------------------------------------------------------------------------
81 _allowFlips = allowFlips;
82 _allowDrops = allowDrops;
83}

Referenced by TrkHelixMaker::addZValues().

Member Data Documentation

◆ m_debug

bool TrkHelixFitter::m_debug = false
static

◆ nSigmaCut

double TrkHelixFitter::nSigmaCut
static
Initial value:
= {
10.,5.,5.,10., 10.,5.,5.,10.,
10.,5.,5.,5., 5.,5.,5.,5., 5.,5.,5.,10.,
10.,5.,5.,5., 5.,5.,5.,5., 5.,5.,5.,5., 5.,5.,5.,10.,
10.,5.,5.,5., 5.,5.,10.
}

Definition at line 41 of file TrkHelixFitter.h.

Referenced by fit(), MdcTrkRecon::initialize(), MdcxTrackFinder::initialize(), and MdcFlagHold::printPar().


The documentation for this class was generated from the following files: