00001 #include <FWCore/Utilities/interface/Exception.h>
00002
00003
00004
00005
00006
00007
00008
00009 #include "Alignment/CocoaModel/interface/Model.h"
00010 #include "Alignment/CocoaModel/interface/OpticalObject.h"
00011 #include "Alignment/CocoaFit/interface/Fit.h"
00012
00013 #include "Alignment/CocoaModel/interface/Measurement.h"
00014 #include "Alignment/CocoaModel/interface/Entry.h"
00015 #include "Alignment/CocoaUtilities/interface/ALIUtils.h"
00016 #include "Alignment/CocoaUtilities/interface/ALIFileOut.h"
00017 #include "Alignment/CocoaUtilities/interface/GlobalOptionMgr.h"
00018 #include "Alignment/CocoaModel/interface/DeviationsFromFileSensor2D.h"
00019 #include "Alignment/CocoaAnalysis/interface/NtupleManager.h"
00020 #include "Alignment/CocoaAnalysis/interface/FittedEntriesManager.h"
00021 #include "Alignment/CocoaAnalysis/interface/FittedEntriesSet.h"
00022 #ifdef COCOA_VIS
00023 #include "Alignment/CocoaVisMgr/interface/ALIVRMLMgr.h"
00024 #include "Alignment/IgCocoaFileWriter/interface/IgCocoaFileMgr.h"
00025 #endif
00026 #include "Alignment/CocoaModel/interface/OpticalObjectMgr.h"
00027 #include "Alignment/CocoaModel/interface/ErrorCorrelationMgr.h"
00028 #include "Alignment/CocoaModel/interface/ErrorCorrelation.h"
00029 #include "Alignment/CocoaModel/interface/FittedEntriesReader.h"
00030 #include "Alignment/CocoaDaq/interface/CocoaDaqReader.h"
00031 #include <stdlib.h>
00032 #include <iomanip>
00033 #include <math.h>
00034 #include <time.h>
00035 #include <set>
00036
00037 Fit* Fit::instance = 0;
00038
00039 ALIMatrix* Fit::AMatrix;
00040 ALIMatrix* Fit::AtMatrix;
00041 ALIMatrix* Fit::WMatrix;
00042 ALIMatrix* Fit::AtWAMatrix;
00043
00044 ALIMatrix* Fit::DaMatrix;
00045
00046
00047 ALIMatrix* Fit::yfMatrix;
00048
00049
00050 ALIint Fit::_NoLinesA;
00051 ALIint Fit::_NoColumnsA;
00052
00053
00054 ALIint Fit::theMinimumEntryQuality;
00055 ALIdouble Fit::thePreviousIterationFitQuality = DBL_MAX;
00056 ALIdouble Fit::theFitQualityCut = -1;
00057 ALIdouble Fit::theRelativeFitQualityCut = -1;
00058 ALIint Fit::theNoFitIterations;
00059 ALIint Fit::MaxNoFitIterations = -1;
00060 ALIdouble Fit::theMinDaFactor = 1.e-8;
00061
00062
00063
00064
00065 Fit& Fit::getInstance()
00066 {
00067 if(!instance) {
00068 instance = new Fit;
00069 ALIdouble go;
00070 GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00071
00072 gomgr->getGlobalOptionValue("maxDeviDerivative", go );
00073 ALIUtils::setMaximumDeviationDerivative( go );
00074 if( ALIUtils::debug >= 3 ) std::cout << " Fit::maximum_deviation_derivative " << ALIUtils::getMaximumDeviationDerivative() << std::endl;
00075
00076 gomgr->getGlobalOptionValue("maxNoFitIterations", go );
00077 MaxNoFitIterations = int(go);
00078
00079 gomgr->getGlobalOptionValue("fitQualityCut", go );
00080 theFitQualityCut = go;
00081 if( ALIUtils::debug >= 3 ) std::cout << " theFitQualityCut " << theFitQualityCut << std::endl;
00082
00083 gomgr->getGlobalOptionValue("RelativeFitQualityCut", go );
00084 theRelativeFitQualityCut = go;
00085 if( ALIUtils::debug >= 3 ) std::cout << " theRelativeFitQualityCut " << theRelativeFitQualityCut << std::endl;
00086
00087 gomgr->getGlobalOptionValue("minDaFactor", go );
00088 theMinDaFactor = go;
00089
00090 }
00091
00092 return *instance;
00093 }
00094
00095
00096
00097
00098
00099 void Fit::startFit()
00100 {
00101
00102
00103 NtupleManager* NTmgr = NtupleManager::getInstance();
00104 NTmgr->BookNtuple();
00105
00106 ALIuint nEvent = 0;
00107 ALIUtils::setFirstTime( 1 );
00108
00109 WriteVisualisationFiles();
00110
00111 for(;;) {
00112
00113 if( !fitNextEvent( nEvent ) ) break;
00114
00115
00116
00117 }
00118
00119
00120 GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00121 if(gomgr->GlobalOptions()["histograms"] > 0) {
00122 FittedEntriesManager* FEmgr = FittedEntriesManager::getInstance();
00123 FEmgr->MakeHistos();
00124 }
00125
00126 NTmgr->WriteNtuple();
00127
00128 }
00129
00130
00131
00132 ALIbool Fit::fitNextEvent( ALIuint& nEvent )
00133 {
00134 if( Model::getFittedEntriesReader() != 0 ) Model::getFittedEntriesReader()->readFittedEntriesFromFile();
00135
00136
00137 std::vector< OpticalObject* >::iterator voite;
00138 for( voite = Model::OptOList().begin(); voite != Model::OptOList().end(); voite++ ) {
00139 (*voite)->resetOriginalOriginalCoordinates();
00140 }
00141
00142
00143 std::vector< Entry* >::iterator veite;
00144 for( veite = Model::EntryList().begin(); veite != Model::EntryList().end(); veite++ ) {
00145 (*veite)->resetValueDisplacementByFitting();
00146 }
00147
00148
00149 ALIbool lastEvent = 0;
00150
00151
00152
00153
00154
00155
00156 ALIbool moreDataSets = 1;
00157 if(CocoaDaqReader::GetDaqReader() != 0) moreDataSets = CocoaDaqReader::GetDaqReader()->ReadNextEvent();
00158
00159 if(ALIUtils::debug >= 2) std::cout << CocoaDaqReader::GetDaqReader() << "$$$$$$$$$$$$$$$ moreData Sets " << moreDataSets << std::endl;
00160
00161 if( moreDataSets ) {
00162 if( ALIUtils::debug >= 2 ) std::cout << "@@@@@@@@@@@@@@@@@@ Starting data set fit ..." << nEvent << std::endl;
00163
00164
00165 setFittableEntries();
00166
00167
00168 ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
00169 fileout << std::endl << "@@@@@@@ NEW MEASUREMENT SET " << nEvent << std::endl;
00170 if( ALIUtils::report >= 1 ) ALIUtils::dumpDimensions( fileout );
00171
00172
00173 theNoFitIterations = 0;
00174
00175 GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00176 ALIdouble dumpMat;
00177 gomgr->getGlobalOptionValue("save_matrices", dumpMat );
00178
00179
00180 double daFactor = 1.;
00181 Model::setCocoaStatus(COCOA_FirstIterationInEvent );
00182 for(;; ){
00183
00184 calculateSimulatedMeasurementsWithOriginalValues();
00185
00186 FitQuality fq = fitParameters( daFactor );
00187 if( dumpMat > 1 ) dumpMatrices();
00188
00189
00190
00191
00192 if( fq == FQsmallDistanceToMinimum ) {
00193 addDaMatrixToEntries();
00194 if(ALIUtils::report >= 1) dumpFittedValues( ALIFileOut::getInstance( Model::ReportFName() ), TRUE, TRUE );
00195
00196 ALIdouble go;
00197 gomgr->getGlobalOptionValue("dumpInAllFrames", go );
00198 if(go >= 1) dumpFittedValuesInAllAncestorFrames( ALIFileOut::getInstance( Model::ReportFName() ), FALSE, FALSE );
00199
00200 break;
00201 } else if( fq == FQbigDistanceToMinimum ) {
00202 addDaMatrixToEntries();
00203 if(ALIUtils::report >= 1) dumpFittedValues( ALIFileOut::getInstance( Model::ReportFName() ), TRUE, TRUE );
00204
00205
00206 theNoFitIterations++;
00207 daFactor = 1.;
00208
00209
00210 if( theNoFitIterations >= MaxNoFitIterations ) {
00211 std::cerr << "!!!! WARNING: Too many iterations " << theNoFitIterations << " and fit DOES NOT CONVERGE " << std::endl;
00212
00213 if(ALIUtils::report >= 2) {
00214 ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
00215 fileout << "!!!! WARNING: Too many iterations " << theNoFitIterations << " and fit DOES NOT CONVERGE " << std::endl;
00216 }
00217
00218 break;
00219 }
00220
00221 } else if( fq == FQchiSquareWorsened ) {
00222
00223
00224 daFactor *= 0.5;
00225 if( daFactor > theMinDaFactor ){
00226 substractLastDisplacementToEntries( 0.5 );
00227
00228 if(ALIUtils::report >= 2) {
00229 ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
00230 fileout << " Redoing iteration with Da factor " << daFactor << std::endl;
00231 }
00232 } else {
00233 daFactor *= 2.;
00234 std::cerr << " !!!ERROR: not possible to get good fit quality even multiplying Da by " << daFactor << std::endl;
00235 if(ALIUtils::report >= 2) {
00236 ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
00237 fileout << " !!!ERROR: not possible to get good fit quality even multiplying Da by " << daFactor << std::endl;
00238 }
00239
00240
00241 break;
00242 }
00243 }
00244 Model::setCocoaStatus(COCOA_NextIterationInEvent );
00245
00246 }
00247
00248
00249 if(ALIUtils::debug >= 1) calculateSimulatedMeasurementsWithOriginalValues();
00250 if(gomgr->GlobalOptions()["histograms"] > 0) {
00251 FittedEntriesManager::getInstance()->AddFittedEntriesSet( new FittedEntriesSet( AtWAMatrix ) );
00252 }
00253
00254 NtupleManager* ntupleMgr = NtupleManager::getInstance();
00255 ntupleMgr->InitNtuple();
00256 ntupleMgr->FillChi2();
00257 ntupleMgr->FillOptObjects(AtWAMatrix);
00258 ntupleMgr->FillMeasurements();
00259 ntupleMgr->FillFitParameters(AtWAMatrix);
00260 ntupleMgr->FillNtupleTree();
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 if( CocoaDaqReader::GetDaqReader() == 0 ) {
00272
00273 lastEvent = 1;
00274 return !lastEvent;
00275 }
00276
00277
00278 if( Measurement::only1 ) {
00279 lastEvent = 1;
00280 return !lastEvent;
00281 }
00282 nEvent++;
00283 } else {
00284 lastEvent = 1;
00285 return !lastEvent;
00286 }
00287
00288 return !lastEvent;
00289 }
00290
00291
00292
00293 void Fit::WriteVisualisationFiles()
00294 {
00295 #ifdef COCOA_VIS
00296 if(gomgr->GlobalOptions()["VisOnly"] == 1) {
00297 calculateSimulatedMeasurementsWithOriginalValues();
00298 }
00299
00300 GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00301 if(gomgr->GlobalOptions()["VisWriteVRML"] > 0) {
00302 if(ALIUtils::getFirstTime()) ALIVRMLMgr::getInstance().writeFile();
00303 }
00304 if(gomgr->GlobalOptions()["VisWriteIguana"] > 0) {
00305 if(ALIUtils::getFirstTime()) IgCocoaFileMgr::getInstance().writeFile();
00306 }
00307
00308 if(gomgr->GlobalOptions()["VisOnly"] == 1) {
00309 if(ALIUtils::debug >= 1 )std::cout << " Visualiation file(s) succesfully written. Ending.... " << std::endl;
00310 exit(1);
00311 }
00312 #endif
00313 }
00314
00315
00316
00317
00318
00319 void Fit::setFittableEntries()
00320 {
00321
00322 std::vector< Entry* >::const_iterator vecite;
00323
00324 GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00325 theMinimumEntryQuality = int(gomgr->GlobalOptions()[ALIstring("calcul_type")]) + 1;
00326 if ( ALIUtils::debug >= 4) std::cout << "Fit::setFittableEntries: total Entry List size= " << Model::EntryList().size() << std::endl;
00327
00328 int No_entry_to_fit = 0;
00329 for ( vecite = Model::EntryList().begin();
00330 vecite != Model::EntryList().end(); vecite++ ) {
00331
00332
00333 if ( (*vecite)->quality() >= theMinimumEntryQuality ) {
00334 (*vecite)->setFitPos( No_entry_to_fit );
00335 if( ALIUtils::debug >= 4 ) std::cout << " Entry To Fit= " << No_entry_to_fit << " " << (*vecite)->OptOCurrent()->name() << " " << (*vecite)->name() << " with quality= " << (*vecite)->quality() << std::endl;
00336 No_entry_to_fit++;
00337 }
00338 }
00339
00340 }
00341
00342
00343
00344
00345
00346
00347 FitQuality Fit::fitParameters( const double daFactor )
00348 {
00349 redoMatrices();
00350
00351 if(ALIUtils::debug >= 2) std::cout << " Fit quality daFactor " << daFactor << std::endl;
00352
00353 if(ALIUtils::debug >= 0) {
00354 std::cout << std::endl << "Fit iteration " << theNoFitIterations << " ..." << std::endl;
00355 }
00356
00357
00358
00359 if( Model::getCocoaStatus() == COCOA_FirstIterationInEvent ) {
00360 thePreviousIterationFitQuality = GetSChi2( 0 );
00361
00362 GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00363 if (gomgr->GlobalOptions()[ ALIstring("stopAfter1stIteration") ] == 1) {
00364 std::cout << "@!! STOPPED by user after 1st iteration " << std::endl;
00365 exit(1);
00366 }
00367 }
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 if(ALIUtils::debug >= 10) {
00380 std::cout << std::endl << " End fitParameters " << theNoFitIterations << " ..." << std::endl;
00381 }
00382
00383 return getFitQuality();
00384
00385 }
00386
00387
00388 void Fit::redoMatrices()
00389 {
00390 deleteMatrices();
00391
00392 calculateSimulatedMeasurementsWithOriginalValues();
00393
00394 PropagateErrors();
00395
00396 }
00397
00398
00399
00400
00401
00402
00403 void Fit::PropagateErrors()
00404 {
00405
00406
00407 CreateMatrices();
00408
00409
00410 time_t now;
00411 now = clock();
00412 if(ALIUtils::debug >= 2) std::cout << "TIME:CREATE_MAT : " << now << " " << difftime(now, ALIUtils::time_now())/1.E6 << std::endl;
00413 ALIUtils::set_time_now(now);
00414
00415
00416 FillMatricesWithMeasurements();
00417
00418
00419 now = clock();
00420 if(ALIUtils::debug >= 0) std::cout << "TIME:MAT_MEAS_FILLED: " << now << " " << difftime(now, ALIUtils::time_now())/1.E6 << std::endl;
00421 ALIUtils::set_time_now(now);
00422
00423
00424 GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00425 if (gomgr->GlobalOptions()[ ALIstring("calcul_type") ] == 0) {
00426 FillMatricesWithCalibratedParameters();
00427
00428
00429 now = clock();
00430 if(ALIUtils::debug >= 0) std::cout << "TIME:MAT_CAL_FILLED : " << now << " " << difftime(now, ALIUtils::time_now())/1.E6 << std::endl;
00431 ALIUtils::set_time_now(now);
00432
00433 }
00434
00435
00436 setCorrelationsInWMatrix();
00437
00438 if(ALIUtils::debug >= 3) WMatrix->Dump("WMatrix before inverse");
00439
00440
00441 if( m_norm1( WMatrix->MatNonConst() ) == 0 ) {
00442
00443 return;
00444 } else {
00445 WMatrix->inverse();
00446 }
00447
00448 if(ALIUtils::debug >= 3) AMatrix->Dump("AMatrix");
00449 if(ALIUtils::debug >= 3) WMatrix->Dump("WMatrix");
00450 if(ALIUtils::debug >= 3) yfMatrix->Dump("yfMatrix");
00451
00452 if(gomgr->GlobalOptions()["onlyDeriv"] >= 1) {
00453 std::cout << "ENDING after derivatives are calculated ('onlyDeriv' option set)" << std::endl;
00454 exit(1);
00455 }
00456
00457 multiplyMatrices();
00458
00459 now = clock();
00460 if(ALIUtils::debug >= 0) std::cout << "TIME:MAT_MULTIPLIED : " << now << " " << difftime(now, ALIUtils::time_now())/1.E6 << std::endl;
00461 ALIUtils::set_time_now(now);
00462
00463 if( ALIUtils::getFirstTime() == 1) ALIUtils::setFirstTime( 0 );
00464
00465
00466 }
00467
00468
00469
00470
00471
00472 void Fit::calculateSimulatedMeasurementsWithOriginalValues()
00473 {
00474
00475
00476
00477 DeviationsFromFileSensor2D::setApply( 1 );
00478
00479 if(ALIUtils::debug >= 2) std::cout << "Fit::calculateSimulatedMeasurementsWithOriginalValues" <<std::endl;
00480
00481 std::vector< Measurement* >::const_iterator vmcite;
00482 for ( vmcite = Model::MeasurementList().begin(); vmcite != Model::MeasurementList().end(); vmcite++) {
00483
00484 (*vmcite)->calculateOriginalSimulatedValue();
00485 }
00486
00487
00488
00489 DeviationsFromFileSensor2D::setApply( 0 );
00490
00491 }
00492
00493
00494
00495 void Fit::deleteMatrices()
00496 {
00497
00498 delete DaMatrix;
00499 delete AMatrix;
00500 delete WMatrix;
00501 delete yfMatrix;
00502
00503 delete AtMatrix;
00504 delete AtWAMatrix;
00505
00506
00507
00508
00509 }
00510
00511
00512
00513
00514 void Fit::CreateMatrices()
00515 {
00516
00517
00518 ALIint NoMeas = 0;
00519 std::vector< Measurement* >::const_iterator vmcite;
00520 for ( vmcite = Model::MeasurementList().begin();
00521 vmcite != Model::MeasurementList().end(); vmcite++ ) {
00522 NoMeas += (*vmcite)->dim();
00523 }
00524 if( ALIUtils::debug >= 9) std::cout << "NOMEAS" << NoMeas << std::endl;
00525
00526
00527 ALIint nEnt_cal = 0;
00528 ALIint noent = 0;
00529
00530 GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00531 if ( gomgr->GlobalOptions()[ "calcul_type" ] == 0) {
00532
00533 if( ALIUtils::debug >= 5 ) std::cout << " Count number of 'cal'ibrated parameters " << std::endl;
00534 std::vector< Entry* >::iterator veite;
00535 for ( veite = Model::EntryList().begin();
00536 veite != Model::EntryList().end(); veite++ ) {
00537 if ( (*veite)->quality() == 1 ) nEnt_cal++;
00538 noent++;
00539 if( ALIUtils::debug >= 6) {
00540 std::cout <<(*veite)->quality() << " " << (*veite)->OptOCurrent()->name() << " "
00541 << (*veite)->name() << " # ENT CAL " << nEnt_cal << " # ENT " << noent << std::endl;
00542 }
00543 }
00544 }
00545
00546
00547 ALIint NoParamFit = 0;
00548 std::vector<Entry*>::const_iterator vecite;
00549 for ( vecite = Model::EntryList().begin();
00550 vecite != Model::EntryList().end(); vecite++) {
00551 if ( (*vecite)->quality() >= theMinimumEntryQuality ) {
00552 NoParamFit++;
00553 if( ALIUtils::debug >= 99) std::cout <<(*vecite)->quality() << (*vecite)->OptOCurrent()->name() << (*vecite)->name() << "NoParamFit" << NoParamFit << std::endl;
00554
00555 }
00556 }
00557
00558
00559 ALIint NoLinesA = NoMeas + nEnt_cal;
00560 ALIint NoColumnsA = NoParamFit;
00561 AMatrix = new ALIMatrix( NoLinesA, NoColumnsA );
00562
00563 ALIint NoLinesW = NoLinesA;
00564 ALIint NoColumnsW = NoLinesA;
00565 WMatrix = new ALIMatrix( NoLinesW, NoColumnsW );
00566
00567 ALIint NoLinesY = NoLinesA;
00568
00569 yfMatrix = new ALIMatrix( NoLinesY, 1 );
00570
00571
00572
00573 if ( ALIUtils::debug >= 4 ) std::cout << "CreateMatrices: NoLinesA = " << NoLinesA <<
00574 " NoColumnsA = " << NoColumnsA << std::endl;
00575 }
00576
00577
00578
00579
00580
00581
00582
00583 void Fit::FillMatricesWithMeasurements()
00584 {
00585
00586 int Aline = 0;
00587
00588
00589 std::vector<Measurement*>::const_iterator vmcite;
00590 std::vector<Entry*>::const_iterator vecite;
00591 for ( vmcite = Model::MeasurementList().begin();
00592 vmcite != Model::MeasurementList().end(); vmcite++) {
00593 if( ALIUtils::debug >= 5 ) std::cout << "FillMatricesWithMeasurements: measurement " << (*vmcite)->name() << " # entries affecting " <<(*vmcite)->affectingEntryList().size() << std::endl;
00594
00595
00596 ALIint measdim = (*vmcite)->dim();
00597 ALIdouble* derivRE;
00598 derivRE = new ALIdouble[measdim];
00599
00600
00601
00602
00603 for ( vecite = (*vmcite)->affectingEntryList().begin();
00604 vecite != (*vmcite)->affectingEntryList().end(); vecite++) {
00605
00606 if ( (*vecite)->quality() >= theMinimumEntryQuality ) {
00607 if ( ALIUtils::debug >= 4) {
00608
00609 std::cout <<"entry affecting: " << (*vecite)->OptOCurrent()->name() << " " << (*vecite)->name() <<std::endl;
00610 }
00611 derivRE = (*vmcite)->DerivativeRespectEntry(*vecite);
00612
00613 for ( ALIuint jj = 0; jj < ALIuint(measdim); jj++) {
00614 AMatrix->AddData( Aline+jj, (*vecite)->fitPos(), derivRE[jj] );
00615 if( ALIUtils::debug >= 5) std::cout << "FillMatricesWithMeasurements: AMATRIX (" << Aline+jj << "," << (*vecite)->fitPos() << " = " << derivRE[jj] << std::endl;
00616
00617 (*vmcite)->setValueSimulated( jj, (*vmcite)->valueSimulated_orig(jj) );
00618 }
00619 }
00620 }
00621 delete[] derivRE;
00622
00623
00624
00625 for ( ALIuint jj=0; jj < ALIuint((*vmcite)->dim()); jj++) {
00626 ALIdouble sigma = (*vmcite)->sigma()[jj];
00627 if ( sigma == 0. ) {
00628 std::cerr << "EXITING: Measurement number " <<
00629 vmcite - Model::MeasurementList().begin() <<
00630 "has 0 error!!" << std::endl;
00631 } else {
00632
00633
00634 ALIdouble sigmanew = sigma * Measurement::cameraScaleFactor;
00635
00636 WMatrix->AddData( Aline+jj, Aline+jj, (sigmanew*sigmanew) );
00637 }
00638
00639
00640
00641
00642
00643 yfMatrix->AddData( Aline+jj, 0, (*vmcite)->value()[jj] - (*vmcite)->valueSimulated_orig(jj) );
00644
00645 }
00646 if ( ALIUtils::debug >= 99) std::cout << "change line" << Aline << std::endl;
00647 Aline += measdim;
00648 if ( ALIUtils::debug >= 99) std::cout << "change line" << Aline << std::endl;
00649
00650 }
00651
00652 if ( ALIUtils::debug >= 4) AMatrix->Dump("Matrix A with meas");
00653 if ( ALIUtils::debug >= 4) WMatrix->Dump("Matrix W with meas");
00654 if ( ALIUtils::debug >= 4) yfMatrix->Dump("Matrix y with meas");
00655
00656 }
00657
00658
00659
00660
00661
00662
00663
00664
00665 void Fit::FillMatricesWithCalibratedParameters()
00666 {
00667
00668 ALIint NolinMes = 0;
00669 std::vector<Measurement*>::const_iterator vmcite;
00670 for ( vmcite = Model::MeasurementList().begin();
00671 vmcite != Model::MeasurementList().end(); vmcite++) {
00672 NolinMes += (*vmcite)->dim();
00673 }
00674 if(ALIUtils::debug>=4) std::cout << "@@FillMatricesWithCalibratedParameters" << std::endl;
00675
00676 std::vector< Entry* >::const_iterator vecite;
00677 ALIint nEntcal = 0;
00678
00679 for ( vecite = Model::EntryList().begin();
00680 vecite != Model::EntryList().end(); vecite++ ) {
00681
00682
00683
00684 if ( (*vecite)->quality() == 1 ){
00685
00686 ALIint lineNo = NolinMes + nEntcal;
00687 ALIint columnNo = (*vecite)->fitPos();
00688 AMatrix->AddData( lineNo, columnNo, 1. );
00689 if(ALIUtils::debug >= 4) std::cout << "Fit::FillMatricesWithCalibratedParameters: AMatrix ( " << lineNo << " , " << columnNo << ") = " << 1. << std::endl;
00690
00691
00692 ALIdouble entsig = (*vecite)->sigma();
00693 if(ALIUtils::debug >= 4) std::cout << "Fit::FillMatricesWithCalibratedParameters: WMatrix ( " << lineNo << " , " << columnNo << ") = " << entsig*entsig << std::endl;
00694 WMatrix->AddData( lineNo, lineNo, entsig*entsig );
00695
00696
00697
00698
00699 ALIdouble calFit;
00700 GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00701 gomgr->getGlobalOptionValue("calParamInyfMatrix", calFit );
00702 if( calFit ) {
00703 yfMatrix->AddData( lineNo, 0, -(*vecite)->valueDisplacementByFitting() );
00704
00705
00706
00707
00708
00709
00710 } else {
00711 yfMatrix->AddData( lineNo, 0, 0. );
00712 }
00713
00714 nEntcal++;
00715 }
00716 }
00717
00718 }
00719
00720
00721
00722
00723
00724 void Fit::setCorrelationsInWMatrix()
00725 {
00726
00727 ErrorCorrelationMgr* corrMgr = ErrorCorrelationMgr::getInstance();
00728 ALIint siz = corrMgr->getNumberOfCorrelations();
00729 if( siz == 0 ) return;
00730
00731
00732 ALIuint ii;
00733 for( ii = 0; ii < ALIuint(siz); ii++ ){
00734
00735 ErrorCorrelation* corr = corrMgr->getCorrelation( ii );
00736 setCorrelationFromParamFitted( corr->getEntry1(), corr->getEntry2(), corr->getCorrelation() );
00737 }
00738
00739 }
00740
00741
00742
00743
00744
00745 void Fit::setCorrelationFromParamFitted( const pss& entry1, const pss& entry2,
00746 ALIdouble correl )
00747 {
00748
00749 ALIint pmsize = WMatrix->NoLines();
00750 ALIint fit_pos1 = Model::getEntryByName(entry1.first, entry1.second)->fitPos();
00751 ALIint fit_pos2 = Model::getEntryByName(entry2.first, entry2.second)->fitPos();
00752 std::cout << "CHECKsetCorrelatiFPF " << fit_pos1 << " " << fit_pos2 << std::endl;
00753
00754 if( fit_pos1 >= 0 && fit_pos1 < pmsize && fit_pos2 >= 0 && fit_pos2 < pmsize ) {
00755 setCorrelationFromParamFitted( fit_pos1, fit_pos2, correl );
00756 }
00757 }
00758
00759
00760
00761 void Fit::setCorrelationFromParamFitted( const ALIint fit_pos1, const ALIint fit_pos2, ALIdouble correl )
00762 {
00763
00764
00765 WMatrix->SetCorrelation( fit_pos1, fit_pos2, correl );
00766 std::cout << "setCorrelatiFPF " << fit_pos1 << " " << fit_pos2 << " " << correl << std::endl;
00767 }
00768
00769
00770
00771
00772
00773 void Fit::multiplyMatrices()
00774 {
00775 if(ALIUtils::debug >= 4) std::cout << "@@Multiplying matrices " << std::endl;
00776
00777 AtMatrix = new ALIMatrix( *AMatrix );
00778 if(ALIUtils::debug >= 5) AtMatrix->Dump("AtMatrix=A");
00779
00780 AtMatrix->transpose();
00781 if(ALIUtils::debug >= 4) AtMatrix->Dump("AtMatrix");
00782
00783
00784 AtWAMatrix = new ALIMatrix(0, 0);
00785
00786 *AtWAMatrix = *AtMatrix * *WMatrix * *AMatrix;
00787 if(ALIUtils::debug >= 5) AtWAMatrix->Dump("AtWAMatrix");
00788
00789 CheckIfFitPossible();
00790
00791
00792
00793 time_t now;
00794 now = clock();
00795 if(ALIUtils::debug >= 0) std::cout << "TIME:BEFORE_INVERSE : " << now << " " << difftime(now, ALIUtils::time_now())/1.E6 << std::endl;
00796 ALIUtils::set_time_now(now);
00797
00798
00799
00800
00801
00802
00803
00804 AtWAMatrix->inverse();
00805 if(ALIUtils::debug >= 4) AtWAMatrix->Dump("inverse AtWAmatrix");
00806 now = clock();
00807 if(ALIUtils::debug >= 0) std::cout << "TIME:AFTER_INVERSE : " << now << " " << difftime(now, ALIUtils::time_now())/1.E6 << std::endl;
00808 ALIUtils::set_time_now(now);
00809
00810
00811
00812
00813
00814
00815 std::vector< Entry* >::const_iterator vecite;
00816
00817 if( ALIUtils::debug >= 4 ) {
00818 std::cout << "PARAM" << " Optical Object " << " entry name " << " Param.Value "
00819 << " Prog.Error" << " Orig.Error" << std::endl;
00820 }
00821
00822 ALIint nEnt = 0;
00823 ALIint nEntUnk = 0;
00824 for ( vecite = Model::EntryList().begin();
00825 vecite != Model::EntryList().end(); vecite++ ) {
00826
00827
00828 if( (*vecite)->quality() >= theMinimumEntryQuality ){
00829 if( ALIUtils::debug >= 4) {
00830 std::cout << nEnt << "PARAM" << std::setw(26)
00831 << (*vecite)->OptOCurrent()->name().c_str()
00832 << std::setw(8) << " " << (*vecite)->name().c_str() << " "
00833 << std::setw(8) << " " << (*vecite)->value() << " "
00834 << std::setw(8) << sqrt(AtWAMatrix->Mat()->me[nEnt][nEnt]) /
00835 (*vecite)->OutputSigmaDimensionFactor()
00836 << " " << (*vecite)->sigma() / (*vecite)->OutputSigmaDimensionFactor()
00837 << " Q" << (*vecite)->quality() << std::endl;
00838 }
00839 nEnt++;
00840 }
00841 if ( (*vecite)->quality() == 2 ) nEntUnk++;
00842 }
00843
00844 if(ALIUtils::debug >= 5) yfMatrix->Dump("PD(y-f)Matrix final");
00845
00846 }
00847
00848
00849
00850
00851
00852 FitQuality Fit::getFitQuality( const ALIbool canBeGood )
00853 {
00854
00855 double fit_quality = GetSChi2(1);
00856
00857
00858 ALIMatrix* DatMatrix = new ALIMatrix( *DaMatrix );
00859
00860 DatMatrix->transpose();
00861 if(ALIUtils::debug >= 5) DatMatrix->Dump("DatMatrix");
00862
00863 ALIMatrix* DSMat = new ALIMatrix(*DatMatrix * *AtMatrix * *WMatrix * *yfMatrix);
00864 ALIMatrix* DSMattemp = new ALIMatrix(*DatMatrix * *AtMatrix * *WMatrix);
00865 if(ALIUtils::debug >= 5) DSMattemp->Dump("DSMattempMatrix=Dat*At*W");
00866 ALIMatrix* DSMattemp2 = new ALIMatrix(*AtMatrix * *WMatrix * *yfMatrix);
00867 if(ALIUtils::debug >= 5) DSMattemp2->Dump("DSMattempMatrix2=At*W*yf");
00868 ALIMatrix* DSMattemp3 = new ALIMatrix(*AtMatrix * *WMatrix);
00869 if(ALIUtils::debug >= 5) DSMattemp3->Dump("DSMattempMatrix3=At*W");
00870 if(ALIUtils::debug >= 5) AtMatrix->Dump("AtMatrix");
00871
00872
00873
00874
00875
00876
00877
00878 if(ALIUtils::debug >= 5) (*yfMatrix).Dump("yfMatrix");
00879 if(ALIUtils::debug >= 5) DSMat->Dump("DSMatrix final");
00880
00881
00882 ALIdouble fit_quality_cut = (*DSMat)(0,0);
00883
00884 delete DSMat;
00885 if(ALIUtils::debug >= 0) std::cout << theNoFitIterations << " Fit quality predicted improvement in distance to minimum is = " << fit_quality_cut << std::endl;
00886 if( ALIUtils::report >= 2 ) {
00887 ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
00888 fileout << theNoFitIterations << " Fit quality predicted improvement in distance to minimum is = " << fit_quality_cut << std::endl;
00889 }
00890
00891
00892
00893
00894
00895 time_t now;
00896 now = clock();
00897 if(ALIUtils::debug >= 0) std::cout << "TIME:QUALITY_CHECKED: " << now << " " << difftime(now, ALIUtils::time_now())/1.E6 << std::endl;
00898 ALIUtils::set_time_now(now);
00899
00900 FitQuality fitQuality;
00901
00902
00903
00904 if( fit_quality_cut < 0. ) {
00905 fitQuality = FQchiSquareWorsened;
00906 if(ALIUtils::debug >= 0) std::cerr << "!!WARNING: Fit quality has worsened: Fit Quality now = " << fit_quality
00907 << " before " << thePreviousIterationFitQuality << " diff " << fit_quality - thePreviousIterationFitQuality << std::endl;
00908
00909
00910 } else {
00911
00912 if( (fit_quality_cut < theFitQualityCut || fabs(thePreviousIterationFitQuality - fit_quality_cut )/fit_quality_cut < theRelativeFitQualityCut ) && canBeGood ) {
00913 fitQuality = FQsmallDistanceToMinimum;
00914 if(ALIUtils::report >= 1) {
00915 ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
00916 fileout << "STOP: SMALL IMPROVEMENT IN ITERATION " << theNoFitIterations << " = " << fit_quality_cut << " < " << theFitQualityCut << std::endl;
00917 }
00918 if(ALIUtils::debug >= 4) {
00919 std::cout << "STOP: SMALL IMPROVEMENT IN ITERATION " << theNoFitIterations << " = " << fit_quality_cut << " < " << theFitQualityCut << std::endl;
00920 }
00921
00922
00923 } else {
00924 fitQuality = FQbigDistanceToMinimum;
00925
00926 thePreviousIterationFitQuality = fit_quality;
00927
00928 if(ALIUtils::report >= 2) {
00929 ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
00930 fileout << "CONTINUE: BIG IMPROVEMENT IN ITERATION " << theNoFitIterations << " = " << fit_quality_cut << " >= " << theFitQualityCut << std::endl;
00931 }
00932 if(ALIUtils::debug >= 4) {
00933 std::cout << "CONTINUE: BIG IMPROVEMENT IN ITERATION " << theNoFitIterations << " = " << fit_quality_cut << " >= " << theFitQualityCut << std::endl;
00934 }
00935 }
00936 }
00937
00938 return fitQuality;
00939
00940 }
00941
00942
00943 ALIdouble Fit::GetSChi2( ALIbool useDa )
00944 {
00945 ALIMatrix* SMat = 0;
00946 if( useDa ){
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957 DaMatrix = new ALIMatrix(0, 0);
00958
00959 *DaMatrix = ( *AtWAMatrix * *AtMatrix * *WMatrix * *yfMatrix);
00960 if(ALIUtils::debug >= 5) DaMatrix->Dump("DaMatrix");
00961
00962
00963
00964
00965
00966 ALIMatrix* tmpM = new ALIMatrix( 0,0 );
00967 *tmpM = *AMatrix * *DaMatrix - *yfMatrix;
00968 if(ALIUtils::debug >= 5) tmpM->Dump("A*Da + f - y Matrix ");
00969
00970 ALIMatrix* tmptM = new ALIMatrix( *tmpM );
00971 tmptM->transpose();
00972 if(ALIUtils::debug >= 5) tmptM->Dump("tmptM after transpose");
00973 if(ALIUtils::debug >= 5) WMatrix->Dump("WMatrix");
00974
00975
00976
00977 ALIMatrix* SMat1 = MatrixByMatrix(*tmptM,*WMatrix);
00978
00979 if(ALIUtils::debug >= 5) SMat1->Dump("(A*Da + f - y)^T * W Matrix");
00980 SMat = MatrixByMatrix(*SMat1,*tmpM);
00981
00982 delete tmpM;
00983 delete tmptM;
00984 if(ALIUtils::debug >= 5) SMat->Dump("SMatrix with Da");
00985 } else {
00986 ALIMatrix* yftMat = new ALIMatrix(*yfMatrix);
00987 yftMat->transpose();
00988 SMat = new ALIMatrix(*yftMat * *WMatrix * *yfMatrix);
00989 delete yftMat;
00990 if(ALIUtils::debug >= 5) SMat->Dump("SMatrix no Da");
00991 }
00992 ALIdouble fit_quality = (*SMat)(0,0);
00993 delete SMat;
00994 if(ALIUtils::debug >= 5) std::cout << " GetSChi2 " << useDa << " = " << fit_quality << std::endl;
00995
00996 PrintChi2( fit_quality, !useDa );
00997
00998 return fit_quality;
00999
01000 }
01001
01002
01003
01004
01005
01006 void Fit::addDaMatrixToEntries()
01007 {
01008
01009 if(ALIUtils::debug >= 4) {
01010 std::cout << "@@ Adding correction (DaMatrix) to Entries " << std::endl;
01011 DaMatrix->Dump("DaMatrix =");
01012 }
01013
01014
01015
01016
01017
01018
01019
01020
01021 ALIint nEnt = 0;
01022 std::vector<Entry*>::const_iterator vecite;
01023 for ( vecite = Model::EntryList().begin();
01024 vecite != Model::EntryList().end(); vecite++ ) {
01025
01026
01027
01028 if ( (*vecite)->quality() >= theMinimumEntryQuality ){
01029 if ( ALIUtils::debug >= 5) {
01030 std::cout << std::endl << " @@@ PENTRY change "
01031 << (*vecite)->OptOCurrent()->name() << " " << (*vecite)->name() << " "
01032 << " change= " << (*DaMatrix)(nEnt,0)
01033 << " value= " << (*vecite)->valueDisplacementByFitting()
01034 << std::endl;
01035 }
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046 if(ALIUtils::debug >= 4) std::cout << " old valueDisplacementByFitting " << (*vecite)->name() << " " << (*vecite)->valueDisplacementByFitting() << " original value " << (*vecite)->value() <<std::endl;
01047
01048 (*vecite)->addFittedDisplacementToValue( (*DaMatrix)(nEnt,0) );
01049
01050 if(ALIUtils::debug >= 4) std::cout << nEnt << " new valueDisplacementByFitting " << (*vecite)->OptOCurrent()->name() << " " << (*vecite)->name() << " = " << (*vecite)->valueDisplacementByFitting() << " " << (*DaMatrix)(nEnt,0) << std::endl ;
01051 nEnt++;
01052 }
01053 }
01054
01055 }
01056
01057
01058
01059
01060 void Fit::substractLastDisplacementToEntries( const ALIdouble factor )
01061 {
01062
01063 if(ALIUtils::debug >= 4) {
01064 std::cout << "@@ Fit::substractToHalfDaMatrixToEntries " << std::endl;
01065 }
01066
01067 std::vector<Entry*>::const_iterator vecite;
01068 for ( vecite = Model::EntryList().begin(); vecite != Model::EntryList().end(); vecite++ ) {
01069 if ( (*vecite)->quality() >= theMinimumEntryQuality ){
01070
01071
01072 ALIdouble lastadd = (*vecite)->lastAdditionToValueDisplacementByFitting() * factor;
01073
01074 (*vecite)->addFittedDisplacementToValue( -lastadd );
01075 (*vecite)->setLastAdditionToValueDisplacementByFitting( - (*vecite)->lastAdditionToValueDisplacementByFitting() );
01076
01077
01078 if(ALIUtils::debug >= 4) std::cout << " new valueDisplacementByFitting " << (*vecite)->OptOCurrent()->name() << " " << (*vecite)->name() << " = " << (*vecite)->valueDisplacementByFitting() << " " << std::endl ;
01079 }
01080 }
01081
01082 }
01083
01084
01085
01086
01087
01088 void Fit::dumpFittedValues( ALIFileOut& fileout, ALIbool printErrors, ALIbool printOrig )
01089 {
01090
01091 if(ALIUtils::debug >= 0) {
01092 std::cout << "SRPARPOS " << " Optical Object "
01093 << " Parameter" << " Fit.Value " << " Orig.Value" << std::endl;
01094 }
01095
01096 if(ALIUtils::debug >= 0) std::cout << std::endl << "FITTED VALUES " << std::endl;
01097 fileout << std::endl << "FITTED VALUES " << std::endl
01098 << "nEnt_unk"
01099 << " Optical Object"
01100 << " Parameter ";
01101 if( printErrors ) {
01102 fileout << " value (+-error)"
01103 << " orig.val (+-error)";
01104 } else {
01105 fileout << " value "
01106 << " orig.val ";
01107 }
01108 fileout << " quality"
01109 << std::endl;
01110
01111
01112 std::vector< Entry* > entries;
01113
01114 int ii, siz;
01115 std::vector< OpticalObject* >::const_iterator vocite;
01116 for( vocite = Model::OptOList().begin(); vocite != Model::OptOList().end(); vocite++ ) {
01117 if( (*vocite)->type() == ALIstring("system") ) continue;
01118
01119 fileout << " %%%% Optical Object: " << (*vocite)->longName() << std::endl;
01120
01121 entries = (*vocite)->CoordinateEntryList();
01122 siz = entries.size();
01123 if( siz != 6 ) {
01124 std::cerr << "!!! FATAL ERROR: strange number of coordinates = " << siz << std::endl;
01125 abort();
01126 }
01127
01128
01129 OpticalObject* opto = entries[0]->OptOCurrent();
01130 const OpticalObject* optoParent = opto->parent();
01131 printCentreInOptOFrame( opto, optoParent, fileout, printErrors, printOrig );
01132
01133
01134 printRotationAnglesInOptOFrame( opto, optoParent, fileout, printErrors, printOrig );
01135
01136
01137 entries = (*vocite)->ExtraEntryList();
01138 siz = entries.size();
01139 for( ii = 0; ii < siz; ii++ ){
01140 double entryvalue = getEntryValue( entries[ii] );
01141 dumpEntryAfterFit( fileout, entries[ii], entryvalue, printErrors, printOrig );
01142 }
01143 }
01144
01145 dumpEntryCorrelations( fileout );
01146
01147 }
01148
01149
01150
01151 void Fit::dumpFittedValuesInAllAncestorFrames( ALIFileOut& fileout, ALIbool printErrors, ALIbool printOrig )
01152 {
01153
01154 fileout << std::endl << "@@@@ FITTED VALUES IN ALL ANCESTORS " << std::endl
01155 << "nEnt_unk"
01156 << " Optical Object"
01157 << " Parameter ";
01158 if( printErrors ) {
01159 fileout << " value (+-error)";
01160 if( printOrig ){
01161 fileout << " orig.val (+-error)";
01162 }
01163 } else {
01164 fileout << " value ";
01165 if( printOrig ){
01166 fileout << " orig.val ";
01167 }
01168 }
01169 fileout << " quality"
01170 << std::endl;
01171
01172
01173 std::vector< Entry* > entries;
01174 std::vector< OpticalObject* >::const_iterator vocite;
01175 for( vocite = Model::OptOList().begin(); vocite != Model::OptOList().end(); vocite++ ) {
01176 if( (*vocite)->type() == ALIstring("system") ) continue;
01177
01178 fileout << " %%%% Optical Object: " << (*vocite)->longName() << std::endl;
01179
01180 entries = (*vocite)->CoordinateEntryList();
01181
01182
01183 OpticalObject* opto = *vocite;
01184 const OpticalObject* optoParent = opto->parent();
01185 do {
01186 fileout << " %% IN FRAME : " << optoParent->longName() << std::endl;
01187 printCentreInOptOFrame( opto, optoParent, fileout, printErrors, printOrig );
01188
01189
01190 printRotationAnglesInOptOFrame( opto, optoParent, fileout, printErrors, printOrig );
01191 optoParent = optoParent->parent();
01192 }while( optoParent );
01193 }
01194
01195 }
01196
01197
01198 void Fit::printCentreInOptOFrame( const OpticalObject* opto, const OpticalObject* optoAncestor, ALIFileOut& fileout, ALIbool printErrors, ALIbool printOrig )
01199 {
01200 Hep3Vector centreLocal;
01201 if( optoAncestor->type() == "system" ) {
01202 centreLocal = opto->centreGlob();
01203 } else {
01204 centreLocal = opto->centreGlob() - optoAncestor->centreGlob();
01205 CLHEP::HepRotation parentRmGlobInv = inverseOf( optoAncestor->rmGlob() );
01206 centreLocal = parentRmGlobInv * centreLocal;
01207 }
01208 if(ALIUtils::debug >= 2 ) {
01209 std::cout << "CENTRE LOCAL "<< opto->name() << " " << centreLocal << " GLOBL " << opto->centreGlob() << " parent GLOB " << optoAncestor->centreGlob() << std::endl;
01210 ALIUtils::dumprm( optoAncestor->rmGlob(), " parent rm " );
01211 }
01212 std::vector< Entry* > entries = opto->CoordinateEntryList();
01213 for( uint ii = 0; ii < 3; ii++ ){
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223 dumpEntryAfterFit( fileout, entries[ii], centreLocal[ii] / entries[ii]->OutputValueDimensionFactor(), printErrors, printOrig );
01224 }
01225
01226 }
01227
01228
01229
01230 void Fit::printRotationAnglesInOptOFrame( const OpticalObject* opto, const OpticalObject* optoAncestor, ALIFileOut& fileout, ALIbool printErrors, ALIbool printOrig )
01231 {
01232 std::vector< Entry* > entries = opto->CoordinateEntryList();
01233 std::vector<double> entryvalues = opto->getRotationAnglesInOptOFrame( optoAncestor, entries );
01234
01235 for( uint ii = 3; ii < entries.size(); ii++ ){
01236 dumpEntryAfterFit( fileout, entries[ii], entryvalues[ii-3]/entries[ii]->OutputValueDimensionFactor(), printErrors, printOrig );
01237 }
01238
01239 }
01240
01241
01242 double Fit::getEntryValue( const Entry* entry )
01243 {
01244 double entryvalue;
01245 if( entry->type() == "angles") {
01246 if(ALIUtils::debug >= 2 ) std::cout << "WARNING valueDisplacementByFitting has no sense for angles " << std::endl;
01247 entryvalue = -999;
01248 }
01249 entryvalue = ( entry->value() + entry->valueDisplacementByFitting() ) / entry->OutputValueDimensionFactor();
01250 return entryvalue;
01251 }
01252
01253
01254 void Fit::dumpEntryAfterFit( ALIFileOut& fileout, const Entry* entry, double entryvalue, ALIbool printErrors, ALIbool printOrig )
01255 {
01256
01257 ALIdouble dimv = entry->OutputValueDimensionFactor();
01258 ALIdouble dims = entry->OutputSigmaDimensionFactor();
01259
01260 if(ALIUtils::debug >= 3) {
01261 std::cout << "ENTRY: "
01262 << std::setw(30) << entry->OptOCurrent()->name()
01263 << std::setw(8) << " " << entry->name() << " "
01264 << std::setw(8) << ( entry->value() + entry->valueDisplacementByFitting() )
01265 <<" " << entry->value()
01266 << " Q" << entry->quality() << std::endl;
01267 }
01268
01269 if ( entry->quality() == 2 ) {
01270 fileout << "UNK: " << entry->fitPos() << " ";
01271 } else if ( entry->quality() == 1 ) {
01272 fileout << "CAL: " << entry->fitPos() << " ";
01273
01274 } else {
01275 fileout << "FIX: -1 ";
01276 }
01277
01278 fileout << std::setw(30) << entry->OptOCurrent()->name()
01279 << std::setw(8) << " " << entry->name() << " "
01280 << std::setw(8) << std::setprecision(8) << entryvalue;
01281 if ( entry->quality() >= theMinimumEntryQuality ) {
01282 if( printErrors ) fileout << " +- " << std::setw(8) << sqrt(AtWAMatrix->Mat()->me[entry->fitPos()][entry->fitPos()]) / dims;
01283 } else {
01284 if( printErrors ) fileout << " +- " << std::setw(8) << 0.;
01285 }
01286 if( printOrig ) {
01287 fileout << std::setw(8) << " " << entry->value() / dimv;
01288 if( printErrors ) fileout << " +- " << std::setw(8) << entry->sigma() /dims << " Q" << entry->quality();
01289
01290 if( ALIUtils::report >= 2) {
01291 float dif = ( entry->value() + entry->valueDisplacementByFitting() ) / dimv - entry->value() / dimv;
01292 if( fabs(dif) < 1.E-9 ) dif = 0.;
01293 fileout << " DIFF= " << dif;
01294
01295 } else {
01296
01297 }
01298 }
01299
01300 fileout << std::endl;
01301
01302 }
01303
01304
01305 void Fit::dumpEntryCorrelations( ALIFileOut& fileout )
01306 {
01307
01308 ALIdouble minCorrel = 1.E-6;
01309
01310 fileout << std::endl << "CORRELATION BETWEEN 'unk' ENTRIES: (>= " << minCorrel<< " )" << std::endl
01311 << "No_1 No_2 correlation " << std::endl;
01312
01313 ALIuint i1,i2;
01314 std::vector< Entry* >::iterator veite1, veite2;
01315 std::string E1, E2;
01316 for ( veite1 = Model::EntryList().begin();
01317 veite1 != Model::EntryList().end(); veite1++ ) {
01318 if( (*veite1)->quality() == 0 ) {
01319 continue;
01320 } else if( (*veite1)->quality() == 1 ) {
01321 E1 = "C";
01322 } else if( (*veite1)->quality() == 2 ) {
01323 E1 = "U";
01324 }
01325 i1 = (*veite1)->fitPos();
01326
01327 for ( veite2 = veite1+1;
01328 veite2 != Model::EntryList().end(); veite2++ ) {
01329 i2 = (*veite2)->fitPos();
01330 if( (*veite2)->quality() == 0 ) {
01331 continue;
01332 } else if( (*veite2)->quality() == 1 ) {
01333 E2 = "C";
01334 } else if( (*veite2)->quality() == 2 ) {
01335 E2 = "U";
01336 }
01337 ALIdouble corr = AtWAMatrix->Mat()->me[i1][i2];
01338 ALIdouble corrf = corr / sqrt(AtWAMatrix->Mat()->me[i1][i1])
01339 / sqrt(AtWAMatrix->Mat()->me[i2][i2]);
01340 if (fabs(corrf) >= minCorrel ) {
01341 if(ALIUtils::debug >= 0) {
01342 std::cout << "CORR:" << E1 << "" << E2 << " (" << i1 << ")" << " (" << i2 << ")" << " " << corrf << std::endl;
01343 }
01344 fileout << "CORR:" << E1 << "" << E2 << " (" << i1 << ")" << " (" << i2 << ")" << " " << corrf << std::endl;
01345 }
01346 }
01347 }
01348
01349 if( ALIUtils::debug >= 2) OpticalObjectMgr::getInstance()->dumpOptOs();
01350
01351 }
01352
01353
01354
01355
01356
01357 void Fit::dumpMatrices()
01358 {
01359
01360 ALIFileOut& matout = ALIFileOut::getInstance( Model::MatricesFName() );
01361
01362 matout << std::endl << " @@@@@@@@@@@@@@@ Iteration No : " << theNoFitIterations << std::endl;
01363 AMatrix->ostrDump( matout, "Matrix A" );
01364 AtMatrix->ostrDump( matout, "Matrix At" );
01365 WMatrix->ostrDump( matout, "Matrix W" );
01366 AtWAMatrix->ostrDump( matout, "Matrix AtWA" );
01367
01368 DaMatrix->ostrDump( matout, "Matrix Da" );
01369 yfMatrix->ostrDump( matout, "Matrix y" );
01370
01371
01372 matout.close();
01373
01374 }
01375
01376
01377
01378
01379
01380 ALIint Fit::findEntryFitPosition( const ALIstring& opto_name, const ALIstring& entry_name )
01381 {
01382 ALIint fitposi = -99;
01383
01384 OpticalObject* opto = Model::getOptOByName( opto_name );
01385
01386 std::vector<Entry*>::const_iterator vecite;
01387 for (vecite = opto->CoordinateEntryList().begin();
01388 vecite < opto->CoordinateEntryList().end(); vecite++) {
01389
01390 if ((*vecite)->name() == entry_name ) {
01391
01392 fitposi = (*vecite)->fitPos();
01393 }
01394 }
01395 for (vecite = opto->ExtraEntryList().begin();
01396 vecite < opto->ExtraEntryList().end(); vecite++) {
01397
01398 if ((*vecite)->name() == entry_name ) {
01399
01400 fitposi = (*vecite)->fitPos();
01401 }
01402 }
01403
01404 if(fitposi == -99) {
01405 std::cerr << "!!EXITING: entry name not found: " << entry_name << std::endl;
01406 exit(2);
01407 } else {
01408 return fitposi;
01409 }
01410 }
01411
01412
01413
01414 void Fit::PrintChi2( ALIdouble fit_quality, ALIbool isFirst )
01415 {
01416 double chi2meas = 0;
01417 double chi2cal = 0;
01418 ALIint nMeas = 0, nUnk = 0;
01419
01420
01421 std::vector< Measurement* >::const_iterator vmcite;
01422 for ( vmcite = Model::MeasurementList().begin(); vmcite != Model::MeasurementList().end(); vmcite++) {
01423
01424 for ( ALIuint ii = 0; ii < ALIuint((*vmcite)->dim()); ii++ ){
01425 nMeas++;
01426 double c2 = ( (*vmcite)->value(ii) - (*vmcite)->valueSimulated(ii) ) / (*vmcite)->sigma(ii);
01427 chi2meas += c2*c2;
01428 if( ALIUtils::debug >= 0) {
01429 std::cout << c2 << " adding chi2meas " << chi2meas << " " << (*vmcite)->name() << ": " << ii << " (mm)R: " << (*vmcite)->value(ii)*1000. << " S: " << (*vmcite)->valueSimulated(ii)*1000. << " Diff= " << ((*vmcite)->value(ii) - (*vmcite)->valueSimulated(ii))*1000. << std::endl;
01430 }
01431 }
01432 }
01433
01434
01435 std::vector< Entry* >::iterator veite;
01436 for ( veite = Model::EntryList().begin();
01437 veite != Model::EntryList().end(); veite++ ) {
01438 if ( (*veite)->quality() == 2 ) nUnk++;
01439 if ( (*veite)->quality() == 1 ) {
01440 double c2 = (*veite)->valueDisplacementByFitting() / (*veite)->sigma();
01441
01442 chi2cal += c2*c2;
01443 if( ALIUtils::debug >= 0) std::cout << c2 << " adding chi2cal " << chi2cal << " " << (*veite)->OptOCurrent()->name() << " " << (*veite)->name() << std::endl;
01444
01445 }
01446 }
01447
01448 if( ALIUtils::report >= 1) {
01449 ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
01450 fileout << " Chi2= " << chi2meas+chi2cal << " / " << nMeas-nUnk << " dof " << " From measurements= " << chi2meas << " from calibrated parameters= " << chi2cal << std::endl;
01451 }
01452 if( ALIUtils::debug >= 3) std::cout << " quality Chi2 (no correlations) " << chi2meas+chi2cal << " " << chi2meas << " " << chi2cal << std::endl;
01453
01454
01455 if( !isFirst ) {
01456
01457
01458 if(ALIUtils::debug >= 0) {
01459 std::cout << std::endl << "Fit iteration " << theNoFitIterations << " ..." << std::endl;
01460
01461 }
01462 if( ALIUtils::report >= 1 ) {
01463 ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
01464 fileout << std::endl << "Fit iteration " << theNoFitIterations << " ..." << std::endl;
01465
01466 }
01467 }
01468
01469
01470 if(ALIUtils::debug >= 0) std::cout << theNoFitIterations << " Chi2 after iteration = " << fit_quality << std::endl;
01471 if( ALIUtils::report >= 1 ) {
01472
01473 ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
01474 fileout << theNoFitIterations << " Chi2 after iteration = " << fit_quality << std::endl;
01475 }
01476
01477 }
01478
01479
01480 void Fit::CheckIfFitPossible()
01481 {
01482
01483 ALIint NolinMes = 0;
01484 std::vector<Measurement*>::const_iterator vmcite;
01485 for ( vmcite = Model::MeasurementList().begin();
01486 vmcite != Model::MeasurementList().end(); vmcite++) {
01487 NolinMes += (*vmcite)->dim();
01488 }
01489
01490 std::vector< Entry* >::const_iterator vecite;
01491 for ( vecite = Model::EntryList().begin(); vecite != Model::EntryList().end(); vecite++ ) {
01492 if( ALIUtils::debug >= 3 ) std::cout << "Fit::CheckFitIsPossible looping for entry " << (*vecite)->longName() << std::endl;
01493 if ( (*vecite)->quality() == 2 ) {
01494 ALIint nCol = (*vecite)->fitPos();
01495
01496 ALIbool noDepend = TRUE;
01497 if( ALIUtils::debug >= 3 ) std::cout << "Fit::CheckFitIsPossible looping for entry " << nCol << std::endl;
01498 for( ALIint ii = 0; ii < NolinMes; ii++ ) {
01499 if( ALIUtils::debug >= 4 ) std::cout << ii << "Derivative " << (*AMatrix)(ii,nCol) << std::endl;
01500
01501 if( fabs((*AMatrix)(ii,nCol)) > ALI_DBL_MIN ) {
01502 if( ALIUtils::debug >= 3 ) std::cout << "Fit::CheckIfFitIsPossible " << nCol << " " << ii << " = " << (*AMatrix)(ii,nCol) << std::endl;
01503 noDepend = FALSE;
01504 break;
01505 }
01506 }
01507 if( noDepend ){
01508 throw cms::Exception("SDFError") << "!!!FATAL ERROR: Fit::CheckIfFitPossible() no measurement depends on unknown entry " << (*vecite)->OptOCurrent()->name() << "/" << (*vecite)->name() << std::endl
01509 << "!!! Fit will not be possible! " << std::endl;
01510 }
01511 }
01512 }
01513
01514
01515
01516 std::vector< Entry* >::const_iterator vecite1,vecite2;
01517 ALIint nLin = AMatrix->NoLines();
01518 ALIdouble derivPrec = ALI_DBL_MIN;
01519
01520 for ( vecite1 = Model::EntryList().begin();
01521 vecite1 != Model::EntryList().end(); vecite1++ ) {
01522 if( (*vecite1)->quality() == 2 ) {
01523 vecite2 = vecite1; vecite2++;
01524 for ( ;vecite2 != Model::EntryList().end(); vecite2++ ) {
01525 if( (*vecite2)->quality() == 2 ) {
01526 ALIint fitpos1 = (*vecite1)->fitPos();
01527 ALIint fitpos2 = (*vecite2)->fitPos();
01528 if( ALIUtils::debug >= 5 ) std::cout << "Fit::CheckIfFitIsPossible checking " << (*vecite1)->longName() << " ( " << fitpos1 << " ) & " << (*vecite2)->longName() << " ( " << fitpos2 << " ) "<< std::endl;
01529 ALIdouble prop = DBL_MAX;
01530 ALIbool isProp = TRUE;
01531 for( ALIint ii = 0; ii < nLin; ii++ ) {
01532 if( ALIUtils::debug >= 5 ) std::cout << "Fit::CheckIfFitIsPossible " << ii << " : " << (*AMatrix)(ii,fitpos1) << " ?= " << (*AMatrix)(ii,fitpos2) << std::endl;
01533 if( fabs((*AMatrix)(ii,fitpos1)) < derivPrec ) {
01534 if( fabs((*AMatrix)(ii,fitpos2)) > derivPrec ) {
01535 isProp = FALSE;
01536 break;
01537 }
01538 } else {
01539 ALIdouble propn = (*AMatrix)(ii,fitpos2) / (*AMatrix)(ii,fitpos1);
01540 if( prop != DBL_MAX && prop != propn ) {
01541 isProp = FALSE;
01542 break;
01543 }
01544 prop = propn;
01545 }
01546 }
01547 if( isProp ) {
01548 std::cerr << "!!!FATAL ERROR: Fit::CheckIfFitPossible() two entries for which the measurements have the same dependency (or 100% proportional) " << (*vecite1)->longName() << " & " << (*vecite2)->longName() << std::endl
01549 << "!!! Fit will not be possible! " << std::endl;
01550 throw cms::Exception("SDFError");
01551 }
01552 }
01553 }
01554 }
01555 }
01556 }
01557
01558
01559 int Fit::CheckIfMeasIsProportionalToAnother( uint measNo )
01560 {
01561 int measProp = -1;
01562
01563 std::set<uint> columnsEqual;
01564 std::set<uint> columnsEqualSave;
01565 uint biggestColumn = 0;
01566 ALIdouble biggest = 0.;
01567 for (int ii = 0; ii < AMatrix->NoColumns(); ii++ ){
01568 if( fabs((*AMatrix)(measNo,ii)) > biggest ) {
01569 biggest = fabs((*AMatrix)(measNo,ii));
01570 biggestColumn = ii;
01571 }
01572 columnsEqualSave.insert(ii);
01573 }
01574
01575 ALIdouble div;
01576
01577 for( int jj = 0; jj < AMatrix->NoLines(); jj++ ){
01578 if( jj == int(measNo) ) continue;
01579 columnsEqual = columnsEqualSave;
01580
01581 for (int ii = 0; ii < AMatrix->NoColumns(); ii++ ){
01582 div = (*AMatrix)(measNo,ii)/(*AMatrix)(measNo,biggestColumn);
01583 if( fabs((*AMatrix)(jj,ii)) > ALI_DBL_MIN && fabs(div - (*AMatrix)(jj,ii)/(*AMatrix)(jj,biggestColumn) ) > ALI_DBL_MIN ) {
01584 if( ALIUtils::debug >= 3 ) std::cout << "CheckIfMeasIsProportionalToAnother 2 columns = " << ii << " in " << measNo << " & " << jj << std::endl;
01585 } else {
01586 if( ALIUtils::debug >= 3 ) std::cout << "CheckIfMeasIsProportionalToAnother 2 columns != " << ii << " in " << measNo << " & " << jj << std::endl;
01587
01588 std::set<uint>::iterator ite = columnsEqual.find( ii );
01589 if( ite != columnsEqual.end() ){
01590 columnsEqual.erase(ite);
01591 }
01592 }
01593 }
01594
01595 if( columnsEqual.size() != 0 ) {
01596 if( ALIUtils::debug >= 3 ) std::cout << "CheckIfMeasIsProportionalToAnother " << measNo << " = " << jj << std::endl;
01597 measProp = jj;
01598 break;
01599 }
01600 }
01601
01602 return measProp;
01603 }
01604
01605
01606
01607 std::string Fit::GetMeasurementName( int imeas )
01608 {
01609 std::string measname = " ";
01610
01611 std::cout << " imeas " << imeas << std::endl;
01612 int Aline = 0;
01613 std::vector< Measurement* >::const_iterator vmcite;
01614 for ( vmcite = Model::MeasurementList().begin(); vmcite != Model::MeasurementList().end(); vmcite++) {
01615 for ( ALIuint jj = 0; jj < ALIuint((*vmcite)->dim()); jj++) {
01616 if( Aline == imeas ) {
01617 char ctmp[20];
01618 gcvt( jj, 10, ctmp );
01619 return ((*vmcite)->name()) + ":" + std::string(ctmp);
01620 }
01621 Aline++;
01622 }
01623 }
01624
01625 std::cout << " return measname " << measname << std::endl;
01626 return measname;
01627 }
01628