Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:02

0001 //   COCOA class implementation file
0002 //Id:  Model.cc
0003 //CAT: Model
0004 //
0005 //   History: v1.0
0006 //   Pedro Arce
0007 
0008 #include "Alignment/CocoaModel/interface/Model.h"
0009 
0010 #include "Alignment/CocoaUtilities/interface/ALIFileIn.h"
0011 #include "Alignment/CocoaUtilities/interface/GlobalOptionMgr.h"
0012 #include "Alignment/CocoaModel/interface/OpticalObject.h"
0013 #include "Alignment/CocoaModel/interface/Measurement.h"
0014 #include "Alignment/CocoaModel/interface/MeasurementSensor2D.h"
0015 #include "Alignment/CocoaModel/interface/MeasurementDistancemeter.h"
0016 #include "Alignment/CocoaModel/interface/MeasurementDistancemeter3dim.h"
0017 #include "Alignment/CocoaModel/interface/MeasurementTiltmeter.h"
0018 #include "Alignment/CocoaModel/interface/MeasurementCOPS.h"
0019 #include "Alignment/CocoaModel/interface/MeasurementDiffEntry.h"
0020 #include "Alignment/CocoaModel/interface/CocoaDaqReaderText.h"
0021 #include "Alignment/CocoaModel/interface/CocoaDaqReaderRoot.h"
0022 #include "Alignment/CocoaUtilities/interface/ALIUtils.h"
0023 #include "Alignment/CocoaModel/interface/EntryAngle.h"
0024 #include "Alignment/CocoaModel/interface/ParameterMgr.h"
0025 #include "Alignment/CocoaModel/interface/ErrorCorrelationMgr.h"
0026 //#include "Alignment/Scan/interface/ScanMgr.h"
0027 #include "Alignment/CocoaModel/interface/EntryMgr.h"
0028 #include "Alignment/CocoaModel/interface/EntryData.h"
0029 #include "Alignment/CocoaModel/interface/FittedEntriesReader.h"
0030 
0031 #include "CondFormats/OptAlignObjects/interface/OpticalAlignments.h"
0032 #include "CondFormats/OptAlignObjects/interface/OpticalAlignMeasurements.h"
0033 
0034 #include "FWCore/Utilities/interface/Exception.h"
0035 
0036 #include <cstdlib>
0037 #include <cctype>
0038 //#include <algo.h>
0039 #include <cassert>
0040 #include <ctime>
0041 
0042 #include <algorithm>
0043 
0044 #ifdef OS_OSPACE_STD_NAMESPACE
0045 using namespace os_std;
0046 #endif
0047 
0048 //using namespace os_std;
0049 
0050 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0051 Model* Model::theInstance = nullptr;
0052 //map< ALIstring, ALIdouble, std::less<ALIstring> > Model::theParameters;
0053 std::vector<std::vector<ALIstring> > Model::theOptODictionary;
0054 //-map< ALIstring, int, std::less<ALIstring> > Model::theStandardMeasurerTypes;
0055 //-multimap< ALIstring, OpticalObject*, std::less<ALIstring> > Model::_OptOtree;
0056 //map< ALIstring, OpticalObject*, std::less<ALIstring> > Model::theOptOlist;
0057 std::vector<OpticalObject*> Model::theOptOList;
0058 std::vector<Entry*> Model::theEntryVector;
0059 std::vector<Measurement*> Model::theMeasurementVector;
0060 std::vector<ALIdouble> Model::theParamFittedSigmaVector;
0061 std::map<ALIstring, ALIdouble, std::less<ALIstring> > Model::theParamFittedValueDisplacementMap;
0062 std::vector<OpticalObject*> Model::theOptOsToCopyList;
0063 std::vector<OpticalObject*>::const_iterator Model::theOptOsToCopyListIterator;
0064 ALIint Model::CMSLinkIteration = 0;
0065 ALIint Model::Ncmslinkrange = 0;
0066 std::vector<ALIdouble> Model::CMSLinkRangeDetValue;
0067 ALIstring Model::theSDFName = "SystemDescription.txt";
0068 ALIstring Model::theMeasFName = "Measurements.txt";
0069 ALIstring Model::theReportFName = "report.out";
0070 ALIstring Model::theMatricesFName = "matrices.out";
0071 //struct tm Model::theMeasurementsTime = struct tm();
0072 // struct tm Model::theMeasurementsTime;
0073 cocoaStatus Model::theCocoaStatus = COCOA_Init;
0074 FittedEntriesReader* Model::theFittedEntriesReader = nullptr;
0075 std::vector<OpticalAlignInfo> Model::theOpticalAlignments;
0076 
0077 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0078 //@@  Gets the only instance of Model
0079 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0080 Model& Model::getInstance() {
0081   if (!theInstance) {
0082     theInstance = new Model;
0083   }
0084   return *theInstance;
0085 }
0086 
0087 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0088 //@@  Constructor
0089 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0090 Model::Model() {
0091   //  theMeasurementsTime = clock();
0092 }
0093 
0094 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0095 //@@  Reads the System Description file section by section and acts accordingly
0096 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0097 void Model::readSystemDescription() {
0098   Model::setCocoaStatus(COCOA_Init);
0099 
0100   ALIint data_reading = 0;  // variable to identify the first line of section SYSTEM_TREE_DATA
0101 
0102   //---------- Open the system description file
0103   ALIFileIn& filein = ALIFileIn::getInstance(Model::SDFName());
0104 
0105   //----------- Set section titles
0106   std::vector<ALIstring> SectionTitle;
0107   SectionTitle.push_back(ALIstring("GLOBAL_OPTIONS"));
0108   SectionTitle.push_back(ALIstring("PARAMETERS"));
0109   SectionTitle.push_back(ALIstring("SYSTEM_TREE_DESCRIPTION"));
0110   SectionTitle.push_back(ALIstring("SYSTEM_TREE_DATA"));
0111   SectionTitle.push_back(ALIstring("MEASUREMENTS"));
0112   SectionTitle.push_back(ALIstring("REPORT.OUT"));
0113   std::vector<ALIstring>::iterator SectionTitleIterator;
0114 
0115   //---------------------------------------- Loops lines in SDF file
0116   std::vector<ALIstring> wordlist;
0117   ALIint InSectionNo = -1;
0118   ALIint currentSectionNo = -1;
0119   while (!filein.eof()) {
0120     if (!filein.getWordsInLine(wordlist))
0121       break;  //----- Read line
0122     assert(!wordlist.empty());
0123 
0124     //----- checking
0125     if (ALIUtils::debug > 99) {
0126       ALIUtils::dumpVS(wordlist, " ", std::cout);
0127     }
0128 
0129     //---------- Get in which section the current line is and act accordingly
0130     //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0131     //@@@ ---------- Check if word is start of section
0132     SectionTitleIterator = find(SectionTitle.begin(), SectionTitle.end(), *wordlist.begin());
0133     if (SectionTitleIterator != SectionTitle.end()) {
0134       //---------- Check that previous sections are correct
0135       currentSectionNo = SectionTitleIterator - SectionTitle.begin();
0136       if (currentSectionNo != InSectionNo + 1) {
0137         if (currentSectionNo != sectReportOut) {
0138           ALIFileIn::getInstance(Model::SDFName()).ErrorInLine();
0139           std::cerr << "BAD ORDER OF SECTIONS, reading section " << *SectionTitleIterator << std::endl
0140                     << " currentSectionNo = " << currentSectionNo << " InSectionNo = " << InSectionNo << std::endl
0141                     << " ---------  Please see documentation  ---------- " << std::endl;
0142           exit(1);
0143         }
0144       } else {
0145         if (currentSectionNo != sectReportOut) {
0146           InSectionNo++;
0147         }
0148       }
0149       if (currentSectionNo == sectMeasurements) {
0150         SetValueDisplacementsFromReportOut();
0151       }
0152 
0153       if (ALIUtils::debug >= 4)
0154         std::cout << std::endl << "START OF SECTION: " << currentSectionNo << " " << *SectionTitleIterator << std::endl;
0155 
0156       //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0157       //@@@ ---------- Reading GLOBAL_OPTIONS section
0158     } else if (currentSectionNo == sectGlobalOptions) {
0159       //-       std::cout << "utilsdebug " << ALIUtils::debug << std::endl;
0160       //-------- Check format of line read
0161       //----- Two words
0162       if (wordlist.size() == 2) {
0163         //----- Second word is number
0164         int isnumber = ALIUtils::IsNumber(wordlist[1]);
0165         if (!isnumber && wordlist[0] != ALIstring("external_meas")) {
0166           ALIFileIn::getInstance(Model::SDFName()).ErrorInLine();
0167           std::cerr << ": EXPECTING A NUMBER, FOUND: " << wordlist[1] << std::endl;
0168           exit(2);
0169         }
0170 
0171         GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
0172         gomgr->setGlobalOption(wordlist[0], ALIUtils::getFloat(wordlist[1]), ALIFileIn::getInstance(Model::SDFName()));
0173 
0174         //-or    GlobalOptions.insert(std::pair<const ALIstring, ALIdouble>(wordlist[0] , atof(wordlist[1].c_str()) ) );
0175 
0176         if (ALIUtils::debug >= 1) {
0177           ALIUtils::dumpVS(wordlist, "GLOBAL_OPTION:  ", std::cout);
0178         }
0179 
0180       } else {
0181         std::cout << "error < 1" << std::endl;
0182         ALIFileIn::getInstance(Model::SDFName()).ErrorInLine();
0183         std::cerr << ": IN GLOBAL_OPTIONS section TWO-WORD LINES ARE EXPECTED " << std::endl;
0184         exit(2);
0185       }
0186 
0187       //------- Set dimension factors for lengths and angles
0188       ALIUtils::SetLengthDimensionFactors();
0189       ALIUtils::SetAngleDimensionFactors();
0190       ALIUtils::SetOutputLengthDimensionFactors();
0191       ALIUtils::SetOutputAngleDimensionFactors();
0192 
0193       //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0194       //@@@ ---------- Reading PARAMETERS section
0195     } else if (currentSectionNo == sectParameters ||
0196                currentSectionNo ==
0197                    -1) {  // Allow parameters in first lines (for easy study of different parameter values)
0198 
0199       //---------- Check format of line read
0200       //----- Two words
0201       if (wordlist.size() == 2) {
0202         /* now checked in ParameterMgr
0203         //----- Second is number
0204         int isnumber = ALIUtils::IsNumber( wordlist[1] );
0205         if( !isnumber ) {
0206           ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
0207           std::cerr << ": EXPECTING A NUMBER, FOUND: " << wordlist[1] << std::endl;
0208           exit(2);
0209           } */
0210 
0211         //old---------- Create parameter with value read (or overwrite existing value)
0212         //---------- Create parameter with value read if parameter does not exist yet
0213         ParameterMgr* parmgr = ParameterMgr::getInstance();
0214         parmgr->addParameter(wordlist[0], wordlist[1]);
0215 
0216       } else if (wordlist.size() == 3) {
0217         if (wordlist[1] != "seed") {
0218           ALIFileIn::getInstance(Model::SDFName()).ErrorInLine();
0219           std::cerr << ": For a three-word parameter line, second has to be 'seed', it is  " << wordlist[1]
0220                     << std::endl;
0221           exit(1);
0222         }
0223 
0224         if (wordlist[0] == "gauss" || wordlist[0] == "flat") {
0225           ParameterMgr::getInstance()->setRandomSeed(ALIUtils::getInt(wordlist[2]));
0226         } else {
0227           ALIFileIn::getInstance(Model::SDFName()).ErrorInLine();
0228           std::cerr << ": For a three-word parameter line, first has to be 'gauss' or 'flat', it is  " << wordlist[0]
0229                     << std::endl;
0230           exit(1);
0231         }
0232       } else if (wordlist.size() == 4) {
0233         if (wordlist[0] == "gauss") {
0234           ParameterMgr::getInstance()->addRandomGaussParameter(wordlist[1], wordlist[2], wordlist[3]);
0235         } else if (wordlist[0] == "flat") {
0236           ParameterMgr::getInstance()->addRandomFlatParameter(wordlist[1], wordlist[2], wordlist[3]);
0237         } else {
0238           ALIFileIn::getInstance(Model::SDFName()).ErrorInLine();
0239           std::cerr << ": For a four-word parameter line, first has to be 'gauss' or 'flat', it is  " << wordlist[0]
0240                     << std::endl;
0241           exit(1);
0242         }
0243       } else {
0244         ALIFileIn::getInstance(Model::SDFName()).ErrorInLine();
0245         std::cerr << ": IN PARAMETERS section TWO-WORD-LINES ARE EXPECTED " << std::endl;
0246         ALIUtils::dumpVS(wordlist, " ");
0247         exit(2);
0248       }
0249 
0250       //print it out
0251       if (ALIUtils::debug >= 1) {
0252         ALIUtils::dumpVS(wordlist, "PARAMETERS:  ", std::cout);
0253       }
0254 
0255       //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0256       //@@@ ---------- Reading SYSTEM_TREE_DESCRIPTION section
0257     } else if (currentSectionNo == sectSystemTreeDescription) {
0258       //---------- Convert the numbers found in list of components (e.g. 2 laser -> laser laser )
0259       //----- Backup wordlist and erase it
0260       std::vector<ALIstring> wordlist2 = wordlist;
0261       std::vector<ALIstring>::iterator vsite;
0262       ALIint wsiz = wordlist.size();
0263       for (ALIint ii = 0; ii < wsiz; ii++) {
0264         wordlist.pop_back();
0265       }
0266       //----- Loop objects looking for numbers to convert
0267       for (vsite = wordlist2.begin(); vsite != wordlist2.end(); ++vsite) {
0268         if (ALIUtils::IsNumber(*vsite)) {
0269           int nOpticalObjects = atoi((*vsite).c_str());
0270           //----- If number is 1 it is not necessary
0271           if (nOpticalObjects == 1) {
0272             if (ALIUtils::debug >= 0)
0273               std::cerr << "WARNING: in line " << ALIFileIn::getInstance(Model::SDFName()).nline()
0274                         << " number of repeated OpticalObjects = 1. Please avoid the numbering " << std::endl;
0275             //-    wordlist.erase( wordlist.begin() + (vsite-wordlist2.begin()) );
0276           } else {
0277             //----- The number cannot be the last in the list and you cannot put two numbers together
0278             if (vsite + 1 == wordlist.end() || ALIUtils::IsNumber(*(vsite + 1))) {
0279               ALIFileIn::getInstance(Model::SDFName()).ErrorInLine();
0280               std::cerr << "NUMBER SHOULD BE FOLLOWED BY an OpticalObject type" << std::endl;
0281               exit(2);
0282             }
0283             //----- Right format: convert
0284             //--- Substitute the number by the object type in wordlist
0285             //-   *( wordlist.begin() + (vsite-wordlist2.begin()) ) = *(vsite+1);
0286             //--- Add n-1 object types to wordlist (the nth object will be added as the object taht goes after the number)
0287             for (ALIint ii = 0; ii < nOpticalObjects - 1; ii++) {
0288               //-std::cout << ii << "inserting in wordlist " << *(vsite+1) << std::endl;
0289               wordlist.push_back(*(vsite + 1));
0290             }
0291           }
0292         } else {
0293           //----- Not number, add it to wordlist
0294           wordlist.push_back(*vsite);
0295         }
0296       }
0297 
0298       //---------- Dump system structure
0299       if (ALIUtils::debug >= 1) {
0300         ALIUtils::dumpVS(wordlist, "SYSTEM TREE DESCRIPTION: before ordering, OBJECT: ", std::cout);
0301       }
0302 
0303       //---------- Fill the list of Optical Object with components (theOptODictionary)
0304       //----- First word is 'object': new OptO
0305       if (wordlist[0] == ALIstring("object")) {
0306         //----- Check out repeated objects
0307         std::vector<std::vector<ALIstring> >::iterator vvsite;
0308         for (vvsite = theOptODictionary.begin(); vvsite != theOptODictionary.end(); ++vvsite) {
0309           //-     std::cout << " system" << vvsite << std::endl;
0310 
0311           if (*((*vvsite).begin()) == wordlist[1]) {
0312             ALIFileIn::getInstance(Model::SDFName()).ErrorInLine();
0313             std::cerr << "SYSTEM_TREE_DESCRIPTION: REPEATED object " << *((*vvsite).begin())
0314                       << " ( NOT ALLOWED NEITHER WITH EQUAL NOR WITH DIFFERENT COMPONENTS)" << std::endl;
0315             exit(1);
0316           }
0317         }
0318         //------- Add an item to theOptODictionary
0319         std::vector<ALIstring> vstemp;
0320         copy(wordlist.begin() + 1, wordlist.end(), back_inserter(vstemp));
0321         Model::OptODictionary().push_back(vstemp);
0322       } else {
0323         //----- First word is not 'object': add to previous OptO
0324         if (OptODictionary().empty()) {
0325           ALIFileIn::getInstance(Model::SDFName()).ErrorInLine();
0326           std::cerr << "SYSTEM_TREE_DESCRIPTION section: FIRST LINE SHOULD START WITH 'object'" << std::endl;
0327           exit(2);
0328         }
0329         copy(wordlist.begin(), wordlist.end(), back_inserter(*(OptODictionary().end() - 1)));
0330       }
0331 
0332       //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0333       //---------------------------------- Reading SYSTEM_TREE_DATA section
0334     } else if (currentSectionNo == sectSystemTreeData) {
0335       //---------- First line of data:
0336       if (!data_reading) {
0337         //        ALIint dictsize = theOptODictionary.size();
0338         //------- Reorder theOptODictionary
0339         std::vector<std::vector<ALIstring> > OptODictionary2;
0340         reorderOptODictionary("system", OptODictionary2);
0341         if (OptODictionary2.empty()) {
0342           std::cerr << "SYSTEM_TREE_DESCRIPTION section: no object 'system' found " << std::endl;
0343           exit(9);
0344         }
0345         //------- We start reordering with 'system', therefore if 'system' is not the top most object, the objects not hanging from it would not be considered
0346         //---- Check if all the objects are here
0347         std::vector<std::vector<ALIstring> >::const_iterator vvscite, vvscite2;
0348         //      ALIint dictsizen = 0;
0349         for (vvscite = theOptODictionary.begin(); vvscite != theOptODictionary.end(); ++vvscite) {
0350           ALIbool ofound = false;
0351           for (vvscite2 = OptODictionary2.begin(); vvscite2 != OptODictionary2.end(); ++vvscite2) {
0352             if (*((*vvscite).begin()) == *((*vvscite2).begin())) {
0353               ofound = true;
0354               break;
0355             }
0356           }
0357           if (!ofound) {
0358             std::cerr << "!!!SYSTEM_TREE_DESCRIPTION section: object " << *((*vvscite).begin())
0359                       << " is not hanging from object 'system' " << std::endl;
0360             for (vvscite = OptODictionary().begin(); vvscite != OptODictionary().end(); ++vvscite) {
0361               const std::vector<ALIstring>& ptemp = *vvscite;
0362               ALIUtils::dumpVS(ptemp, "OBJECT ", std::cerr);
0363             }
0364             exit(9);
0365           }
0366         }
0367         theOptODictionary = OptODictionary2;
0368 
0369         data_reading = 1;
0370 
0371         //------- Dump ordered OptOs
0372         if (ALIUtils::debug >= 3) {
0373           std::vector<std::vector<ALIstring> >::iterator itevs;
0374           for (itevs = OptODictionary().begin(); itevs != OptODictionary().end(); ++itevs) {
0375             std::vector<ALIstring> ptemp = *itevs;
0376             ALIUtils::dumpVS(ptemp, " SYSTEM TREE DESCRIPTION: after ordering: OBJECT ", std::cout);
0377           }
0378         }
0379 
0380         //---------- Create OpticalObject 'system' (first OpticalObject object):
0381         //---------- it will create its components and recursively all the System Tree of Optical Objects
0382         if (wordlist[0] != "system") {
0383           std::cerr << "SYSTEM_TREE_DATA section: object 'system' is not the first one " << std::endl;
0384           exit(9);
0385         }
0386 
0387         OpticalObject* OptOsystem = new OpticalObject(nullptr, "system", wordlist[1], false);
0388         OptOsystem->construct();
0389         //-              Model::_OptOtree.insert( std::multimap< ALIstring, OpticalObject*, std::less<ALIstring> >::value_type(OptOsystem->type(), OptOsystem) );
0390         //              theOptOlist[OptOsystem->name()] = OptOsystem;
0391         theOptOList.push_back(OptOsystem);
0392 
0393       } else {
0394         //----------- All system is read by the Optical Objects, it should not reach here
0395         ALIFileIn::getInstance(Model::SDFName()).ErrorInLine();
0396         std::cerr << " STILL SOME LINES AFTER ALL SYSTEM TREE IS READ!!!" << std::endl;
0397         exit(9);
0398       }
0399 
0400       //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0401       //----------------------------------- Reading MEASUREMENTS section
0402     } else if (currentSectionNo == sectMeasurements) {
0403       //---------- Create Measurement with appropiate dimension
0404       Measurement* meastemp = nullptr;
0405       ALIstring measType = wordlist[0];
0406       ALIstring measName;
0407       if (wordlist.size() == 2) {
0408         measName = wordlist[1];
0409         wordlist.pop_back();
0410       } else {
0411         measName = "";
0412       }
0413       if (measType == ALIstring("SENSOR2D")) {
0414         meastemp = new MeasurementSensor2D(2, measType, measName);
0415         meastemp->setConversionFactor(wordlist);
0416         meastemp->construct();
0417       } else if (measType == ALIstring("DISTANCEMETER3DIM")) {
0418         meastemp = new MeasurementDistancemeter3dim(1, measType, measName);
0419         meastemp->setConversionFactor(wordlist);
0420         meastemp->construct();
0421       } else if (measType == ALIstring("DISTANCEMETER") || measType == ALIstring("DISTANCEMETER1DIM")) {
0422         meastemp = new MeasurementDistancemeter(1, measType, measName);
0423         meastemp->setConversionFactor(wordlist);
0424         meastemp->construct();
0425       } else if (measType == ALIstring("TILTMETER")) {
0426         meastemp = new MeasurementTiltmeter(1, measType, measName);
0427         meastemp->setConversionFactor(wordlist);
0428         meastemp->construct();
0429       } else if (measType == ALIstring("COPS")) {
0430         meastemp = new MeasurementCOPS(4, measType, measName);
0431         meastemp->setConversionFactor(wordlist);
0432         meastemp->construct();
0433       } else if (measType == ALIstring("DIFFENTRY")) {
0434         meastemp = new MeasurementDiffEntry(1, measType, measName);
0435         meastemp->construct();
0436       } else if (measType == ALIstring("measurements_from_file") || measType == ALIstring("@measurements_from_file")) {
0437         new CocoaDaqReaderText(wordlist[1]);
0438         //m Measurement::setMeasurementsFileName( wordlist[1] );
0439         //m if ( ALIUtils::debug >= 2) std::cout << " setting measurements_from_file " << measType << " == " << Measurement::measurementsFileName() << std::endl;
0440         if (wordlist.size() == 4) {
0441           Measurement::only1 = true;
0442           Measurement::only1Date = wordlist[2];
0443           Measurement::only1Time = wordlist[3];
0444           //-      std::cout << " setting Measurement::only1" <<  Measurement::only1 << std::endl;
0445         }
0446       } else if (measType == ALIstring("measurements_from_file_ROOT") ||
0447                  measType == ALIstring("@measurements_from_file")) {
0448         new CocoaDaqReaderRoot(wordlist[1]);
0449       } else if (wordlist[0] == ALIstring("correlations_from_file") ||
0450                  wordlist[0] == ALIstring("@correlations_from_file")) {
0451         ErrorCorrelationMgr::getInstance()->readFromReportFile(wordlist[1]);
0452       } else if (wordlist[0] == ALIstring("copy_measurements") || wordlist[0] == ALIstring("@copy_measurements")) {
0453         copyMeasurements(wordlist);
0454         //      } else if( wordlist[0] == "scan" || wordlist[0] == "@scan" ) {
0455         //      ScanMgr::getInstance()->addOptOEntry( wordlist );
0456       } else if (wordlist[0] == ALIstring("fittedEntries_from_file")) {
0457         theFittedEntriesReader = new FittedEntriesReader(wordlist[1]);
0458         if (ALIUtils::debug >= 2)
0459           std::cout << " setting fittedEntries_from_file " << wordlist[0] << " == " << wordlist[1] << std::endl;
0460       } else {
0461         std::cerr << "Measurement:" << std::endl;
0462         ALIFileIn::getInstance(Model::SDFName()).ErrorInLine();
0463         std::cerr << "!!! type of measurement not allowed: " << wordlist[0] << std::endl;
0464         std::cerr << " Allowed types: SENSOR2D, DISTANCEMETER, DISTANCEMETER1DIM, TILTMETER, COPS, DIFFENTRY "
0465                   << std::endl;
0466         exit(2);
0467       }
0468       //-      meastemp->setGlobalName( wordlist[0] );
0469       //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0470       //@@@ ---------- Reading REPORT OUT  section
0471     } else if (currentSectionNo == sectReportOut) {
0472       //----- It must be after global options section
0473       if (InSectionNo + 1 != sectParameters) {
0474         ALIFileIn::getInstance(Model::SDFName()).ErrorInLine();
0475         std::cerr << "BAD ORDER OF SECTIONS, reading section " << *SectionTitleIterator << std::endl
0476                   << " currentSectionNo = " << currentSectionNo << " InSectionNo = " << InSectionNo << std::endl
0477                   << " ---------  Please see documentation  ---------- " << std::endl;
0478         exit(1);
0479       }
0480 
0481       EntryMgr::getInstance()->readEntryFromReportOut(wordlist);
0482     }
0483   }
0484 
0485   //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0486   //@@@ All system read
0487   //---------- make Measurements links to their OptOs
0488   if (ALIUtils::debug >= 2)
0489     std::cout << std::endl << "@@@@ Building Measurements links to OptOs" << std::endl;
0490   Model::buildMeasurementsLinksToOptOs();
0491 
0492   if (ALIUtils::debug >= 1) {
0493     std::cout << "----------  SYSTEM SUCCESFULLY READ ----------" << std::endl << std::endl;
0494   }
0495   filein.close();
0496 
0497   return;
0498 }
0499 
0500 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0501 //@@  reorderOptODictionary: REBUILDS THE SYSTEM_TREE_DESCRIPTION SECTION 'objects'
0502 //@@ (_OptODictionary) IN A HIERARCHICAL (TREE LIKE) ORDER
0503 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0504 void Model::reorderOptODictionary(const ALIstring& ssearch, std::vector<std::vector<ALIstring> >& OptODictionary2) {
0505   //  int ALIstring_found = 0;
0506   std::vector<std::vector<ALIstring> >::iterator vvsite;
0507   std::vector<ALIstring>::iterator vsite;
0508 
0509   //---------------------------------------- Look for 'ssearch' as the first ALIstring of an OptODictionary item
0510   for (vvsite = OptODictionary().begin(); vvsite != OptODictionary().end(); ++vvsite) {
0511     if (*((*vvsite).begin()) == ssearch) {
0512       //     ALIstring_found = 1;
0513       OptODictionary2.push_back(*vvsite);
0514 
0515       //-    std::cout << "VVSITE";
0516       //-   ostream_iterator<ALIstring> outs(std::cout,"&");
0517       //-   copy( (*vvsite).begin(), (*vvsite).end(), outs);
0518 
0519       //---------------------------------- look for components of this _OptODictionary item
0520       for (vsite = (*vvsite).begin() + 1; vsite != (*vvsite).end(); ++vsite) {
0521         reorderOptODictionary(*vsite, OptODictionary2);
0522       }
0523       break;
0524     }
0525   }
0526 
0527   /*  //------- object 'system' should exist
0528   if(!ALIstring_found && ssearch == "system") {
0529     std::cerr << "SYSTEM_TREE_DATA section: no 'object system' found " << std::endl;
0530     exit(9);
0531     } */
0532 }
0533 
0534 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0535 //@@ BuildMeasurementLinksToOptOs
0536 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0537 void Model::buildMeasurementsLinksToOptOs() {
0538   //---------- Loop Measurements
0539   std::vector<Measurement*>::const_iterator vmcite;
0540   for (vmcite = MeasurementList().begin(); vmcite != MeasurementList().end(); ++vmcite) {
0541     //---------- Transform for each Measurement the Measured OptO names to Measured OptO pointers
0542     //     (*vmcite)->buildOptOList();
0543 
0544     //---------- Build list of Entries that affect a Measurement
0545     // (*vmcite)->buildAffectingEntryList();
0546   }
0547 }
0548 
0549 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0550 //@@ Get the value of a parameter in theParameters std::vector
0551 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0552 ALIint Model::getParameterValue(const ALIstring& sstr, ALIdouble& val) {
0553   ParameterMgr* parmgr = ParameterMgr::getInstance();
0554   ALIint iret = parmgr->getParameterValue(sstr, val);
0555 
0556   return iret;
0557 }
0558 
0559 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0560 //@@ getOptOByName: Find an OptO name in _OptOlist and return a pointer to it
0561 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0562 OpticalObject* Model::getOptOByName(const ALIstring& opto_name) {
0563   //---------- Look for Optical Object name in OptOList
0564   std::vector<OpticalObject*>::const_iterator vocite;
0565   for (vocite = OptOList().begin(); vocite != OptOList().end(); ++vocite) {
0566     if ((*vocite)->name() == opto_name)
0567       break;
0568   }
0569 
0570   if (vocite == OptOList().end()) {
0571     //---------- If opto_name not found, exit
0572     std::cerr << " LIST OF OpticalObjects " << std::endl;
0573     for (vocite = OptOList().begin(); vocite != OptOList().end(); ++vocite) {
0574       std::cerr << (*vocite)->name() << std::endl;
0575     }
0576     std::cerr << "!!EXITING at getOptOByName: Optical Object " << opto_name << " doesn't exist!!" << std::endl;
0577     exit(4);
0578     //       return (OpticalObject*)0;
0579   } else {
0580     //---------- If opto_name found, return pointer to it
0581     if (ALIUtils::debug > 999) {
0582       std::cout << opto_name.c_str() << "SSOptOitem" << (*vocite) << (*vocite)->name() << "len" << OptOList().size()
0583                 << std::endl;
0584     }
0585     return (*vocite);
0586   }
0587 }
0588 
0589 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0590 //@@ getOptOByType: Find an OptO type in _OptOList (the first one with this type)
0591 //@@ and return a pointer to it
0592 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0593 OpticalObject* Model::getOptOByType(const ALIstring& opto_type) {
0594   //---------- Look for Optical Object type in OptOList
0595   std::vector<OpticalObject*>::const_iterator vocite;
0596   for (vocite = OptOList().begin(); vocite != OptOList().end(); ++vocite) {
0597     //   std::cout << "OPTOList" << (*msocite).first << std::endl;
0598     if ((*vocite)->type() == opto_type)
0599       break;
0600   }
0601 
0602   if (vocite == OptOList().end()) {
0603     //---------- If opto_type not found, exit
0604     std::cerr << "!!EXITING at getOptOByType: Optical Object " << opto_type << " doesn't exist!!" << std::endl;
0605     exit(4);
0606   } else {
0607     //---------- If opto_type found, return pointer to it
0608     return (*vocite);
0609   }
0610 }
0611 
0612 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0613 //@@  Find an Entry name in theEntryVector and return a pointer to it
0614 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0615 Entry* Model::getEntryByName(const ALIstring& opto_name, const ALIstring& entry_name) {
0616   //---------- Look for Entry name type in EntryList
0617   std::vector<Entry*>::const_iterator vecite;
0618   for (vecite = EntryList().begin(); vecite != EntryList().end(); ++vecite) {
0619     if (ALIUtils::debug >= 4)
0620       std::cout << "getEntryByName: " << (*vecite)->OptOCurrent()->name() << " E " << (*vecite)->name()
0621                 << " Searching: " << opto_name << " E " << entry_name << std::endl;
0622     //-    std::cout << " optoName " << (*vecite)->OptOCurrent()->name()<< " " << (*vecite)->name() << std::endl;
0623     if ((*vecite)->OptOCurrent()->name() == opto_name && (*vecite)->name() == entry_name) {
0624       return *vecite;
0625     }
0626   }
0627   //---------- Entry not found!
0628   std::cerr << "!!!EXITING at getEntryByName: Entry name not found:" << opto_name << "  " << entry_name << std::endl;
0629   exit(1);
0630 }
0631 
0632 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0633 Measurement* Model::getMeasurementByName(const ALIstring& meas_name, ALIbool exists) {
0634   //---------- Look for Optical Object name in OptOList
0635   std::vector<Measurement*>::const_iterator vmcite;
0636   for (vmcite = theMeasurementVector.begin(); vmcite != theMeasurementVector.end(); ++vmcite) {
0637     if ((*vmcite)->name() == meas_name)
0638       break;
0639   }
0640 
0641   if (vmcite != theMeasurementVector.end()) {
0642     //---------- If opto_name found, return pointer to it
0643     return (*vmcite);
0644   } else {
0645     if (exists) {
0646       //---------- If opto_name not found, exit
0647       std::cerr << " LIST OF Measurements " << std::endl;
0648       for (vmcite = theMeasurementVector.begin(); vmcite != theMeasurementVector.end(); ++vmcite) {
0649         std::cerr << (*vmcite)->name() << std::endl;
0650       }
0651       std::cerr << "!!EXITING at getMeasurementByName: Measurement " << meas_name << " doesn't exist!!" << std::endl;
0652       abort();
0653       //       return (OpticalObject*)0;
0654     } else {
0655       return nullptr;
0656     }
0657   }
0658 }
0659 
0660 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0661 //@@  Get an OptO list of components
0662 //@@  Looks the theOptODictionary item that has 'opto_type' as the first ALIstring,
0663 //@@  copies this item to 'vcomponents', substracting the first ALIstring, that is the opto_type itself,
0664 //@@  Returns 1 if item found, 0 if not
0665 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0666 ALIbool Model::getComponentOptOTypes(const ALIstring& opto_type, std::vector<ALIstring>& vcomponents) {
0667   //---------- clean std::vector in which you are going to store opto types
0668   std::vector<ALIstring>::iterator vsite;
0669   for (vsite = vcomponents.begin(); vsite != vcomponents.end(); ++vsite) {
0670     vcomponents.pop_back();
0671   }
0672 
0673   //---------- Looks the theOptODictionary item that has 'opto_type' as the first ALIstring,
0674   ALIint ALIstring_found = 0;
0675   std::vector<std::vector<ALIstring> >::iterator vvsite;
0676   for (vvsite = OptODictionary().begin(); vvsite != OptODictionary().end(); ++vvsite) {
0677     if (*((*vvsite).begin()) == opto_type) {
0678       ALIstring_found = 1;
0679       //tt  copies this item to 'vcomponents', substracting the first ALIstring, that is the opto_type itself,
0680       vcomponents = *vvsite;
0681       vcomponents.erase(vcomponents.begin());
0682       break;
0683     }
0684   }
0685 
0686   if (ALIstring_found) {
0687     return true;
0688   } else {
0689     return false;
0690   }
0691 }
0692 
0693 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0694 //@@ Get from _OptOList the list of OptOs pointers that has as parent 'opto_name'
0695 //@@ and store it in vcomponents
0696 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0697 ALIbool Model::getComponentOptOs(const ALIstring& opto_name, std::vector<OpticalObject*>& vcomponents) {
0698   //---------- clean std::vector in which you are going to store opto pointers
0699   std::vector<OpticalObject*>::iterator voite;
0700   for (voite = vcomponents.begin(); voite != vcomponents.end(); ++voite) {
0701     vcomponents.pop_back();
0702   }
0703 
0704   //---------- Get OptO corresponding to name 'opto_name'
0705   OpticalObject* opto = getOptOByName(opto_name);
0706 
0707   if (ALIUtils::debug >= 99)
0708     std::cout << opto_name << "getComponentOptOs: opto " << opto << opto->name() << std::endl;
0709   std::vector<OpticalObject*>::const_iterator vocite;
0710 
0711   if (ALIUtils::debug >= 99)
0712     std::cout << "optolist size " << OptOList().size() << std::endl;
0713   ALIbool opto_found = false;
0714   for (vocite = OptOList().begin(); vocite != OptOList().end(); ++vocite) {
0715     if ((*vocite)->parent() != nullptr) {
0716       //        std::cout << "looping OptOlist" << (*vocite)->name() << " parent " <<(*vocite)->parent()->name() << std::endl;
0717       if ((*vocite)->parent()->name() == opto_name) {
0718         opto_found = true;
0719         vcomponents.push_back((*vocite));
0720       }
0721     }
0722   }
0723 
0724   /*  std::pair<multimap< ALIstring, OpticalObject*, std::less<ALIstring> >::iterator,
0725        std::multimap< ALIstring, OpticalObject*, std::less<ALIstring> >::iterator>
0726   pmmao =  _OptOtree.equal_range(opto_name);
0727 
0728   if( pmmao.first != _OptOtree.end()) {
0729     std::multimap< ALIstring, OpticalObject*, std::less<ALIstring> >::const_iterator socite;
0730     for (socite = pmmao.first; socite != (pmmao.second); socite++) {
0731          vcomponents.push_back( (*socite).second );
0732     }
0733   }
0734   */
0735   return opto_found;
0736 }
0737 
0738 ///**************** FOR COPYING AN OPTO
0739 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0740 //@@ createCopyComponentList:
0741 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0742 ALIbool Model::createCopyComponentList(const ALIstring& typ) {
0743   //---------- 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)
0744   if (ALIUtils::debug >= 3)
0745     std::cout << "createCopyComponentList " << typ << std::endl;
0746   OpticalObject* start_opto = getOptOByType(typ);
0747 
0748   //---------- clean list of OptOs to copy
0749   theOptOsToCopyList.erase(theOptOsToCopyList.begin(), theOptOsToCopyList.end());
0750 
0751   //---------- Fill list of OptOs to copy
0752   fillCopyComponentList(start_opto);
0753   //- if(ALIUtils::debug >= 9) std::cout << "createCopyComponentList " << typ << theOptOsToCopyList.size() << std::endl;
0754 
0755   theOptOsToCopyListIterator = theOptOsToCopyList.begin();
0756   return true;
0757 }
0758 
0759 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0760 //@@ fillCopyOptOList: Fill list of objects to copy with the components of 'opto'
0761 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0762 ALIbool Model::fillCopyComponentList(const OpticalObject* opto) {
0763   if (ALIUtils::debug >= 3)
0764     std::cout << "entering fillCopyComponentList(): OptO" << opto->name() << std::endl;
0765   //---------- Get list of components of 'opto'
0766   std::vector<OpticalObject*> vopto;
0767   ALIbool opto_found = getComponentOptOs(opto->name(), vopto);
0768   if (!opto_found) {
0769     if (ALIUtils::debug >= 5)
0770       std::cout << "fillCopyComponentList: NO COMPONENTS TO COPY IN THIS OptO" << opto->name() << std::endl;
0771   }
0772 
0773   //---------- Loop list of components of 'opto'
0774   std::vector<OpticalObject*>::const_iterator vocite;
0775   for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
0776     theOptOsToCopyList.push_back(*vocite);
0777     if (ALIUtils::debug >= 5)
0778       std::cout << "fillCopyOptOList " << (*vocite)->type() << " " << (*vocite)->name() << std::endl;
0779     //---------- Add components of this component
0780     fillCopyComponentList(*vocite);
0781   }
0782   return opto_found;
0783 }
0784 
0785 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0786 //@@ nextOptOToCopy: return next object to copy from theOptOsToCopyListIterator
0787 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0788 OpticalObject* Model::nextOptOToCopy() {
0789   if (ALIUtils::debug >= 5)
0790     std::cout << "entering nextOptOToCopy() " << std::endl;
0791   ++theOptOsToCopyListIterator;
0792   //  if(ALIUtils::debug >= 5) std::cout <<" nextOptOToCopy " << (*(theOptOsToCopyListIterator-1))->name() << std::endl;
0793   return *(theOptOsToCopyListIterator - 1);
0794 }
0795 
0796 ///*************** FOR CMS LINK SYSTEM (to fit it part by part)
0797 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0798 //@@ If fitting CMS link, it has to be fitted part by part, in several iterations
0799 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0800 void Model::CMSLinkFit(ALIint cmslink) {
0801   /*
0802   //---------- Get number of fits
0803   ALIint cmslinkNoFits = 0;
0804   if( cmslink == 1) {
0805     cmslinkNoFits = 3;
0806   } else if( cmslink == 2) {
0807     cmslinkNoFits = 2;
0808   }
0809   if(ALIUtils::debug >= 4) std::cout << " Start CMS link fit with method " << cmslink << " (number of fits = " << cmslinkNoFits << ")" << std::endl;
0810 
0811   //----------- Loop number of cms_link fits
0812   for(ALIint ilink = ALIint(Model::GlobalOptions()["cms_link"]); ilink <= cmslinkNoFits; ilink++) {
0813 
0814   //----- Iteration 2 of method 2 resembles iteration 3 of method 1
0815      ALIdouble cmslink_method;
0816      assert(Model::getGlobalOptionValue("cms_link_method", cmslink_method));
0817      if( cmslink_method == 2 && ilink == 2) ilink = 3;
0818 
0819 //---- Set variable CMSLinkIteration, Checked in Fit.C and other     //- std::cout << "ilink" << ilink << std::endl;
0820     Model::setGlobalOption("cms_link", ilink);
0821     Model::CMSLinkIteration = ilink;
0822 
0823     if(ilink > 1)Model::readSystemDescription(); //already read once to fill value Model::GlobalOptions()["cms_link"]
0824 
0825     //---------- Delete the OptO not fitted in this iteration
0826     //    Model::CMSLinkDeleteOptOs();
0827     // cannot be here because you may recover a parameter saved in previous iteration that now it is deleted (!!MODIFY THIS)
0828 
0829     //---------- Recover parameters fitted in previous iteration
0830     Model::CMSLinkRecoverParamFittedSigma( ilink );
0831 
0832     Model::CMSLinkRecoverParamFittedValueDisplacement( ilink );
0833 
0834     //---------- Delete the OptO not fitted in this iteration
0835     Model::CMSLinkDeleteOptOs();
0836 
0837     //---------- Start fit
0838     Fit::startFit();
0839 
0840     //---------- Save parameters fitted in this iteration (to be used in next one)
0841     Model::CMSLinkSaveParamFittedSigma( ilink );
0842 
0843     Model::CMSLinkSaveParamFittedValueDisplacement( ilink );
0844 
0845     //---------- Delete whole system to start anew in next iteration
0846     Model::CMSLinkCleanModel();
0847 
0848   }
0849   */
0850 }
0851 
0852 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0853 //@@ CMSLinkCleanModel: clean Model for new iteration while fitting CMS link
0854 //@@ part by part
0855 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0856 void Model::CMSLinkCleanModel() {
0857   deleteOptO("s");
0858   //---------- Clean OptOdicitionary (in case this is not first reading)
0859   ALIuint odsize = theOptODictionary.size();
0860   for (ALIuint ii = 0; ii < odsize; ii++) {
0861     theOptODictionary.pop_back();
0862   }
0863 }
0864 
0865 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0866 //@@ CMSLinkDeleteOptOs
0867 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0868 void Model::CMSLinkDeleteOptOs() {
0869   ALIint cmslink_iter = Model::CMSLinkIteration;
0870   ALIdouble cmslink_method;
0871 
0872   GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
0873   assert(gomgr->getGlobalOptionValue("cms_link_method", cmslink_method));
0874   ALIdouble cmslink_halfplanes;
0875   assert(gomgr->getGlobalOptionValue("cms_link_halfplanes", cmslink_halfplanes));
0876   if (ALIUtils::debug >= 2)
0877     std::cout << "CMSLinkDeleteOptOs: cms_link_halfplanes " << cmslink_halfplanes << cmslink_iter << std::endl;
0878 
0879   if (cmslink_iter == 1) {
0880     //---------- First fit: delete everything but laser1 and det_tkU
0881     //    deleteOptO("s/laserboxL/laser2");
0882     //-    std::cout << "delete mabs" << std::endl;
0883     deleteOptO("s/mabsL");
0884     //------- Method 1: detectors at tracker down
0885     if (cmslink_method == 1) {
0886       deleteOptO("s/tracker/det_trkDL");
0887       deleteOptO("s/tracker/det_trkDR");
0888     }
0889 
0890     if (cmslink_halfplanes == 2) {
0891       //      deleteOptO("s/laserboxR/laser2");
0892       deleteOptO("s/mabsR");
0893     }
0894 
0895   } else if (cmslink_iter == 2) {
0896     //---------- Second fit (method 1): delete everything but laser1 and det3
0897     //    deleteOptO("s/laserboxL/laser2");
0898     deleteOptO("s/mabsL");
0899     deleteOptO("s/tracker/CST/wheel_trkL/peri/mirror");  //??
0900     deleteOptO("s/tracker/CST/wheel_trkL/det_trkU");
0901     //------- Method 1: detectors on CST, Method 2: detectors on tracker
0902     //not necessary    deleteOptO("s/tracker/CST/det6");
0903 
0904     if (cmslink_halfplanes <= 1) {
0905       deleteOptO("s/tracker/CST/wheel_trkR");
0906     } else if (cmslink_halfplanes == 2) {
0907       //      deleteOptO("s/laserboxR/laser2");
0908       deleteOptO("s/mabsR");
0909       deleteOptO("s/tracker/CST/wheel_trkR/peri/mirror");  //??
0910       deleteOptO("s/tracker/CST/wheel_trkR/det_trkU");
0911     }
0912 
0913   } else if (cmslink_iter == 3) {
0914     //---------- Third fit: delete everything but laser2 and mabs
0915     //    deleteOptO("s/laserboxL/laser1");
0916     deleteOptO("s/tracker");
0917 
0918     if (cmslink_halfplanes == 2) {
0919       //      deleteOptO("s/laserboxR/laser1");
0920     }
0921     //---------- Do nothing
0922   } else {
0923   }
0924 }
0925 
0926 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0927 //@@ CMSLinkSaveParamFittedSigma
0928 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0929 void Model::CMSLinkSaveParamFittedSigma(ALIint cms_link) {
0930   /*
0931   ALIdouble cms_link_halfplanes = (GlobalOptions()["cms_link_halfplanes"]);
0932 
0933   cleanParamFittedSigmaVector();
0934 
0935   //---------- First fit: Save laserbox angles fitted at tracker
0936   if( cms_link == 1) {
0937     //?    if (GlobalOptions()["cms_link_method"] < 10){
0938     saveParamFittedSigma("s/laserboxL","Angles X");
0939     saveParamFittedSigma("s/laserboxL","Angles Y");
0940 
0941     saveParamFittedCorrelation("s/laserboxL","Angles X",
0942      "s/tracker/CST","Centre Y");
0943     saveParamFittedCorrelation("s/laserboxL","Angles Y",
0944      "s/tracker/CST","Centre X");
0945     if( cms_link_halfplanes == 2) {
0946       saveParamFittedSigma("s/laserboxR","Angles X");
0947       saveParamFittedSigma("s/laserboxR","Angles Y");
0948 
0949       saveParamFittedCorrelation("s/laserboxR","Angles X",
0950        "s/tracker/CST","Centre Y");
0951       saveParamFittedCorrelation("s/laserboxR","Angles Y",
0952        "s/tracker/CST","Centre X");
0953     }
0954 
0955   } else if( cms_link == 2) {
0956   //---------- Second fit: Save laserbox angles and position
0957     saveParamFittedSigma("s/laserboxL","Centre X");
0958     saveParamFittedSigma("s/laserboxL","Centre Y");
0959     // Make quality unk to cal
0960     saveParamFittedSigma("s/laserboxL","Centre Z");
0961     saveParamFittedSigma("s/laserboxL","Angles X");
0962     saveParamFittedSigma("s/laserboxL","Angles Y");
0963 
0964     saveParamFittedCorrelation("s/laserboxL","Centre X",
0965         "s/laserboxL","Angles Y");
0966     saveParamFittedCorrelation("s/laserboxL","Centre Y",
0967         "s/laserboxL","Angles X");
0968 
0969    if( cms_link_halfplanes == 2) {
0970     saveParamFittedSigma("s/laserboxR","Centre X");
0971     saveParamFittedSigma("s/laserboxR","Centre Y");
0972     // Make quality unk to cal
0973     saveParamFittedSigma("s/laserboxR","Centre Z");
0974     saveParamFittedSigma("s/laserboxR","Angles X");
0975     saveParamFittedSigma("s/laserboxR","Angles Y");
0976 
0977     saveParamFittedCorrelation("s/laserboxR","Centre X",
0978         "s/laserboxR","Angles Y");
0979     saveParamFittedCorrelation("s/laserboxR","Centre Y",
0980         "s/laserboxR","Angles X");
0981    }
0982   } else {
0983  //---------- Do nothing
0984 
0985   }
0986   */
0987 }
0988 
0989 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0990 //@@ CMSLinkSaveParamFittedValueDisplacement:
0991 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0992 void Model::CMSLinkSaveParamFittedValueDisplacement(ALIint cms_link) {
0993   /*
0994   cleanParamFittedValueDisplacementMap();
0995 
0996   //----------
0997   if( cms_link == 1 || cms_link == 2 ) {
0998     std::vector<Entry*>::const_iterator vecite;
0999     for( vecite = EntryList().begin(); vecite != EntryList().end(); vecite++) {
1000       if( (*vecite)->valueDisplacementByFitting() != 0 ) {
1001         ALIstring names = (*vecite)->OptOCurrent()->name() + "/" + (*vecite)->name();
1002         std::cout << "saeParamFittedValueDisplacementMap" << names << (*vecite)->valueDisplacementByFitting() << std::endl;
1003         theParamFittedValueDisplacementMap[ names ] = (*vecite)->valueDisplacementByFitting();
1004       }
1005     }
1006 
1007   //---------- Do nothing
1008   } else {
1009 
1010   }
1011   */
1012 }
1013 
1014 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1015 //@@ CMSLinkRecoverParamFittedSigma:
1016 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1017 void Model::CMSLinkRecoverParamFittedSigma(ALIint cms_link) {
1018   /*
1019   ALIdouble cms_link_halfplanes = (GlobalOptions()["cms_link_halfplanes"]);
1020 
1021   if( cms_link == 2) {
1022   //---------- Second fit: recover laserbox angles
1023     recoverParamFittedSigma("s/laserboxL","Angles X",0);
1024     recoverParamFittedSigma("s/laserboxL","Angles Y",1);
1025 
1026     if( cms_link_halfplanes == 2) {
1027       recoverParamFittedSigma("s/laserboxR","Angles X",0);
1028       recoverParamFittedSigma("s/laserboxR","Angles Y",1);
1029     }
1030 
1031   } else if( cms_link == 3) {
1032   //---------- Third fit: recover laserbox angles and position and rotate angles to mabs
1033     recoverParamFittedSigma("s/laserboxL","Centre X",0);
1034     recoverParamFittedSigma("s/laserboxL","Centre Y",1);
1035     recoverParamFittedSigma("s/laserboxL","Centre Z",2);
1036     recoverParamFittedSigma("s/laserboxL","Angles X",3);
1037 
1038     //----- Angle around Y is converted to angle around Z when turning 90 deg
1039     Entry* slaZ = getEntryByName("s/laserboxL","Angles Z");
1040     //--- prec_level_laser
1041     Entry* smaZ = getEntryByName("s/mabsL","Angles Z");
1042     slaZ->setQuality(0);
1043     slaZ->setValue( smaZ->value() );
1044     //    smaZ->setQuality(0); //!!???!!?
1045 
1046     Entry* slaY = getEntryByName("s/laserboxL","Angles Y");
1047     slaY->setQuality(0);
1048 
1049     if( cms_link_halfplanes == 2) {
1050       recoverParamFittedSigma("s/laserboxR","Centre X",0);
1051       recoverParamFittedSigma("s/laserboxR","Centre Y",1);
1052       recoverParamFittedSigma("s/laserboxR","Centre Z",2);
1053       recoverParamFittedSigma("s/laserboxR","Angles X",3);
1054 
1055       //----- Angle around Y is converted to angle around Z when turning 90 deg
1056       Entry* slaZ = getEntryByName("s/laserboxR","Angles Z");
1057       //--- prec_level_laser
1058       Entry* smaZ = getEntryByName("s/mabsR","Angles Z");
1059       slaZ->setQuality(0);
1060       slaZ->setValue( smaZ->value() );
1061       //    smaZ->setQuality(0); //!!???!!?
1062 
1063       Entry* slaY = getEntryByName("s/laserboxR","Angles Y");
1064       slaY->setQuality(0);
1065     }
1066   } else {
1067   //---------- Do nothing
1068 
1069   }
1070   */
1071 }
1072 
1073 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1074 //@@ CMSLinkRecoverParamFittedValueDisplacement:
1075 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1076 void Model::CMSLinkRecoverParamFittedValueDisplacement(ALIint cms_link) {
1077   /*
1078   //---------- Second fit: recover laserbox angles
1079   if( cms_link == 2 || cms_link == 3 ) {
1080 
1081     std::map<ALIstring, ALIdouble, std::less<ALIstring> >::const_iterator vsdmite;
1082     if ( ALIUtils::debug >= 99) std::cout << "theParamFittedValueDisplacementMap.size " << theParamFittedValueDisplacementMap.size() << std::endl;
1083     for( vsdmite =  theParamFittedValueDisplacementMap.begin(); vsdmite !=  theParamFittedValueDisplacementMap.end(); vsdmite++) {
1084       std::cout << "reoverValueDisp" <<  (*vsdmite).first << "  " << (*vsdmite).second << std::endl;
1085       Entry* this_entry = getEntryByName( (*vsdmite).first);
1086       this_entry->displaceOriginal( (*vsdmite).second );
1087       this_entry->OptOCurrent()->resetGlobalCoordinates();
1088       this_entry->setValueDisplacementByFitting(  (*vsdmite).second );
1089 
1090     }
1091 
1092   //---------- Do nothing
1093   } else {
1094 
1095   }
1096   */
1097 }
1098 
1099 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1100 //@@  Delete an OptO
1101 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1102 void Model::deleteOptO(const ALIstring& opto_name) {
1103   OpticalObject* opto = getOptOByName(opto_name);
1104   deleteOptO(opto);
1105 }
1106 
1107 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1108 //@@  deleteOptO: delete OptO, its Entries, and the Measurements in which it participates. Then deleteOptO of components
1109 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1110 void Model::deleteOptO(OpticalObject* opto) {
1111   if (ALIUtils::debug >= 5)
1112     std::cout << "DELETING OptO" << opto->name() << std::endl;
1113 
1114   //---------- Delete Coordinate Entries of this OptO
1115   std::vector<Entry*>::const_iterator vecite;
1116   std::vector<Entry*>::iterator veite2;
1117   if (ALIUtils::debug >= 9)
1118     std::cout << "SIZE" << theEntryVector.size() << std::endl;
1119   for (vecite = opto->CoordinateEntryList().begin(); vecite != opto->CoordinateEntryList().end(); ++vecite) {
1120     //    ALIuint pos = FindItemInVector( (*veite), opto->CoordinateEntryList() );
1121     veite2 = find(theEntryVector.begin(), theEntryVector.end(), (*vecite));
1122     //-  if ( ALIUtils::debug >= 9) std::cout << (*veite2) << "DELETE ENTRY " << (*vecite) <<(*veite2)->OptOCurrent()->name() << (*veite2)->name() << std::endl;
1123     delete ((*veite2));
1124     theEntryVector.erase(veite2);
1125   }
1126 
1127   for (vecite = opto->ExtraEntryList().begin(); vecite != opto->ExtraEntryList().end(); ++vecite) {
1128     //    ALIuint pos = FindItemInVector( (*veite), opto->CoordinateEntryList() );
1129     veite2 = find(theEntryVector.begin(), theEntryVector.end(), (*vecite));
1130     //-    if(ALIUtils::debug >= 9) std::cout << (*veite2) << "DELETE ENTRY " << (*veite2)->OptOCurrent()->name() << (*veite2)->name() << std::endl;
1131     delete ((*veite2));
1132     theEntryVector.erase(veite2);
1133   }
1134 
1135   for (vecite = theEntryVector.begin(); vecite != theEntryVector.end(); ++vecite) {
1136     //     std::cout << (*vecite) << "ENTReY " << (*vecite)->OptOCurrent()->name() << (*vecite)->name() << std::endl;
1137   }
1138 
1139   //---------- Delete all Measurement in which opto takes part
1140   std::vector<Measurement*> MeasToBeDeleted;
1141   std::vector<Measurement*>::const_iterator vmite;
1142   std::vector<OpticalObject*>::const_iterator vocite;
1143   for (vmite = MeasurementList().begin(); vmite != MeasurementList().end(); ++vmite) {
1144     if (ALIUtils::debug >= 5)
1145       std::cout << "Deleting Measurement" << (*vmite)->name() << std::endl;
1146     //----- If any of the OptO Measured is opto, delete this Measurement
1147     for (vocite = (*vmite)->OptOList().begin(); vocite != (*vmite)->OptOList().end(); ++vocite) {
1148       if ((*vocite) == opto) {
1149         //-      std::cout << "MEASTBD" << (*vmite) << std::endl;
1150         MeasToBeDeleted.push_back(*vmite);
1151         //?       delete (*vmite);
1152         break;
1153       }
1154     }
1155   }
1156 
1157   //---------- Delete Measurements from list
1158   std::vector<Measurement*>::const_iterator vmcite;
1159   std::vector<Measurement*>::iterator vmite2;
1160   if (ALIUtils::debug >= 9)
1161     std::cout << "SIZEMEAS" << MeasToBeDeleted.size() << std::endl;
1162   for (vmcite = MeasToBeDeleted.begin(); vmcite != MeasToBeDeleted.end(); ++vmcite) {
1163     vmite2 = find(theMeasurementVector.begin(), theMeasurementVector.end(), (*vmcite));
1164     //    std::cout << (*vmite2) << "DELETE MSEASU " << (*vmcite) << (*vmite2)->name()[0] << std::endl;
1165     delete ((*vmite2));
1166     theMeasurementVector.erase(vmite2);
1167   }
1168 
1169   //---------- Delete components
1170   //  std::vector<OpticalObject*>::iterator voite;
1171   std::vector<OpticalObject*> vopto;
1172   //  ALIbool opto_found = getComponentOptOs( opto->name(), vopto );
1173   for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
1174     deleteOptO(*vocite);
1175   }
1176 
1177   //---------- Delete this OptO
1178   //---------- Delete OptO (only from list, to delete it really first delete components)
1179   /*  map< ALIstring, OpticalObject*, std::less<ALIstring> >::iterator msoite =
1180 find( theOptOList.begin(), theOptOList.end(),
1181 map< ALIstring, OpticalObject*, std::less<ALIstring> >::value_type( opto->name(), opto) );*/
1182   std::vector<OpticalObject*>::iterator dvoite =
1183       find(theOptOList.begin(), theOptOList.end(), std::vector<OpticalObject*>::value_type(opto));
1184   //-  std::cout << (*dvoite) << "DELETE OPTO " << opto <<"WW" << (*dvoite)->name() << std::endl;
1185   theOptOList.erase(dvoite);
1186   delete opto;
1187 }
1188 
1189 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1190 //@@
1191 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1192 void Model::saveParamFittedSigma(const ALIstring& opto_name, const ALIstring& entry_name) {
1193   /*
1194 //---------- Get sigma of param Optical
1195   Entry* entry = getEntryByName(opto_name, entry_name);
1196   ALIuint fit_pos = entry->fitPos();
1197   if( fit_pos < 0 || fit_pos >= Fit::propagationMatrix().NoLines()) {
1198     std::cerr << "!!EXITING at saveParamFittedSigma: fit position incorrect " <<
1199       fit_pos << "propagationMatrix size =" << Fit::propagationMatrix().NoLines() << opto_name << std::endl;
1200     exit(3);
1201   }
1202   std::cout << entry_name << "saveParamFittedSigma" << fit_pos << sqrt(Fit::propagationMatrix()( fit_pos, fit_pos)) << std::endl;
1203   theParamFittedSigmaVector.push_back( sqrt(Fit::propagationMatrix()( fit_pos, fit_pos)) );
1204   //-    Fit::propagationMatrix().Dump(" the 2PropagationMatrix");
1205   */
1206 }
1207 
1208 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1209 //@@
1210 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1211 void Model::saveParamFittedCorrelation(const ALIstring& opto_name1,
1212                                        const ALIstring& entry_name1,
1213                                        const ALIstring& opto_name2,
1214                                        const ALIstring& entry_name2) {
1215   /*
1216 //---------- Get sigma of param Optical
1217   Entry* entry1 = getEntryByName(opto_name1, entry_name1);
1218   Entry* entry2 = getEntryByName(opto_name2, entry_name2);
1219   ALIuint fit_pos1 = entry1->fitPos();
1220   ALIuint fit_pos2 = entry2->fitPos();
1221 
1222   //---------- Get correlation if entry has been fitted
1223   ALIuint pmsize = Fit::propagationMatrix().NoLines();
1224   if( fit_pos1 >= 0 && fit_pos1 < pmsize && fit_pos2 >= 0 && fit_pos2 < pmsize ) {
1225     ALIdouble error1 = sqrt( Fit::propagationMatrix()( fit_pos1, fit_pos1) );
1226     ALIdouble error2 = sqrt( Fit::propagationMatrix()( fit_pos2, fit_pos2) );
1227     ALIdouble correl = Fit::propagationMatrix()( fit_pos1, fit_pos2) / error1 / error2;
1228     theParamFittedSigmaVector.push_back( correl );
1229     if(ALIUtils::debug>=9) {
1230       std::cout  << "saveParamFittedCorre" << opto_name1 << entry_name1 << fit_pos1 <<
1231         opto_name2 << entry_name2 << fit_pos2 << "MM " << correl << std::endl;
1232     }
1233   } else {
1234     if(ALIUtils::debug>=9) {
1235       std::cout  << "NOsaveParamFittedCorre" << opto_name1 << entry_name1 << fit_pos1 <<
1236         opto_name2 << entry_name2 << fit_pos2 << "MM " << std::endl;
1237     theParamFittedSigmaVector.push_back( 0. );
1238     }
1239 
1240   }
1241   //-    Fit::propagationMatrix().Dump(" the 2PropagationMatrix");
1242   */
1243 }
1244 
1245 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1246 //@@
1247 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1248 void Model::recoverParamFittedSigma(const ALIstring& opto_name, const ALIstring& entry_name, const ALIuint position) {
1249   /*
1250   if( position >= theParamFittedSigmaVector.size() ) {
1251     std::cerr << "!!EXITING at recoverParamFittedSigma: position" << position <<
1252  " bigger than dimension of theParamFittedSigmaVector " << theParamFittedSigmaVector.size() << std::endl;
1253     exit(3);
1254   }
1255   ALIdouble sigma = getParamFittedSigmaVectorItem( position );
1256 
1257   Entry* entry = getEntryByName(opto_name, entry_name);
1258   entry->setSigma( sigma );
1259   entry->setQuality( 1 );
1260   std::cout << "recover " << opto_name << entry_name << entry->sigma() <<std::endl;
1261   */
1262 }
1263 
1264 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1265 //@@  getParamFittedSigmaVectorItem
1266 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1267 ALIdouble Model::getParamFittedSigmaVectorItem(const ALIuint position) {
1268   if (position >= theParamFittedSigmaVector.size()) {
1269     std::cerr << "!!EXITING at getParamFittedSigma: position" << position
1270               << " bigger than dimension of theParamFittedSigmaVector " << theParamFittedSigmaVector.size()
1271               << std::endl;
1272     exit(3);
1273   }
1274   std::vector<ALIdouble>::const_iterator vdcite = theParamFittedSigmaVector.begin() + position;
1275   return (*vdcite);
1276 }
1277 
1278 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1279 //@@  getParamFittedSigmaVectorItem
1280 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1281 ALIbool Model::readMeasurementsFromFile(ALIstring only1Date, ALIstring only1Time) {
1282   if (ALIUtils::debug >= 5)
1283     std::cout << " readMeasurementsFromFile " << Measurement::measurementsFileName() << std::endl;
1284   if (Measurement::measurementsFileName().empty())
1285     return true;
1286 
1287   ALIFileIn& filein = ALIFileIn::getInstance(Measurement::measurementsFileName());
1288   std::vector<ALIstring> wordlist;
1289   //---------- read date
1290   //  ALIint retfil = filein.getWordsInLine(wordlist);
1291   // std::cout << "@@@@@@@@@@@@@@@ RETFIL " << retfil << std::endl;
1292   //if( retfil == 0 ) {
1293   if (filein.getWordsInLine(wordlist) == 0) {
1294     if (ALIUtils::debug >= 4)
1295       std::cout << "@@@@ No more measurements left" << std::endl;
1296     return false;
1297   }
1298 
1299   ////--- Transform to time_t format and save it
1300   //  struct tm tim;
1301   //t Model::setMeasurementsTime( tim );
1302 
1303   //if you are looking for only one measurement with a given date and time, loop all measurements until you find it
1304   if (Measurement::only1) {
1305     for (;;) {
1306       if (wordlist[0] == "DATE:" && wordlist[1] == Measurement::only1Date && wordlist[2] == Measurement::only1Time)
1307         break;
1308       filein.getWordsInLine(wordlist);
1309       if (filein.eof()) {
1310         std::cerr << "!! EXITING date not found in measurements file" << Measurement::only1Date << " "
1311                   << Measurement::only1Time << std::endl;
1312         exit(1);
1313       }
1314     }
1315   }
1316 
1317   //set date and time of current measurement
1318   if (wordlist[0] == "DATE:") {
1319     Measurement::setCurrentDate(wordlist);
1320   }
1321 
1322   //---------- loop measurements
1323   ALIint nMeas = Model::MeasurementList().size();
1324   if (ALIUtils::debug >= 4) {
1325     std::cout << " Reading " << nMeas << " measurements from file " << Measurement::measurementsFileName()
1326               << " DATE: " << wordlist[1] << " " << wordlist[1] << std::endl;
1327   }
1328   ALIint ii;
1329   for (ii = 0; ii < nMeas; ii++) {
1330     filein.getWordsInLine(wordlist);
1331     if (wordlist[0] == ALIstring("SENSOR2D") || wordlist[0] == ALIstring("TILTMETER") ||
1332         wordlist[0] == ALIstring("DISTANCEMETER") || wordlist[0] == ALIstring("DISTANCEMETER1DIM") ||
1333         wordlist[0] == ALIstring("COPS")) {
1334       if (wordlist.size() != 2) {
1335         std::cerr << "!!!EXITING Model::readMeasurementsFromFile. number of words should be 2 instead of "
1336                   << wordlist.size() << std::endl;
1337         ALIUtils::dumpVS(wordlist, " ");
1338         exit(1);
1339       }
1340       std::vector<Measurement*>::const_iterator vmcite;
1341       for (vmcite = MeasurementList().begin(); vmcite != MeasurementList().end(); ++vmcite) {
1342         //-------- Measurement found, fill data
1343         /*      ALIint last_slash =  (*vmcite)->name().rfind('/');
1344         ALIstring oname = (*vmcite)->name();
1345         if( last_slash != -1 ) {
1346           oname = oname.substr(last_slash+1, (*vmcite)->name().size()-1);
1347           }
1348         */
1349         ALIint fcolon = (*vmcite)->name().find(':');
1350         ALIstring oname = (*vmcite)->name();
1351         oname = oname.substr(fcolon + 1, oname.length());
1352         //-    std::cout << " measurement name " << (*vmcite)->name() << " short " << oname << std::endl;
1353         if (oname == wordlist[1]) {
1354           //-   std::cout << " measurement name found " << oname << std::endl;
1355           if ((*vmcite)->type() != wordlist[0]) {
1356             std::cerr << "!!! Reading measurement from file: type in file is " << wordlist[0] << " and should be "
1357                       << (*vmcite)->type() << std::endl;
1358             exit(1);
1359           }
1360           Measurement* meastemp = *vmcite;
1361 
1362           GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
1363           ALIbool sigmaFF = gomgr->GlobalOptions()["measurementErrorFromFile"];
1364           //---------- Read the data
1365           for (ALIuint ii = 0; ii < meastemp->dim(); ii++) {
1366             filein.getWordsInLine(wordlist);
1367             ALIdouble sigma = 0.;
1368             if (!sigmaFF) {
1369               // keep the sigma, do not read it from file
1370               const ALIdouble* sigmav = meastemp->sigma();
1371               sigma = sigmav[ii];
1372             }
1373             //---- Check measurement value type is OK
1374             if (meastemp->valueType(ii) != wordlist[0]) {
1375               filein.ErrorInLine();
1376               std::cerr << "!!!FATAL ERROR: Measurement value type is " << wordlist[0]
1377                         << " while in setup definition was " << meastemp->valueType(ii) << std::endl;
1378               exit(1);
1379             }
1380             meastemp->fillData(ii, wordlist);
1381             if (!sigmaFF) {
1382               meastemp->setSigma(ii, sigma);
1383             }
1384           }
1385           meastemp->correctValueAndSigma();
1386           break;
1387         }
1388       }
1389       if (vmcite == MeasurementList().end()) {
1390         for (vmcite = MeasurementList().begin(); vmcite != MeasurementList().end(); ++vmcite) {
1391           std::cerr << "MEAS: " << (*vmcite)->name() << " " << (*vmcite)->type() << std::endl;
1392         }
1393         std::cerr << "!!! Reading measurement from file: measurement not found in list: type in file is " << wordlist[1]
1394                   << std::endl;
1395         exit(1);
1396       }
1397     } else {
1398       std::cerr << " wrong type of measurement: " << wordlist[0] << std::endl
1399                 << " Available types are SENSOR2D, TILTMETER, DISTANCEMETER, DISTANCEMETER1DIM, COPS" << std::endl;
1400       exit(1);
1401     }
1402   }
1403   //-  std::cout << " returning readmeasff" << std::endl;
1404 
1405   return true;
1406 }
1407 
1408 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1409 
1410 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1411 void Model::copyMeasurements(const std::vector<ALIstring>& wl) {
1412   //----- Check format, e.g.  @copy_measurements _1/_2/*_1
1413 
1414   //----- get list of Measurement's that satisfy the query in their name
1415   //t  std::string querystr = wl[1].substr( wl[1].find("/")+1, wl[1].length() );
1416   std::string subsstr1 = wl[1].substr(0, wl[1].find('/'));
1417   std::string subsstr2 = wl[1].substr(wl[1].find('/') + 1, wl[1].rfind('/') - wl[1].find('/') - 1);
1418   std::string querystr = wl[1].substr(wl[1].rfind('/') + 1, wl[1].length());
1419 
1420   std::cout << " Model::copyMeasurements "
1421             << " subsstr1 " << subsstr1 << " subsstr2 " << subsstr2 << " querystr " << querystr << std::endl;
1422 
1423   std::vector<Measurement*> measToCopy;
1424   std::vector<Measurement*>::iterator mite;
1425   for (mite = theMeasurementVector.begin(); mite != theMeasurementVector.end(); ++mite) {
1426     Measurement* meas = (*mite);
1427     //improve this
1428     if (meas->name().find(querystr) != std::string::npos) {
1429       measToCopy.push_back(meas);
1430     }
1431   }
1432 
1433   //---- Build new measurements
1434   Measurement* meastemp = nullptr;
1435   for (mite = measToCopy.begin(); mite != measToCopy.end(); ++mite) {
1436     Measurement* meas = (*mite);
1437     std::vector<ALIstring> wlt;
1438     wlt.push_back(meas->type());
1439 
1440     //---- create new name
1441     std::string newName = ALIUtils::changeName(meas->name(), subsstr1, subsstr2);
1442     std::cout << " newName " << newName << std::endl;
1443     wlt.push_back(newName);
1444 
1445     ALIstring measType = wlt[0];
1446     ALIstring measName;
1447     if (wlt.size() == 2) {
1448       measName = wlt[1];
1449     } else {
1450       measName = "";
1451     }
1452     if (meas->type() == ALIstring("SENSOR2D")) {
1453       meastemp = new MeasurementSensor2D(2, measType, measName);
1454       //          } else if ( meas->type() == ALIstring("DISTANCEMETER3DIM") ) {
1455       //            meastemp = new MeasurementDistancemeter3dim( 1, measType, measName );
1456     } else if (meas->type() == ALIstring("DISTANCEMETER") || meas->type() == ALIstring("DISTANCEMETER1DIM")) {
1457       meastemp = new MeasurementDistancemeter(1, measType, measName);
1458     } else if (meas->type() == ALIstring("TILTMETER")) {
1459       meastemp = new MeasurementTiltmeter(1, measType, measName);
1460       // } else if ( meas->type() == ALIstring("DIFFCENTRE") ) {
1461       //   meastemp = new MeasurementDiffCentre( 1, measType, measName );
1462       // } else if ( meas->type() == ALIstring("DIFFANGLE") ) {
1463       //   meastemp = new MeasurementDiffAngle( 1, measType, measName );
1464     } else if (meas->type() == ALIstring("DIFFENTRY")) {
1465       meastemp = new MeasurementDiffEntry(1, measType, measName);
1466     } else if (meas->type() == ALIstring("COPS")) {
1467       meastemp = new MeasurementCOPS(4, measType, measName);
1468     } else {
1469       throw cms::Exception("LogicError") << "@SUB=Model::copyMeasurements\n"
1470                                          << "unknown measurement type: " << meas->type();
1471     }
1472 
1473     //later        meastemp->copyConversionFactor( wordlist );
1474     meastemp->copyMeas(meas, subsstr1, subsstr2);
1475 
1476     break;
1477   }
1478 }
1479 
1480 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1481 void Model::SetValueDisplacementsFromReportOut() {
1482   if (ALIUtils::debug >= 3)
1483     std::cout << " Model::SetValueDisplacementsFromReportOut() " << std::endl;
1484 
1485   EntryMgr* entryMgr = EntryMgr::getInstance();
1486 
1487   if (entryMgr->numberOfEntries() != 0) {
1488     EntryData* entryData;
1489 
1490     std::vector<Entry*>::const_iterator vecite;
1491     for (vecite = Model::EntryList().begin(); vecite != Model::EntryList().end(); ++vecite) {
1492       //----- Find the EntryData corresponding to this entry
1493       entryData = entryMgr->findEntryByLongName((*vecite)->OptOCurrent()->longName(), (*vecite)->name());
1494       if (ALIUtils::debug >= 3)
1495         std::cout << "SetValueDisplacementsFromReportOut " << (*vecite)->OptOCurrent()->longName() << " "
1496                   << (*vecite)->name() << " " << entryData->valueDisplacement() << std::endl;
1497       (*vecite)->addFittedDisplacementToValue(entryData->valueDisplacement());
1498     }
1499   }
1500 }
1501 
1502 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1503 std::string Model::printCocoaStatus(const cocoaStatus cs) {
1504   std::string str = "";
1505 
1506   if (cs == COCOA_Init) {
1507     str = "COCOA_Init ";
1508   } else if (cs == COCOA_ReadingModel) {
1509     str = "COCOA_ReadingModel";
1510   } else if (cs == COCOA_InitFit) {
1511     str = "COCOA_InitFit";
1512   } else if (cs == COCOA_FitOK) {
1513     str = "COCOA_FitOK";
1514   } else if (cs == COCOA_FitImproving) {
1515     str = "COCOA_FitImproving";
1516   } else if (cs == COCOA_FitCannotImprove) {
1517     str = "COCOA_FitCannotImprove";
1518   } else if (cs == COCOA_FitChi2Worsened) {
1519     str = "COCOA_FitChi2Worsened";
1520   } else if (cs == COCOA_FitMatrixNonInversable) {
1521     str = "COCOA_FitMatrixNonInversable";
1522   }
1523 
1524   return str;
1525 }
1526 
1527 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1528 void Model::BuildSystemDescriptionFromOA(OpticalAlignments& optAlig) {
1529   theOpticalAlignments = optAlig.opticalAlignments();
1530 
1531   OpticalAlignInfo oai_system = FindOptAlignInfoByType("system");
1532 
1533   OpticalObject* OptOsystem = new OpticalObject(nullptr, "system", oai_system.name_, false);
1534 
1535   OptOsystem->constructFromOptAligInfo(oai_system);
1536 
1537   //-              Model::_OptOtree.insert( std::multimap< ALIstring, OpticalObject*, std::less<ALIstring> >::value_type(OptOsystem->type(), OptOsystem) );
1538   //              theOptOlist[OptOsystem->name()] = OptOsystem;
1539   theOptOList.push_back(OptOsystem);
1540 }
1541 
1542 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1543 OpticalAlignInfo Model::FindOptAlignInfoByType(const ALIstring& type) {
1544   OpticalAlignInfo oai;
1545 
1546   ALIbool bFound = false;
1547   std::vector<OpticalAlignInfo>::iterator ite;
1548   for (ite = theOpticalAlignments.begin(); ite != theOpticalAlignments.end(); ++ite) {
1549     //    std::cout << " Model::FindOptAlignInfoByType " <<  (*ite).type_ << " =? " << type << std::endl;
1550     if ((*ite).type_ == type) {
1551       if (!bFound) {
1552         oai = *ite;
1553         bFound = true;
1554       } else {
1555         std::cerr << "!! WARNING: Model::FindOptAlignInfoByType more than one objects of type " << type << std::endl;
1556         std::cerr << " returning object " << oai.name_ << std::endl << " skipping object " << (*ite).name_ << std::endl;
1557       }
1558     }
1559   }
1560   if (!bFound) {
1561     std::cerr << "!! ERROR: Model::FindOptAlignInfoByType object not found, of type " << type << std::endl;
1562     std::exception();
1563   }
1564 
1565   return oai;
1566 }
1567 
1568 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1569 void Model::BuildMeasurementsFromOA(OpticalAlignMeasurements& measList) {
1570   std::vector<OpticalAlignMeasurementInfo>::iterator mite;
1571 
1572   if (ALIUtils::debug >= 5)
1573     std::cout << " BuildMeasurementsFromOA " << std::endl;
1574   std::vector<OpticalAlignMeasurementInfo> measInfos = measList.oaMeasurements_;
1575   for (mite = measInfos.begin(); mite != measInfos.end(); ++mite) {
1576     std::string measType = (*mite).type_;
1577     std::string measName = (*mite).name_;
1578     if (ALIUtils::debug >= 4)
1579       std::cout << " BuildMeasurementsFromOA measType " << measType << " measName " << measName << std::endl;
1580     //---------- Create Measurement with appropiate dimension
1581     Measurement* meastemp = nullptr;
1582     if (measType == ALIstring("SENSOR2D")) {
1583       meastemp = new MeasurementSensor2D(2, measType, measName);
1584     } else if (measType == ALIstring("DISTANCEMETER3DIM")) {
1585       meastemp = new MeasurementDistancemeter3dim(1, measType, measName);
1586     } else if (measType == ALIstring("DISTANCEMETER") || measType == ALIstring("DISTANCEMETER1DIM")) {
1587       meastemp = new MeasurementDistancemeter(1, measType, measName);
1588     } else if (measType == ALIstring("TILTMETER")) {
1589       meastemp = new MeasurementTiltmeter(1, measType, measName);
1590     } else if (measType == ALIstring("COPS")) {
1591       meastemp = new MeasurementCOPS(4, measType, measName);
1592       // } else if ( measType == ALIstring("DIFFCENTRE") ) {
1593       //   meastemp = new MeasurementDiffCentre( 1, measType, measName );
1594       // } else if ( measType == ALIstring("DIFFANGLE") ) {
1595       //   meastemp = new MeasurementDiffAngle( 2, measType, measName );
1596     } else if (measType == ALIstring("DIFFENTRY")) {
1597       meastemp = new MeasurementDiffEntry(1, measType, measName);
1598     } else {
1599       std::cerr << " !!! Model::BuildMeasurementsFromOA : measType not found " << measType << std::endl;
1600       throw std::exception();
1601     }
1602     meastemp->constructFromOA(*mite);
1603   }
1604 }