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