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