CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/Alignment/CocoaModel/src/Measurement.cc

Go to the documentation of this file.
00001 // COCOA class implementation file
00002 // Id:  Measurement.cc
00003 // CAT: Model
00004 // ---------------------------------------------------------------------------
00005 // History: v1.0 
00006 // Authors:
00007 //   Pedro Arce
00008 
00009 #include "Alignment/CocoaModel/interface/Model.h"
00010 
00011 #include <algorithm>
00012 #include <iomanip> 
00013 #include <iostream>
00014 #include <iterator>
00015 //#include <algo.h>
00016 #include <cstdlib>
00017 
00018 #include "Alignment/CocoaModel/interface/Entry.h"
00019 #include "Alignment/CocoaUtilities/interface/ALIFileIn.h"
00020 #include "Alignment/CocoaUtilities/interface/GlobalOptionMgr.h"
00021 #include "Alignment/CocoaModel/interface/Measurement.h"
00022 #include "Alignment/CocoaModel/interface/OpticalObject.h"
00023 #include "Alignment/CocoaModel/interface/ParameterMgr.h"
00024 #ifdef COCOA_VIS
00025 #include "Alignment/CocoaVisMgr/interface/ALIVRMLMgr.h"
00026 #include "Alignment/IgCocoaFileWriter/interface/IgCocoaFileMgr.h"
00027 #endif
00028 #include "CondFormats/OptAlignObjects/interface/OpticalAlignMeasurementInfo.h"
00029 #include "CondFormats/OptAlignObjects/interface/OpticalAlignInfo.h"
00030 
00031 
00032 ALIdouble Measurement::cameraScaleFactor = 1.;
00033 ALIstring Measurement::theMeasurementsFileName = "";
00034 ALIstring Measurement::theCurrentDate = "99/99/99";
00035 ALIstring Measurement::theCurrentTime = "99:99";
00036 
00037 ALIbool Measurement::only1 = 0;
00038 ALIstring Measurement::only1Date = "";
00039 ALIstring Measurement::only1Time = "";
00040 
00041 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00042 //@@ constructor:
00043 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00044 Measurement::Measurement( const ALIint measdim, ALIstring& type, ALIstring& name ) 
00045 : theDim(measdim), theType(type), theName( name )
00046 {
00047   //  _OptOnames = new ALIstring[theDim];
00048   theValue = new ALIdouble[theDim];
00049   theSigma = new ALIdouble[theDim];
00050   theValueType = new ALIstring[theDim];
00051 
00052   theValueSimulated = new ALIdouble[theDim];
00053   theValueSimulated_orig = new ALIdouble[theDim];
00054   theValueIsSimulated = new ALIbool[theDim];
00055 
00056 }
00057 
00058 
00059 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00060 //@@ construct (read from file)
00061 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00062 void Measurement::construct() 
00063 {
00064 
00065   ALIFileIn& filein = ALIFileIn::getInstance( Model::SDFName() );
00066 
00067   //---------- Read OptOs that take part in this Measurement
00068   std::vector<ALIstring> wordlist;
00069   filein.getWordsInLine( wordlist );
00070 
00071   //--------- Fill the list of names of OptOs that take part in this measurement ( names only )
00072   buildOptONamesList( wordlist );
00073   
00074   if(ALIUtils::debug >= 3) {
00075     std::cout << "@@@@ Reading Measurement " << name() << " TYPE= " << type() << std::endl
00076               << " MEASURED OPTO NAMES: ";
00077     std::ostream_iterator<ALIstring> outs(std::cout," ");
00078     copy(wordlist.begin(), wordlist.end(), outs);
00079     std::cout << std::endl;
00080   }
00081 
00082 
00083   //---------- Read the data 
00084   for ( unsigned int ii=0; ii<dim(); ii++){
00085     filein.getWordsInLine( wordlist );
00086     fillData( ii, wordlist );
00087   }
00088 
00089   if( !valueIsSimulated(0) ) correctValueAndSigma();
00090 
00091   postConstruct();
00092 }
00093 
00094 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00095 void Measurement::constructFromOA( OpticalAlignMeasurementInfo&  measInfo) 
00096 {
00097   //---- Build wordlist to build object name list
00098   std::vector<std::string> objNames = measInfo.measObjectNames_;
00099   std::vector<std::string>::const_iterator site;
00100   std::vector<ALIstring> wordlist;
00101   //--- Fill the list of names of OptOs that take part in this measurement ( names only )
00102   for( site = objNames.begin(); site != objNames.end(); site++) {
00103     if( site != objNames.begin() ) wordlist.push_back("&");
00104     wordlist.push_back(*site);
00105   }
00106   buildOptONamesList( wordlist );
00107 
00108   if(ALIUtils::debug >= 3) {
00109     std::cout << "@@@@ Reading Measurement " << name() << " TYPE= " << type() << " " << measInfo << std::endl
00110               << " MEASURED OPTO NAMES: ";
00111     for( size_t ii = 0; ii < _OptONameList.size(); ii++ ){
00112       std::cout << _OptONameList[ii] << " ";
00113     }
00114     std::cout << std::endl;
00115   }
00116 
00117   //---------- No data, set to simulated_value
00118   for ( unsigned int ii=0; ii<dim(); ii++){
00119     wordlist.clear();
00120     wordlist.push_back( (measInfo.values_)[ii].name_ );
00121     char ctmp[20];
00122     if( measInfo.isSimulatedValue_[ii] ){
00123       if ( ALIUtils::debug >= 5 ) {
00124         std::cout << "Measurement::constructFromOA:  meas value " << ii << " "  << dim() << " = simulated_value" << std::endl;
00125       }
00126       wordlist.push_back("simulated_value");
00127     } else { 
00128       if ( ALIUtils::debug >= 5 ) {
00129         std::cout << "Measurement::constructFromOA:  meas value " << ii << " "  << dim() << " = " << measInfo.values_.size() << std::endl;
00130       }
00131       ALIdouble val = (measInfo.values_)[ii].value_ / valueDimensionFactor(); //in XML  values are without dimensions, so neutralize multiplying by valueDimensionFactor() in fillData
00132       gcvt( val, 10, ctmp );
00133       wordlist.push_back( ctmp );
00134     }
00135     ALIdouble err = (measInfo.values_)[ii].error_ / sigmaDimensionFactor(); //in XML  values are without dimensions, so neutralize multiplying by valueDimensionFactor() in fillData
00136     gcvt( err, 10, ctmp );
00137     wordlist.push_back( ctmp );
00138     std::cout << " sigma " << err << " = " << ctmp << " " << (measInfo.values_)[ii].error_ << std::endl;
00139     //-    wordlist.push_back( "simulated_value" );
00140     //-   wordlist.push_back( "1." );
00141       if ( ALIUtils::debug >= 5 ) ALIUtils::dumpVS(wordlist, " Measurement: calling fillData ");
00142     //-    std::cout << " MEAS INFO " << measInfo << std::endl;
00143     //-   std::cout << ii << " MEAS INFO PARAM " <<  (measInfo.values_)[ii] << std::endl;
00144     //- std::cout << ii << " MEAS INFO PARAM VALUE " <<  (measInfo.values_)[ii].value_ << std::endl;
00145     fillData( ii, wordlist );
00146   }
00147 
00148   postConstruct();
00149 
00150 }
00151 
00152 
00153 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00154 void Measurement::postConstruct()
00155 {
00156   //---------- Set name as name of last OptO 
00157   setName();
00158 
00159   //---------- Transform for each Measurement the Measured OptO names to Measured OptO pointers
00160   buildOptOList();
00161 
00162   //---------- Build list of Entries that affect a Measurement 
00163   buildAffectingEntryList();
00164   
00165   //---------- add this measurement to the global list of measurements
00166   Model::addMeasurementToList( this );
00167 
00168   if ( ALIUtils::debug >= 10 ) {
00169     std::cout << Model::MeasurementList().size() << std::endl;
00170   }
00171 }
00172 
00173 
00174 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00175 //@@ Fills a list of names of OpticalObjects that take part in this measurement
00176 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00177 void Measurement::buildOptONamesList( const std::vector<ALIstring>& wl ) 
00178 {
00179 
00180   int NPairs = (wl.size()+1)/2;   // Number of OptO names ( pair of name and '&' )
00181 
00182   //--------- Fill list with names 
00183   for ( int ii=0; ii<NPairs; ii++ ) {
00184     _OptONameList.push_back( wl[ii*2] );
00185     // Check for separating '&'
00186     if (ii != NPairs-1 && wl[2*ii+1] != ALIstring("&") ) {
00187       //      ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00188       std::cerr << "!!! Measured Optical Objects should be separated by '&', not by" 
00189                 << wl[2*ii+1] << std::endl; 
00190       exit(2);
00191     }
00192   }
00193  
00194 }
00195 
00196 
00197 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00198 //@@ Fill the data of measurement coordinate 'coor' with values in 'wordlist'
00199 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00200 void Measurement::fillData( ALIuint coor, const std::vector<ALIstring>& wordlist) 
00201 {
00202   if ( ALIUtils::debug >= 3 ) {
00203     std::cout << "@@ Filling coordinate " << coor << std::endl ;
00204     //-   ostream_iterator<ALIstring> outs(std::cout," ");
00205     //-  copy(wordlist.begin(), wordlist.end(), outs);
00206   }
00207   
00208   ParameterMgr* parmgr = ParameterMgr::getInstance();
00209 
00210   //---------- Check that there are 3 attributes: name, value, error
00211   if( wordlist.size() != 3 ) {
00212     //    ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00213     std::cerr << " Incorrect format for Measurement value:" << std::endl; 
00214     std::ostream_iterator<ALIstring> outs(std::cout," ");
00215     copy(wordlist.begin(), wordlist.end(), outs);
00216     std::cout << std::endl << "There should be three words: name value sigma " << std::endl;
00217     exit(2);
00218   }
00219 
00220   //---------- check coor value
00221   if (coor >= theDim ) {
00222     // ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00223     std::cerr << "Trying to fill Measurement coordinate No "
00224          << coor << " but the dimension is " << theDim << std::endl; 
00225     exit(2);
00226   }
00227 
00228   //---------- set data members 
00229   //----- Set valueType
00230   theValueType[coor] = wordlist[0];
00231 
00232   //----- Set value (translate it if a PARAMETER is used)
00233   ALIdouble val = 0.;
00234   theValueIsSimulated[coor] = 0;
00235   if( !ALIUtils::IsNumber(wordlist[1]) ) {
00236     if ( parmgr->getParameterValue( wordlist[1], val ) == 0 ) {
00237       if( wordlist[1] == ALIstring("simulated_value") ) {
00238         theValueIsSimulated[coor] = 1;
00239       } else {
00240         //      ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00241         std::cerr << "!!! parameter for value not found: " << wordlist[1].c_str() << std::endl;
00242         exit(2);
00243       }
00244     }
00245     //d val *= valueDimensionFactor();
00246   } else {
00247     //d val = DimensionMgr()::getInstance()->extractValue( wordlist[1], ValueDimensionFactor() );
00248     val = atof( wordlist[1].c_str() ); 
00249   }
00250   val *= valueDimensionFactor();
00251   if( ALIUtils::debug >= 3 ) std::cout << "Meas VALUE= " << val << " (ValueDimensionFactor= " << valueDimensionFactor() <<std::endl;
00252 
00253   //----- Set sigma (translate it if a PARAMETER is used)
00254   ALIdouble sig = 0.;
00255   if( !ALIUtils::IsNumber(wordlist[2]) ) {
00256     if ( parmgr->getParameterValue( wordlist[2], sig ) == 0 ) {
00257       // ALIFileIn::getInstance( Model::SDFName() ).ErrorInLine();
00258       std::cerr << "!!! parameter for sigma not found: " << wordlist[2].c_str() << std::endl;
00259       exit(2);
00260     }
00261     //d sig *= sigmaDimensionFactor();
00262   } else {
00263     //    sig = DimensionMgr()::getInstance()->extractValue( wordlist[2], ValueDimensionFactor() );
00264     sig = atof( wordlist[2].c_str() );  
00265   }
00266   sig *= sigmaDimensionFactor();
00267   if( ALIUtils::debug >= 3) std::cout << "SIGMA= " << sig << " (SigmaDimensionFactor= " << sigmaDimensionFactor() <<std::endl;
00268   
00269   //----- set theValue & theSigma
00270   theValue[coor] = val;
00271   theSigma[coor] = sig;
00272 
00273 }
00274 
00275 
00276 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00277 void Measurement::fillData( ALIuint coor, OpticalAlignParam* oaParam) 
00278 {
00279   if ( ALIUtils::debug >= 3 ) {
00280     std::cout << "@@ Filling coordinate " << coor << std::endl ;
00281   }
00282   
00283   //  ParameterMgr* parmgr = ParameterMgr::getInstance();
00284 
00285   //---------- check coor value
00286   if (coor >= theDim ) {
00287     std::cerr << "Trying to fill Measurement coordinate No "
00288          << coor << " but the dimension is " << theDim << std::endl; 
00289     exit(2);
00290   }
00291 
00292   //---------- set data members 
00293   //----- Set value (translate it if a PARAMETER is used)
00294   ALIdouble val = 0.;
00295   theValueIsSimulated[coor] = 0;
00296   val = oaParam->value();
00297   val *= valueDimensionFactor();
00298   theValue[coor] = val;
00299   if( ALIUtils::debug >= 3 ) std::cout << "Meas VALUE= " << val << " (ValueDimensionFactor= " << valueDimensionFactor() <<std::endl;
00300 
00301   ALIbool sigmaFF = GlobalOptionMgr::getInstance()->GlobalOptions()["measurementErrorFromFile"];
00302   if( sigmaFF ) {
00303     //----- Set sigma (translate it if a PARAMETER is used)
00304     ALIdouble sig = 0.;
00305     sig = oaParam->sigma(); // it is in mm always
00306     sig *= sigmaDimensionFactor();
00307     theSigma[coor] = sig;
00308     if( ALIUtils::debug >= 3) std::cout << "SIGMA= " << sig << " (SigmaDimensionFactor= " << sigmaDimensionFactor() <<std::endl;
00309    
00310   }
00311 }
00312 
00313 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00314 //@@ Once the complete list of OptOs is read, convert OptO names to pointers
00315 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00316 void Measurement::buildOptOList()
00317 {
00318   //-  if ( ALIUtils::debug >= 3 ) std::cout << std::endl << " MEASUREMENT: " << " " << this->name() << std::endl; 
00319   ALIstring twopoints(".."); // .. goes one level up in the tree fo OptOs
00320 
00321 //---------------------------------------- Loop OptONameList
00322   std::vector<ALIstring>::iterator vsite;
00323   for (vsite = _OptONameList.begin();
00324        vsite != _OptONameList.end(); vsite++) {
00325 //----------------------------------- Count how many '..' there are in the name
00326     ALIuint ii = 0;
00327     //    ALIuint slen = (*vsite).length();
00328     ALIuint Ntwopoints = 0;    //---- No '..' in ALIstring
00329     for(;;) {
00330       int i2p = (*vsite).find_first_of( twopoints, 3*ii ); // if it is ., it also finds it!!!
00331       if ( i2p < 0 ) break;
00332       if ( i2p != ALIint(3*ii)) {
00333         std::cerr << i2p << "!!! Bad position of '..' in reference ALIstring: " 
00334              << (*vsite).c_str() << std::endl; 
00335         exit(2);
00336       } else {
00337         Ntwopoints++;
00338         if ( ALIUtils::debug >=9 ) std::cout << "N2p" << Ntwopoints;
00339       }
00340       ii++;   
00341     }     
00342     //------ Substitute '..' by reference (the last OptO in list)
00343     if (Ntwopoints != 0) {
00344       Substitute2p( (*vsite), *(_OptONameList.end()-1), Ntwopoints);
00345     }
00346     //----- Get OpticalObject* that correspond to ALIstring and fill list
00347     ALIstring referenceOptO = (*vsite);
00348     //--- a ':' is used in OptOs that have several possible behavious
00349     ALIint colon = referenceOptO.find(':');
00350     if ( colon != -1 ) {
00351       if (ALIUtils::debug >=99) { 
00352         std::cout << "colon in reference OptO name " << colon << 
00353           referenceOptO.c_str() << std::endl;
00354       }
00355       referenceOptO = referenceOptO.substr( 0, colon );
00356     }
00357     OpticalObject* OptOitem = Model::getOptOByName( referenceOptO );
00358     if ( ALIUtils::debug >= 3 ) std::cout << "Measurement::buildOptOList: OptO in Measurement: " << OptOitem->name() << std::endl;
00359     if ( OptOitem != (OpticalObject*)0 ) {
00360       _OptOList.push_back( OptOitem);
00361     } else {
00362       std::cerr << "!!! Error in Measurement: can't find Optical Object " <<
00363         (*vsite).c_str() << std::endl;
00364       exit(2);
00365     }      
00366   }
00367 }
00368 
00369 
00370 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00371 //@@ Build the list of all entries of every OptO that take part in this 
00372 //@@ Measurement and also the list of all entries of their OptO ancestors
00373 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00374 void Measurement::buildAffectingEntryList(){
00375   
00376   //---------- Loop OptO MeasuredList
00377   std::vector< OpticalObject* >::const_iterator vocite;
00378   for (vocite = _OptOList.begin();
00379        vocite != _OptOList.end(); vocite++) {
00380       addAffectingEntriesFromOptO( *vocite );
00381   }
00382 }
00383 
00384 
00385 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00386 //@@ Get the list of all entries of this OptO that take part in this Measurement
00387 //@@ and of their OptO ancestors
00388 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00389 void Measurement::addAffectingEntriesFromOptO( const OpticalObject* optoP )
00390 {
00391   if(ALIUtils::debug >= 3)  std::cout << "Measurement::addAffectingEntriesFromOptO: OptO taking part in Measurement: " << optoP->name() << std::endl;
00392       //---------- Loop entries in this OptO           
00393   std::vector< Entry* >::const_iterator vecite;
00394   std::vector< Entry* >::const_iterator fvecite;
00395   for (vecite = optoP->CoordinateEntryList().begin();
00396       vecite != optoP->CoordinateEntryList().end(); vecite++) {
00397     //T     if( find( theAffectingEntryList.begin(), theAffectingEntryList.end(), (*vecite) ) == theAffectingEntryList.end() ){
00398     //t      theAffectingEntryList.push_back(*vecite);
00399     //T    }
00400     fvecite = find( theAffectingEntryList.begin(), theAffectingEntryList.end(), (*vecite) );
00401     if (fvecite == theAffectingEntryList.end() ){
00402       theAffectingEntryList.push_back(*vecite);
00403       if(ALIUtils::debug >= 4)  std::cout << "Entry that may affect Measurement: " << (*vecite)->name() << std::endl;
00404     }
00405   }
00406   for (vecite = optoP->ExtraEntryList().begin();
00407       vecite != optoP->ExtraEntryList().end(); vecite++) {
00408     fvecite = find( theAffectingEntryList.begin(), theAffectingEntryList.end(), (*vecite) );
00409     if (fvecite == theAffectingEntryList.end() ){
00410       theAffectingEntryList.push_back(*vecite);
00411       if(ALIUtils::debug >= 4)  std::cout << "Entry that may affect Measurement: " << (*vecite)->name() << std::endl;
00412     }
00413   }
00414   if(optoP->parent() != 0) {
00415     addAffectingEntriesFromOptO( optoP->parent() );
00416   }
00417 }
00418 
00419 
00420 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00421 //@@ Substitute '..' in 'ref' by name of parent OptO ('firstref')
00422 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00423 void Measurement::Substitute2p( ALIstring& ref, const ALIstring& firstref, int Ntwopoints) 
00424 {
00425   // '/' sets hierarchy of OptOs
00426   ALIstring slash("/");
00427   
00428   int pos1st = firstref.length();
00429   // Go back an '/' in firstref for each '..' in ref
00430   for (int ii=0; ii < Ntwopoints; ii++) {
00431       pos1st = firstref.find_last_of( slash, pos1st-1);
00432       if ( ALIUtils::debug >=9 ) std::cout << "pos1st=" << pos1st;
00433   }
00434 
00435   if ( ALIUtils::debug >=9 ) std::cout << "before change ref: " << ref << " 1ref " << firstref << std::endl;
00436   // Substitute name
00437   ref.replace( 0, (Ntwopoints*3)-1, firstref, 0, pos1st);
00438   if ( ALIUtils::debug >=9 ) std::cout << "after change ref: " << ref << " 1ref " << firstref << std::endl;
00439 
00440 }
00441 
00442  
00443 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00444 void Measurement::printStartCalculateSimulatedValue( const Measurement* meas) 
00445 {
00446   std::cout << std::endl << "@@@ Start calculation of simulated value of " << meas->type() << " Measurement " << meas->name() << std::endl;
00447 }
00448 
00449 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00450 //@@ Calculate the simulated value of the Measurement propagating the LightRay when all the entries have their original values
00451 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00452 void Measurement::calculateOriginalSimulatedValue() 
00453 {
00454   //----------  Calculate the simulated value of the Measurement
00455   calculateSimulatedValue( 1 );
00456 
00457 #ifdef COCOA_VIS
00458   if( ALIUtils::getFirstTime() ) {
00459     GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00460     if(gomgr->GlobalOptions()["VisWriteVRML"] > 1) {
00461       ALIVRMLMgr::getInstance().newLightRay();
00462     }
00463     /*-    if(Model::GlobalOptions()["VisWriteIguana"] > 1) {
00464            IgCocoaFileMgr::getInstance().newLightPath( theName );
00465            } */
00466   }
00467 #endif
00468 
00469   //---------- Set original simulated values to it
00470   //-  if(ALIUtils::debug >= 5) std::cout << "MEAS DIMENSION" << dim() << std::endl;
00471   for ( ALIuint ii = 0; ii < dim(); ii++) {
00472     setValueSimulated_orig( ii, valueSimulated(ii) );
00473     if ( ALIUtils::debug >= 4 ) std::cout << "SETsimuvalOriginal" << valueSimulated(ii) << std::endl;
00474     //----- If Measurement has as value 'simulated_value', set the value to the simulated one
00475     if( valueIsSimulated(ii) == 1 ){
00476       setValue( ii, valueSimulated(ii) );
00477       //- std::cout << ii << " setting value as simulated " <<  valueSimulated(ii) << " " << value(ii) << this << std::endl;
00478     }
00479   }
00480  
00481 }
00482 
00483 
00484 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00485 //@@ 
00486 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00487 void Measurement::DumpBadOrderOptOs() 
00488 {
00489   std::cerr << " Detector can not make measurement with these optical objects " << std::endl;
00490   if (ALIUtils::debug >= 1) {
00491     //  std::vector<OpticalObject*>::iterator voite;
00492     //      for ( voite = _OptOList.begin(); 
00493     //     voite != _OptOList.end(); voite++) {
00494     //    std::cout << (*voite)->type() << " : " << (*voite)->name() << std::endl;
00495     // }
00496     std::vector<ALIstring>::const_iterator vsite;
00497     for ( vsite = OptONameList().begin(); 
00498           vsite != OptONameList().end(); vsite++) {
00499       std::cerr << (*vsite) << " : " ;
00500     }
00501     std::cerr << std::endl;
00502   }
00503   exit(2);
00504 
00505 }
00506 
00507 
00508 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00509 //@@ 
00510 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00511 std::vector<ALIdouble> Measurement::DerivativeRespectEntry( Entry* entry )
00512 {
00513   //---------- std::vector of derivatives to return
00514   std::vector<ALIdouble> deriv;
00515   ALIdouble sumderiv;
00516   
00517   //---------- displacement to start with 
00518   ALIdouble displacement = entry->startingDisplacement();
00519   //----- all angles are in radians, so, if displace is not, rescale it before making the displacement 
00520   //-  displacement *= entry->SigmaDimensionFactor();
00521   if( ALIUtils::debug >= 3) std::cout << std::endl << "%%% Derivative w.r.t. entry " << entry->name() << ": displacement = " << displacement << std::endl;
00522   
00523   ALIint count_itera = 0; 
00524   
00525   //---------- Loop decreasing the displacement a factor 2, until the precision set is reached
00526   do {
00527     count_itera++;
00528     entry->displace( displacement );
00529     
00530     if ( ALIUtils::debug >= 5) std::cout << "Get simulated value for displacement " << displacement << std::endl;
00531     calculateSimulatedValue( 0 ); 
00532     
00533     //---------- Get sum of derivatives
00534     sumderiv = 0;   
00535     for ( ALIuint ii = 0; ii < theDim; ii++) {
00536       sumderiv += fabs( theValueSimulated[ii] - theValueSimulated_orig[ii] );
00537       if( ALIUtils::debug >= 4 ) {
00538         std::cout << "iteration " << count_itera << " COOR " << ii 
00539              << " difference =" << ( theValueSimulated[ii] - theValueSimulated_orig[ii] )
00540           //-             << "  " << theValueSimulated[ii] << "  " << theValueSimulated_orig[ii] 
00541              << " derivative = " << (theValueSimulated[ii] - theValueSimulated_orig[ii]) /displacement << " disp " << displacement 
00542              << " sum derivatives = " << sumderiv << std::endl;
00543       }
00544       if( ALIUtils::debug >= 5 ) {
00545         std::cout << " new simu value= " << theValueSimulated[ii] << " orig simu value " << theValueSimulated_orig[ii] << std::endl;
00546        }
00547     }
00548     if (count_itera >= 100) {
00549       std::cerr << "EXITING: too many iterations in derivative, displacement is " <<
00550         displacement << " sum of derivatives is " << sumderiv << std::endl;
00551       exit(3);   
00552     }
00553     displacement /= 2.; 
00554     //-    std::cout << "sumderiv " << sumderiv << " maximu " <<  Fit::maximum_deviation_derivative << std::endl;
00555   }while( sumderiv > ALIUtils::getMaximumDeviationDerivative() );
00556   displacement *= 2; 
00557   
00558   //---------- Enough precision reached: pass result
00559   for ( ALIuint ii = 0; ii < theDim; ii++) {
00560     deriv.push_back( ( theValueSimulated[ii] - theValueSimulated_orig[ii] ) / displacement );
00561     //----- change it to entry sigma dimensions
00562     //     deriv[ii] /= entry->SigmaDimensionFactor();
00563     if( ALIUtils::debug >= 1) std::cout << name() << ": " <<  entry->OptOCurrent()->name() << " " <<  entry->name() << " " << ii << "### DERIVATIVE: " << deriv[ii] <<  std::endl;
00564   }
00565   //-  if(ALIUtils::debug >= 5) std::cout << "END derivative: " << deriv << "disp" << displacement << std::endl;
00566   
00567   //--------------------- Reset _centreGlob and _rmGlob of OptO entry belongs to (and component OptOs)
00568   entry->OptOCurrent()->resetGlobalCoordinates();
00569   
00570   return deriv;
00571  
00572 }
00573 
00574 
00575 
00576 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00577 //@@ destructor
00578 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00579 Measurement::~Measurement() 
00580 {
00581   //  delete[] _name; 
00582   delete[] theValue;
00583   delete[] theSigma;
00584 
00585 }
00586 
00587 
00588 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00589 //@@ get the ':X' that determines how the behaviour of the OptO w.r.t. this Measurement 
00590 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00591 ALIstring Measurement::getMeasuringBehaviour( const std::vector< OpticalObject* >::const_iterator vocite ){ 
00592   std::vector<ALIstring>::const_iterator vscite = _OptONameList.begin() +
00593     ( vocite - _OptOList.begin() ); // point to corresponding name of this OptO
00594   ALIint colon = (*vscite).find(':');
00595   ALIstring behav;
00596   if(colon != -1 ) {
00597     behav = (*vscite).substr(colon+1,(*vscite).size());
00598   } else {
00599     behav = " ";
00600   }
00601   return behav;
00602 }
00603 
00604 
00605 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00606 //@@ Get the previous OptOs in the list of OptO that take part in this measurement
00607 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00608 const OpticalObject* Measurement::getPreviousOptO( const OpticalObject* Popto ) const 
00609 {
00610   //--------- Loop OptOs that take part in this measurement
00611   std::vector<OpticalObject*>::const_iterator vocite;
00612   for( vocite = _OptOList.begin(); vocite != _OptOList.end(); vocite++ ){
00613     if( *vocite == Popto ) {
00614       if( vocite == _OptOList.begin() ) {
00615         std::cerr << " ERROR in  getPreviousOptO of measurement " << name() << std::endl;
00616         std::cerr << " OptO " << Popto->name() << " is the first one " << std::endl;
00617         exit(1);
00618       } else {
00619         return *(vocite-1);
00620       }
00621     }
00622   }
00623   
00624   std::cerr << " ERROR in  getPreviousOptO of measurement " << name() << std::endl;
00625   std::cerr << " OptO " << Popto->name() << " not found " << std::endl;
00626   exit(1);
00627 }
00628 
00629 
00630 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00631 void Measurement::setCurrentDate( const std::vector<ALIstring>& wl )
00632 {
00633 
00634   if( wl.size() != 3 ){
00635     std::cerr << "!!!EXITING: reading DATE of measurements set: it must have three words, it is though " << std::endl;
00636     ALIUtils::dumpVS(wl, " ");
00637    exit(1);
00638   } else if(wl[0] != "DATE:" ){ 
00639     std::cerr << "!!!EXITING: reading DATE of measurements set: first word must be 'DATE:', it is though " << std::endl;
00640     ALIUtils::dumpVS( wl, " ");
00641     exit(1);
00642   } else {
00643     theCurrentDate = wl[1];
00644     theCurrentTime = wl[2];
00645  }
00646 } 
00647 
00648 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00649 void Measurement::copyMeas( Measurement* meas, const std::string& subsstr1, const std::string& subsstr2 )
00650 {
00651   theDim = meas->dim();
00652   theType = meas->type();
00653   theName =  ALIUtils::changeName( meas->name(), subsstr1, subsstr2);
00654 
00655    //  _OptOnames = new ALIstring[theDim];
00656   theValueSimulated = new ALIdouble[theDim];
00657   theValueSimulated_orig = new ALIdouble[theDim];
00658   theValueIsSimulated = new ALIbool[theDim];
00659   theValue = const_cast<ALIdouble*>(meas->value());
00660   theSigma = const_cast<ALIdouble*>(meas->sigma());
00661 
00662   unsigned int ii;
00663   for(ii = 0; ii < theDim; ii++) {
00664     theValueSimulated[ii] = meas->valueSimulated( ii );
00665     theValueSimulated_orig[ii] = meas->valueSimulated_orig( ii );
00666     theValueIsSimulated[ii] = meas->valueIsSimulated( ii );
00667   }
00668 
00669   //--------- Fill the list of names of OptOs that take part in this measurement ( names only )
00670 
00671   std::vector<std::string> wordlist;
00672   std::vector<OpticalObject*>& optolist = meas->OptOList();
00673   ALIuint nOptos = optolist.size();
00674   for ( ALIuint ii = 0; ii < nOptos; ii++ ) {
00675     wordlist.push_back( ALIUtils::changeName( optolist[ii]->longName(), subsstr1, subsstr2) );
00676     std::cout << " copymeas " << ALIUtils::changeName( optolist[ii]->longName(), subsstr1, subsstr2) << std::endl;
00677     if( ii != nOptos -1 ) wordlist.push_back("&");
00678   }
00679 
00680   buildOptONamesList( wordlist );
00681   
00682   if(ALIUtils::debug >= 3) {
00683     std::cout << "@@@@ Reading Measurement " << name() << " TYPE= " << type() << std::endl
00684               << " MEASURED OPTO NAMES: ";
00685     std::ostream_iterator<ALIstring> outs(std::cout," ");
00686     copy(wordlist.begin(), wordlist.end(), outs);
00687     std::cout << std::endl;
00688   }
00689 
00690 
00691   postConstruct();
00692 
00693 }
00694 
00695 
00696 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00697 void Measurement::setName()
00698 {
00699   // name already set by passing one argument with sensor type
00700   if( theName != "" ) return;
00701   if( _OptONameList.size() == 0) {
00702     std::cerr << " !!! Error in your code, you cannot ask for the name of the Measurement before the OptONameList is build " << std::endl;
00703     exit(9);
00704   }
00705   std::vector<ALIstring>::iterator vsite = (_OptONameList.end()-1);
00706   theName = type() + ":" + (*vsite);
00707 }