00001
00002
00003
00004
00005
00006
00007
00008 #include "Alignment/CocoaModel/interface/OptOSensor2D.h"
00009 #include "Alignment/CocoaModel/interface/LightRay.h"
00010 #include "Alignment/CocoaModel/interface/ALIPlane.h"
00011 #include "Alignment/CocoaModel/interface/Measurement.h"
00012 #include "Alignment/CocoaModel/interface/Model.h"
00013 #include "Alignment/CocoaModel/interface/Entry.h"
00014 #include "Alignment/CocoaModel/interface/DeviationsFromFileSensor2D.h"
00015 #ifdef COCOA_VIS
00016 #include "Alignment/CocoaVisMgr/interface/ALIVRMLMgr.h"
00017 #include "Alignment/IgCocoaFileWriter/interface/IgCocoaFileMgr.h"
00018 #endif
00019 #include "Alignment/CocoaUtilities/interface/ALIFileIn.h"
00020 #include "Alignment/CocoaDDLObjects/interface/CocoaSolidShapeBox.h"
00021 #include "Alignment/CocoaUtilities/interface/GlobalOptionMgr.h"
00022
00023 #include <iostream>
00024 #include <iomanip>
00025 #include <fstream>
00026 #include <cstdlib>
00027
00028 using namespace CLHEP;
00029
00030
00031
00032
00033 void OptOSensor2D::defaultBehaviour( LightRay& lightray, Measurement& meas )
00034 {
00035 makeMeasurement( lightray, meas);
00036 }
00037
00038
00039
00040
00041
00042 void OptOSensor2D::makeMeasurement( LightRay& lightray, Measurement& meas )
00043 {
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 CLHEP::Hep3Vector ZAxis(0.,0,1.);
00055 CLHEP::HepRotation rmt = rmGlob();
00056 ZAxis = rmt * ZAxis;
00057 if( ALIUtils::debug >= 4 ) {
00058 ALIUtils::dump3v( centreGlob(), " sensor2D centre Glob ");
00059 ALIUtils::dump3v( ZAxis, " snsor2D normal ");
00060 }
00061
00062 lightray.intersect( ALIPlane(centreGlob(), ZAxis) );
00063 CLHEP::Hep3Vector inters = lightray.point();
00064
00065 ALIdouble* interslc;
00066 interslc = convertPointToLocalCoordinates( inters );
00067 ALIdouble interslcx = interslc[0];
00068 ALIdouble interslcy = interslc[1];
00069 meas.setValueSimulated( 0, interslcx );
00070 meas.setValueSimulated( 1, interslcy );
00071
00072
00073 if (ALIUtils::debug >= 2) {
00074
00075 ALIstring chrg = "";
00076
00077
00078
00079
00080
00081 CLHEP::Hep3Vector measvv( meas.value()[0], meas.value()[1], 0.);
00082 measvv = rmt*measvv;
00083 ALIUtils::dump3v( measvv, " $$$$$$MEAS IN LOCAL FRAME");
00084 ALIUtils::dump3v( measvv+centreGlob(), " $$$$$$MEAS IN GLOBAL FRAME");
00085
00086 ALIdouble detH = 1000*meas.valueSimulated(0); if(fabs(detH) <= 1.e-9 ) detH = 0.;
00087 ALIdouble detV = 1000*meas.valueSimulated(1); if(fabs(detV) <= 1.e-9 ) detV = 0.;
00088 std::cout << "REAL value: " << chrg << meas.valueType(0) << ": " << 1000*meas.value()[0] << chrg << " " << meas.valueType(1) << ": " << 1000*meas.value()[1] << " (mm) " << (this)->name()
00089 << " DIFF= " << detH-1000*meas.value()[0] << " " << detV-1000*meas.value()[1] << std::endl;
00090 std::cout << "SIMU value: " << chrg << " " << meas.valueType(0) << ": "
00091
00092 << detH
00093 << chrg << " " << meas.valueType(1) << ": " << detV
00094 << " (mm) " << (this)->name() << std::endl;
00095
00096
00097
00098
00099
00100
00101
00102
00103 ALIUtils::dump3v( 1000.*(inters - parent()->centreGlob()) , " $$$$$$SIMU inters - parent centre");
00104 ALIUtils::dump3v( 1000.*(inters - centreGlob()) , " $$$$$$SIMU inters - centre");
00105 }
00106
00107
00108
00109 meas.setLightRayPosition( lightray.point() );
00110 meas.setLightRayDirection( lightray.direction() );
00111
00112 delete[] interslc;
00113 }
00114
00115
00116
00117
00118
00119 void OptOSensor2D::fastTraversesLightRay( LightRay& lightray )
00120 {
00121 verbose = ALIUtils::debug;
00122 if (ALIUtils::debug >= 2) std::cout << "LR: FAST TRAVERSES SENSOR2D " << name() << std::endl;
00123
00124
00125 ALIdouble shiftX = findExtraEntryValue("shiftX");
00126 ALIdouble shiftY = findExtraEntryValue("shiftY");
00127 ALIdouble shift;
00128 ALIbool bb = findExtraEntryValueIfExists("shift", shift);
00129 if( bb ) {
00130 shiftX = shift;
00131 shiftY = shift;
00132 }
00133
00134
00135 CLHEP::Hep3Vector ZAxis(0.,0,1.);
00136 CLHEP::HepRotation rmt = rmGlob();
00137 ZAxis = rmt * ZAxis;
00138 lightray.intersect( ALIPlane(centreGlob(), ZAxis) );
00139 CLHEP::Hep3Vector inters = lightray.point();
00140
00141 ALIdouble deviX, deviY, devi;
00142
00143
00144 if( DeviationsFromFileSensor2D::apply() && fdevi_from_file) {
00145
00146 if(ALIUtils::debug >= 4) {
00147 std::cout << "fdeviFromFile" << fdevi_from_file << std::endl;
00148
00149 }
00150
00151 std::vector< Measurement* >& measv = Model::MeasurementList();
00152 unsigned int ii;
00153 Measurement *omeas = 0;
00154 for( ii = 0; ii < measv.size(); ii++ ) {
00155
00156 if( measv[ii]->sensorName() == name() ) {
00157 omeas = measv[ii];
00158 break;
00159 }
00160 }
00161 if( omeas == 0 ) {
00162 std::cerr << "!!!EXITING OptOSensor2D::fastTraversesLightRay: meas " << name() << " not found " << std::endl;
00163 }
00164
00165 ALIdouble interslcx = omeas->value( 0 );
00166 ALIdouble interslcy = omeas->value( 1 );
00167 if(ALIUtils::debug >= 5) std::cout << " interslcx " << interslcx << " interslcy " << interslcy << std::endl;
00168
00169
00170
00171 ALIdouble df = ALIUtils::LengthValueDimensionFactor();
00172 interslcx = interslcx/df + 0.010/df;
00173 interslcy = interslcy/df + 0.010/df;
00174 if(ALIUtils::debug >= 5) std::cout << " interslcx " << interslcx << " interslcy " << interslcy << std::endl;
00175
00176
00177 std::pair< ALIdouble, ALIdouble> devis = deviFromFile->getDevis( interslcx, interslcy );
00178 deviX = devis.second;
00179 deviY = -devis.first;
00180
00181
00182
00183
00184
00185
00186
00187 ALIuint entryNo = extraEntryNo( "deviX" );
00188 if( verbose >= 3 ) std::cout << "entrynox" << entryNo << name() << verbose << std::endl;
00189 Entry* entryDeviX = *(ExtraEntryList().begin()+entryNo);
00190 entryDeviX->setValue( deviX );
00191
00192 std::vector< ALIdouble > eevil = ExtraEntryValueOriginalList();
00193
00194 std::vector< ALIdouble >::iterator eevolite = eevil.begin();
00195
00196 *(eevolite+entryNo) = deviX;
00197 if( verbose >= 3 ) std::cout<< " entryDeviX name " << entryDeviX->name() << entryDeviX->value() << std::endl;
00198 entryNo = extraEntryNo( "deviY" );
00199 Entry* entryDeviY = *(ExtraEntryList().begin()+entryNo);
00200
00201 entryDeviY->setValue( deviY );
00202 *(eevolite+entryNo) = deviY;
00203
00204
00205 } else {
00206 deviX = findExtraEntryValue("deviX");
00207 deviY = findExtraEntryValue("deviY");
00208
00209
00210 if( fdevi_from_file ) {
00211 if( ALIUtils::debug >= 5) std::cout << "fdeviFromFile" << fdevi_from_file << std::endl;
00212 ALIuint entryNo = extraEntryNo( "deviX" );
00213 Entry* entryDeviX = *(ExtraEntryList().begin()+entryNo);
00214 if( verbose >= 3 ) std::cout<< entryDeviX << " entryDeviX name " << entryDeviX->name() << entryDeviX->value() << std::endl;
00215 deviX = entryDeviX->value();
00216 entryNo = extraEntryNo( "deviY" );
00217 Entry* entryDeviY = *(ExtraEntryList().begin()+entryNo);
00218 if( verbose >= 3 ) std::cout<< entryDeviY << " entryDeviY name " << entryDeviY->name() << entryDeviY->value() << std::endl;
00219 deviY = entryDeviY->value();
00220
00221 } else {
00222 bb = findExtraEntryValueIfExists("devi", devi);
00223 if( bb ) {
00224 deviX = devi;
00225 deviY = devi;
00226 }
00227 }
00228 }
00229 if(ALIUtils::debug >= 4) {
00230 std::cout << "devi " << devi << " devi x " << deviX << " devi y " << deviY << std::endl;
00231 }
00232
00233 lightray.setPoint( inters );
00234
00235
00236 lightray.shiftAndDeviateWhileTraversing( this, 'T' );
00237 if (ALIUtils::debug >= 2) {
00238 lightray.dumpData("Shifted and Deviated");
00239 }
00240
00241 }
00242
00243
00244
00245
00246
00247 void OptOSensor2D::detailedTraversesLightRay( LightRay& lightray )
00248 {
00249 if (ALIUtils::debug >= 4) std::cout << "%%% LR: DETAILED TRAVERSES SENSOR2D " << name() << std::endl;
00250 if( DeviationsFromFileSensor2D::apply() && fdevi_from_file) {
00251 DeviationsFromFileSensor2D::setApply( 0 );
00252
00253 if(ALIUtils::debug >= 0 )std::cerr << "!!WARNING: sensor " << name() << " has read deviation from file and it will not be taken into account. Please use FAST TRAVERSES" << deviFromFile << std::endl;
00254 }
00255
00256
00257 ALIdouble width = findExtraEntryValue("width");
00258 if( width == 0 ) {
00259
00260 CLHEP::Hep3Vector ZAxis(0.,0,1.);
00261 CLHEP::HepRotation rmt = rmGlob();
00262 ZAxis = rmt * ZAxis;
00263 lightray.intersect( ALIPlane(centreGlob(), ZAxis) );
00264 CLHEP::Hep3Vector inters = lightray.point();
00265 lightray.setPoint( inters );
00266 if (ALIUtils::debug >= 2) {
00267 lightray.dumpData("LightRay Sensor2D traversed: ");
00268 }
00269 return;
00270 }
00271
00272 if (ALIUtils::debug >= 4) std::cout << std::endl << "$$$ LR: REFRACTION IN FORWARD PLATE " << std::endl;
00273
00274 ALIPlane plate = getPlate(1, 1);
00275
00276 ALIdouble refra_ind1 = 1.;
00277 ALIdouble refra_ind2 = findExtraEntryValueMustExist("refra_ind");
00278 lightray.refract( plate, refra_ind1, refra_ind2 );
00279
00280 if (ALIUtils::debug >= 4) std::cout << std::endl << "$$$ LR: REFRACTION IN BACKWARD PLATE " << std::endl;
00281
00282 plate = getPlate(0, 1);
00283
00284 lightray.refract( plate, refra_ind2, refra_ind1 );
00285
00286 CLHEP::Hep3Vector inters = lightray.point();
00287 lightray.setPoint( inters );
00288
00289 if (ALIUtils::debug >= 4) {
00290 lightray.dumpData("LightRay Sensor2D traversed: ");
00291 }
00292
00293
00294 }
00295
00296
00297
00298
00299
00300 void OptOSensor2D::fillExtraEntry( std::vector<ALIstring>& wordlist )
00301 {
00302
00303 if(ALIUtils::debug >= 5) std::cout << "OptOSensor2D fillExtraEntry wordlist[1] " << wordlist[1] << std::endl;
00304
00305 fdevi_from_file = 0;
00306
00307 if( wordlist[1] == ALIstring("devi") && wordlist[2] == ALIstring("from_file") ) {
00308
00309 ALIstring fnam;
00310 if( wordlist.size() >= 4) {
00311 fnam = wordlist[3];
00312 } else {
00313
00314 fnam = "dat/devi-";
00315 fnam += shortName();
00316 fnam += ".dat";
00317 }
00318
00319 ALIFileIn& ifdevi = ALIFileIn::getInstance( fnam );
00320
00321
00322 if(ALIUtils::debug >= 4) std::cout << "Opening deviation file: " << fnam << std::endl;
00323
00324
00325
00326
00327
00328 deviFromFile = new DeviationsFromFileSensor2D();
00329 fdevi_from_file = 1;
00330 if(ALIUtils::debug >= 5 ) std::cout << "deviFromFile " << deviFromFile << std::endl;
00331
00332 ALIstring sensor1_name, sensor2_name;
00333 ALIint sensor1_side, sensor2_side;
00334 ALIdouble sensor_dist;
00335 ALIdouble prec_deviX,prec_deviY;
00336
00337 std::vector<ALIstring> wl;
00338 ifdevi.getWordsInLine( wl );
00339 sensor1_name = wl[0];
00340 sensor2_name = wl[1];
00341 sensor_dist = atof( wl[2].c_str() );
00342
00343 sensor1_side = 1;
00344 if(sensor1_name[sensor1_name.size()-2] == 'c') {
00345 sensor1_side = 2;
00346 sensor1_name = sensor1_name.substr(0,sensor1_name.size()-1);
00347 }
00348 sensor2_side = 1;
00349 if(sensor2_name[sensor2_name.size()-2] == 'c') {
00350 sensor2_side = 2;
00351 sensor2_name = sensor2_name.substr(0,sensor2_name.size()-1);
00352 }
00353 if(ALIUtils::debug >= 5) std::cout << "sensor1_name " << sensor1_name << " sensor2_name " << sensor2_name << " sensor_dist " << sensor_dist << " unknown " << wl[3] << std::endl;
00354
00355 ifdevi.getWordsInLine( wl );
00356 prec_deviX = atof( wl[0].c_str() );
00357 prec_deviY = atof( wl[1].c_str() );
00358
00359 if(ALIUtils::debug >= 5) std::cout << "prec_deviX " << prec_deviX << " prec_deviY " << prec_deviY << std::endl;
00360
00361 deviFromFile = new DeviationsFromFileSensor2D();
00362 ALIdouble offsetX, offsetY;
00363 if( wl.size() == 5 ) {
00364 offsetX = ALIUtils::getFloat( wl[3] );
00365 offsetY = ALIUtils::getFloat( wl[4] );
00366 deviFromFile->setOffset( offsetX, offsetY );
00367 }
00368 deviFromFile->readFile( ifdevi );
00369 fdevi_from_file = 1;
00370 if(ALIUtils::debug >= 5 ) std::cout << "deviFromFile " << deviFromFile << std::endl;
00371
00372
00373
00374 std::vector<ALIstring> wlo;
00375 char chartmp[20];
00376 wlo.push_back( wordlist[0] );
00377 wlo.push_back("deviX");
00378 wlo.push_back("0");
00379 gcvt( prec_deviX, 10, chartmp );
00380 wlo.push_back( ALIstring(chartmp) );
00381 wlo.push_back("cal");
00382 std::vector<ALIstring> wl2(wlo);
00383 OpticalObject::fillExtraEntry( wlo );
00384
00385 wl2[1] = "deviY";
00386 gcvt( prec_deviY, 10, chartmp );
00387 wl2[3] = ALIstring( chartmp );
00388 OpticalObject::fillExtraEntry( wl2 );
00389
00390 } else {
00391 OpticalObject::fillExtraEntry( wordlist );
00392 }
00393 }
00394
00395
00396
00397
00398
00399 ALIdouble* OptOSensor2D::convertPointToLocalCoordinates( const CLHEP::Hep3Vector& point)
00400 {
00401 ALIdouble* interslc = new ALIdouble[2];
00402
00403
00404 CLHEP::HepRotation rmt = rmGlob();
00405 CLHEP::Hep3Vector XAxism(1.,0.,0.);
00406 XAxism*=rmt;
00407 if( ALIUtils::debug >= 5) ALIUtils::dump3v( (this)->centreGlob(), "centre glob sensor2D" );
00408 if( ALIUtils::debug >= 5) ALIUtils::dumprm( rmt, "rotation matrix sensor2D" );
00409
00410 if( ALIUtils::debug >= 5) ALIUtils::dump3v(XAxism , "XAxism");
00411 interslc[0] = (point - (this)->centreGlob() ) * XAxism;
00412
00413
00414 CLHEP::Hep3Vector YAxism(0.,1.,0.);
00415 YAxism*=rmt;
00416 if( ALIUtils::debug >= 5)
00417 ALIUtils::dump3v(YAxism , "YAxism");
00418 interslc[1] = (point - (this)->centreGlob() ) * YAxism;
00419
00420 if( ALIUtils::debug >= 5 ) {
00421 std::cout << " intersection in local coordinates: X= " << interslc[0] << " Y= " << interslc[1] << std::endl;
00422 ALIUtils::dump3v( point - (this)->centreGlob() , " inters - centre " );
00423 }
00424 return interslc;
00425 }
00426
00427 #ifdef COCOA_VIS
00428
00429 void OptOSensor2D::fillVRML()
00430 {
00431
00432 ALIVRMLMgr& vrmlmgr = ALIVRMLMgr::getInstance();
00433 ALIColour* col = new ALIColour( 0., 0., 1., 0. );
00434 vrmlmgr.AddBox( *this, 1., 1., .2, col);
00435 vrmlmgr.SendReferenceFrame( *this, 0.6);
00436 vrmlmgr.SendName( *this, 0.1 );
00437 }
00438
00439
00440
00441 void OptOSensor2D::fillIguana()
00442 {
00443 ALIColour* col = new ALIColour( 0., 0., 1., 0. );
00444 std::vector<ALIdouble> spar;
00445 spar.push_back(20.);
00446 spar.push_back(20.);
00447 spar.push_back(5.);
00448 IgCocoaFileMgr::getInstance().addSolid( *this, "BOX", spar, col);
00449 }
00450 #endif
00451
00452
00453
00454 void OptOSensor2D::constructSolidShape()
00455 {
00456 ALIdouble go;
00457 GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00458 gomgr->getGlobalOptionValue("VisScale", go );
00459
00460 theSolidShape = new CocoaSolidShapeBox( "Box", go*4.*cm/m, go*4.*cm/m, go*1.*cm/m );
00461 }