File indexing completed on 2023-03-17 10:38:43
0001
0002
0003
0004
0005
0006
0007
0008 #include "Alignment/CocoaModel/interface/OptOCOPS.h"
0009 #include "Alignment/CocoaModel/interface/LightRay.h"
0010 #include "Alignment/CocoaModel/interface/Measurement.h"
0011 #include "Alignment/CocoaModel/interface/Model.h"
0012 #ifdef COCOA_VIS
0013 #include "Alignment/CocoaVisMgr/interface/ALIVRMLMgr.h"
0014 #include "Alignment/IgCocoaFileWriter/interface/IgCocoaFileMgr.h"
0015 #endif
0016 #include "Alignment/CocoaModel/interface/ALILine.h"
0017 #include "Alignment/CocoaModel/interface/ALIPlane.h"
0018 #include "Alignment/CocoaDDLObjects/interface/CocoaSolidShapeBox.h"
0019 #include "Alignment/CocoaUtilities/interface/GlobalOptionMgr.h"
0020 #include "FWCore/Utilities/interface/isFinite.h"
0021
0022 #include <iostream>
0023 #include <iomanip>
0024 #include <fstream>
0025 #include <cmath> // among others include also floating-point std::abs functions
0026 #include <cstdlib>
0027
0028 using namespace CLHEP;
0029
0030
0031
0032
0033 void OptOCOPS::defaultBehaviour(LightRay& lightray, Measurement& meas) {
0034 if (ALIUtils::debug >= 5)
0035 std::cout << "***** OptOCOPS::defaultBehaviour" << std::endl;
0036 makeMeasurement(lightray, meas);
0037 }
0038
0039
0040
0041
0042 void OptOCOPS::makeMeasurement(LightRay& lightray, Measurement& meas) {
0043 if (ALIUtils::debug >= 4)
0044 std::cout << "***** OptOCOPS::makeMeasurement(lightray, meas) " << std::endl;
0045
0046 CLHEP::Hep3Vector dowel2 = centreGlob();
0047
0048 ALIdouble posx12 = findExtraEntryValue("dowel1X");
0049
0050 if (posx12 == 0.)
0051 posx12 = -0.045;
0052 CLHEP::Hep3Vector dowel1(posx12, findExtraEntryValue("dowel1Y"), 0.);
0053 CLHEP::HepRotation rmt = rmGlob();
0054 dowel1 = rmt * dowel1;
0055 dowel1 += dowel2;
0056 if (ALIUtils::debug >= 3) {
0057 ALIUtils::dump3v(dowel2, " dowel2");
0058 ALIUtils::dump3v(dowel1, " dowel1");
0059 }
0060
0061
0062
0063 CLHEP::Hep3Vector line_dowel21 = (dowel1 - dowel2);
0064 CLHEP::Hep3Vector ZAxis(0., 0, 1.);
0065 ZAxis = rmt * ZAxis;
0066 CLHEP::Hep3Vector line_dowel21_perp = ZAxis.cross(line_dowel21);
0067 if (ALIUtils::debug >= 3) {
0068 ALIUtils::dump3v(line_dowel21, " line_dowel21");
0069 ALIUtils::dump3v(line_dowel21_perp, " line_dowel21_perp");
0070 }
0071
0072
0073
0074 ALIdouble CCDlength = findExtraEntryValue("CCDlength");
0075 if (CCDlength == 0.)
0076 CCDlength = 2048 * 14 * 1.E-6;
0077
0078
0079
0080
0081 if (ALIUtils::debug >= 3)
0082 std::cout << std::endl << "***** UP CCD *****" << std::endl << "******************" << std::endl << std::endl;
0083 ALIdouble posX = findExtraEntryValue("upCCDXtoDowel2");
0084 ALIdouble posY;
0085 ALIbool eexists = findExtraEntryValueIfExists("upCCDYtoDowel2", posY);
0086 if (!eexists)
0087 posY = CCDlength + 0.004;
0088
0089 CLHEP::Hep3Vector posxy(posX, posY, 0);
0090 if (ALIUtils::debug >= 3)
0091 std::cout << " %%%% CCD distances to Dowel2: " << std::endl;
0092 if (ALIUtils::debug >= 3)
0093 std::cout << " up ccd in local RF " << posxy << std::endl;
0094 posxy = rmt * posxy;
0095 if (ALIUtils::debug >= 3)
0096 std::cout << " up ccd in global RF " << posxy << std::endl;
0097
0098
0099 ALILine upCCD(dowel2 + posxy, line_dowel21);
0100 ccds[0] = ALILine(posxy, line_dowel21);
0101
0102 if (ALIUtils::debug >= 3)
0103 std::cout << std::endl << "***** DOWN CCD *****" << std::endl << "********************" << std::endl << std::endl;
0104 posX = findExtraEntryValue("downCCDXtoDowel2");
0105 eexists = findExtraEntryValueIfExists("downCCDYtoDowel2", posY);
0106 if (!eexists)
0107 posY = 0.002;
0108 posxy = CLHEP::Hep3Vector(posX, posY, 0);
0109 if (ALIUtils::debug >= 3)
0110 std::cout << " down ccd in local RF " << posxy << std::endl;
0111 posxy = rmt * posxy;
0112 if (ALIUtils::debug >= 3)
0113 std::cout << " down ccd in global RF " << posxy << std::endl;
0114
0115
0116
0117 ALILine downCCD(dowel2 + posxy, line_dowel21);
0118 ccds[1] = ALILine(posxy, line_dowel21);
0119
0120
0121
0122 if (ALIUtils::debug >= 3)
0123 std::cout << std::endl << "***** LEFT CCD *****" << std::endl << "********************" << std::endl << std::endl;
0124 eexists = findExtraEntryValueIfExists("leftCCDXtoDowel2", posX);
0125
0126 if (!eexists)
0127 posX = -CCDlength - 0.002;
0128 posY = findExtraEntryValue("leftCCDYtoDowel2");
0129 posxy = CLHEP::Hep3Vector(posX, posY, 0);
0130 if (ALIUtils::debug >= 3)
0131 std::cout << " left ccd in local RF " << posxy << std::endl;
0132 posxy = rmt * posxy;
0133 if (ALIUtils::debug >= 3)
0134 std::cout << " left ccd in global RF " << posxy << std::endl;
0135
0136
0137
0138 ALILine leftCCD(dowel2 + posxy, -line_dowel21_perp);
0139 ccds[2] = ALILine(posxy, -line_dowel21_perp);
0140
0141
0142 if (ALIUtils::debug >= 3)
0143 std::cout << std::endl << "***** RIGHT CCD *****" << std::endl << "*********************" << std::endl << std::endl;
0144 eexists = findExtraEntryValueIfExists("rightCCDXtoDowel2", posX);
0145
0146 if (!eexists)
0147 posX = -0.004;
0148 posY = findExtraEntryValue("rightCCDYtoDowel2");
0149 posxy = CLHEP::Hep3Vector(posX, posY, 0);
0150 if (ALIUtils::debug >= 3)
0151 std::cout << " right ccd in local RF " << posxy << std::endl;
0152 posxy = rmt * posxy;
0153 if (ALIUtils::debug >= 3)
0154 std::cout << " right ccd in global RF " << posxy << std::endl << std::endl;
0155
0156
0157
0158 ALILine rightCCD(dowel2 + posxy, -line_dowel21_perp);
0159 ccds[3] = ALILine(posxy, -line_dowel21_perp);
0160
0161 if (ALIUtils::debug >= 3) {
0162 std::cout << " %%% Positions of CCDs in global RF: " << std::endl << std::endl;
0163 std::cout << " upCCD: " << upCCD << std::endl;
0164 std::cout << " downCCD: " << downCCD << std::endl;
0165 std::cout << " leftCCD: " << leftCCD << std::endl;
0166 std::cout << " rightCCD: " << rightCCD << std::endl << std::endl;
0167 }
0168
0169
0170 if (ALIUtils::debug >= 3)
0171 std::cout << " %%% Intersecting x-hair laser with COPS: " << std::endl;
0172 ALIPlane copsPlane(centreGlob(), ZAxis);
0173 lightray.intersect(*this);
0174 CLHEP::Hep3Vector inters = lightray.point();
0175 if (ALIUtils::debug >= 3) {
0176 ALIUtils::dump3v(inters, " Intersection of x-hair laser with COPS ");
0177 }
0178
0179
0180 if (ALIUtils::debug >= 5)
0181 std::cout << "1. Get the OptO x-hair laser from the measurement list of OptOs" << std::endl;
0182
0183 OpticalObject* xhairOptO = *(meas.OptOList().begin());
0184
0185 if (ALIUtils::debug >= 35)
0186 std::cout << "2. Get the Y of the laser and project it on the COPS" << std::endl;
0187 CLHEP::Hep3Vector YAxis_xhair(0., 1., 0.);
0188 const CLHEP::HepRotation& rmtx = xhairOptO->rmGlob();
0189 YAxis_xhair = rmtx * YAxis_xhair;
0190 ALILine Yline_xhair(inters, copsPlane.project(YAxis_xhair));
0191 if (ALIUtils::debug >= 3) {
0192 std::cout << " %%%% Projecting x-hair laser on COPS: " << std::endl;
0193 ALIUtils::dump3v(YAxis_xhair, " Y direction of laser ");
0194 std::cout << " Y line of laser projected on COPS " << Yline_xhair << std::endl;
0195 }
0196
0197 if (ALIUtils::debug >= 5)
0198 std::cout << " 3. Get the X of the laser (correct it if cross is not 90o) and project it on the COPS" << std::endl;
0199
0200 ALIdouble anglebx;
0201 eexists = xhairOptO->findExtraEntryValueIfExists("angleBetweenAxis", anglebx);
0202 if (!eexists)
0203 anglebx = PI / 2.;
0204 CLHEP::Hep3Vector XAxis_xhair = YAxis_xhair;
0205
0206
0207 ZAxis = CLHEP::Hep3Vector(0., 0., 1.);
0208 ZAxis = rmtx * ZAxis;
0209 XAxis_xhair.rotate(anglebx, ZAxis);
0210 ALILine Xline_xhair(inters, copsPlane.project(XAxis_xhair));
0211 if (ALIUtils::debug >= 3) {
0212 std::cout << "angleBetweenAxis = " << anglebx << std::endl;
0213 ALIUtils::dump3v(XAxis_xhair, " X direction of laser ");
0214 std::cout << " X line of laser projected on COPS " << Xline_xhair << std::endl;
0215 }
0216
0217
0218 if (ALIUtils::debug >= 3)
0219 std::cout << " Getting measurements as intersection with four CCDs: " << std::endl;
0220
0221 if (ALIUtils::debug >= 4)
0222 std::cout << "intersecting with upCCD " << std::endl;
0223 ALIdouble measv[4][2];
0224
0225
0226
0227 if (ALIUtils::debug >= 5)
0228 std::cout << "$@S@ measv[0][0] upccd " << std::endl;
0229 measv[0][0] = getMeasFromInters(Yline_xhair, upCCD, line_dowel21);
0230 if (ALIUtils::debug >= 5)
0231 std::cout << "$@$@ measv[0][1] upccd " << std::endl;
0232 measv[0][1] = getMeasFromInters(Xline_xhair, upCCD, line_dowel21);
0233
0234
0235 if (ALIUtils::debug >= 4)
0236 std::cout << "intersecting with downCCD " << std::endl;
0237 measv[1][0] = getMeasFromInters(Yline_xhair, downCCD, line_dowel21);
0238 measv[1][1] = getMeasFromInters(Xline_xhair, downCCD, line_dowel21);
0239
0240
0241
0242 if (ALIUtils::debug >= 4)
0243 std::cout << "intersecting with leftCCD " << std::endl;
0244 measv[2][0] = getMeasFromInters(Xline_xhair, leftCCD, line_dowel21_perp);
0245 measv[2][1] = getMeasFromInters(Yline_xhair, leftCCD, line_dowel21_perp);
0246 if (ALIUtils::debug >= 4)
0247 std::cout << "intersecting with rightCCD " << std::endl;
0248 measv[3][0] = getMeasFromInters(Xline_xhair, rightCCD, line_dowel21_perp);
0249 measv[3][1] = getMeasFromInters(Yline_xhair, rightCCD, line_dowel21_perp);
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282 ALIstring measNames[4] = {"up", "down", "left", "right"};
0283 ALIbool laserLine;
0284 if (ALIUtils::debug >= 2)
0285 std::cout << std::endl
0286 << "--> Now comparing measurement in ccds by x and y laser lines (will always choose the smaller one) "
0287 << std::endl;
0288
0289 unsigned int ii;
0290 for (ii = 0; ii < 4; ii++) {
0291 if (ALIUtils::debug >= 2)
0292 std::cout << "\tmeas CCD " << measNames[ii] << " ii=(" << ii
0293 << ") \t Values: "
0294
0295 << " " << std::abs(measv[ii][0]) << " " << std::abs(measv[ii][1])
0296 << " edm::isNotFinite() = " << edm::isNotFinite(measv[ii][1]) << std::endl;
0297
0298 if (meas.xlaserLine(ii) != -1) {
0299 laserLine = ALIbool(meas.xlaserLine(ii));
0300 } else {
0301
0302
0303
0304
0305
0306 if (edm::isNotFinite(measv[ii][1]) != 0) {
0307 measv[ii][1] = 1e99;
0308 if (ALIUtils::debug >= 2)
0309 std::cout << " --> Swapping for " << measv[ii][1] << "(inf)" << std::endl;
0310 }
0311
0312 laserLine = std::abs(measv[ii][0]) < std::abs(measv[ii][1]);
0313
0314 meas.setXlaserLine(ii, int(laserLine));
0315 }
0316 laserLine ? meas.setValueSimulated(ii, measv[ii][0]) : meas.setValueSimulated(ii, measv[ii][1]);
0317 }
0318
0319 if (ALIUtils::debug >= 2)
0320 std::cout << std::endl;
0321
0322
0323
0324
0325 if (ALIUtils::debug >= 2)
0326 std::cout << "***** OptOCOPS::makeMeasurement - identify pathological cases U and D intersected by same line"
0327 << std::endl;
0328 ALIbool xlaserDir[4];
0329 for (ii = 0; ii < 4; ii++) {
0330
0331 xlaserDir[ii] = ALIbool(meas.xlaserLine(ii));
0332 }
0333 if (xlaserDir[0] ^ xlaserDir[1]) {
0334 std::cerr << "!!EXITING up and down CCDs intersected by different x-laser line " << xlaserDir[0] << " "
0335 << xlaserDir[1] << std::endl;
0336 exit(1);
0337 }
0338 if (xlaserDir[2] ^ xlaserDir[3]) {
0339 std::cerr << "!!EXITING right and left CCDs intersected by different x-laser line " << xlaserDir[0] << " "
0340 << xlaserDir[1] << std::endl;
0341 exit(1);
0342 }
0343
0344 if (ALIUtils::debug >= 5)
0345 std::cout << "***** OptOCOPS::makeMeasurement - now output sim values" << std::endl;
0346
0347 if (ALIUtils::debug >= 1) {
0348 ALIstring chrg = "";
0349 std::cout << "REAL value: " << chrg << "U: " << 1000 * meas.value()[0] << chrg << " D: " << 1000 * meas.value()[1]
0350 << " L: " << 1000 * meas.value()[2] << " R: " << 1000 * meas.value()[3] << " (mm) " << (this)->name()
0351 << std::endl;
0352 ALIdouble detU = 1000 * meas.valueSimulated(0);
0353 if (std::abs(detU) <= 1.e-9)
0354 detU = 0.;
0355 ALIdouble detD = 1000 * meas.valueSimulated(1);
0356 if (std::abs(detD) <= 1.e-9)
0357 detD = 0.;
0358 ALIdouble detL = 1000 * meas.valueSimulated(2);
0359 if (std::abs(detL) <= 1.e-9)
0360 detL = 0.;
0361 ALIdouble detR = 1000 * meas.valueSimulated(3);
0362 if (std::abs(detR) <= 1.e-9)
0363 detR = 0.;
0364 std::cout << "SIMU value: " << chrg
0365 << "U: "
0366
0367 << detU << chrg << " D: " << detD << chrg << " L: " << detL << chrg << " R: " << detR << " (mm) "
0368 << (this)->name() << std::endl;
0369 }
0370 }
0371
0372
0373
0374
0375 void OptOCOPS::fastTraversesLightRay(LightRay& lightray) {
0376 if (ALIUtils::debug >= 5)
0377 std::cout << "***** OptOCOPS::fastTraversesLightRay" << std::endl;
0378 if (ALIUtils::debug >= 2)
0379 std::cout << "LR: FAST TRAVERSES COPS " << name() << std::endl;
0380
0381
0382 CLHEP::Hep3Vector ZAxis(0., 0, 1.);
0383 CLHEP::HepRotation rmt = rmGlob();
0384 ZAxis = rmt * ZAxis;
0385 lightray.intersect(ALIPlane(centreGlob(), ZAxis));
0386 CLHEP::Hep3Vector inters = lightray.point();
0387 lightray.setPoint(inters);
0388
0389 if (ALIUtils::debug >= 2) {
0390 lightray.dumpData(" after COPS ");
0391 }
0392 }
0393
0394
0395 ALIdouble* OptOCOPS::convertPointToLocalCoordinates(const CLHEP::Hep3Vector& point) {
0396 if (ALIUtils::debug >= 1)
0397 std::cout << "***** OptOCOPS::convertPointToLocalCoordinates" << std::endl;
0398 ALIdouble* interslc = new ALIdouble[2];
0399
0400
0401 CLHEP::HepRotation rmt = rmGlob();
0402 CLHEP::Hep3Vector XAxism(1., 0., 0.);
0403 XAxism *= rmt;
0404 if (ALIUtils::debug >= 5)
0405 ALIUtils::dump3v((this)->centreGlob(), "centre glob sensor2D");
0406 if (ALIUtils::debug >= 5)
0407 ALIUtils::dumprm(rmt, "rotation matrix sensor2D");
0408
0409
0410 interslc[0] = (point - (this)->centreGlob()) * XAxism;
0411
0412
0413 CLHEP::Hep3Vector YAxism(0., 1., 0.);
0414 YAxism *= rmt;
0415
0416 interslc[1] = (point - (this)->centreGlob()) * YAxism;
0417
0418 if (ALIUtils::debug >= 5) {
0419 std::cout << " intersection in local coordinates: X= " << interslc[0] << " Y= " << interslc[1] << std::endl;
0420 }
0421 return interslc;
0422 }
0423
0424
0425 ALIdouble OptOCOPS::getMeasFromInters(ALILine& line_xhair, ALILine& ccd, CLHEP::Hep3Vector& cops_line) {
0426 if (ALIUtils::debug >= 5)
0427 std::cout << "***** OptOCOPS::getMeasFromInters" << std::endl;
0428 CLHEP::Hep3Vector inters = line_xhair.intersect(ccd, false) - ccd.pt();
0429 ALIdouble sign = inters * ccd.vec();
0430 if (sign != 0) {
0431 sign = std::abs(sign) / sign;
0432
0433
0434
0435 }
0436 return sign * inters.mag();
0437 }
0438
0439 #ifdef COCOA_VIS
0440
0441 void OptOCOPS::fillVRML() {
0442 ALIVRMLMgr& vrmlmgr = ALIVRMLMgr::getInstance();
0443
0444
0445
0446 ALIdouble CCDlength = findExtraEntryValue("CCDlength");
0447 if (CCDlength == 0.)
0448 CCDlength = 2048 * 14 * 1.E-6;
0449
0450
0451 ALIdouble CCDdim = 2048 * 14 * 1.E-6;
0452
0453
0454 ALIdouble CCDwidth = .005;
0455
0456 if (ALIUtils::debug >= 4)
0457 std::cout << " ccds0 " << ccds[0] << "ccds1 " << ccds[1] << std::endl;
0458 ALIColour colup(1., 0., 0., 0.);
0459 ALIColour coldown(0., 1., 0., 0.);
0460 ALIColour colleft(0., 0., 1., 0.);
0461 ALIColour colright(0., 1., 1., 0.);
0462
0463
0464 ALIdouble ccdsize = 1.;
0465
0466
0467
0468
0469
0470
0471 vrmlmgr.AddBoxDisplaced(
0472 *this, CCDdim, CCDwidth, CCDwidth, ccdsize * (ccds[0].pt() + 0.5 * CCDlength * ccds[0].vec()), &colup);
0473
0474 vrmlmgr.AddBoxDisplaced(
0475 *this, CCDdim, CCDwidth, CCDwidth, ccdsize * (ccds[1].pt() + 0.5 * CCDlength * ccds[1].vec()), &coldown);
0476
0477 vrmlmgr.AddBoxDisplaced(
0478 *this, CCDwidth, CCDdim, CCDwidth, ccdsize * (ccds[2].pt() + 0.5 * CCDlength * ccds[2].vec()), &colleft);
0479
0480 vrmlmgr.AddBoxDisplaced(
0481 *this, CCDwidth, CCDdim, CCDwidth, ccdsize * (ccds[3].pt() + 0.5 * CCDlength * ccds[3].vec()), &colright);
0482
0483 vrmlmgr.SendReferenceFrame(*this, CCDdim);
0484 vrmlmgr.SendName(*this, 0.01);
0485 }
0486
0487
0488 void OptOCOPS::fillIguana() {
0489
0490
0491 ALIdouble CCDlength;
0492 ALIbool pexists = findExtraEntryValueIfExists("CCDlength", CCDlength);
0493 if (!pexists)
0494 CCDlength = 2048 * 14 * 1.E-6;
0495 ALIdouble CCDdim = 2048 * 14 * 1.E-6;
0496 ALIdouble CCDwidth = .005;
0497
0498 if (ALIUtils::debug >= 4)
0499 std::cout << " ccds0 " << ccds[0] << "ccds1 " << ccds[1] << std::endl;
0500 ALIColour colup(0.2, 1., 0., 0.);
0501 ALIColour coldown(0.3, 1., 0., 0.);
0502 ALIColour colleft(0.4, 1., 0., 0.);
0503 ALIColour colright(0.5, 1., 0., 0.);
0504
0505
0506 ALIdouble ccdsize = 1;
0507
0508
0509
0510
0511
0512 std::vector<ALIdouble> spar;
0513 spar.push_back(CCDdim);
0514 spar.push_back(CCDwidth);
0515 spar.push_back(CCDwidth);
0516
0517 IgCocoaFileMgr::getInstance().addSolid(
0518 *this, "BOX", spar, &colup, ccdsize * (ccds[0].pt() + 0.5 * CCDlength * ccds[0].vec()));
0519
0520 IgCocoaFileMgr::getInstance().addSolid(
0521 *this, "BOX", spar, &coldown, ccdsize * (ccds[1].pt() + 0.5 * CCDlength * ccds[1].vec()));
0522
0523 std::vector<ALIdouble> spar2;
0524 spar2.push_back(CCDwidth);
0525 spar2.push_back(CCDdim);
0526 spar2.push_back(CCDwidth);
0527 IgCocoaFileMgr::getInstance().addSolid(
0528 *this, "BOX", spar2, &colleft, ccdsize * (ccds[2].pt() + 0.5 * CCDlength * ccds[2].vec()));
0529
0530 IgCocoaFileMgr::getInstance().addSolid(
0531 *this, "BOX", spar2, &colright, ccdsize * (ccds[3].pt() + 0.5 * CCDlength * ccds[3].vec()));
0532 }
0533 #endif
0534
0535
0536 void OptOCOPS::constructSolidShape() {
0537 ALIdouble go;
0538 GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
0539 gomgr->getGlobalOptionValue("VisScale", go);
0540
0541 theSolidShape = new CocoaSolidShapeBox(
0542 "Box", go * 5. * cm / m, go * 5. * cm / m, go * 1. * cm / m);
0543 }