00001
00002
00003
00004
00005
00006
00007
00008 #include "Alignment/CocoaModel/interface/OptOCOPS.h"
00009 #include "Alignment/CocoaModel/interface/LightRay.h"
00010 #include "Alignment/CocoaModel/interface/Measurement.h"
00011 #ifdef COCOA_VIS
00012 #include "Alignment/CocoaVisMgr/interface/ALIVRMLMgr.h"
00013 #include "Alignment/IgCocoaFileWriter/interface/IgCocoaFileMgr.h"
00014 #endif
00015 #include "Alignment/CocoaModel/interface/ALIPlane.h"
00016 #include "Alignment/CocoaDDLObjects/interface/CocoaSolidShapeBox.h"
00017 #include "Alignment/CocoaUtilities/interface/GlobalOptionMgr.h"
00018
00019 #include <iostream>
00020 #include <iomanip>
00021 #include <fstream>
00022 #include <math.h>
00023
00024
00025
00026
00027
00028 void OptOCOPS::defaultBehaviour( LightRay& lightray, Measurement& meas )
00029 {
00030 if(ALIUtils::debug >= 5) std::cout << "***** OptOCOPS::defaultBehaviour" <<std::endl;
00031 makeMeasurement( lightray, meas);
00032 }
00033
00034
00035
00036
00037
00038 void OptOCOPS::makeMeasurement( LightRay& lightray, Measurement& meas )
00039 {
00040
00041 if (ALIUtils::debug >= 4) std::cout << "***** OptOCOPS::makeMeasurement(lightray, meas) " << std::endl;
00042
00043 CLHEP::Hep3Vector dowel2 = centreGlob();
00044
00045 ALIdouble posx12 = findExtraEntryValue("dowel1X");
00046
00047 if(posx12 == 0. ) posx12 = 0.045;
00048 CLHEP::Hep3Vector dowel1(posx12,findExtraEntryValue("dowel1Y"), 0.);
00049 CLHEP::HepRotation rmt = rmGlob();
00050 dowel1 = rmt*dowel1;
00051 dowel1 += dowel2;
00052 if (ALIUtils::debug >= 3) {
00053 ALIUtils::dump3v(dowel2, " dowel2");
00054 ALIUtils::dump3v(dowel1, " dowel1");
00055 }
00056
00057
00058 CLHEP::Hep3Vector line_dowel21 = - (dowel1-dowel2 );
00059 CLHEP::Hep3Vector ZAxis(0.,0,1.);
00060 ZAxis = rmt * ZAxis;
00061 CLHEP::Hep3Vector line_dowel21_perp = ZAxis.cross( line_dowel21 );
00062 if (ALIUtils::debug >= 3) {
00063 ALIUtils::dump3v(line_dowel21," line_dowel21");
00064 ALIUtils::dump3v(line_dowel21_perp," line_dowel21_perp");
00065 }
00066
00067
00068
00069 ALIdouble CCDlength = findExtraEntryValue("CCDlength");
00070 if( CCDlength == 0. ) CCDlength = 2048*14 * 1.E-6;
00071
00072
00073
00074
00075
00076
00077
00078 if(ALIUtils::debug>= 3) std::cout << std::endl << "***** UP CCD *****" << std::endl
00079 << "******************" << std::endl << std::endl;
00080 ALIdouble posX = findExtraEntryValue("upCCDXtoDowel2");
00081 ALIdouble posY;
00082 ALIbool eexists = findExtraEntryValueIfExists("upCCDYtoDowel2", posY);
00083 if(!eexists) posY = CCDlength + 0.004;
00084 CLHEP::Hep3Vector posxy( posX, posY, 0);
00085 if(ALIUtils::debug>= 3) std::cout << " %%%% CCD distances to Dowel2: " << std::endl;
00086 if(ALIUtils::debug>= 3) std::cout << " up ccd in local RF " << posxy << std::endl;
00087 posxy = rmt * posxy;
00088 if(ALIUtils::debug>= 3) std::cout << " up ccd in global RF " << posxy << std::endl;
00089 ALILine upCCD( dowel2 + posxy, -line_dowel21 );
00090 ccds[0] = ALILine( posxy, -line_dowel21 );
00091
00092
00093 if(ALIUtils::debug>= 3) std::cout << std::endl << "***** DOWN CCD *****" << std::endl
00094 << "********************" << std::endl << std::endl ;
00095 posX = findExtraEntryValue("downCCDXtoDowel2");
00096 eexists = findExtraEntryValueIfExists("downCCDYtoDowel2", posY);
00097 if(!eexists) posY = 0.002;
00098 posxy = CLHEP::Hep3Vector( posX, posY, 0);
00099 if(ALIUtils::debug>= 3) std::cout << " down ccd in local RF " << posxy << std::endl;
00100 posxy = rmt * posxy;
00101 if(ALIUtils::debug>= 3) std::cout << " down ccd in global RF " << posxy << std::endl;
00102 ALILine downCCD( dowel2 + posxy, -line_dowel21 );
00103 ccds[1] = ALILine( posxy, -line_dowel21 );
00104
00105
00106
00107 if(ALIUtils::debug>= 3) std::cout << std::endl << "***** LEFT CCD *****" << std::endl
00108 << "********************" << std::endl << std::endl;
00109 eexists = findExtraEntryValueIfExists("leftCCDXtoDowel2", posX);
00110 if(!eexists) posX = -0.002;
00111 posY = findExtraEntryValue("leftCCDYtoDowel2");
00112 posxy = CLHEP::Hep3Vector( posX, posY, 0);
00113 if(ALIUtils::debug>= 3) std::cout << " left ccd in local RF " << posxy << std::endl;
00114 posxy = rmt * posxy;
00115 if(ALIUtils::debug>= 3) std::cout << " left ccd in global RF " << posxy << std::endl;
00116 ALILine leftCCD( dowel2 + posxy, line_dowel21_perp );
00117 ccds[2] = ALILine( posxy, line_dowel21_perp );
00118
00119
00120 if(ALIUtils::debug>= 3) std::cout << std::endl << "***** RIGHT CCD *****" << std::endl
00121 << "*********************" << std::endl<< std::endl ;
00122 eexists = findExtraEntryValueIfExists("rightCCDXtoDowel2", posX);
00123 if(!eexists) posX = -CCDlength - 0.004;
00124 posY = findExtraEntryValue("rightCCDYtoDowel2");
00125 posxy = CLHEP::Hep3Vector( posX, posY, 0);
00126 if(ALIUtils::debug>= 3) std::cout << " right ccd in local RF " << posxy << std::endl;
00127 posxy = rmt * posxy;
00128 if(ALIUtils::debug>= 3) std::cout << " right ccd in global RF " << posxy << std::endl << std::endl;
00129 ALILine rightCCD( dowel2 + posxy, line_dowel21_perp );
00130 ccds[3] = ALILine( posxy, line_dowel21_perp );
00131
00132 if (ALIUtils::debug >= 3) {
00133 std::cout << " %%% Positions of CCDs in global RF: " << std::endl<< std::endl;
00134 std::cout << " upCCD: " << upCCD << std::endl;
00135 std::cout << " downCCD: " << downCCD << std::endl;
00136 std::cout << " leftCCD: " << leftCCD << std::endl;
00137 std::cout << " rightCCD: " << rightCCD << std::endl << std::endl;
00138 }
00139
00140
00141 if (ALIUtils::debug >= 3) std::cout << " %%% Intersecting x-hair laser with COPS: " << std::endl;
00142 ALIPlane copsPlane(centreGlob(), ZAxis);
00143 lightray.intersect( *this );
00144 CLHEP::Hep3Vector inters = lightray.point();
00145 if (ALIUtils::debug >= 3) {
00146 ALIUtils::dump3v(inters, " Intersection of x-hair laser with COPS ");
00147 }
00148
00149
00150 if (ALIUtils::debug >= 5) std::cout << "1. Get the OptO x-hair laser from the measurement list of OptOs" << std::endl;
00151
00152 OpticalObject* xhairOptO = *(meas.OptOList().begin());
00153
00154 if (ALIUtils::debug >= 35) std::cout << "2. Get the Y of the laser and project it on the COPS" << std::endl;
00155 CLHEP::Hep3Vector YAxis_xhair(0.,1.,0.);
00156 CLHEP::HepRotation rmtx = xhairOptO->rmGlob();
00157 YAxis_xhair = rmtx * YAxis_xhair;
00158 ALILine Yline_xhair( inters, copsPlane.project( YAxis_xhair ));
00159 if (ALIUtils::debug >= 3) {
00160 std::cout << " %%%% Projecting x-hair laser on COPS: " << std::endl;
00161 ALIUtils::dump3v(YAxis_xhair, " Y direction of laser ");
00162 std::cout << " Y line of laser projected on COPS " << Yline_xhair << std::endl;
00163 }
00164
00165 if (ALIUtils::debug >= 5) std::cout << " 3. Get the X of the laser (correct it if cross is not 90o) and project it on the COPS" << std::endl;
00166
00167 ALIdouble anglebx;
00168 eexists = xhairOptO->findExtraEntryValueIfExists("angleBetweenAxis", anglebx);
00169 if(!eexists) anglebx = PI/2.;
00170 CLHEP::Hep3Vector XAxis_xhair = YAxis_xhair;
00171
00172
00173 ZAxis = CLHEP::Hep3Vector(0.,0.,1.);
00174 ZAxis = rmtx * ZAxis;
00175 XAxis_xhair.rotate(anglebx, ZAxis );
00176 ALILine Xline_xhair( inters, copsPlane.project( XAxis_xhair ) );
00177 if (ALIUtils::debug >= 3) {
00178 std::cout << "angleBetweenAxis = " << anglebx << std::endl;
00179 ALIUtils::dump3v(XAxis_xhair," X direction of laser ");
00180 std::cout << " X line of laser projected on COPS " << Xline_xhair << std::endl;
00181 }
00182
00183
00184
00185 if(ALIUtils::debug >= 3) std::cout << " Getting measurements as intersection with four CCDs: " << std::endl;
00186
00187 if(ALIUtils::debug >= 4)std::cout << "intersecting with upCCD " << std::endl;
00188 ALIdouble measv[4][2];
00189
00190
00191
00192 if(ALIUtils::debug >= 5)std::cout << "$@S@ measv[0][0] upccd " << std::endl;
00193 measv[0][0] = getMeasFromInters( Yline_xhair, upCCD, line_dowel21 );
00194 if(ALIUtils::debug >= 5)std::cout << "$@$@ measv[0][1] upccd " << std::endl;
00195 measv[0][1] = getMeasFromInters( Xline_xhair, upCCD, line_dowel21 );
00196
00197
00198 if(ALIUtils::debug >= 4) std::cout << "intersecting with downCCD " << std::endl;
00199 measv[1][0] = getMeasFromInters(Yline_xhair, downCCD, line_dowel21 );
00200 measv[1][1] = getMeasFromInters(Xline_xhair, downCCD, line_dowel21 );
00201
00202
00203
00204 if(ALIUtils::debug >= 4) std::cout << "intersecting with leftCCD " << std::endl;
00205 measv[2][0] = getMeasFromInters(Xline_xhair, leftCCD, line_dowel21_perp );
00206 measv[2][1] = getMeasFromInters(Yline_xhair, leftCCD, line_dowel21_perp );
00207 if(ALIUtils::debug >= 4) std::cout << "intersecting with rightCCD " << std::endl;
00208 measv[3][0] = getMeasFromInters(Xline_xhair, rightCCD, line_dowel21_perp );
00209 measv[3][1] = getMeasFromInters(Yline_xhair, rightCCD, line_dowel21_perp );
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 ALIstring measNames[4] ={"up","down","left","right"};
00243 ALIbool laserLine;
00244 if (ALIUtils::debug >= 2) std::cout << std::endl << "--> Now comparing measurement in ccds by x and y laser lines (will always choose the smaller one) " <<std::endl;
00245
00246 uint ii;
00247 for( ii = 0; ii < 4; ii++ ) {
00248 if (ALIUtils::debug >= 2) std::cout << "\tmeas CCD " << measNames[ii] << " ii=(" << ii << ") \t Values: "
00249
00250 << " " << fabs( measv[ii][0] ) << " " << fabs( measv[ii][1] ) << " isnan() = " <<
00251 isnan(measv[ii][1]) << std::endl;
00252
00253 if( meas.xlaserLine( ii ) != -1 ) {
00254 laserLine = ALIbool( meas.xlaserLine( ii ) );
00255 } else {
00256
00257
00258
00259
00260
00261
00262 if(isnan(measv[ii][1]) != 0){
00263 measv[ii][1] = 1e99;
00264 if (ALIUtils::debug >= 2) std::cout << " --> Swapping for " << measv[ii][1] << "(inf)" << std::endl;
00265 }
00266
00267 laserLine = fabs( measv[ii][0] ) < fabs( measv[ii][1] );
00268
00269 meas.setXlaserLine( ii, int(laserLine) );
00270 }
00271 laserLine ? meas.setValueSimulated( ii, measv[ii][0] )
00272 : meas.setValueSimulated( ii, measv[ii][1] );
00273 }
00274
00275 if (ALIUtils::debug >= 2) std::cout << std::endl;
00276
00277
00278
00279
00280 if(ALIUtils::debug >= 2) std::cout << "***** OptOCOPS::makeMeasurement - identify pathological cases U and D intersected by same line" <<std::endl;
00281 ALIbool xlaserDir[4];
00282 for( ii = 0; ii < 4; ii++ ) {
00283
00284 xlaserDir[ii] = ALIbool( meas.xlaserLine( ii ) );
00285 }
00286 if( xlaserDir[0] ^ xlaserDir[1] ) {
00287 std::cerr << "!!EXITING up and down CCDs intersected by different x-laser line " << xlaserDir[0] << " " << xlaserDir[1] << std::endl;
00288 exit(1);
00289 }
00290 if( xlaserDir[2] ^ xlaserDir[3] ) {
00291 std::cerr << "!!EXITING right and left CCDs intersected by different x-laser line " << xlaserDir[0] << " " << xlaserDir[1] << std::endl;
00292 exit(1);
00293 }
00294
00295 if(ALIUtils::debug >= 5) std::cout << "***** OptOCOPS::makeMeasurement - now output sim values" << std::endl;
00296
00297 if (ALIUtils::debug >= 1) {
00298 ALIstring chrg = "";
00299 std::cout << "REAL value: " << chrg <<"U: " << 1000*meas.value()[0] << chrg
00300 << " D: " << 1000*meas.value()[1]
00301 << " L: " << 1000*meas.value()[2]
00302 << " R: " << 1000*meas.value()[3]
00303 << " (mm) " << (this)->name() << std::endl;
00304 ALIdouble detU = 1000*meas.valueSimulated(0); if(fabs(detU) <= 1.e-9 ) detU = 0.;
00305 ALIdouble detD = 1000*meas.valueSimulated(1); if(fabs(detD) <= 1.e-9 ) detD = 0.;
00306 ALIdouble detL = 1000*meas.valueSimulated(2); if(fabs(detL) <= 1.e-9 ) detL = 0.;
00307 ALIdouble detR = 1000*meas.valueSimulated(3); if(fabs(detR) <= 1.e-9 ) detR = 0.;
00308 std::cout << "SIMU value: " << chrg << "U: "
00309
00310 << detU
00311 << chrg << " D: " << detD
00312 << chrg << " L: " << detL
00313 << chrg << " R: " << detR
00314 << " (mm) " << (this)->name() << std::endl;
00315 }
00316
00317
00318 }
00319
00320
00321
00322
00323
00324 void OptOCOPS::fastTraversesLightRay( LightRay& lightray )
00325 {
00326
00327 if(ALIUtils::debug >= 5) std::cout << "***** OptOCOPS::fastTraversesLightRay" <<std::endl;
00328 if (ALIUtils::debug >= 2) std::cout << "LR: FAST TRAVERSES COPS " << name() << std::endl;
00329
00330
00331 CLHEP::Hep3Vector ZAxis(0.,0,1.);
00332 CLHEP::HepRotation rmt = rmGlob();
00333 ZAxis = rmt * ZAxis;
00334 lightray.intersect( ALIPlane(centreGlob(), ZAxis) );
00335 CLHEP::Hep3Vector inters = lightray.point();
00336 lightray.setPoint( inters );
00337
00338 if (ALIUtils::debug >= 2) {
00339 lightray.dumpData(" after COPS ");
00340 }
00341
00342 }
00343
00344
00345
00346 ALIdouble* OptOCOPS::convertPointToLocalCoordinates( const CLHEP::Hep3Vector& point)
00347 {
00348 if(ALIUtils::debug >= 1) std::cout << "***** OptOCOPS::convertPointToLocalCoordinates" <<std::endl;
00349 ALIdouble interslc[2];
00350
00351
00352 CLHEP::HepRotation rmt = rmGlob();
00353 CLHEP::Hep3Vector XAxism(1.,0.,0.);
00354 XAxism*=rmt;
00355 if( ALIUtils::debug >= 5) ALIUtils::dump3v( (this)->centreGlob(), "centre glob sensor2D" );
00356 if( ALIUtils::debug >= 5) ALIUtils::dumprm( rmt, "rotation matrix sensor2D" );
00357
00358
00359 interslc[0] = (point - (this)->centreGlob() ) * XAxism;
00360
00361
00362 CLHEP::Hep3Vector YAxism(0.,1.,0.);
00363 YAxism*=rmt;
00364
00365 interslc[1] = (point - (this)->centreGlob() ) * YAxism;
00366
00367 if( ALIUtils::debug >=5 ) {
00368 std::cout << " intersection in local coordinates: X= " << interslc[0] << " Y= " << interslc[1] << std::endl;
00369 }
00370 return interslc;
00371 }
00372
00373
00374
00375 ALIdouble OptOCOPS::getMeasFromInters( ALILine& line_xhair, ALILine& ccd, CLHEP::Hep3Vector& cops_line )
00376 {
00377
00378 if(ALIUtils::debug >= 5) std::cout << "***** OptOCOPS::getMeasFromInters" <<std::endl;
00379 CLHEP::Hep3Vector inters = line_xhair.intersect( ccd, 0 ) - ccd.pt();
00380 ALIdouble sign = inters*ccd.vec();
00381 if( sign != 0 ){
00382 sign = fabs(sign)/sign;
00383
00384
00385
00386 }
00387 return sign*inters.mag();
00388 }
00389
00390
00391 #ifdef COCOA_VIS
00392
00393 void OptOCOPS::fillVRML()
00394 {
00395
00396 ALIVRMLMgr& vrmlmgr = ALIVRMLMgr::getInstance();
00397
00398
00399
00400 ALIdouble CCDlength = findExtraEntryValue("CCDlength");
00401 if( CCDlength == 0. ) CCDlength = 2048*14 * 1.E-6;
00402
00403
00404 ALIdouble CCDdim = 2048*14 * 1.E-6;
00405
00406
00407 ALIdouble CCDwidth = .005;
00408
00409 if(ALIUtils::debug >= 4) std::cout << " ccds0 " << ccds[0] << "ccds1 " << ccds[1] << std::endl;
00410 ALIColour colup( 1.,0.,0., 0.);
00411 ALIColour coldown( 0.,1.,0., 0.);
00412 ALIColour colleft( 0.,0.,1., 0.);
00413 ALIColour colright( 0.,1.,1., 0.);
00414
00415
00416 ALIdouble ccdsize = 1.;
00417
00418
00419
00420
00421
00422
00423 vrmlmgr.AddBoxDisplaced( *this, CCDdim, CCDwidth, CCDwidth, ccdsize*(ccds[0].pt()+0.5*CCDlength*ccds[0].vec()), &colup);
00424
00425 vrmlmgr.AddBoxDisplaced( *this, CCDdim, CCDwidth, CCDwidth, ccdsize*(ccds[1].pt()+0.5*CCDlength*ccds[1].vec()), &coldown );
00426
00427 vrmlmgr.AddBoxDisplaced( *this, CCDwidth, CCDdim, CCDwidth, ccdsize*(ccds[2].pt()+0.5*CCDlength*ccds[2].vec()), &colleft );
00428
00429 vrmlmgr.AddBoxDisplaced( *this, CCDwidth, CCDdim, CCDwidth, ccdsize*(ccds[3].pt()+0.5*CCDlength*ccds[3].vec()), &colright );
00430
00431 vrmlmgr.SendReferenceFrame( *this, CCDdim);
00432 vrmlmgr.SendName( *this, 0.01 );
00433
00434 }
00435
00436
00437
00438 void OptOCOPS::fillIguana()
00439 {
00440
00441
00442 ALIdouble CCDlength;
00443 ALIbool pexists = findExtraEntryValueIfExists("CCDlength",CCDlength);
00444 if( !pexists ) CCDlength = 2048*14 * 1.E-6;
00445 ALIdouble CCDdim = 2048*14 * 1.E-6;
00446 ALIdouble CCDwidth = .005;
00447
00448 if(ALIUtils::debug >= 4) std::cout << " ccds0 " << ccds[0] << "ccds1 " << ccds[1] << std::endl;
00449 ALIColour colup( 0.2,1.,0., 0.);
00450 ALIColour coldown( 0.3,1.,0., 0.);
00451 ALIColour colleft( 0.4,1.,0., 0.);
00452 ALIColour colright( 0.5,1.,0., 0.);
00453
00454
00455 ALIdouble ccdsize = 1;
00456
00457
00458
00459
00460
00461 std::vector<ALIdouble> spar;
00462 spar.push_back(CCDdim);
00463 spar.push_back(CCDwidth);
00464 spar.push_back(CCDwidth);
00465
00466 IgCocoaFileMgr::getInstance().addSolid( *this, "BOX", spar, &colup, ccdsize*(ccds[0].pt()+0.5*CCDlength*ccds[0].vec()));
00467
00468 IgCocoaFileMgr::getInstance().addSolid( *this, "BOX", spar, &coldown, ccdsize*(ccds[1].pt()+0.5*CCDlength*ccds[1].vec()) );
00469
00470 std::vector<ALIdouble> spar2;
00471 spar2.push_back(CCDwidth);
00472 spar2.push_back(CCDdim);
00473 spar2.push_back(CCDwidth);
00474 IgCocoaFileMgr::getInstance().addSolid( *this, "BOX", spar2, &colleft, ccdsize*(ccds[2].pt()+0.5*CCDlength*ccds[2].vec()));
00475
00476 IgCocoaFileMgr::getInstance().addSolid( *this, "BOX", spar2, &colright, ccdsize*(ccds[3].pt()+0.5*CCDlength*ccds[3].vec()));
00477
00478 }
00479 #endif
00480
00481
00482
00483 void OptOCOPS::constructSolidShape()
00484 {
00485 ALIdouble go;
00486 GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00487 gomgr->getGlobalOptionValue("VisScale", go );
00488
00489 theSolidShape = new CocoaSolidShapeBox( "Box", go*5.*cm/m, go*5.*cm/m, go*1.*cm/m );
00490 }