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