MuonGeometrySanityCheck

MuonGeometrySanityCheckCustomFrame

MuonGeometrySanityCheckPoint

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
// -*- C++ -*-
//
// Package:    MuonGeometrySanityCheck
// Class:      MuonGeometrySanityCheck
//
/**\class MuonGeometrySanityCheck MuonGeometrySanityCheck.cc Alignment/MuonAlignment/plugins/MuonGeometrySanityCheck.cc

 Description: <one line class summary>

 Implementation:
     <Notes on implementation>
*/
//
// Original Author:  Jim Pivarski
//         Created:  Sat Jul  3 13:33:13 CDT 2010
// $Id: MuonGeometrySanityCheck.cc,v 1.3 2011/10/12 22:13:00 khotilov Exp $
//
//

// system include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include "Geometry/DTGeometry/interface/DTGeometry.h"
#include "Geometry/CSCGeometry/interface/CSCGeometry.h"
#include "Geometry/Records/interface/MuonGeometryRecord.h"
#include "Geometry/CommonDetUnit/interface/GeomDet.h"
#include "DataFormats/GeometryVector/interface/GlobalPoint.h"
#include "DataFormats/DetId/interface/DetId.h"
#include "DataFormats/MuonDetId/interface/MuonSubdetId.h"
#include "DataFormats/MuonDetId/interface/DTChamberId.h"
#include "DataFormats/MuonDetId/interface/DTSuperLayerId.h"
#include "DataFormats/MuonDetId/interface/DTLayerId.h"
#include "DataFormats/MuonDetId/interface/CSCDetId.h"
#include "DataFormats/CLHEP/interface/AlgebraicObjects.h"

//
// class decleration
//

class MuonGeometrySanityCheckCustomFrame {
public:
  MuonGeometrySanityCheckCustomFrame(const edm::ParameterSet &iConfig, std::string name);

  GlobalPoint transform(GlobalPoint point) const;
  GlobalPoint transformInverse(GlobalPoint point) const;
  AlgebraicMatrix matrix;
  AlgebraicMatrix matrixInverse;
};

class MuonGeometrySanityCheckPoint {
public:
  MuonGeometrySanityCheckPoint(const edm::ParameterSet &iConfig,
                               const std::map<std::string, const MuonGeometrySanityCheckCustomFrame *> &frames);

  enum { kDTChamber, kDTSuperLayer, kDTLayer, kCSCChamber, kCSCLayer };

  enum { kGlobal, kLocal, kChamber, kCustom };

  std::string detName() const;

  int type;
  DetId detector;
  int frame;
  const MuonGeometrySanityCheckCustomFrame *customFrame;
  GlobalPoint displacement;
  bool has_expectation;
  GlobalPoint expectation;
  std::string name;
  int outputFrame;
  const MuonGeometrySanityCheckCustomFrame *outputCustomFrame;

private:
  bool numeric(std::string s);
  int number(std::string s);
};

class MuonGeometrySanityCheck : public edm::one::EDAnalyzer<> {
public:
  explicit MuonGeometrySanityCheck(const edm::ParameterSet &iConfig);
  ~MuonGeometrySanityCheck() override;

private:
  void analyze(const edm::Event &, const edm::EventSetup &iConfig) override;

  std::string printout;
  double tolerance;
  std::string prefix;
  std::map<std::string, const MuonGeometrySanityCheckCustomFrame *> m_frames;
  std::vector<MuonGeometrySanityCheckPoint> m_points;

  const edm::ESGetToken<DTGeometry, MuonGeometryRecord> dtGeomToken_;
  const edm::ESGetToken<CSCGeometry, MuonGeometryRecord> cscGeomToken_;
};

//
// constants, enums and typedefs
//

//
// static data member definitions
//

//
// constructors and destructor
//

MuonGeometrySanityCheck::MuonGeometrySanityCheck(const edm::ParameterSet &iConfig)
    : dtGeomToken_(esConsumes<edm::Transition::BeginRun>()), cscGeomToken_(esConsumes<edm::Transition::BeginRun>()) {
  printout = iConfig.getParameter<std::string>("printout");
  if (printout != std::string("all") && printout != std::string("bad")) {
    throw cms::Exception("BadConfig") << "Printout must be \"all\" or \"bad\"." << std::endl;
  }

  tolerance = iConfig.getParameter<double>("tolerance");
  if (tolerance <= 0) {
    throw cms::Exception("BadConfig") << "Tolerance must be positive." << std::endl;
  }

  prefix = iConfig.getParameter<std::string>("prefix");

  std::vector<edm::ParameterSet> frames = iConfig.getParameter<std::vector<edm::ParameterSet> >("frames");
  for (std::vector<edm::ParameterSet>::const_iterator frame = frames.begin(); frame != frames.end(); ++frame) {
    std::string name = frame->getParameter<std::string>("name");
    if (m_frames.find(name) != m_frames.end()) {
      throw cms::Exception("BadConfig") << "Custom frame \"" << name << "\" has been defined twice." << std::endl;
    }
    m_frames[name] = new MuonGeometrySanityCheckCustomFrame(*frame, name);
  }

  std::vector<edm::ParameterSet> points = iConfig.getParameter<std::vector<edm::ParameterSet> >("points");
  for (std::vector<edm::ParameterSet>::const_iterator point = points.begin(); point != points.end(); ++point) {
    m_points.push_back(MuonGeometrySanityCheckPoint(*point, m_frames));
  }
}

MuonGeometrySanityCheck::~MuonGeometrySanityCheck() {
  for (std::map<std::string, const MuonGeometrySanityCheckCustomFrame *>::iterator iter = m_frames.begin();
       iter != m_frames.end();
       ++iter) {
    delete iter->second;
  }
}

MuonGeometrySanityCheckCustomFrame::MuonGeometrySanityCheckCustomFrame(const edm::ParameterSet &iConfig,
                                                                       std::string name) {
  std::vector<double> numbers = iConfig.getParameter<std::vector<double> >("matrix");
  if (numbers.size() != 9) {
    throw cms::Exception("BadConfig") << "Custom frame \"" << name << "\" has a matrix which is not 3x3." << std::endl;
  }

  matrix = AlgebraicMatrix(3, 3);
  matrix[0][0] = numbers[0];
  matrix[0][1] = numbers[1];
  matrix[0][2] = numbers[2];
  matrix[1][0] = numbers[3];
  matrix[1][1] = numbers[4];
  matrix[1][2] = numbers[5];
  matrix[2][0] = numbers[6];
  matrix[2][1] = numbers[7];
  matrix[2][2] = numbers[8];

  int ierr;
  matrixInverse = matrix;
  matrixInverse.invert(ierr);
  if (ierr != 0) {
    throw cms::Exception("BadConfig") << "Could not invert matrix for custom frame \"" << name << "\"." << std::endl;
  }
}

GlobalPoint MuonGeometrySanityCheckCustomFrame::transform(GlobalPoint point) const {
  AlgebraicVector input(3);
  input[0] = point.x();
  input[1] = point.x();
  input[2] = point.x();
  AlgebraicVector output = matrix * input;
  return GlobalPoint(output[0], output[1], output[3]);
}

GlobalPoint MuonGeometrySanityCheckCustomFrame::transformInverse(GlobalPoint point) const {
  AlgebraicVector input(3);
  input[0] = point.x();
  input[1] = point.x();
  input[2] = point.x();
  AlgebraicVector output = matrixInverse * input;
  return GlobalPoint(output[0], output[1], output[3]);
}

bool MuonGeometrySanityCheckPoint::numeric(std::string s) {
  return (s == std::string("0") || s == std::string("1") || s == std::string("2") || s == std::string("3") ||
          s == std::string("4") || s == std::string("5") || s == std::string("6") || s == std::string("7") ||
          s == std::string("8") || s == std::string("9"));
}

int MuonGeometrySanityCheckPoint::number(std::string s) {
  if (s == std::string("0"))
    return 0;
  else if (s == std::string("1"))
    return 1;
  else if (s == std::string("2"))
    return 2;
  else if (s == std::string("3"))
    return 3;
  else if (s == std::string("4"))
    return 4;
  else if (s == std::string("5"))
    return 5;
  else if (s == std::string("6"))
    return 6;
  else if (s == std::string("7"))
    return 7;
  else if (s == std::string("8"))
    return 8;
  else if (s == std::string("9"))
    return 9;
  else
    assert(false);
}

MuonGeometrySanityCheckPoint::MuonGeometrySanityCheckPoint(
    const edm::ParameterSet &iConfig, const std::map<std::string, const MuonGeometrySanityCheckCustomFrame *> &frames) {
  std::string detName = iConfig.getParameter<std::string>("detector");

  bool parsing_error = false;

  bool barrel = (detName.substr(0, 2) == std::string("MB"));
  bool endcap = (detName.substr(0, 2) == std::string("ME"));
  if (!barrel && !endcap)
    parsing_error = true;

  if (!parsing_error && barrel) {
    int index = 2;

    bool plus = true;
    if (detName.substr(index, 1) == std::string("+")) {
      plus = true;
      index++;
    } else if (detName.substr(index, 1) == std::string("-")) {
      plus = false;
      index++;
    }

    int wheel = 0;
    bool wheel_digit = false;
    while (!parsing_error && numeric(detName.substr(index, 1))) {
      wheel *= 10;
      wheel += number(detName.substr(index, 1));
      wheel_digit = true;
      index++;
    }
    if (!plus)
      wheel *= -1;
    if (!wheel_digit)
      parsing_error = true;

    if (detName.substr(index, 1) != std::string("/"))
      parsing_error = true;
    index++;

    int station = 0;
    bool station_digit = false;
    while (!parsing_error && numeric(detName.substr(index, 1))) {
      station *= 10;
      station += number(detName.substr(index, 1));
      station_digit = true;
      index++;
    }
    if (!station_digit)
      parsing_error = true;

    if (detName.substr(index, 1) != std::string("/"))
      parsing_error = true;
    index++;

    int sector = 0;
    bool sector_digit = false;
    while (!parsing_error && numeric(detName.substr(index, 1))) {
      sector *= 10;
      sector += number(detName.substr(index, 1));
      sector_digit = true;
      index++;
    }
    if (!sector_digit)
      parsing_error = true;

    // these are optional
    int superlayer = 0;
    bool superlayer_digit = false;
    int layer = 0;
    if (detName.substr(index, 1) == std::string("/")) {
      index++;
      while (!parsing_error && numeric(detName.substr(index, 1))) {
        superlayer *= 10;
        superlayer += number(detName.substr(index, 1));
        superlayer_digit = true;
        index++;
      }
      if (!superlayer_digit)
        parsing_error = true;

      if (detName.substr(index, 1) == std::string("/")) {
        index++;
        while (!parsing_error && numeric(detName.substr(index, 1))) {
          layer *= 10;
          layer += number(detName.substr(index, 1));
          index++;
        }
      }
    }

    if (!parsing_error) {
      bool no_such_chamber = false;

      if (wheel < -2 || wheel > 2)
        no_such_chamber = true;
      if (station < 1 || station > 4)
        no_such_chamber = true;
      if (station == 4 && (sector < 1 || sector > 14))
        no_such_chamber = true;
      if (station < 4 && (sector < 1 || sector > 12))
        no_such_chamber = true;

      if (no_such_chamber) {
        throw cms::Exception("BadConfig") << "Chamber doesn't exist: MB" << (plus ? "+" : "-") << wheel << "/"
                                          << station << "/" << sector << std::endl;
      }

      if (superlayer == 0) {
        detector = DTChamberId(wheel, station, sector);
        type = kDTChamber;
      } else {
        bool no_such_superlayer = false;
        if (superlayer < 1 || superlayer > 3)
          no_such_superlayer = true;
        if (station == 4 && superlayer == 2)
          no_such_superlayer = true;

        if (no_such_superlayer) {
          throw cms::Exception("BadConfig") << "Superlayer doesn't exist: MB" << (plus ? "+" : "-") << wheel << "/"
                                            << station << "/" << sector << "/" << superlayer << std::endl;
        }

        if (layer == 0) {
          detector = DTSuperLayerId(wheel, station, sector, superlayer);
          type = kDTSuperLayer;
        } else {
          bool no_such_layer = false;
          if (layer < 1 || layer > 4)
            no_such_layer = true;

          if (no_such_layer) {
            throw cms::Exception("BadConfig")
                << "Layer doesn't exist: MB" << (plus ? "+" : "-") << wheel << "/" << station << "/" << sector << "/"
                << superlayer << "/" << layer << std::endl;
          }

          detector = DTLayerId(wheel, station, sector, superlayer, layer);
          type = kDTLayer;
        }
      }
    }
  } else if (!parsing_error && endcap) {
    int index = 2;

    bool plus = true;
    if (detName.substr(index, 1) == std::string("+")) {
      plus = true;
      index++;
    } else if (detName.substr(index, 1) == std::string("-")) {
      plus = false;
      index++;
    } else
      parsing_error = true;

    int station = 0;
    bool station_digit = false;
    while (!parsing_error && numeric(detName.substr(index, 1))) {
      station *= 10;
      station += number(detName.substr(index, 1));
      station_digit = true;
      index++;
    }
    if (!plus)
      station *= -1;
    if (!station_digit)
      parsing_error = true;

    if (detName.substr(index, 1) != std::string("/"))
      parsing_error = true;
    index++;

    int ring = 0;
    bool ring_digit = false;
    while (!parsing_error && numeric(detName.substr(index, 1))) {
      ring *= 10;
      ring += number(detName.substr(index, 1));
      ring_digit = true;
      index++;
    }
    if (!ring_digit)
      parsing_error = true;

    if (detName.substr(index, 1) != std::string("/"))
      parsing_error = true;
    index++;

    int chamber = 0;
    bool chamber_digit = false;
    while (!parsing_error && numeric(detName.substr(index, 1))) {
      chamber *= 10;
      chamber += number(detName.substr(index, 1));
      chamber_digit = true;
      index++;
    }
    if (!chamber_digit)
      parsing_error = true;

    // this is optional
    int layer = 0;
    bool layer_digit = false;
    if (detName.substr(index, 1) == std::string("/")) {
      index++;
      while (!parsing_error && numeric(detName.substr(index, 1))) {
        layer *= 10;
        layer += number(detName.substr(index, 1));
        layer_digit = true;
        index++;
      }
      if (!layer_digit)
        parsing_error = true;
    }

    if (!parsing_error) {
      bool no_such_chamber = false;

      int endcap = (station > 0 ? 1 : 2);
      station = abs(station);
      if (station < 1 || station > 4)
        no_such_chamber = true;
      if (station == 1 && (ring < 1 || ring > 4))
        no_such_chamber = true;
      if (station > 1 && (ring < 1 || ring > 2))
        no_such_chamber = true;
      if (station == 1 && (chamber < 1 || chamber > 36))
        no_such_chamber = true;
      if (station > 1 && ring == 1 && (chamber < 1 || chamber > 18))
        no_such_chamber = true;
      if (station > 1 && ring == 2 && (chamber < 1 || chamber > 36))
        no_such_chamber = true;

      if (no_such_chamber) {
        throw cms::Exception("BadConfig") << "Chamber doesn't exist: ME" << (endcap == 1 ? "+" : "-") << station << "/"
                                          << ring << "/" << chamber << std::endl;
      }

      if (layer == 0) {
        detector = CSCDetId(endcap, station, ring, chamber);
        type = kCSCChamber;
      } else {
        bool no_such_layer = false;
        if (layer < 1 || layer > 6)
          no_such_layer = true;

        if (no_such_layer) {
          throw cms::Exception("BadConfig") << "Layer doesn't exist: ME" << (endcap == 1 ? "+" : "-") << station << "/"
                                            << ring << "/" << chamber << "/" << layer << std::endl;
        }

        detector = CSCDetId(endcap, station, ring, chamber, layer);
        type = kCSCLayer;
      }
    }
  }

  if (parsing_error) {
    throw cms::Exception("BadConfig") << "Detector name is malformed: " << detName << std::endl;
  }

  std::string frameName = iConfig.getParameter<std::string>("frame");
  const std::map<std::string, const MuonGeometrySanityCheckCustomFrame *>::const_iterator frameIter =
      frames.find(frameName);
  if (frameName == std::string("global")) {
    frame = kGlobal;
    customFrame = nullptr;
  } else if (frameName == std::string("local")) {
    frame = kLocal;
    customFrame = nullptr;
  } else if (frameName == std::string("chamber")) {
    frame = kChamber;
    customFrame = nullptr;
  } else if (frameIter != frames.end()) {
    frame = kCustom;
    customFrame = frameIter->second;
  } else {
    throw cms::Exception("BadConfig") << "Frame \"" << frameName << "\" has not been defined." << std::endl;
  }

  std::vector<double> point = iConfig.getParameter<std::vector<double> >("displacement");
  if (point.size() != 3) {
    throw cms::Exception("BadConfig") << "Displacement relative to detector " << detName
                                      << " doesn't have exactly three components." << std::endl;
  }

  displacement = GlobalPoint(point[0], point[1], point[2]);

  const edm::Entry *entry = iConfig.retrieveUnknown("expectation");
  if (entry != nullptr) {
    has_expectation = true;

    point = iConfig.getParameter<std::vector<double> >("expectation");
    if (point.size() != 3) {
      throw cms::Exception("BadConfig") << "Expectation for detector " << detName << ", displacement " << displacement
                                        << " doesn't have exactly three components." << std::endl;
    }

    expectation = GlobalPoint(point[0], point[1], point[2]);
  } else {
    has_expectation = false;
  }

  entry = iConfig.retrieveUnknown("name");
  if (entry != nullptr) {
    name = iConfig.getParameter<std::string>("name");
  } else {
    name = std::string("anonymous");
  }

  entry = iConfig.retrieveUnknown("outputFrame");
  if (entry != nullptr) {
    frameName = iConfig.getParameter<std::string>("outputFrame");
    const std::map<std::string, const MuonGeometrySanityCheckCustomFrame *>::const_iterator frameIter =
        frames.find(frameName);
    if (frameName == std::string("global")) {
      outputFrame = kGlobal;
      outputCustomFrame = nullptr;
    } else if (frameName == std::string("local")) {
      outputFrame = kLocal;
      outputCustomFrame = nullptr;
    } else if (frameName == std::string("chamber")) {
      outputFrame = kChamber;
      outputCustomFrame = nullptr;
    } else if (frameIter != frames.end()) {
      outputFrame = kCustom;
      outputCustomFrame = frameIter->second;
    } else {
      throw cms::Exception("BadConfig") << "Frame \"" << frameName << "\" has not been defined." << std::endl;
    }
  } else {
    outputFrame = kGlobal;
    outputCustomFrame = nullptr;
  }
}

std::string MuonGeometrySanityCheckPoint::detName() const {
  std::stringstream output;
  if (type == kDTChamber) {
    DTChamberId id(detector);
    output << "MB" << (id.wheel() > 0 ? "+" : "") << id.wheel() << "/" << id.station() << "/" << id.sector();
  } else if (type == kDTSuperLayer) {
    DTSuperLayerId id(detector);
    output << "MB" << (id.wheel() > 0 ? "+" : "") << id.wheel() << "/" << id.station() << "/" << id.sector() << "/"
           << id.superlayer();
  } else if (type == kDTLayer) {
    DTLayerId id(detector);
    output << "MB" << (id.wheel() > 0 ? "+" : "") << id.wheel() << "/" << id.station() << "/" << id.sector() << "/"
           << id.superlayer() << "/" << id.layer();
  } else if (type == kCSCChamber) {
    CSCDetId id(detector);
    output << "ME" << (id.endcap() == 1 ? "+" : "-") << id.station() << "/" << id.ring() << "/" << id.chamber();
  } else if (type == kCSCLayer) {
    CSCDetId id(detector);
    output << "ME" << (id.endcap() == 1 ? "+" : "-") << id.station() << "/" << id.ring() << "/" << id.chamber() << "/"
           << id.layer();
  } else
    assert(false);
  return output.str();
}

// ------------ method called to for each event  ------------
void MuonGeometrySanityCheck::analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) {
  const DTGeometry *dtGeometry = &iSetup.getData(dtGeomToken_);
  const CSCGeometry *cscGeometry = &iSetup.getData(cscGeomToken_);

  int num_transformed = 0;
  int num_tested = 0;
  int num_bad = 0;
  for (std::vector<MuonGeometrySanityCheckPoint>::const_iterator point = m_points.begin(); point != m_points.end();
       ++point) {
    num_transformed++;

    bool dt = (point->detector.subdetId() == MuonSubdetId::DT);

    // convert the displacement vector into the chosen coordinate system and add it to the chamber's position
    GlobalPoint chamberPos;
    if (dt)
      chamberPos = dtGeometry->idToDet(point->detector)->surface().toGlobal(LocalPoint(0., 0., 0.));
    else
      chamberPos = cscGeometry->idToDet(point->detector)->surface().toGlobal(LocalPoint(0., 0., 0.));

    GlobalPoint result;
    if (point->frame == MuonGeometrySanityCheckPoint::kGlobal) {
      result = GlobalPoint(chamberPos.x() + point->displacement.x(),
                           chamberPos.y() + point->displacement.y(),
                           chamberPos.z() + point->displacement.z());
    }

    else if (point->frame == MuonGeometrySanityCheckPoint::kLocal) {
      if (dt)
        result = dtGeometry->idToDet(point->detector)
                     ->surface()
                     .toGlobal(LocalPoint(point->displacement.x(), point->displacement.y(), point->displacement.z()));
      else
        result = cscGeometry->idToDet(point->detector)
                     ->surface()
                     .toGlobal(LocalPoint(point->displacement.x(), point->displacement.y(), point->displacement.z()));
    }

    else if (point->frame == MuonGeometrySanityCheckPoint::kChamber) {
      if (point->detector.subdetId() == MuonSubdetId::DT) {
        DTChamberId id(point->detector);
        if (dt)
          result = dtGeometry->idToDet(id)->surface().toGlobal(
              LocalPoint(point->displacement.x(), point->displacement.y(), point->displacement.z()));
        else
          result = cscGeometry->idToDet(id)->surface().toGlobal(
              LocalPoint(point->displacement.x(), point->displacement.y(), point->displacement.z()));
      } else if (point->detector.subdetId() == MuonSubdetId::CSC) {
        CSCDetId cscid(point->detector);
        CSCDetId id(cscid.endcap(), cscid.station(), cscid.ring(), cscid.chamber());
        if (dt)
          result = dtGeometry->idToDet(id)->surface().toGlobal(
              LocalPoint(point->displacement.x(), point->displacement.y(), point->displacement.z()));
        else
          result = cscGeometry->idToDet(id)->surface().toGlobal(
              LocalPoint(point->displacement.x(), point->displacement.y(), point->displacement.z()));
      } else {
        assert(false);
      }
    }

    else if (point->frame == MuonGeometrySanityCheckPoint::kCustom) {
      GlobalPoint transformed = point->customFrame->transform(point->displacement);
      result = GlobalPoint(
          chamberPos.x() + transformed.x(), chamberPos.y() + transformed.y(), chamberPos.z() + transformed.z());
    }

    else {
      assert(false);
    }

    // convert the result into the chosen output coordinate system
    if (point->outputFrame == MuonGeometrySanityCheckPoint::kGlobal) {
    }

    else if (point->outputFrame == MuonGeometrySanityCheckPoint::kLocal) {
      LocalPoint transformed;
      if (dt)
        transformed = dtGeometry->idToDet(point->detector)->surface().toLocal(result);
      else
        transformed = cscGeometry->idToDet(point->detector)->surface().toLocal(result);
      result = GlobalPoint(transformed.x(), transformed.y(), transformed.z());
    }

    else if (point->outputFrame == MuonGeometrySanityCheckPoint::kChamber) {
      if (point->detector.subdetId() == MuonSubdetId::DT) {
        DTChamberId id(point->detector);
        LocalPoint transformed;
        if (dt)
          transformed = dtGeometry->idToDet(id)->surface().toLocal(result);
        else
          transformed = cscGeometry->idToDet(id)->surface().toLocal(result);
        result = GlobalPoint(transformed.x(), transformed.y(), transformed.z());
      } else if (point->detector.subdetId() == MuonSubdetId::CSC) {
        CSCDetId cscid(point->detector);
        CSCDetId id(cscid.endcap(), cscid.station(), cscid.ring(), cscid.chamber());
        LocalPoint transformed;
        if (dt)
          transformed = dtGeometry->idToDet(id)->surface().toLocal(result);
        else
          transformed = cscGeometry->idToDet(id)->surface().toLocal(result);
        result = GlobalPoint(transformed.x(), transformed.y(), transformed.z());
      } else {
        assert(false);
      }
    }

    else if (point->outputFrame == MuonGeometrySanityCheckPoint::kCustom) {
      result = point->outputCustomFrame->transformInverse(result);
    }

    std::stringstream output;
    output << prefix << " " << point->name << " " << point->detName() << " " << result.x() << " " << result.y() << " "
           << result.z();

    bool bad = false;
    if (point->has_expectation) {
      num_tested++;
      double residx = result.x() - point->expectation.x();
      double residy = result.y() - point->expectation.y();
      double residz = result.z() - point->expectation.z();

      if (fabs(residx) > tolerance || fabs(residy) > tolerance || fabs(residz) > tolerance) {
        num_bad++;
        bad = true;
        output << " BAD " << residx << " " << residy << " " << residz << std::endl;
      } else {
        output << " GOOD " << residx << " " << residy << " " << residz << std::endl;
      }
    } else {
      output << " UNTESTED 0 0 0" << std::endl;
    }

    if (printout == std::string("all") || (printout == std::string("bad") && bad)) {
      std::cout << output.str();
    }
  }

  std::cout << std::endl
            << "SUMMARY transformed: " << num_transformed << " tested: " << num_tested << " bad: " << num_bad
            << " good: " << (num_tested - num_bad) << std::endl;
}

//define this as a plug-in
DEFINE_FWK_MODULE(MuonGeometrySanityCheck);