CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/Alignment/CocoaFit/src/Fit.cc

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