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
/** \file LaserAlignment.cc
 *  LAS reconstruction module
 *
 *  $Date: 2013/01/07 20:26:37 $
 *  $Revision: 1.47 $
 *  \author Maarten Thomas
 *  \author Jan Olzem
 */

#include "Alignment/LaserAlignment/plugins/LaserAlignment.h"
#include "FWCore/Framework/interface/Run.h"
#include "Geometry/Records/interface/TrackerTopologyRcd.h"
#include "CondFormats/GeometryObjects/interface/PTrackerParameters.h"
#include "CondFormats/GeometryObjects/interface/PTrackerAdditionalParametersPerDet.h"
#include "Geometry/Records/interface/PTrackerParametersRcd.h"
#include "Geometry/Records/interface/PTrackerAdditionalParametersPerDetRcd.h"

///
///
///
LaserAlignment::LaserAlignment(edm::ParameterSet const& theConf)
    : topoToken_(esConsumes()),
      geomToken_(esConsumes()),
      geomDetToken_(esConsumes()),
      ptpToken_(esConsumes()),
      ptitpToken_(esConsumes()),
      gprToken_(esConsumes()),
      stripPedestalsToken_(esConsumes()),
      theEvents(0),
      theDoPedestalSubtraction(theConf.getUntrackedParameter<bool>("SubtractPedestals", true)),
      theUseMinuitAlgorithm(theConf.getUntrackedParameter<bool>("RunMinuitAlignmentTubeAlgorithm", false)),
      theApplyBeamKinkCorrections(theConf.getUntrackedParameter<bool>("ApplyBeamKinkCorrections", true)),
      peakFinderThreshold(theConf.getUntrackedParameter<double>("PeakFinderThreshold", 10.)),
      enableJudgeZeroFilter(theConf.getUntrackedParameter<bool>("EnableJudgeZeroFilter", true)),
      judgeOverdriveThreshold(theConf.getUntrackedParameter<unsigned int>("JudgeOverdriveThreshold", 220)),
      updateFromInputGeometry(theConf.getUntrackedParameter<bool>("UpdateFromInputGeometry", false)),
      misalignedByRefGeometry(theConf.getUntrackedParameter<bool>("MisalignedByRefGeometry", false)),
      theStoreToDB(theConf.getUntrackedParameter<bool>("SaveToDbase", false)),
      theDigiProducersList(theConf.getParameter<std::vector<edm::ParameterSet> >("DigiProducersList")),
      theSaveHistograms(theConf.getUntrackedParameter<bool>("SaveHistograms", false)),
      theCompression(theConf.getUntrackedParameter<int>("ROOTFileCompression", 1)),
      theFileName(theConf.getUntrackedParameter<std::string>("ROOTFileName", "test.root")),
      theMaskTecModules(theConf.getUntrackedParameter<std::vector<unsigned int> >("MaskTECModules")),
      theMaskAtModules(theConf.getUntrackedParameter<std::vector<unsigned int> >("MaskATModules")),
      theSetNominalStrips(theConf.getUntrackedParameter<bool>("ForceFitterToNominalStrips", false)),
      theLasConstants(theConf.getUntrackedParameter<std::vector<edm::ParameterSet> >("LaserAlignmentConstants")),
      theFile(),
      theAlignableTracker(),
      theAlignRecordName("TrackerAlignmentRcd"),
      theErrorRecordName("TrackerAlignmentErrorExtendedRcd"),
      firstEvent_(true) {
  std::cout << std::endl;
  std::cout << "=============================================================="
            << "\n===         LaserAlignment module configuration            ==="
            << "\n"
            << "\n    Write histograms to file       = " << (theSaveHistograms ? "true" : "false")
            << "\n    Histogram file name            = " << theFileName
            << "\n    Histogram file compression     = " << theCompression
            << "\n    Subtract pedestals             = " << (theDoPedestalSubtraction ? "true" : "false")
            << "\n    Run Minuit AT algorithm        = " << (theUseMinuitAlgorithm ? "true" : "false")
            << "\n    Apply beam kink corrections    = " << (theApplyBeamKinkCorrections ? "true" : "false")
            << "\n    Peak Finder Threshold          = " << peakFinderThreshold
            << "\n    EnableJudgeZeroFilter          = " << (enableJudgeZeroFilter ? "true" : "false")
            << "\n    JudgeOverdriveThreshold        = " << judgeOverdriveThreshold
            << "\n    Update from input geometry     = " << (updateFromInputGeometry ? "true" : "false")
            << "\n    Misalignment from ref geometry = " << (misalignedByRefGeometry ? "true" : "false")
            << "\n    Number of TEC modules masked   = " << theMaskTecModules.size() << " (s. below list if > 0)"
            << "\n    Number of AT modules masked    = " << theMaskAtModules.size() << " (s. below list if > 0)"
            << "\n    Store to database              = " << (theStoreToDB ? "true" : "false")
            << "\n    ----------------------------------------------- ----------"
            << (theSetNominalStrips ? "\n    Set strips to nominal       =  true" : "\n")
            << "\n=============================================================" << std::endl;

  // tell about masked modules
  if (!theMaskTecModules.empty()) {
    std::cout << " ===============================================================================================\n"
              << std::flush;
    std::cout << " The following " << theMaskTecModules.size()
              << " TEC modules have been masked out and will not be considered by the TEC algorithm:\n " << std::flush;
    for (std::vector<unsigned int>::iterator moduleIt = theMaskTecModules.begin(); moduleIt != theMaskTecModules.end();
         ++moduleIt) {
      std::cout << *moduleIt << (moduleIt != --theMaskTecModules.end() ? ", " : "") << std::flush;
    }
    std::cout << std::endl << std::flush;
    std::cout << " ===============================================================================================\n\n"
              << std::flush;
  }
  if (!theMaskAtModules.empty()) {
    std::cout << " ===============================================================================================\n"
              << std::flush;
    std::cout << " The following " << theMaskAtModules.size()
              << " AT modules have been masked out and will not be considered by the AT algorithm:\n " << std::flush;
    for (std::vector<unsigned int>::iterator moduleIt = theMaskAtModules.begin(); moduleIt != theMaskAtModules.end();
         ++moduleIt) {
      std::cout << *moduleIt << (moduleIt != --theMaskAtModules.end() ? ", " : "") << std::flush;
    }
    std::cout << std::endl << std::flush;
    std::cout << " ===============================================================================================\n\n"
              << std::flush;
  }

  // alias for the Branches in the root files
  std::string alias(theConf.getParameter<std::string>("@module_label"));

  // declare the product to produce
  produces<TkLasBeamCollection, edm::Transition::EndRun>("tkLaserBeams").setBranchAlias(alias + "TkLasBeamCollection");

  // switch judge's zero filter depending on cfg
  judge.EnableZeroFilter(enableJudgeZeroFilter);

  // set the upper threshold for zero suppressed data
  judge.SetOverdriveThreshold(judgeOverdriveThreshold);
}

///
///
///
LaserAlignment::~LaserAlignment() {
  if (theSaveHistograms)
    theFile->Write();
  if (theFile) {
    delete theFile;
  }
  if (theAlignableTracker) {
    delete theAlignableTracker;
  }
}

///
///
///
void LaserAlignment::beginJob() {
  // write sumed histograms to file (if selected in cfg)
  if (theSaveHistograms) {
    // creating a new file
    theFile = new TFile(theFileName.c_str(), "RECREATE", "CMS ROOT file");

    // initialize the histograms
    if (theFile) {
      theFile->SetCompressionLevel(theCompression);
      singleModulesDir = theFile->mkdir("single modules");
    } else
      throw cms::Exception(" [LaserAlignment::beginJob]")
          << " ** ERROR: could not open file:" << theFileName.c_str() << " for writing." << std::endl;
  }

  // detector id maps (hard coded)
  fillDetectorId();

  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  //   PROFILE, HISTOGRAM & FITFUNCTION INITIALIZATION
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  // object used to build various strings for names and labels
  std::stringstream nameBuilder;

  // loop variables for use with LASGlobalLoop object
  int det, ring, beam, disk, pos;

  // loop TEC modules
  det = 0;
  ring = 0;
  beam = 0;
  disk = 0;
  do {  // loop using LASGlobalLoop functionality
    // init the profiles
    pedestalProfiles.GetTECEntry(det, ring, beam, disk).SetAllValuesTo(0.);
    currentDataProfiles.GetTECEntry(det, ring, beam, disk).SetAllValuesTo(0.);
    collectedDataProfiles.GetTECEntry(det, ring, beam, disk).SetAllValuesTo(0.);

    // init the hit maps
    isAcceptedProfile.SetTECEntry(det, ring, beam, disk, 0);
    numberOfAcceptedProfiles.SetTECEntry(det, ring, beam, disk, 0);

    // create strings for histo names
    nameBuilder.clear();
    nameBuilder.str("");
    nameBuilder << "TEC";
    if (det == 0)
      nameBuilder << "+";
    else
      nameBuilder << "-";
    nameBuilder << "_Ring";
    if (ring == 0)
      nameBuilder << "4";
    else
      nameBuilder << "6";
    nameBuilder << "_Beam" << beam;
    nameBuilder << "_Disk" << disk;
    theProfileNames.SetTECEntry(det, ring, beam, disk, nameBuilder.str());

    // init the histograms
    if (theSaveHistograms) {
      nameBuilder << "_Histo";
      summedHistograms.SetTECEntry(
          det, ring, beam, disk, new TH1D(nameBuilder.str().c_str(), nameBuilder.str().c_str(), 512, 0, 512));
      summedHistograms.GetTECEntry(det, ring, beam, disk)->SetDirectory(singleModulesDir);
    }

  } while (moduleLoop.TECLoop(det, ring, beam, disk));

  // TIB & TOB section
  det = 2;
  beam = 0;
  pos = 0;
  do {  // loop using LASGlobalLoop functionality
    // init the profiles
    pedestalProfiles.GetTIBTOBEntry(det, beam, pos).SetAllValuesTo(0.);
    currentDataProfiles.GetTIBTOBEntry(det, beam, pos).SetAllValuesTo(0.);
    collectedDataProfiles.GetTIBTOBEntry(det, beam, pos).SetAllValuesTo(0.);

    // init the hit maps
    isAcceptedProfile.SetTIBTOBEntry(det, beam, pos, 0);
    numberOfAcceptedProfiles.SetTIBTOBEntry(det, beam, pos, 0);

    // create strings for histo names
    nameBuilder.clear();
    nameBuilder.str("");
    if (det == 2)
      nameBuilder << "TIB";
    else
      nameBuilder << "TOB";
    nameBuilder << "_Beam" << beam;
    nameBuilder << "_Zpos" << pos;

    theProfileNames.SetTIBTOBEntry(det, beam, pos, nameBuilder.str());

    // init the histograms
    if (theSaveHistograms) {
      nameBuilder << "_Histo";
      summedHistograms.SetTIBTOBEntry(
          det, beam, pos, new TH1D(nameBuilder.str().c_str(), nameBuilder.str().c_str(), 512, 0, 512));
      summedHistograms.GetTIBTOBEntry(det, beam, pos)->SetDirectory(singleModulesDir);
    }

  } while (moduleLoop.TIBTOBLoop(det, beam, pos));

  // TEC2TEC AT section
  det = 0;
  beam = 0;
  disk = 0;
  do {  // loop using LASGlobalLoop functionality
    // init the profiles
    pedestalProfiles.GetTEC2TECEntry(det, beam, disk).SetAllValuesTo(0.);
    currentDataProfiles.GetTEC2TECEntry(det, beam, disk).SetAllValuesTo(0.);
    collectedDataProfiles.GetTEC2TECEntry(det, beam, disk).SetAllValuesTo(0.);

    // init the hit maps
    isAcceptedProfile.SetTEC2TECEntry(det, beam, disk, 0);
    numberOfAcceptedProfiles.SetTEC2TECEntry(det, beam, disk, 0);

    // create strings for histo names
    nameBuilder.clear();
    nameBuilder.str("");
    nameBuilder << "TEC(AT)";
    if (det == 0)
      nameBuilder << "+";
    else
      nameBuilder << "-";
    nameBuilder << "_Beam" << beam;
    nameBuilder << "_Disk" << disk;
    theProfileNames.SetTEC2TECEntry(det, beam, disk, nameBuilder.str());

    // init the histograms
    if (theSaveHistograms) {
      nameBuilder << "_Histo";
      summedHistograms.SetTEC2TECEntry(
          det, beam, disk, new TH1D(nameBuilder.str().c_str(), nameBuilder.str().c_str(), 512, 0, 512));
      summedHistograms.GetTEC2TECEntry(det, beam, disk)->SetDirectory(singleModulesDir);
    }

  } while (moduleLoop.TEC2TECLoop(det, beam, disk));

  firstEvent_ = true;
}

///
///
///
void LaserAlignment::produce(edm::Event& theEvent, edm::EventSetup const& theSetup) {
  if (firstEvent_) {
    //Retrieve tracker topology from geometry
    const TrackerTopology* const tTopo = &theSetup.getData(topoToken_);

    // access the tracker
    gD = theSetup.getHandle(geomDetToken_);
    theTrackerGeometry = theSetup.getHandle(geomToken_);

    // access pedestals (from db..) if desired
    edm::ESHandle<SiStripPedestals> pedestalsHandle;
    if (theDoPedestalSubtraction) {
      pedestalsHandle = theSetup.getHandle(stripPedestalsToken_);
      fillPedestalProfiles(pedestalsHandle);
    }

    // global positions
    theGlobalPositionRcd = &theSetup.getData(gprToken_);

    // select the reference geometry
    if (!updateFromInputGeometry) {
      // the AlignableTracker object is initialized with the ideal geometry
      const GeometricDet* theGeometricDet = &theSetup.getData(geomDetToken_);
      const PTrackerParameters* ptp = &theSetup.getData(ptpToken_);
      const PTrackerAdditionalParametersPerDet* ptitp = &theSetup.getData(ptitpToken_);

      TrackerGeomBuilderFromGeometricDet trackerBuilder;
      TrackerGeometry* theRefTracker = trackerBuilder.build(&*theGeometricDet, &*ptitp, *ptp, tTopo);

      theAlignableTracker = new AlignableTracker(&(*theRefTracker), tTopo);
    } else {
      // the AlignableTracker object is initialized with the input geometry from DB
      theAlignableTracker = new AlignableTracker(&(*theTrackerGeometry), tTopo);
    }

    firstEvent_ = false;
  }

  LogDebug("LaserAlignment") << "==========================================================="
                             << "\n   Private analysis of event #" << theEvent.id().event() << " in run #"
                             << theEvent.id().run();

  // do the Tracker Statistics to retrieve the current profiles
  fillDataProfiles(theEvent, theSetup);

  // index variables for the LASGlobalLoop object
  int det, ring, beam, disk, pos;

  //
  // first pre-loop on selected entries to find out
  // whether the TEC or the AT beams have fired
  // (pedestal profiles are left empty if false in cfg)
  //

  // TEC+- (only ring 6)
  ring = 1;
  for (det = 0; det < 2; ++det) {
    for (beam = 0; beam < 8; ++beam) {
      for (disk = 0; disk < 9; ++disk) {
        if (judge.IsSignalIn(currentDataProfiles.GetTECEntry(det, ring, beam, disk) -
                                 pedestalProfiles.GetTECEntry(det, ring, beam, disk),
                             0)) {
          isAcceptedProfile.SetTECEntry(det, ring, beam, disk, 1);
        } else {  // assume no initialization
          isAcceptedProfile.SetTECEntry(det, ring, beam, disk, 0);
        }
      }
    }
  }

  // TIBTOB
  det = 2;
  beam = 0;
  pos = 0;
  do {
    // add current event's data and subtract pedestals
    if (judge.IsSignalIn(
            currentDataProfiles.GetTIBTOBEntry(det, beam, pos) - pedestalProfiles.GetTIBTOBEntry(det, beam, pos),
            getTIBTOBNominalBeamOffset(det, beam, pos))) {
      isAcceptedProfile.SetTIBTOBEntry(det, beam, pos, 1);
    } else {  // dto.
      isAcceptedProfile.SetTIBTOBEntry(det, beam, pos, 0);
    }

  } while (moduleLoop.TIBTOBLoop(det, beam, pos));

  // now come the beam finders
  bool isTECMode = isTECBeam();
  //  LogDebug( " [LaserAlignment::produce]" ) << "LaserAlignment::isTECBeam declares this event " << ( isTECMode ? "" : "NOT " ) << "a TEC event." << std::endl;
  std::cout << " [LaserAlignment::produce] -- LaserAlignment::isTECBeam declares this event "
            << (isTECMode ? "" : "NOT ") << "a TEC event." << std::endl;

  bool isATMode = isATBeam();
  //  LogDebug( " [LaserAlignment::produce]" ) << "LaserAlignment::isATBeam declares this event "  << ( isATMode ? "" : "NOT " )  << "an AT event." << std::endl;
  std::cout << " [LaserAlignment::produce] -- LaserAlignment::isATBeam declares this event " << (isATMode ? "" : "NOT ")
            << "an AT event." << std::endl;

  //
  // now pass the pedestal subtracted profiles to the judge
  // if they're accepted, add them on the collectedDataProfiles
  // (pedestal profiles are left empty if false in cfg)
  //

  // loop TEC+- modules
  det = 0;
  ring = 0;
  beam = 0;
  disk = 0;
  do {
    LogDebug("[LaserAlignment::produce]")
        << "Profile is: " << theProfileNames.GetTECEntry(det, ring, beam, disk) << "." << std::endl;

    // this now depends on the AT/TEC mode, is this a doubly hit module? -> look for it in vector<int> tecDoubleHitDetId
    // (ring == 0 not necessary but makes it a little faster)
    if (ring == 0 &&
        find(tecDoubleHitDetId.begin(), tecDoubleHitDetId.end(), detectorId.GetTECEntry(det, ring, beam, disk)) !=
            tecDoubleHitDetId.end()) {
      if (isTECMode) {  // add profile to TEC collection
        // add current event's data and subtract pedestals
        if (judge.JudgeProfile(currentDataProfiles.GetTECEntry(det, ring, beam, disk) -
                                   pedestalProfiles.GetTECEntry(det, ring, beam, disk),
                               0)) {
          collectedDataProfiles.GetTECEntry(det, ring, beam, disk) +=
              currentDataProfiles.GetTECEntry(det, ring, beam, disk) -
              pedestalProfiles.GetTECEntry(det, ring, beam, disk);
          numberOfAcceptedProfiles.GetTECEntry(det, ring, beam, disk)++;
        }
      }
    }

    else {  // not a doubly hit module, don't care about the mode
      // add current event's data and subtract pedestals
      if (judge.JudgeProfile(currentDataProfiles.GetTECEntry(det, ring, beam, disk) -
                                 pedestalProfiles.GetTECEntry(det, ring, beam, disk),
                             0)) {
        collectedDataProfiles.GetTECEntry(det, ring, beam, disk) +=
            currentDataProfiles.GetTECEntry(det, ring, beam, disk) -
            pedestalProfiles.GetTECEntry(det, ring, beam, disk);
        numberOfAcceptedProfiles.GetTECEntry(det, ring, beam, disk)++;
      }
    }

  } while (moduleLoop.TECLoop(det, ring, beam, disk));

  // loop TIB/TOB modules
  det = 2;
  beam = 0;
  pos = 0;
  do {
    LogDebug("[LaserAlignment::produce]")
        << "Profile is: " << theProfileNames.GetTIBTOBEntry(det, beam, pos) << "." << std::endl;

    // add current event's data and subtract pedestals
    if (judge.JudgeProfile(
            currentDataProfiles.GetTIBTOBEntry(det, beam, pos) - pedestalProfiles.GetTIBTOBEntry(det, beam, pos),
            getTIBTOBNominalBeamOffset(det, beam, pos))) {
      collectedDataProfiles.GetTIBTOBEntry(det, beam, pos) +=
          currentDataProfiles.GetTIBTOBEntry(det, beam, pos) - pedestalProfiles.GetTIBTOBEntry(det, beam, pos);
      numberOfAcceptedProfiles.GetTIBTOBEntry(det, beam, pos)++;
    }

  } while (moduleLoop.TIBTOBLoop(det, beam, pos));

  // loop TEC2TEC modules
  det = 0;
  beam = 0;
  disk = 0;
  do {
    LogDebug("[LaserAlignment::produce]")
        << "Profile is: " << theProfileNames.GetTEC2TECEntry(det, beam, disk) << "." << std::endl;

    // this again depends on the AT/TEC mode, is this a doubly hit module?
    // (ring == 0 not necessary but makes it a little faster)
    if (ring == 0 &&
        find(tecDoubleHitDetId.begin(), tecDoubleHitDetId.end(), detectorId.GetTECEntry(det, ring, beam, disk)) !=
            tecDoubleHitDetId.end()) {
      if (isATMode) {  // add profile to TEC2TEC collection
        // add current event's data and subtract pedestals
        if (judge.JudgeProfile(currentDataProfiles.GetTEC2TECEntry(det, beam, disk) -
                                   pedestalProfiles.GetTEC2TECEntry(det, beam, disk),
                               0)) {
          collectedDataProfiles.GetTEC2TECEntry(det, beam, disk) +=
              currentDataProfiles.GetTEC2TECEntry(det, beam, disk) - pedestalProfiles.GetTEC2TECEntry(det, beam, disk);
          numberOfAcceptedProfiles.GetTEC2TECEntry(det, beam, disk)++;
        }
      }

    }

    else {  // not a doubly hit module, don't care about the mode
      // add current event's data and subtract pedestals
      if (judge.JudgeProfile(
              currentDataProfiles.GetTEC2TECEntry(det, beam, disk) - pedestalProfiles.GetTEC2TECEntry(det, beam, disk),
              0)) {
        collectedDataProfiles.GetTEC2TECEntry(det, beam, disk) +=
            currentDataProfiles.GetTEC2TECEntry(det, beam, disk) - pedestalProfiles.GetTEC2TECEntry(det, beam, disk);
        numberOfAcceptedProfiles.GetTEC2TECEntry(det, beam, disk)++;
      }
    }

  } while (moduleLoop.TEC2TECLoop(det, beam, disk));

  // total event number counter
  theEvents++;
}

///
///
///
void LaserAlignment::endRunProduce(edm::Run& theRun, const edm::EventSetup& theSetup) {
  std::cout << " [LaserAlignment::endRun] -- Total number of events processed: " << theEvents << std::endl;

  // for debugging only..
  DumpHitmaps(numberOfAcceptedProfiles);

  // index variables for the LASGlobalLoop objects
  int det, ring, beam, disk, pos;

  // measured positions container for the algorithms
  LASGlobalData<LASCoordinateSet> measuredCoordinates;

  // fitted peak positions in units of strips (pair for value,error)
  LASGlobalData<std::pair<float, float> > measuredStripPositions;

  // the peak finder, a pair (pos/posErr in units of strips) for its results, and the success confirmation
  LASPeakFinder peakFinder;
  peakFinder.SetAmplitudeThreshold(peakFinderThreshold);
  std::pair<double, double> peakFinderResults;
  bool isGoodFit;

  // tracker geom. object for calculating the global beam positions
  const TrackerGeometry& theTracker(*theTrackerGeometry);

  // fill LASGlobalData<LASCoordinateSet> nominalCoordinates
  CalculateNominalCoordinates();

  // for determining the phi errors
  //    ErrorFrameTransformer errorTransformer; // later...

  // do the fits for TEC+- internal
  det = 0;
  ring = 0;
  beam = 0;
  disk = 0;
  do {
    // do the fit
    isGoodFit = peakFinder.FindPeakIn(collectedDataProfiles.GetTECEntry(det, ring, beam, disk),
                                      peakFinderResults,
                                      summedHistograms.GetTECEntry(det, ring, beam, disk),
                                      0);  // offset is 0 for TEC

    // now we have the measured positions in units of strips.
    if (!isGoodFit)
      std::cout << " [LaserAlignment::endRun] ** WARNING: Fit failed for TEC det: " << det << ", ring: " << ring
                << ", beam: " << beam << ", disk: " << disk << " (id: " << detectorId.GetTECEntry(det, ring, beam, disk)
                << ")." << std::endl;

    // <- here we will later implement the kink corrections

    // access the tracker geometry for this module
    const DetId theDetId(detectorId.GetTECEntry(det, ring, beam, disk));
    const StripGeomDetUnit* const theStripDet = dynamic_cast<const StripGeomDetUnit*>(theTracker.idToDet(theDetId));

    if (theStripDet) {
      // first, set the measured coordinates to their nominal values
      measuredCoordinates.SetTECEntry(det, ring, beam, disk, nominalCoordinates.GetTECEntry(det, ring, beam, disk));

      if (isGoodFit) {  // convert strip position to global phi and replace the nominal phi value/error

        measuredStripPositions.GetTECEntry(det, ring, beam, disk) = peakFinderResults;
        const float positionInStrips =
            theSetNominalStrips
                ? 256.
                : peakFinderResults.first;  // implementation of "ForceFitterToNominalStrips" config parameter
        const GlobalPoint& globalPoint =
            theStripDet->surface().toGlobal(theStripDet->specificTopology().localPosition(positionInStrips));
        measuredCoordinates.GetTECEntry(det, ring, beam, disk).SetPhi(ConvertAngle(globalPoint.barePhi()));

        // const GlobalError& globalError = errorTransformer.transform( theStripDet->specificTopology().localError( peakFinderResults.first, pow( peakFinderResults.second, 2 ) ), theStripDet->surface() );
        // measuredCoordinates.GetTECEntry( det, ring, beam, disk ).SetPhiError( globalError.phierr( globalPoint ) );
        measuredCoordinates.GetTECEntry(det, ring, beam, disk).SetPhiError(0.00046);  // PRELIMINARY ESTIMATE

      } else {  // keep nominal position (middle-of-module) but set a giant phi error so that the module can be ignored by the alignment algorithm
        measuredStripPositions.GetTECEntry(det, ring, beam, disk) = std::pair<float, float>(256., 1000.);
        const GlobalPoint& globalPoint =
            theStripDet->surface().toGlobal(theStripDet->specificTopology().localPosition(256.));
        measuredCoordinates.GetTECEntry(det, ring, beam, disk).SetPhi(ConvertAngle(globalPoint.barePhi()));
        measuredCoordinates.GetTECEntry(det, ring, beam, disk).SetPhiError(1000.);
      }
    }

  } while (moduleLoop.TECLoop(det, ring, beam, disk));

  // do the fits for TIB/TOB
  det = 2;
  beam = 0;
  pos = 0;
  do {
    // do the fit
    isGoodFit = peakFinder.FindPeakIn(collectedDataProfiles.GetTIBTOBEntry(det, beam, pos),
                                      peakFinderResults,
                                      summedHistograms.GetTIBTOBEntry(det, beam, pos),
                                      getTIBTOBNominalBeamOffset(det, beam, pos));

    // now we have the measured positions in units of strips.
    if (!isGoodFit)
      std::cout << " [LaserAlignment::endJob] ** WARNING: Fit failed for TIB/TOB det: " << det << ", beam: " << beam
                << ", pos: " << pos << " (id: " << detectorId.GetTIBTOBEntry(det, beam, pos) << ")." << std::endl;

    // <- here we will later implement the kink corrections

    // access the tracker geometry for this module
    const DetId theDetId(detectorId.GetTIBTOBEntry(det, beam, pos));
    const StripGeomDetUnit* const theStripDet = dynamic_cast<const StripGeomDetUnit*>(theTracker.idToDet(theDetId));

    if (theStripDet) {
      // first, set the measured coordinates to their nominal values
      measuredCoordinates.SetTIBTOBEntry(det, beam, pos, nominalCoordinates.GetTIBTOBEntry(det, beam, pos));

      if (isGoodFit) {  // convert strip position to global phi and replace the nominal phi value/error
        measuredStripPositions.GetTIBTOBEntry(det, beam, pos) = peakFinderResults;
        const float positionInStrips =
            theSetNominalStrips
                ? 256. + getTIBTOBNominalBeamOffset(det, beam, pos)
                : peakFinderResults.first;  // implementation of "ForceFitterToNominalStrips" config parameter
        const GlobalPoint& globalPoint =
            theStripDet->surface().toGlobal(theStripDet->specificTopology().localPosition(positionInStrips));
        measuredCoordinates.GetTIBTOBEntry(det, beam, pos).SetPhi(ConvertAngle(globalPoint.barePhi()));
        measuredCoordinates.GetTIBTOBEntry(det, beam, pos).SetPhiError(0.00028);  // PRELIMINARY ESTIMATE
      } else {  // keep nominal position but set a giant phi error so that the module can be ignored by the alignment algorithm
        measuredStripPositions.GetTIBTOBEntry(det, beam, pos) =
            std::pair<float, float>(256. + getTIBTOBNominalBeamOffset(det, beam, pos), 1000.);
        const GlobalPoint& globalPoint = theStripDet->surface().toGlobal(
            theStripDet->specificTopology().localPosition(256. + getTIBTOBNominalBeamOffset(det, beam, pos)));
        measuredCoordinates.GetTIBTOBEntry(det, beam, pos).SetPhi(ConvertAngle(globalPoint.barePhi()));
        measuredCoordinates.GetTIBTOBEntry(det, beam, pos).SetPhiError(1000.);
      }
    }

  } while (moduleLoop.TIBTOBLoop(det, beam, pos));

  // do the fits for TEC AT
  det = 0;
  beam = 0;
  disk = 0;
  do {
    // do the fit
    isGoodFit = peakFinder.FindPeakIn(collectedDataProfiles.GetTEC2TECEntry(det, beam, disk),
                                      peakFinderResults,
                                      summedHistograms.GetTEC2TECEntry(det, beam, disk),
                                      getTEC2TECNominalBeamOffset(det, beam, disk));
    // now we have the positions in units of strips.
    if (!isGoodFit)
      std::cout << " [LaserAlignment::endRun] ** WARNING: Fit failed for TEC2TEC det: " << det << ", beam: " << beam
                << ", disk: " << disk << " (id: " << detectorId.GetTEC2TECEntry(det, beam, disk) << ")." << std::endl;

    // <- here we will later implement the kink corrections

    // access the tracker geometry for this module
    const DetId theDetId(detectorId.GetTEC2TECEntry(det, beam, disk));
    const StripGeomDetUnit* const theStripDet = dynamic_cast<const StripGeomDetUnit*>(theTracker.idToDet(theDetId));

    if (theStripDet) {
      // first, set the measured coordinates to their nominal values
      measuredCoordinates.SetTEC2TECEntry(det, beam, disk, nominalCoordinates.GetTEC2TECEntry(det, beam, disk));

      if (isGoodFit) {  // convert strip position to global phi and replace the nominal phi value/error
        measuredStripPositions.GetTEC2TECEntry(det, beam, disk) = peakFinderResults;
        const float positionInStrips =
            theSetNominalStrips
                ? 256. + getTEC2TECNominalBeamOffset(det, beam, disk)
                : peakFinderResults.first;  // implementation of "ForceFitterToNominalStrips" config parameter
        const GlobalPoint& globalPoint =
            theStripDet->surface().toGlobal(theStripDet->specificTopology().localPosition(positionInStrips));
        measuredCoordinates.GetTEC2TECEntry(det, beam, disk).SetPhi(ConvertAngle(globalPoint.barePhi()));
        measuredCoordinates.GetTEC2TECEntry(det, beam, disk).SetPhiError(0.00047);  // PRELIMINARY ESTIMATE
      } else {  // keep nominal position but set a giant phi error so that the module can be ignored by the alignment algorithm
        measuredStripPositions.GetTEC2TECEntry(det, beam, disk) =
            std::pair<float, float>(256. + getTEC2TECNominalBeamOffset(det, beam, disk), 1000.);
        const GlobalPoint& globalPoint = theStripDet->surface().toGlobal(
            theStripDet->specificTopology().localPosition(256. + getTEC2TECNominalBeamOffset(det, beam, disk)));
        measuredCoordinates.GetTEC2TECEntry(det, beam, disk).SetPhi(ConvertAngle(globalPoint.barePhi()));
        measuredCoordinates.GetTEC2TECEntry(det, beam, disk).SetPhiError(1000.);
      }
    }

  } while (moduleLoop.TEC2TECLoop(det, beam, disk));

  // see what we got (for debugging)
  //  DumpStripFileSet( measuredStripPositions );
  //  DumpPosFileSet( measuredCoordinates );

  // CALCULATE PARAMETERS AND UPDATE DB OBJECT
  // for beam kink corrections, reconstructing the geometry and updating the db object
  LASGeometryUpdater geometryUpdater(nominalCoordinates, theLasConstants);

  // apply all beam corrections
  if (theApplyBeamKinkCorrections)
    geometryUpdater.ApplyBeamKinkCorrections(measuredCoordinates);

  // if we start with input geometry instead of IDEAL,
  // reverse the adjustments in the AlignableTracker object
  if (updateFromInputGeometry)
    geometryUpdater.SetReverseDirection(true);

  // if we have "virtual" misalignment which is introduced via the reference geometry,
  // tell the LASGeometryUpdater to reverse x & y adjustments
  if (misalignedByRefGeometry)
    geometryUpdater.SetMisalignmentFromRefGeometry(true);

  // run the endcap algorithm
  LASEndcapAlgorithm endcapAlgorithm;
  LASEndcapAlignmentParameterSet endcapParameters;

  // this basically sets all the endcap modules to be masked
  // to their nominal positions (since endcapParameters is overall zero)
  if (!theMaskTecModules.empty()) {
    ApplyEndcapMaskingCorrections(measuredCoordinates, nominalCoordinates, endcapParameters);
  }

  // run the algorithm
  endcapParameters = endcapAlgorithm.CalculateParameters(measuredCoordinates, nominalCoordinates);

  //
  // loop to mask out events
  // DESCRIPTION:
  //

  // do this only if there are modules to be masked..
  if (!theMaskTecModules.empty()) {
    const unsigned int nIterations = 30;
    for (unsigned int iteration = 0; iteration < nIterations; ++iteration) {
      // set the endcap modules to be masked to their positions
      // according to the reconstructed parameters
      ApplyEndcapMaskingCorrections(measuredCoordinates, nominalCoordinates, endcapParameters);

      // modifications applied, so re-run the algorithm
      endcapParameters = endcapAlgorithm.CalculateParameters(measuredCoordinates, nominalCoordinates);
    }
  }

  // these are now final, so:
  endcapParameters.Print();

  // do a pre-alignment of the endcaps (TEC2TEC only)
  // so that the alignment tube algorithms finds orderly disks
  geometryUpdater.EndcapUpdate(endcapParameters, measuredCoordinates);

  // the alignment tube algorithms, choose from config
  LASBarrelAlignmentParameterSet alignmentTubeParameters;
  // the MINUIT-BASED alignment tube algorithm
  LASBarrelAlgorithm barrelAlgorithm;
  // the ANALYTICAL alignment tube algorithm
  LASAlignmentTubeAlgorithm alignmentTubeAlgorithm;

  // this basically sets all the modules to be masked
  // to their nominal positions (since alignmentTubeParameters is overall zero)
  if (!theMaskAtModules.empty()) {
    ApplyATMaskingCorrections(measuredCoordinates, nominalCoordinates, alignmentTubeParameters);
  }

  if (theUseMinuitAlgorithm) {
    // run the MINUIT-BASED alignment tube algorithm
    alignmentTubeParameters = barrelAlgorithm.CalculateParameters(measuredCoordinates, nominalCoordinates);
  } else {
    // the ANALYTICAL alignment tube algorithm
    alignmentTubeParameters = alignmentTubeAlgorithm.CalculateParameters(measuredCoordinates, nominalCoordinates);
  }

  //
  // loop to mask out events
  // DESCRIPTION:
  //

  // do this only if there are modules to be masked..
  if (!theMaskAtModules.empty()) {
    const unsigned int nIterations = 30;
    for (unsigned int iteration = 0; iteration < nIterations; ++iteration) {
      // set the AT modules to be masked to their positions
      // according to the reconstructed parameters
      ApplyATMaskingCorrections(measuredCoordinates, nominalCoordinates, alignmentTubeParameters);

      // modifications applied, so re-run the algorithm
      if (theUseMinuitAlgorithm) {
        alignmentTubeParameters = barrelAlgorithm.CalculateParameters(measuredCoordinates, nominalCoordinates);
      } else {
        alignmentTubeParameters = alignmentTubeAlgorithm.CalculateParameters(measuredCoordinates, nominalCoordinates);
      }
    }
  }

  // these are now final, so:
  alignmentTubeParameters.Print();

  // combine the results and update the db object
  geometryUpdater.TrackerUpdate(endcapParameters, alignmentTubeParameters, *theAlignableTracker);

  /// laser hit section for trackbased interface
  ///
  /// due to the peculiar order of beams in TkLasBeamCollection,
  /// we cannot use the LASGlobalLoop object here

  // the collection container
  auto laserBeams = std::make_unique<TkLasBeamCollection>();

  // first for the endcap internal beams
  for (det = 0; det < 2; ++det) {
    for (ring = 0; ring < 2; ++ring) {
      for (beam = 0; beam < 8; ++beam) {
        // the beam and its identifier (see TkLasTrackBasedInterface TWiki)
        TkLasBeam currentBeam(100 * det + 10 * beam + ring);

        // order the hits in the beam by increasing z
        const int firstDisk = det == 0 ? 0 : 8;
        const int lastDisk = det == 0 ? 8 : 0;

        // count upwards or downwards
        for (disk = firstDisk; det == 0 ? disk <= lastDisk : disk >= lastDisk; det == 0 ? ++disk : --disk) {
          // detId for the SiStripLaserRecHit2D
          const SiStripDetId theDetId(detectorId.GetTECEntry(det, ring, beam, disk));

          // need this to calculate the localPosition and its error
          const StripGeomDetUnit* const theStripDet =
              dynamic_cast<const StripGeomDetUnit*>(theTracker.idToDet(theDetId));

          // the hit container
          const SiStripLaserRecHit2D currentHit(theStripDet->specificTopology().localPosition(
                                                    measuredStripPositions.GetTECEntry(det, ring, beam, disk).first),
                                                theStripDet->specificTopology().localError(
                                                    measuredStripPositions.GetTECEntry(det, ring, beam, disk).first,
                                                    measuredStripPositions.GetTECEntry(det, ring, beam, disk).second),
                                                theDetId);

          currentBeam.push_back(currentHit);
        }

        laserBeams->push_back(currentBeam);
      }
    }
  }

  // then, following the convention in TkLasTrackBasedInterface TWiki, the alignment tube beams;
  // they comprise hits in TIBTOB & TEC2TEC

  for (beam = 0; beam < 8; ++beam) {
    // the beam and its identifier (see TkLasTrackBasedInterface TWiki)
    TkLasBeam currentBeam(100 * 2 /*beamGroup=AT=2*/ + 10 * beam + 0 /*ring=0*/);

    // first: tec-
    det = 1;
    for (disk = 4; disk >= 0; --disk) {
      // detId for the SiStripLaserRecHit2D
      const SiStripDetId theDetId(detectorId.GetTEC2TECEntry(det, beam, disk));

      // need this to calculate the localPosition and its error
      const StripGeomDetUnit* const theStripDet = dynamic_cast<const StripGeomDetUnit*>(theTracker.idToDet(theDetId));

      // the hit container
      const SiStripLaserRecHit2D currentHit(
          theStripDet->specificTopology().localPosition(measuredStripPositions.GetTEC2TECEntry(det, beam, disk).first),
          theStripDet->specificTopology().localError(measuredStripPositions.GetTEC2TECEntry(det, beam, disk).first,
                                                     measuredStripPositions.GetTEC2TECEntry(det, beam, disk).second),
          theDetId);

      currentBeam.push_back(currentHit);
    }

    // now TIB and TOB in one go
    for (det = 2; det < 4; ++det) {
      for (pos = 5; pos >= 0; --pos) {  // stupidly, pos is defined from +z to -z in LASGlobalLoop

        // detId for the SiStripLaserRecHit2D
        const SiStripDetId theDetId(detectorId.GetTIBTOBEntry(det, beam, pos));

        // need this to calculate the localPosition and its error
        const StripGeomDetUnit* const theStripDet = dynamic_cast<const StripGeomDetUnit*>(theTracker.idToDet(theDetId));

        // the hit container
        const SiStripLaserRecHit2D currentHit(
            theStripDet->specificTopology().localPosition(measuredStripPositions.GetTIBTOBEntry(det, beam, pos).first),
            theStripDet->specificTopology().localError(measuredStripPositions.GetTIBTOBEntry(det, beam, pos).first,
                                                       measuredStripPositions.GetTIBTOBEntry(det, beam, pos).second),
            theDetId);

        currentBeam.push_back(currentHit);
      }
    }

    // then: tec+
    det = 0;
    for (disk = 0; disk < 5; ++disk) {
      // detId for the SiStripLaserRecHit2D
      const SiStripDetId theDetId(detectorId.GetTEC2TECEntry(det, beam, disk));

      // need this to calculate the localPosition and its error
      const StripGeomDetUnit* const theStripDet = dynamic_cast<const StripGeomDetUnit*>(theTracker.idToDet(theDetId));

      // the hit container
      const SiStripLaserRecHit2D currentHit(
          theStripDet->specificTopology().localPosition(measuredStripPositions.GetTEC2TECEntry(det, beam, disk).first),
          theStripDet->specificTopology().localError(measuredStripPositions.GetTEC2TECEntry(det, beam, disk).first,
                                                     measuredStripPositions.GetTEC2TECEntry(det, beam, disk).second),
          theDetId);

      currentBeam.push_back(currentHit);
    }

    // save this beam to the beamCollection
    laserBeams->push_back(currentBeam);

  }  // (close beam loop)

  // now attach the collection to the run
  theRun.put(std::move(laserBeams), "tkLaserBeams");

  // store the estimated alignment parameters into the DB
  // first get them
  Alignments alignments = *(theAlignableTracker->alignments());
  AlignmentErrorsExtended alignmentErrors = *(theAlignableTracker->alignmentErrors());

  if (theStoreToDB) {
    std::cout << " [LaserAlignment::endRun] -- Storing the calculated alignment parameters to the DataBase:"
              << std::endl;

    // Call service
    edm::Service<cond::service::PoolDBOutputService> poolDbService;
    if (!poolDbService.isAvailable())  // Die if not available
      throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";

    // Store

    //     if ( poolDbService->isNewTagRequest(theAlignRecordName) ) {
    //       poolDbService->createOneIOV<Alignments>( alignments, poolDbService->currentTime(), theAlignRecordName );
    //     }
    //     else {
    //       poolDbService->appendOneIOV<Alignments>( alignments, poolDbService->currentTime(), theAlignRecordName );
    //     }
    poolDbService->writeOneIOV<Alignments>(alignments, poolDbService->beginOfTime(), theAlignRecordName);

    //     if ( poolDbService->isNewTagRequest(theErrorRecordName) ) {
    //       poolDbService->createOneIOV<AlignmentErrorsExtended>( alignmentErrors, poolDbService->currentTime(), poolDbService->endOfTime(), theErrorRecordName );
    //     }
    //     else {
    //       poolDbService->appendOneIOV<AlignmentErrorsExtended>( alignmentErrors, poolDbService->currentTime(), theErrorRecordName );
    //     }
    poolDbService->writeOneIOV<AlignmentErrorsExtended>(
        alignmentErrors, poolDbService->beginOfTime(), theErrorRecordName);

    std::cout << " [LaserAlignment::endRun] -- Storing done." << std::endl;
  }
}

///
///
///
void LaserAlignment::endJob() {}

///
/// fills the module profiles (LASGlobalLoop<LASModuleProfile> currentDataProfiles)
/// from the event digi containers, distinguishing between SiStripDigi or SiStripRawDigi.
///
void LaserAlignment::fillDataProfiles(edm::Event const& theEvent, edm::EventSetup const& theSetup) {
  // two handles for the two different kinds of digis
  edm::Handle<edm::DetSetVector<SiStripRawDigi> > theStripRawDigis;
  edm::Handle<edm::DetSetVector<SiStripDigi> > theStripDigis;

  bool isRawDigi = false;

  // indices for the LASGlobalLoop object
  int det = 0, ring = 0, beam = 0, disk = 0, pos = 0;

  // query config set and loop over all PSets in the VPSet
  for (std::vector<edm::ParameterSet>::iterator itDigiProducersList = theDigiProducersList.begin();
       itDigiProducersList != theDigiProducersList.end();
       ++itDigiProducersList) {
    std::string digiProducer = itDigiProducersList->getParameter<std::string>("DigiProducer");
    std::string digiLabel = itDigiProducersList->getParameter<std::string>("DigiLabel");
    std::string digiType = itDigiProducersList->getParameter<std::string>("DigiType");

    // now branch according to digi type (raw or processed);
    // first we go for raw digis => SiStripRawDigi
    if (digiType == "Raw") {
      theEvent.getByLabel(digiProducer, digiLabel, theStripRawDigis);
      isRawDigi = true;
    } else if (digiType == "Processed") {
      theEvent.getByLabel(digiProducer, digiLabel, theStripDigis);
      isRawDigi = false;
    } else {
      throw cms::Exception(" [LaserAlignment::fillDataProfiles]")
          << " ** ERROR: Invalid digi type: \"" << digiType << "\" specified in configuration." << std::endl;
    }

    // loop TEC internal modules
    det = 0;
    ring = 0;
    beam = 0;
    disk = 0;
    do {
      // first clear the profile
      currentDataProfiles.GetTECEntry(det, ring, beam, disk).SetAllValuesTo(0.);

      // retrieve the raw id of that module
      const int detRawId = detectorId.GetTECEntry(det, ring, beam, disk);

      if (isRawDigi) {  // we have raw SiStripRawDigis

        // search the digis for the raw id
        edm::DetSetVector<SiStripRawDigi>::const_iterator detSetIter = theStripRawDigis->find(detRawId);
        if (detSetIter == theStripRawDigis->end()) {
          throw cms::Exception("[Laser Alignment::fillDataProfiles]")
              << " ** ERROR: No raw DetSet found for det: " << detRawId << "." << std::endl;
        }

        // fill the digis to the profiles
        edm::DetSet<SiStripRawDigi>::const_iterator digiRangeIterator = detSetIter->data.begin();  // for the loop
        edm::DetSet<SiStripRawDigi>::const_iterator digiRangeStart = digiRangeIterator;  // save starting positions

        // loop all digis
        for (; digiRangeIterator != detSetIter->data.end(); ++digiRangeIterator) {
          const SiStripRawDigi& digi = *digiRangeIterator;
          const int channel = distance(digiRangeStart, digiRangeIterator);
          if (channel >= 0 && channel < 512)
            currentDataProfiles.GetTECEntry(det, ring, beam, disk).SetValue(channel, digi.adc());
          else
            throw cms::Exception("[Laser Alignment::fillDataProfiles]")
                << " ** ERROR: raw digi channel: " << channel << " out of range for det: " << detRawId << "."
                << std::endl;
        }

      }

      else {  // we have zero suppressed SiStripDigis

        // search the digis for the raw id
        edm::DetSetVector<SiStripDigi>::const_iterator detSetIter = theStripDigis->find(detRawId);

        // processed DetSets may be missing, just skip
        if (detSetIter == theStripDigis->end())
          continue;

        // fill the digis to the profiles
        edm::DetSet<SiStripDigi>::const_iterator digiRangeIterator = detSetIter->data.begin();  // for the loop

        for (; digiRangeIterator != detSetIter->data.end(); ++digiRangeIterator) {
          const SiStripDigi& digi = *digiRangeIterator;
          if (digi.strip() < 512)
            currentDataProfiles.GetTECEntry(det, ring, beam, disk).SetValue(digi.strip(), digi.adc());
          else
            throw cms::Exception("[Laser Alignment::fillDataProfiles]")
                << " ** ERROR: digi strip: " << digi.strip() << " out of range for det: " << detRawId << "."
                << std::endl;
        }
      }

    } while (moduleLoop.TECLoop(det, ring, beam, disk));

    // loop TIBTOB modules
    det = 2;
    beam = 0;
    pos = 0;
    do {
      // first clear the profile
      currentDataProfiles.GetTIBTOBEntry(det, beam, pos).SetAllValuesTo(0.);

      // retrieve the raw id of that module
      const int detRawId = detectorId.GetTIBTOBEntry(det, beam, pos);

      if (isRawDigi) {  // we have raw SiStripRawDigis

        // search the digis for the raw id
        edm::DetSetVector<SiStripRawDigi>::const_iterator detSetIter = theStripRawDigis->find(detRawId);
        if (detSetIter == theStripRawDigis->end()) {
          throw cms::Exception("[Laser Alignment::fillDataProfiles]")
              << " ** ERROR: No raw DetSet found for det: " << detRawId << "." << std::endl;
        }

        // fill the digis to the profiles
        edm::DetSet<SiStripRawDigi>::const_iterator digiRangeIterator = detSetIter->data.begin();  // for the loop
        edm::DetSet<SiStripRawDigi>::const_iterator digiRangeStart = digiRangeIterator;  // save starting positions

        // loop all digis
        for (; digiRangeIterator != detSetIter->data.end(); ++digiRangeIterator) {
          const SiStripRawDigi& digi = *digiRangeIterator;
          const int channel = distance(digiRangeStart, digiRangeIterator);
          if (channel >= 0 && channel < 512)
            currentDataProfiles.GetTIBTOBEntry(det, beam, pos).SetValue(channel, digi.adc());
          else
            throw cms::Exception("[Laser Alignment::fillDataProfiles]")
                << " ** ERROR: raw digi channel: " << channel << " out of range for det: " << detRawId << "."
                << std::endl;
        }

      }

      else {  // we have zero suppressed SiStripDigis

        // search the digis for the raw id
        edm::DetSetVector<SiStripDigi>::const_iterator detSetIter = theStripDigis->find(detRawId);

        // processed DetSets may be missing, just skip
        if (detSetIter == theStripDigis->end())
          continue;

        // fill the digis to the profiles
        edm::DetSet<SiStripDigi>::const_iterator digiRangeIterator = detSetIter->data.begin();  // for the loop

        for (; digiRangeIterator != detSetIter->data.end(); ++digiRangeIterator) {
          const SiStripDigi& digi = *digiRangeIterator;
          if (digi.strip() < 512)
            currentDataProfiles.GetTIBTOBEntry(det, beam, pos).SetValue(digi.strip(), digi.adc());
          else
            throw cms::Exception("[Laser Alignment::fillDataProfiles]")
                << " ** ERROR: digi strip: " << digi.strip() << " out of range for det: " << detRawId << "."
                << std::endl;
        }
      }

    } while (moduleLoop.TIBTOBLoop(det, beam, pos));

    // loop TEC AT modules
    det = 0;
    beam = 0;
    disk = 0;
    do {
      // first clear the profile
      currentDataProfiles.GetTEC2TECEntry(det, beam, disk).SetAllValuesTo(0.);

      // retrieve the raw id of that module
      const int detRawId = detectorId.GetTEC2TECEntry(det, beam, disk);

      if (isRawDigi) {  // we have raw SiStripRawDigis

        // search the digis for the raw id
        edm::DetSetVector<SiStripRawDigi>::const_iterator detSetIter = theStripRawDigis->find(detRawId);
        if (detSetIter == theStripRawDigis->end()) {
          throw cms::Exception("[Laser Alignment::fillDataProfiles]")
              << " ** ERROR: No raw DetSet found for det: " << detRawId << "." << std::endl;
        }

        // fill the digis to the profiles
        edm::DetSet<SiStripRawDigi>::const_iterator digiRangeIterator = detSetIter->data.begin();  // for the loop
        edm::DetSet<SiStripRawDigi>::const_iterator digiRangeStart = digiRangeIterator;  // save starting positions

        // loop all digis
        for (; digiRangeIterator != detSetIter->data.end(); ++digiRangeIterator) {
          const SiStripRawDigi& digi = *digiRangeIterator;
          const int channel = distance(digiRangeStart, digiRangeIterator);
          if (channel >= 0 && channel < 512)
            currentDataProfiles.GetTEC2TECEntry(det, beam, disk).SetValue(channel, digi.adc());
          else
            throw cms::Exception("[Laser Alignment::fillDataProfiles]")
                << " ** ERROR: raw digi channel: " << channel << " out of range for det: " << detRawId << "."
                << std::endl;
        }

      }

      else {  // we have zero suppressed SiStripDigis

        // search the digis for the raw id
        edm::DetSetVector<SiStripDigi>::const_iterator detSetIter = theStripDigis->find(detRawId);

        // processed DetSets may be missing, just skip
        if (detSetIter == theStripDigis->end())
          continue;

        // fill the digis to the profiles
        edm::DetSet<SiStripDigi>::const_iterator digiRangeIterator = detSetIter->data.begin();  // for the loop

        for (; digiRangeIterator != detSetIter->data.end(); ++digiRangeIterator) {
          const SiStripDigi& digi = *digiRangeIterator;
          if (digi.strip() < 512)
            currentDataProfiles.GetTEC2TECEntry(det, beam, disk).SetValue(digi.strip(), digi.adc());
          else
            throw cms::Exception("[Laser Alignment::fillDataProfiles]")
                << " ** ERROR: digi strip: " << digi.strip() << " out of range for det: " << detRawId << "."
                << std::endl;
        }
      }

    } while (moduleLoop.TEC2TECLoop(det, beam, disk));

  }  // theDigiProducersList loop
}

///
/// This function fills the pedestal profiles (LASGlobalData<LASModuleProfiles> pedestalProfiles)
/// from the ESHandle (from file or DB)
///
/// Argument: readily connected SiStripPedestals object (get() alredy called)
/// The functionality inside the loops is basically taken from:
/// CommonTools/SiStripZeroSuppression/src/SiStripPedestalsSubtractor.cc
///
void LaserAlignment::fillPedestalProfiles(edm::ESHandle<SiStripPedestals>& pedestalsHandle) {
  int det, ring, beam, disk, pos;

  // loop TEC modules (yet without AT)
  det = 0;
  ring = 0;
  beam = 0;
  disk = 0;
  do {  // loop using LASGlobalLoop functionality
    SiStripPedestals::Range pedRange = pedestalsHandle->getRange(detectorId.GetTECEntry(det, ring, beam, disk));
    for (int strip = 0; strip < 512; ++strip) {
      int thePedestal = int(pedestalsHandle->getPed(strip, pedRange));
      if (thePedestal > 895)
        thePedestal -= 1024;
      pedestalProfiles.GetTECEntry(det, ring, beam, disk).SetValue(strip, thePedestal);
    }
  } while (moduleLoop.TECLoop(det, ring, beam, disk));

  // TIB & TOB section
  det = 2;
  beam = 0;
  pos = 0;
  do {  // loop using LASGlobalLoop functionality
    SiStripPedestals::Range pedRange = pedestalsHandle->getRange(detectorId.GetTIBTOBEntry(det, beam, pos));
    for (int strip = 0; strip < 512; ++strip) {
      int thePedestal = int(pedestalsHandle->getPed(strip, pedRange));
      if (thePedestal > 895)
        thePedestal -= 1024;
      pedestalProfiles.GetTIBTOBEntry(det, beam, pos).SetValue(strip, thePedestal);
    }
  } while (moduleLoop.TIBTOBLoop(det, beam, pos));

  // TEC2TEC AT section
  det = 0;
  beam = 0;
  disk = 0;
  do {  // loop using LASGlobalLoop functionality
    SiStripPedestals::Range pedRange = pedestalsHandle->getRange(detectorId.GetTEC2TECEntry(det, beam, disk));
    for (int strip = 0; strip < 512; ++strip) {
      int thePedestal = int(pedestalsHandle->getPed(strip, pedRange));
      if (thePedestal > 895)
        thePedestal -= 1024;
      pedestalProfiles.GetTEC2TECEntry(det, beam, disk).SetValue(strip, thePedestal);
    }
  } while (moduleLoop.TEC2TECLoop(det, beam, disk));
}

///
/// count useable profiles in TEC,
/// operates on LASGlobalData<int> LaserAlignment::isAcceptedProfile
/// to allow for more elaborate patterns in the future
///
bool LaserAlignment::isTECBeam(void) {
  int numberOfProfiles = 0;

  int ring = 1;  // search all ring6 modules for signals
  for (int det = 0; det < 2; ++det) {
    for (int beam = 0; beam < 8; ++beam) {
      for (int disk = 0; disk < 9; ++disk) {
        if (isAcceptedProfile.GetTECEntry(det, ring, beam, disk) == 1)
          numberOfProfiles++;
      }
    }
  }

  LogDebug("[LaserAlignment::isTECBeam]") << " Found: " << numberOfProfiles << "hits." << std::endl;
  std::cout << " [LaserAlignment::isTECBeam] -- Found: " << numberOfProfiles << " hits." << std::endl;  ////

  if (numberOfProfiles > 10)
    return (true);
  return (false);
}

///
/// count useable profiles in TIBTOB,
/// operates on LASGlobalData<bool> LaserAlignment::isAcceptedProfile
/// to allow for more elaborate patterns in the future
///

bool LaserAlignment::isATBeam(void) {
  int numberOfProfiles = 0;

  int det = 2;
  int beam = 0;
  int pos = 0;  // search all TIB/TOB for signals
  do {
    if (isAcceptedProfile.GetTIBTOBEntry(det, beam, pos) == 1)
      numberOfProfiles++;
  } while (moduleLoop.TIBTOBLoop(det, beam, pos));

  LogDebug("[LaserAlignment::isATBeam]") << " Found: " << numberOfProfiles << "hits." << std::endl;
  std::cout << " [LaserAlignment::isATBeam] -- Found: " << numberOfProfiles << " hits." << std::endl;  /////

  if (numberOfProfiles > 10)
    return (true);
  return (false);
}

///
/// not all TIB & TOB modules are hit in the center;
/// this func returns the nominal beam offset locally on a module (in strips)
/// for the ProfileJudge and the LASPeakFinder in strips.
/// (offset = middle of module - nominal position)
///
/// the hard coded numbers will later be supplied by a special geometry class..
///
double LaserAlignment::getTIBTOBNominalBeamOffset(unsigned int det, unsigned int beam, unsigned int pos) {
  if (det < 2 || det > 3 || beam > 7 || pos > 5) {
    throw cms::Exception("[LaserAlignment::getTIBTOBNominalBeamOffset]")
        << " ERROR ** Called with nonexisting parameter set: det " << det << " beam " << beam << " pos " << pos << "."
        << std::endl;
  }

  const double nominalOffsetsTIB[8] = {
      0.00035, 2.10687, -2.10827, -0.00173446, 2.10072, -0.00135114, 2.10105, -2.10401};

  // in tob, modules have alternating orientations along the rods.
  // this is described by the following pattern.
  // (even more confusing, this pattern is inversed for beams 0, 5, 6, 7)
  const int orientationPattern[6] = {-1, 1, 1, -1, -1, 1};
  const double nominalOffsetsTOB[8] = {0.00217408, 1.58678, 117.733, 119.321, 120.906, 119.328, 117.743, 1.58947};

  if (det == 2)
    return (-1. * nominalOffsetsTIB[beam]);

  else {
    if (beam == 0 or beam > 4)
      return (nominalOffsetsTOB[beam] * orientationPattern[pos]);
    else
      return (-1. * nominalOffsetsTOB[beam] * orientationPattern[pos]);
  }
}

///
/// not all TEC-AT modules are hit in the center;
/// this func returns the nominal beam offset locally on a module (in strips)
/// for the ProfileJudge and the LASPeakFinder in strips.
/// (offset = middle of module - nominal position)
///
/// the hard coded numbers will later be supplied by a special geometry class..
///
double LaserAlignment::getTEC2TECNominalBeamOffset(unsigned int det, unsigned int beam, unsigned int disk) {
  if (det > 1 || beam > 7 || disk > 5) {
    throw cms::Exception("[LaserAlignment::getTEC2TECNominalBeamOffset]")
        << " ERROR ** Called with nonexisting parameter set: det " << det << " beam " << beam << " disk " << disk << "."
        << std::endl;
  }

  const double nominalOffsets[8] = {0., 2.220, -2.221, 0., 2.214, 0., 2.214, -2.217};

  if (det == 0)
    return -1. * nominalOffsets[beam];
  else
    return nominalOffsets[beam];
}

///
///
///
void LaserAlignment::CalculateNominalCoordinates(void) {
  //
  // hard coded data yet...
  //

  // nominal phi values of tec beam / alignment tube hits (parameter is beam 0-7)
  const double tecPhiPositions[8] = {
      0.392699, 1.178097, 1.963495, 2.748894, 3.534292, 4.319690, 5.105088, 5.890486};  // new values calculated by maple
  const double atPhiPositions[8] = {
      0.392699, 1.289799, 1.851794, 2.748894, 3.645995, 4.319690, 5.216791, 5.778784};  // new values calculated by maple

  // nominal r values (mm) of hits
  const double tobRPosition = 600.;
  const double tibRPosition = 514.;
  const double tecRPosition[2] = {564., 840.};  // ring 4,6

  // nominal z values (mm) of hits in barrel (parameter is pos 0-6)
  const double tobZPosition[6] = {1040., 580., 220., -140., -500., -860.};
  const double tibZPosition[6] = {620., 380., 180., -100., -340., -540.};

  // nominal z values (mm) of hits in tec (parameter is disk 0-8); FOR TEC-: (* -1.)
  const double tecZPosition[9] = {1322.5, 1462.5, 1602.5, 1742.5, 1882.5, 2057.5, 2247.5, 2452.5, 2667.5};

  //
  // now we fill these into the nominalCoordinates container;
  // errors are zero for nominal values..
  //

  // loop object and its variables
  LASGlobalLoop moduleLoop;
  int det, ring, beam, disk, pos;

  // TEC+- section
  det = 0;
  ring = 0, beam = 0;
  disk = 0;
  do {
    if (det == 0) {  // this is TEC+
      nominalCoordinates.SetTECEntry(
          det,
          ring,
          beam,
          disk,
          LASCoordinateSet(tecPhiPositions[beam], 0., tecRPosition[ring], 0., tecZPosition[disk], 0.));
    } else {  // now TEC-
      nominalCoordinates.SetTECEntry(
          det,
          ring,
          beam,
          disk,
          LASCoordinateSet(
              tecPhiPositions[beam], 0., tecRPosition[ring], 0., -1. * tecZPosition[disk], 0.));  // just * -1.
    }

  } while (moduleLoop.TECLoop(det, ring, beam, disk));

  // TIB & TOB section
  det = 2;
  beam = 0;
  pos = 0;
  do {
    if (det == 2) {  // this is TIB
      nominalCoordinates.SetTIBTOBEntry(
          det, beam, pos, LASCoordinateSet(atPhiPositions[beam], 0., tibRPosition, 0., tibZPosition[pos], 0.));
    } else {  // now TOB
      nominalCoordinates.SetTIBTOBEntry(
          det, beam, pos, LASCoordinateSet(atPhiPositions[beam], 0., tobRPosition, 0., tobZPosition[pos], 0.));
    }

  } while (moduleLoop.TIBTOBLoop(det, beam, pos));

  // TEC2TEC AT section
  det = 0;
  beam = 0;
  disk = 0;
  do {
    if (det == 0) {  // this is TEC+, ring4 only
      nominalCoordinates.SetTEC2TECEntry(
          det, beam, disk, LASCoordinateSet(atPhiPositions[beam], 0., tecRPosition[0], 0., tecZPosition[disk], 0.));
    } else {  // now TEC-
      nominalCoordinates.SetTEC2TECEntry(
          det,
          beam,
          disk,
          LASCoordinateSet(atPhiPositions[beam], 0., tecRPosition[0], 0., -1. * tecZPosition[disk], 0.));  // just * -1.
    }

  } while (moduleLoop.TEC2TECLoop(det, beam, disk));
}

///
/// convert an angle in the [-pi,pi] range
/// to the [0,2*pi] range
///
double LaserAlignment::ConvertAngle(double angle) {
  if (angle < -1. * M_PI || angle > M_PI) {
    throw cms::Exception(" [LaserAlignment::ConvertAngle] ")
        << "** ERROR: Called with illegal input angle: " << angle << "." << std::endl;
  }

  if (angle >= 0.)
    return angle;
  else
    return (angle + 2. * M_PI);
}

///
/// debug only, will disappear
///
void LaserAlignment::DumpPosFileSet(LASGlobalData<LASCoordinateSet>& coordinates) {
  LASGlobalLoop loop;
  int det, ring, beam, disk, pos;

  std::cout << std::endl << " [LaserAlignment::DumpPosFileSet] -- Dump: " << std::endl;

  // TEC INTERNAL
  det = 0;
  ring = 0;
  beam = 0;
  disk = 0;
  do {
    std::cout << "POS " << det << "\t" << beam << "\t" << disk << "\t" << ring << "\t"
              << coordinates.GetTECEntry(det, ring, beam, disk).GetPhi() << "\t"
              << coordinates.GetTECEntry(det, ring, beam, disk).GetPhiError() << std::endl;
  } while (loop.TECLoop(det, ring, beam, disk));

  // TIBTOB
  det = 2;
  beam = 0;
  pos = 0;
  do {
    std::cout << "POS " << det << "\t" << beam << "\t" << pos << "\t"
              << "-1"
              << "\t" << coordinates.GetTIBTOBEntry(det, beam, pos).GetPhi() << "\t"
              << coordinates.GetTIBTOBEntry(det, beam, pos).GetPhiError() << std::endl;
  } while (loop.TIBTOBLoop(det, beam, pos));

  // TEC2TEC
  det = 0;
  beam = 0;
  disk = 0;
  do {
    std::cout << "POS " << det << "\t" << beam << "\t" << disk << "\t"
              << "-1"
              << "\t" << coordinates.GetTEC2TECEntry(det, beam, disk).GetPhi() << "\t"
              << coordinates.GetTEC2TECEntry(det, beam, disk).GetPhiError() << std::endl;
  } while (loop.TEC2TECLoop(det, beam, disk));

  std::cout << std::endl << " [LaserAlignment::DumpPosFileSet] -- End dump: " << std::endl;
}

///
///
///
void LaserAlignment::DumpStripFileSet(LASGlobalData<std::pair<float, float> >& measuredStripPositions) {
  LASGlobalLoop loop;
  int det, ring, beam, disk, pos;

  std::cout << std::endl << " [LaserAlignment::DumpStripFileSet] -- Dump: " << std::endl;

  // TEC INTERNAL
  det = 0;
  ring = 0;
  beam = 0;
  disk = 0;
  do {
    std::cout << "STRIP " << det << "\t" << beam << "\t" << disk << "\t" << ring << "\t"
              << measuredStripPositions.GetTECEntry(det, ring, beam, disk).first << "\t"
              << measuredStripPositions.GetTECEntry(det, ring, beam, disk).second << std::endl;
  } while (loop.TECLoop(det, ring, beam, disk));

  // TIBTOB
  det = 2;
  beam = 0;
  pos = 0;
  do {
    std::cout << "STRIP " << det << "\t" << beam << "\t" << pos << "\t"
              << "-1"
              << "\t" << measuredStripPositions.GetTIBTOBEntry(det, beam, pos).first << "\t"
              << measuredStripPositions.GetTIBTOBEntry(det, beam, pos).second << std::endl;
  } while (loop.TIBTOBLoop(det, beam, pos));

  // TEC2TEC
  det = 0;
  beam = 0;
  disk = 0;
  do {
    std::cout << "STRIP " << det << "\t" << beam << "\t" << disk << "\t"
              << "-1"
              << "\t" << measuredStripPositions.GetTEC2TECEntry(det, beam, disk).first << "\t"
              << measuredStripPositions.GetTEC2TECEntry(det, beam, disk).second << std::endl;
  } while (loop.TEC2TECLoop(det, beam, disk));

  std::cout << std::endl << " [LaserAlignment::DumpStripFileSet] -- End dump: " << std::endl;
}

///
///
///
void LaserAlignment::DumpHitmaps(LASGlobalData<int>& numberOfAcceptedProfiles) {
  std::cout << " [LaserAlignment::DumpHitmaps] -- Dumping hitmap for TEC+:" << std::endl;
  std::cout << " [LaserAlignment::DumpHitmaps] -- Ring4:" << std::endl;
  std::cout << "     disk0   disk1   disk2   disk3   disk4   disk5   disk6   disk7   disk8" << std::endl;

  for (int beam = 0; beam < 8; ++beam) {
    std::cout << " beam" << beam << ":";
    for (int disk = 0; disk < 9; ++disk) {
      std::cout << "\t" << numberOfAcceptedProfiles.GetTECEntry(0, 0, beam, disk);
    }
    std::cout << std::endl;
  }

  std::cout << " [LaserAlignment::DumpHitmaps] -- Ring6:" << std::endl;
  std::cout << "     disk0   disk1   disk2   disk3   disk4   disk5   disk6   disk7   disk8" << std::endl;

  for (int beam = 0; beam < 8; ++beam) {
    std::cout << " beam" << beam << ":";
    for (int disk = 0; disk < 9; ++disk) {
      std::cout << "\t" << numberOfAcceptedProfiles.GetTECEntry(0, 1, beam, disk);
    }
    std::cout << std::endl;
  }

  std::cout << " [LaserAlignment::DumpHitmaps] -- Dumping hitmap for TEC-:" << std::endl;
  std::cout << " [LaserAlignment::DumpHitmaps] -- Ring4:" << std::endl;
  std::cout << "     disk0   disk1   disk2   disk3   disk4   disk5   disk6   disk7   disk8" << std::endl;

  for (int beam = 0; beam < 8; ++beam) {
    std::cout << " beam" << beam << ":";
    for (int disk = 0; disk < 9; ++disk) {
      std::cout << "\t" << numberOfAcceptedProfiles.GetTECEntry(1, 0, beam, disk);
    }
    std::cout << std::endl;
  }

  std::cout << " [LaserAlignment::DumpHitmaps] -- Ring6:" << std::endl;
  std::cout << "     disk0   disk1   disk2   disk3   disk4   disk5   disk6   disk7   disk8" << std::endl;

  for (int beam = 0; beam < 8; ++beam) {
    std::cout << " beam" << beam << ":";
    for (int disk = 0; disk < 9; ++disk) {
      std::cout << "\t" << numberOfAcceptedProfiles.GetTECEntry(1, 1, beam, disk);
    }
    std::cout << std::endl;
  }

  std::cout << " [LaserAlignment::DumpHitmaps] -- End of dump." << std::endl << std::endl;
}

///
/// loop the list of endcap modules to be masked and
/// apply the corrections from the "endcapParameters" to them
///
void LaserAlignment::ApplyEndcapMaskingCorrections(LASGlobalData<LASCoordinateSet>& measuredCoordinates,
                                                   LASGlobalData<LASCoordinateSet>& nominalCoordinates,
                                                   LASEndcapAlignmentParameterSet& endcapParameters) {
  // loop the list of modules to be masked
  for (std::vector<unsigned int>::iterator moduleIt = theMaskTecModules.begin(); moduleIt != theMaskTecModules.end();
       ++moduleIt) {
    // loop variables
    LASGlobalLoop moduleLoop;
    int det, ring, beam, disk;

    // this will calculate the corrections from the alignment parameters
    LASEndcapAlgorithm endcapAlgorithm;

    // find the location of the respective module in the container with this loop
    det = 0;
    ring = 0;
    beam = 0;
    disk = 0;
    do {
      // here we got it
      if (detectorId.GetTECEntry(det, ring, beam, disk) == *moduleIt) {
        // the nominal phi value for this module
        const double nominalPhi = nominalCoordinates.GetTECEntry(det, ring, beam, disk).GetPhi();

        // the offset from the alignment parameters
        const double phiCorrection = endcapAlgorithm.GetAlignmentParameterCorrection(
            det, ring, beam, disk, nominalCoordinates, endcapParameters);

        // apply the corrections
        measuredCoordinates.GetTECEntry(det, ring, beam, disk).SetPhi(nominalPhi - phiCorrection);
      }

    } while (moduleLoop.TECLoop(det, ring, beam, disk));
  }
}

///
/// loop the list of alignment tube modules to be masked and
/// apply the corrections from the "barrelParameters" to them
///
void LaserAlignment::ApplyATMaskingCorrections(LASGlobalData<LASCoordinateSet>& measuredCoordinates,
                                               LASGlobalData<LASCoordinateSet>& nominalCoordinates,
                                               LASBarrelAlignmentParameterSet& atParameters) {
  // loop the list of modules to be masked
  for (std::vector<unsigned int>::iterator moduleIt = theMaskAtModules.begin(); moduleIt != theMaskAtModules.end();
       ++moduleIt) {
    // loop variables
    LASGlobalLoop moduleLoop;
    int det, beam, disk, pos;

    // this will calculate the corrections from the alignment parameters
    LASAlignmentTubeAlgorithm atAlgorithm;

    // find the location of the respective module in the container with these loops:

    // first TIB+TOB
    det = 2;
    beam = 0;
    pos = 0;
    do {
      // here we got it
      if (detectorId.GetTIBTOBEntry(det, beam, pos) == *moduleIt) {
        // the nominal phi value for this module
        const double nominalPhi = nominalCoordinates.GetTIBTOBEntry(det, beam, pos).GetPhi();

        // the offset from the alignment parameters
        const double phiCorrection =
            atAlgorithm.GetTIBTOBAlignmentParameterCorrection(det, beam, pos, nominalCoordinates, atParameters);

        // apply the corrections
        measuredCoordinates.GetTIBTOBEntry(det, beam, pos).SetPhi(nominalPhi - phiCorrection);
      }

    } while (moduleLoop.TIBTOBLoop(det, beam, pos));

    // then TEC(AT)
    det = 0;
    beam = 0;
    disk = 0;
    do {
      // here we got it
      if (detectorId.GetTEC2TECEntry(det, beam, disk) == *moduleIt) {
        // the nominal phi value for this module
        const double nominalPhi = nominalCoordinates.GetTEC2TECEntry(det, beam, disk).GetPhi();

        // the offset from the alignment parameters
        const double phiCorrection =
            atAlgorithm.GetTEC2TECAlignmentParameterCorrection(det, beam, disk, nominalCoordinates, atParameters);

        // apply the corrections
        measuredCoordinates.GetTEC2TECEntry(det, beam, disk).SetPhi(nominalPhi - phiCorrection);
      }

    } while (moduleLoop.TEC2TECLoop(det, beam, disk));
  }
}

///
/// this function is for debugging and testing only
/// and will disappear..
///
void LaserAlignment::testRoutine(void) {
  // tracker geom. object for calculating the global beam positions
  const TrackerGeometry& theTracker(*theTrackerGeometry);

  const double atPhiPositions[8] = {0.392699, 1.289799, 1.851794, 2.748894, 3.645995, 4.319690, 5.216791, 5.778784};
  const double tecPhiPositions[8] = {0.392699, 1.178097, 1.963495, 2.748894, 3.534292, 4.319690, 5.105088, 5.890486};
  const double zPositions[9] = {125.0, 139.0, 153.0, 167.0, 181.0, 198.5, 217.5, 238.0, 259.5};
  const double zPositionsTIB[6] = {62.0, 38.0, 18.0, -10.0, -34.0, -54.0};
  const double zPositionsTOB[6] = {104.0, 58.0, 22.0, -14.0, -50.0, -86.0};

  int det, beam, disk, pos, ring;

  // loop TEC+- internal
  det = 0;
  ring = 0;
  beam = 0;
  disk = 0;
  do {
    const double radius = ring ? 84.0 : 56.4;

    // access the tracker geometry for this module
    const DetId theDetId(detectorId.GetTECEntry(det, ring, beam, disk));
    const StripGeomDetUnit* const theStripDet = dynamic_cast<const StripGeomDetUnit*>(theTracker.idToDet(theDetId));

    if (theStripDet) {
      const GlobalPoint gp(GlobalPoint::Cylindrical(radius, tecPhiPositions[beam], zPositions[disk]));

      const LocalPoint lp(theStripDet->surface().toLocal(gp));
      std::cout << "__TEC: " << 256. - theStripDet->specificTopology().strip(lp)
                << std::endl;  /////////////////////////////////
    }

  } while (moduleLoop.TECLoop(det, ring, beam, disk));

  // loop TIBTOB
  det = 2;
  beam = 0;
  pos = 0;
  do {
    const double radius =
        (det == 2 ? 51.4 : 58.4);  /////////////////////////////////////////////////////////////////////////////
    const double theZ = (det == 2 ? zPositionsTIB[pos] : zPositionsTOB[pos]);

    // access the tracker geometry for this module
    const DetId theDetId(detectorId.GetTIBTOBEntry(det, beam, pos));
    const StripGeomDetUnit* const theStripDet = dynamic_cast<const StripGeomDetUnit*>(theTracker.idToDet(theDetId));

    if (theStripDet) {
      const GlobalPoint gp(GlobalPoint::Cylindrical(radius, atPhiPositions[beam], theZ));

      const LocalPoint lp(theStripDet->surface().toLocal(gp));
      std::cout << "__TIBTOB det " << det << " beam " << beam << " pos " << pos << "  "
                << 256. - theStripDet->specificTopology().strip(lp);
      std::cout << "           " << theStripDet->position().perp() << std::endl;  /////////////////////////////////
    }

  } while (moduleLoop.TIBTOBLoop(det, beam, pos));

  // loop TEC2TEC
  det = 0;
  beam = 0;
  disk = 0;
  do {
    const double radius = 56.4;

    // access the tracker geometry for this module
    const DetId theDetId(detectorId.GetTEC2TECEntry(det, beam, disk));
    const StripGeomDetUnit* const theStripDet = dynamic_cast<const StripGeomDetUnit*>(theTracker.idToDet(theDetId));

    if (theStripDet) {
      const GlobalPoint gp(GlobalPoint::Cylindrical(radius, atPhiPositions[beam], zPositions[disk]));

      const LocalPoint lp(theStripDet->surface().toLocal(gp));
      std::cout << "__TEC2TEC det " << det << " beam " << beam << " disk " << disk << "  "
                << 256. - theStripDet->specificTopology().strip(lp) << std::endl;  /////////////////////////////////
    }

  } while (moduleLoop.TEC2TECLoop(det, beam, disk));
}

// define the SEAL module
#include "FWCore/Framework/interface/MakerMacros.h"

DEFINE_FWK_MODULE(LaserAlignment);

// the ATTIC