Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
//   COCOA class implementation file
//Id:  OpticalObject.cc
//CAT: Model
//
//   History: v1.0
//   Pedro Arce

#include "Alignment/CocoaModel/interface/OpticalObject.h"
#include "Alignment/CocoaModel/interface/OpticalObjectMgr.h"
#include "Alignment/CocoaModel/interface/OptOLaser.h"
#include "Alignment/CocoaModel/interface/OptOSource.h"
#include "Alignment/CocoaModel/interface/OptOXLaser.h"
#include "Alignment/CocoaModel/interface/OptOMirror.h"
#include "Alignment/CocoaModel/interface/OptOPlateSplitter.h"
#include "Alignment/CocoaModel/interface/OptOCubeSplitter.h"
#include "Alignment/CocoaModel/interface/OptOModifiedRhomboidPrism.h"
#include "Alignment/CocoaModel/interface/OptOOpticalSquare.h"
#include "Alignment/CocoaModel/interface/OptOLens.h"
#include "Alignment/CocoaModel/interface/OptORisleyPrism.h"
#include "Alignment/CocoaModel/interface/OptOSensor2D.h"
#include "Alignment/CocoaModel/interface/OptODistancemeter.h"
#include "Alignment/CocoaModel/interface/OptODistancemeter3dim.h"
#include "Alignment/CocoaModel/interface/OptOScreen.h"
#include "Alignment/CocoaModel/interface/OptOTiltmeter.h"
#include "Alignment/CocoaModel/interface/OptOPinhole.h"
#include "Alignment/CocoaModel/interface/OptOCOPS.h"
#include "Alignment/CocoaModel/interface/OptOUserDefined.h"
#include "Alignment/CocoaModel/interface/ALIPlane.h"

#include "Alignment/CocoaUtilities/interface/ALIUtils.h"
#include "Alignment/CocoaModel/interface/Model.h"
#include "Alignment/CocoaUtilities/interface/ALIFileIn.h"
#include "Alignment/CocoaUtilities/interface/GlobalOptionMgr.h"
#include "Alignment/CocoaModel/interface/EntryLengthAffCentre.h"
#include "Alignment/CocoaModel/interface/EntryAngleAffAngles.h"
#include "Alignment/CocoaModel/interface/EntryNoDim.h"
#include "Alignment/CocoaModel/interface/EntryMgr.h"

#include "Alignment/CocoaDDLObjects/interface/CocoaMaterialElementary.h"
#include "Alignment/CocoaDDLObjects/interface/CocoaSolidShapeBox.h"

#include "CondFormats/OptAlignObjects/interface/OpticalAlignInfo.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include <CLHEP/Units/SystemOfUnits.h>

#include <cstdlib>
#include <iostream>

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Constructor: set OptO parent, type, name and fcopyData (if data is read or copied)
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
OpticalObject::OpticalObject(OpticalObject* parent,
                             const ALIstring& type,
                             const ALIstring& name,
                             const ALIbool copy_data)
    : theParent(parent), theType(type), theName(name), fcopyData(copy_data) {
  if (ALIUtils::debug >= 4) {
    std::cout << std::endl
              << "@@@@ Creating OpticalObject: NAME= " << theName << " TYPE= " << theType << " fcopyData " << fcopyData
              << std::endl;
  }

  OpticalObjectMgr::getInstance()->registerMe(this);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ construct: read centre and angles from file (or copy it) and create component OptOs
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::construct() {
  //---------- Get file handler
  ALIFileIn& filein = ALIFileIn::getInstance(Model::SDFName());
  /*-  if(!filein) {
    filein.ErrorInLine();
    std::cerr << "cannot open file SystemDescription.txt" << std::endl;
    exit(0);
    }*/

  if (theParent != nullptr) {  //----- OptO 'system' has no parent (and no affine frame)
    //---------- Read or copy Data
    if (!fcopyData) {
      if (ALIUtils::debug >= 4)
        std::cout << "@@@@ Reading data of Optical Object " << name() << std::endl;
      readData(filein);
    } else {
      if (ALIUtils::debug >= 4)
        std::cout << "Copy data of Optical Object " << name() << std::endl;
      copyData();
    }

    //---------- Set global coordinates
    setGlobalCoordinates();
    //---------- Set ValueDisplacementByFitting to 0. !!done at Entry construction
    /*    std::vector<Entry*>::const_iterator vecite;
    for ( vecite = CoordinateEntryList().begin(); vecite != CoordinateEntryList().end(); vecite++) {
      (*vecite)->setValueDisplacementByFitting( 0. );
    }
    */

    //---------- Set original entry values
    setOriginalEntryValues();
  }

  //---------- Create the OptO that compose this one
  createComponentOptOs(filein);

  //---------- Construct material
  constructMaterial();

  //---------- Construct solid shape
  constructSolidShape();
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Reads the affine frame (centre and angles) that every OpticalObject should have
//@@ If type is source, it may only read the center (and set the angles to 0)
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::readData(ALIFileIn& filein) {
  //---------- See if there are extra entries and read them
  std::vector<ALIstring> wordlist;
  filein.getWordsInLine(wordlist);
  if (wordlist[0] == ALIstring("ENTRY")) {
    //---------- Read extra entries from file
    readExtraEntries(filein);
    filein.getWordsInLine(wordlist);
  }

  //--------- set centre and angles not global (default behaviour)
  centreIsGlobal = false;
  anglesIsGlobal = false;

  //--------- readCoordinates
  if (type() == ALIstring("source") || type() == ALIstring("pinhole")) {
    readCoordinates(wordlist[0], "centre", filein);
    setAnglesNull();
  } else {
    //---------- Read centre and angles
    readCoordinates(wordlist[0], "centre", filein);
    filein.getWordsInLine(wordlist);
    readCoordinates(wordlist[0], "angles", filein);
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ ReadExtraEntries: Reads extra entries from file
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::readExtraEntries(ALIFileIn& filein) {
  //---------- Loop extra entries until a '}'-line is found
  std::vector<ALIstring> wordlist;
  for (;;) {
    filein.getWordsInLine(wordlist);
    if (wordlist[0] != ALIstring("}")) {
      fillExtraEntry(wordlist);
    } else {
      break;
    }
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ fillExtraEntry: create and fill an extra entry. Put it in lists and set link OptO it belongs to
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::fillExtraEntry(std::vector<ALIstring>& wordlist) {
  //- if(ALIUtils::debug >= 5) std::cout << "fillExtraEntry wordlist0 " << wordlist[0].c_str() << std::endl;

  //---------- Check which type of entry to create
  Entry* xentry;
  if (wordlist[0] == ALIstring("length")) {
    xentry = new EntryLength(wordlist[0]);
  } else if (wordlist[0] == ALIstring("angle")) {
    xentry = new EntryAngle(wordlist[0]);
  } else if (wordlist[0] == ALIstring("nodim")) {
    xentry = new EntryNoDim(wordlist[0]);
  } else {
    std::cerr << "!!ERROR: Exiting...  unknown type of Extra Entry " << wordlist[0] << std::endl;
    ALIUtils::dumpVS(wordlist, " Only 'length', 'angle' or 'nodim' are allowed ", std::cerr);
    exit(2);
  }

  if (ALIUtils::debug >= 99) {
    ALIUtils::dumpVS(wordlist, "fillExtraEntry: ", std::cout);
  }
  //---------- Erase first word of line read (type of entry)
  wordlist.erase(wordlist.begin());

  if (ALIUtils::debug >= 99) {
    ALIUtils::dumpVS(wordlist, "fillExtraEntry: ", std::cout);
  }

  //---------- Set link from entry to OptO it belongs to
  xentry->setOptOCurrent(this);
  //----- Name is filled from here to be consistent with fillCoordinate
  xentry->fillName(wordlist[0]);
  //---------- Fill entry with data in line read
  xentry->fill(wordlist);

  //---------- Add entry to entry lists
  Model::addEntryToList(xentry);
  addExtraEntryToList(xentry);

  if (ALIUtils::debug >= 5)
    std::cout << "fillExtraEntry: xentry_value" << xentry->value() << xentry->ValueDimensionFactor() << std::endl;

  //---------- Add entry value to list
  addExtraEntryValueToList(xentry->value());
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ readCoordinates: Read centre or angles ( following coor_type )
//@@ check that coordinate type is the expected one
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::readCoordinates(const ALIstring& coor_type_read,
                                    const ALIstring& coor_type_expected,
                                    ALIFileIn& filein) {
  ALIstring coor_type_reads = coor_type_read.substr(0, 6);
  if (coor_type_reads == "center")
    coor_type_reads = "centre";
  //---------- if after the first six letters ther is a 'G', it means they are global coordinates
  //----- If data is read from a 'report.out', it is always local and this is not needed

  //TODO: check that if only one entry of the three is read from 'report.out', the input file does not give global coordinates (it would cause havoc)
  if (EntryMgr::getInstance()->findEntryByLongName(longName(), "") == nullptr) {
    if (coor_type_read.size() == 7) {
      if (coor_type_read[6] == 'G') {
        if (ALIUtils::debug >= 5)
          std::cout << " coordinate global " << coor_type_read << std::endl;
        if (coor_type_expected == "centre") {
          centreIsGlobal = true;
        } else if (coor_type_expected == "angles") {
          anglesIsGlobal = true;
        }
      }
    }
  }

  std::vector<ALIstring> wordlist;
  //---------- Read 4 lines: first is entry type, rest are three coordinates (each one will be an individual entry)
  ALIstring coor_names[3];  // to check if using cartesian, cylindrical or spherical coordinates

  for (int ii = 0; ii < 4; ii++) {
    if (ii == 0) {
      //---------- Check that first line is of expected type
      if (coor_type_reads != coor_type_expected) {
        filein.ErrorInLine();
        std::cerr << "readCoordinates: " << coor_type_expected << " should be read here, instead of " << coor_type_reads
                  << std::endl;
        exit(1);
      }
    } else {
      //----------- Fill entry Data
      filein.getWordsInLine(wordlist);
      coor_names[ii - 1] = wordlist[0];
      fillCoordinateEntry(coor_type_expected, wordlist);
    }
  }

  //---- Transform coordinate system if cylindrical or spherical
  if (coor_names[0] == ALIstring("X") && coor_names[1] == ALIstring("Y") && coor_names[2] == ALIstring("Z")) {
    //do nothing
  } else if (coor_names[0] == ALIstring("R") && coor_names[1] == ALIstring("PHI") && coor_names[2] == ALIstring("Z")) {
    transformCylindrical2Cartesian();
  } else if (coor_names[0] == ALIstring("R") && coor_names[1] == ALIstring("THE") &&
             coor_names[2] == ALIstring("PHI")) {
    transformSpherical2Cartesian();
  } else {
    std::cerr << "!!!EXITING: coordinates have to be cartesian (X ,Y ,Z), or cylindrical (R, PHI, Z) or spherical (R, "
                 "THE, PHI) "
              << std::endl
              << " they are " << coor_names[0] << ", " << coor_names[1] << ", " << coor_names[2] << "." << std::endl;
    exit(1);
  }
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void OpticalObject::transformCylindrical2Cartesian() {
  ALIuint ii;
  ALIuint siz = theCoordinateEntryVector.size();
  ALIdouble R = theCoordinateEntryVector[0]->value();
  ALIdouble phi = theCoordinateEntryVector[1]->value() / ALIUtils::LengthValueDimensionFactor() *
                  ALIUtils::AngleValueDimensionFactor();
  if (siz != 3) {
    throw cms::Exception("LogicError") << "@SUB=OpticalObject::transformCylindrical2Cartesian\n"
                                       << "Transformation from cylindrical to cartesian coordinates requires the"
                                       << " coordinate entry vector to have a size of three.";
  }
  ALIdouble newcoor[3] = {
      R * cos(phi),
      R * sin(phi),
      theCoordinateEntryVector[2]->value()  // Z
  };
  //-  std::cout << " phi " << phi << std::endl;
  //----- Name is filled from here to include 'centre' or 'angles'

  for (ii = 0; ii < siz; ii++) {
    if (ALIUtils::debug >= 5)
      std::cout << " OpticalObject::transformCylindrical2Cartesian " << ii << " " << newcoor[ii] << std::endl;
    theCoordinateEntryVector[ii]->setValue(newcoor[ii]);
  }
  // change the names
  ALIstring name = "centre_X";
  theCoordinateEntryVector[0]->fillName(name);
  name = "centre_Y";
  theCoordinateEntryVector[1]->fillName(name);
  name = "centre_Z";
  theCoordinateEntryVector[2]->fillName(name);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void OpticalObject::transformSpherical2Cartesian() {}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ fillCoordinateEntry: fill data of Coordinate Entry with data read
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::fillCoordinateEntry(const ALIstring& coor_type, const std::vector<ALIstring>& wordlist) {
  //---------- Select which type of entry to create
  Entry* entry = nullptr;
  if (coor_type == ALIstring("centre")) {
    entry = new EntryLengthAffCentre(coor_type);
  } else if (coor_type == ALIstring("angles")) {
    entry = new EntryAngleAffAngles(coor_type);
  } else {
    std::cerr << " !!! FATAL ERROR at  OpticalObject::fillCoordinateEntry : wrong coordinate type " << coor_type
              << std::endl;
    exit(1);
  }

  //---------- Set link from entry to OptO it belongs to
  entry->setOptOCurrent(this);
  //----- Name is filled from here to include 'centre' or 'angles'
  ALIstring name = coor_type + "_" + wordlist[0];
  entry->fillName(name);
  //---------- Fill entry with data read
  entry->fill(wordlist);

  //---------- Add entry to lists
  Model::addEntryToList(entry);
  addCoordinateEntryToList(entry);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ SetAnglesNull: create three angle entries and set values to zero
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::setAnglesNull() {
  EntryAngleAffAngles* entry;
  //---------- three names will be X, Y and Z
  ALIstring coor("XYZ");

  //---------- Fill the three entries
  for (int ii = 0; ii < 3; ii++) {
    entry = new EntryAngleAffAngles("angles");

    //---------- Set link from entry to OptO it belongs to
    entry->setOptOCurrent(this);
    //----- Name is filled from here to be consistent with fillCoordinate
    ALIstring name = "angles_" + coor.substr(ii, 1);
    entry->fillName(name);
    //---------- Set entry data to zero
    entry->fillNull();

    // entry.fillNull( tt );

    //---------- Add entry to lists
    Model::addEntryToList(entry);
    addCoordinateEntryToList(entry);
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ copyData: look for OptO of the same type as this one and copy its components as the components of present OptO
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::copyData() {
  centreIsGlobal = false;
  anglesIsGlobal = false;
  if (ALIUtils::debug >= 5)
    std::cout << "entering copyData()" << std::endl;

  //---------- Get copied OptO
  OpticalObject* opto = Model::nextOptOToCopy();

  //---------- build name: for a copied OptO, now name is parent name, add the last part of the copied OptO
  ALIint copy_name_last_slash = opto->name().rfind('/');
  ALIint copy_name_size = opto->name().length();
  //-  if(ALIUtils::debug >= 9) std::cout << "BUILD UP NAME0 " << theName << std::endl;
  theName.append(opto->name(), copy_name_last_slash, copy_name_size);
  if (ALIUtils::debug >= 5)
    std::cout << "copying OptO: " << opto->name() << " to OptO " << theName << std::endl;

  //---------- Copy Extra Entries from copied OptO
  std::vector<Entry*>::const_iterator vecite;
  for (vecite = opto->ExtraEntryList().begin(); vecite != opto->ExtraEntryList().end(); ++vecite) {
    std::vector<ALIstring> wordlist;
    wordlist.push_back((*vecite)->type());
    buildWordList((*vecite), wordlist);
    if (ALIUtils::debug >= 9) {
      ALIUtils::dumpVS(wordlist, "copyData: ", std::cout);
    }
    fillExtraEntry(wordlist);
  }

  //---------- Copy Coordinate Entries from copied OptO
  for (vecite = opto->CoordinateEntryList().begin(); vecite != opto->CoordinateEntryList().end(); ++vecite) {
    std::vector<ALIstring> wordlist;
    buildWordList((*vecite), wordlist);
    //----- first three coordinates centre, second three coordinates angles!!PROTECT AGAINST OTHER POSSIBILITIES!!
    ALIstring coor_name;
    if (vecite - opto->CoordinateEntryList().begin() < 3) {
      coor_name = "centre";
    } else {
      coor_name = "angles";
    }
    fillCoordinateEntry(coor_name, wordlist);
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ buildWordList:  for copied OptOs, make a list of words to be passed to fillExtraEntry or fill CoordinateEntry
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::buildWordList(const Entry* entry, std::vector<ALIstring>& wordlist) {
  //---------- 1st add name
  wordlist.push_back(entry->name());

  //---------- 1st add value
  char chartmp[20];
  gcvt(entry->value() / entry->ValueDimensionFactor(), 10, chartmp);
  wordlist.push_back(chartmp);

  //---------- 1st add sigma
  gcvt(entry->sigma() / entry->SigmaDimensionFactor(), 10, chartmp);
  wordlist.push_back(chartmp);

  //---------- 1st add quality
  ALIstring strtmp;
  ALIint inttmp = entry->quality();
  switch (inttmp) {
    case 0:
      strtmp = "fix";
      break;
    case 1:
      strtmp = "cal";
      break;
    case 2:
      strtmp = "unk";
      break;
    default:
      std::cerr << "buildWordList: entry " << entry->OptOCurrent()->name() << entry->name() << " quality not found "
                << inttmp << std::endl;
      break;
  }
  wordlist.push_back(strtmp);

  if (ALIUtils::debug >= 9) {
    ALIUtils::dumpVS(wordlist, "buildWordList: ", std::cout);
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ createComponentOptOs: create components objects of this Optical Object (and call their construct(), so that they read their own data)
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::createComponentOptOs(ALIFileIn& filein) {
  //---------- flag to determine if components are copied or read (it is passed to the constructor of component OptOs)
  ALIbool fcopyComponents = false;

  //---------- Get list of components of current OptO (copy it to 'vopto_types')
  std::vector<ALIstring> vopto_types;
  int igetood = Model::getComponentOptOTypes(type(), vopto_types);
  if (!igetood) {
    if (ALIUtils::debug >= 5)
      std::cout << " NO MORE COMPONENTS IN THIS OptO" << name() << std::endl;
    return;
  }

  /*  //---------- Dump component list
  if(ALIUtils::debug >= 5) {
    ALIUtils::dumpVS( wordlist, "SYSTEM: ", std::cout );
    }*/

  //---------- Loop components (have to follow list in 'vopto_types')
  std::vector<ALIstring>::iterator vsite;
  std::vector<ALIstring> wordlist;
  for (vsite = vopto_types.begin(); vsite != vopto_types.end(); ++vsite) {
    //----- If it is not being copied, read first line describing object
    //- std::cout << "fcopyy" << fcopyComponents << fcopyData << theName << *vsite << std::endl;
    if (!fcopyData && !fcopyComponents)
      filein.getWordsInLine(wordlist);
    //t    if( !fcopyData ) filein.getWordsInLine(wordlist);

    //----- Check first line describing object
    //--- Don't check it if OptO is going to be copied (fcopyData = 1)
    //--- If OptO is not copied, but components will be copied, check if only for the first component (for the second fcopyComponents=1)
    if (fcopyData || fcopyComponents) {
      fcopyComponents = true;
      //--- If OptO not copied, but components will be copied
    } else if (wordlist[0] == ALIstring("copy_components")) {
      if (ALIUtils::debug >= 3)
        std::cout << "createComponentOptOs: copy_components" << wordlist[0] << std::endl;
      Model::createCopyComponentList(type());
      fcopyComponents = true;  //--- for the second and following components
      //----- If no copying: check that type is the expected one
    } else if (wordlist[0] != (*vsite)) {
      filein.ErrorInLine();
      std::cerr << "!!! Badly placed OpticalObject: " << wordlist[0] << " should be = " << (*vsite) << std::endl;
      exit(2);
    }

    //---------- Make composite component name
    ALIstring component_name = name();
    //----- if component is not going to be copied add name of component to the parent (if OptO is not copied and components will be, wordlist = 'copy-components', there is no wordlist[1]
    if (!fcopyComponents) {
      component_name += '/';
      component_name += wordlist[1];
    }
    //    if ( ALIUtils::debug >= 6 ) std::cout << "MAKE NAME " << name() << " TO " << component_name << std::endl;

    //---------- Create OpticalObject of the corresponding type
    OpticalObject* OptOcomponent = createNewOptO(this, *vsite, component_name, fcopyComponents);

    //----- Fill CMS software ID
    if (wordlist.size() == 3) {
      OptOcomponent->setID(ALIUtils::getInt(wordlist[2]));
    } else {
      OptOcomponent->setID(OpticalObjectMgr::getInstance()->buildCmsSwID());
    }

    //---------- Construct it (read data and
    OptOcomponent->construct();

    //---------- Fill OptO tree and OptO list
    Model::OptOList().push_back(OptOcomponent);
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
OpticalObject* OpticalObject::createNewOptO(OpticalObject* parent,
                                            ALIstring optoType,
                                            ALIstring optoName,
                                            ALIbool fcopyComponents) {
  if (ALIUtils::debug >= 3)
    std::cout << " OpticalObject::createNewOptO optoType " << optoType << " optoName " << optoName << " parent "
              << parent->name() << std::endl;
  OpticalObject* OptOcomponent;
  if (optoType == "laser") {
    OptOcomponent = new OptOLaser(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "source") {
    OptOcomponent = new OptOSource(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "Xlaser") {
    OptOcomponent = new OptOXLaser(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "mirror") {
    OptOcomponent = new OptOMirror(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "plate_splitter") {
    OptOcomponent = new OptOPlateSplitter(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "cube_splitter") {
    OptOcomponent = new OptOCubeSplitter(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "modified_rhomboid_prism") {
    OptOcomponent = new OptOModifiedRhomboidPrism(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "pseudo_pentaprism" || optoType == "optical_square") {
    OptOcomponent = new OptOOpticalSquare(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "lens") {
    OptOcomponent = new OptOLens(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "Risley_prism") {
    OptOcomponent = new OptORisleyPrism(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "sensor2D") {
    OptOcomponent = new OptOSensor2D(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "distancemeter" || optoType == "distancemeter1dim") {
    OptOcomponent = new OptODistancemeter(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "distancemeter3dim") {
    OptOcomponent = new OptODistancemeter3dim(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "distance_target") {
    OptOcomponent = new OptOScreen(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "tiltmeter") {
    OptOcomponent = new OptOTiltmeter(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "pinhole") {
    OptOcomponent = new OptOPinhole(this, optoType, optoName, fcopyComponents);
  } else if (optoType == "COPS") {
    OptOcomponent = new OptOCOPS(this, optoType, optoName, fcopyComponents);
  } else {
    OptOcomponent =
        //o       new OpticalObject( this, optoType, optoName, fcopyComponents );
        new OptOUserDefined(this, optoType, optoName, fcopyComponents);
  }

  return OptOcomponent;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ SetGlobalCoordinates
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::setGlobalCoordinates() {
  setGlobalCentre();
  setGlobalRM();
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::setGlobalCentre() {
  SetCentreLocalFromEntryValues();
  if (type() != ALIstring("system") && !centreIsGlobal) {
    SetCentreGlobFromCentreLocal();
  }
  if (anglesIsGlobal) {
    std::cerr << "!!!FATAL ERROR: angles in global coordinates not supported momentarily " << std::endl;
    abort();
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::setGlobalRM() {
  SetRMLocalFromEntryValues();
  if (!anglesIsGlobal) {
    SetRMGlobFromRMLocal();
  }

  // Calculate local rot axis with new rm glob
  calculateLocalRotationAxisInGlobal();
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::setGlobalRMOriginalOriginal(const CLHEP::HepRotation& rmorioriLocal) {
  CLHEP::HepRotation rmorioriold = rmGlobOriginalOriginal();
  if (ALIUtils::debug >= 5) {
    std::cout << " setGlobalRMOriginalOriginal OptO " << name() << std::endl;
    ALIUtils::dumprm(rmorioriLocal, " setGlobalRMOriginalOriginal new local");
    ALIUtils::dumprm(rmGlobOriginalOriginal(), " setGlobalRMOriginalOriginal old ");
  }

  SetRMGlobFromRMLocalOriginalOriginal(rmorioriLocal);

  /*  //---- multiplyt it by parent rmGlobOriginalOriginal
  if( parent()->type() != ALIstring("system") ) {
    theRmGlobOriginalOriginal = parent()->rmGlobOriginalOriginal() * theRmGlobOriginalOriginal;
    }*/

  if (ALIUtils::debug >= 5) {
    ALIUtils::dumprm(parent()->rmGlobOriginalOriginal(), " parent rmoriori glob  ");
    ALIUtils::dumprm(rmGlobOriginalOriginal(), " setGlobalRMOriginalOriginal new ");
  }

  //----------- Reset RMGlobOriginalOriginal() of every component
  std::vector<OpticalObject*> vopto;
  ALIbool igetood = Model::getComponentOptOs(name(), vopto);
  if (!igetood) {
    //    std::cout << " NO MORE COMPONENTS IN THIS OptO" << name() << std::endl ;
    return;
  }
  std::vector<OpticalObject*>::const_iterator vocite;
  for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
    CLHEP::HepRotation rmorioriLocalChild = (*vocite)->buildRmFromEntryValuesOriginalOriginal();
    (*vocite)->setGlobalRMOriginalOriginal(rmorioriLocalChild);
    //    (*vocite)->propagateGlobalRMOriginalOriginalChangeToChildren( rmorioriold, rmGlobOriginalOriginal() );
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::propagateGlobalRMOriginalOriginalChangeToChildren(const CLHEP::HepRotation& rmorioriold,
                                                                      const CLHEP::HepRotation& rmoriorinew) {
  std::cout << " propagateGlobalRMOriginalOriginalChangeToChildren OptO " << name() << std::endl;
  ALIUtils::dumprm(rmGlobOriginalOriginal(), " setGlobalRMOriginalOriginal old ");
  theRmGlobOriginalOriginal = rmoriorinew.inverse() * theRmGlobOriginalOriginal;
  theRmGlobOriginalOriginal = rmorioriold * theRmGlobOriginalOriginal;
  ALIUtils::dumprm(rmGlobOriginalOriginal(), " setGlobalRMOriginalOriginal new ");

  //----------- Reset RMGlobOriginalOriginal() of every component
  std::vector<OpticalObject*> vopto;
  ALIbool igetood = Model::getComponentOptOs(name(), vopto);
  if (!igetood) {
    //    std::cout << " NO MORE COMPONENTS IN THIS OptO" << name() << std::endl ;
    return;
  }
  std::vector<OpticalObject*>::const_iterator vocite;
  for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
    //    CLHEP::HepRotation rmoriorid = buildRmFromEntryValues();
    (*vocite)->propagateGlobalRMOriginalOriginalChangeToChildren(rmorioriold, rmoriorinew);
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CLHEP::HepRotation OpticalObject::buildRmFromEntryValuesOriginalOriginal() {
  CLHEP::HepRotation rm;
  const OpticalObject* opto_par = this;
  //  if(Model::GlobalOptions()["rotateAroundLocal"] == 0) {
  if (ALIUtils::debug >= 55)
    std::cout << "rotate with parent: before X " << opto_par->parent()->name() << " "
              << parent()->getEntryRMangle(XCoor) << std::endl;
  const std::vector<Entry*>& cel = CoordinateEntryList();
  rm.rotateX(cel[3]->valueOriginalOriginal());
  if (ALIUtils::debug >= 55)
    std::cout << "rotate with parent: before Y " << opto_par->parent()->name() << " "
              << parent()->getEntryRMangle(YCoor) << std::endl;
  rm.rotateY(cel[4]->valueOriginalOriginal());
  if (ALIUtils::debug >= 55)
    std::cout << "rotate with parent: before Z " << opto_par->parent()->name() << " "
              << parent()->getEntryRMangle(ZCoor) << std::endl;
  rm.rotateZ(cel[5]->valueOriginalOriginal());
  //-  rm.rotateZ( getEntryRMangle(ZCoor) );
  if (ALIUtils::debug >= 54)
    ALIUtils::dumprm(theRmGlob, "SetRMGlobFromRMLocal: RM GLOB after " + opto_par->parent()->longName());

  return rm;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::SetCentreLocalFromEntryValues() {
  //  std::vector<Entry*>::const_iterator vecite = CoordinateEntryList().begin();
  //-  std::cout << "PARENTSYSTEM" << name() << parent() <<"ZZ"<<vecite<< std::endl;
  //  std::cout << " OpticalObject::setGlobalCoordinates " << this->name() << std::endl;
  //-      std::cout << veite << "WW" << *vecite << std::endl;
  //---------------------------------------- Set global centre
  //----------------------------------- Get local centre from Entries
  theCentreGlob.setX(getEntryCentre(XCoor));
  theCentreGlob.setY(getEntryCentre(YCoor));
  theCentreGlob.setZ(getEntryCentre(ZCoor));
  if (ALIUtils::debug >= 4)
    ALIUtils::dump3v(centreGlob(), "SetCentreLocalFromEntryValues: CENTRE LOCAL ");
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::SetRMLocalFromEntryValues() {
  //---------- Set global rotation matrix
  //-------- Get rm from Entries
  theRmGlob = CLHEP::HepRotation();
  theRmGlob.rotateX(getEntryRMangle(XCoor));
  if (ALIUtils::debug >= 4) {
    std::cout << "  getEntryRMangle(XCoor) )" << getEntryRMangle(XCoor) << std::endl;
    ALIUtils::dumprm(theRmGlob, "SetRMLocalFromEntryValues: RM after X");
  }
  theRmGlob.rotateY(getEntryRMangle(YCoor));
  if (ALIUtils::debug >= 4) {
    std::cout << "  getEntryRMangle(YCoor) )" << getEntryRMangle(YCoor) << std::endl;
    ALIUtils::dumprm(theRmGlob, "SetRMLocalFromEntryValues: RM after Y");
  }
  theRmGlob.rotateZ(getEntryRMangle(ZCoor));
  if (ALIUtils::debug >= 4) {
    std::cout << "  getEntryRMangle(ZCoor) )" << getEntryRMangle(ZCoor) << std::endl;
    ALIUtils::dumprm(theRmGlob, "SetRMLocalFromEntryValues: RM FINAL");
  }

  //----- angles are relative to parent, so rotate parent angles first
  //  RmGlob() = 0;
  //-  if(ALIUtils::debug >= 4) ALIUtils::dumprm( parent()->rmGlob(), "OPTO0: RM LOCAL ");
  //  if ( type() != ALIstring("system") ) theRmGlob.transform( parent()->rmGlob() );
  //----- if anglesIsGlobal, RM is already in global coordinates, else multiply by ancestors

  /*  /////////////
  CLHEP::Hep3Vector ztest(0.,0.,1.);
  ztest = theRmGlob * ztest;
  if( ALIUtils::debug >= 5 ) ALIUtils::dump3v( ztest, "z rotated by theRmGlob ");
  */
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::SetCentreGlobFromCentreLocal() {
  //----------- Get global centre: parent centre plus local centre traslated to parent coordinate system
  CLHEP::Hep3Vector cLocal = theCentreGlob;
  theCentreGlob = parent()->rmGlob() * theCentreGlob;

  if (ALIUtils::debug >= 5)
    ALIUtils::dump3v(theCentreGlob, "SetCentreGlobFromCentreLocal: CENTRE in parent local frame ");
  theCentreGlob += parent()->centreGlob();

  if (ALIUtils::debug >= 5)
    ALIUtils::dump3v(theCentreGlob, "SetCentreGlobFromCentreLocal: CENTRE GLOBAL ");
  if (ALIUtils::debug >= 5) {
    ALIUtils::dump3v(parent()->centreGlob(), " parent centreGlob" + parent()->name());
    ALIUtils::dumprm(parent()->rmGlob(), " parent rmGlob ");
  }

  /*  CLHEP::Hep3Vector cLocal2 = theCentreGlob - parent()->centreGlob();
  CLHEP::HepRotation rmParentInv = inverseOf( parent()->rmGlob() );
  cLocal2 = rmParentInv * cLocal2;
  if( (cLocal2 - cLocal).mag() > 1.E-9 ) {
    std::cerr << "!!!! CALCULATE LOCAL WRONG. Diff= " << (cLocal2 - cLocal).mag() << " " << cLocal2 << " " << cLocal << std::endl;
    if( (cLocal2 - cLocal).mag() > 1.E-4 ) {
      std::exception();
    }
    }*/
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::SetRMGlobFromRMLocal() {
  const OpticalObject* opto_par = this;
  GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
  if (gomgr->GlobalOptions()["rotateAroundLocal"] == 0) {
    while (opto_par->parent()->type() != ALIstring("system")) {
      //t    vecite = opto_par->parent()->GetCoordinateEntry( CEanglesX );
      if (ALIUtils::debug >= 5)
        std::cout << "rotate with parent: before X " << opto_par->parent()->name() << " "
                  << parent()->getEntryRMangle(XCoor) << std::endl;
      theRmGlob.rotateX(parent()->getEntryRMangle(XCoor));
      if (ALIUtils::debug >= 5)
        std::cout << "rotate with parent: before Y " << opto_par->parent()->name() << " "
                  << parent()->getEntryRMangle(YCoor) << std::endl;
      theRmGlob.rotateY(parent()->getEntryRMangle(YCoor));
      if (ALIUtils::debug >= 5)
        std::cout << "rotate with parent: before Z " << opto_par->parent()->name() << " "
                  << parent()->getEntryRMangle(ZCoor) << std::endl;
      theRmGlob.rotateZ(parent()->getEntryRMangle(ZCoor));
      if (ALIUtils::debug >= 4)
        ALIUtils::dumprm(theRmGlob, "SetRMGlobFromRMLocal: RM GLOB after " + opto_par->parent()->longName());
      opto_par = opto_par->parent();
    }
  } else {
    if (ALIUtils::debug >= 4) {
      std::cout << " Composing rmGlob with parent " << parent()->name() << std::endl;
      //      ALIUtils::dumprm( theRmGlob, "SetRMGlobFromRMLocal: RM GLOB ");
    }
    theRmGlob = parent()->rmGlob() * theRmGlob;
  }

  //    std::cout << "rotate with parent (parent)" << opto_par->name() <<parent()->name() << (*vecite)->name() << (*vecite)->value() <<std::endl;
  if (ALIUtils::debug >= 4) {
    ALIUtils::dumprm(theRmGlob, "SetRMGlobFromRMLocal: final RM GLOB ");
    ALIUtils::dumprm(parent()->rmGlob(), "parent rm glob ");
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::SetRMGlobFromRMLocalOriginalOriginal(const CLHEP::HepRotation& rmoriori) {
  theRmGlobOriginalOriginal = rmoriori;
  theRmGlobOriginalOriginal = parent()->rmGlobOriginalOriginal() * theRmGlobOriginalOriginal;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ SetOriginalEntryValues: Set orig coordinates and extra entry values for backup
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::setOriginalEntryValues() {
  //---------- Set orig coordinates
  theCentreGlobOriginal = centreGlob();
  theRmGlobOriginal = rmGlob();

  theCentreGlobOriginalOriginal = centreGlob();
  theRmGlobOriginalOriginal = rmGlob();

  /*  if ( ALIUtils::debug >= 5 ) {
    ALIUtils::dump3v( centreGlob(), "OPTO: CENTRE GLOB ");
    ALIUtils::dumprm( rmGlob(), "OPTO: RM GLOB ");
    }*/

  //---------- Set extra entry values
  std::vector<ALIdouble>::const_iterator vdcite;
  for (vdcite = ExtraEntryValueList().begin(); vdcite != ExtraEntryValueList().end(); ++vdcite) {
    addExtraEntryValueOriginalToList(*vdcite);
    addExtraEntryValueOriginalOriginalToList(*vdcite);
  }
  //-  test();
  if (ALIUtils::debug >= 6)
    std::cout << " setOriginalEntryValues " << std::endl;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Propagate the light ray with the behaviour 'behav'
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::participateInMeasurement(LightRay& lightray, Measurement& meas, const ALIstring& behav) {
  //---------- see if light traverses or reflects
  setMeas(&meas);
  if (behav == " ") {
    defaultBehaviour(lightray, meas);
  } else if (behav == "D" || behav == "DD") {
    detailedDeviatesLightRay(lightray);
  } else if (behav == "T" || behav == "DT") {
    detailedTraversesLightRay(lightray);
  } else if (behav == "FD") {
    fastDeviatesLightRay(lightray);
  } else if (behav == "FT") {
    fastTraversesLightRay(lightray);
  } else if (behav == "M") {
    makeMeasurement(lightray, meas);
  } else {
    userDefinedBehaviour(lightray, meas, behav);
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ default behaviour (depends of subclass type). A default behaviour can be makeMeasurement(), therefore you have to pass 'meas'
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::defaultBehaviour(LightRay& lightray, Measurement& meas) {
  std::cerr << "!!! Optical Object " << name() << " of type " << type() << " does not implement a default behaviour"
            << std::endl;
  std::cerr << " You have to specify some behaviour, like :D or :T or ..." << std::endl;
  exit(1);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Fast simulation of deviation of the light ray (reflection, shift, ...)
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::fastDeviatesLightRay(LightRay& lightray) {
  std::cerr << "!!! Optical Object " << name() << " of type " << type() << " does not implement deviation (:D)"
            << std::endl;
  std::cerr << " Please read documentation for this object type" << std::endl;
  exit(1);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Fast simulation of the light ray traversing
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::fastTraversesLightRay(LightRay& lightray) {
  std::cerr << "!!! Optical Object " << name() << " of type " << type()
            << " does not implement the light traversing (:T)" << std::endl;
  std::cerr << " Please read documentation for this object type" << std::endl;
  exit(1);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Detailed simulation of deviation of the light ray (reflection, shift, ...)
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::detailedDeviatesLightRay(LightRay& lightray) {
  std::cerr << "!!! Optical Object " << name() << " of type " << type()
            << " does not implement detailed deviation (:DD / :D)" << std::endl;
  std::cerr << " Please read documentation for this object type" << std::endl;
  exit(1);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Detailed simulation of the light ray traversing
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::detailedTraversesLightRay(LightRay& lightray) {
  std::cerr << "!!! Optical Object " << name() << " of type " << type()
            << " does not implement detailed traversing of light ray (:DT / :T)" << std::endl;
  std::cerr << " Please read documentation for this object type" << std::endl;
  exit(1);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Make the measurement
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::makeMeasurement(LightRay& lightray, Measurement& meas) {
  std::cerr << "!!! Optical Object " << name() << " of type " << type() << " does not implement making measurement (:M)"
            << std::endl;
  std::cerr << " Please read documentation for this object type" << std::endl;
  exit(1);
}

void OpticalObject::userDefinedBehaviour(LightRay& lightray, Measurement& meas, const ALIstring& behav) {
  std::cerr << "!!! Optical Object " << name() << " of type " << type()
            << " does not implement user defined behaviour = " << behav << std::endl;
  std::cerr << " Please read documentation for this object type" << std::endl;
  exit(1);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Get one of the plates of an OptO
//@@
//@@ The point is defined taking the centre of the splitter,
//@@ and traslating it by +/-1/2 'width' in the direction of the splitter Z.
//@@ The normal of this plane is obtained as the splitter Z,
//@@ and then it is rotated with the global rotation matrix.
//@@ If applyWedge it is also rotated around the splitter X and Y axis by +/-1/2 of the wedge.
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ALIPlane OpticalObject::getPlate(const ALIbool forwardPlate, const ALIbool applyWedge) {
  if (ALIUtils::debug >= 4)
    std::cout << "% LR: GET PLATE " << name() << " forward= " << forwardPlate << std::endl;
  //---------- Get OptO variables
  const ALIdouble width = (findExtraEntryValue("width"));

  //---------- Get centre and normal of plate
  //----- Get plate normal before wedge (Z axis of OptO)
  CLHEP::Hep3Vector ZAxis(0., 0., 1.);
  CLHEP::HepRotation rmt = rmGlob();
  CLHEP::Hep3Vector plate_normal = rmt * ZAxis;

  //----- plate centre = OptO centre +/- 1/2 width before wedge
  CLHEP::Hep3Vector plate_point = centreGlob();
  //--- Add to it half of the width following the direction of the plate normal. -1/2 if it is forward plate, +1/2 if it is backward plate
  ALIdouble normal_sign = -forwardPlate * 2 + 1;
  plate_point += normal_sign * width / 2. * plate_normal;
  //-  if (ALIUtils::debug >= 4) std::cout << "width = " << width <<std::endl;
  if (ALIUtils::debug >= 3) {
    ALIUtils::dump3v(plate_point, "plate_point");
    ALIUtils::dump3v(plate_normal, "plate_normal before wedge");
    ALIUtils::dumprm(rmt, "rmt before wedge");
  }

  if (applyWedge) {
    ALIdouble wedge;
    wedge = findExtraEntryValue("wedge");
    if (wedge != 0.) {
      //---------- Rotate plate normal by 1/2 wedge angles
      CLHEP::Hep3Vector XAxis(1., 0., 0.);
      XAxis = rmt * XAxis;
      plate_normal.rotate(normal_sign * wedge / 2., XAxis);
      if (ALIUtils::debug >= 3)
        ALIUtils::dump3v(plate_normal, "plate_normal after wedgeX ");
      if (ALIUtils::debug >= 4)
        ALIUtils::dump3v(XAxis, "X Axis for applying wedge ");
      CLHEP::Hep3Vector YAxis(0., 1., 0.);
      YAxis = rmt * YAxis;
      plate_normal.rotate(normal_sign * wedge / 2., YAxis);
      if (ALIUtils::debug >= 3)
        ALIUtils::dump3v(plate_normal, "plate_normal after wedgeY ");
      if (ALIUtils::debug >= 4)
        ALIUtils::dump3v(YAxis, "Y Axis for applying wedge ");
    }
  }

  //---------- Return plate plane
  return ALIPlane(plate_point, plate_normal);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Displace the centre coordinate 'coor' to get the derivative
//@@ of a measurement w.r.t this entry
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::displaceCentreGlob(const XYZcoor coor, const ALIdouble disp) {
  if (ALIUtils::debug >= 5)
    std::cout << name() << " displaceCentreGlob: coor " << coor << " disp = " << disp << std::endl;

  theCentreGlob = centreGlobOriginal();
  CLHEP::Hep3Vector dispVec = getDispVec(coor, disp);
  theCentreGlob += dispVec;

  //----------- Displace CentreGlob() of every component
  std::vector<OpticalObject*> vopto;
  ALIbool igetood = Model::getComponentOptOs(name(), vopto);
  if (!igetood) {
    //    std::cout << " NO MORE COMPONENTS IN THIS OptO" << name() << std::endl ;
    return;
  }
  std::vector<OpticalObject*>::const_iterator vocite;
  for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
    (*vocite)->displaceCentreGlob(dispVec);
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CLHEP::Hep3Vector OpticalObject::getDisplacementInLocalCoordinates(const XYZcoor coor, const ALIdouble disp) {
  CLHEP::Hep3Vector dispVec;
  switch (coor) {
    case 0:
      dispVec = CLHEP::Hep3Vector(disp, 0., 0.);
      break;
    case 1:
      dispVec = CLHEP::Hep3Vector(0., disp, 0.);
      break;
    case 2:
      dispVec = CLHEP::Hep3Vector(0., 0., disp);
      break;
    default:
      std::cerr << "!!! DISPLACECENTREGLOB coordinate should be 0-2, not " << coor << std::endl;
      exit(2);
  }

  return dispVec;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Displace the centre coordinate 'coor' to get the derivative
//@@ of a measurement w.r.t this entry
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::displaceCentreGlob(const CLHEP::Hep3Vector& dispVec) {
  if (ALIUtils::debug >= 5)
    std::cout << name() << " displaceCentreGlob: dispVec = " << dispVec << std::endl;

  theCentreGlob = centreGlobOriginal();
  theCentreGlob += dispVec;

  //----------- Displace CentreGlob() of every component
  std::vector<OpticalObject*> vopto;
  ALIbool igetood = Model::getComponentOptOs(name(), vopto);
  if (!igetood) {
    return;
  }
  std::vector<OpticalObject*>::const_iterator vocite;
  for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
    (*vocite)->displaceCentreGlob(dispVec);
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ displaceExtraEntry:
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::displaceExtraEntry(const ALIuint entryNo, const ALIdouble disp) {
  ALIdouble Pentry_orig_value = *(theExtraEntryValueOriginalVector.begin() + entryNo);
  ALIdouble Pentry_value = (Pentry_orig_value) + disp;
  LogDebug("OpticalObject::displaceExtraEntry")
      << " displaceExtraEntry " << Pentry_value << " <> " << Pentry_orig_value << std::endl;
  theExtraEntryValueVector[entryNo] = Pentry_value;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::setExtraEntryValue(const ALIuint entryNo, const ALIdouble val) {
  theExtraEntryValueVector[entryNo] = val;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::displaceCentreGlobOriginal(const XYZcoor coor, const ALIdouble disp) {
  if (ALIUtils::debug >= 4)
    std::cout << "@@ OpticalObject::displaceCentreGloboriginal " << name() << " " << coor << " " << disp << std::endl;
  if (ALIUtils::debug >= 5)
    ALIUtils::dump3v(theCentreGlobOriginal, "the centre glob original 0");
  CLHEP::Hep3Vector dispVec = getDispVec(coor, disp);
  theCentreGlobOriginal += dispVec;

  if (ALIUtils::debug >= 5)
    ALIUtils::dump3v(theCentreGlobOriginal, "the centre glob original displaced");

  //----------- Displace CentreGlob() of every component
  std::vector<OpticalObject*> vopto;
  Model::getComponentOptOs(name(), vopto);
  std::vector<OpticalObject*>::const_iterator vocite;
  for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
    (*vocite)->displaceCentreGlobOriginal(dispVec);
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ displaceCentreGlobOriginal:
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::displaceCentreGlobOriginal(const CLHEP::Hep3Vector& dispVec) {
  if (ALIUtils::debug >= 4)
    std::cout << " OpticalObject::displaceCentreGloboriginal " << name() << " dispVec " << dispVec << std::endl;

  theCentreGlobOriginal += dispVec;

  if (ALIUtils::debug >= 5)
    ALIUtils::dump3v(theCentreGlobOriginal, "the centre glob original");

  //----------- Displace CentreGlob() of every component
  std::vector<OpticalObject*> vopto;
  Model::getComponentOptOs(name(), vopto);
  std::vector<OpticalObject*>::const_iterator vocite;
  for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
    (*vocite)->displaceCentreGlobOriginal(dispVec);
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::displaceCentreGlobOriginalOriginal(const XYZcoor coor, const ALIdouble disp) {
  if (ALIUtils::debug >= 4)
    std::cout << "@@ OpticalObject::displaceCentreGloboriginal " << name() << " " << coor << " " << disp << std::endl;
  if (ALIUtils::debug >= 5)
    ALIUtils::dump3v(theCentreGlobOriginalOriginal, "the centre glob originalOriginal 0");
  CLHEP::Hep3Vector dispVec = getDispVec(coor, disp);
  theCentreGlobOriginalOriginal += dispVec;

  if (ALIUtils::debug >= 5)
    ALIUtils::dump3v(theCentreGlobOriginalOriginal, "the centre glob original displaced");

  //----------- Displace CentreGlob() of every component
  std::vector<OpticalObject*> vopto;
  Model::getComponentOptOs(name(), vopto);
  std::vector<OpticalObject*>::const_iterator vocite;
  for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
    (*vocite)->displaceCentreGlobOriginalOriginal(dispVec);
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ displaceCentreGlobOriginal:
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::displaceCentreGlobOriginalOriginal(const CLHEP::Hep3Vector& dispVec) {
  if (ALIUtils::debug >= 4)
    std::cout << " OpticalObject::displaceCentreGloboriginal " << name() << " dispVec " << dispVec << std::endl;

  theCentreGlobOriginalOriginal += dispVec;

  if (ALIUtils::debug >= 5)
    ALIUtils::dump3v(theCentreGlobOriginalOriginal, "the centre glob original");

  //----------- Displace CentreGlob() of every component
  std::vector<OpticalObject*> vopto;
  Model::getComponentOptOs(name(), vopto);
  std::vector<OpticalObject*>::const_iterator vocite;
  for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
    (*vocite)->displaceCentreGlobOriginalOriginal(dispVec);
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Rotate around axis 'coor' to get the derivative of
//@@ a measurement w.r.t this entry
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::displaceRmGlobAroundGlobal(OpticalObject* opto1stRotated,
                                               const XYZcoor coor,
                                               const ALIdouble disp) {
  if (ALIUtils::debug >= 5)
    std::cout << name() << "DISPLACERMGLOBAROUNDGLOBAL" << coor << "disp" << disp << std::endl;
  //-------------------- Rotate rotation matrix
  theRmGlob = rmGlobOriginal();
  theCentreGlob = centreGlobOriginal();
  if (ALIUtils::debug >= 5) {
    std::cout << this->name() << std::endl;
    ALIUtils::dumprm(theRmGlob, "before disp rm ");
  }
  rotateItAroundGlobal(theRmGlob, coor, disp);
  if (ALIUtils::debug >= 5) {
    ALIUtils::dumprm(theRmGlob, "after disp rm ");
  }
  //-------------------- Rotation translate the centre of component OptO
  if (ALIUtils::debug >= 5)
    ALIUtils::dump3v(centreGlob(), " centre_glob before rotation");
  if (ALIUtils::debug >= 5)
    ALIUtils::dump3v(centreGlobOriginal(), "         centreGlobOriginal before rotation");
  if (opto1stRotated != this) {  //own _centre_glob is not displaced
    //---------- Distance to 1st rotated OptO
    CLHEP::Hep3Vector radiusOriginal = centreGlobOriginal() - opto1stRotated->centreGlobOriginal();
    CLHEP::Hep3Vector radius_rotated = radiusOriginal;
    rotateItAroundGlobal(radius_rotated, coor, disp);
    theCentreGlob = centreGlobOriginal() + (radius_rotated - radiusOriginal);
    if (ALIUtils::debug >= 5)
      ALIUtils::dump3v(centreGlob(), " centre_glob after rotation");
    if (ALIUtils::debug >= 5)
      ALIUtils::dump3v(centreGlobOriginal(), "         centre_globOriginal() after rotation");
  }

  //----------- Displace every component
  std::vector<OpticalObject*> vopto;
  Model::getComponentOptOs(name(), vopto);
  std::vector<OpticalObject*>::const_iterator vocite;
  for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
    (*vocite)->displaceRmGlobAroundGlobal(opto1stRotated, coor, disp);
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Rotate around axis 'coor' to get the derivative of
//@@ a measurement w.r.t this entry
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::displaceRmGlobAroundLocal(OpticalObject* opto1stRotated, const XYZcoor coor, const ALIdouble disp) {
  if (anglesIsGlobal) {
    std::cerr << "!!!FATAL ERROR: angles in global coordinates not supported momentarily if 'rotateAroundGlobal' is "
                 "set as a Global Option "
              << std::endl;
    abort();
  }

  if (ALIUtils::debug >= 5)
    std::cout << name() << " DISPLACE_RMGLOB_AROUND_LOCAL " << coor << " disp " << disp << std::endl;
  //---------- Build the rmGlob and centreGlob again, with displacement values
  //----- Local rotation is build with entry values plus displacement
  theRmGlob = CLHEP::HepRotation();
  //---------- Set global rotation matrix
  //-------- Get rm from Entries
  if (coor == XCoor) {
    theRmGlob.rotateX(getEntryRMangle(XCoor) + disp);
    if (ALIUtils::debug >= 5)
      std::cout << " rmglob rotated around x " << getEntryRMangle(XCoor) + disp << std::endl;
  } else {
    theRmGlob.rotateX(getEntryRMangle(XCoor));
  }
  if (ALIUtils::debug >= 4) {
    ALIUtils::dumprm(theRmGlob, "displaceRmGlobAroundLocal: rm local after X ");
  }

  //-  std::cout << name() << " " << coor << " " << XCoor << " getEntryRMangle(coor) )" << getEntryRMangle(coor) << std::endl;
  if (coor == YCoor) {
    theRmGlob.rotateY(getEntryRMangle(YCoor) + disp);
    if (ALIUtils::debug >= 5)
      std::cout << " rmglob rotated around y " << getEntryRMangle(YCoor) + disp << std::endl;
  } else {
    theRmGlob.rotateY(getEntryRMangle(YCoor));
  }
  if (ALIUtils::debug >= 4) {
    std::cout << "  getEntryRMangle(YCoor) " << getEntryRMangle(YCoor) << std::endl;
    ALIUtils::dumprm(theRmGlob, "displaceRmGlobAroundLocal: rm local after Y ");
  }

  if (coor == ZCoor) {
    theRmGlob.rotateZ(getEntryRMangle(ZCoor) + disp);
    if (ALIUtils::debug >= 5)
      std::cout << " rmglob rotated around z " << getEntryRMangle(ZCoor) + disp << std::endl;
  } else {
    theRmGlob.rotateZ(getEntryRMangle(ZCoor));
  }
  if (ALIUtils::debug >= 4) {
    std::cout << "  getEntryRMangle(ZCoor) " << getEntryRMangle(ZCoor) << std::endl;
    ALIUtils::dumprm(theRmGlob, "SetRMLocalFromEntryValues: RM ");
  }

  //-  theCentreGlob = CLHEP::Hep3Vector(0.,0.,0.);
  if (ALIUtils::debug >= 5 && disp != 0) {
    std::cout << this->name() << std::endl;
    ALIUtils::dumprm(theRmGlob, "displaceRmGlobAroundLocal: rm local ");
  }

  if (!anglesIsGlobal) {
    SetRMGlobFromRMLocal();
  }

  //----- calculate local rot axis with new rm glob
  calculateLocalRotationAxisInGlobal();

  //-  theCentreGlob = CLHEP::Hep3Vector(0.,0.,0.);
  if (ALIUtils::debug >= 5 && disp != 0) {
    std::cout << this->name() << std::endl;
    ALIUtils::dumprm(theRmGlob, "displaceRmGlobAroundLocal: rm global ");
  }

  if (opto1stRotated != this) {  //own _centre_glob doesn't rotate
    setGlobalCentre();
    if (ALIUtils::debug >= 5) {
      ALIUtils::dump3v(centreGlob(), " centre_glob after rotation");
      ALIUtils::dump3v(centreGlobOriginal(), "         centre_globOriginal() after rotation");
    }
  }

  //----------- Displace every component
  std::vector<OpticalObject*> vopto;
  Model::getComponentOptOs(name(), vopto);
  std::vector<OpticalObject*>::const_iterator vocite;
  for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
    (*vocite)->displaceRmGlobAroundLocal(opto1stRotated, coor, 0.);
    //for aroundglobal all components are explicitly rotated disp, for aroundLocal, they will be rotated automatically if the parent is rotated, as the rmGlob is built from scratch
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::setGlobalCoordinatesOfComponents() {
  // Calculate the displaced centreGlob and rmGlob of components
  std::vector<OpticalObject*> vopto;
  Model::getComponentOptOs(name(), vopto);
  std::vector<OpticalObject*>::const_iterator vocite;
  for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
    (*vocite)->setGlobalCoordinates();
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::displaceRmGlobOriginal(const OpticalObject* opto1stRotated,
                                           const XYZcoor coor,
                                           const ALIdouble disp) {
  if (ALIUtils::debug >= 9)
    std::cout << name() << " DISPLACEORIGRMGLOB " << coor << " disp " << disp << std::endl;
  GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
  if (gomgr->GlobalOptions()["rotateAroundLocal"] == 0) {
    //-------------------- Rotate rotation matrix
    if (ALIUtils::debug >= 5)
      ALIUtils::dumprm(theRmGlobOriginal, name() + ALIstring(" theRmGlobOriginal before displaced "));
    switch (coor) {
      case 0:
        theRmGlobOriginal.rotateX(disp);
        break;
      case 1:
        theRmGlobOriginal.rotateY(disp);
        break;
      case 2:
        theRmGlobOriginal.rotateZ(disp);
        break;
      default:
        std::cerr << "!!! DISPLACERMGLOB coordinate should be 0-2, not " << coor << std::endl;
        exit(2);
    }

    //-------------------- Rotation translate the centre of component OptO
    if (ALIUtils::debug >= 98)
      ALIUtils::dump3v(centreGlob(), "angles rotate centre_glob");
    if (ALIUtils::debug >= 98)
      ALIUtils::dump3v(centreGlobOriginal(), "         centreGlobOriginal");
    if (opto1stRotated != this) {  //own _centre_glob doesn't rotate
      //---------- Distance to 1st rotated OptO
      CLHEP::Hep3Vector radiusOriginal = centreGlobOriginal() - opto1stRotated->centreGlobOriginal();
      CLHEP::Hep3Vector radius_rotated = radiusOriginal;
      switch (coor) {
        case 0:
          radius_rotated.rotateX(disp);
          break;
        case 1:
          radius_rotated.rotateY(disp);
          break;
        case 2:
          radius_rotated.rotateZ(disp);
          break;
        default:
          break;  // already exited in previous switch
      }
      theCentreGlobOriginal = centreGlobOriginal() + (radius_rotated - radiusOriginal);
      if (ALIUtils::debug >= 98)
        ALIUtils::dump3v(centreGlob(), "angle rotate centre_glob");
      if (ALIUtils::debug >= 98)
        ALIUtils::dump3v(centreGlobOriginal(), "         centre_globOriginal()");
    }

    if (ALIUtils::debug >= 5)
      ALIUtils::dumprm(theRmGlobOriginal, name() + ALIstring(" theRmGlobOriginal displaced "));

    //----------- Displace every OptO component
    std::vector<OpticalObject*> vopto;
    Model::getComponentOptOs(name(), vopto);
    std::vector<OpticalObject*>::const_iterator vocite;
    for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
      (*vocite)->displaceRmGlobOriginal(opto1stRotated, coor, disp);
    }

  } else {
    setGlobalCoordinates();
    theCentreGlobOriginal = theCentreGlob;
    theRmGlobOriginal = theRmGlob;  //!!temporary, theRmGlobOriginal should disappear
    //----------- Displace every OptO component
    std::vector<OpticalObject*> vopto;
    Model::getComponentOptOs(name(), vopto);
    std::vector<OpticalObject*>::const_iterator vocite;
    for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
      (*vocite)->displaceRmGlobOriginal(opto1stRotated, coor, disp);
    }
    if (ALIUtils::debug >= 5) {
      ALIUtils::dump3v(theCentreGlob, " displaceRmGlobOriginal ");
      ALIUtils::dumprm(theRmGlob, " displaceRmGlobOriginal ");
    }
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::displaceRmGlobOriginalOriginal(const OpticalObject* opto1stRotated,
                                                   const XYZcoor coor,
                                                   const ALIdouble disp) {
  if (ALIUtils::debug >= 9)
    std::cout << name() << " DISPLACEORIGRMGLOB " << coor << " disp " << disp << std::endl;
  GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
  if (gomgr->GlobalOptions()["rotateAroundLocal"] == 0) {
    //-------------------- Rotate rotation matrix
    if (ALIUtils::debug >= 5)
      ALIUtils::dumprm(theRmGlobOriginalOriginal, name() + ALIstring(" theRmGlobOriginalOriginal before displaced"));
    switch (coor) {
      case 0:
        theRmGlobOriginalOriginal.rotateX(disp);
        break;
      case 1:
        theRmGlobOriginalOriginal.rotateY(disp);
        break;
      case 2:
        theRmGlobOriginalOriginal.rotateZ(disp);
        break;
      default:
        std::cerr << "!!! DISPLACERMGLOB coordinate should be 0-2, not " << coor << std::endl;
        exit(2);
    }

    //-------------------- Rotation translate the centre of component OptO
    if (ALIUtils::debug >= 98)
      ALIUtils::dump3v(centreGlob(), "angles rotate centre_glob");
    if (ALIUtils::debug >= 98)
      ALIUtils::dump3v(centreGlobOriginalOriginal(), "         centreGlobOriginalOriginal");
    if (opto1stRotated != this) {  //own _centre_glob doesn't rotate
      //---------- Distance to 1st rotated OptO
      CLHEP::Hep3Vector radiusOriginalOriginal =
          centreGlobOriginalOriginal() - opto1stRotated->centreGlobOriginalOriginal();
      CLHEP::Hep3Vector radius_rotated = radiusOriginalOriginal;
      switch (coor) {
        case 0:
          radius_rotated.rotateX(disp);
          break;
        case 1:
          radius_rotated.rotateY(disp);
          break;
        case 2:
          radius_rotated.rotateZ(disp);
          break;
        default:
          break;  // already exited in previous switch
      }
      theCentreGlobOriginalOriginal = centreGlobOriginalOriginal() + (radius_rotated - radiusOriginalOriginal);
      if (ALIUtils::debug >= 98)
        ALIUtils::dump3v(centreGlob(), "angle rotate centre_glob");
      if (ALIUtils::debug >= 98)
        ALIUtils::dump3v(centreGlobOriginalOriginal(), "         centre_globOriginalOriginal()");
    }

    if (ALIUtils::debug >= 5)
      ALIUtils::dumprm(theRmGlobOriginalOriginal, name() + ALIstring(" theRmGlobOriginalOriginal displaced "));

    //----------- Displace every OptO component
    std::vector<OpticalObject*> vopto;
    Model::getComponentOptOs(name(), vopto);
    std::vector<OpticalObject*>::const_iterator vocite;
    for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
      (*vocite)->displaceRmGlobOriginalOriginal(opto1stRotated, coor, disp);
    }

  } else {
    setGlobalCoordinates();
    theCentreGlobOriginalOriginal = theCentreGlob;
    theRmGlobOriginalOriginal = theRmGlob;  //!!temporary, theRmGlobOriginalOriginal should disappear
    //----------- Displace every OptO component
    std::vector<OpticalObject*> vopto;
    Model::getComponentOptOs(name(), vopto);
    std::vector<OpticalObject*>::const_iterator vocite;
    for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
      (*vocite)->displaceRmGlobOriginalOriginal(opto1stRotated, coor, disp);
    }
    if (ALIUtils::debug >= 5) {
      ALIUtils::dump3v(theCentreGlob, " displaceRmGlobOriginalOriginal ");
      ALIUtils::dumprm(theRmGlob, " displaceRmGlobOriginalOriginal ");
    }
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::displaceExtraEntryOriginal(const ALIuint entryNo, const ALIdouble disp) {
  ALIdouble Pentry_orig_value = *(theExtraEntryValueOriginalVector.begin() + entryNo);
  Pentry_orig_value += disp;
  //  std::cout << " displaceExtraEntryOriginal " << *(theExtraEntryValueOriginalVector.begin() + entryNo) << " + " << disp << std::endl;
  theExtraEntryValueOriginalVector[entryNo] = Pentry_orig_value;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::displaceExtraEntryOriginalOriginal(const ALIuint entryNo, const ALIdouble disp) {
  ALIdouble Pentry_orig_value = *(theExtraEntryValueOriginalOriginalVector.begin() + entryNo);
  Pentry_orig_value += disp;
  //  std::cout << " displaceExtraEntryOriginalOriginal " << *(theExtraEntryValueOriginalOriginalVector.begin() + entryNo) << " + " << disp << std::endl;
  theExtraEntryValueOriginalOriginalVector[entryNo] = Pentry_orig_value;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
const ALIint OpticalObject::extraEntryNo(const ALIstring& entry_name) const {
  //-  std::cout << ExtraEntryList().size() << "entry name " << entry_name << std::endl;

  std::vector<Entry*>::const_iterator vecite;
  for (vecite = ExtraEntryList().begin(); vecite != ExtraEntryList().end(); ++vecite) {
    //-    std::cout <<"in entryno" << (*vecite)->name() << entry_name << std::endl;
    if ((*vecite)->name() == entry_name) {
      return (vecite - ExtraEntryList().begin());
    }
    //-    std::cout <<"DD in entryno" << (*vecite)->name() << entry_name << std::endl;
  }
  //-  std::cout << "!!: extra entry name not found: " << entry_name << " in OptO " << name() << std::endl;
  //  exit(2);
  return ALIint(-1);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Find an extra Entry by name and return its value. If entry not found, return 0.
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
const ALIdouble OpticalObject::findExtraEntryValue(const ALIstring& eename) const {
  ALIdouble retval;
  const ALIint entryNo = extraEntryNo(eename);
  if (entryNo >= 0) {
    const ALIdouble Pentry_value = *(theExtraEntryValueVector.begin() + entryNo);
    retval = (Pentry_value);
  } else {
    //    if(ALIUtils::debug >= 0) std::cerr << "!!Warning: entry not found; " << eename << ", in object " << name() << " returns 0. " << std::endl;
    ALIdouble check;
    GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
    gomgr->getGlobalOptionValue("check_extra_entries", check);
    if (check == 1) {
      //    if( check <= 1) {//exit temporarily
      std::cerr << "!!OpticalObject:ERROR: entry not found; " << eename << ", in object " << name() << std::endl;
      exit(1);
    } else {
      //-      std::cerr << "!!temporal WARNING in OpticalObject::findExtraEntryValue: entry not found; " << eename << ", in object " << name() << std::endl;
      retval = 0.;
    }
  }

  if (ALIUtils::debug >= 5)
    std::cout << " OpticalObject::findExtraEntryValue: " << eename << " = " << retval << std::endl;
  return retval;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Find an extra Entry by name and return its value. If entry not found, stop.
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
const ALIdouble OpticalObject::findExtraEntryValueMustExist(const ALIstring& eename) const {
  ALIdouble entry = findExtraEntryValue(eename);
  const ALIint entryNo = extraEntryNo(eename);
  if (entryNo < 0) {
    std::cerr << "!!OpticalObject::findExtraEntryValueMustExist: ERROR: entry not found; " << eename << ", in object "
              << name() << std::endl;
    exit(1);
  }
  //  if(ALIUtils::debug >= 5)  std::cout << " OpticalObject::findExtraEntryValueMustExist: " << eename << " = " << entry << std::endl;
  return entry;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Find an extra Entry by name and pass its value. Return if entry is found or not
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
const ALIbool OpticalObject::findExtraEntryValueIfExists(const ALIstring& eename, ALIdouble& value) const {
  value = findExtraEntryValue(eename);
  const ALIint entryNo = extraEntryNo(eename);
  //-    std::cout << eename << " entryNo " << entryNo << " value " << value << std::endl;
  return (entryNo >= 0);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ resetGlobalCoordinates: Reset Global Coordinates (after derivative is finished)
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::resetGlobalCoordinates() {
  //---------- Reset centre and rm
  theRmGlob = rmGlobOriginal();
  theCentreGlob = centreGlobOriginal();

  //---------- Reset extra entries
  //---------- Set extra entry values list
  std::vector<ALIdouble>::iterator vdite;
  std::vector<ALIdouble>::const_iterator vdcite_o = ExtraEntryValueOriginalList().begin();
  for (vdite = ExtraEntryValueList().begin(); vdite != ExtraEntryValueList().end(); ++vdite, ++vdcite_o) {
    (*vdite) = (*vdcite_o);
  }

  //----------- Reset entries of every component
  std::vector<OpticalObject*> vopto;
  Model::getComponentOptOs(name(), vopto);
  std::vector<OpticalObject*>::const_iterator vocite;
  for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
    (*vocite)->resetGlobalCoordinates();
  }

  calculateLocalRotationAxisInGlobal();
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ resetGlobalCoordinates: Reset Global Coordinates (after derivative is finished)
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::resetOriginalOriginalCoordinates() {
  //  std::cout << " !!! CALLING resetOriginalOriginalCoordinates(). STOP " << std::endl;

  //---------- Reset centre and rm
  theRmGlob = theRmGlobOriginalOriginal;
  theCentreGlob = theCentreGlobOriginalOriginal;
  theRmGlobOriginal = theRmGlobOriginalOriginal;
  theCentreGlobOriginal = theCentreGlobOriginalOriginal;

  //---------- Reset extra entry values list
  std::vector<ALIdouble>::iterator vdite;
  std::vector<ALIdouble>::iterator vdite_o = theExtraEntryValueOriginalVector.begin();
  std::vector<ALIdouble>::const_iterator vdcite_oo = theExtraEntryValueOriginalOriginalVector.begin();
  std::vector<Entry*>::const_iterator vdciteE = ExtraEntryList().begin();
  for (vdite = ExtraEntryValueList().begin(); vdite != ExtraEntryValueList().end();
       ++vdite, ++vdite_o, ++vdcite_oo, ++vdciteE) {
    (*vdite) = (*vdcite_oo);
    (*vdite_o) = (*vdcite_oo);
    (*vdciteE)->addFittedDisplacementToValue(-(*vdciteE)->valueDisplacementByFitting());
    //      std::cout << " resetting extra entry origorig " << (*vdciteE)->name() << " = " << (*vdite) << " ? " << (*vdcite_oo)  << std::endl;
    //      std::cout << " resetting extra entry origorig " << (*vdciteE)->name() << " = " << (*vdite) << " ? " << (*vdciteE)->value()  << std::endl;
    //  std::cout << " check extra entry " << (*vdciteE)->value() << " =? " << (*vdite) << std::endl;
  }

  /*  std::vector< Entry* >::iterator eite;
  for( eite = theCoordinateEntryVector.begin(); eite != theCoordinateEntryVector.end(); eite++ ){
    (*eite)->addFittedDisplacementToValue( - (*eite)->valueDisplacementByFitting() );
  }
  */

  calculateLocalRotationAxisInGlobal();

  //----------- Reset entries of every component
  std::vector<OpticalObject*> vopto;
  Model::getComponentOptOs(name(), vopto);
  std::vector<OpticalObject*>::const_iterator vocite;
  for (vocite = vopto.begin(); vocite != vopto.end(); ++vocite) {
    (*vocite)->resetOriginalOriginalCoordinates();
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@ Destructor
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
OpticalObject::~OpticalObject() {}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@  Return the name of the OptO without its path
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
const ALIstring OpticalObject::shortName() const {
  ALIint last_slash = name().rfind('/');
  ALIstring sname = name().substr(last_slash + 1, name().size() - 1);
  if (last_slash == -1) {  //object of type "system"
    sname = name();
  } else {
    sname = name().substr(last_slash + 1, name().size() - 1);
  }
  return sname;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
std::ostream& operator<<(std::ostream& os, const OpticalObject& c) {
  os << "OPTICALOBJECT: " << c.theName << " of type: " << c.theType << "  " << c.theCentreGlob << c.theRmGlob
     << std::endl;

  return os;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
const CLHEP::HepRotation OpticalObject::rmLocal() const {
  CLHEP::HepRotation rm;
  rm.rotateX(theCoordinateEntryVector[3]->value());
  rm.rotateY(theCoordinateEntryVector[4]->value());
  rm.rotateZ(theCoordinateEntryVector[5]->value());

  return rm;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
std::vector<double> OpticalObject::getLocalRotationAngles(const std::vector<Entry*>& entries) const {
  return getRotationAnglesInOptOFrame(theParent, entries);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
std::vector<double> OpticalObject::getRotationAnglesInOptOFrame(const OpticalObject* optoAncestor,
                                                                const std::vector<Entry*>& entries) const {
  const CLHEP::HepRotation& rmParent = optoAncestor->rmGlob();  //ORIGINAL ?????????????????
  CLHEP::HepRotation rmLocal = rmParent.inverse() * theRmGlob;

  //I was using theRmGlobOriginal, assuming it has been set to theRmGlob already, check it, in case it may have other consequences
  if (theRmGlobOriginal != theRmGlob) {
    std::cerr << " !!!FATAL ERROR: OpticalObject::getRotationAnglesInOptOFrame   theRmGlobOriginal != theRmGlob "
              << std::endl;
    ALIUtils::dumprm(theRmGlobOriginal, " theRmGlobOriginal ");
    ALIUtils::dumprm(theRmGlob, " theRmGlob ");
    exit(1);
  }

  if (ALIUtils::debug >= 5) {
    std::cout << " OpticalObject::getRotationAnglesInOptOFrame " << name() << " optoAncestor " << optoAncestor->name()
              << std::endl;
    ALIUtils::dumprm(rmParent, " rm parent ");
    ALIUtils::dumprm(rmLocal, " rm local ");
    ALIUtils::dumprm(theRmGlobOriginal, " theRmGlobOriginal ");
    ALIUtils::dumprm(theRmGlob, " theRmGlob ");
  }
  return getRotationAnglesFromMatrix(rmLocal, entries);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
std::vector<double> OpticalObject::getRotationAnglesFromMatrix(CLHEP::HepRotation& rmLocal,
                                                               const std::vector<Entry*>& entries) const {
  std::vector<double> newang(3);
  double angleX = entries[3]->value() + entries[3]->valueDisplacementByFitting();
  double angleY = entries[4]->value() + entries[4]->valueDisplacementByFitting();
  double angleZ = entries[5]->value() + entries[5]->valueDisplacementByFitting();
  if (ALIUtils::debug >= 5) {
    std::cout << " angles as value entries: X= " << angleX << " Y= " << angleY << " Z " << angleZ << std::endl;
  }
  return ALIUtils::getRotationAnglesFromMatrix(rmLocal, angleX, angleY, angleZ);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::calculateLocalRotationAxisInGlobal() {
  axisXLocalInGlobal = CLHEP::Hep3Vector(1., 0., 0.);
  axisXLocalInGlobal *= theRmGlob;
  axisYLocalInGlobal = CLHEP::Hep3Vector(0., 1., 0.);
  axisYLocalInGlobal *= theRmGlob;
  axisZLocalInGlobal = CLHEP::Hep3Vector(0., 0., 1.);
  axisZLocalInGlobal *= theRmGlob;
  if (ALIUtils::debug >= 4) {
    std::cout << name() << " axis X local in global " << axisXLocalInGlobal << std::endl;
    std::cout << name() << " axis Y local in global " << axisYLocalInGlobal << std::endl;
    std::cout << name() << " axis Z local in global " << axisZLocalInGlobal << std::endl;
  }
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
template <class T>
void OpticalObject::rotateItAroundGlobal(T& object, const XYZcoor coor, const double disp) {
  switch (coor) {
    case 0:
      object.rotateX(disp);
      break;
    case 1:
      object.rotateY(disp);
      break;
    case 2:
      object.rotateZ(disp);
      break;
  }
  //  CLHEP::Hep3Vector axisToRotate = GetAxisForDisplacement( coor );
  //  object.rotate(disp, axisToRotate);
  if (ALIUtils::debug >= 5)
    std::cout << " rotateItAroundGlobal coor " << coor << " disp " << disp << std::endl;
}

/*
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CLHEP::Hep3Vector OpticalObject::GetAxisForDisplacement( const XYZcoor coor )
{
  CLHEP::Hep3Vector axis;
  if(Model::GlobalOptions()["rotateAroundLocal"] == 0) {
    switch (coor) {
    case 0:
      axis = CLHEP::Hep3Vector( 1., 0., 0. );
      break;
    case 1:
      axis = CLHEP::Hep3Vector( 0., 1., 0. );
      break;
    case 2:
      axis = CLHEP::Hep3Vector( 0., 0., 1. );
      break;
    default:
      break;  // already exited in previous switch
    }
  } else {
    switch (coor) {
    case 0:
      if( ALIUtils::debug >= 5 ) std::cout << coor << "rotate local " << axisXLocalInGlobal << std::endl;
      axis = axisXLocalInGlobal;
      break;
    case 1:
      if( ALIUtils::debug >= 5 ) std::cout << coor << "rotate local " << axisLocalInGlobal << std::endl;
      axis = axisYLocalInGlobal;
      break;
    case 2:
       if( ALIUtils::debug >= 5 ) std::cout << coor << "rotate local " << axisZLocalInGlobal << std::endl;
      axis = axisZLocalInGlobal;
      break;
    default:
      break;  // already exited in previous switch
    }
  }

   return axis;
}
*/

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
double OpticalObject::diff2pi(double ang1, double ang2) {
  double diff = fabs(ang1 - ang2);
  diff = diff - int(diff / 2. / M_PI) * 2 * M_PI;
  return diff;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
bool OpticalObject::eq2ang(double ang1, double ang2) {
  bool beq = true;

  double diff = diff2pi(ang1, ang2);
  if (diff > 0.00001) {
    if (fabs(diff - 2 * M_PI) > 0.00001) {
      //-      std::cout << " diff " << diff << " " << ang1 << " " << ang2 << std::endl;
      beq = false;
    }
  }

  return beq;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
double OpticalObject::approxTo0(double val) {
  double precision = 1.e-9;
  if (fabs(val) < precision)
    val = 0;
  return val;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
double OpticalObject::addPii(double val) {
  if (val < M_PI) {
    val += M_PI;
  } else {
    val -= M_PI;
  }

  return val;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
int OpticalObject::checkMatrixEquations(double angleX, double angleY, double angleZ, CLHEP::HepRotation* rot) {
  //-  std::cout << " cme " << angleX << " " << angleY << " " << angleZ << std::endl;
  if (rot == nullptr) {
    rot = new CLHEP::HepRotation();
    rot->rotateX(angleX);
    rot->rotateY(angleY);
    rot->rotateZ(angleZ);
  }
  double sx = sin(angleX);
  double cx = cos(angleX);
  double sy = sin(angleY);
  double cy = cos(angleY);
  double sz = sin(angleZ);
  double cz = cos(angleZ);

  double rotxx = cy * cz;
  double rotxy = sx * sy * cz - cx * sz;
  double rotxz = cx * sy * cz + sx * sz;
  double rotyx = cy * sz;
  double rotyy = sx * sy * sz + cx * cz;
  double rotyz = cx * sy * sz - sx * cz;
  double rotzx = -sy;
  double rotzy = sx * cy;
  double rotzz = cx * cy;

  int matrixElemBad = 0;
  if (!eq2ang(rot->xx(), rotxx)) {
    std::cerr << " EQUATION for xx() IS BAD " << rot->xx() << " <> " << rotxx << std::endl;
    matrixElemBad++;
  }
  if (!eq2ang(rot->xy(), rotxy)) {
    std::cerr << " EQUATION for xy() IS BAD " << rot->xy() << " <> " << rotxy << std::endl;
    matrixElemBad++;
  }
  if (!eq2ang(rot->xz(), rotxz)) {
    std::cerr << " EQUATION for xz() IS BAD " << rot->xz() << " <> " << rotxz << std::endl;
    matrixElemBad++;
  }
  if (!eq2ang(rot->yx(), rotyx)) {
    std::cerr << " EQUATION for yx() IS BAD " << rot->yx() << " <> " << rotyx << std::endl;
    matrixElemBad++;
  }
  if (!eq2ang(rot->yy(), rotyy)) {
    std::cerr << " EQUATION for yy() IS BAD " << rot->yy() << " <> " << rotyy << std::endl;
    matrixElemBad++;
  }
  if (!eq2ang(rot->yz(), rotyz)) {
    std::cerr << " EQUATION for yz() IS BAD " << rot->yz() << " <> " << rotyz << std::endl;
    matrixElemBad++;
  }
  if (!eq2ang(rot->zx(), rotzx)) {
    std::cerr << " EQUATION for zx() IS BAD " << rot->zx() << " <> " << rotzx << std::endl;
    matrixElemBad++;
  }
  if (!eq2ang(rot->zy(), rotzy)) {
    std::cerr << " EQUATION for zy() IS BAD " << rot->zy() << " <> " << rotzy << std::endl;
    matrixElemBad++;
  }
  if (!eq2ang(rot->zz(), rotzz)) {
    std::cerr << " EQUATION for zz() IS BAD " << rot->zz() << " <> " << rotzz << std::endl;
    matrixElemBad++;
  }

  //-  std::cout << " cme: matrixElemBad " << matrixElemBad << std::endl;
  return matrixElemBad;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CLHEP::Hep3Vector OpticalObject::getDispVec(const XYZcoor coor, const ALIdouble disp) {
  CLHEP::Hep3Vector dispVec;
  switch (coor) {
    case 0:
      dispVec = CLHEP::Hep3Vector(disp, 0., 0.);
      break;
    case 1:
      dispVec = CLHEP::Hep3Vector(0., disp, 0.);
      break;
    case 2:
      dispVec = CLHEP::Hep3Vector(0., 0., disp);
      break;
    default:
      break;  // already exited in previous switch
  }
  //-  CLHEP::Hep3Vector dispVec = getDisplacementInLocalCoordinates( coor, disp);
  if (ALIUtils::debug >= 5) {
    ALIUtils::dump3v(dispVec, " dispVec in local ");
    CLHEP::HepRotation rmt = parent()->rmGlob();
    ALIUtils::dumprm(rmt, "parent rmGlob ");
  }
  dispVec = parent()->rmGlob() * dispVec;
  if (ALIUtils::debug >= 5)
    ALIUtils::dump3v(dispVec, " dispVec in global ");

  return dispVec;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
const CLHEP::Hep3Vector OpticalObject::centreLocal() const {
  CLHEP::Hep3Vector cLocal = theCentreGlob - parent()->centreGlob();
  CLHEP::HepRotation rmParentInv = inverseOf(parent()->rmGlob());
  cLocal = rmParentInv * cLocal;

  return cLocal;
  /*-

  if( theCoordinateEntryVector.size() >= 3 ) {
    return CLHEP::Hep3Vector( theCoordinateEntryVector[0]->value(), theCoordinateEntryVector[1]->value(), theCoordinateEntryVector[2]->value() );
  } else {
    return CLHEP::Hep3Vector(0.,0.,0.);
  }
  */
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
const double OpticalObject::getEntryCentre(const XYZcoor coor) const {
  Entry* ce = theCoordinateEntryVector[coor];
  //  std::cout << coor << " getEntryCentre " << ce->value() << " + " << ce->valueDisplacementByFitting() << std::endl;
  return ce->value() + ce->valueDisplacementByFitting();
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
const double OpticalObject::getEntryCentre(const ALIstring& coorstr) const {
  XYZcoor coor = XCoor;
  if (coorstr == "X") {
    coor = XCoor;
  } else if (coorstr == "Y") {
    coor = YCoor;
  } else if (coorstr == "Z") {
    coor = ZCoor;
  }
  Entry* ce = theCoordinateEntryVector[coor];
  //  std::cout << coor << " getEntryCentre " << ce->value() << " + " << ce->valueDisplacementByFitting() << std::endl;
  return ce->value() + ce->valueDisplacementByFitting();
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
const double OpticalObject::getEntryRMangle(const XYZcoor coor) const {
  Entry* ce = theCoordinateEntryVector[coor + 3];
  //  std::cout << coor << " getEntryRMangle " << ce->value() << " + " << ce->valueDisplacementByFitting() << " size = " << theCoordinateEntryVector.size() << " ce = " << ce << " entry name " << ce->name() << " opto name " << name() << std::endl;

  return ce->value() + ce->valueDisplacementByFitting();
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
const double OpticalObject::getEntryRMangle(const ALIstring& coorstr) const {
  XYZcoor coor = XCoor;
  if (coorstr == "X") {
    coor = XCoor;
  } else if (coorstr == "Y") {
    coor = YCoor;
  } else if (coorstr == "Z") {
    coor = ZCoor;
  }
  Entry* ce = theCoordinateEntryVector[coor + 3];
  //  std::cout << coor << " getEntryRMangle " << ce->value() << " + " << ce->valueDisplacementByFitting() << " size = " << theCoordinateEntryVector.size() << " ce = " << ce << " entry name " << ce->name() << " opto name " << name() << std::endl;

  return ce->value() + ce->valueDisplacementByFitting();
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::constructMaterial() {
  theMaterial = new CocoaMaterialElementary("Hydrogen", 70.8 * CLHEP::mg / CLHEP::cm3, "H", 1.00794f, 1);
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::constructSolidShape() {
  ALIdouble go;
  GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
  gomgr->getGlobalOptionValue("VisScale", go);

  theSolidShape = new CocoaSolidShapeBox("Box",
                                         go * 5. * CLHEP::cm / CLHEP::m,
                                         go * 5. * CLHEP::cm / CLHEP::m,
                                         go * 5. * CLHEP::cm / CLHEP::m);  //COCOA internal units are meters
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::constructFromOptAligInfo(const OpticalAlignInfo& oaInfo) {
  if (theParent != nullptr) {  //----- OptO 'system' has no parent (and no affine frame)
    //---------- Build Data
    //---------- See if there are extra entries and read them
    std::vector<OpticalAlignParam> exEnt = oaInfo.extraEntries_;
    std::vector<OpticalAlignParam>::iterator ite;
    std::vector<ALIstring> wordlist;
    for (ite = exEnt.begin(); ite != exEnt.end(); ++ite) {
      wordlist = getCoordinateFromOptAlignParam(*ite);
      wordlist.insert(wordlist.begin(), (*ite).dimType());
      fillExtraEntry(wordlist);
    }

    //--------- set centre and angles not global (default behaviour)
    centreIsGlobal = false;
    anglesIsGlobal = false;

    setCmsswID(oaInfo.ID_);
    //--------- build Coordinates
    fillCoordinateEntry("centre", getCoordinateFromOptAlignParam(oaInfo.x_));
    fillCoordinateEntry("centre", getCoordinateFromOptAlignParam(oaInfo.y_));
    fillCoordinateEntry("centre", getCoordinateFromOptAlignParam(oaInfo.z_));
    fillCoordinateEntry("angles", getCoordinateFromOptAlignParam(oaInfo.angx_));
    fillCoordinateEntry("angles", getCoordinateFromOptAlignParam(oaInfo.angy_));
    fillCoordinateEntry("angles", getCoordinateFromOptAlignParam(oaInfo.angz_));

    //---------- Set global coordinates
    setGlobalCoordinates();

    //---------- Set original entry values
    setOriginalEntryValues();
  }

  //---------- Construct material
  constructMaterial();

  //---------- Construct solid shape
  constructSolidShape();

  if (ALIUtils::debug >= 5) {
    std::cout << "constructFromOptAligInfo constructed: " << *this << std::endl;
  }

  //---------- Create the OptO that compose this one
  createComponentOptOsFromOptAlignInfo();
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
std::vector<ALIstring> OpticalObject::getCoordinateFromOptAlignParam(const OpticalAlignParam& oaParam) {
  char chartmp[20];
  std::vector<ALIstring> wordlist;
  wordlist.push_back(oaParam.name());
  gcvt(oaParam.value(), 10, chartmp);
  wordlist.push_back(chartmp);
  gcvt(oaParam.sigma(), 10, chartmp);
  wordlist.push_back(chartmp);
  if (oaParam.quality() == 0) {
    wordlist.push_back("fix");
  } else if (oaParam.quality() == 1) {
    wordlist.push_back("cal");
  } else if (oaParam.quality() == 2) {
    wordlist.push_back("unk");
  }

  if (ALIUtils::debug >= 5) {
    ALIUtils::dumpVS(wordlist, " getCoordinateFromOptAlignParam " + oaParam.name());
  }

  return wordlist;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void OpticalObject::createComponentOptOsFromOptAlignInfo() {
  //----- Build children list of this object
  std::vector<OpticalAlignInfo> children;

  std::vector<OpticalAlignInfo>::const_iterator ite;
  if (ALIUtils::debug >= 5) {
    std::cout << " Model::getOpticalAlignments().size " << Model::getOpticalAlignments().size() << std::endl;
  }
  //  for( ite = Model::getOpticalAlignments().begin(); ite != Model::getOpticalAlignments().end(); ite++ ){
  int siz = Model::getOpticalAlignments().size();
  for (int ii = 0; ii < siz; ii++) {
    //    std::cout << " OpticalObject::getComponentOptOsFromOptAlignInfo name " <<  (*ite).name_ << std::endl;
    //   std::cout << " OpticalObject::getComponentOptOsFromOptAlignInfo " <<  (*ite).parentName_ << " =? " << theName << std::endl;
    //    std::cout <<  " OpticalObject::getComponentOptOsFromOptAlignInfo name " <<  ii << std::endl;
    //    if( (*ite)parentName_. == oaInfo.name() && (*ite).name() != "simple2DWithMirror:mirror1" ) {
    if (Model::getOpticalAlignments()[ii].parentName_ == theName) {
      //    if( (*ite).parentName_ == theName ) {

      //      std::cout << "createComponentOptOsFromOptAlignInfo: 1 to push_back " << std::endl;
      std::vector<OpticalAlignParam> exent = Model::getOpticalAlignments()[ii].extraEntries_;
      //    std::vector<OpticalAlignParam> exent = (*ite).extraEntries_;
      //-      std::cout << "createComponentOptOsFromOptAlignInfo: 2 to push_back " << std::endl;
      /*      for( ALIuint ij = 0; ij < exent.size(); ij++ ){
        std::cout << " extra entry " << exent[ij].name_;
        std::cout << " extra entry " << exent[ij].dimType();
        std::cout << " extra entry " << exent[ij].value_;
        std::cout << " extra entry " << exent[ij].error_;
        std::cout << " extra entry " << exent[ij].quality_;
        } */
      //      std::cout << "createComponentOptOsFromOptAlignInfo: 3 to push_back " << Model::getOpticalAlignments()[ii] << std::endl;
      OpticalAlignInfo oaInfochild = Model::getOpticalAlignments()[ii];
      //    OpticalAlignInfo oaInfochild =  *ite;
      //      std::cout << "createComponentOptOsFromOptAlignInfo: 4 to push_back " << std::endl;
      children.push_back(oaInfochild);
      if (ALIUtils::debug >= 5) {
        std::cout << theName << "createComponentOptOsFromOptAlignInfo: children added " << oaInfochild.name_
                  << std::endl;
      }
    }
    //    std::cout << "createComponentOptOsFromOptAlignInfo: 6 push_backed " << std::endl;
  }
  //  std::cout << "createComponentOptOsFromOptAlignInfo: 10 push_backed " << std::endl;

  if (ALIUtils::debug >= 5) {
    std::cout << "OpticalObject::createComponentsFromAlignInfo: N components = " << children.size() << std::endl;
  }
  for (ite = children.begin(); ite != children.end(); ++ite) {
    //---------- Get component type
    ALIstring optoType = (*ite).type_;
    //-    //---------- Get composite component name
    //-  ALIstring optoName = name()+"/"+(*ite).name_;
    //---------- Get component name
    ALIstring optoName = (*ite).name_;
    ALIbool fcopyComponents = false;

    //---------- Create OpticalObject of the corresponding type
    OpticalObject* OptOcomponent = createNewOptO(this, optoType, optoName, fcopyComponents);

    //---------- Construct it (read data and
    OptOcomponent->constructFromOptAligInfo(*ite);

    //---------- Fill OptO tree and OptO list
    Model::OptOList().push_back(OptOcomponent);
  }
}