387 {
388
389
390 int i, j, k, ik, ij, ist, nderlc, ndergl, ndf;
391 int ja = -1;
392 int jb = 0;
393 int nagbn = 0;
394
395 double rmeas, wght, rms, cutval;
396
397 double summ = 0.0;
398 int nsum = 0;
399 nst = 0;
400 nst = arest.size();
401
402
403
404 if ( itert < 2 && single_fit != 1 )
405 {
406 if (
debug_mode ) cout <<
"Store equation no: " <<
n << endl;
407
408 for ( i = 0; i < nst; i++ )
409 {
410 storeind.push_back( indst[i] );
411 storeare.push_back( arest[i] );
412 storenl.push_back( arenl[i] );
413
414 if ( arenl[i] != 0. )
415 arest[i] = 0.0;
416 }
417
418 arenl.clear();
419
420 storeplace.push_back( storeind.size() );
421
422 if (
verbose_mode ) cout <<
"StorePlace size = " << storeplace[
n] << endl;
423 if (
verbose_mode ) cout <<
"StoreInd size = " << storeind.size() << endl;
424 }
425
426 for ( i = 0; i < nalc; i++ )
427 {
428 blvec[i] = 0.0;
429
430 for ( j = 0; j < nalc; j++ ) { clmat[i][j] = 0.0; }
431 }
432
433 for ( i = 0; i < nagb; i++ ) { indnz[i] = -1; }
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 ist = 0;
463 indst.push_back( -1 );
464
465 while ( ist <= nst )
466 {
467 if ( indst[ist] == -1 )
468 {
469 if ( ja == -1 ) { ja = ist; }
470 else if ( jb == 0 ) { jb = ist; }
471 else
472 {
473 rmeas = arest[ja];
474 wght = arest[jb];
475 if (
verbose_mode ) cout <<
"rmeas = " << rmeas << endl;
477
478 for ( i = ( jb + 1 ); i < ist; i++ )
479
480 {
481 j = indst[i];
482 if (
verbose_mode ) cout <<
"dparm[" << j <<
"] = " << dparm[j] << endl;
483 if (
verbose_mode ) cout <<
"Starting misalignment = " << pparm[j] << endl;
484 rmeas -= arest[i] * ( pparm[j] + dparm[j] );
485 }
486
487 if (
verbose_mode ) cout <<
"rmeas after global stuff removal = " << rmeas << endl;
488
489 for ( i = ( ja + 1 ); i < jb; i++ )
490 {
491 j = indst[i];
492 blvec[j] += wght * rmeas * arest[i];
493
494 if (
verbose_mode ) cout <<
"blvec[" << j <<
"] = " << blvec[j] << endl;
495
496 for ( k = ( ja + 1 ); k <= i; k++ )
497 {
498 ik = indst[k];
499 clmat[j][ik] += wght * arest[i] * arest[k];
500
502 cout << "clmat[" << j << "][" << ik << "] = " << clmat[j][ik] << endl;
503 }
504 }
505 ja = -1;
506 jb = 0;
507 ist--;
508 }
509 }
510 ist++;
511 }
512
513
514
515
516
517 nrank = 0;
518 nrank = Millepede::SpmInv( clmat, blvec, nalc, scdiag, scflag );
519
521 if (
debug_mode ) cout <<
" __________________________________________________" << endl;
522 if (
debug_mode ) cout <<
" Printout of local fit (FITLOC) with rank= " << nrank << endl;
523 if (
debug_mode ) cout <<
" Result of local fit : (index/parameter/error)" << endl;
524
525 for ( i = 0; i < nalc; i++ )
526 {
527 if (
debug_mode ) cout << std::setprecision( 4 ) << std::fixed;
529 cout << std::setw( 20 ) << i << " / " << std::setw( 10 ) << blvec[i] << " / "
530 << sqrt( clmat[i][i] ) << endl;
531 }
532
533
534
535 for ( i = 0; i < nalc; i++ )
536 {
537 track_params[2 * i] = blvec[i];
538 track_params[2 * i + 1] = sqrt( fabs( clmat[i][i] ) );
539 }
540
541
542
543
544
545 ist = 0;
546 ja = -1;
547 jb = 0;
548
549 while ( ist <= nst )
550 {
551 if ( indst[ist] == -1 )
552 {
553 if ( ja == -1 ) { ja = ist; }
554 else if ( jb == 0 ) { jb = ist; }
555 else
556 {
557 rmeas = arest[ja];
558 wght = arest[jb];
559
560 nderlc = jb - ja - 1;
561 ndergl = ist - jb - 1;
562
563
564
566 if (
verbose_mode ) cout << std::setprecision( 4 ) << std::fixed;
568 cout << ". equation: measured value " << std::setw( 13 ) << rmeas << " +/- "
569 << std::setw( 13 ) << 1.0 / sqrt( wght ) << endl;
571 cout << "Number of derivatives (global, local): " << ndergl << ", " << nderlc
572 << endl;
574 cout << "Global derivatives are: (index/derivative/parvalue) " << endl;
575
576 for ( i = ( jb + 1 ); i < ist; i++ )
577 {
579 cout << indst[i] << " / " << arest[i] << " / " << pparm[indst[i]] << endl;
580 }
581
582 if (
verbose_mode ) cout <<
"Local derivatives are: (index/derivative) " << endl;
583
584 for ( i = ( ja + 1 ); i < jb; i++ )
585 {
586 if (
verbose_mode ) cout << indst[i] <<
" / " << arest[i] << endl;
587 }
588
589
590
591 for ( i = ( ja + 1 ); i < jb; i++ )
592 {
593 j = indst[i];
594 rmeas -= arest[i] * blvec[j];
595 }
596
597 for ( i = ( jb + 1 ); i < ist; i++ )
598 {
599 j = indst[i];
600 rmeas -= arest[i] * ( pparm[j] + dparm[j] );
601 }
602
603
604
605 if (
verbose_reject ) cout <<
"Residual value : " << rmeas << endl;
606
607
608 if ( fabs( rmeas ) >= m_residual_cut_init && itert <= 1 )
609 {
610
612 locrej++;
613 indst.clear();
614 arest.clear();
615 return false;
616 }
617
618 if ( fabs( rmeas ) >= m_residual_cut && itert > 1 )
619 {
620
622 locrej++;
623 indst.clear();
624 arest.clear();
625 return false;
626 }
627
628 summ += wght * rmeas * rmeas;
629 nsum++;
630 ja = -1;
631 jb = 0;
632 ist--;
633 }
634 }
635 ist++;
636 }
637
638 ndf = nsum - nrank;
639 rms = 0.0;
640
642 cout << "Final chi square / degrees of freedom " << summ << " / " << ndf << endl;
643
644 if ( ndf > 0 ) rms = summ / float( ndf );
645
646 if ( single_fit == 0 ) loctot++;
647
648 if ( nstdev != 0 && ndf > 0 && single_fit != 1 )
649 {
650 cutval = Millepede::chindl( nstdev, ndf ) * cfactr;
651
652 if (
debug_mode ) cout <<
"Reject if Chisq/Ndf = " << rms <<
" > " << cutval << endl;
653
654 if ( rms > cutval )
655 {
656 if (
verbose_mode ) cout <<
"Rejected track !!!!!" << endl;
657 if ( single_fit == 0 ) locrej++;
658 indst.clear();
659 arest.clear();
660 return false;
661 }
662 }
663
664 if ( single_fit == 1 )
665 {
666 indst.clear();
667 arest.clear();
668 return true;
669 }
670
671
672
673
674
675
676 ist = 0;
677 ja = -1;
678 jb = 0;
679
680 while ( ist <= nst )
681 {
682 if ( indst[ist] == -1 )
683 {
684 if ( ja == -1 ) { ja = ist; }
685 else if ( jb == 0 ) { jb = ist; }
686 else
687 {
688 rmeas = arest[ja];
689 wght = arest[jb];
690
691 for ( i = ( jb + 1 ); i < ist; i++ )
692 {
693 j = indst[i];
694 rmeas -= arest[i] * ( pparm[j] + dparm[j] );
695 }
696
697
698
699 for ( i = ( jb + 1 ); i < ist; i++ )
700 {
701 j = indst[i];
702
703 bgvec[j] += wght * rmeas * arest[i];
704 if (
verbose_mode ) cout <<
"bgvec[" << j <<
"] = " << bgvec[j] << endl;
705
706 for ( k = ( jb + 1 ); k < ist; k++ )
707 {
708 ik = indst[k];
709 cgmat[j][ik] += wght * arest[i] * arest[k];
711 cout << "cgmat[" << j << "][" << ik << "] = " << cgmat[j][ik] << endl;
712 }
713 }
714
715
716
717 for ( i = ( jb + 1 ); i < ist; i++ )
718 {
719 j = indst[i];
720 ik = indnz[j];
721
722 if ( ik == -1 )
723 {
724 for ( k = 0; k < nalc; k++ ) { clcmat[nagbn][k] = 0.0; }
725
726 indnz[j] = nagbn;
727 indbk[nagbn] = j;
728 ik = nagbn;
729 nagbn++;
730 }
731
732 for ( k = ( ja + 1 ); k < jb; k++ )
733 {
734 ij = indst[k];
735 clcmat[ik][ij] += wght * arest[i] * arest[k];
737 cout << "clcmat[" << ik << "][" << ij << "] = " << clcmat[ik][ij] << endl;
738 }
739 }
740 ja = -1;
741 jb = 0;
742 ist--;
743 }
744 }
745 ist++;
746 }
747
748
749
750 Millepede::SpAVAt( clmat, clcmat, corrm, nalc, nagbn );
751 Millepede::SpAX( clcmat, blvec, corrv, nalc, nagbn );
752
753 for ( i = 0; i < nagbn; i++ )
754 {
755 j = indbk[i];
756 bgvec[j] -= corrv[i];
757
758 for ( k = 0; k < nagbn; k++ )
759 {
760 ik = indbk[k];
761 cgmat[j][ik] -= corrm[i][k];
762 }
763 }
764
765 indst.clear();
766 arest.clear();
767
768 return true;
769}
const bool verbose_reject