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