CaloGeometryAnalyzer

CenterOrCorner

XorYorZ

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

#include "FWCore/Framework/interface/one/EDAnalyzer.h"

#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Utilities/interface/ESGetToken.h"

#include "DataFormats/HcalDetId/interface/HcalCastorDetId.h"
#include "DataFormats/HcalDetId/interface/HcalZDCDetId.h"
#include "DataFormats/HcalDetId/interface/HcalDetId.h"

#include "DataFormats/Math/interface/GeantUnits.h"

#include "Geometry/EcalAlgo/interface/EcalBarrelGeometry.h"
#include "Geometry/EcalAlgo/interface/EcalEndcapGeometry.h"
#include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
#include "Geometry/CaloGeometry/interface/CaloGeometry.h"
#include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
#include "Geometry/CaloGeometry/interface/TruncatedPyramid.h"
#include "Geometry/EcalAlgo/interface/EcalPreshowerGeometry.h"
#include "Geometry/Records/interface/CaloGeometryRecord.h"
#include "Geometry/CaloGeometry/interface/CaloGenericDetId.h"
#include "Geometry/CaloTopology/interface/HcalTopology.h"
#include "Geometry/CaloTopology/interface/CaloSubdetectorTopology.h"

#include "Geometry/HcalTowerAlgo/interface/HcalGeometry.h"

#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "CommonTools/UtilAlgos/interface/TFileService.h"

#include <fstream>
#include <iomanip>
#include <iterator>
#include <sstream>
#include "TH1.h"
#include "TH1D.h"
#include "TProfile.h"

using namespace geant_units;
using namespace geant_units::operators;

class CaloGeometryAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
  enum CenterOrCorner { kCenter, kCorner };
  enum XorYorZ { kX, kY, kZ };

public:
  explicit CaloGeometryAnalyzer(const edm::ParameterSet&);
  ~CaloGeometryAnalyzer() override;

  void beginJob() override {}
  void analyze(edm::Event const& iEvent, edm::EventSetup const&) override;
  void endJob() override {}

private:
  // ----------member data ---------------------------
  void build(const CaloGeometry* cg,
             const HcalTopology& ht,
             DetId::Detector det,
             int subdetn,
             const char* name,
             unsigned int histi);

  void buildHcal(const CaloGeometry* cg,
                 const HcalTopology& ht,
                 DetId::Detector det,
                 int subdetn,
                 const char* name,
                 unsigned int histi);

  void ctrcor(const DetId& did,
              const CaloCellGeometry& cell,
              std::fstream& fCtr,
              std::fstream& fCor,
              std::fstream& oldCtr,
              std::fstream& oldCor,
              unsigned int histi);

  void checkDiff(int i1, int i2, int i3, CenterOrCorner iCtrCor, XorYorZ iXYZ, double diff);
  int pass_;

  EEDetId gid(unsigned int ix, unsigned int iy, unsigned int iz, const EEDetId& did) const;

  void cmpset(const CaloSubdetectorGeometry* geom, const GlobalPoint& gp, const double dR);

  void ovrTst(const CaloGeometry* cg, const CaloSubdetectorGeometry* geom, const EEDetId& id, std::fstream& fOvr);

  void ovrTst(const CaloGeometry* cg, const CaloSubdetectorGeometry* geom, const EBDetId& id, std::fstream& fOvr);

  edm::ESGetToken<CaloGeometry, CaloGeometryRecord> geometryToken_;
  edm::ESGetToken<HcalTopology, HcalRecNumberingRecord> topologyToken_;

  edm::Service<TFileService> h_fs;

  TProfile* h_dPhi[7];
  TProfile* h_dPhiR[7];

  TProfile* h_dEta[7];
  TProfile* h_dEtaR[7];

  TProfile* h_eta;
  TProfile* h_phi;

  TH1D* h_diffs[10][12];

  TH1D* h_scindex;

  bool m_allOK;
};

CaloGeometryAnalyzer::CaloGeometryAnalyzer(const edm::ParameterSet& /*iConfig*/)
    : geometryToken_{esConsumes<CaloGeometry, CaloGeometryRecord>(edm::ESInputTag{})},
      topologyToken_{esConsumes<HcalTopology, HcalRecNumberingRecord>(edm::ESInputTag{})} {
  usesResource("TFileService");

  pass_ = 0;

  h_dPhi[0] = h_fs->make<TProfile>("dPhi:EB:index", "EB: dPhi vs index", 61200, -0.5, 61199.5, " ");
  h_dPhiR[0] = h_fs->make<TProfile>("dPhi:EB:R", "EB: dPhi vs R", 100, 125, 135, " ");

  h_dEta[0] = h_fs->make<TProfile>("dEta:EB:index", "EB: dEta vs index", 61200, -0.5, 61199.5, " ");
  h_dEtaR[0] = h_fs->make<TProfile>("dEta:EB:R", "EB: dEta vs R", 100, 125, 135, " ");

  h_dPhi[1] = h_fs->make<TProfile>("dPhi:EE:index", "EE: dPhi vs index", 14648, -0.5, 14647.5, " ");
  h_dPhiR[1] = h_fs->make<TProfile>("dPhi:EE:R", "EE: dPhi vs R", 130, 30, 160, " ");

  h_dEta[1] = h_fs->make<TProfile>("dEta:EE:index", "EE: dEta vs index", 14648, -0.5, 14647.5, " ");
  h_dEtaR[1] = h_fs->make<TProfile>("dEta:EE:R", "EE: dEta vs R", 130, 30, 160, " ");

  h_dPhi[2] = h_fs->make<TProfile>("dPhi:ES:index", "ES: dPhi vs index", 137216, -0.5, 137215.5, " ");
  h_dPhiR[2] = h_fs->make<TProfile>("dPhi:ES:R", "ES: dPhi vs R", 90, 40, 130, " ");

  h_dEta[2] = h_fs->make<TProfile>("dEta:ES:index", "ES: dEta vs index", 137216, -0.5, 137215.5, " ");
  h_dEtaR[2] = h_fs->make<TProfile>("dEta:ES:R", "ES: dEta vs R", 90, 40, 130, " ");

  h_dPhi[3] = h_fs->make<TProfile>("dPhi:HC:index", "HC: dPhi vs index", 9072, -0.5, 9071.5, " ");
  h_dPhiR[3] = h_fs->make<TProfile>("dPhi:HC:R", "HC: dPhi vs R", 400, 0, 400, " ");

  h_dEta[3] = h_fs->make<TProfile>("dEta:HC:index", "HC: dEta vs index", 9072, -0.5, 9071.5, " ");
  h_dEtaR[3] = h_fs->make<TProfile>("dEta:HC:R", "HC: dEta vs R", 400, 0, 400, " ");

  h_dPhi[4] = h_fs->make<TProfile>("dPhi:ZD:index", "ZD: dPhi vs index", 22, -0.5, 21.5, " ");
  h_dPhiR[4] = h_fs->make<TProfile>("dPhi:ZD:R", "ZD: dPhi vs R", 100, 0, 10, " ");

  h_dEta[4] = h_fs->make<TProfile>("dEta:ZD:index", "ZD: dEta vs index", 22, -0.5, 21.5, " ");
  h_dEtaR[4] = h_fs->make<TProfile>("dEta:ZD:R", "ZD: dEta vs R", 100, 0, 10, " ");

  h_dPhi[5] = h_fs->make<TProfile>("dPhi:CA:index", "CA: dPhi vs index", 224, -0.5, 223.5, " ");
  h_dPhiR[5] = h_fs->make<TProfile>("dPhi:CA:R", "CA: dPhi vs R", 100, 0, 20, " ");

  h_dEta[5] = h_fs->make<TProfile>("dEta:CA:index", "CA: dEta vs index", 224, -0.5, 223.5, " ");
  h_dEtaR[5] = h_fs->make<TProfile>("dEta:CA:R", "CA: dEta vs R", 100, 0, 20, " ");

  h_dPhi[6] = h_fs->make<TProfile>("dPhi:CT:index", "CT: dPhi vs index", 4320, -0.5, 4319.5, " ");
  h_dPhiR[6] = h_fs->make<TProfile>("dPhi:CT:R", "CT: dPhi vs R", 150, 0, 150, " ");

  h_dEta[6] = h_fs->make<TProfile>("dEta:CT:index", "CT: dEta vs index", 4320, -0.5, 4319.5, " ");
  h_dEtaR[6] = h_fs->make<TProfile>("dEta:CT:R", "CT: dEta vs R", 150, 0, 150, " ");

  h_eta = h_fs->make<TProfile>("iEta", "Eta vs iEta", 86 * 2 * 4, -86, 86, " ");
  h_phi = h_fs->make<TProfile>("iPhi", "Phi vs iPhi", 360 * 4, 1, 361, " ");

  const std::string hname[10] = {"EB", "EE", "ES", "HB", "HO", "HE", "HF", "CT", "ZD", "CA"};
  const std::string cname[12] = {
      "XCtr", "YCtr", "ZCtr", "XCor0", "YCor0", "ZCor0", "XCor3", "YCor3", "ZCor3", "XCor6", "YCor6", "ZCor6"};

  for (unsigned int i(0); i != 10; ++i) {
    for (unsigned int j(0); j != 12; ++j) {
      h_diffs[i][j] =
          h_fs->make<TH1D>(std::string(hname[i] + cname[j] + std::string("Diff (microns)")).c_str(),
                           std::string(hname[i] + std::string(": New-Nom(") + cname[j] + std::string(")")).c_str(),
                           200,
                           -200.,
                           200.);
    }
  }
  h_scindex = h_fs->make<TH1D>(
      std::string("Supercrystal Hashed Index").c_str(), std::string("SC Hashed Index").c_str(), 632, -0.5, 631.5);
}

CaloGeometryAnalyzer::~CaloGeometryAnalyzer() {}

void CaloGeometryAnalyzer::cmpset(const CaloSubdetectorGeometry* geom, const GlobalPoint& gp, const double dR) {
  typedef CaloSubdetectorGeometry::DetIdSet DetSet;

  DetSet base = geom->CaloSubdetectorGeometry::getCells(gp, dR);
  DetSet over = geom->getCells(gp, dR);
  if (over == base) {
#ifdef EDM_ML_DEBUG
    edm::LogVerbatim("CaloGeom") << "getCells Test dR=" << dR << ", gp=" << gp << ", gp.eta=" << gp.eta()
                                 << ", gp.phi=" << gp.phi()
                                 << ": base and over are equal!\n ***************************\n ";
#endif
  } else {
    if (2 < std::abs((int)(base.size()) - (int)(over.size()))) {
      DetSet inBaseNotOver;
      DetSet inOverNotBase;
      std::set_difference(
          base.begin(), base.end(), over.begin(), over.end(), std::inserter(inBaseNotOver, inBaseNotOver.begin()));

      if (inBaseNotOver.empty()) {
        edm::LogVerbatim("CaloGeom") << "getCells Test dR=" << dR << ", gp=" << gp << ", gp.eta=" << gp.eta()
                                     << ", gp.phi=" << gp.phi() << ": No elements in base but not overload ";
      } else {
        edm::LogVerbatim("CaloGeom") << "Length of Base is " << base.size();
        edm::LogVerbatim("CaloGeom") << "Length of Over is " << over.size();
        edm::LogVerbatim("CaloGeom") << "There are " << inBaseNotOver.size() << " items in Base but not in Overload";

        for (const auto& iS : inBaseNotOver) {
          std::ostringstream st1;
          st1 << "getCells Test dR=" << dR << ", gp=" << gp << ": cell in base but not overload = ";
          if (iS.det() == DetId::Ecal && iS.subdetId() == EcalBarrel)
            st1 << EBDetId(iS);
          if (iS.det() == DetId::Ecal && iS.subdetId() == EcalEndcap)
            st1 << EEDetId(iS);
          if (iS.det() == DetId::Hcal)
            st1 << HcalDetId(iS);
          edm::LogVerbatim("CaloGeom") << st1.str();
        }
      }

      std::set_difference(
          over.begin(), over.end(), base.begin(), base.end(), std::inserter(inOverNotBase, inOverNotBase.begin()));

      if (inOverNotBase.empty()) {
        edm::LogVerbatim("CaloGeom") << "getCells Test dR=" << dR << ", gp=" << gp << ", gp.eta=" << gp.eta()
                                     << ", gp.phi=" << gp.phi() << ": No elements in overload but not base ";
      } else {
        edm::LogVerbatim("CaloGeom") << "Length of Base is " << base.size();
        edm::LogVerbatim("CaloGeom") << "Length of Over is " << over.size();
        edm::LogVerbatim("CaloGeom") << "There are " << inOverNotBase.size() << " items in Overload but not in Base";

        for (const auto& iS : inOverNotBase) {
          std::ostringstream st1;
          st1 << "getCells Test dR=" << dR << ", gp=" << gp << ": cell in overload but not base = ";
          if (iS.det() == DetId::Ecal && iS.subdetId() == EcalBarrel)
            st1 << EBDetId(iS);
          if (iS.det() == DetId::Ecal && iS.subdetId() == EcalEndcap)
            st1 << EEDetId(iS);
          if (iS.det() == DetId::Hcal)
            st1 << HcalDetId(iS);
          edm::LogVerbatim("CaloGeom") << st1.str();
        }
      }
      edm::LogVerbatim("CaloGeom") << "------------- done with mismatch printout ---------------";
    }
  }
}

EEDetId CaloGeometryAnalyzer::gid(unsigned int ix, unsigned int iy, unsigned int iz, const EEDetId& did) const {
  return (EEDetId::validDetId(ix, iy, iz)
              ? EEDetId(ix, iy, iz)
              : (EEDetId::validDetId(ix + 1, iy, iz)
                     ? EEDetId(ix + 1, iy, iz)
                     : (EEDetId::validDetId(ix - 1, iy, iz)
                            ? EEDetId(ix - 1, iy, iz)
                            : (EEDetId::validDetId(ix, iy + 1, iz)
                                   ? EEDetId(ix, iy + 1, iz)
                                   : (EEDetId::validDetId(ix, iy - 1, iz)
                                          ? EEDetId(ix, iy - 1, iz)
                                          : (EEDetId::validDetId(ix + 1, iy + 1, iz)
                                                 ? EEDetId(ix + 1, iy + 1, iz)
                                                 : (EEDetId::validDetId(ix + 1, iy - 1, iz)
                                                        ? EEDetId(ix + 1, iy - 1, iz)
                                                        : (EEDetId::validDetId(ix - 1, iy + 1, iz)
                                                               ? EEDetId(ix - 1, iy + 1, iz)
                                                               : (EEDetId::validDetId(ix - 1, iy - 1, iz)
                                                                      ? EEDetId(ix - 1, iy - 1, iz)
                                                                      : did)))))))));
}

void CaloGeometryAnalyzer::ovrTst(const CaloGeometry* cg,
                                  const CaloSubdetectorGeometry* geom,
                                  const EEDetId& id,
                                  std::fstream& fOvr) {
  static const GlobalPoint origin(0, 0, 0);
  const int iphi(id.iPhiOuterRing());
  if (iphi != 0) {
    fOvr << "Barrel Neighbors of Endcap id = " << id << std::endl;
    const EcalEndcapGeometry* eeG(dynamic_cast<const EcalEndcapGeometry*>(geom));
    auto cell(geom->getGeometry(id));
    const CaloSubdetectorGeometry* bar(cg->getSubdetectorGeometry(DetId::Ecal, EcalBarrel));
    const EcalEndcapGeometry::OrderedListOfEBDetId* ol(eeG->getClosestBarrelCells(id));
    // assert ( nullptr != ol ) ;
    if (ol != nullptr) {
      for (unsigned int i(0); i != ol->size(); ++i) {
        fOvr << "           " << i << "  " << (*ol)[i];
        auto other(bar->getGeometry((*ol)[i]));
        const GlobalVector cv(cell->getPosition() - origin);
        const GlobalVector ov(other->getPosition() - origin);
        const double cosang(cv.dot(ov) / (cv.mag() * ov.mag()));
        const double angle(convertRadToDeg(acos(std::abs(cosang) < 1. ? cosang : 1.)));
        fOvr << ", angle = " << angle << std::endl;
      }
    }
  }
}

void CaloGeometryAnalyzer::ovrTst(const CaloGeometry* cg,
                                  const CaloSubdetectorGeometry* geom,
                                  const EBDetId& id,
                                  std::fstream& fOvr) {
  static const GlobalPoint origin(0, 0, 0);
  const int ieta(id.ieta());
  if (85 == std::abs(ieta)) {
    const EcalBarrelGeometry* ebG(dynamic_cast<const EcalBarrelGeometry*>(geom));
    auto cell(geom->getGeometry(id));
    const CaloSubdetectorGeometry* ecap(cg->getSubdetectorGeometry(DetId::Ecal, EcalEndcap));
    fOvr << "Endcap Neighbors of Barrel id = " << id << std::endl;
    const EcalBarrelGeometry::OrderedListOfEEDetId* ol(ebG->getClosestEndcapCells(id));
    // assert ( nullptr != ol ) ;
    if (ol != nullptr) {
      for (unsigned int i(0); i != ol->size(); ++i) {
        fOvr << "           " << i << "  " << (*ol)[i];
        auto other(ecap->getGeometry((*ol)[i]));
        const GlobalVector cv(cell->getPosition() - origin);
        const GlobalVector ov(other->getPosition() - origin);
        const double cosang(cv.dot(ov) / (cv.mag() * ov.mag()));
        const double angle(convertRadToDeg(acos(std::abs(cosang) < 1. ? cosang : 1.)));
        fOvr << ", angle = " << angle << std::endl;
      }
    } else {
      fOvr << "endcap ecal ptr is null " << std::endl;
    }
  }
}

void CaloGeometryAnalyzer::checkDiff(int i1, int i2, int i3, CenterOrCorner iCtrCor, XorYorZ iXYZ, double diff) {
  if (3.5 < fabs(diff)) {
    edm::LogVerbatim("CaloGeom") << "For a volume " << (kCenter == iCtrCor ? "CENTER" : "CORNER") << ", & "
                                 << "i1=" << i1 << " & i2=" << i2 << " & i3=" << i3 << ", ***BIG DISAGREEMENT FOUND. D"
                                 << (kX == iXYZ ? "X" : (kY == iXYZ ? "Y" : "Z")) << "=" << diff << " microns";
    m_allOK = false;
  }
}

void CaloGeometryAnalyzer::ctrcor(const DetId& did,
                                  const CaloCellGeometry& cell,
                                  std::fstream& fCtr,
                                  std::fstream& fCor,
                                  std::fstream& oldCtr,
                                  std::fstream& oldCor,
                                  unsigned int histi) {
  int oldie(0);
  int oldip(0);
  oldCtr >> oldie >> oldip;
  oldCor >> oldie >> oldip;
  const CaloGenericDetId cgid(did);
  if (cgid.isEB()) {
    const EBDetId ebid(did);
    const int ie(ebid.ieta());
    const int ip(ebid.iphi());
    fCtr << std::setw(4) << ie << std::setw(4) << ip;
    fCor << std::setw(4) << ie << std::setw(4) << ip;
  }
  if (cgid.isEE()) {
    const EEDetId eeid(did);
    const int ix(eeid.ix());
    const int iy(eeid.iy());

    fCtr << std::setw(4) << ix << std::setw(4) << iy;
    fCor << std::setw(4) << ix << std::setw(4) << iy;
  }
  if (cgid.isES()) {
    const ESDetId esid(did);
    const int pl(esid.plane());
    const int ix(esid.six());
    const int iy(esid.siy());
    const int st(esid.strip());
    fCtr << std::setw(4) << pl << std::setw(4) << ix << std::setw(4) << iy << std::setw(4) << st;
    fCor << std::setw(4) << pl << std::setw(4) << ix << std::setw(4) << iy << std::setw(4) << st;
    int oldiy, oldst;
    oldCtr >> oldiy >> oldst;
    oldCor >> oldip >> oldst;
  }
  int depth = 0;
  if (cgid.det() == DetId::Hcal) {
    const HcalDetId hcid(did);
    const int ie(hcid.ieta());
    const int ip(hcid.iphi());
    const int de(hcid.depth());
    fCtr << std::setw(4) << ie << std::setw(4) << ip << std::setw(4) << de;
    fCor << std::setw(4) << ie << std::setw(4) << ip << std::setw(4) << de;
    int oldde;
    oldCtr >> oldde;
    oldCor >> oldde;
    depth = de;
  }
  if (cgid.isZDC()) {
    const HcalZDCDetId zcid(did);
    const int is(zcid.section());
    const int ic(zcid.channel());
    fCtr << std::setw(4) << is << std::setw(4) << ic;
    fCor << std::setw(4) << is << std::setw(4) << ic;
  }
  if (cgid.isCastor()) {
    const HcalCastorDetId cid(did);
    const int is(cid.sector());
    const int im(cid.module());
    fCtr << std::setw(4) << is << std::setw(4) << im;
    fCor << std::setw(4) << is << std::setw(4) << im;
  }
  if (cgid.isCaloTower()) {
    const CaloTowerDetId cid(did);
    const int ie(cid.ieta());
    const int ip(cid.iphi());
    fCtr << std::setw(4) << ie << std::setw(4) << ip;
    fCor << std::setw(4) << ie << std::setw(4) << ip;
  }

  const double x(cell.getPosition().x());
  const double y(cell.getPosition().y());
  const double z(cell.getPosition().z());

  double oldx, oldy, oldz;

  oldCtr >> oldx >> oldy >> oldz;

  const double dx(1.e4 * (x - oldx));
  const double dy(1.e4 * (y - oldy));
  const double dz(1.e4 * (z - oldz));

  h_diffs[histi][0]->Fill(dx);
  h_diffs[histi][1]->Fill(dy);
  h_diffs[histi][2]->Fill(dz);

  checkDiff(oldie, oldip, depth, kCenter, kX, dx);
  checkDiff(oldie, oldip, depth, kCenter, kY, dy);
  checkDiff(oldie, oldip, depth, kCenter, kZ, dz);

  fCtr << std::fixed << std::setw(12) << std::setprecision(4) << x << std::fixed << std::setw(12)
       << std::setprecision(4) << y << std::fixed << std::setw(12) << std::setprecision(4) << z << std::endl;

  const CaloCellGeometry::CornersVec& co(cell.getCorners());

  for (unsigned int j(0); j < co.size(); ++(++(++j))) {
    const double x(co[j].x());
    const double y(co[j].y());
    const double z(co[j].z());

    double oldx, oldy, oldz;

    oldCor >> oldx >> oldy >> oldz;

    const double dx(1.e4 * (x - oldx));
    const double dy(1.e4 * (y - oldy));
    const double dz(1.e4 * (z - oldz));

    h_diffs[histi][j + 3]->Fill(dx);
    h_diffs[histi][j + 4]->Fill(dy);
    h_diffs[histi][j + 5]->Fill(dz);

    checkDiff(oldie, oldip, j, kCorner, kX, dx);
    checkDiff(oldie, oldip, j, kCorner, kY, dy);
    checkDiff(oldie, oldip, j, kCorner, kZ, dz);

    fCor << std::fixed << std::setw(12) << std::setprecision(4) << x << std::fixed << std::setw(12)
         << std::setprecision(4) << y << std::fixed << std::setw(12) << std::setprecision(4) << z;
  }
  fCor << std::endl;
}

void CaloGeometryAnalyzer::buildHcal(const CaloGeometry* cg,
                                     const HcalTopology& ht,
                                     DetId::Detector det,
                                     int subdetn,
                                     const char* name,
                                     unsigned int histi) {
  edm::LogVerbatim("CaloGeom") << "Now checking detector " << name;
  const std::string oldnameCtr("old" + std::string(name) + ".ctr");
  const std::string oldnameCor("old" + std::string(name) + ".cor");
  const std::string fnameCtr(std::string(name) + ".ctr");
  const std::string fnameCor(std::string(name) + ".cor");
  const std::string fnameOvr(std::string(name) + ".ovr");
  const std::string fnameRoot(std::string(name) + ".C");
  std::fstream oldCtr(oldnameCtr.c_str(), std::ios_base::in);
  std::fstream oldCor(oldnameCor.c_str(), std::ios_base::in);
  std::fstream fCtr(fnameCtr.c_str(), std::ios_base::out);
  std::fstream fCor(fnameCor.c_str(), std::ios_base::out);
  std::fstream fOvr(fnameOvr.c_str(), std::ios_base::out);
  std::fstream f(fnameRoot.c_str(), std::ios_base::out);

  const CaloSubdetectorGeometry* geom(cg->getSubdetectorGeometry(det, subdetn));

  f << "{" << std::endl;
  f << "  TGeoManager* geoManager = new TGeoManager(\"ROOT\", \"" << name << "\");" << std::endl;
  f << "  TGeoMaterial* dummyMaterial = new TGeoMaterial(\"Vacuum\", 0,0,0); " << std::endl;
  f << "  TGeoMedium* dummyMedium =  new TGeoMedium(\"Vacuum\",1,dummyMaterial);" << std::endl;
  f << "  TGeoVolume* world=geoManager->MakeBox(\"world\",dummyMedium, 8000.0, 8000.0, 14000.0); " << std::endl;
  f << "  geoManager->SetTopVolume(world); " << std::endl;
  f << "  TGeoVolume* box; " << std::endl;
  int n = 0;
  const std::vector<DetId>& ids(geom->getValidDetIds(det, subdetn));

  const std::vector<DetId>& ids2(cg->getValidDetIds(det, subdetn));

  if (ids != ids2) {
    edm::LogVerbatim("CaloGeom") << "Methods differ! One gives size " << ids.size() << " and the other gives size "
                                 << ids2.size();
  }
  assert(ids == ids2);

  for (const auto& i : ids) {
    ++n;
    auto cell = (geom->getGeometry(i));

    assert(cg->present(i));

    ctrcor(i, *cell, fCtr, fCor, oldCtr, oldCor, histi);

    const DetId id(i);

    const HcalDetId hcId(i);

    const GlobalPoint pos(cell->getPosition());
    const double posmag(pos.mag());

    const double disin(DetId::Ecal == det && EcalPreshower == subdetn ? 0.000001 : 0.001);

    const GlobalPoint pointIn(
        pos.x() + disin * pos.x() / posmag, pos.y() + disin * pos.y() / posmag, pos.z() + disin * pos.z() / posmag);
    const GlobalPoint pointFr(
        pos.x() - 0.1 * pos.x() / posmag, pos.y() - 0.1 * pos.y() / posmag, pos.z() - 0.1 * pos.z() / posmag);

    if (cell->inside(pointFr))
      edm::LogVerbatim("CaloGeom") << "Bad outside: " << pointIn << ", " << pointFr;
    assert(cell->inside(pointIn));
    assert(!cell->inside(pointFr));

    const double deltaPhi(geom->deltaPhi(id));

    const double deltaEta(geom->deltaEta(id));

    const unsigned int detIndex(3);
    const GlobalPoint ggp(cell->getPosition());

    h_dPhi[detIndex]->Fill(ht.detId2denseId(hcId), deltaPhi);
    h_dPhiR[detIndex]->Fill(ggp.perp(), deltaPhi);

    h_dEta[detIndex]->Fill(ht.detId2denseId(hcId), deltaEta);
    h_dEtaR[detIndex]->Fill(ggp.perp(), deltaEta);

    const unsigned int i1(HcalGeometry::alignmentTransformIndexLocal(hcId));

    const DetId d1(HcalGeometry::detIdFromLocalAlignmentIndex(i1));

    const unsigned int i2(HcalGeometry::alignmentTransformIndexLocal(d1));

    assert(i1 == i2);

    f << "  // " << HcalDetId(i) << std::endl;

    const GlobalPoint gp(cell->getPosition());
    f << "  // Checking getClosestCell for position " << gp << std::endl;

    HcalDetId closestCell(geom->getClosestCell(gp));

    f << "  // Return position is " << closestCell << std::endl;
    if (closestCell != HcalDetId(i)) {
      const double rr(reco::deltaR(gp.eta(),
                                   gp.phi(),
                                   geom->getGeometry(closestCell)->getPosition().eta(),
                                   geom->getGeometry(closestCell)->getPosition().phi()));
      if (rr > 1.e-5)
        edm::LogVerbatim("CaloGeom") << "For " << HcalDetId(i) << " closest is " << closestCell << " HCAL dR=" << rr;
    }
    // test getCells against base class version every so often
    if (0 == ht.detId2denseId(closestCell) % 30) {
      cmpset(geom, gp, 2._deg);
      cmpset(geom, gp, 5._deg);
      cmpset(geom, gp, 7._deg);
      cmpset(geom, gp, 25._deg);
      cmpset(geom, gp, 45._deg);
    }

    if (det == DetId::Hcal && subdetn == HcalForward)
      f << "  box=geoManager->MakeBox(\"point\",dummyMedium,1.0,1.0,1.0);" << std::endl;
    else
      f << "  box=geoManager->MakeBox(\"point\",dummyMedium,3.0,3.0,3.0);" << std::endl;
    f << "  world->AddNode(box," << n << ",new TGeoHMatrix(TGeoTranslation(" << cell->getPosition().x() << ","
      << cell->getPosition().y() << "," << cell->getPosition().z() << ")));" << std::endl;
  }

  f << "  geoManager->CloseGeometry();" << std::endl;
  f << "world->Voxelize(\"\"); // now the new geometry is valid for tracking, so you can do \n // even raytracing \n "
       "//  if (!canvas) { \n    TCanvas* canvas=new TCanvas(\"EvtDisp\",\"EvtDisp\",500,500); \n //  } \n  "
       "canvas->Modified(); \n  canvas->Update();      \n  world->Draw(); \n";
  f << "}" << std::endl;
  f.close();
  fCtr.close();
  fCor.close();
}

void CaloGeometryAnalyzer::build(const CaloGeometry* cg,
                                 const HcalTopology& ht,
                                 DetId::Detector det,
                                 int subdetn,
                                 const char* name,
                                 unsigned int histi) {
  edm::LogVerbatim("CaloGeom") << "Now checking detector " << name;

  const std::string oldnameCtr("old" + std::string(name) + ".ctr");
  const std::string oldnameCor("old" + std::string(name) + ".cor");
  const std::string fnameCtr(std::string(name) + ".ctr");
  const std::string fnameCor(std::string(name) + ".cor");
  const std::string fnameOvr(std::string(name) + ".ovr");
  const std::string fnameRoot(std::string(name) + ".C");
  std::fstream oldCtr(oldnameCtr.c_str(), std::ios_base::in);
  std::fstream oldCor(oldnameCor.c_str(), std::ios_base::in);
  std::fstream fCtr(fnameCtr.c_str(), std::ios_base::out);
  std::fstream fCor(fnameCor.c_str(), std::ios_base::out);
  std::fstream fOvr(fnameOvr.c_str(), std::ios_base::out);
  std::fstream f(fnameRoot.c_str(), std::ios_base::out);

  const CaloSubdetectorGeometry* geom(cg->getSubdetectorGeometry(det, subdetn));

  f << "{" << std::endl;
  f << "  TGeoManager* geoManager = new TGeoManager(\"ROOT\", \"" << name << "\");" << std::endl;
  f << "  TGeoMaterial* dummyMaterial = new TGeoMaterial(\"Vacuum\", 0,0,0); " << std::endl;
  f << "  TGeoMedium* dummyMedium =  new TGeoMedium(\"Vacuum\",1,dummyMaterial);" << std::endl;
  f << "  TGeoVolume* world=geoManager->MakeBox(\"world\",dummyMedium, 8000.0, 8000.0, 14000.0); " << std::endl;
  f << "  geoManager->SetTopVolume(world); " << std::endl;
  f << "  TGeoVolume* box; " << std::endl;
  int n = 0;
  const std::vector<DetId>& ids(geom->getValidDetIds(det, subdetn));

  const std::vector<DetId>& ids2(cg->getValidDetIds(det, subdetn));

  if (ids != ids2) {
    edm::LogVerbatim("CaloGeom") << "Methods differ! One gives size " << ids.size() << " and the other gives size "
                                 << ids2.size();
  }

  assert(ids == ids2);

  for (const auto& i : ids) {
    ++n;
    auto cell(geom->getGeometry(i));

    assert(cg->present(i));

    ctrcor(i, *cell, fCtr, fCor, oldCtr, oldCor, histi);

    const DetId id(i);

    const CaloGenericDetId cid(id);

    // assert(cid.validDetId());
    // This line fails for CaloTower because this method does not support CaloTower

    // assert(CaloGenericDetId(id.det(), id.subdetId(), cid.denseIndex()) == id);
    // This line fails for CaloTower because this three-parameter constructor does not support CaloTower

    const GlobalPoint pos(cell->getPosition());
    const double posmag(pos.mag());

    const double disin(DetId::Ecal == det && EcalPreshower == subdetn ? 0.000001 : 0.001);

    const GlobalPoint pointIn(
        pos.x() + disin * pos.x() / posmag, pos.y() + disin * pos.y() / posmag, pos.z() + disin * pos.z() / posmag);
    const GlobalPoint pointFr(
        pos.x() - 0.1 * pos.x() / posmag, pos.y() - 0.1 * pos.y() / posmag, pos.z() - 0.1 * pos.z() / posmag);

    if (cell->inside(pointFr))
      edm::LogVerbatim("CaloGeom") << "Bad outside: " << pointIn << ", " << pointFr;
    assert(cell->inside(pointIn));
    assert(!cell->inside(pointFr));

    const double deltaPhi(geom->deltaPhi(id));

    const double deltaEta(geom->deltaEta(id));

    const unsigned int detIndex(
        DetId::Ecal == det && EcalBarrel == subdetn
            ? 0
            : (DetId::Ecal == det && EcalEndcap == subdetn
                   ? 1
                   : (DetId::Ecal == det && EcalPreshower == subdetn
                          ? 2
                          : (DetId::Hcal == det
                                 ? 3
                                 : (DetId::Calo == det && HcalZDCDetId::SubdetectorId == subdetn
                                        ? 4
                                        : (DetId::Calo == det && HcalCastorDetId::SubdetectorId == subdetn ? 5 : 6))))));

    const CaloGenericDetId cgid(id);

    if (cgid.isCaloTower() == false) {  // CaloGenericDetId::denseIndex() not supported for CaloTower
      const GlobalPoint ggp(cell->getPosition());

      h_dPhi[detIndex]->Fill(cgid.denseIndex(), deltaPhi);
      h_dPhiR[detIndex]->Fill(ggp.perp(), deltaPhi);

      h_dEta[detIndex]->Fill(cgid.denseIndex(), deltaEta);
      h_dEtaR[detIndex]->Fill(ggp.perp(), deltaEta);
    }

    if (det == DetId::Ecal) {
      if (subdetn == EcalBarrel) {
        f << "  // " << EBDetId(i) << std::endl;

        const GlobalPoint gp(cell->getPosition(0.));

        f << "  // Checking getClosestCell for position " << gp << std::endl;

        const EBDetId ebid(id);
        for (unsigned int j(0); j != 4; ++j) {
          const CaloCellGeometry::CornersVec& corn(cell->getCorners());
          h_eta->Fill(ebid.ieta() * 1. + 0.25 * j, corn[j].eta());
          if (ebid.ieta() > 0)
            h_phi->Fill(ebid.iphi() * 1. + 0.25 * j, corn[j].phi());
        }

        EBDetId closestCell(geom->getClosestCell(gp));

        f << "  // Return position is " << closestCell << std::endl;
        assert(closestCell == EBDetId(i));
        // test getCells against base class version every so often
        if (0 == closestCell.hashedIndex() % 100) {
          cmpset(geom, gp, 2._deg);
          cmpset(geom, gp, 5._deg);
          cmpset(geom, gp, 25._deg);
          cmpset(geom, gp, 45._deg);
        }

        ovrTst(cg, geom, EBDetId(i), fOvr);

        const unsigned int i1(EcalBarrelGeometry::alignmentTransformIndexLocal(ebid));

        const DetId d1(EcalBarrelGeometry::detIdFromLocalAlignmentIndex(i1));

        const unsigned int i2(EcalBarrelGeometry::alignmentTransformIndexLocal(d1));

        assert(i1 == i2);
      }
      if (subdetn == EcalEndcap) {
        const EEDetId did(i);
        const int ix(did.ix());
        const int iy(did.iy());
        const int iz(did.zside());

        const unsigned int i1(EcalEndcapGeometry::alignmentTransformIndexLocal(did));

        const DetId d1(EcalEndcapGeometry::detIdFromLocalAlignmentIndex(i1));

        const unsigned int i2(EcalEndcapGeometry::alignmentTransformIndexLocal(d1));

        assert(i1 == i2);

        f << "  // Checking getClosestCell for position " << cell->getPosition(0.) << std::endl;

        const GlobalPoint gp(cell->getPosition(0.));

        const EEDetId closestCell(geom->getClosestCell(gp));
        f << "  // Return position is " << closestCell << std::endl;

        assert(closestCell == did);
        // test getCells against base class version every so often
        if (0 == closestCell.hashedIndex() % 10) {
          cmpset(geom, gp, 2._deg);
          cmpset(geom, gp, 5._deg);
          cmpset(geom, gp, 25._deg);
          cmpset(geom, gp, 45._deg);
        }

        const GlobalVector xx(2.5, 0, 0);
        const GlobalVector yy(0, 2.5, 0);
        const GlobalVector zz(0, 0, 1);
        const GlobalPoint pointIn(cell->getPosition(1.));
        const GlobalPoint pointFr(cell->getPosition(-1.));
        const GlobalPoint pointBk(cell->getPosition(24.));
        const GlobalPoint pointXP(cell->getPosition(1.) + xx);
        const GlobalPoint pointXM(cell->getPosition(1.) - xx);
        const GlobalPoint pointYP(cell->getPosition(1.) + yy);
        const GlobalPoint pointYM(cell->getPosition(1.) - yy);
        const GlobalPoint pointPP(cell->getPosition(1.) + xx + yy);
        const GlobalPoint pointPM(cell->getPosition(1.) + xx - yy);
        const GlobalPoint pointMP(cell->getPosition(1.) - xx + yy);
        const GlobalPoint pointMM(cell->getPosition(1.) - xx - yy);
        const EEDetId didXP(gid(ix + 1, iy, iz, did));
        const EEDetId didXM(gid(ix - 1, iy, iz, did));
        const EEDetId didYP(gid(ix, iy + 1, iz, did));
        const EEDetId didYM(gid(ix, iy - 1, iz, did));
        const EEDetId didPP(gid(ix + 1, iy + 1, iz, did));
        const EEDetId didPM(gid(ix + 1, iy - 1, iz, did));
        const EEDetId didMP(gid(ix - 1, iy + 1, iz, did));
        const EEDetId didMM(gid(ix - 1, iy - 1, iz, did));

        assert(cell->inside(pointIn));
        assert(!cell->inside(pointFr));
        assert(!cell->inside(pointBk));
        assert(!cell->inside(pointXP));
        assert(!cell->inside(pointXM));
        assert(!cell->inside(pointYP));
        assert(!cell->inside(pointYM));
        assert(!cell->inside(pointPP));
        assert(!cell->inside(pointPM));
        assert(!cell->inside(pointMP));
        assert(!cell->inside(pointMM));

        const EEDetId ccBk(geom->getClosestCell(pointBk));
        const EEDetId ccIn(geom->getClosestCell(pointIn));
        const EEDetId ccFr(geom->getClosestCell(pointFr));
        const EEDetId ccXP(geom->getClosestCell(pointXP));
        const EEDetId ccXM(geom->getClosestCell(pointXM));
        const EEDetId ccYP(geom->getClosestCell(pointYP));
        const EEDetId ccYM(geom->getClosestCell(pointYM));
        const EEDetId ccPP(geom->getClosestCell(pointPP));
        const EEDetId ccPM(geom->getClosestCell(pointPM));
        const EEDetId ccMP(geom->getClosestCell(pointMP));
        const EEDetId ccMM(geom->getClosestCell(pointMM));

        assert(ccIn == did);
        assert(ccFr == did);
        assert(ccBk == did);
        assert(ccXP == didXP || !geom->getGeometry(didXP)->inside(pointXP));
        assert(ccXM == didXM || !geom->getGeometry(didXM)->inside(pointXM));
        assert(ccYP == didYP || !geom->getGeometry(didYP)->inside(pointYP));
        assert(ccYM == didYM || !geom->getGeometry(didYM)->inside(pointYM));
        assert(ccPP == didPP || !geom->getGeometry(didPP)->inside(pointPP));
        assert(ccPM == didPM || !geom->getGeometry(didPM)->inside(pointPM));
        assert(ccMP == didMP || !geom->getGeometry(didMP)->inside(pointMP));
        assert(ccMM == didMM || !geom->getGeometry(didMM)->inside(pointMM));

        ovrTst(cg, geom, EEDetId(i), fOvr);
      }
      if (subdetn == EcalPreshower) {
        const ESDetId esid(i);

        f << "  // " << esid << std::endl;
        f << "  // Checking getClosestCell for position " << cell->getPosition() << " in plane " << esid.plane()
          << std::endl;
        ESDetId closestCell = ESDetId((dynamic_cast<const EcalPreshowerGeometry*>(geom))
                                          ->getClosestCellInPlane(cell->getPosition(), esid.plane()));
        f << "  // Return position is " << closestCell << std::endl;
        //sanity checks
        int o_zside = esid.zside();

        assert((o_zside < 0 && cell->getPosition().z() < 0.) || (o_zside > 0 && cell->getPosition().z() > 0.));

        if (closestCell != esid)
          edm::LogVerbatim("CaloGeom") << "** esid=" << esid << ", closest=" << closestCell;

        const unsigned int i1(EcalPreshowerGeometry::alignmentTransformIndexLocal(esid));

        const DetId d1(EcalPreshowerGeometry::detIdFromLocalAlignmentIndex(i1));

        const unsigned int i2(EcalPreshowerGeometry::alignmentTransformIndexLocal(d1));

        assert(i1 == i2);
      }
    } else if (det == DetId::Hcal) {
      const HcalDetId hcId(i);

      const unsigned int i1(HcalGeometry::alignmentTransformIndexLocal(hcId));

      const DetId d1(HcalGeometry::detIdFromLocalAlignmentIndex(i1));

      const unsigned int i2(HcalGeometry::alignmentTransformIndexLocal(d1));

      assert(i1 == i2);

      f << "  // " << HcalDetId(i) << std::endl;

      const GlobalPoint& gp(cell->getPosition());

      f << "  // Checking getClosestCell for position " << gp << std::endl;

      const HcalDetId closestCell(geom->getClosestCell(gp));

      f << "  // Return position is " << closestCell << std::endl;
      if (closestCell != HcalDetId(i)) {
        const double rr(reco::deltaR(gp.eta(),
                                     gp.phi(),
                                     geom->getGeometry(closestCell)->getPosition().eta(),
                                     geom->getGeometry(closestCell)->getPosition().phi()));
        if (rr > 1.e-5)
          edm::LogVerbatim("CaloGeom") << "For " << HcalDetId(i) << " closest is " << closestCell << " HCAL dR=" << rr;
      }
      // test getCells against base class version every so often
      if (0 == ht.detId2denseId(closestCell) % 30) {
        cmpset(geom, gp, 2._deg);
        cmpset(geom, gp, 5._deg);
        cmpset(geom, gp, 7._deg);
        cmpset(geom, gp, 25._deg);
        cmpset(geom, gp, 45._deg);
      }
    } else if (det == DetId::Calo && subdetn == HcalCastorDetId::SubdetectorId) {
      f << "  // " << HcalCastorDetId(i) << std::endl;

      const GlobalPoint gp(cell->getPosition().x(), cell->getPosition().y(), cell->getPosition().z() - 0.1);

      f << "  // Checking getClosestCell for position " << gp << std::endl;

      const DetId closestCell(geom->getClosestCell(gp));

      if (closestCell != DetId(0)) {
        f << "  // Return position is " << HcalCastorDetId(closestCell) << std::endl;
        if (closestCell != HcalCastorDetId(i)) {
          const double rr(reco::deltaR(gp.eta(),
                                       gp.phi(),
                                       geom->getGeometry(closestCell)->getPosition().eta(),
                                       geom->getGeometry(closestCell)->getPosition().phi()));
          if (rr > 1.e-5)
            edm::LogVerbatim("CaloGeom") << "For " << HcalCastorDetId(i) << " closest is "
                                         << HcalCastorDetId(closestCell) << " dR=" << rr;
        }
      }
      // test getCells against base class version every so often
      // if( 0 == closestCell.denseIndex()%30 )
      {
        cmpset(geom, gp, 2._deg);
        cmpset(geom, gp, 5._deg);
        cmpset(geom, gp, 7._deg);
        cmpset(geom, gp, 25._deg);
        cmpset(geom, gp, 45._deg);
      }
    } else if (det == DetId::Calo && subdetn == HcalZDCDetId::SubdetectorId) {
      f << "  // " << HcalZDCDetId(i) << std::endl;
      const double sign(HcalZDCDetId(i).zside());
      const GlobalPoint gp(cell->getPosition().x(), cell->getPosition().y(), cell->getPosition().z() + sign * 0.1);

      f << "  // Checking getClosestCell for position " << gp << std::endl;

      const DetId closestCell(geom->getClosestCell(gp));

      if (closestCell != DetId(0)) {
        f << "  // Return position is " << HcalZDCDetId(closestCell) << std::endl;
        if (closestCell != HcalZDCDetId(i)) {
          const double rr(reco::deltaR(gp.eta(),
                                       gp.phi(),
                                       geom->getGeometry(closestCell)->getPosition().eta(),
                                       geom->getGeometry(closestCell)->getPosition().phi()));
          if (rr > 1.e-5)
            edm::LogVerbatim("CaloGeom") << "For " << HcalZDCDetId(i) << " closest is " << HcalZDCDetId(closestCell)
                                         << " dR=" << rr;
        }
      }
      // test getCells against base class version every so often
      // if( 0 == closestCell.denseIndex()%30 )
      {
        cmpset(geom, gp, 2._deg);
        cmpset(geom, gp, 5._deg);
        cmpset(geom, gp, 7._deg);
        cmpset(geom, gp, 25._deg);
        cmpset(geom, gp, 45._deg);
      }
    }

    if (det == DetId::Hcal && subdetn == HcalForward)
      f << "  box=geoManager->MakeBox(\"point\",dummyMedium,1.0,1.0,1.0);" << std::endl;
    else
      f << "  box=geoManager->MakeBox(\"point\",dummyMedium,3.0,3.0,3.0);" << std::endl;
    f << "  world->AddNode(box," << n << ",new TGeoHMatrix(TGeoTranslation(" << cell->getPosition().x() << ","
      << cell->getPosition().y() << "," << cell->getPosition().z() << ")));" << std::endl;
  }
  f << "  geoManager->CloseGeometry();" << std::endl;
  f << "world->Voxelize(\"\"); // now the new geometry is valid for tracking, so you can do \n // even raytracing \n "
       "//  if (!canvas) { \n    TCanvas* canvas=new TCanvas(\"EvtDisp\",\"EvtDisp\",500,500); \n //  } \n  "
       "canvas->Modified(); \n  canvas->Update();      \n  world->Draw(); \n";
  f << "}" << std::endl;
  f.close();
  fCtr.close();
  fCor.close();
}

void CaloGeometryAnalyzer::analyze(const edm::Event& /*iEvent*/, const edm::EventSetup& iSetup) {
  const auto& pG = iSetup.getData(geometryToken_);
  const CaloGeometry* cG = &pG;
  const auto& pT = iSetup.getData(topologyToken_);

  const std::vector<DetId> allDetId(pG.getValidDetIds());

  const std::vector<DetId>& deb(pG.getValidDetIds(DetId::Ecal, EcalBarrel));
  const std::vector<DetId>& dee(pG.getValidDetIds(DetId::Ecal, EcalEndcap));
  const std::vector<DetId>& des(pG.getValidDetIds(DetId::Ecal, EcalPreshower));
  const std::vector<DetId>& dhb(pG.getValidDetIds(DetId::Hcal, HcalBarrel));
  const std::vector<DetId>& dhe(pG.getValidDetIds(DetId::Hcal, HcalEndcap));
  const std::vector<DetId>& dho(pG.getValidDetIds(DetId::Hcal, HcalOuter));
  const std::vector<DetId>& dhf(pG.getValidDetIds(DetId::Hcal, HcalForward));
  const std::vector<DetId>& dct(pG.getValidDetIds(DetId::Calo, CaloTowerDetId::SubdetId));
  const std::vector<DetId>& dca(pG.getValidDetIds(DetId::Calo, HcalCastorDetId::SubdetectorId));
  const std::vector<DetId>& dzd(pG.getValidDetIds(DetId::Calo, HcalZDCDetId::SubdetectorId));

  const std::vector<DetId>& dha(pG.getSubdetectorGeometry(DetId::Hcal, 1)->getValidDetIds());

  const unsigned int sum(deb.size() + dee.size() + des.size() + dhb.size() + dhe.size() + dho.size() + dhf.size() +
                         dct.size() + dca.size() + dzd.size());

  if (sum != allDetId.size()) {
    edm::LogVerbatim("CaloGeom") << "Sums differ! One is " << allDetId.size() << " and the other is " << sum;
  }

  assert(sum == allDetId.size());

  assert(dha.size() == dhb.size() + dhe.size() + dho.size() + dhf.size());
  //
  // get the ecal & hcal geometry
  //
  if (pass_ == 0) {
    edm::LogVerbatim("CaloGeom") << "**Ecal Barrel avg Radius = "
                                 << dynamic_cast<const EcalBarrelGeometry*>(
                                        pG.getSubdetectorGeometry(DetId::Ecal, EcalBarrel))
                                        ->avgRadiusXYFrontFaceCenter();

    edm::LogVerbatim("CaloGeom") << "**Ecal Endcap avg Zabs = "
                                 << dynamic_cast<const EcalEndcapGeometry*>(
                                        pG.getSubdetectorGeometry(DetId::Ecal, EcalEndcap))
                                        ->avgAbsZFrontFaceCenter();

    m_allOK = true;

    build(cG, pT, DetId::Ecal, EcalBarrel, "eb", 0);
    build(cG, pT, DetId::Ecal, EcalEndcap, "ee", 1);
    build(cG, pT, DetId::Ecal, EcalPreshower, "es", 2);
    buildHcal(cG, pT, DetId::Hcal, HcalBarrel, "hb", 3);
    buildHcal(cG, pT, DetId::Hcal, HcalEndcap, "he", 4);
    buildHcal(cG, pT, DetId::Hcal, HcalOuter, "ho", 5);
    buildHcal(cG, pT, DetId::Hcal, HcalForward, "hf", 6);
    build(cG, pT, DetId::Calo, CaloTowerDetId::SubdetId, "ct", 7);
    // build(cG, pT, DetId::Calo, HcalCastorDetId::SubdetectorId, "ca", 8);
    // Castor has been removed from CMS
    build(cG, pT, DetId::Calo, HcalZDCDetId::SubdetectorId, "zd", 9);

    edm::LogVerbatim("CaloGeom") << "\n\n*********** Validation of cell centers and corners "
                                 << (m_allOK ? "SUCCEEDS!! " : "FAILS!! ") << "**********************\n\n\n";
  }

  pass_++;
}

DEFINE_FWK_MODULE(CaloGeometryAnalyzer);