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:  Measurement.cc
0003 // CAT: Model
0004 // ---------------------------------------------------------------------------
0005 // History: v1.0
0006 // Authors:
0007 //   Pedro Arce
0008 
0009 #include "Alignment/CocoaModel/interface/Model.h"
0010 
0011 #include <algorithm>
0012 #include <iomanip>
0013 #include <iostream>
0014 #include <iterator>
0015 //#include <algo.h>
0016 #include <cstdlib>
0017 #include <cmath>  // include floating-point std::abs functions
0018 
0019 #include "Alignment/CocoaModel/interface/Entry.h"
0020 #include "Alignment/CocoaUtilities/interface/ALIFileIn.h"
0021 #include "Alignment/CocoaUtilities/interface/GlobalOptionMgr.h"
0022 #include "Alignment/CocoaModel/interface/Measurement.h"
0023 #include "Alignment/CocoaModel/interface/OpticalObject.h"
0024 #include "Alignment/CocoaModel/interface/ParameterMgr.h"
0025 #ifdef COCOA_VIS
0026 #include "Alignment/CocoaVisMgr/interface/ALIVRMLMgr.h"
0027 #include "Alignment/IgCocoaFileWriter/interface/IgCocoaFileMgr.h"
0028 #endif
0029 #include "CondFormats/OptAlignObjects/interface/OpticalAlignMeasurementInfo.h"
0030 #include "CondFormats/OptAlignObjects/interface/OpticalAlignInfo.h"
0031 
0032 ALIdouble Measurement::cameraScaleFactor = 1.;
0033 ALIstring Measurement::theMeasurementsFileName = "";
0034 ALIstring Measurement::theCurrentDate = "99/99/99";
0035 ALIstring Measurement::theCurrentTime = "99:99";
0036 
0037 ALIbool Measurement::only1 = false;
0038 ALIstring Measurement::only1Date = "";
0039 ALIstring Measurement::only1Time = "";
0040 
0041 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0042 //@@ constructor:
0043 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0044 Measurement::Measurement(const ALIint measdim, ALIstring& type, ALIstring& name)
0045     : theDim(measdim), theType(type), theName(name) {
0046   //  _OptOnames = new ALIstring[theDim];
0047   theValue = new ALIdouble[theDim];
0048   theSigma = new ALIdouble[theDim];
0049   theValueType = new ALIstring[theDim];
0050 
0051   theValueSimulated = new ALIdouble[theDim];
0052   theValueSimulated_orig = new ALIdouble[theDim];
0053   theValueIsSimulated = new ALIbool[theDim];
0054 }
0055 
0056 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0057 //@@ construct (read from file)
0058 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0059 void Measurement::construct() {
0060   ALIFileIn& filein = ALIFileIn::getInstance(Model::SDFName());
0061 
0062   //---------- Read OptOs that take part in this Measurement
0063   std::vector<ALIstring> wordlist;
0064   filein.getWordsInLine(wordlist);
0065 
0066   //--------- Fill the list of names of OptOs that take part in this measurement ( names only )
0067   buildOptONamesList(wordlist);
0068 
0069   if (ALIUtils::debug >= 3) {
0070     std::cout << "@@@@ Reading Measurement " << name() << " TYPE= " << type() << std::endl << " MEASURED OPTO NAMES: ";
0071     std::ostream_iterator<ALIstring> outs(std::cout, " ");
0072     copy(wordlist.begin(), wordlist.end(), outs);
0073     std::cout << std::endl;
0074   }
0075 
0076   //---------- Read the data
0077   for (unsigned int ii = 0; ii < dim(); ii++) {
0078     filein.getWordsInLine(wordlist);
0079     fillData(ii, wordlist);
0080   }
0081 
0082   if (!valueIsSimulated(0))
0083     correctValueAndSigma();
0084 
0085   postConstruct();
0086 }
0087 
0088 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0089 void Measurement::constructFromOA(OpticalAlignMeasurementInfo& measInfo) {
0090   //---- Build wordlist to build object name list
0091   std::vector<std::string> objNames = measInfo.measObjectNames_;
0092   std::vector<std::string>::const_iterator site;
0093   std::vector<ALIstring> wordlist;
0094   //--- Fill the list of names of OptOs that take part in this measurement ( names only )
0095   for (site = objNames.begin(); site != objNames.end(); ++site) {
0096     if (site != objNames.begin())
0097       wordlist.push_back("&");
0098     wordlist.push_back(*site);
0099   }
0100   buildOptONamesList(wordlist);
0101 
0102   if (ALIUtils::debug >= 3) {
0103     std::cout << "@@@@ Reading Measurement " << name() << " TYPE= " << type() << " " << measInfo << std::endl
0104               << " MEASURED OPTO NAMES: ";
0105     for (size_t ii = 0; ii < _OptONameList.size(); ii++) {
0106       std::cout << _OptONameList[ii] << " ";
0107     }
0108     std::cout << std::endl;
0109   }
0110 
0111   //---------- No data, set to simulated_value
0112   for (unsigned int ii = 0; ii < dim(); ii++) {
0113     wordlist.clear();
0114     wordlist.push_back((measInfo.values_)[ii].name_);
0115     char ctmp[20];
0116     if (measInfo.isSimulatedValue_[ii]) {
0117       if (ALIUtils::debug >= 5) {
0118         std::cout << "Measurement::constructFromOA:  meas value " << ii << " " << dim() << " = simulated_value"
0119                   << std::endl;
0120       }
0121       wordlist.push_back("simulated_value");
0122     } else {
0123       if (ALIUtils::debug >= 5) {
0124         std::cout << "Measurement::constructFromOA:  meas value " << ii << " " << dim() << " = "
0125                   << measInfo.values_.size() << std::endl;
0126       }
0127       ALIdouble val =
0128           (measInfo.values_)[ii].value_ /
0129           valueDimensionFactor();  //in XML  values are without dimensions, so neutralize multiplying by valueDimensionFactor() in fillData
0130       gcvt(val, 10, ctmp);
0131       wordlist.push_back(ctmp);
0132     }
0133     ALIdouble err =
0134         (measInfo.values_)[ii].error_ /
0135         sigmaDimensionFactor();  //in XML  values are without dimensions, so neutralize multiplying by valueDimensionFactor() in fillData
0136     gcvt(err, 10, ctmp);
0137     wordlist.push_back(ctmp);
0138     std::cout << " sigma " << err << " = " << ctmp << " " << (measInfo.values_)[ii].error_ << std::endl;
0139     //-    wordlist.push_back( "simulated_value" );
0140     //-   wordlist.push_back( "1." );
0141     if (ALIUtils::debug >= 5)
0142       ALIUtils::dumpVS(wordlist, " Measurement: calling fillData ");
0143     //-    std::cout << " MEAS INFO " << measInfo << std::endl;
0144     //-   std::cout << ii << " MEAS INFO PARAM " <<  (measInfo.values_)[ii] << std::endl;
0145     //- std::cout << ii << " MEAS INFO PARAM VALUE " <<  (measInfo.values_)[ii].value_ << std::endl;
0146     fillData(ii, wordlist);
0147   }
0148 
0149   postConstruct();
0150 }
0151 
0152 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0153 void Measurement::postConstruct() {
0154   //---------- Set name as name of last OptO
0155   setName();
0156 
0157   //---------- Transform for each Measurement the Measured OptO names to Measured OptO pointers
0158   buildOptOList();
0159 
0160   //---------- Build list of Entries that affect a Measurement
0161   buildAffectingEntryList();
0162 
0163   //---------- add this measurement to the global list of measurements
0164   Model::addMeasurementToList(this);
0165 
0166   if (ALIUtils::debug >= 10) {
0167     std::cout << Model::MeasurementList().size() << std::endl;
0168   }
0169 }
0170 
0171 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0172 //@@ Fills a list of names of OpticalObjects that take part in this measurement
0173 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0174 void Measurement::buildOptONamesList(const std::vector<ALIstring>& wl) {
0175   int NPairs = (wl.size() + 1) / 2;  // Number of OptO names ( pair of name and '&' )
0176 
0177   //--------- Fill list with names
0178   for (int ii = 0; ii < NPairs; ii++) {
0179     _OptONameList.push_back(wl[ii * 2]);
0180     // Check for separating '&'
0181     if (ii != NPairs - 1 && wl[2 * ii + 1] != ALIstring("&")) {
0182       //      ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
0183       std::cerr << "!!! Measured Optical Objects should be separated by '&', not by" << wl[2 * ii + 1] << std::endl;
0184       exit(2);
0185     }
0186   }
0187 }
0188 
0189 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0190 //@@ Fill the data of measurement coordinate 'coor' with values in 'wordlist'
0191 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0192 void Measurement::fillData(ALIuint coor, const std::vector<ALIstring>& wordlist) {
0193   if (ALIUtils::debug >= 3) {
0194     std::cout << "@@ Filling coordinate " << coor << std::endl;
0195     //-   ostream_iterator<ALIstring> outs(std::cout," ");
0196     //-  copy(wordlist.begin(), wordlist.end(), outs);
0197   }
0198 
0199   ParameterMgr* parmgr = ParameterMgr::getInstance();
0200 
0201   //---------- Check that there are 3 attributes: name, value, error
0202   if (wordlist.size() != 3) {
0203     //    ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
0204     std::cerr << " Incorrect format for Measurement value:" << std::endl;
0205     std::ostream_iterator<ALIstring> outs(std::cout, " ");
0206     copy(wordlist.begin(), wordlist.end(), outs);
0207     std::cout << std::endl << "There should be three words: name value sigma " << std::endl;
0208     exit(2);
0209   }
0210 
0211   //---------- check coor value
0212   if (coor >= theDim) {
0213     // ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
0214     std::cerr << "Trying to fill Measurement coordinate No " << coor << " but the dimension is " << theDim << std::endl;
0215     exit(2);
0216   }
0217 
0218   //---------- set data members
0219   //----- Set valueType
0220   theValueType[coor] = wordlist[0];
0221 
0222   //----- Set value (translate it if a PARAMETER is used)
0223   ALIdouble val = 0.;
0224   theValueIsSimulated[coor] = false;
0225   if (!ALIUtils::IsNumber(wordlist[1])) {
0226     if (parmgr->getParameterValue(wordlist[1], val) == 0) {
0227       if (wordlist[1] == ALIstring("simulated_value")) {
0228         theValueIsSimulated[coor] = true;
0229       } else {
0230         //      ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
0231         std::cerr << "!!! parameter for value not found: " << wordlist[1].c_str() << std::endl;
0232         exit(2);
0233       }
0234     }
0235     //d val *= valueDimensionFactor();
0236   } else {
0237     //d val = DimensionMgr()::getInstance()->extractValue( wordlist[1], ValueDimensionFactor() );
0238     val = atof(wordlist[1].c_str());
0239   }
0240   val *= valueDimensionFactor();
0241   if (ALIUtils::debug >= 3)
0242     std::cout << "Meas VALUE= " << val << " (ValueDimensionFactor= " << valueDimensionFactor() << std::endl;
0243 
0244   //----- Set sigma (translate it if a PARAMETER is used)
0245   ALIdouble sig = 0.;
0246   if (!ALIUtils::IsNumber(wordlist[2])) {
0247     if (parmgr->getParameterValue(wordlist[2], sig) == 0) {
0248       // ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
0249       std::cerr << "!!! parameter for sigma not found: " << wordlist[2].c_str() << std::endl;
0250       exit(2);
0251     }
0252     //d sig *= sigmaDimensionFactor();
0253   } else {
0254     //    sig = DimensionMgr()::getInstance()->extractValue( wordlist[2], ValueDimensionFactor() );
0255     sig = atof(wordlist[2].c_str());
0256   }
0257   sig *= sigmaDimensionFactor();
0258   if (ALIUtils::debug >= 3)
0259     std::cout << "SIGMA= " << sig << " (SigmaDimensionFactor= " << sigmaDimensionFactor() << std::endl;
0260 
0261   //----- set theValue & theSigma
0262   theValue[coor] = val;
0263   theSigma[coor] = sig;
0264 }
0265 
0266 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0267 void Measurement::fillData(ALIuint coor, OpticalAlignParam* oaParam) {
0268   if (ALIUtils::debug >= 3) {
0269     std::cout << "@@ Filling coordinate " << coor << std::endl;
0270   }
0271 
0272   //  ParameterMgr* parmgr = ParameterMgr::getInstance();
0273 
0274   //---------- check coor value
0275   if (coor >= theDim) {
0276     std::cerr << "Trying to fill Measurement coordinate No " << coor << " but the dimension is " << theDim << std::endl;
0277     exit(2);
0278   }
0279 
0280   //---------- set data members
0281   //----- Set value (translate it if a PARAMETER is used)
0282   ALIdouble val = 0.;
0283   theValueIsSimulated[coor] = false;
0284   val = oaParam->value();
0285   val *= valueDimensionFactor();
0286   theValue[coor] = val;
0287   if (ALIUtils::debug >= 3)
0288     std::cout << "Meas VALUE= " << val << " (ValueDimensionFactor= " << valueDimensionFactor() << std::endl;
0289 
0290   ALIbool sigmaFF = GlobalOptionMgr::getInstance()->GlobalOptions()["measurementErrorFromFile"];
0291   if (sigmaFF) {
0292     //----- Set sigma (translate it if a PARAMETER is used)
0293     ALIdouble sig = 0.;
0294     sig = oaParam->sigma();  // it is in mm always
0295     sig *= sigmaDimensionFactor();
0296     theSigma[coor] = sig;
0297     if (ALIUtils::debug >= 3)
0298       std::cout << "SIGMA= " << sig << " (SigmaDimensionFactor= " << sigmaDimensionFactor() << std::endl;
0299   }
0300 }
0301 
0302 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0303 //@@ Once the complete list of OptOs is read, convert OptO names to pointers
0304 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0305 void Measurement::buildOptOList() {
0306   //-  if ( ALIUtils::debug >= 3 ) std::cout << std::endl << " MEASUREMENT: " << " " << this->name() << std::endl;
0307   ALIstring twopoints("..");  // .. goes one level up in the tree fo OptOs
0308 
0309   //---------------------------------------- Loop OptONameList
0310   std::vector<ALIstring>::iterator vsite;
0311   for (vsite = _OptONameList.begin(); vsite != _OptONameList.end(); ++vsite) {
0312     //----------------------------------- Count how many '..' there are in the name
0313     ALIuint ii = 0;
0314     //    ALIuint slen = (*vsite).length();
0315     ALIuint Ntwopoints = 0;  //---- No '..' in ALIstring
0316     for (;;) {
0317       int i2p = (*vsite).find_first_of(twopoints, 3 * ii);  // if it is ., it also finds it!!!
0318       if (i2p < 0)
0319         break;
0320       if (i2p != ALIint(3 * ii)) {
0321         std::cerr << i2p << "!!! Bad position of '..' in reference ALIstring: " << (*vsite).c_str() << std::endl;
0322         exit(2);
0323       } else {
0324         Ntwopoints++;
0325         if (ALIUtils::debug >= 9)
0326           std::cout << "N2p" << Ntwopoints;
0327       }
0328       ii++;
0329     }
0330     //------ Substitute '..' by reference (the last OptO in list)
0331     if (Ntwopoints != 0) {
0332       Substitute2p((*vsite), *(_OptONameList.end() - 1), Ntwopoints);
0333     }
0334     //----- Get OpticalObject* that correspond to ALIstring and fill list
0335     ALIstring referenceOptO = (*vsite);
0336     //--- a ':' is used in OptOs that have several possible behavious
0337     ALIint colon = referenceOptO.find(':');
0338     if (colon != -1) {
0339       if (ALIUtils::debug >= 99) {
0340         std::cout << "colon in reference OptO name " << colon << referenceOptO.c_str() << std::endl;
0341       }
0342       referenceOptO = referenceOptO.substr(0, colon);
0343     }
0344     OpticalObject* OptOitem = Model::getOptOByName(referenceOptO);
0345     if (ALIUtils::debug >= 3)
0346       std::cout << "Measurement::buildOptOList: OptO in Measurement: " << OptOitem->name() << std::endl;
0347     if (OptOitem != (OpticalObject*)nullptr) {
0348       _OptOList.push_back(OptOitem);
0349     } else {
0350       std::cerr << "!!! Error in Measurement: can't find Optical Object " << (*vsite).c_str() << std::endl;
0351       exit(2);
0352     }
0353   }
0354 }
0355 
0356 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0357 //@@ Build the list of all entries of every OptO that take part in this
0358 //@@ Measurement and also the list of all entries of their OptO ancestors
0359 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0360 void Measurement::buildAffectingEntryList() {
0361   //---------- Loop OptO MeasuredList
0362   std::vector<OpticalObject*>::const_iterator vocite;
0363   for (vocite = _OptOList.begin(); vocite != _OptOList.end(); ++vocite) {
0364     addAffectingEntriesFromOptO(*vocite);
0365   }
0366 }
0367 
0368 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0369 //@@ Get the list of all entries of this OptO that take part in this Measurement
0370 //@@ and of their OptO ancestors
0371 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0372 void Measurement::addAffectingEntriesFromOptO(const OpticalObject* optoP) {
0373   if (ALIUtils::debug >= 3)
0374     std::cout << "Measurement::addAffectingEntriesFromOptO: OptO taking part in Measurement: " << optoP->name()
0375               << std::endl;
0376   //---------- Loop entries in this OptO
0377   std::vector<Entry*>::const_iterator vecite;
0378   std::vector<Entry*>::const_iterator fvecite;
0379   for (vecite = optoP->CoordinateEntryList().begin(); vecite != optoP->CoordinateEntryList().end(); ++vecite) {
0380     //T     if( find( theAffectingEntryList.begin(), theAffectingEntryList.end(), (*vecite) ) == theAffectingEntryList.end() ){
0381     //t      theAffectingEntryList.push_back(*vecite);
0382     //T    }
0383     fvecite = find(theAffectingEntryList.begin(), theAffectingEntryList.end(), (*vecite));
0384     if (fvecite == theAffectingEntryList.end()) {
0385       theAffectingEntryList.push_back(*vecite);
0386       if (ALIUtils::debug >= 4)
0387         std::cout << "Entry that may affect Measurement: " << (*vecite)->name() << std::endl;
0388     }
0389   }
0390   for (vecite = optoP->ExtraEntryList().begin(); vecite != optoP->ExtraEntryList().end(); ++vecite) {
0391     fvecite = find(theAffectingEntryList.begin(), theAffectingEntryList.end(), (*vecite));
0392     if (fvecite == theAffectingEntryList.end()) {
0393       theAffectingEntryList.push_back(*vecite);
0394       if (ALIUtils::debug >= 4)
0395         std::cout << "Entry that may affect Measurement: " << (*vecite)->name() << std::endl;
0396     }
0397   }
0398   if (optoP->parent() != nullptr) {
0399     addAffectingEntriesFromOptO(optoP->parent());
0400   }
0401 }
0402 
0403 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0404 //@@ Substitute '..' in 'ref' by name of parent OptO ('firstref')
0405 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0406 void Measurement::Substitute2p(ALIstring& ref, const ALIstring& firstref, int Ntwopoints) {
0407   // '/' sets hierarchy of OptOs
0408   ALIstring slash("/");
0409 
0410   int pos1st = firstref.length();
0411   // Go back an '/' in firstref for each '..' in ref
0412   for (int ii = 0; ii < Ntwopoints; ii++) {
0413     pos1st = firstref.find_last_of(slash, pos1st - 1);
0414     if (ALIUtils::debug >= 9)
0415       std::cout << "pos1st=" << pos1st;
0416   }
0417 
0418   if (ALIUtils::debug >= 9)
0419     std::cout << "before change ref: " << ref << " 1ref " << firstref << std::endl;
0420   // Substitute name
0421   ref.replace(0, (Ntwopoints * 3) - 1, firstref, 0, pos1st);
0422   if (ALIUtils::debug >= 9)
0423     std::cout << "after change ref: " << ref << " 1ref " << firstref << std::endl;
0424 }
0425 
0426 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0427 void Measurement::printStartCalculateSimulatedValue(const Measurement* meas) {
0428   std::cout << std::endl
0429             << "@@@ Start calculation of simulated value of " << meas->type() << " Measurement " << meas->name()
0430             << std::endl;
0431 }
0432 
0433 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0434 //@@ Calculate the simulated value of the Measurement propagating the LightRay when all the entries have their original values
0435 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0436 void Measurement::calculateOriginalSimulatedValue() {
0437   //----------  Calculate the simulated value of the Measurement
0438   calculateSimulatedValue(true);
0439 
0440 #ifdef COCOA_VIS
0441   if (ALIUtils::getFirstTime()) {
0442     GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
0443     if (gomgr->GlobalOptions()["VisWriteVRML"] > 1) {
0444       ALIVRMLMgr::getInstance().newLightRay();
0445     }
0446     /*-    if(Model::GlobalOptions()["VisWriteIguana"] > 1) {
0447            IgCocoaFileMgr::getInstance().newLightPath( theName );
0448            } */
0449   }
0450 #endif
0451 
0452   //---------- Set original simulated values to it
0453   //-  if(ALIUtils::debug >= 5) std::cout << "MEAS DIMENSION" << dim() << std::endl;
0454   for (ALIuint ii = 0; ii < dim(); ii++) {
0455     setValueSimulated_orig(ii, valueSimulated(ii));
0456     if (ALIUtils::debug >= 4)
0457       std::cout << "SETsimuvalOriginal" << valueSimulated(ii) << std::endl;
0458     //----- If Measurement has as value 'simulated_value', set the value to the simulated one
0459     if (valueIsSimulated(ii) == 1) {
0460       setValue(ii, valueSimulated(ii));
0461       //- std::cout << ii << " setting value as simulated " <<  valueSimulated(ii) << " " << value(ii) << this << std::endl;
0462     }
0463   }
0464 }
0465 
0466 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0467 //@@
0468 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0469 void Measurement::DumpBadOrderOptOs() {
0470   std::cerr << " Detector can not make measurement with these optical objects " << std::endl;
0471   if (ALIUtils::debug >= 1) {
0472     //  std::vector<OpticalObject*>::iterator voite;
0473     //      for ( voite = _OptOList.begin();
0474     //     voite != _OptOList.end(); voite++) {
0475     //    std::cout << (*voite)->type() << " : " << (*voite)->name() << std::endl;
0476     // }
0477     std::vector<ALIstring>::const_iterator vsite;
0478     for (vsite = OptONameList().begin(); vsite != OptONameList().end(); ++vsite) {
0479       std::cerr << (*vsite) << " : ";
0480     }
0481     std::cerr << std::endl;
0482   }
0483   exit(2);
0484 }
0485 
0486 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0487 //@@
0488 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0489 std::vector<ALIdouble> Measurement::DerivativeRespectEntry(Entry* entry) {
0490   //---------- std::vector of derivatives to return
0491   std::vector<ALIdouble> deriv;
0492   ALIdouble sumderiv;
0493 
0494   //---------- displacement to start with
0495   ALIdouble displacement = entry->startingDisplacement();
0496   //----- all angles are in radians, so, if displace is not, rescale it before making the displacement
0497   //-  displacement *= entry->SigmaDimensionFactor();
0498   if (ALIUtils::debug >= 3)
0499     std::cout << std::endl
0500               << "%%% Derivative w.r.t. entry " << entry->name() << ": displacement = " << displacement << std::endl;
0501 
0502   ALIint count_itera = 0;
0503 
0504   //---------- Loop decreasing the displacement a factor 2, until the precision set is reached
0505   do {
0506     count_itera++;
0507     entry->displace(displacement);
0508 
0509     if (ALIUtils::debug >= 5)
0510       std::cout << "Get simulated value for displacement " << displacement << std::endl;
0511     calculateSimulatedValue(false);
0512 
0513     //---------- Get sum of derivatives
0514     sumderiv = 0;
0515     for (ALIuint ii = 0; ii < theDim; ii++) {
0516       sumderiv += std::abs(theValueSimulated[ii] - theValueSimulated_orig[ii]);
0517       if (ALIUtils::debug >= 4) {
0518         std::cout << "iteration " << count_itera << " COOR " << ii << " difference ="
0519                   << (theValueSimulated[ii] - theValueSimulated_orig[ii])
0520                   //-             << "  " << theValueSimulated[ii] << "  " << theValueSimulated_orig[ii]
0521                   << " derivative = " << (theValueSimulated[ii] - theValueSimulated_orig[ii]) / displacement << " disp "
0522                   << displacement << " sum derivatives = " << sumderiv << std::endl;
0523       }
0524       if (ALIUtils::debug >= 5) {
0525         std::cout << " new simu value= " << theValueSimulated[ii] << " orig simu value " << theValueSimulated_orig[ii]
0526                   << std::endl;
0527       }
0528     }
0529     if (count_itera >= 100) {
0530       std::cerr << "EXITING: too many iterations in derivative, displacement is " << displacement
0531                 << " sum of derivatives is " << sumderiv << std::endl;
0532       exit(3);
0533     }
0534     displacement /= 2.;
0535     //-    std::cout << "sumderiv " << sumderiv << " maximu " <<  Fit::maximum_deviation_derivative << std::endl;
0536   } while (sumderiv > ALIUtils::getMaximumDeviationDerivative());
0537   displacement *= 2;
0538 
0539   //---------- Enough precision reached: pass result
0540   for (ALIuint ii = 0; ii < theDim; ii++) {
0541     deriv.push_back((theValueSimulated[ii] - theValueSimulated_orig[ii]) / displacement);
0542     //----- change it to entry sigma dimensions
0543     //     deriv[ii] /= entry->SigmaDimensionFactor();
0544     if (ALIUtils::debug >= 1)
0545       std::cout << name() << ": " << entry->OptOCurrent()->name() << " " << entry->name() << " " << ii
0546                 << "### DERIVATIVE: " << deriv[ii] << std::endl;
0547   }
0548   //-  if(ALIUtils::debug >= 5) std::cout << "END derivative: " << deriv << "disp" << displacement << std::endl;
0549 
0550   //--------------------- Reset _centreGlob and _rmGlob of OptO entry belongs to (and component OptOs)
0551   entry->OptOCurrent()->resetGlobalCoordinates();
0552 
0553   return deriv;
0554 }
0555 
0556 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0557 //@@ destructor
0558 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0559 Measurement::~Measurement() {
0560   //  delete[] _name;
0561   delete[] theValue;
0562   delete[] theSigma;
0563 }
0564 
0565 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0566 //@@ get the ':X' that determines how the behaviour of the OptO w.r.t. this Measurement
0567 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0568 ALIstring Measurement::getMeasuringBehaviour(const std::vector<OpticalObject*>::const_iterator vocite) {
0569   std::vector<ALIstring>::const_iterator vscite =
0570       _OptONameList.begin() + (vocite - _OptOList.begin());  // point to corresponding name of this OptO
0571   ALIint colon = (*vscite).find(':');
0572   ALIstring behav;
0573   if (colon != -1) {
0574     behav = (*vscite).substr(colon + 1, (*vscite).size());
0575   } else {
0576     behav = " ";
0577   }
0578   return behav;
0579 }
0580 
0581 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0582 //@@ Get the previous OptOs in the list of OptO that take part in this measurement
0583 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0584 const OpticalObject* Measurement::getPreviousOptO(const OpticalObject* Popto) const {
0585   //--------- Loop OptOs that take part in this measurement
0586   std::vector<OpticalObject*>::const_iterator vocite;
0587   for (vocite = _OptOList.begin(); vocite != _OptOList.end(); ++vocite) {
0588     if (*vocite == Popto) {
0589       if (vocite == _OptOList.begin()) {
0590         std::cerr << " ERROR in  getPreviousOptO of measurement " << name() << std::endl;
0591         std::cerr << " OptO " << Popto->name() << " is the first one " << std::endl;
0592         exit(1);
0593       } else {
0594         return *(vocite - 1);
0595       }
0596     }
0597   }
0598 
0599   std::cerr << " ERROR in  getPreviousOptO of measurement " << name() << std::endl;
0600   std::cerr << " OptO " << Popto->name() << " not found " << std::endl;
0601   exit(1);
0602 }
0603 
0604 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0605 void Measurement::setCurrentDate(const std::vector<ALIstring>& wl) {
0606   if (wl.size() != 3) {
0607     std::cerr << "!!!EXITING: reading DATE of measurements set: it must have three words, it is though " << std::endl;
0608     ALIUtils::dumpVS(wl, " ");
0609     exit(1);
0610   } else if (wl[0] != "DATE:") {
0611     std::cerr << "!!!EXITING: reading DATE of measurements set: first word must be 'DATE:', it is though " << std::endl;
0612     ALIUtils::dumpVS(wl, " ");
0613     exit(1);
0614   } else {
0615     theCurrentDate = wl[1];
0616     theCurrentTime = wl[2];
0617   }
0618 }
0619 
0620 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0621 void Measurement::copyMeas(Measurement* meas, const std::string& subsstr1, const std::string& subsstr2) {
0622   theDim = meas->dim();
0623   theType = meas->type();
0624   theName = ALIUtils::changeName(meas->name(), subsstr1, subsstr2);
0625 
0626   //  _OptOnames = new ALIstring[theDim];
0627   theValueSimulated = new ALIdouble[theDim];
0628   theValueSimulated_orig = new ALIdouble[theDim];
0629   theValueIsSimulated = new ALIbool[theDim];
0630   theValue = const_cast<ALIdouble*>(meas->value());
0631   theSigma = const_cast<ALIdouble*>(meas->sigma());
0632 
0633   unsigned int ii;
0634   for (ii = 0; ii < theDim; ii++) {
0635     theValueSimulated[ii] = meas->valueSimulated(ii);
0636     theValueSimulated_orig[ii] = meas->valueSimulated_orig(ii);
0637     theValueIsSimulated[ii] = meas->valueIsSimulated(ii);
0638   }
0639 
0640   //--------- Fill the list of names of OptOs that take part in this measurement ( names only )
0641 
0642   std::vector<std::string> wordlist;
0643   auto& optolist = meas->OptOList();
0644   ALIuint nOptos = optolist.size();
0645   for (ALIuint ii = 0; ii < nOptos; ii++) {
0646     wordlist.push_back(ALIUtils::changeName(optolist[ii]->longName(), subsstr1, subsstr2));
0647     std::cout << " copymeas " << ALIUtils::changeName(optolist[ii]->longName(), subsstr1, subsstr2) << std::endl;
0648     if (ii != nOptos - 1)
0649       wordlist.push_back("&");
0650   }
0651 
0652   buildOptONamesList(wordlist);
0653 
0654   if (ALIUtils::debug >= 3) {
0655     std::cout << "@@@@ Reading Measurement " << name() << " TYPE= " << type() << std::endl << " MEASURED OPTO NAMES: ";
0656     std::ostream_iterator<ALIstring> outs(std::cout, " ");
0657     copy(wordlist.begin(), wordlist.end(), outs);
0658     std::cout << std::endl;
0659   }
0660 
0661   postConstruct();
0662 }
0663 
0664 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0665 void Measurement::setName() {
0666   // name already set by passing one argument with sensor type
0667   if (!theName.empty())
0668     return;
0669   if (_OptONameList.empty()) {
0670     std::cerr
0671         << " !!! Error in your code, you cannot ask for the name of the Measurement before the OptONameList is build "
0672         << std::endl;
0673     exit(9);
0674   }
0675   std::vector<ALIstring>::iterator vsite = (_OptONameList.end() - 1);
0676   theName = type() + ":" + (*vsite);
0677 }