436{
437
438
439 int i, j, k, ik, ij, ist, nderlc, ndergl, ndf;
440 int ja = -1;
441 int jb = 0;
442 int nagbn = 0;
443
444 double rmeas, wght, rms, cutval;
445
446 double summ = 0.0;
447 int nsum = 0;
448 nst = 0;
449 nst = arest.size();
450 if(debug_mode) cout<<"Start FitLoc, nst = "<<nst<<endl;
451
452
453
454
455 if (itert < 2 && single_fit != 1)
456 {
457 if (debug_mode) cout <<
"Store equation no: " <<
n << endl;
458
459 for (i=0; i<nst; i++)
460 {
461 storeind.push_back(indst[i]);
462 storeare.push_back(arest[i]);
463 storenl.push_back(arenl[i]);
464
465 if (arenl[i] != 0.) arest[i] = 0.0;
466 }
467
468 arenl.clear();
469
470 storeplace.push_back(storeind.size());
471
472 if (verbose_mode) cout <<
"StorePlace size = " << storeplace[
n] << endl;
473 if (verbose_mode) cout << "StoreInd size = " << storeind.size() << endl;
474 }
475
476
477 for (i=0; i<nalc; i++)
478 {
479 blvec[i] = 0.0;
480
481 for (j=0; j<nalc; j++) {clmat[i][j] = 0.0;}
482 }
483
484 for (i=0; i<nagb; i++) {indnz[i] = -1;}
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515 ist = 0;
516 indst.push_back(-1);
517
518 while (ist <= nst)
519 {
520 if (indst[ist] == -1)
521 {
522 if (ja == -1) {ja = ist;}
523 else if (jb == 0) {jb = ist;}
524 else
525 {
526 rmeas = arest[ja];
527 wght = arest[jb];
528 if (verbose_mode) cout << "rmeas = " << rmeas << endl ;
529 if (verbose_mode) cout << "wght = " << wght << endl ;
530
531 for (i=(jb+1); i<ist; i++)
532
533 {
534 j = indst[i];
535 if (verbose_mode) cout << "dparm[" << j << "] = " << dparm[j] << endl;
536 if (verbose_mode) cout << "Starting misalignment = " << pparm[j] << endl;
537 rmeas -= arest[i]*(pparm[j]+dparm[j]);
538 }
539
540 if (verbose_mode) cout << "rmeas after global stuff removal = " << rmeas << endl ;
541
542 for (i=(ja+1); i<jb; i++)
543 {
544 j = indst[i];
545
546 blvec[j] += wght*rmeas*arest[i];
547
548 if (verbose_mode) cout << "blvec[" << j << "] = " << blvec[j] << endl ;
549
550 for (k=(ja+1); k<=i ; k++)
551 {
552 ik = indst[k];
553
554 clmat[j][ik] += wght*arest[i]*arest[k];
555
556 if (verbose_mode) cout << "clmat[" << j << "][" << ik << "] = " << clmat[j][ik] << endl;
557 }
558 }
559 ja = -1;
560 jb = 0;
561 ist--;
562 }
563 }
564 ist++;
565 }
566
567
568
569
570
571
572 nrank = 0;
573 nrank = Millepede::SpmInv(clmat, blvec, nalc, scdiag, scflag);
574
575 if (debug_mode) cout << "" << endl;
576 if (debug_mode) cout << " __________________________________________________" << endl;
577 if (debug_mode) cout << " Printout of local fit (FITLOC) with rank= "<< nrank << endl;
578 if (debug_mode) cout << " Result of local fit : (index/parameter/error)" << endl;
579
580 for (i=0; i<nalc; i++)
581 {
582 if (debug_mode) cout << std::setprecision(4) << std::fixed;
583 if (debug_mode) cout << std::setw(20) << i << " / " << std::setw(10)
584 << blvec[i] << " / " << sqrt(clmat[i][i]) << endl;
585 }
586
587
588
589
590 for (i=0; i<nalc; i++)
591 {
592 track_params[2*i] = blvec[i];
593 track_params[2*i+1] = sqrt(fabs(clmat[i][i]));
594 }
595
596
597
598
599
600
601 ist = 0;
602 ja = -1;
603 jb = 0;
604
605 while (ist <= nst)
606 {
607 if (verbose_mode) cout<<"ist , nst = "<<ist<<", "<<nst<<endl;
608 if (indst[ist] == -1)
609 {
610 if (ja == -1) {ja = ist;}
611 else if (jb == 0) {jb = ist;}
612 else
613 {
614 rmeas = arest[ja];
615 wght = arest[jb];
616
617 nderlc = jb-ja-1;
618 ndergl = ist-jb-1;
619
620
621
622 if (verbose_mode) cout << "" << endl;
623 if (verbose_mode) cout << std::setprecision(4) << std::fixed;
624 if (verbose_mode) cout << ". equation: measured value " << std::setw(13)
625 << rmeas << " +/- " << std::setw(13) << 1.0/sqrt(wght) << endl;
626 if (verbose_mode) cout << "Number of derivatives (global, local): "
627 << ndergl << ", " << nderlc << endl;
628 if (verbose_mode) cout << "Global derivatives are: (index/derivative/parvalue) " << endl;
629
630 for (i=(jb+1); i<ist; i++)
631 {if (verbose_mode) cout << indst[i] << " / " << arest[i]
632 << " / " << pparm[indst[i]] << endl;}
633
634 if (verbose_mode) cout << "Local derivatives are: (index/derivative) " << endl;
635
636 for (i=(ja+1); i<jb; i++) {if (verbose_mode) cout << indst[i] << " / " << arest[i] << endl;}
637
638
639
640 for (i=(ja+1); i<jb; i++)
641 {
642 j = indst[i];
643 rmeas -= arest[i]*blvec[j];
644 }
645
646 for (i=(jb+1); i<ist; i++)
647 {
648 j = indst[i];
649 rmeas -= arest[i]*(pparm[j]+dparm[j]);
650 }
651
652
653
654 if (verbose_reject) cout << "Residual value : "<< rmeas << endl;
655 if (verbose_reject) cout << "Residual cut init value : "<< m_residual_cut_init << endl;
656 if (verbose_reject) cout << "Residual cut value : "<< m_residual_cut << endl;
657
658
659 if (fabs(rmeas) >= m_residual_cut_init && itert <= 1)
660 {
661
662 if (verbose_reject) cout << "Rejected track !!!!!" << endl;
663 locrej++;
664 indst.clear();
665 arest.clear();
666 return false;
667 }
668
669 if (fabs(rmeas) >= m_residual_cut && itert > 1)
670 {
671
672 if (verbose_reject) cout << "Rejected track !!!!!" << endl;
673 locrej++;
674 indst.clear();
675 arest.clear();
676 return false;
677 }
678
679 if(debug_mode) cout<<"chisq , wght, rmeas = "<<summ<<", "<<wght<<", "<<rmeas<<endl;
680 summ += wght*rmeas*rmeas ;
681 nsum++;
682 ja = -1;
683 jb = 0;
684 ist--;
685 }
686 }
687 ist++;
688 }
689
690 ndf = nsum-nrank;
691 if (debug_mode) cout<<"nsum, nrank = "<<nsum<<", "<<nrank<<endl;
692 rms = 0.0;
693
694 if (debug_mode) cout << "Final chi square / degrees of freedom "<< summ << " / " << ndf << endl;
695
696
697
698
699
700 if (ndf > 0) rms = summ/float(ndf);
701
702 if (single_fit == 0) loctot++;
703
704 if (nstdev != 0 && ndf > 0 && single_fit != 1)
705 {
706 cutval = Millepede::chindl(nstdev, ndf)*cfactr;
707 if (debug_mode) cout<<"nstdev, ndf, cfactr, cutval = "<<nstdev<<", "<<ndf<<", "<<cfactr<<", "<<cutval<<endl;
708
709 if (debug_mode) cout << "Reject if Chisq/Ndf = " << rms << " > " << cutval << endl;
710
711 if (rms > cutval)
712 {
713 if (verbose_mode) cout << "Rejected track !!!!!" << endl;
714 if (single_fit == 0) locrej++;
715 indst.clear();
716 arest.clear();
717 return false;
718 }
719 }
720
721 if (single_fit == 1)
722 {
723 indst.clear();
724 arest.clear();
725 return true;
726 }
727
728
729
730
731
732 if(record_mill)
733 {
734 ofstream fmill(
"mill.csv",ios::app);
735
736 fmill<<itert<<
", "<<
n <<
", "<< summ <<
", " << ndf <<
", "<<nsum<<
", "<<cutval<<
", "<< blvec[0]<<
", "<<blvec[1]<<
", "<<blvec[3]<<
", "<<blvec[4]<<
", "<<dparm[0]<<
", "<<dparm[3]<<
", "<<dparm[6]<<
", "<<dparm[9]<< endl;
737 fmill.close();
738 }
739
740 ist = 0;
741 ja = -1;
742 jb = 0;
743
744 while (ist <= nst)
745 {
746 if (indst[ist] == -1)
747 {
748 if (ja == -1) {ja = ist;}
749 else if (jb == 0) {jb = ist;}
750 else
751 {
752 rmeas = arest[ja];
753 wght = arest[jb];
754
755 for (i=(jb+1); i<ist; i++)
756 {
757 j = indst[i];
758 rmeas -= arest[i]*(pparm[j]+dparm[j]);
759 }
760
761
762
763 for (i=(jb+1); i<ist; i++)
764 {
765 j = indst[i];
766
767 bgvec[j] += wght*rmeas*arest[i];
768 if (verbose_mode) cout << "bgvec[" << j << "] = " << bgvec[j] << endl ;
769
770 for (k=(jb+1); k<ist ; k++)
771 {
772 ik = indst[k];
773 cgmat[j][ik] += wght*arest[i]*arest[k];
774 if (verbose_mode) cout << "cgmat[" << j << "][" << ik << "] = " << cgmat[j][ik] << endl;
775 }
776 }
777
778
779
780 for (i=(jb+1); i<ist; i++)
781 {
782 j = indst[i];
783 ik = indnz[j];
784
785 if (ik == -1)
786 {
787 for (k=0; k<nalc; k++) {clcmat[nagbn][k] = 0.0;}
788
789 indnz[j] = nagbn;
790 indbk[nagbn] = j;
791 ik = nagbn;
792 nagbn++;
793 }
794
795 for (k=(ja+1); k<jb ; k++)
796 {
797 ij = indst[k];
798 clcmat[ik][ij] += wght*arest[i]*arest[k];
799 if (verbose_mode) cout << "clcmat[" << ik << "][" << ij << "] = " << clcmat[ik][ij] << endl;
800 }
801 }
802 ja = -1;
803 jb = 0;
804 ist--;
805 }
806 }
807 ist++;
808 }
809
810
811
812 Millepede::SpAVAt(clmat, clcmat, corrm, nalc, nagbn);
813 Millepede::SpAX(clcmat, blvec, corrv, nalc, nagbn);
814
815 for (i=0; i<nagbn; i++)
816 {
817 j = indbk[i];
818 bgvec[j] -= corrv[i];
819
820 for (k=0; k<nagbn; k++)
821 {
822 ik = indbk[k];
823 cgmat[j][ik] -= corrm[i][k];
824 }
825 }
826
827 indst.clear();
828 arest.clear();
829
830 return true;
831}