CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/Alignment/CocoaModel/src/Model.cc

Go to the documentation of this file.
00001 //   COCOA class implementation file
00002 //Id:  Model.cc
00003 //CAT: Model
00004 //
00005 //   History: v1.0 
00006 //   Pedro Arce
00007  
00008 #include "Alignment/CocoaModel/interface/Model.h"
00009 
00010 #include "Alignment/CocoaUtilities/interface/ALIFileIn.h"
00011 #include "Alignment/CocoaUtilities/interface/GlobalOptionMgr.h"
00012 #include "Alignment/CocoaModel/interface/OpticalObject.h"
00013 #include "Alignment/CocoaModel/interface/Measurement.h"
00014 #include "Alignment/CocoaModel/interface/MeasurementSensor2D.h"
00015 #include "Alignment/CocoaModel/interface/MeasurementDistancemeter.h"
00016 #include "Alignment/CocoaModel/interface/MeasurementDistancemeter3dim.h"
00017 #include "Alignment/CocoaModel/interface/MeasurementTiltmeter.h"
00018 #include "Alignment/CocoaModel/interface/MeasurementCOPS.h"
00019 #include "Alignment/CocoaModel/interface/MeasurementDiffEntry.h"
00020 #include "Alignment/CocoaModel/interface/CocoaDaqReaderText.h"
00021 #include "Alignment/CocoaModel/interface/CocoaDaqReaderRoot.h"
00022 //t#include "Alignment/CocoaModel/interface/MeasurementDiffAngle.h"
00023 //t#include "Alignment/CocoaModel/interface/MeasurementCentreEntry.h"
00024 #include "Alignment/CocoaUtilities/interface/ALIUtils.h"
00025 #include "Alignment/CocoaModel/interface/EntryAngle.h"
00026 #include "Alignment/CocoaModel/interface/ParameterMgr.h"
00027 #include "Alignment/CocoaModel/interface/ErrorCorrelationMgr.h"
00028 //#include "Alignment/Scan/interface/ScanMgr.h"
00029 #include "Alignment/CocoaModel/interface/EntryMgr.h"
00030 #include "Alignment/CocoaModel/interface/EntryData.h"
00031 #include "Alignment/CocoaModel/interface/FittedEntriesReader.h"
00032 
00033 #include "CondFormats/OptAlignObjects/interface/OpticalAlignments.h"
00034 #include "CondFormats/OptAlignObjects/interface/OpticalAlignMeasurements.h"
00035 
00036 #include <stdlib.h>
00037 #include <ctype.h>
00038 //#include <algo.h>
00039 #include <assert.h>
00040 #include <time.h>
00041 
00042 #include <algorithm>
00043 
00044 #ifdef OS_OSPACE_STD_NAMESPACE
00045   using namespace os_std;
00046 #endif
00047 
00048 //using namespace os_std;
00049 
00050 
00051 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00052 Model* Model::theInstance = 0;
00053 //map< ALIstring, ALIdouble, std::less<ALIstring> > Model::theParameters;
00054 std::vector< std::vector<ALIstring> > Model::theOptODictionary;
00055 //-map< ALIstring, int, std::less<ALIstring> > Model::theStandardMeasurerTypes;
00056 //-multimap< ALIstring, OpticalObject*, std::less<ALIstring> > Model::_OptOtree;
00057 //map< ALIstring, OpticalObject*, std::less<ALIstring> > Model::theOptOlist;
00058 std::vector< OpticalObject* > Model::theOptOList;
00059 std::vector<Entry*> Model::theEntryVector;
00060 std::vector<Measurement*> Model::theMeasurementVector;
00061 std::vector<ALIdouble> Model::theParamFittedSigmaVector;
00062 std::map<ALIstring, ALIdouble, std::less<ALIstring> > Model::theParamFittedValueDisplacementMap;
00063 std::vector<OpticalObject*> Model::theOptOsToCopyList;
00064 std::vector<OpticalObject*>::const_iterator Model::theOptOsToCopyListIterator;
00065 ALIint Model::CMSLinkIteration = 0;
00066 ALIint Model::Ncmslinkrange = 0;
00067 std::vector<ALIdouble> Model::CMSLinkRangeDetValue;
00068 ALIstring Model::theSDFName = "SystemDescription.txt";
00069 ALIstring Model::theMeasFName = "Measurements.txt";
00070 ALIstring Model::theReportFName = "report.out";
00071 ALIstring Model::theMatricesFName = "matrices.out";
00072 //struct tm Model::theMeasurementsTime = struct tm();
00073 // struct tm Model::theMeasurementsTime;
00074 cocoaStatus Model::theCocoaStatus = COCOA_Init;
00075 FittedEntriesReader* Model::theFittedEntriesReader = 0;
00076 std::vector<OpticalAlignInfo> Model::theOpticalAlignments;
00077 
00078 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00079 //@@  Gets the only instance of Model
00080 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00081 Model& Model::getInstance()
00082 {
00083   if(!theInstance) {
00084     theInstance = new Model;
00085   }
00086   return *theInstance;
00087 }
00088 
00089 
00090 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00091 //@@  Constructor
00092 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00093 Model::Model()
00094 {
00095   //  theMeasurementsTime = clock();
00096 }
00097 
00098 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00099 //@@  Reads the System Description file section by section and acts accordingly
00100 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00101 void Model::readSystemDescription()
00102 {
00103   Model::setCocoaStatus( COCOA_Init );
00104   
00105   ALIint data_reading = 0;  // variable to identify the first line of section SYSTEM_TREE_DATA 
00106   
00107   //---------- Open the system description file
00108   ALIFileIn& filein = ALIFileIn::getInstance( Model::SDFName());
00109  
00110   //----------- Set section titles
00111   std::vector<ALIstring> SectionTitle;
00112   SectionTitle.push_back(ALIstring("GLOBAL_OPTIONS"));
00113   SectionTitle.push_back(ALIstring("PARAMETERS"));
00114   SectionTitle.push_back(ALIstring("SYSTEM_TREE_DESCRIPTION"));
00115   SectionTitle.push_back(ALIstring("SYSTEM_TREE_DATA"));
00116   SectionTitle.push_back(ALIstring("MEASUREMENTS"));
00117   SectionTitle.push_back(ALIstring("REPORT.OUT"));
00118   std::vector<ALIstring>::iterator SectionTitleIterator;
00119 
00120   //---------------------------------------- Loops lines in SDF file  
00121   std::vector<ALIstring> wordlist;
00122   ALIint InSectionNo = -1; 
00123   ALIint currentSectionNo = -1; 
00124   while (!filein.eof()) {
00125 
00126     if( !filein.getWordsInLine(wordlist) ) break;  //----- Read line
00127     assert( wordlist.size() != 0 );
00128 
00129     //----- checking
00130     if( ALIUtils::debug > 99) {
00131       ALIUtils::dumpVS( wordlist, " ", std::cout );
00132     }
00133 
00134     //---------- Get in which section the current line is and act accordingly
00135 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00136 //@@@ ---------- Check if word is start of section 
00137     SectionTitleIterator = find(SectionTitle.begin(), SectionTitle.end(), *wordlist.begin());
00138     if( SectionTitleIterator != SectionTitle.end() ) { 
00139       //---------- Check that previous sections are correct
00140       currentSectionNo = SectionTitleIterator - SectionTitle.begin();
00141       if ( currentSectionNo != InSectionNo + 1 ) {
00142         if( currentSectionNo != sectReportOut ) {
00143           ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00144           std::cerr << "BAD ORDER OF SECTIONS, reading section " 
00145                << *SectionTitleIterator << std::endl 
00146                << " currentSectionNo = " << currentSectionNo << " InSectionNo = " << InSectionNo << std::endl
00147                << " ---------  Please see documentation  ---------- " << std::endl;
00148           exit(1);
00149         }
00150       } else {
00151         if( currentSectionNo != sectReportOut ) {
00152           InSectionNo++;
00153         }
00154       }
00155       if( currentSectionNo == sectMeasurements ) {
00156         SetValueDisplacementsFromReportOut();
00157       }
00158 
00159       if( ALIUtils::debug >= 4) std::cout << std::endl << "START OF SECTION: " << currentSectionNo << " " << *SectionTitleIterator << std::endl;
00160 
00161 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00162 //@@@ ---------- Reading GLOBAL_OPTIONS section
00163     } else if( currentSectionNo == sectGlobalOptions ) {
00164       //-       std::cout << "utilsdebug " << ALIUtils::debug << std::endl;
00165       //-------- Check format of line read
00166       //----- Two words
00167       if( wordlist.size() == 2 ) {
00168         //----- Second word is number
00169         int isnumber = ALIUtils::IsNumber( wordlist[1] );
00170         if (!isnumber && wordlist[0] != ALIstring("external_meas")) {
00171           ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00172           std::cerr << ": EXPECTING A NUMBER, FOUND: " << wordlist[1] << std::endl;
00173           exit(2);
00174         }
00175         
00176         GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00177         gomgr->setGlobalOption( wordlist[0], ALIUtils::getFloat(wordlist[1]), ALIFileIn::getInstance(Model::SDFName()) );
00178  
00179           //-or    GlobalOptions.insert(std::pair<const ALIstring, ALIdouble>(wordlist[0] , atof(wordlist[1].c_str()) ) );
00180         
00181         if( ALIUtils::debug >= 1) {
00182           ALIUtils::dumpVS( wordlist, "GLOBAL_OPTION:  ", std::cout );
00183         }
00184 
00185       } else {
00186         std::cout << "error < 1" << std::endl;
00187         ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00188         std::cerr << ": IN GLOBAL_OPTIONS section TWO-WORD LINES ARE EXPECTED " << std::endl;
00189         exit(2);
00190       }
00191         
00192       //------- Set dimension factors for lengths and angles
00193       ALIUtils::SetLengthDimensionFactors();
00194       ALIUtils::SetAngleDimensionFactors();
00195       ALIUtils::SetOutputLengthDimensionFactors();
00196       ALIUtils::SetOutputAngleDimensionFactors();
00197 
00198 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00199 //@@@ ---------- Reading PARAMETERS section
00200     } else if( currentSectionNo == sectParameters ||
00201                currentSectionNo == -1 ) {  // Allow parameters in first lines (for easy study of different parameter values)
00202 
00203       //---------- Check format of line read
00204       //----- Two words
00205       if( wordlist.size() == 2 ) {
00206         /* now checked in ParameterMgr
00207         //----- Second is number
00208         int isnumber = ALIUtils::IsNumber( wordlist[1] );
00209         if( !isnumber ) {
00210           ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00211           std::cerr << ": EXPECTING A NUMBER, FOUND: " << wordlist[1] << std::endl;
00212           exit(2);
00213           } */
00214         
00215         //old---------- Create parameter with value read (or overwrite existing value)
00216         //---------- Create parameter with value read if parameter does not exist yet
00217         ParameterMgr* parmgr = ParameterMgr::getInstance();
00218         parmgr->addParameter( wordlist[0], wordlist[1] );
00219         
00220       } else if( wordlist.size() == 3 ) {
00221         if( wordlist[1] != "seed" ) { 
00222           ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00223           std::cerr << ": For a three-word parameter line, second has to be 'seed', it is  " << wordlist[1] << std::endl;
00224           exit(1);
00225         } 
00226 
00227         if( wordlist[0] == "gauss" || wordlist[0] == "flat" ){ 
00228           ParameterMgr::getInstance()->setRandomSeed( ALIUtils::getInt( wordlist[2] ));
00229         } else {
00230           ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00231           std::cerr << ": For a three-word parameter line, first has to be 'gauss' or 'flat', it is  " << wordlist[0] << std::endl;
00232           exit(1);
00233         } 
00234       } else if( wordlist.size() == 4 ) {
00235         if( wordlist[0] == "gauss" ) {  
00236           ParameterMgr::getInstance()->addRandomGaussParameter( wordlist[1], wordlist[2], wordlist[3] ); 
00237         } else if( wordlist[0] == "flat" ) {
00238          ParameterMgr::getInstance()->addRandomFlatParameter( wordlist[1], wordlist[2], wordlist[3] ); 
00239         } else {
00240           ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00241           std::cerr << ": For a four-word parameter line, first has to be 'gauss' or 'flat', it is  " << wordlist[0] << std::endl;
00242           exit(1);
00243         } 
00244       } else {
00245         ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00246         std::cerr << ": IN PARAMETERS section TWO-WORD-LINES ARE EXPECTED " << std::endl;
00247         ALIUtils::dumpVS( wordlist , " " );
00248         exit(2);
00249       } 
00250 
00251       //print it out
00252       if( ALIUtils::debug >= 1) { 
00253         ALIUtils::dumpVS( wordlist, "PARAMETERS:  ", std::cout );
00254       }
00255     
00256 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00257 //@@@ ---------- Reading SYSTEM_TREE_DESCRIPTION section
00258     } else if( currentSectionNo == sectSystemTreeDescription ) {
00259 
00260       //---------- Convert the numbers found in list of components (e.g. 2 laser -> laser laser )
00261       //----- Backup wordlist and erase it
00262       std::vector<ALIstring> wordlist2 = wordlist;
00263       std::vector<ALIstring>::iterator vsite;
00264       ALIint wsiz = wordlist.size();
00265       for ( ALIint ii = 0; ii < wsiz; ii++) {
00266         wordlist.pop_back();
00267       }
00268       //----- Loop objects looking for numbers to convert
00269       for ( vsite=wordlist2.begin(); vsite!=wordlist2.end(); vsite++ ) {
00270         if( ALIUtils::IsNumber( *vsite ) ) {
00271           int nOpticalObjects = atoi( (*vsite).c_str() );
00272           //----- If number is 1 it is not necessary
00273           if( nOpticalObjects == 1 ) {
00274             if( ALIUtils::debug >= 0) std::cerr << "WARNING: in line " << ALIFileIn::getInstance( Model::SDFName() ).nline() 
00275                  << " number of repeated OpticalObjects = 1. Please avoid the numbering " << std::endl; 
00276             //-    wordlist.erase( wordlist.begin() + (vsite-wordlist2.begin()) );
00277           } else {
00278             //----- The number cannot be the last in the list and you cannot put two numbers together
00279             if( vsite+1 == wordlist.end() || ALIUtils::IsNumber(*(vsite+1)) ) {
00280               ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00281               std::cerr << "NUMBER SHOULD BE FOLLOWED BY an OpticalObject type" << std::endl;
00282               exit(2);
00283             }
00284             //----- Right format: convert
00285             //--- Substitute the number by the object type in wordlist
00286             //-   *( wordlist.begin() + (vsite-wordlist2.begin()) ) = *(vsite+1);
00287             //--- Add n-1 object types to wordlist (the nth object will be added as the object taht goes after the number)
00288             for ( ALIint ii=0; ii<nOpticalObjects-1; ii++ ) {
00289               //-std::cout << ii << "inserting in wordlist " << *(vsite+1) << std::endl;
00290               wordlist.push_back( *(vsite+1) ); 
00291             }
00292           }
00293         } else {
00294           //----- Not number, add it to wordlist
00295           wordlist.push_back( *vsite );
00296         }
00297       }
00298 
00299       //---------- Dump system structure
00300       if ( ALIUtils::debug >= 1 ) {
00301         ALIUtils::dumpVS( wordlist, "SYSTEM TREE DESCRIPTION: before ordering, OBJECT: ", std::cout );
00302       }
00303 
00304       //---------- Fill the list of Optical Object with components (theOptODictionary)
00305       //----- First word is 'object': new OptO
00306       if ( wordlist[0] == ALIstring("object") ) {
00307           //----- Check out repeated objects
00308         std::vector< std::vector<ALIstring> >::iterator vvsite; 
00309         for ( vvsite = theOptODictionary.begin(); vvsite != theOptODictionary.end(); vvsite++) {
00310           //-     std::cout << " system" << vvsite << std::endl;
00311 
00312           if( *( (*vvsite).begin() ) == wordlist[1] ) {
00313             ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00314             std::cerr << "SYSTEM_TREE_DESCRIPTION: REPEATED object " << *((*vvsite).begin()) << " ( NOT ALLOWED NEITHER WITH EQUAL NOR WITH DIFFERENT COMPONENTS)" << std::endl;
00315             exit(1);
00316           }
00317         }
00318         //------- Add an item to theOptODictionary
00319         std::vector<ALIstring> vstemp;
00320         copy( wordlist.begin()+1, wordlist.end(),back_inserter( vstemp ) );
00321         Model::OptODictionary().push_back(vstemp);
00322       } else {
00323         //----- First word is not 'object': add to previous OptO
00324         if(OptODictionary().size() == 0) {
00325           ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00326           std::cerr << "SYSTEM_TREE_DESCRIPTION section: FIRST LINE SHOULD START WITH 'object'" << std::endl;
00327           exit(2);
00328         }
00329         copy(wordlist.begin(), wordlist.end(),
00330              back_inserter( *(OptODictionary().end()-1) ) );
00331       }
00332  
00333 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00334 //---------------------------------- Reading SYSTEM_TREE_DATA section
00335     } else if( currentSectionNo == sectSystemTreeData ) {
00336       //---------- First line of data:
00337       if ( !data_reading ) {
00338         //        ALIint dictsize = theOptODictionary.size();
00339         //------- Reorder theOptODictionary
00340         std::vector< std::vector<ALIstring> > OptODictionary2; 
00341         reorderOptODictionary( "system", OptODictionary2);
00342         if(OptODictionary2.size() == 0) {
00343           std::cerr << "SYSTEM_TREE_DESCRIPTION section: no object 'system' found " << std::endl;
00344           exit(9);
00345         }
00346         //------- We start reordering with 'system', therefore if 'system' is not the top most object, the objects not hanging from it would not be considered
00347         //---- Check if all the objects are here
00348         std::vector< std::vector<ALIstring> >::const_iterator vvscite, vvscite2; 
00349         //      ALIint dictsizen = 0;
00350         for( vvscite = theOptODictionary.begin(); vvscite != theOptODictionary.end(); vvscite++) {
00351           ALIbool ofound = 0;
00352           for( vvscite2 = OptODictionary2.begin(); vvscite2 != OptODictionary2.end(); vvscite2++) {
00353             if( *( (*vvscite).begin() ) == *( (*vvscite2).begin() ) ) {
00354               ofound = 1;
00355               break;
00356             }
00357           }
00358           if( !ofound  ) {
00359             std::cerr << "!!!SYSTEM_TREE_DESCRIPTION section: object " << *( (*vvscite).begin()) << " is not hanging from object 'system' " << std::endl;
00360             for (vvscite=OptODictionary().begin();vvscite!=OptODictionary().end();vvscite++){
00361               std::vector<ALIstring> ptemp = *vvscite;
00362               ALIUtils::dumpVS( ptemp, "OBJECT ", std::cerr );
00363             }       
00364             exit(9);
00365           }
00366         }
00367         theOptODictionary = OptODictionary2;
00368 
00369         data_reading = 1;
00370 
00371         //------- Dump ordered OptOs
00372         if ( ALIUtils::debug >= 3) {
00373           std::vector< std::vector<ALIstring> >::iterator itevs;
00374           for (itevs=OptODictionary().begin();itevs!=OptODictionary().end();itevs++){
00375             std::vector<ALIstring> ptemp = *itevs;
00376             ALIUtils::dumpVS( ptemp, " SYSTEM TREE DESCRIPTION: after ordering: OBJECT ", std::cout );
00377           }
00378         }
00379 
00380         //---------- Create OpticalObject 'system' (first OpticalObject object): 
00381         //---------- it will create its components and recursively all the System Tree of Optical Objects
00382         if(wordlist[0] != "system" ) {
00383           std::cerr << "SYSTEM_TREE_DATA section: object 'system' is not the first one " << std::endl;
00384           exit(9);
00385         }
00386 
00387         OpticalObject* OptOsystem = new OpticalObject( 0, "system", wordlist[1], 0 );
00388         OptOsystem->construct();
00389             //-              Model::_OptOtree.insert( std::multimap< ALIstring, OpticalObject*, std::less<ALIstring> >::value_type(OptOsystem->type(), OptOsystem) );
00390             //              theOptOlist[OptOsystem->name()] = OptOsystem; 
00391         theOptOList.push_back( OptOsystem ); 
00392        
00393       } else {      
00394         //----------- All system is read by the Optical Objects, it should not reach here
00395         ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00396         std::cerr << " STILL SOME LINES AFTER ALL SYSTEM TREE IS READ!!!"<< std::endl;
00397         exit(9);
00398       }
00399  
00400 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00401 //----------------------------------- Reading MEASUREMENTS section
00402     } else if( currentSectionNo == sectMeasurements ) {
00403       //---------- Create Measurement with appropiate dimension
00404       Measurement* meastemp = 0;
00405       ALIstring measType = wordlist[0];
00406       ALIstring measName;
00407       if( wordlist.size() == 2 ) {
00408         measName = wordlist[1];
00409         wordlist.pop_back();
00410       } else {
00411         measName = "";
00412       }
00413       if ( measType == ALIstring("SENSOR2D") ) {
00414         meastemp = new MeasurementSensor2D( 2, measType, measName );      
00415         meastemp->setConversionFactor( wordlist );
00416         meastemp->construct();
00417       } else if ( measType == ALIstring("DISTANCEMETER3DIM") ) {
00418         meastemp = new MeasurementDistancemeter3dim( 1, measType, measName );
00419         meastemp->setConversionFactor( wordlist );
00420         meastemp->construct();
00421       } else if ( measType == ALIstring("DISTANCEMETER") ||
00422                   measType == ALIstring("DISTANCEMETER1DIM") ) {
00423         meastemp = new MeasurementDistancemeter( 1, measType, measName );
00424         meastemp->setConversionFactor( wordlist );
00425         meastemp->construct();
00426       } else if ( measType == ALIstring("TILTMETER") ) {
00427         meastemp = new MeasurementTiltmeter( 1, measType, measName );
00428         meastemp->setConversionFactor( wordlist );
00429         meastemp->construct();
00430       } else if ( measType == ALIstring("COPS") ) {
00431         meastemp = new MeasurementCOPS( 4, measType, measName );
00432         meastemp->setConversionFactor( wordlist );
00433         meastemp->construct();
00434       } else if ( measType == ALIstring("DIFFCENTRE") ) {
00435  //t       meastemp = new MeasurementDiffCentre( 1, measType, measName );
00436         meastemp->construct();
00437       } else if ( measType == ALIstring("DIFFANGLE") ) {
00438 //t        meastemp = new MeasurementDiffAngle( 2, measType, measName );
00439         meastemp->construct();
00440       } else if ( measType == ALIstring("DIFFENTRY") ) {
00441         meastemp = new MeasurementDiffEntry( 1, measType, measName );
00442         meastemp->construct();
00443       } else if ( measType == ALIstring("measurements_from_file") || measType == ALIstring("@measurements_from_file") ) {
00444         new CocoaDaqReaderText( wordlist[1] );
00445         //m Measurement::setMeasurementsFileName( wordlist[1] );
00446         //m if ( ALIUtils::debug >= 2) std::cout << " setting measurements_from_file " << measType << " == " << Measurement::measurementsFileName() << std::endl;
00447         if( wordlist.size() == 4) {
00448           Measurement::only1 = 1; 
00449           Measurement::only1Date = wordlist[2]; 
00450           Measurement::only1Time = wordlist[3]; 
00451           //-      std::cout << " setting Measurement::only1" <<  Measurement::only1 << std::endl;
00452         }
00453       } else if ( measType == ALIstring("measurements_from_file_ROOT") || measType == ALIstring("@measurements_from_file") ) {
00454         new CocoaDaqReaderRoot( wordlist[1] );
00455       } else if ( wordlist[0] == ALIstring("correlations_from_file") || wordlist[0] == ALIstring("@correlations_from_file") ) {
00456         ErrorCorrelationMgr::getInstance()->readFromReportFile( wordlist[1] );
00457       } else if ( wordlist[0] == ALIstring("copy_measurements") || wordlist[0] == ALIstring("@copy_measurements") ) {
00458         copyMeasurements( wordlist );
00459         //      } else if( wordlist[0] == "scan" || wordlist[0] == "@scan" ) {
00460         //      ScanMgr::getInstance()->addOptOEntry( wordlist );
00461       } else if ( wordlist[0] == ALIstring("fittedEntries_from_file") ) {
00462         theFittedEntriesReader = new FittedEntriesReader( wordlist[1] );
00463          if ( ALIUtils::debug >= 2) std::cout << " setting fittedEntries_from_file " << wordlist[0] << " == " << wordlist[1] << std::endl;
00464       } else {
00465         std::cerr << "Measurement:" << std::endl;
00466         ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00467         std::cerr << "!!! type of measurement not allowed: " << wordlist[0] 
00468              << std::endl;
00469         std::cerr << " Allowed types: SENSOR2D, DISTANCEMETER, DISTANCEMETER1DIM, TILTMETER, COPS, DIFFENTRY " << std::endl; 
00470         exit(2);
00471       }
00472 //-      meastemp->setGlobalName( wordlist[0] );
00473 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00474 //@@@ ---------- Reading REPORT OUT  section
00475     } else if( currentSectionNo == sectReportOut ) {
00476       //----- It must be after global options section
00477       if ( InSectionNo+1 != sectParameters ) {
00478         ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00479         std::cerr << "BAD ORDER OF SECTIONS, reading section " 
00480              << *SectionTitleIterator << std::endl 
00481              << " currentSectionNo = " << currentSectionNo << " InSectionNo = " << InSectionNo << std::endl
00482              << " ---------  Please see documentation  ---------- " << std::endl;
00483         exit(1);
00484       }
00485 
00486       EntryMgr::getInstance()->readEntryFromReportOut( wordlist );
00487     }
00488   }
00489 
00490 
00491 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00492 //@@@ All system read
00493   //---------- make Measurements links to their OptOs 
00494   if ( ALIUtils::debug >= 2) std::cout << std::endl << "@@@@ Building Measurements links to OptOs" << std::endl;
00495   Model::buildMeasurementsLinksToOptOs();
00496 
00497   if ( ALIUtils::debug >= 1) {
00498       std::cout << "----------  SYSTEM SUCCESFULLY READ ----------" << std::endl << std::endl;
00499   }  
00500   filein.close();
00501 
00502   return;
00503   
00504 }
00505 
00506 
00507 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00508 //@@  reorderOptODictionary: REBUILDS THE SYSTEM_TREE_DESCRIPTION SECTION 'objects' 
00509 //@@ (_OptODictionary) IN A HIERARCHICAL (TREE LIKE) ORDER
00510 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00511 void Model::reorderOptODictionary(const ALIstring& ssearch, std::vector< std::vector<ALIstring> >& OptODictionary2 ){
00512 
00513   //  int ALIstring_found = 0;
00514   std::vector< std::vector<ALIstring> >::iterator vvsite; 
00515   std::vector< ALIstring >::iterator vsite; 
00516 
00517 //---------------------------------------- Look for 'ssearch' as the first ALIstring of an OptODictionary item
00518   for( vvsite = OptODictionary().begin(); vvsite != OptODictionary().end(); ++vvsite) {
00519    if( *( (*vvsite).begin() ) == ssearch ) {
00520      //     ALIstring_found = 1;
00521      OptODictionary2.push_back(*vvsite);
00522  
00523      //-    std::cout << "VVSITE";
00524      //-   ostream_iterator<ALIstring> outs(std::cout,"&");
00525      //-   copy( (*vvsite).begin(), (*vvsite).end(), outs);
00526  
00527 //---------------------------------- look for components of this _OptODictionary item
00528      for(  vsite = (*vvsite).begin()+1; vsite != (*vvsite).end(); ++vsite ) {
00529        reorderOptODictionary( *vsite, OptODictionary2 );
00530      }
00531      break;
00532    }
00533   }
00534  
00535   /*  //------- object 'system' should exist
00536   if(!ALIstring_found && ssearch == "system") {
00537     std::cerr << "SYSTEM_TREE_DATA section: no 'object system' found " << std::endl;
00538     exit(9);
00539     } */
00540 
00541 } 
00542 
00543 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00544 //@@ BuildMeasurementLinksToOptOs
00545 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00546 void Model::buildMeasurementsLinksToOptOs(){
00547 
00548 //---------- Loop Measurements
00549   std::vector< Measurement* >::const_iterator vmcite;
00550   for ( vmcite = MeasurementList().begin(); vmcite != MeasurementList().end(); vmcite++) {
00551 //---------- Transform for each Measurement the Measured OptO names to Measured OptO pointers
00552     //     (*vmcite)->buildOptOList();
00553 
00554 //---------- Build list of Entries that affect a Measurement 
00555     // (*vmcite)->buildAffectingEntryList();
00556 
00557   }
00558  
00559 }
00560 
00561 
00562 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00563 //@@ Get the value of a parameter in theParameters std::vector
00564 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00565 ALIint Model::getParameterValue( const ALIstring& sstr, ALIdouble& val ) 
00566 {
00567   ParameterMgr* parmgr = ParameterMgr::getInstance();
00568   ALIint iret = parmgr->getParameterValue( sstr, val );
00569 
00570   return iret;
00571 
00572 }
00573 
00574 
00575 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00576 //@@ getOptOByName: Find an OptO name in _OptOlist and return a pointer to it
00577 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00578 OpticalObject* Model::getOptOByName( const ALIstring& opto_name ) 
00579 {
00580   //---------- Look for Optical Object name in OptOList
00581   std::vector< OpticalObject* >::const_iterator vocite;
00582   for( vocite = OptOList().begin(); vocite != OptOList().end(); vocite++ ) {
00583     if( (*vocite)->name() == opto_name ) break;
00584   }
00585 
00586   if ( vocite == OptOList().end() ) {
00587     //---------- If opto_name not found, exit
00588     std::cerr << " LIST OF OpticalObjects " << std::endl;
00589     for( vocite = OptOList().begin(); vocite != OptOList().end(); vocite++ ) {
00590       std::cerr <<  (*vocite)->name() << std::endl;
00591     }
00592     std::cerr << "!!EXITING at getOptOByName: Optical Object " << opto_name << " doesn't exist!!" << std::endl;
00593     exit(4);
00594        //       return (OpticalObject*)0;
00595   } else {
00596     //---------- If opto_name found, return pointer to it
00597     if( ALIUtils::debug>999) {
00598       std::cout << opto_name.c_str() << "SSOptOitem" << (*vocite) << (*vocite)->name() << "len" << OptOList().size() << std::endl;
00599     }
00600     return (*vocite);
00601   } 
00602 
00603 }
00604 
00605 
00606 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00607 //@@ getOptOByType: Find an OptO type in _OptOList (the first one with this type)
00608 //@@ and return a pointer to it
00609 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00610 OpticalObject* Model::getOptOByType( const ALIstring& opto_type ) 
00611 {
00612   //---------- Look for Optical Object type in OptOList
00613   std::vector< OpticalObject* >::const_iterator vocite;
00614   for( vocite = OptOList().begin(); vocite != OptOList().end(); vocite++ ) {
00615    //   std::cout << "OPTOList" << (*msocite).first << std::endl;
00616     if( (*vocite)->type() == opto_type ) break;
00617   }
00618 
00619   if ( vocite == OptOList().end() ) {
00620     //---------- If opto_type not found, exit
00621     std::cerr << "!!EXITING at getOptOByType: Optical Object " << opto_type << " doesn't exist!!" << std::endl;
00622     exit(4);
00623   } else {
00624     //---------- If opto_type found, return pointer to it
00625     return (*vocite);
00626  } 
00627 
00628 }
00629 
00630 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00631 //@@  Find an Entry name in theEntryVector and return a pointer to it
00632 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00633 Entry* Model::getEntryByName( const ALIstring& opto_name, const ALIstring& entry_name )
00634 {
00635   //---------- Look for Entry name type in EntryList
00636   std::vector<Entry*>::const_iterator vecite;
00637   for( vecite = EntryList().begin(); vecite != EntryList().end(); vecite++) {
00638     if( ALIUtils::debug >= 4 ) std::cout <<  "getEntryByName: " <<(*vecite)->OptOCurrent()->name() 
00639          << " E " << (*vecite)->name() << " Searching: " << opto_name << " E " << entry_name << std::endl; 
00640     //-    std::cout << " optoName " << (*vecite)->OptOCurrent()->name()<< " " << (*vecite)->name() << std::endl;
00641     if( (*vecite)->OptOCurrent()->name() == opto_name &&
00642         (*vecite)->name() == entry_name ) {
00643       return *vecite;
00644     }
00645   }
00646   //---------- Entry not found!
00647   std::cerr << "!!!EXITING at getEntryByName: Entry name not found:" << opto_name << "  " << entry_name << std::endl; 
00648   exit(1);
00649 }
00650 
00651 
00652 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00653 Measurement* Model::getMeasurementByName( const ALIstring& meas_name, ALIbool exists ) 
00654 {
00655   //---------- Look for Optical Object name in OptOList
00656   std::vector< Measurement* >::const_iterator vmcite;
00657   for( vmcite = theMeasurementVector.begin(); vmcite != theMeasurementVector.end(); vmcite++ ) {
00658     if( (*vmcite)->name() == meas_name ) break;
00659   }
00660   
00661   if ( vmcite != theMeasurementVector.end() ) {
00662     //---------- If opto_name found, return pointer to it
00663     return (*vmcite);
00664   }else {
00665     if( exists ) {
00666       //---------- If opto_name not found, exit
00667       std::cerr << " LIST OF Measurements " << std::endl;
00668       for( vmcite = theMeasurementVector.begin(); vmcite != theMeasurementVector.end(); vmcite++ ) {
00669         std::cerr << (*vmcite)->name() << std::endl;
00670       }
00671       std::cerr << "!!EXITING at getMeasurementByName: Measurement " << meas_name << " doesn't exist!!" << std::endl;
00672       abort();
00673       //       return (OpticalObject*)0;
00674     } else {
00675       return 0;
00676     }
00677 
00678   } 
00679 
00680   
00681 }
00682 
00683 
00684 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00685 //@@  Get an OptO list of components 
00686 //@@  Looks the theOptODictionary item that has 'opto_type' as the first ALIstring,
00687 //@@  copies this item to 'vcomponents', substracting the first ALIstring, that is the opto_type itself,
00688 //@@  Returns 1 if item found, 0 if not 
00689 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00690 ALIbool Model::getComponentOptOTypes( const ALIstring& opto_type, std::vector<ALIstring>& vcomponents )
00691 {
00692   //---------- clean std::vector in which you are going to store opto types
00693   std::vector<ALIstring>::iterator vsite;
00694   for (vsite = vcomponents.begin(); vsite != vcomponents.end(); vsite++) {
00695     vcomponents.pop_back();
00696   }
00697  
00698   //---------- Looks the theOptODictionary item that has 'opto_type' as the first ALIstring,
00699   ALIint ALIstring_found = 0;
00700   std::vector< std::vector<ALIstring> >::iterator vvsite; 
00701   for( vvsite = OptODictionary().begin(); vvsite != OptODictionary().end(); ++vvsite) {
00702     if( *( (*vvsite).begin() ) == opto_type ) {
00703       ALIstring_found = 1;
00704       //tt  copies this item to 'vcomponents', substracting the first ALIstring, that is the opto_type itself,
00705       vcomponents = *vvsite;
00706       vcomponents.erase( vcomponents.begin() );
00707       break;
00708     }
00709   }
00710   
00711   if ( ALIstring_found ) {
00712     return 1;
00713   } else {
00714     return 0;
00715   } 
00716 } 
00717 
00718 
00719 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00720 //@@ Get from _OptOList the list of OptOs pointers that has as parent 'opto_name' 
00721 //@@ and store it in vcomponents
00722 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00723 ALIbool Model::getComponentOptOs( const ALIstring& opto_name, std::vector<OpticalObject*>& vcomponents) 
00724 {
00725   //---------- clean std::vector in which you are going to store opto pointers
00726   std::vector<OpticalObject*>::iterator voite;
00727   for (voite = vcomponents.begin(); voite != vcomponents.end(); voite++) {
00728     vcomponents.pop_back();
00729   }
00730 
00731   //---------- Get OptO corresponding to name 'opto_name'
00732   OpticalObject* opto = getOptOByName( opto_name );
00733  
00734   if ( ALIUtils::debug >= 99) std::cout << opto_name << "getComponentOptOs: opto " << opto << opto->name() << std::endl;
00735   std::vector<OpticalObject*>::const_iterator vocite;
00736 
00737   if ( ALIUtils::debug >= 99) std::cout << "optolist size " << OptOList().size() << std::endl;
00738   ALIbool opto_found = 0; 
00739   for (vocite = OptOList().begin(); vocite != OptOList().end(); vocite++) {
00740     if( (*vocite)->parent() != 0 ) {
00741       //        std::cout << "looping OptOlist" << (*vocite)->name() << " parent " <<(*vocite)->parent()->name() << std::endl; 
00742       if( (*vocite)->parent()->name() == opto_name ) {
00743         opto_found = 1;
00744         vcomponents.push_back( (*vocite) );    
00745       }
00746     }
00747   }
00748     
00749   /*  std::pair<multimap< ALIstring, OpticalObject*, std::less<ALIstring> >::iterator,
00750        std::multimap< ALIstring, OpticalObject*, std::less<ALIstring> >::iterator> 
00751   pmmao =  _OptOtree.equal_range(opto_name);
00752 
00753   if( pmmao.first != _OptOtree.end()) {
00754     std::multimap< ALIstring, OpticalObject*, std::less<ALIstring> >::const_iterator socite;
00755     for (socite = pmmao.first; socite != (pmmao.second); socite++) {
00756          vcomponents.push_back( (*socite).second );
00757     }
00758   }
00759   */
00760   return opto_found;
00761 
00762 }
00763 
00764 
00766 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00767 //@@ createCopyComponentList:
00768 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00769 ALIbool Model::createCopyComponentList( const ALIstring& typ )
00770 {
00771   //---------- Find an OptO with the same type (YOU HAVE TO BE SURE THAT ALL EXISTING OPTOs OF THIS TYPE HAVE SAME COMPONENTS, IF NOT COPYING MAY GIVE YOU UNPREDICTABLE RESULTS)
00772   if(ALIUtils::debug >= 3) std::cout << "createCopyComponentList " << typ << std::endl; 
00773   OpticalObject* start_opto = getOptOByType( typ );
00774   
00775   //---------- clean list of OptOs to copy
00776   theOptOsToCopyList.erase( theOptOsToCopyList.begin(), theOptOsToCopyList.end() );
00777   
00778   //---------- Fill list of OptOs to copy
00779   fillCopyComponentList( start_opto );
00780   //- if(ALIUtils::debug >= 9) std::cout << "createCopyComponentList " << typ << theOptOsToCopyList.size() << std::endl; 
00781   
00782   theOptOsToCopyListIterator = theOptOsToCopyList.begin();
00783   return 1;
00784 }
00785 
00786 
00787 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00788 //@@ fillCopyOptOList: Fill list of objects to copy with the components of 'opto'
00789 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00790 ALIbool Model::fillCopyComponentList( const OpticalObject* opto )
00791 {
00792   if(ALIUtils::debug >= 3) std::cout << "entering fillCopyComponentList(): OptO" << opto->name() << std::endl;
00793   //---------- Get list of components of 'opto'
00794   std::vector<OpticalObject*> vopto; 
00795   ALIbool opto_found = getComponentOptOs( opto->name(), vopto);
00796   if( !opto_found ) {
00797     if(ALIUtils::debug >= 5) std::cout << "fillCopyComponentList: NO COMPONENTS TO COPY IN THIS OptO" << opto->name() << std::endl ; 
00798   }
00799   
00800   //---------- Loop list of components of 'opto'
00801   std::vector<OpticalObject*>::const_iterator vocite;
00802   for( vocite = vopto.begin(); vocite != vopto.end(); vocite++ ){
00803     theOptOsToCopyList.push_back( *vocite );
00804     if(ALIUtils::debug >= 5) std::cout << "fillCopyOptOList " << (*vocite)->type() << " " << (*vocite)->name() << std::endl; 
00805     //---------- Add components of this component
00806     fillCopyComponentList( *vocite );
00807   }
00808   return opto_found;
00809 }
00810 
00811 
00812 
00813 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00814 //@@ nextOptOToCopy: return next object to copy from theOptOsToCopyListIterator
00815 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00816 OpticalObject* Model::nextOptOToCopy()
00817 {
00818   if(ALIUtils::debug >= 5) std::cout << "entering nextOptOToCopy() " << std::endl;
00819   theOptOsToCopyListIterator++;
00820   //  if(ALIUtils::debug >= 5) std::cout <<" nextOptOToCopy " << (*(theOptOsToCopyListIterator-1))->name() << std::endl;
00821   return *(theOptOsToCopyListIterator-1);
00822 }
00823 
00824 
00826 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00827 //@@ If fitting CMS link, it has to be fitted part by part, in several iterations
00828 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00829 void Model::CMSLinkFit( ALIint cmslink)
00830 {
00831   /*
00832   //---------- Get number of fits 
00833   ALIint cmslinkNoFits = 0;
00834   if( cmslink == 1) {
00835     cmslinkNoFits = 3;
00836   } else if( cmslink == 2) {
00837     cmslinkNoFits = 2;
00838   }
00839   if(ALIUtils::debug >= 4) std::cout << " Start CMS link fit with method " << cmslink << " (number of fits = " << cmslinkNoFits << ")" << std::endl;
00840 
00841   //----------- Loop number of cms_link fits 
00842   for(ALIint ilink = ALIint(Model::GlobalOptions()["cms_link"]); ilink <= cmslinkNoFits; ilink++) { 
00843 
00844   //----- Iteration 2 of method 2 resembles iteration 3 of method 1
00845      ALIdouble cmslink_method;
00846      assert(Model::getGlobalOptionValue("cms_link_method", cmslink_method)); 
00847      if( cmslink_method == 2 && ilink == 2) ilink = 3;
00848 
00849 //---- Set variable CMSLinkIteration, Checked in Fit.C and other     //- std::cout << "ilink" << ilink << std::endl;
00850     Model::setGlobalOption("cms_link", ilink); 
00851     Model::CMSLinkIteration = ilink;
00852 
00853     if(ilink > 1)Model::readSystemDescription(); //already read once to fill value Model::GlobalOptions()["cms_link"]
00854 
00855     //---------- Delete the OptO not fitted in this iteration
00856     //    Model::CMSLinkDeleteOptOs(); 
00857     // cannot be here because you may recover a parameter saved in previous iteration that now it is deleted (!!MODIFY THIS)  
00858 
00859     //---------- Recover parameters fitted in previous iteration
00860     Model::CMSLinkRecoverParamFittedSigma( ilink );
00861 
00862     Model::CMSLinkRecoverParamFittedValueDisplacement( ilink );
00863 
00864     //---------- Delete the OptO not fitted in this iteration
00865     Model::CMSLinkDeleteOptOs();
00866   
00867     //---------- Start fit
00868     Fit::startFit();
00869 
00870     //---------- Save parameters fitted in this iteration (to be used in next one)
00871     Model::CMSLinkSaveParamFittedSigma( ilink );
00872 
00873     Model::CMSLinkSaveParamFittedValueDisplacement( ilink );
00874 
00875     //---------- Delete whole system to start anew in next iteration
00876     Model::CMSLinkCleanModel();
00877 
00878   }    
00879   */
00880 }
00881 
00882 
00883 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00884 //@@ CMSLinkCleanModel: clean Model for new iteration while fitting CMS link 
00885 //@@ part by part
00886 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00887 void Model::CMSLinkCleanModel()
00888 {
00889   deleteOptO("s");
00890   //---------- Clean OptOdicitionary (in case this is not first reading)
00891   ALIuint odsize = theOptODictionary.size(); 
00892   for( ALIuint ii = 0; ii < odsize; ii++) {
00893       theOptODictionary.pop_back(); 
00894   }
00895 
00896 }
00897 
00898 
00899 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00900 //@@ CMSLinkDeleteOptOs
00901 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00902 void Model::CMSLinkDeleteOptOs( )
00903 {
00904   ALIint cmslink_iter = Model::CMSLinkIteration; 
00905   ALIdouble cmslink_method;
00906 
00907   GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00908   assert(gomgr->getGlobalOptionValue("cms_link_method", cmslink_method));
00909   ALIdouble cmslink_halfplanes;
00910   assert(gomgr->getGlobalOptionValue("cms_link_halfplanes", cmslink_halfplanes));
00911   if(ALIUtils::debug >= 2) std::cout << "CMSLinkDeleteOptOs: cms_link_halfplanes " << cmslink_halfplanes <<  cmslink_iter  << std::endl;
00912 
00913   if( cmslink_iter == 1){
00914     //---------- First fit: delete everything but laser1 and det_tkU
00915     //    deleteOptO("s/laserboxL/laser2");
00916     //-    std::cout << "delete mabs" << std::endl;
00917     deleteOptO("s/mabsL");
00918     //------- Method 1: detectors at tracker down
00919     if (cmslink_method == 1){
00920       deleteOptO("s/tracker/det_trkDL");
00921       deleteOptO("s/tracker/det_trkDR");
00922     }
00923 
00924     if( cmslink_halfplanes == 2) { 
00925       //      deleteOptO("s/laserboxR/laser2");
00926       deleteOptO("s/mabsR");
00927     }
00928 
00929   } else if( cmslink_iter == 2) {
00930     //---------- Second fit (method 1): delete everything but laser1 and det3
00931     //    deleteOptO("s/laserboxL/laser2");
00932     deleteOptO("s/mabsL");
00933     deleteOptO("s/tracker/CST/wheel_trkL/peri/mirror"); //??
00934     deleteOptO("s/tracker/CST/wheel_trkL/det_trkU");
00935     //------- Method 1: detectors on CST, Method 2: detectors on tracker
00936       //not necessary    deleteOptO("s/tracker/CST/det6");
00937 
00938     if( cmslink_halfplanes <= 1) {  
00939       deleteOptO("s/tracker/CST/wheel_trkR");
00940     } else if( cmslink_halfplanes == 2) { 
00941       //      deleteOptO("s/laserboxR/laser2");
00942       deleteOptO("s/mabsR");
00943       deleteOptO("s/tracker/CST/wheel_trkR/peri/mirror"); //??
00944       deleteOptO("s/tracker/CST/wheel_trkR/det_trkU");
00945     }
00946 
00947   } else if( cmslink_iter == 3) {
00948     //---------- Third fit: delete everything but laser2 and mabs
00949     //    deleteOptO("s/laserboxL/laser1");
00950     deleteOptO("s/tracker");
00951 
00952     if( cmslink_halfplanes == 2) {  
00953       //      deleteOptO("s/laserboxR/laser1");
00954     }
00955   //---------- Do nothing
00956   } else {
00957 
00958   }
00959 
00960 }
00961 
00962 
00963 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00964 //@@ CMSLinkSaveParamFittedSigma
00965 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00966 void Model::CMSLinkSaveParamFittedSigma( ALIint cms_link )
00967 {
00968   /*
00969   ALIdouble cms_link_halfplanes = (GlobalOptions()["cms_link_halfplanes"]);
00970 
00971   cleanParamFittedSigmaVector(); 
00972 
00973   //---------- First fit: Save laserbox angles fitted at tracker 
00974   if( cms_link == 1) {
00975     //?    if (GlobalOptions()["cms_link_method"] < 10){
00976     saveParamFittedSigma("s/laserboxL","Angles X");   
00977     saveParamFittedSigma("s/laserboxL","Angles Y");
00978 
00979     saveParamFittedCorrelation("s/laserboxL","Angles X",
00980      "s/tracker/CST","Centre Y");
00981     saveParamFittedCorrelation("s/laserboxL","Angles Y",
00982      "s/tracker/CST","Centre X");
00983     if( cms_link_halfplanes == 2) {  
00984       saveParamFittedSigma("s/laserboxR","Angles X");   
00985       saveParamFittedSigma("s/laserboxR","Angles Y");
00986 
00987       saveParamFittedCorrelation("s/laserboxR","Angles X",
00988        "s/tracker/CST","Centre Y");
00989       saveParamFittedCorrelation("s/laserboxR","Angles Y",
00990        "s/tracker/CST","Centre X");
00991     }
00992 
00993   } else if( cms_link == 2) {
00994   //---------- Second fit: Save laserbox angles and position
00995     saveParamFittedSigma("s/laserboxL","Centre X");
00996     saveParamFittedSigma("s/laserboxL","Centre Y");
00997     // Make quality unk to cal
00998     saveParamFittedSigma("s/laserboxL","Centre Z");
00999     saveParamFittedSigma("s/laserboxL","Angles X");
01000     saveParamFittedSigma("s/laserboxL","Angles Y");
01001    
01002     saveParamFittedCorrelation("s/laserboxL","Centre X",
01003         "s/laserboxL","Angles Y");
01004     saveParamFittedCorrelation("s/laserboxL","Centre Y",
01005         "s/laserboxL","Angles X");
01006 
01007    if( cms_link_halfplanes == 2) {  
01008     saveParamFittedSigma("s/laserboxR","Centre X");
01009     saveParamFittedSigma("s/laserboxR","Centre Y");
01010     // Make quality unk to cal
01011     saveParamFittedSigma("s/laserboxR","Centre Z");
01012     saveParamFittedSigma("s/laserboxR","Angles X");
01013     saveParamFittedSigma("s/laserboxR","Angles Y");
01014    
01015     saveParamFittedCorrelation("s/laserboxR","Centre X",
01016         "s/laserboxR","Angles Y");
01017     saveParamFittedCorrelation("s/laserboxR","Centre Y",
01018         "s/laserboxR","Angles X");
01019    }  
01020   } else {
01021  //---------- Do nothing
01022 
01023   }
01024   */
01025 }
01026 
01027 
01028 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01029 //@@ CMSLinkSaveParamFittedValueDisplacement: 
01030 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01031 void Model::CMSLinkSaveParamFittedValueDisplacement( ALIint cms_link )
01032 {
01033   /*
01034   cleanParamFittedValueDisplacementMap(); 
01035 
01036   //---------- 
01037   if( cms_link == 1 || cms_link == 2 ) {
01038     std::vector<Entry*>::const_iterator vecite;
01039     for( vecite = EntryList().begin(); vecite != EntryList().end(); vecite++) {
01040       if( (*vecite)->valueDisplacementByFitting() != 0 ) {
01041         ALIstring names = (*vecite)->OptOCurrent()->name() + "/" + (*vecite)->name();
01042         std::cout << "saeParamFittedValueDisplacementMap" << names << (*vecite)->valueDisplacementByFitting() << std::endl;
01043         theParamFittedValueDisplacementMap[ names ] = (*vecite)->valueDisplacementByFitting();
01044       }
01045     }
01046 
01047   //---------- Do nothing
01048   } else {
01049 
01050   }
01051   */
01052 }
01053 
01054 
01055 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01056 //@@ CMSLinkRecoverParamFittedSigma:
01057 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01058 void Model::CMSLinkRecoverParamFittedSigma( ALIint cms_link )
01059 {
01060   /*
01061   ALIdouble cms_link_halfplanes = (GlobalOptions()["cms_link_halfplanes"]);
01062 
01063   if( cms_link == 2) {
01064   //---------- Second fit: recover laserbox angles
01065     recoverParamFittedSigma("s/laserboxL","Angles X",0);
01066     recoverParamFittedSigma("s/laserboxL","Angles Y",1);
01067 
01068     if( cms_link_halfplanes == 2) {  
01069       recoverParamFittedSigma("s/laserboxR","Angles X",0);
01070       recoverParamFittedSigma("s/laserboxR","Angles Y",1);
01071     }
01072 
01073   } else if( cms_link == 3) {
01074   //---------- Third fit: recover laserbox angles and position and rotate angles to mabs
01075     recoverParamFittedSigma("s/laserboxL","Centre X",0);
01076     recoverParamFittedSigma("s/laserboxL","Centre Y",1);
01077     recoverParamFittedSigma("s/laserboxL","Centre Z",2);
01078     recoverParamFittedSigma("s/laserboxL","Angles X",3);
01079 
01080     //----- Angle around Y is converted to angle around Z when turning 90 deg
01081     Entry* slaZ = getEntryByName("s/laserboxL","Angles Z");
01082     //--- prec_level_laser
01083     Entry* smaZ = getEntryByName("s/mabsL","Angles Z");
01084     slaZ->setQuality(0);
01085     slaZ->setValue( smaZ->value() );
01086     //    smaZ->setQuality(0); //!!???!!?
01087 
01088     Entry* slaY = getEntryByName("s/laserboxL","Angles Y");
01089     slaY->setQuality(0); 
01090 
01091     if( cms_link_halfplanes == 2) {   
01092       recoverParamFittedSigma("s/laserboxR","Centre X",0);
01093       recoverParamFittedSigma("s/laserboxR","Centre Y",1);
01094       recoverParamFittedSigma("s/laserboxR","Centre Z",2);
01095       recoverParamFittedSigma("s/laserboxR","Angles X",3);
01096 
01097       //----- Angle around Y is converted to angle around Z when turning 90 deg
01098       Entry* slaZ = getEntryByName("s/laserboxR","Angles Z");
01099       //--- prec_level_laser
01100       Entry* smaZ = getEntryByName("s/mabsR","Angles Z");
01101       slaZ->setQuality(0);
01102       slaZ->setValue( smaZ->value() );
01103       //    smaZ->setQuality(0); //!!???!!?
01104 
01105       Entry* slaY = getEntryByName("s/laserboxR","Angles Y");
01106       slaY->setQuality(0); 
01107     }
01108   } else {
01109   //---------- Do nothing
01110 
01111   }
01112   */
01113 }
01114 
01115 
01116 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01117 //@@ CMSLinkRecoverParamFittedValueDisplacement:
01118 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01119 void Model::CMSLinkRecoverParamFittedValueDisplacement( ALIint cms_link )
01120 {
01121   /*
01122   //---------- Second fit: recover laserbox angles
01123   if( cms_link == 2 || cms_link == 3 ) {
01124 
01125     std::map<ALIstring, ALIdouble, std::less<ALIstring> >::const_iterator vsdmite;
01126     if ( ALIUtils::debug >= 99) std::cout << "theParamFittedValueDisplacementMap.size " << theParamFittedValueDisplacementMap.size() << std::endl;
01127     for( vsdmite =  theParamFittedValueDisplacementMap.begin(); vsdmite !=  theParamFittedValueDisplacementMap.end(); vsdmite++) {
01128       std::cout << "reoverValueDisp" <<  (*vsdmite).first << "  " << (*vsdmite).second << std::endl;
01129       Entry* this_entry = getEntryByName( (*vsdmite).first);
01130       this_entry->displaceOriginal( (*vsdmite).second ); 
01131       this_entry->OptOCurrent()->resetGlobalCoordinates();
01132       this_entry->setValueDisplacementByFitting(  (*vsdmite).second ); 
01133 
01134     }
01135 
01136   //---------- Do nothing
01137   } else {
01138 
01139   }
01140   */
01141 }
01142 
01143 
01144 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01145 //@@  Delete an OptO
01146 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01147 void Model::deleteOptO( const ALIstring& opto_name )
01148 {
01149   OpticalObject* opto = getOptOByName( opto_name );
01150   deleteOptO( opto );
01151 }
01152 
01153 
01154 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01155 //@@  deleteOptO: delete OptO, its Entries, and the Measurements in which it participates. Then deleteOptO of components
01156 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01157 void Model::deleteOptO( OpticalObject* opto )
01158 {
01159   if(ALIUtils::debug >= 5 ) std::cout << "DELETING OptO" << opto->name() << std::endl;
01160   
01161   //---------- Delete Coordinate Entries of this OptO
01162   std::vector<Entry*>::const_iterator vecite;
01163   std::vector<Entry*>::iterator veite2;
01164   if ( ALIUtils::debug >= 9) std::cout << "SIZE" << theEntryVector.size() <<std::endl;
01165   for( vecite = opto->CoordinateEntryList().begin(); vecite != opto->CoordinateEntryList().end(); vecite++) {
01166     //    ALIuint pos = FindItemInVector( (*veite), opto->CoordinateEntryList() );
01167     veite2 = find( theEntryVector.begin(), theEntryVector.end(), (*vecite) );
01168     //-  if ( ALIUtils::debug >= 9) std::cout << (*veite2) << "DELETE ENTRY " << (*vecite) <<(*veite2)->OptOCurrent()->name() << (*veite2)->name() << std::endl;
01169     delete ( (*veite2) );
01170     theEntryVector.erase( veite2 );
01171   }
01172 
01173   for( vecite = opto->ExtraEntryList().begin(); vecite != opto->ExtraEntryList().end(); vecite++) {
01174     //    ALIuint pos = FindItemInVector( (*veite), opto->CoordinateEntryList() );
01175     veite2 = find( theEntryVector.begin(), theEntryVector.end(), (*vecite) );
01176     //-    if(ALIUtils::debug >= 9) std::cout << (*veite2) << "DELETE ENTRY " << (*veite2)->OptOCurrent()->name() << (*veite2)->name() << std::endl;
01177     delete ( (*veite2) );
01178     theEntryVector.erase( veite2 );
01179   }
01180 
01181  
01182   for( vecite = theEntryVector.begin(); vecite != theEntryVector.end(); vecite++ ) {
01183     //     std::cout << (*vecite) << "ENTReY " << (*vecite)->OptOCurrent()->name() << (*vecite)->name() << std::endl;
01184   }
01185 
01186   //---------- Delete all Measurement in which opto takes part
01187   std::vector<Measurement*> MeasToBeDeleted;
01188   std::vector<Measurement*>::const_iterator vmite;
01189   std::vector<OpticalObject*>::const_iterator vocite;
01190   for( vmite = MeasurementList().begin(); vmite != MeasurementList().end(); vmite++ ) {
01191       if(ALIUtils::debug >= 5) std::cout << "Deleting Measurement" << (*vmite)->name() << std::endl;
01192     //----- If any of the OptO Measured is opto, delete this Measurement
01193     for( vocite = (*vmite)->OptOList().begin(); vocite != (*vmite)->OptOList().end(); vocite++) {
01194       if( (*vocite) == opto ) {
01195         //-      std::cout << "MEASTBD" << (*vmite) << std::endl;
01196         MeasToBeDeleted.push_back( *vmite );
01197         //?       delete (*vmite);
01198         break;
01199       }
01200     } 
01201   }
01202 
01203  //---------- Delete Measurements from list
01204   std::vector<Measurement*>::const_iterator vmcite;
01205   std::vector<Measurement*>::iterator vmite2;
01206   if ( ALIUtils::debug >= 9) std::cout << "SIZEMEAS" << MeasToBeDeleted.size() <<std::endl;
01207   for( vmcite = MeasToBeDeleted.begin(); vmcite != MeasToBeDeleted.end(); vmcite++) {
01208     vmite2 = find( theMeasurementVector.begin(), theMeasurementVector.end(), (*vmcite) );
01209     //    std::cout << (*vmite2) << "DELETE MSEASU " << (*vmcite) << (*vmite2)->name()[0] << std::endl;
01210     delete ( (*vmite2) );
01211     theMeasurementVector.erase( vmite2 );
01212   }
01213 
01214 
01215   //---------- Delete components
01216   //  std::vector<OpticalObject*>::iterator voite;
01217   std::vector<OpticalObject*> vopto;
01218   //  ALIbool opto_found = getComponentOptOs( opto->name(), vopto );
01219   for( vocite = vopto.begin(); vocite != vopto.end(); vocite++ ) {
01220     deleteOptO( *vocite );
01221   }
01222      
01223   //---------- Delete this OptO
01224   //---------- Delete OptO (only from list, to delete it really first delete components)
01225   /*  map< ALIstring, OpticalObject*, std::less<ALIstring> >::iterator msoite =  
01226 find( theOptOList.begin(), theOptOList.end(), 
01227 map< ALIstring, OpticalObject*, std::less<ALIstring> >::value_type( opto->name(), opto) );*/
01228   std::vector< OpticalObject* >::iterator dvoite =  
01229    find( theOptOList.begin(), theOptOList.end(), 
01230          std::vector< OpticalObject* >::value_type( opto) );
01231   //-  std::cout << (*dvoite) << "DELETE OPTO " << opto <<"WW" << (*dvoite)->name() << std::endl;
01232   theOptOList.erase( dvoite );
01233   delete opto;
01234 
01235 }
01236 
01237 
01238 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01239 //@@  
01240 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01241 void Model::saveParamFittedSigma( const ALIstring& opto_name, const ALIstring& entry_name)
01242 {
01243   /*
01244 //---------- Get sigma of param Optical
01245   Entry* entry = getEntryByName(opto_name, entry_name);
01246   ALIuint fit_pos = entry->fitPos();
01247   if( fit_pos < 0 || fit_pos >= Fit::propagationMatrix().NoLines()) {
01248     std::cerr << "!!EXITING at saveParamFittedSigma: fit position incorrect " <<
01249       fit_pos << "propagationMatrix size =" << Fit::propagationMatrix().NoLines() << opto_name << std::endl;
01250     exit(3);
01251   }
01252   std::cout << entry_name << "saveParamFittedSigma" << fit_pos << sqrt(Fit::propagationMatrix()( fit_pos, fit_pos)) << std::endl;
01253   theParamFittedSigmaVector.push_back( sqrt(Fit::propagationMatrix()( fit_pos, fit_pos)) );
01254   //-    Fit::propagationMatrix().Dump(" the 2PropagationMatrix");
01255   */
01256 }
01257 
01258 
01259 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01260 //@@  
01261 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01262 void Model::saveParamFittedCorrelation( const ALIstring& opto_name1, const ALIstring& entry_name1, const ALIstring& opto_name2, const ALIstring& entry_name2)
01263 {
01264   /*
01265 //---------- Get sigma of param Optical
01266   Entry* entry1 = getEntryByName(opto_name1, entry_name1);
01267   Entry* entry2 = getEntryByName(opto_name2, entry_name2);
01268   ALIuint fit_pos1 = entry1->fitPos();
01269   ALIuint fit_pos2 = entry2->fitPos();
01270 
01271   //---------- Get correlation if entry has been fitted
01272   ALIuint pmsize = Fit::propagationMatrix().NoLines();
01273   if( fit_pos1 >= 0 && fit_pos1 < pmsize && fit_pos2 >= 0 && fit_pos2 < pmsize ) {
01274     ALIdouble error1 = sqrt( Fit::propagationMatrix()( fit_pos1, fit_pos1) );
01275     ALIdouble error2 = sqrt( Fit::propagationMatrix()( fit_pos2, fit_pos2) );
01276     ALIdouble correl = Fit::propagationMatrix()( fit_pos1, fit_pos2) / error1 / error2;
01277     theParamFittedSigmaVector.push_back( correl );
01278     if(ALIUtils::debug>=9) {
01279       std::cout  << "saveParamFittedCorre" << opto_name1 << entry_name1 << fit_pos1 << 
01280         opto_name2 << entry_name2 << fit_pos2 << "MM " << correl << std::endl;
01281     }
01282   } else {
01283     if(ALIUtils::debug>=9) {
01284       std::cout  << "NOsaveParamFittedCorre" << opto_name1 << entry_name1 << fit_pos1 << 
01285         opto_name2 << entry_name2 << fit_pos2 << "MM " << std::endl;
01286     theParamFittedSigmaVector.push_back( 0. );
01287     }
01288 
01289   }
01290   //-    Fit::propagationMatrix().Dump(" the 2PropagationMatrix");
01291   */
01292 }
01293 
01294 
01295 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01296 //@@  
01297 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01298 void Model::recoverParamFittedSigma( const ALIstring& opto_name, const ALIstring& entry_name, const ALIuint position )
01299 {
01300   /*
01301   if( position >= theParamFittedSigmaVector.size() ) {
01302     std::cerr << "!!EXITING at recoverParamFittedSigma: position" << position << 
01303  " bigger than dimension of theParamFittedSigmaVector " << theParamFittedSigmaVector.size() << std::endl;
01304     exit(3);
01305   }
01306   ALIdouble sigma = getParamFittedSigmaVectorItem( position );
01307 
01308   Entry* entry = getEntryByName(opto_name, entry_name);
01309   entry->setSigma( sigma );
01310   entry->setQuality( 1 );
01311   std::cout << "recover " << opto_name << entry_name << entry->sigma() <<std::endl;
01312   */
01313 }
01314 
01315 
01316 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01317 //@@  getParamFittedSigmaVectorItem
01318 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01319 ALIdouble Model::getParamFittedSigmaVectorItem( const ALIuint position )
01320 {
01321   if( position >= theParamFittedSigmaVector.size() ) {
01322     std::cerr << "!!EXITING at getParamFittedSigma: position" << position << 
01323  " bigger than dimension of theParamFittedSigmaVector " << theParamFittedSigmaVector.size() << std::endl;
01324     exit(3);
01325   }
01326   std::vector<ALIdouble>::const_iterator vdcite = theParamFittedSigmaVector.begin() + position;
01327   return (*vdcite);
01328 
01329 }
01330  
01331 
01332 
01333 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01334 //@@  getParamFittedSigmaVectorItem
01335 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01336 ALIbool Model::readMeasurementsFromFile(ALIstring only1Date, ALIstring only1Time )
01337 {
01338  if(ALIUtils::debug >= 5) std::cout << " readMeasurementsFromFile " << Measurement::measurementsFileName() << std::endl;
01339   if( Measurement::measurementsFileName() == "") return 1;
01340 
01341   ALIFileIn& filein = ALIFileIn::getInstance( Measurement::measurementsFileName() );
01342   std::vector<ALIstring> wordlist;
01343   //---------- read date
01344   //  ALIint retfil = filein.getWordsInLine(wordlist);
01345   // std::cout << "@@@@@@@@@@@@@@@ RETFIL " << retfil << std::endl;
01346   //if( retfil == 0 ) {
01347   if( filein.getWordsInLine(wordlist) == 0 ) {
01348     if(ALIUtils::debug>=4 ) std::cout << "@@@@ No more measurements left" << std::endl;
01349     return 0; 
01350   }
01351 
01353   //  struct tm tim;
01354   //t Model::setMeasurementsTime( tim );
01355 
01356   //if you are looking for only one measurement with a given date and time, loop all measurements until you find it
01357   if( Measurement::only1 ) {
01358     for(;;) {
01359       if( wordlist[0] == "DATE:" && wordlist[1] == Measurement::only1Date && wordlist[2] == Measurement::only1Time ) break;
01360       filein.getWordsInLine(wordlist); 
01361       if(filein.eof() ) {
01362         std::cerr << "!! EXITING date not found in measurements file" << Measurement::only1Date << " " <<  Measurement::only1Time << std::endl;
01363         exit(1);
01364       }
01365     }
01366   }
01367 
01368   //set date and time of current measurement
01369   if( wordlist[0] == "DATE:" ) {
01370     Measurement::setCurrentDate( wordlist ); 
01371   } 
01372 
01373   //---------- loop measurements
01374   ALIint nMeas = Model::MeasurementList().size();
01375   if(ALIUtils::debug >= 4) {
01376     std::cout << " Reading " << nMeas << " measurements from file " << Measurement::measurementsFileName() 
01377          << " DATE: " << wordlist[1] << " " << wordlist[1] << std::endl;
01378   }
01379   ALIint ii;
01380   for(ii = 0; ii < nMeas; ii++) {
01381     filein.getWordsInLine(wordlist);  
01382     if( wordlist[0] == ALIstring("SENSOR2D") || wordlist[0] == ALIstring("TILTMETER") || wordlist[0] == ALIstring("DISTANCEMETER")  || wordlist[0] == ALIstring("DISTANCEMETER1DIM")  || wordlist[0] == ALIstring("COPS") ) {
01383       if( wordlist.size() != 2 ) {
01384         std::cerr << "!!!EXITING Model::readMeasurementsFromFile. number of words should be 2 instead of " << wordlist.size() << std::endl;
01385         ALIUtils::dumpVS( wordlist, " " );
01386         exit(1);
01387       }
01388       std::vector< Measurement* >::const_iterator vmcite;
01389       for( vmcite = MeasurementList().begin();  vmcite != MeasurementList().end(); vmcite++ ) {
01390         //-------- Measurement found, fill data
01391         /*      ALIint last_slash =  (*vmcite)->name().rfind('/');
01392         ALIstring oname = (*vmcite)->name();
01393         if( last_slash != -1 ) {
01394           oname = oname.substr(last_slash+1, (*vmcite)->name().size()-1);
01395           } 
01396         */
01397         ALIint fcolon = (*vmcite)->name().find(':');
01398         ALIstring oname = (*vmcite)->name();
01399         oname = oname.substr(fcolon+1,oname.length());
01400         //-    std::cout << " measurement name " << (*vmcite)->name() << " short " << oname << std::endl;
01401         if( oname == wordlist[1] ) {
01402           //-   std::cout << " measurement name found " << oname << std::endl;
01403           if( (*vmcite)->type() != wordlist[0] ) {
01404             std::cerr << "!!! Reading measurement from file: type in file is " 
01405                  << wordlist[0] << " and should be " << (*vmcite)->type() << std::endl;
01406             exit(1);
01407           }
01408           Measurement* meastemp = *vmcite;
01409           
01410           GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
01411           ALIbool sigmaFF = gomgr->GlobalOptions()["measurementErrorFromFile"];
01412           //---------- Read the data 
01413           for ( ALIuint ii=0; ii < meastemp->dim(); ii++){
01414             filein.getWordsInLine( wordlist );
01415             ALIdouble sigma = 0.;
01416             if( !sigmaFF ) { 
01417               // keep the sigma, do not read it from file 
01418               const ALIdouble* sigmav = meastemp->sigma();
01419               sigma = sigmav[ii];
01420             }
01421             //---- Check measurement value type is OK
01422             if( meastemp->valueType(ii) != wordlist[0] ) {
01423               filein.ErrorInLine();
01424               std::cerr << "!!!FATAL ERROR: Measurement value type is " << wordlist[0] << " while in setup definition was " << meastemp->valueType(ii) << std::endl;
01425               exit(1);
01426             }
01427             meastemp->fillData( ii, wordlist );
01428             if( !sigmaFF ) { 
01429               meastemp->setSigma( ii, sigma );
01430             }
01431           }
01432           meastemp->correctValueAndSigma();
01433           break;
01434         }
01435       }
01436       if( vmcite == MeasurementList().end() ) {
01437         for( vmcite = MeasurementList().begin(); vmcite != MeasurementList().end(); vmcite++ ) {
01438           std::cerr << "MEAS: " << (*vmcite)->name() << " " << (*vmcite)->type() << std::endl;
01439         }
01440         std::cerr << "!!! Reading measurement from file: measurement not found in list: type in file is "  << wordlist[1]  << std::endl;
01441         exit(1);
01442       }
01443     } else {
01444       std::cerr << " wrong type of measurement: " << wordlist[0] << std::endl
01445            << " Available types are SENSOR2D, TILTMETER, DISTANCEMETER, DISTANCEMETER1DIM, COPS" << std::endl;
01446       exit(1);
01447     }
01448   }
01449   //-  std::cout << " returning readmeasff" << std::endl;
01450 
01451   return 1;
01452 }
01453 
01454 
01455 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01456 
01457 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01458 void Model::copyMeasurements( const std::vector<ALIstring>& wl )
01459 {
01460  
01461   //----- Check format, e.g.  @copy_measurements _1/_2/*_1 
01462 
01463 
01464   //----- get list of Measurement's that satisfy the query in their name
01465   //t  std::string querystr = wl[1].substr( wl[1].find("/")+1, wl[1].length() );
01466   std::string subsstr1 = wl[1].substr( 0, wl[1].find("/") );
01467   std::string subsstr2 = wl[1].substr( wl[1].find("/")+1, wl[1].rfind("/") - wl[1].find("/")-1 );
01468   std::string querystr = wl[1].substr( wl[1].rfind("/")+1, wl[1].length() );
01469 
01470   std::cout << " Model::copyMeasurements " << " subsstr1 " << subsstr1 << " subsstr2 " << subsstr2 << " querystr " << querystr << std::endl;
01471 
01472   std::vector< Measurement* > measToCopy;
01473   std::vector< Measurement* >::iterator mite;
01474   for( mite = theMeasurementVector.begin(); mite != theMeasurementVector.end(); mite++) {
01475     Measurement* meas = (*mite);
01476     //improve this
01477     if( meas->name().find( querystr ) != std::string::npos ) {
01478       measToCopy.push_back( meas );
01479     }
01480   }
01481 
01482   //---- Build new measurements
01483   Measurement* meastemp = 0;
01484   for( mite = measToCopy.begin(); mite != measToCopy.end(); mite++) {
01485     Measurement* meas = (*mite);
01486     std::vector<ALIstring> wlt;
01487     wlt.push_back( meas->type() );
01488 
01489     //---- create new name 
01490     std::string newName = ALIUtils::changeName( meas->name(), subsstr1, subsstr2 );
01491     std::cout << " newName " << newName << std::endl;
01492     wlt.push_back( newName );
01493 
01494     ALIstring measType = wlt[0];
01495     ALIstring measName;
01496     if( wlt.size() == 2 ) {
01497       measName = wlt[1];
01498     } else {
01499       measName = "";
01500     }
01501     if ( meas->type() == ALIstring("SENSOR2D") ) {
01502       meastemp = new MeasurementSensor2D( 2, measType, measName );      
01503       //          } else if ( meas->type() == ALIstring("DISTANCEMETER3DIM") ) {
01504       //            meastemp = new MeasurementDistancemeter3dim( 1, measType, measName );
01505     } else if ( meas->type() == ALIstring("DISTANCEMETER") ||
01506                 meas->type() == ALIstring("DISTANCEMETER1DIM") ) {
01507       meastemp = new MeasurementDistancemeter( 1, measType, measName );
01508     } else if ( meas->type() == ALIstring("TILTMETER") ) {
01509       meastemp = new MeasurementTiltmeter( 1, measType, measName );
01510     } else if ( meas->type() == ALIstring("DIFFCENTRE") ) {
01511  //     meastemp = new MeasurementDiffCentre( 1, measType, measName );
01512     } else if ( meas->type() == ALIstring("DIFFANGLE") ) {
01513 //      meastemp = new MeasurementDiffAngle( 1, measType, measName );
01514     } else if ( meas->type() == ALIstring("DIFFENTRY") ) {
01515       meastemp = new MeasurementDiffEntry( 1, measType, measName );
01516     } else if ( meas->type() == ALIstring("COPS") ) {
01517       meastemp = new MeasurementCOPS( 4, measType, measName );
01518     }
01519 
01520     //later        meastemp->copyConversionFactor( wordlist );
01521     meastemp->copyMeas(meas, subsstr1, subsstr2);
01522     
01523     break;
01524   }
01525 
01526 }
01527 
01528 
01529 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01530 void Model::SetValueDisplacementsFromReportOut()
01531 {
01532   if( ALIUtils::debug >= 3 ) std::cout << " Model::SetValueDisplacementsFromReportOut() " << std::endl;
01533   
01534   EntryMgr* entryMgr = EntryMgr::getInstance();
01535   
01536   if( entryMgr->numberOfEntries() != 0 ){
01537     EntryData* entryData;
01538     
01539     std::vector<Entry*>::const_iterator vecite; 
01540     for ( vecite = Model::EntryList().begin();
01541           vecite != Model::EntryList().end(); vecite++ ) {
01542       //----- Find the EntryData corresponding to this entry
01543       entryData = entryMgr->findEntryByLongName( (*vecite)->OptOCurrent()->longName(), (*vecite)->name() );
01544       if( ALIUtils::debug >= 3 ) std::cout << "SetValueDisplacementsFromReportOut "<< (*vecite)->OptOCurrent()->longName() << " " <<  (*vecite)->name() << " " << entryData->valueDisplacement() << std::endl;
01545       (*vecite)->addFittedDisplacementToValue( entryData->valueDisplacement() );
01546     }   
01547   }
01548   
01549 }
01550 
01551 
01552 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01553 std::string Model::printCocoaStatus(const cocoaStatus cs)
01554 {
01555   std::string str ="";
01556 
01557   if( cs == COCOA_Init ) {
01558     str = "COCOA_Init ";
01559   }else if( cs == COCOA_ReadingModel) {
01560     str = "COCOA_ReadingModel";
01561   }else if( cs == COCOA_InitFit) {
01562     str = "COCOA_InitFit";
01563   }else if( cs == COCOA_FitOK) {
01564     str = "COCOA_FitOK";
01565   }else if( cs == COCOA_FitImproving) {
01566     str = "COCOA_FitImproving";
01567   }else if( cs == COCOA_FitCannotImprove) {
01568     str = "COCOA_FitCannotImprove";
01569   }else if( cs == COCOA_FitChi2Worsened) {
01570     str = "COCOA_FitChi2Worsened";
01571   }else if( cs == COCOA_FitMatrixNonInversable) {
01572     str = "COCOA_FitMatrixNonInversable";
01573   }
01574 
01575   return str;
01576 }
01577 
01578 
01579 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01580 void Model::BuildSystemDescriptionFromOA( OpticalAlignments& optAlig )
01581 {
01582   theOpticalAlignments = optAlig.opticalAlignments();
01583 
01584   OpticalAlignInfo oai_system = FindOptAlignInfoByType( "system" );
01585 
01586   OpticalObject* OptOsystem = new OpticalObject( 0, "system", oai_system.name_, 0 );
01587  
01588   OptOsystem->constructFromOptAligInfo( oai_system );
01589 
01590   //-              Model::_OptOtree.insert( std::multimap< ALIstring, OpticalObject*, std::less<ALIstring> >::value_type(OptOsystem->type(), OptOsystem) );
01591   //              theOptOlist[OptOsystem->name()] = OptOsystem; 
01592   theOptOList.push_back( OptOsystem ); 
01593 }
01594 
01595 
01596 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01597 OpticalAlignInfo Model::FindOptAlignInfoByType( const ALIstring& type )
01598 {
01599   OpticalAlignInfo oai;
01600 
01601   ALIbool bFound = 0;
01602   std::vector<OpticalAlignInfo>::iterator ite;
01603   for( ite = theOpticalAlignments.begin(); ite != theOpticalAlignments.end(); ite++ ){
01604     //    std::cout << " Model::FindOptAlignInfoByType " <<  (*ite).type_ << " =? " << type << std::endl;
01605     if( (*ite).type_ == type ) {
01606       if( !bFound ){ 
01607         oai = *ite;
01608         bFound = 1;
01609       } else {
01610         std::cerr << "!! WARNING: Model::FindOptAlignInfoByType more than one objects of type " << type << std::endl;
01611         std::cerr << " returning object " << oai.name_ << std::endl
01612                   << " skipping object " << (*ite).name_ << std::endl;
01613       }
01614     }
01615   }
01616   if( !bFound ) { 
01617     std::cerr << "!! ERROR: Model::FindOptAlignInfoByType object not found, of type " << type << std::endl;
01618     std::exception();
01619   }
01620 
01621   return oai;
01622 }
01623 
01624 
01625 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
01626 void Model::BuildMeasurementsFromOA( OpticalAlignMeasurements& measList )
01627 {
01628   std::vector<OpticalAlignMeasurementInfo>::iterator mite;
01629 
01630   if( ALIUtils::debug >= 5 ) std::cout << " BuildMeasurementsFromOA " << std::endl;
01631   std::vector<OpticalAlignMeasurementInfo> measInfos = measList.oaMeasurements_;
01632   for( mite = measInfos.begin(); mite != measInfos.end(); mite++ ) {
01633     std::string measType = (*mite).type_;
01634     std::string measName = (*mite).name_;
01635   if( ALIUtils::debug >= 4 ) std::cout << " BuildMeasurementsFromOA measType " << measType << " measName " << measName << std::endl;
01636     //---------- Create Measurement with appropiate dimension
01637     Measurement* meastemp = 0;
01638     if ( measType == ALIstring("SENSOR2D") ) {
01639       meastemp = new MeasurementSensor2D( 2, measType, measName );  
01640     } else if ( measType == ALIstring("DISTANCEMETER3DIM") ) {
01641       meastemp = new MeasurementDistancemeter3dim( 1, measType, measName );
01642     } else if ( measType == ALIstring("DISTANCEMETER") ||
01643                 measType == ALIstring("DISTANCEMETER1DIM") ) {
01644       meastemp = new MeasurementDistancemeter( 1, measType, measName );
01645     } else if ( measType == ALIstring("TILTMETER") ) {
01646       meastemp = new MeasurementTiltmeter( 1, measType, measName );
01647     } else if ( measType == ALIstring("COPS") ) {
01648       meastemp = new MeasurementCOPS( 4, measType, measName );
01649     } else if ( measType == ALIstring("DIFFCENTRE") ) {
01650       //t       meastemp = new MeasurementDiffCentre( 1, measType, measName );
01651     } else if ( measType == ALIstring("DIFFANGLE") ) {
01652       //t        meastemp = new MeasurementDiffAngle( 2, measType, measName );
01653     } else if ( measType == ALIstring("DIFFENTRY") ) {
01654       meastemp = new MeasurementDiffEntry( 1, measType, measName );
01655     } else {
01656       std::cerr << " !!! Model::BuildMeasurementsFromOA : measType not found " << measType << std::endl;
01657     std::exception();
01658     } 
01659     meastemp->constructFromOA( *mite );
01660 
01661   }
01662 }