File indexing completed on 2024-04-06 11:56:04
0001
0002
0003
0004
0005
0006
0007
0008 #include "Alignment/CocoaModel/interface/OptOSensor2D.h"
0009 #include "Alignment/CocoaModel/interface/LightRay.h"
0010 #include "Alignment/CocoaModel/interface/ALIPlane.h"
0011 #include "Alignment/CocoaModel/interface/Measurement.h"
0012 #include "Alignment/CocoaModel/interface/Model.h"
0013 #include "Alignment/CocoaModel/interface/Entry.h"
0014 #include "Alignment/CocoaModel/interface/DeviationsFromFileSensor2D.h"
0015 #ifdef COCOA_VIS
0016 #include "Alignment/CocoaVisMgr/interface/ALIVRMLMgr.h"
0017 #include "Alignment/IgCocoaFileWriter/interface/IgCocoaFileMgr.h"
0018 #endif
0019 #include "Alignment/CocoaUtilities/interface/ALIFileIn.h"
0020 #include "Alignment/CocoaDDLObjects/interface/CocoaSolidShapeBox.h"
0021 #include "Alignment/CocoaUtilities/interface/GlobalOptionMgr.h"
0022 #include "FWCore/Utilities/interface/Exception.h"
0023
0024 #include <iostream>
0025 #include <iomanip>
0026 #include <fstream>
0027 #include <cstdlib>
0028 #include <cmath> // include floating-point std::abs functions
0029
0030 using namespace CLHEP;
0031
0032
0033
0034
0035 void OptOSensor2D::defaultBehaviour(LightRay& lightray, Measurement& meas) { makeMeasurement(lightray, meas); }
0036
0037
0038
0039
0040 void OptOSensor2D::makeMeasurement(LightRay& lightray, Measurement& meas) {
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051 CLHEP::Hep3Vector ZAxis(0., 0, 1.);
0052 CLHEP::HepRotation rmt = rmGlob();
0053 ZAxis = rmt * ZAxis;
0054 if (ALIUtils::debug >= 4) {
0055 ALIUtils::dump3v(centreGlob(), " sensor2D centre Glob ");
0056 ALIUtils::dump3v(ZAxis, " snsor2D normal ");
0057 }
0058
0059 lightray.intersect(ALIPlane(centreGlob(), ZAxis));
0060 CLHEP::Hep3Vector inters = lightray.point();
0061
0062 ALIdouble* interslc;
0063 interslc = convertPointToLocalCoordinates(inters);
0064 ALIdouble interslcx = interslc[0];
0065 ALIdouble interslcy = interslc[1];
0066 meas.setValueSimulated(0, interslcx);
0067 meas.setValueSimulated(1, interslcy);
0068
0069
0070 if (ALIUtils::debug >= 2) {
0071
0072 ALIstring chrg = "";
0073
0074
0075
0076
0077
0078 CLHEP::Hep3Vector measvv(meas.value()[0], meas.value()[1], 0.);
0079 measvv = rmt * measvv;
0080 ALIUtils::dump3v(measvv, " $$$$$$MEAS IN LOCAL FRAME");
0081 ALIUtils::dump3v(measvv + centreGlob(), " $$$$$$MEAS IN GLOBAL FRAME");
0082
0083 ALIdouble detH = 1000 * meas.valueSimulated(0);
0084 if (std::abs(detH) <= 1.e-9)
0085 detH = 0.;
0086 ALIdouble detV = 1000 * meas.valueSimulated(1);
0087 if (std::abs(detV) <= 1.e-9)
0088 detV = 0.;
0089 std::cout << "REAL value: " << chrg << meas.valueType(0) << ": " << 1000 * meas.value()[0] << chrg << " "
0090 << meas.valueType(1) << ": " << 1000 * meas.value()[1] << " (mm) " << (this)->name()
0091 << " DIFF= " << detH - 1000 * meas.value()[0] << " " << detV - 1000 * meas.value()[1] << std::endl;
0092 std::cout << "SIMU value: " << chrg << " " << meas.valueType(0)
0093 << ": "
0094
0095 << detH << chrg << " " << meas.valueType(1) << ": " << detV << " (mm) " << (this)->name() << std::endl;
0096
0097
0098
0099
0100
0101
0102
0103
0104 ALIUtils::dump3v(1000. * (inters - parent()->centreGlob()), " $$$$$$SIMU inters - parent centre");
0105 ALIUtils::dump3v(1000. * (inters - centreGlob()), " $$$$$$SIMU inters - centre");
0106 }
0107
0108
0109
0110 meas.setLightRayPosition(lightray.point());
0111 meas.setLightRayDirection(lightray.direction());
0112
0113 delete[] interslc;
0114 }
0115
0116
0117
0118
0119 void OptOSensor2D::fastTraversesLightRay(LightRay& lightray) {
0120 verbose = ALIUtils::debug;
0121 if (ALIUtils::debug >= 2)
0122 std::cout << "LR: FAST TRAVERSES SENSOR2D " << name() << std::endl;
0123
0124
0125
0126
0127 CLHEP::Hep3Vector ZAxis(0., 0, 1.);
0128 CLHEP::HepRotation rmt = rmGlob();
0129 ZAxis = rmt * ZAxis;
0130 lightray.intersect(ALIPlane(centreGlob(), ZAxis));
0131 CLHEP::Hep3Vector inters = lightray.point();
0132
0133 ALIdouble deviX, deviY, devi;
0134
0135
0136 if (DeviationsFromFileSensor2D::apply() && fdevi_from_file) {
0137
0138 if (ALIUtils::debug >= 4) {
0139 std::cout << "fdeviFromFile" << fdevi_from_file << std::endl;
0140
0141 }
0142
0143 std::vector<Measurement*>& measv = Model::MeasurementList();
0144 unsigned int ii;
0145 Measurement* omeas = nullptr;
0146 for (ii = 0; ii < measv.size(); ii++) {
0147
0148 if (measv[ii]->sensorName() == name()) {
0149 omeas = measv[ii];
0150 break;
0151 }
0152 }
0153 if (omeas == nullptr) {
0154 throw cms::Exception("LogicError") << "@SUB=OptOSensor2D::fastTraversesLightRay\n"
0155 << "meas " << name() << " not found";
0156 }
0157
0158 ALIdouble interslcx = omeas->value(0);
0159 ALIdouble interslcy = omeas->value(1);
0160 if (ALIUtils::debug >= 5)
0161 std::cout << " interslcx " << interslcx << " interslcy " << interslcy << std::endl;
0162
0163
0164
0165 ALIdouble df = ALIUtils::LengthValueDimensionFactor();
0166 interslcx = interslcx / df + 0.010 / df;
0167 interslcy = interslcy / df + 0.010 / df;
0168 if (ALIUtils::debug >= 5)
0169 std::cout << " interslcx " << interslcx << " interslcy " << interslcy << std::endl;
0170
0171
0172 std::pair<ALIdouble, ALIdouble> devis = deviFromFile->getDevis(interslcx, interslcy);
0173 deviX = devis.second;
0174 deviY = -devis.first;
0175
0176
0177
0178
0179
0180
0181
0182 ALIuint entryNo = extraEntryNo("deviX");
0183 if (verbose >= 3)
0184 std::cout << "entrynox" << entryNo << name() << verbose << std::endl;
0185 Entry* entryDeviX = *(ExtraEntryList().begin() + entryNo);
0186 entryDeviX->setValue(deviX);
0187
0188 std::vector<ALIdouble> eevil = ExtraEntryValueOriginalList();
0189
0190 std::vector<ALIdouble>::iterator eevolite = eevil.begin();
0191
0192 *(eevolite + entryNo) = deviX;
0193 if (verbose >= 3)
0194 std::cout << " entryDeviX name " << entryDeviX->name() << entryDeviX->value() << std::endl;
0195 entryNo = extraEntryNo("deviY");
0196 Entry* entryDeviY = *(ExtraEntryList().begin() + entryNo);
0197
0198 entryDeviY->setValue(deviY);
0199 *(eevolite + entryNo) = deviY;
0200
0201
0202 } else {
0203 deviX = findExtraEntryValue("deviX");
0204 deviY = findExtraEntryValue("deviY");
0205
0206
0207 if (fdevi_from_file) {
0208 if (ALIUtils::debug >= 5)
0209 std::cout << "fdeviFromFile" << fdevi_from_file << std::endl;
0210 ALIuint entryNo = extraEntryNo("deviX");
0211 Entry* entryDeviX = *(ExtraEntryList().begin() + entryNo);
0212 if (verbose >= 3)
0213 std::cout << entryDeviX << " entryDeviX name " << entryDeviX->name() << entryDeviX->value() << std::endl;
0214 deviX = entryDeviX->value();
0215 entryNo = extraEntryNo("deviY");
0216 Entry* entryDeviY = *(ExtraEntryList().begin() + entryNo);
0217 if (verbose >= 3)
0218 std::cout << entryDeviY << " entryDeviY name " << entryDeviY->name() << entryDeviY->value() << std::endl;
0219 deviY = entryDeviY->value();
0220
0221 } else {
0222 ALIbool bb = findExtraEntryValueIfExists("devi", devi);
0223 if (bb) {
0224 deviX = devi;
0225 deviY = devi;
0226 }
0227 if (ALIUtils::debug >= 4) {
0228 std::cout << "devi " << devi << " devi x " << deviX << " devi y " << deviY << std::endl;
0229 }
0230 }
0231 }
0232 if (ALIUtils::debug >= 4) {
0233 std::cout << " devi x " << deviX << " devi y " << deviY << std::endl;
0234 }
0235
0236 lightray.setPoint(inters);
0237
0238 lightray.shiftAndDeviateWhileTraversing(this, 'T');
0239 if (ALIUtils::debug >= 2) {
0240 lightray.dumpData("Shifted and Deviated");
0241 }
0242 }
0243
0244
0245
0246
0247 void OptOSensor2D::detailedTraversesLightRay(LightRay& lightray) {
0248 if (ALIUtils::debug >= 4)
0249 std::cout << "%%% LR: DETAILED TRAVERSES SENSOR2D " << name() << std::endl;
0250 if (DeviationsFromFileSensor2D::apply() && fdevi_from_file) {
0251 DeviationsFromFileSensor2D::setApply(false);
0252
0253 if (ALIUtils::debug >= 0)
0254 std::cerr << "!!WARNING: sensor " << name()
0255 << " has read deviation from file and it will not be taken into account. Please use FAST TRAVERSES"
0256 << deviFromFile << std::endl;
0257 }
0258
0259
0260 ALIdouble width = findExtraEntryValue("width");
0261 if (width == 0) {
0262
0263 CLHEP::Hep3Vector ZAxis(0., 0, 1.);
0264 CLHEP::HepRotation rmt = rmGlob();
0265 ZAxis = rmt * ZAxis;
0266 lightray.intersect(ALIPlane(centreGlob(), ZAxis));
0267 CLHEP::Hep3Vector inters = lightray.point();
0268 lightray.setPoint(inters);
0269 if (ALIUtils::debug >= 2) {
0270 lightray.dumpData("LightRay Sensor2D traversed: ");
0271 }
0272 return;
0273 }
0274
0275 if (ALIUtils::debug >= 4)
0276 std::cout << std::endl << "$$$ LR: REFRACTION IN FORWARD PLATE " << std::endl;
0277
0278 ALIPlane plate = getPlate(true, true);
0279
0280 ALIdouble refra_ind1 = 1.;
0281 ALIdouble refra_ind2 = findExtraEntryValueMustExist("refra_ind");
0282 lightray.refract(plate, refra_ind1, refra_ind2);
0283
0284 if (ALIUtils::debug >= 4)
0285 std::cout << std::endl << "$$$ LR: REFRACTION IN BACKWARD PLATE " << std::endl;
0286
0287 plate = getPlate(false, true);
0288
0289 lightray.refract(plate, refra_ind2, refra_ind1);
0290
0291 CLHEP::Hep3Vector inters = lightray.point();
0292 lightray.setPoint(inters);
0293
0294 if (ALIUtils::debug >= 4) {
0295 lightray.dumpData("LightRay Sensor2D traversed: ");
0296 }
0297 }
0298
0299
0300
0301
0302 void OptOSensor2D::fillExtraEntry(std::vector<ALIstring>& wordlist) {
0303 if (ALIUtils::debug >= 5)
0304 std::cout << "OptOSensor2D fillExtraEntry wordlist[1] " << wordlist[1] << std::endl;
0305
0306 fdevi_from_file = false;
0307
0308 if (wordlist[1] == ALIstring("devi") && wordlist[2] == ALIstring("from_file")) {
0309
0310 ALIstring fnam;
0311 if (wordlist.size() >= 4) {
0312 fnam = wordlist[3];
0313 } else {
0314
0315 fnam = "dat/devi-";
0316 fnam += shortName();
0317 fnam += ".dat";
0318 }
0319
0320 ALIFileIn& ifdevi = ALIFileIn::getInstance(fnam);
0321
0322
0323 if (ALIUtils::debug >= 4)
0324 std::cout << "Opening deviation file: " << fnam << std::endl;
0325
0326
0327
0328
0329
0330 deviFromFile = new DeviationsFromFileSensor2D();
0331 fdevi_from_file = true;
0332 if (ALIUtils::debug >= 5)
0333 std::cout << "deviFromFile " << deviFromFile << std::endl;
0334
0335 ALIstring sensor1_name, sensor2_name;
0336 ALIdouble sensor_dist;
0337 ALIdouble prec_deviX, prec_deviY;
0338
0339 std::vector<ALIstring> wl;
0340 ifdevi.getWordsInLine(wl);
0341 sensor1_name = wl[0];
0342 sensor2_name = wl[1];
0343 sensor_dist = atof(wl[2].c_str());
0344
0345 if (sensor1_name[sensor1_name.size() - 2] == 'c') {
0346 sensor1_name = sensor1_name.substr(0, sensor1_name.size() - 1);
0347 }
0348 if (sensor2_name[sensor2_name.size() - 2] == 'c') {
0349 sensor2_name = sensor2_name.substr(0, sensor2_name.size() - 1);
0350 }
0351 if (ALIUtils::debug >= 5)
0352 std::cout << "sensor1_name " << sensor1_name << " sensor2_name " << sensor2_name << " sensor_dist " << sensor_dist
0353 << " unknown " << wl[3] << std::endl;
0354
0355 ifdevi.getWordsInLine(wl);
0356 prec_deviX = atof(wl[0].c_str());
0357 prec_deviY = atof(wl[1].c_str());
0358
0359 if (ALIUtils::debug >= 5)
0360 std::cout << "prec_deviX " << prec_deviX << " prec_deviY " << prec_deviY << std::endl;
0361
0362 deviFromFile = new DeviationsFromFileSensor2D();
0363 ALIdouble offsetX, offsetY;
0364 if (wl.size() == 5) {
0365 offsetX = ALIUtils::getFloat(wl[3]);
0366 offsetY = ALIUtils::getFloat(wl[4]);
0367 deviFromFile->setOffset(offsetX, offsetY);
0368 }
0369 deviFromFile->readFile(ifdevi);
0370 fdevi_from_file = true;
0371 if (ALIUtils::debug >= 5)
0372 std::cout << "deviFromFile " << deviFromFile << std::endl;
0373
0374
0375 std::vector<ALIstring> wlo;
0376 char chartmp[20];
0377 wlo.push_back(wordlist[0]);
0378 wlo.push_back("deviX");
0379 wlo.push_back("0");
0380 gcvt(prec_deviX, 10, chartmp);
0381 wlo.push_back(ALIstring(chartmp));
0382 wlo.push_back("cal");
0383 std::vector<ALIstring> wl2(wlo);
0384 OpticalObject::fillExtraEntry(wlo);
0385
0386 wl2[1] = "deviY";
0387 gcvt(prec_deviY, 10, chartmp);
0388 wl2[3] = ALIstring(chartmp);
0389 OpticalObject::fillExtraEntry(wl2);
0390
0391 } else {
0392 OpticalObject::fillExtraEntry(wordlist);
0393 }
0394 }
0395
0396
0397
0398
0399 ALIdouble* OptOSensor2D::convertPointToLocalCoordinates(const CLHEP::Hep3Vector& point) {
0400 ALIdouble* interslc = new ALIdouble[2];
0401
0402
0403 CLHEP::HepRotation rmt = rmGlob();
0404 CLHEP::Hep3Vector XAxism(1., 0., 0.);
0405 XAxism *= rmt;
0406 if (ALIUtils::debug >= 5)
0407 ALIUtils::dump3v((this)->centreGlob(), "centre glob sensor2D");
0408 if (ALIUtils::debug >= 5)
0409 ALIUtils::dumprm(rmt, "rotation matrix sensor2D");
0410
0411 if (ALIUtils::debug >= 5)
0412 ALIUtils::dump3v(XAxism, "XAxism");
0413 interslc[0] = (point - (this)->centreGlob()) * XAxism;
0414
0415
0416 CLHEP::Hep3Vector YAxism(0., 1., 0.);
0417 YAxism *= rmt;
0418 if (ALIUtils::debug >= 5)
0419 ALIUtils::dump3v(YAxism, "YAxism");
0420 interslc[1] = (point - (this)->centreGlob()) * YAxism;
0421
0422 if (ALIUtils::debug >= 5) {
0423 std::cout << " intersection in local coordinates: X= " << interslc[0] << " Y= " << interslc[1] << std::endl;
0424 ALIUtils::dump3v(point - (this)->centreGlob(), " inters - centre ");
0425 }
0426 return interslc;
0427 }
0428
0429 #ifdef COCOA_VIS
0430
0431 void OptOSensor2D::fillVRML() {
0432
0433 ALIVRMLMgr& vrmlmgr = ALIVRMLMgr::getInstance();
0434 ALIColour* col = new ALIColour(0., 0., 1., 0.);
0435 vrmlmgr.AddBox(*this, 1., 1., .2, col);
0436 vrmlmgr.SendReferenceFrame(*this, 0.6);
0437 vrmlmgr.SendName(*this, 0.1);
0438 }
0439
0440
0441 void OptOSensor2D::fillIguana() {
0442 ALIColour* col = new ALIColour(0., 0., 1., 0.);
0443 std::vector<ALIdouble> spar;
0444 spar.push_back(20.);
0445 spar.push_back(20.);
0446 spar.push_back(5.);
0447 IgCocoaFileMgr::getInstance().addSolid(*this, "BOX", spar, col);
0448 }
0449 #endif
0450
0451
0452 void OptOSensor2D::constructSolidShape() {
0453 ALIdouble go;
0454 GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
0455 gomgr->getGlobalOptionValue("VisScale", go);
0456
0457 theSolidShape = new CocoaSolidShapeBox(
0458 "Box", go * 4. * cm / m, go * 4. * cm / m, go * 1. * cm / m);
0459 }