88 {
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 bool permitFlips = _allowFlips;
106 bool lPickHits = _allowDrops;
107
108
109 int i;
111 int lPicked = 0;
112
113 register double chisqold;
114 double chisqnew, chichange;
115 double chitest = 0.01;
117 int nActive = 0;
118
119
120
121
122
123
124
125
126
127 setLastChisq(-1.);
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 int nhits = hitlist.nHit();
150 std::vector<double> delChi(
nhits,0);
151 std::vector<std::vector<double> > deriv(
nhits);
152
154
155 int npar = theTraj.
nPar();
156
157
158
159
160 bool l3d = (npar > 3);
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()));
173 ideriv->resize(npar);
174 if (ihit->isActive()) {
175 nActive++;
180 nXY++;
181 }
182 }
183
184
185
186
187
188 }
189 if (nXY < minXY ||
nZ < minZ || nActive < minAct) {
190 status.setFailure(11,"Not enough hits in TrkHelixFitter! ");
191 return status;
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207 HepVector derivs(npar);
208
210
211 size_t itermax = 12;
213 bool mustIterate(false);
214 chisqold = 0.0;
215 for (i = 0; i < npar; i++) diffsum[i] = 0.0;
216 vparam *= 0.0;
217
218
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()));
223
224
226 double deltaChiNew;
229 calcResult = ihit->getFitStuff(derivs, deltaChiNew);
230 for (i=0; i<npar; ++i) (*ideriv)[i] = derivs[i];
231
232
233
234
235
236
237 } else {
238 calcResult = ihit->getFitStuff(deltaChiNew);
239 }
240 }
243 cout<<"ErrMsg(warning) TrkHelixFitter:"
244 << "unable to getFitStuff for hit " << *ihit << endl;
245 }
246 ihit->setUsability(false);
247 continue;
248 }
249 mustIterate = (mustIterate || (calcResult.
success() != 1));
250 *idelChi = deltaChiNew;
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 }
274
275
276
277 int ierr;
278 vparam.invert(ierr);
279 if (ierr) {
281 cout<<"ErrMsg(warning) TrkHelixFitter:"
282 << "Matrix inversion failed " << endl;
283 }
284 status.setFailure(12, "Matrix inversion failed in TrkHelixFitter");
285
286 }
287 delpar = vparam * (-diffsum);
289 cout << " delpar = "<<delpar << endl;
290 }
291
292
293 if (fabs(delpar[1]) > 1.) {
295 cout<<"ErrMsg(warning) TrkHelixFitter:"
296 << "Pathological fit " << endl;
297 }
298 status.setFailure(13, "Pathological fit in TrkHelixFitter.");
299
300 }
301
302 for (i = 0; i < npar; ++i) params.
parameter()[i] += delpar[i];
304 cout <<
" params "<<params.
parameter() << endl;
305 }
306
307
308
309
310
311 chisqnew = 0.0;
312 lPicked = 0;
313 double bigDelChi = 0.0;
315
316 mustIterate = (mustIterate || (
iter <= 2 && lPickHits));
317 ideriv = deriv.begin();
318 idelChi = delChi.begin();
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
328 for (i = 0; i < npar; i++) {
329 *idelChi += (*ideriv)[i] * delpar[i];
330 }
331 if (ihit->isActive()) chisqnew += *idelChi * *idelChi;
332
333
334 if (!mustIterate && lPickHits) {
335 double abDelChi = fabs(*idelChi);
336
337 if (abDelChi <=
nSigmaCut[ihit->layerNumber()] ) {
339 std::cout<< "abDelChi "<<abDelChi
340 <<
"<?"<<
nSigmaCut[ihit->layerNumber()] << std::endl;
341 }
342 if (ihit->isActive() == 0) {
343 ihit->setActivity(1);
344 if(
m_debug){ cout <<
"set ACTIVE, Added " << endl; }
345 lPicked = 1;
346 nActive++;
351 nXY++;
352 }
353 }
354 } else {
355 if (ihit->isActive()) {
356 if (abDelChi > bigDelChi) {
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 }
366 }
367
368
369 if (lPickHits) {
370 int lDrop = 0;
371 if (bigHit != hitlist.end() && (nActive > minAct)) {
373 nXY--;
374 lDrop = 1;
377 lDrop = 1;
379 nXY > minXY) {
381 nXY--;
382 lDrop = 1;
383 }
384 if (lDrop == 1) {
385 lPicked = 1;
386 nActive--;
387 bigHit->setActivity(0);
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 }
397
398
399 chichange = chisqold - chisqnew;
401 cout << "chisq from "<<chisqold << " -> " << chisqnew << endl;
402 }
403 if (chichange < -0.5 && !mustIterate && lPicked == 0) {
405 cout<<"ErrMsg(warning)" << " blowing up: " << chichange << endl;
406 }
407
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
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 }
427
428
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465 return status;
466}
void print(std::ostream &ostr) const
static double nSigmaCut[43]
TrkErrCode updateMeasurement(TrkHitOnTrk &hot, const TrkDifTraj *traj=0, bool maintainAmbiguity=false) const
HepSymMatrix & covariance()