29 #include "Alignment/CocoaVisMgr/interface/ALIVRMLMgr.h"
30 #include "Alignment/IgCocoaFileWriter/interface/IgCocoaFileMgr.h"
123 std::cout <<
"@@@ Fit::startFit ended n events = " <<
nEvent << std::endl;
149 std::vector<OpticalObject*>::iterator voite;
151 (*voite)->resetOriginalOriginalCoordinates();
155 std::vector<Entry*>::iterator veite;
157 (*veite)->resetValueDisplacementByFitting();
177 std::cout << std::endl <<
"@@@@@@@@@@@@@@@@@@ Starting data set fit : " <<
nEvent << std::endl;
184 fileout << std::endl <<
"@@@@@@@ NEW MEASUREMENT SET " <<
nEvent << std::endl;
196 double daFactor = 1.;
219 std::cout << std::endl <<
"@@@@ Fit quality: distance SMALLER than mininum " << std::endl;
232 std::cout << std::endl <<
"@@@@ Fit quality: distance BIGGER than mininum " << std::endl;
249 fileout <<
"!!!! WARNING: Too many iterations " <<
theNoFitIterations <<
" and fit DOES NOT CONVERGE "
259 std::cerr <<
"!! WARNING: fit quality has worsened, Recalculate fit quality with decreasing values of Da "
269 fileout <<
" Redoing iteration with Da factor " << daFactor << std::endl;
273 std::cerr <<
" !!!ERROR: not possible to get good fit quality even multiplying Da by " << daFactor
277 fileout <<
" !!!ERROR: not possible to get good fit quality even multiplying Da by " << daFactor
317 std::cout << std::endl <<
"@@@@@@@@@@@@@@@@@@ Fit has ended : only one measurement " <<
nEvent << std::endl;
325 std::cout << std::endl <<
"@@@@@@@@@@@@@@@@@@ Fit has ended : 'Measurement::only1' is set" << std::endl;
334 <<
"@@@@@@@@@@@@@@@@@@ Fit has ended : 'Number of events exhausted " <<
nEvent << std::endl;
343 std::cout << std::endl <<
"@@@@@@@@@@@@@@@@@@ Fit has ended : ??no more data sets' " <<
nEvent << std::endl;
348 std::cout << std::endl <<
"@@@@@@@@@@@@@@@@@@ Fit has ended : " <<
nEvent << std::endl;
356 if (gomgr->GlobalOptions()[
"VisOnly"] == 1) {
363 ALIVRMLMgr::getInstance().writeFile();
367 IgCocoaFileMgr::getInstance().writeFile();
372 std::cout <<
" Visualiation file(s) succesfully written. Ending.... " << std::endl;
383 std::vector<Entry*>::const_iterator vecite;
390 int No_entry_to_fit = 0;
394 (*vecite)->setFitPos(No_entry_to_fit);
396 std::cout <<
" Entry To Fit= " << No_entry_to_fit <<
" " << (*vecite)->OptOCurrent()->name() <<
" "
397 << (*vecite)->name() <<
" with quality= " << (*vecite)->quality() << std::endl;
409 std::cout <<
"@@@ Fit::fitParameters: Fit quality daFactor " << daFactor << std::endl;
419 std::cout <<
"@!! STOPPED by user after 1st iteration " << std::endl;
444 std::cout <<
"@@@ Fit::redoMatrices" << std::endl;
459 std::cout <<
"@@@ Fit::PropagateErrors" << std::endl;
514 std::cout <<
"ENDING after derivatives are calculated ('onlyDeriv' option set)" << std::endl;
539 std::cout <<
"@@@ Fit::calculateSimulatedMeasurementsWithOriginalValues" << std::endl;
541 std::vector<Measurement*>::const_iterator vmcite;
544 (*vmcite)->calculateOriginalSimulatedValue();
573 std::vector<Measurement*>::const_iterator vmcite;
575 NoMeas += (*vmcite)->dim();
578 std::cout <<
"NOMEAS" << NoMeas << std::endl;
588 std::cout <<
" Count number of 'cal'ibrated parameters " << std::endl;
589 std::vector<Entry*>::iterator veite;
591 if ((*veite)->quality() == 1)
595 std::cout << (*veite)->quality() <<
" " << (*veite)->OptOCurrent()->name() <<
" " << (*veite)->name()
596 <<
" # ENT CAL " << nEnt_cal <<
" # ENT " << noent << std::endl;
603 std::vector<Entry*>::const_iterator vecite;
608 std::cout << (*vecite)->quality() << (*vecite)->OptOCurrent()->name() << (*vecite)->name() <<
"NoParamFit"
609 << NoParamFit << std::endl;
615 ALIint NoLinesA = NoMeas + nEnt_cal;
616 ALIint NoColumnsA = NoParamFit;
619 ALIint NoLinesW = NoLinesA;
620 ALIint NoColumnsW = NoLinesA;
623 ALIint NoLinesY = NoLinesA;
630 std::cout <<
"CreateMatrices: NoLinesA = " << NoLinesA <<
" NoColumnsA = " << NoColumnsA << std::endl;
640 std::cout <<
"@@@ Fit::FillMatricesWithMeasurements" << std::endl;
645 std::vector<Measurement*>::const_iterator vmcite;
646 std::vector<Entry*>::const_iterator vecite;
649 std::cout <<
"FillMatricesWithMeasurements: measurement " << (*vmcite)->name() <<
" # entries affecting "
650 << (*vmcite)->affectingEntryList().size() << std::endl;
653 ALIint measdim = (*vmcite)->dim();
654 std::vector<ALIdouble> derivRE;
660 for (vecite = (*vmcite)->affectingEntryList().begin(); vecite != (*vmcite)->affectingEntryList().end(); ++vecite) {
665 std::cout <<
"entry affecting: " << (*vecite)->OptOCurrent()->name() <<
" " << (*vecite)->name() << std::endl;
667 derivRE = (*vmcite)->DerivativeRespectEntry(*vecite);
672 std::cout <<
"FillMatricesWithMeasurements: AMATRIX (" << Aline +
jj <<
"," << (*vecite)->fitPos() <<
" = "
673 << derivRE[
jj] << std::endl;
675 (*vmcite)->setValueSimulated(
jj, (*vmcite)->valueSimulated_orig(
jj));
704 std::cout <<
"change line" << Aline << std::endl;
707 std::cout <<
"change line" << Aline << std::endl;
726 std::cout <<
"@@@ Fit::FillMatricesWithCalibratedParameters" << std::endl;
730 std::vector<Measurement*>::const_iterator vmcite;
732 NolinMes += (*vmcite)->dim();
735 std::cout <<
"@@FillMatricesWithCalibratedParameters" << std::endl;
737 std::vector<Entry*>::const_iterator vecite;
744 if ((*vecite)->quality() == 1) {
746 ALIint lineNo = NolinMes + nEntcal;
747 ALIint columnNo = (*vecite)->fitPos();
750 std::cout <<
"Fit::FillMatricesWithCalibratedParameters: AMatrix ( " << lineNo <<
" , " << columnNo
751 <<
") = " << 1. << std::endl;
756 std::cout <<
"Fit::FillMatricesWithCalibratedParameters: WMatrix ( " << lineNo <<
" , " << columnNo
757 <<
") = " << entsig * entsig << std::endl;
767 yfMatrix->
AddData(lineNo, 0, -(*vecite)->valueDisplacementByFitting());
788 std::cout <<
"@@@ Fit::setCorrelationsInWMatrix" << std::endl;
812 std::cout <<
"CHECKsetCorrelatiFPF " << fit_pos1 <<
" " << fit_pos2 << std::endl;
814 if (fit_pos1 >= 0 && fit_pos1 < pmsize && fit_pos2 >= 0 && fit_pos2 < pmsize) {
824 std::cout <<
"setCorrelatiFPF " << fit_pos1 <<
" " << fit_pos2 <<
" " << correl << std::endl;
832 std::cout <<
"@@@ Fit::multiplyMatrices " << std::endl;
878 std::vector<Entry*>::const_iterator vecite;
882 <<
" Optical Object "
886 <<
" Orig.Error" << std::endl;
896 std::cout << nEnt <<
"PARAM" << std::setw(26) << (*vecite)->OptOCurrent()->name().c_str() << std::setw(8) <<
" "
897 << (*vecite)->name().c_str() <<
" " << std::setw(8) <<
" " << (*vecite)->value() <<
" "
898 << std::setw(8) <<
sqrt(
AtWAMatrix->
Mat()->me[nEnt][nEnt]) / (*vecite)->OutputSigmaDimensionFactor()
899 <<
" " << (*vecite)->sigma() / (*vecite)->OutputSigmaDimensionFactor() <<
" Q" << (*vecite)->quality()
904 if ((*vecite)->quality() == 2)
917 std::cout <<
"@@@ Fit::getFitQuality" << std::endl;
919 double fit_quality =
GetSChi2(
true);
926 DatMatrix->
Dump(
"DatMatrix");
931 DSMattemp->
Dump(
"DSMattempMatrix=Dat*At*W");
933 DSMattemp2->
Dump(
"DSMattempMatrix2=At*W*yf");
935 DSMattemp3->
Dump(
"DSMattempMatrix3=At*W");
947 (*yfMatrix).Dump(
"yfMatrix");
949 DSMat->
Dump(
"DSMatrix final");
952 ALIdouble fit_quality_cut = (*DSMat)(0, 0);
957 <<
" Fit quality predicted improvement in distance to minimum is = " << fit_quality_cut << std::endl;
961 <<
" Fit quality predicted improvement in distance to minimum is = " << fit_quality_cut << std::endl;
979 if (fit_quality_cut < 0.) {
982 std::cerr <<
"!!WARNING: Fit quality has worsened: Fit Quality now = " << fit_quality <<
" before "
997 fileout <<
"STOP: SMALL IMPROVEMENT IN ITERATION " <<
theNoFitIterations <<
" = " << fit_quality_cut <<
" < "
1018 fileout <<
"CONTINUE: BIG IMPROVEMENT IN ITERATION " <<
theNoFitIterations <<
" = " << fit_quality_cut
1036 std::cout <<
"@@@ Fit::GetSChi2 useDa= " << useDa << std::endl;
1063 tmpM->
Dump(
"A*Da + f - y Matrix ");
1068 tmptM->
Dump(
"tmptM after transpose");
1077 SMat1->
Dump(
"(A*Da + f - y)^T * W Matrix");
1083 SMat->
Dump(
"SMatrix with Da");
1090 SMat->
Dump(
"SMatrix no Da");
1095 std::cout <<
" GetSChi2 = " << fit_quality << std::endl;
1107 std::cout <<
"@@ Adding correction (DaMatrix) to Entries " << std::endl;
1119 std::vector<Entry*>::const_iterator vecite;
1127 <<
" @@@ PENTRY change " << (*vecite)->OptOCurrent()->name() <<
" " << (*vecite)->name() <<
" "
1128 <<
" change= " << (*DaMatrix)(nEnt, 0) <<
" value= " << (*vecite)->valueDisplacementByFitting()
1142 std::cout <<
" old valueDisplacementByFitting " << (*vecite)->name() <<
" "
1143 << (*vecite)->valueDisplacementByFitting() <<
" original value " << (*vecite)->value() << std::endl;
1145 (*vecite)->addFittedDisplacementToValue((*
DaMatrix)(nEnt, 0));
1148 std::cout << nEnt <<
" new valueDisplacementByFitting " << (*vecite)->OptOCurrent()->name() <<
" "
1149 << (*vecite)->name() <<
" = " << (*vecite)->valueDisplacementByFitting() <<
" "
1150 << (*DaMatrix)(nEnt, 0) << std::endl;
1161 std::cout <<
"@@ Fit::substractToHalfDaMatrixToEntries " << std::endl;
1164 std::vector<Entry*>::const_iterator vecite;
1169 ALIdouble lastadd = (*vecite)->lastAdditionToValueDisplacementByFitting() *
factor;
1171 (*vecite)->addFittedDisplacementToValue(-lastadd);
1172 (*vecite)->setLastAdditionToValueDisplacementByFitting(-(*vecite)->lastAdditionToValueDisplacementByFitting());
1176 std::cout <<
" new valueDisplacementByFitting " << (*vecite)->OptOCurrent()->name() <<
" " << (*vecite)->name()
1177 <<
" = " << (*vecite)->valueDisplacementByFitting() <<
" " << std::endl;
1189 <<
" Optical Object "
1192 <<
" Orig.Value" << std::endl;
1196 std::cout << std::endl <<
"FITTED VALUES " << std::endl;
1197 fileout << std::endl
1198 <<
"FITTED VALUES " << std::endl
1200 <<
" Optical Object"
1203 fileout <<
" value (+-error)"
1204 <<
" orig.val (+-error)";
1206 fileout <<
" value "
1209 fileout <<
" quality" << std::endl;
1212 std::vector<Entry*> entries;
1215 std::vector<OpticalObject*>::const_iterator vocite;
1217 if ((*vocite)->type() ==
ALIstring(
"system"))
1220 fileout <<
" %%%% Optical Object: " << (*vocite)->longName() << std::endl;
1222 entries = (*vocite)->CoordinateEntryList();
1223 siz = entries.size();
1225 std::cerr <<
"!!! FATAL ERROR: strange number of coordinates = " << siz << std::endl;
1238 entries = (*vocite)->ExtraEntryList();
1239 siz = entries.size();
1240 for (
ii = 0;
ii < siz;
ii++) {
1253 fileout << std::endl
1254 <<
"@@@@ FITTED VALUES IN ALL ANCESTORS " << std::endl
1256 <<
" Optical Object"
1259 fileout <<
" value (+-error)";
1261 fileout <<
" orig.val (+-error)";
1264 fileout <<
" value ";
1266 fileout <<
" orig.val ";
1269 fileout <<
" quality" << std::endl;
1272 std::vector<Entry*> entries;
1273 std::vector<OpticalObject*>::const_iterator vocite;
1275 if ((*vocite)->type() ==
ALIstring(
"system"))
1278 fileout <<
" %%%% Optical Object: " << (*vocite)->longName() << std::endl;
1280 entries = (*vocite)->CoordinateEntryList();
1286 fileout <<
" %% IN FRAME : " << optoParent->
longName() << std::endl;
1291 optoParent = optoParent->
parent();
1292 }
while (optoParent);
1302 CLHEP::Hep3Vector centreLocal;
1303 if (optoAncestor->
type() ==
"system") {
1307 CLHEP::HepRotation parentRmGlobInv = inverseOf(optoAncestor->
rmGlob());
1308 centreLocal = parentRmGlobInv * centreLocal;
1312 <<
" parent GLOB " << optoAncestor->
centreGlob() << std::endl;
1327 fileout, entries[
ii], centreLocal[
ii] / entries[
ii]->OutputValueDimensionFactor(), printErrors, printOrig);
1342 fileout, entries[
ii], entryvalues[
ii - 3] / entries[
ii]->OutputValueDimensionFactor(), printErrors, printOrig);
1349 if (
entry->type() ==
"angles") {
1351 std::cout <<
"WARNING valueDisplacementByFitting has no sense for angles " << std::endl;
1360 entryvalue = (
entry->value() +
entry->valueDisplacementByFitting()) /
entry->OutputValueDimensionFactor();
1372 std::cout <<
"ENTRY: " << std::setw(30) <<
entry->OptOCurrent()->name() << std::setw(8) <<
" " <<
entry->name()
1373 <<
" " << std::setw(8) << (
entry->value() +
entry->valueDisplacementByFitting()) <<
" " <<
entry->value()
1374 <<
" Q" <<
entry->quality() << std::endl;
1377 if (
entry->quality() == 2) {
1378 fileout <<
"UNK: " <<
entry->fitPos() <<
" ";
1379 }
else if (
entry->quality() == 1) {
1380 fileout <<
"CAL: " <<
entry->fitPos() <<
" ";
1383 fileout <<
"FIX: -1 ";
1386 fileout << std::setw(30) <<
entry->OptOCurrent()->name() << std::setw(8) <<
" " <<
entry->name() <<
" "
1387 << std::setw(8) << std::setprecision(8) << entryvalue;
1393 fileout <<
" +- " << std::setw(8) << 0.;
1396 fileout << std::setw(8) <<
" " <<
entry->value() / dimv;
1398 fileout <<
" +- " << std::setw(8) <<
entry->sigma() / dims <<
" Q" <<
entry->quality();
1401 float dif = (
entry->value() +
entry->valueDisplacementByFitting()) / dimv -
entry->value() / dimv;
1404 fileout <<
" DIFF= " << dif;
1411 fileout << std::endl;
1419 fileout << std::endl
1420 <<
"CORRELATION BETWEEN 'unk' ENTRIES: (>= " << minCorrel <<
" )" << std::endl
1421 <<
"No_1 No_2 correlation " << std::endl;
1424 std::vector<Entry*>::iterator veite1, veite2;
1427 if ((*veite1)->quality() == 0) {
1429 }
else if ((*veite1)->quality() == 1) {
1431 }
else if ((*veite1)->quality() == 2) {
1434 i1 = (*veite1)->fitPos();
1437 i2 = (*veite2)->fitPos();
1438 if ((*veite2)->quality() == 0) {
1440 }
else if ((*veite2)->quality() == 1) {
1442 }
else if ((*veite2)->quality() == 2) {
1447 if (
std::abs(corrf) >= minCorrel) {
1449 std::cout <<
"CORR:" << E1 <<
"" << E2 <<
" (" <<
i1 <<
")"
1450 <<
" (" <<
i2 <<
")"
1451 <<
" " << corrf << std::endl;
1453 fileout <<
"CORR:" << E1 <<
"" << E2 <<
" (" <<
i1 <<
")"
1454 <<
" (" <<
i2 <<
")"
1455 <<
" " << corrf << std::endl;
1471 matout << std::endl <<
" @@@@@@@@@@@@@@@ Iteration No : " <<
theNoFitIterations << std::endl;
1492 std::vector<Entry*>::const_iterator vecite;
1495 if ((*vecite)->name() == entry_name) {
1497 fitposi = (*vecite)->fitPos();
1502 if ((*vecite)->name() == entry_name) {
1504 fitposi = (*vecite)->fitPos();
1508 if (fitposi == -99) {
1509 std::cerr <<
"!!EXITING: entry name not found: " << entry_name << std::endl;
1518 double chi2meas = 0;
1520 ALIint nMeas = 0, nUnk = 0;
1523 std::vector<Measurement*>::const_iterator vmcite;
1528 double c2 = ((*vmcite)->value(
ii) - (*vmcite)->valueSimulated(
ii)) / (*vmcite)->sigma(
ii);
1529 chi2meas += c2 * c2;
1531 std::cout << c2 <<
" adding chi2meas " << chi2meas <<
" " << (*vmcite)->name() <<
": " <<
ii
1532 <<
" (mm)R: " << (*vmcite)->value(
ii) * 1000. <<
" S: " << (*vmcite)->valueSimulated(
ii) * 1000.
1533 <<
" Diff= " << ((*vmcite)->value(
ii) - (*vmcite)->valueSimulated(
ii)) * 1000. << std::endl;
1539 std::vector<Entry*>::iterator veite;
1541 if ((*veite)->quality() == 2)
1543 if ((*veite)->quality() == 1) {
1544 double c2 = (*veite)->valueDisplacementByFitting() / (*veite)->sigma();
1548 std::cout << c2 <<
" adding chi2cal " << chi2cal <<
" " << (*veite)->OptOCurrent()->name() <<
" "
1549 << (*veite)->name() << std::endl;
1556 fileout <<
" Chi2= " << chi2meas + chi2cal <<
" / " << nMeas - nUnk <<
" dof "
1557 <<
" From measurements= " << chi2meas <<
" from calibrated parameters= " << chi2cal << std::endl;
1560 std::cout <<
" quality Chi2 (no correlations) " << chi2meas + chi2cal <<
" " << chi2meas <<
" " << chi2cal
1572 fileout << std::endl <<
"Fit iteration " <<
theNoFitIterations <<
" ..." << std::endl;
1583 fileout <<
theNoFitIterations <<
" Chi2 after iteration = " << fit_quality << std::endl;
1590 std::cout <<
"@@@ Fit::CheckIfFitPossible" << std::endl;
1594 std::vector<Measurement*>::const_iterator vmcite;
1596 NolinMes += (*vmcite)->dim();
1599 std::vector<Entry*>::const_iterator vecite;
1602 std::cout <<
"Fit::CheckIfFitPossible looping for entry " << (*vecite)->longName() << std::endl;
1603 if ((*vecite)->quality() == 2) {
1604 ALIint nCol = (*vecite)->fitPos();
1608 std::cout <<
"Fit::CheckIfFitPossible looping for entry " << nCol << std::endl;
1611 std::cout <<
" Derivative= (" <<
ii <<
"," << nCol <<
") = " << (*AMatrix)(
ii, nCol) << std::endl;
1615 std::cout <<
"Fit::CheckIfFitIsPossible " << nCol <<
" " <<
ii <<
" = " << (*AMatrix)(
ii, nCol)
1623 <<
"!!!FATAL ERROR: Fit::CheckIfFitPossible() no measurement depends on unknown entry "
1624 << (*vecite)->OptOCurrent()->name() <<
"/" << (*vecite)->name() << std::endl
1625 <<
"!!! Fit will not be possible! " << std::endl;
1632 std::vector<Entry*>::const_iterator vecite1, vecite2;
1637 if ((*vecite1)->quality() == 2) {
1641 if ((*vecite2)->quality() == 2) {
1642 ALIint fitpos1 = (*vecite1)->fitPos();
1643 ALIint fitpos2 = (*vecite2)->fitPos();
1645 std::cout <<
"Fit::CheckIfFitIsPossible checking " << (*vecite1)->longName() <<
" ( " << fitpos1 <<
" ) & "
1646 << (*vecite2)->longName() <<
" ( " << fitpos2 <<
" ) " << std::endl;
1651 std::cout <<
"Fit::CheckIfFitIsPossible " <<
ii <<
" : " << (*AMatrix)(
ii, fitpos1)
1652 <<
" ?= " << (*
AMatrix)(
ii, fitpos2) << std::endl;
1660 if (prop != DBL_MAX && prop != propn) {
1668 std::cerr <<
"!!!FATAL ERROR: Fit::CheckIfFitPossible() two entries for which the measurements have the "
1669 "same dependency (or 100% proportional) "
1670 << (*vecite1)->longName() <<
" & " << (*vecite2)->longName() << std::endl
1671 <<
"!!! Fit will not be possible! " << std::endl;
1684 std::set<ALIuint> columnsEqual;
1685 std::set<ALIuint> columnsEqualSave;
1693 columnsEqualSave.insert(
ii);
1699 if (
jj ==
int(measNo))
1701 columnsEqual = columnsEqualSave;
1704 div = (*AMatrix)(measNo,
ii) / (*
AMatrix)(measNo, biggestColumn);
1708 std::cout <<
"CheckIfMeasIsProportionalToAnother 2 columns = " <<
ii <<
" in " << measNo <<
" & " <<
jj
1712 std::cout <<
"CheckIfMeasIsProportionalToAnother 2 columns != " <<
ii <<
" in " << measNo <<
" & " <<
jj
1715 std::set<ALIuint>::iterator ite = columnsEqual.find(
ii);
1716 if (ite != columnsEqual.end()) {
1717 columnsEqual.erase(ite);
1722 if (!columnsEqual.empty()) {
1724 std::cout <<
"CheckIfMeasIsProportionalToAnother " << measNo <<
" = " <<
jj << std::endl;
1737 std::cout <<
" imeas " << imeas << std::endl;
1739 std::vector<Measurement*>::const_iterator vmcite;
1742 if (Aline == imeas) {
1745 return ((*vmcite)->name()) +
":" +
std::string(ctmp);
1751 std::cout <<
" return measname " << measname << std::endl;