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
00126
00127 CLHEP::Hep3Vector ZAxis(0.,0,1.);
00128 CLHEP::HepRotation rmt = rmGlob();
00129 ZAxis = rmt * ZAxis;
00130 lightray.intersect( ALIPlane(centreGlob(), ZAxis) );
00131 CLHEP::Hep3Vector inters = lightray.point();
00132
00133 ALIdouble deviX, deviY, devi;
00134
00135
00136 if( DeviationsFromFileSensor2D::apply() && fdevi_from_file) {
00137
00138 if(ALIUtils::debug >= 4) {
00139 std::cout << "fdeviFromFile" << fdevi_from_file << std::endl;
00140
00141 }
00142
00143 std::vector< Measurement* >& measv = Model::MeasurementList();
00144 unsigned int ii;
00145 Measurement *omeas = 0;
00146 for( ii = 0; ii < measv.size(); ii++ ) {
00147
00148 if( measv[ii]->sensorName() == name() ) {
00149 omeas = measv[ii];
00150 break;
00151 }
00152 }
00153 if( omeas == 0 ) {
00154 std::cerr << "!!!EXITING OptOSensor2D::fastTraversesLightRay: meas " << name() << " not found " << std::endl;
00155 }
00156
00157 ALIdouble interslcx = omeas->value( 0 );
00158 ALIdouble interslcy = omeas->value( 1 );
00159 if(ALIUtils::debug >= 5) std::cout << " interslcx " << interslcx << " interslcy " << interslcy << std::endl;
00160
00161
00162
00163 ALIdouble df = ALIUtils::LengthValueDimensionFactor();
00164 interslcx = interslcx/df + 0.010/df;
00165 interslcy = interslcy/df + 0.010/df;
00166 if(ALIUtils::debug >= 5) std::cout << " interslcx " << interslcx << " interslcy " << interslcy << std::endl;
00167
00168
00169 std::pair< ALIdouble, ALIdouble> devis = deviFromFile->getDevis( interslcx, interslcy );
00170 deviX = devis.second;
00171 deviY = -devis.first;
00172
00173
00174
00175
00176
00177
00178
00179 ALIuint entryNo = extraEntryNo( "deviX" );
00180 if( verbose >= 3 ) std::cout << "entrynox" << entryNo << name() << verbose << std::endl;
00181 Entry* entryDeviX = *(ExtraEntryList().begin()+entryNo);
00182 entryDeviX->setValue( deviX );
00183
00184 std::vector< ALIdouble > eevil = ExtraEntryValueOriginalList();
00185
00186 std::vector< ALIdouble >::iterator eevolite = eevil.begin();
00187
00188 *(eevolite+entryNo) = deviX;
00189 if( verbose >= 3 ) std::cout<< " entryDeviX name " << entryDeviX->name() << entryDeviX->value() << std::endl;
00190 entryNo = extraEntryNo( "deviY" );
00191 Entry* entryDeviY = *(ExtraEntryList().begin()+entryNo);
00192
00193 entryDeviY->setValue( deviY );
00194 *(eevolite+entryNo) = deviY;
00195
00196
00197 } else {
00198 deviX = findExtraEntryValue("deviX");
00199 deviY = findExtraEntryValue("deviY");
00200
00201
00202 if( fdevi_from_file ) {
00203 if( ALIUtils::debug >= 5) std::cout << "fdeviFromFile" << fdevi_from_file << std::endl;
00204 ALIuint entryNo = extraEntryNo( "deviX" );
00205 Entry* entryDeviX = *(ExtraEntryList().begin()+entryNo);
00206 if( verbose >= 3 ) std::cout<< entryDeviX << " entryDeviX name " << entryDeviX->name() << entryDeviX->value() << std::endl;
00207 deviX = entryDeviX->value();
00208 entryNo = extraEntryNo( "deviY" );
00209 Entry* entryDeviY = *(ExtraEntryList().begin()+entryNo);
00210 if( verbose >= 3 ) std::cout<< entryDeviY << " entryDeviY name " << entryDeviY->name() << entryDeviY->value() << std::endl;
00211 deviY = entryDeviY->value();
00212
00213 } else {
00214 ALIbool bb = findExtraEntryValueIfExists("devi", devi);
00215 if( bb ) {
00216 deviX = devi;
00217 deviY = devi;
00218 }
00219 }
00220 }
00221 if(ALIUtils::debug >= 4) {
00222 std::cout << "devi " << devi << " devi x " << deviX << " devi y " << deviY << std::endl;
00223 }
00224
00225 lightray.setPoint( inters );
00226
00227 lightray.shiftAndDeviateWhileTraversing( this, 'T' );
00228 if (ALIUtils::debug >= 2) {
00229 lightray.dumpData("Shifted and Deviated");
00230 }
00231
00232 }
00233
00234
00235
00236
00237
00238 void OptOSensor2D::detailedTraversesLightRay( LightRay& lightray )
00239 {
00240 if (ALIUtils::debug >= 4) std::cout << "%%% LR: DETAILED TRAVERSES SENSOR2D " << name() << std::endl;
00241 if( DeviationsFromFileSensor2D::apply() && fdevi_from_file) {
00242 DeviationsFromFileSensor2D::setApply( 0 );
00243
00244 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;
00245 }
00246
00247
00248 ALIdouble width = findExtraEntryValue("width");
00249 if( width == 0 ) {
00250
00251 CLHEP::Hep3Vector ZAxis(0.,0,1.);
00252 CLHEP::HepRotation rmt = rmGlob();
00253 ZAxis = rmt * ZAxis;
00254 lightray.intersect( ALIPlane(centreGlob(), ZAxis) );
00255 CLHEP::Hep3Vector inters = lightray.point();
00256 lightray.setPoint( inters );
00257 if (ALIUtils::debug >= 2) {
00258 lightray.dumpData("LightRay Sensor2D traversed: ");
00259 }
00260 return;
00261 }
00262
00263 if (ALIUtils::debug >= 4) std::cout << std::endl << "$$$ LR: REFRACTION IN FORWARD PLATE " << std::endl;
00264
00265 ALIPlane plate = getPlate(1, 1);
00266
00267 ALIdouble refra_ind1 = 1.;
00268 ALIdouble refra_ind2 = findExtraEntryValueMustExist("refra_ind");
00269 lightray.refract( plate, refra_ind1, refra_ind2 );
00270
00271 if (ALIUtils::debug >= 4) std::cout << std::endl << "$$$ LR: REFRACTION IN BACKWARD PLATE " << std::endl;
00272
00273 plate = getPlate(0, 1);
00274
00275 lightray.refract( plate, refra_ind2, refra_ind1 );
00276
00277 CLHEP::Hep3Vector inters = lightray.point();
00278 lightray.setPoint( inters );
00279
00280 if (ALIUtils::debug >= 4) {
00281 lightray.dumpData("LightRay Sensor2D traversed: ");
00282 }
00283
00284
00285 }
00286
00287
00288
00289
00290
00291 void OptOSensor2D::fillExtraEntry( std::vector<ALIstring>& wordlist )
00292 {
00293
00294 if(ALIUtils::debug >= 5) std::cout << "OptOSensor2D fillExtraEntry wordlist[1] " << wordlist[1] << std::endl;
00295
00296 fdevi_from_file = 0;
00297
00298 if( wordlist[1] == ALIstring("devi") && wordlist[2] == ALIstring("from_file") ) {
00299
00300 ALIstring fnam;
00301 if( wordlist.size() >= 4) {
00302 fnam = wordlist[3];
00303 } else {
00304
00305 fnam = "dat/devi-";
00306 fnam += shortName();
00307 fnam += ".dat";
00308 }
00309
00310 ALIFileIn& ifdevi = ALIFileIn::getInstance( fnam );
00311
00312
00313 if(ALIUtils::debug >= 4) std::cout << "Opening deviation file: " << fnam << std::endl;
00314
00315
00316
00317
00318
00319 deviFromFile = new DeviationsFromFileSensor2D();
00320 fdevi_from_file = 1;
00321 if(ALIUtils::debug >= 5 ) std::cout << "deviFromFile " << deviFromFile << std::endl;
00322
00323 ALIstring sensor1_name, sensor2_name;
00324 ALIdouble sensor_dist;
00325 ALIdouble prec_deviX,prec_deviY;
00326
00327 std::vector<ALIstring> wl;
00328 ifdevi.getWordsInLine( wl );
00329 sensor1_name = wl[0];
00330 sensor2_name = wl[1];
00331 sensor_dist = atof( wl[2].c_str() );
00332
00333 if(sensor1_name[sensor1_name.size()-2] == 'c') {
00334 sensor1_name = sensor1_name.substr(0,sensor1_name.size()-1);
00335 }
00336 if(sensor2_name[sensor2_name.size()-2] == 'c') {
00337 sensor2_name = sensor2_name.substr(0,sensor2_name.size()-1);
00338 }
00339 if(ALIUtils::debug >= 5) std::cout << "sensor1_name " << sensor1_name << " sensor2_name " << sensor2_name << " sensor_dist " << sensor_dist << " unknown " << wl[3] << std::endl;
00340
00341 ifdevi.getWordsInLine( wl );
00342 prec_deviX = atof( wl[0].c_str() );
00343 prec_deviY = atof( wl[1].c_str() );
00344
00345 if(ALIUtils::debug >= 5) std::cout << "prec_deviX " << prec_deviX << " prec_deviY " << prec_deviY << std::endl;
00346
00347 deviFromFile = new DeviationsFromFileSensor2D();
00348 ALIdouble offsetX, offsetY;
00349 if( wl.size() == 5 ) {
00350 offsetX = ALIUtils::getFloat( wl[3] );
00351 offsetY = ALIUtils::getFloat( wl[4] );
00352 deviFromFile->setOffset( offsetX, offsetY );
00353 }
00354 deviFromFile->readFile( ifdevi );
00355 fdevi_from_file = 1;
00356 if(ALIUtils::debug >= 5 ) std::cout << "deviFromFile " << deviFromFile << std::endl;
00357
00358
00359
00360 std::vector<ALIstring> wlo;
00361 char chartmp[20];
00362 wlo.push_back( wordlist[0] );
00363 wlo.push_back("deviX");
00364 wlo.push_back("0");
00365 gcvt( prec_deviX, 10, chartmp );
00366 wlo.push_back( ALIstring(chartmp) );
00367 wlo.push_back("cal");
00368 std::vector<ALIstring> wl2(wlo);
00369 OpticalObject::fillExtraEntry( wlo );
00370
00371 wl2[1] = "deviY";
00372 gcvt( prec_deviY, 10, chartmp );
00373 wl2[3] = ALIstring( chartmp );
00374 OpticalObject::fillExtraEntry( wl2 );
00375
00376 } else {
00377 OpticalObject::fillExtraEntry( wordlist );
00378 }
00379 }
00380
00381
00382
00383
00384
00385 ALIdouble* OptOSensor2D::convertPointToLocalCoordinates( const CLHEP::Hep3Vector& point)
00386 {
00387 ALIdouble* interslc = new ALIdouble[2];
00388
00389
00390 CLHEP::HepRotation rmt = rmGlob();
00391 CLHEP::Hep3Vector XAxism(1.,0.,0.);
00392 XAxism*=rmt;
00393 if( ALIUtils::debug >= 5) ALIUtils::dump3v( (this)->centreGlob(), "centre glob sensor2D" );
00394 if( ALIUtils::debug >= 5) ALIUtils::dumprm( rmt, "rotation matrix sensor2D" );
00395
00396 if( ALIUtils::debug >= 5) ALIUtils::dump3v(XAxism , "XAxism");
00397 interslc[0] = (point - (this)->centreGlob() ) * XAxism;
00398
00399
00400 CLHEP::Hep3Vector YAxism(0.,1.,0.);
00401 YAxism*=rmt;
00402 if( ALIUtils::debug >= 5)
00403 ALIUtils::dump3v(YAxism , "YAxism");
00404 interslc[1] = (point - (this)->centreGlob() ) * YAxism;
00405
00406 if( ALIUtils::debug >= 5 ) {
00407 std::cout << " intersection in local coordinates: X= " << interslc[0] << " Y= " << interslc[1] << std::endl;
00408 ALIUtils::dump3v( point - (this)->centreGlob() , " inters - centre " );
00409 }
00410 return interslc;
00411 }
00412
00413 #ifdef COCOA_VIS
00414
00415 void OptOSensor2D::fillVRML()
00416 {
00417
00418 ALIVRMLMgr& vrmlmgr = ALIVRMLMgr::getInstance();
00419 ALIColour* col = new ALIColour( 0., 0., 1., 0. );
00420 vrmlmgr.AddBox( *this, 1., 1., .2, col);
00421 vrmlmgr.SendReferenceFrame( *this, 0.6);
00422 vrmlmgr.SendName( *this, 0.1 );
00423 }
00424
00425
00426
00427 void OptOSensor2D::fillIguana()
00428 {
00429 ALIColour* col = new ALIColour( 0., 0., 1., 0. );
00430 std::vector<ALIdouble> spar;
00431 spar.push_back(20.);
00432 spar.push_back(20.);
00433 spar.push_back(5.);
00434 IgCocoaFileMgr::getInstance().addSolid( *this, "BOX", spar, col);
00435 }
00436 #endif
00437
00438
00439
00440 void OptOSensor2D::constructSolidShape()
00441 {
00442 ALIdouble go;
00443 GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00444 gomgr->getGlobalOptionValue("VisScale", go );
00445
00446 theSolidShape = new CocoaSolidShapeBox( "Box", go*4.*cm/m, go*4.*cm/m, go*1.*cm/m );
00447 }