Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-02 03:38:53

0001 // Test CSCIndexer 22.11.2012 ptc
0002 
0003 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0004 #include <FWCore/Framework/interface/ESHandle.h>
0005 #include <FWCore/Framework/interface/EventSetup.h>
0006 #include <FWCore/Framework/interface/MakerMacros.h>
0007 #include <FWCore/ParameterSet/interface/ParameterSet.h>
0008 #include <memory>
0009 
0010 #include <DataFormats/GeometryVector/interface/Pi.h>
0011 #include <Geometry/CSCGeometry/interface/CSCGeometry.h>
0012 #include <Geometry/CSCGeometry/interface/CSCLayer.h>
0013 #include <Geometry/Records/interface/MuonGeometryRecord.h>
0014 
0015 #include <CalibMuon/CSCCalibration/interface/CSCIndexerBase.h>
0016 #include <CalibMuon/CSCCalibration/interface/CSCIndexerRecord.h>
0017 
0018 #include <cmath>
0019 #include <iomanip>  // for setw() etc.
0020 #include <string>
0021 #include <vector>
0022 
0023 class CSCIndexerAnalyzer2 : public edm::one::EDAnalyzer<> {
0024 public:
0025   explicit CSCIndexerAnalyzer2(const edm::ParameterSet &);
0026   ~CSCIndexerAnalyzer2() = default;
0027 
0028   virtual void analyze(const edm::Event &, const edm::EventSetup &);
0029 
0030   const std::string &myName() const { return myName_; }
0031   const std::string &myAlgo() const { return algoName_; }
0032 
0033 private:
0034   const int dashedLineWidth_;
0035   const std::string dashedLine_;
0036   const std::string myName_;
0037 
0038   std::string algoName_;
0039 
0040   const edm::ESGetToken<CSCGeometry, MuonGeometryRecord> theCSCGeometryToken_;
0041   const edm::ESGetToken<CSCIndexerBase, CSCIndexerRecord> theCSCIndexerToken_;
0042 };
0043 
0044 CSCIndexerAnalyzer2::CSCIndexerAnalyzer2(const edm::ParameterSet &iConfig)
0045     : dashedLineWidth_(146),
0046       dashedLine_(std::string(dashedLineWidth_, '-')),
0047       myName_("CSCIndexerAnalyzer2"),
0048       algoName_("UNKNOWN"),
0049       theCSCGeometryToken_(esConsumes()),
0050       theCSCIndexerToken_(esConsumes()) {
0051   std::cout << dashedLine_ << std::endl;
0052   std::cout << "Welcome to " << myName_ << std::endl;
0053   std::cout << dashedLine_ << std::endl;
0054   std::cout << "At present my CSCIndexer algorithm is    " << myAlgo() << std::endl;
0055   std::cout << dashedLine_ << std::endl;
0056   std::cout << "I will build the CSC geometry, then iterate over all layers." << std::endl;
0057   std::cout << "From each CSCDetId I will build the associated linear index, "
0058                "including ME1a layers."
0059             << std::endl;
0060   std::cout << "I will build this index once from the layer labels and once "
0061                "from the CSCDetId, and check they agree."
0062             << std::endl;
0063   std::cout << "I will output one line per layer, listing the CSCDetId, the "
0064                "labels, and these two indexes."
0065             << std::endl;
0066   std::cout << "I will append the strip-channel indexes for the two edge "
0067                "strips and the central strip."
0068             << std::endl;
0069   std::cout << "Finally, I will rebuild a CSCDetId from the layer index, and "
0070                "check it is the same as the original,"
0071             << std::endl;
0072   std::cout << "and rebuild a CSCDetId from the final strip-channel index, and "
0073                "check it is the same as the original."
0074             << std::endl;
0075   std::cout << "If any of these tests fail, you will see assert failure "
0076                "messages in the output."
0077             << std::endl;
0078   std::cout << "If there are no such failures then the tests passed." << std::endl;
0079   std::cout << dashedLine_ << std::endl;
0080 }
0081 
0082 void CSCIndexerAnalyzer2::analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) {
0083   CSCDetId testCSCDetId;
0084 
0085   const double dPi = Geom::pi();
0086   const double radToDeg = 180. / dPi;
0087 
0088   std::cout << myName() << "::analyze(..) start" << std::endl;
0089   std::cout << dashedLine_ << std::endl;
0090   std::cout << "pi = " << dPi << ", radToDeg = " << radToDeg << std::endl;
0091 
0092   auto const pDD = &iSetup.getData(theCSCGeometryToken_);
0093 
0094   std::cout << " Geometry node for CSCGeom is  " << &(*pDD) << std::endl;
0095   std::cout << " I have " << pDD->detTypes().size() << " detTypes" << std::endl;
0096   std::cout << " I have " << pDD->detUnits().size() << " detUnits" << std::endl;
0097   std::cout << " I have " << pDD->dets().size() << " dets" << std::endl;
0098   std::cout << " I have " << pDD->layers().size() << " layers" << std::endl;
0099   std::cout << " I have " << pDD->chambers().size() << " chambers" << std::endl;
0100 
0101   // Get the CSCIndexer algorithm from EventSetup
0102   const auto theIndexer = &iSetup.getData(theCSCIndexerToken_);
0103   algoName_ = theIndexer->name();
0104 
0105   std::cout << dashedLine_ << std::endl;
0106   std::cout << "Found CSCIndexer algorithm    " << myAlgo() << "    in EventSetup" << std::endl;
0107   std::cout << dashedLine_ << std::endl;
0108 
0109   bool ganged = 1;
0110   if (myAlgo() == "CSCIndexerPostls1")
0111     ganged = 0;
0112 
0113   std::cout << myName() << ": Begin iteration over geometry..." << std::endl;
0114   std::cout << dashedLine_ << std::endl;
0115 
0116   std::cout << "\n  #     id(dec)      id(oct)                        "
0117                "lindex     lindex2      cindex       label       strip  sindex "
0118                "  strip  sindex   strip  sindex"
0119             << std::endl;
0120 
0121   int icount = 0;
0122   int icountAll = 0;
0123 
0124   // Iterate over the DetUnits in the CSCGeometry
0125   for (const auto &it : pDD->detUnits()) {
0126     // Check each DetUnit really is a CSC layer
0127     auto layer = dynamic_cast<const CSCLayer *>(it);
0128 
0129     if (layer) {
0130       ++icountAll;  // how many layers we see
0131 
0132       // Get DetId in various ways
0133 
0134       DetId detId = layer->geographicalId();
0135       int id = detId();  // or detId.rawId()
0136       CSCDetId cscDetId = layer->id();
0137 
0138       // There is going to be a lot of messing with field width (and precision)
0139       // so save input values...
0140       int iw = std::cout.width();      // save current width
0141       int ip = std::cout.precision();  // save current precision
0142 
0143       short ie = CSCDetId::endcap(id);
0144       short is = CSCDetId::station(id);
0145       short ir = CSCDetId::ring(id);
0146       short ic = CSCDetId::chamber(id);
0147       short il = CSCDetId::layer(id);
0148 
0149       ++icount;
0150 
0151       std::cout << std::setw(4) << icount << std::setw(12) << id << std::oct << std::setw(12) << id << std::dec
0152                 << std::setw(iw) << "   E" << ie << " S" << is << " R" << ir << " C" << std::setw(2) << ic
0153                 << std::setw(iw) << " L" << il;
0154 
0155       unsigned cind = theIndexer->chamberIndex(ie, is, ir, ic);
0156       unsigned lind = theIndexer->layerIndex(ie, is, ir, ic, il);
0157       unsigned scind = theIndexer->startChamberIndexInEndcap(ie, is, ir) + ic - 1;
0158       unsigned cind2 = theIndexer->chamberIndex(cscDetId);
0159       unsigned lind2 = theIndexer->layerIndex(cscDetId);
0160 
0161       std::cout << std::setw(12) << lind << std::setw(12) << lind2 << std::setw(12) << scind << std::setw(12)
0162                 << theIndexer->chamberLabelFromChamberIndex(scind) << "     ";
0163 
0164       // Index a few strips
0165       unsigned short nchan = theIndexer->stripChannelsPerOnlineLayer(is, ir);
0166       unsigned int sc1 = theIndexer->stripChannelIndex(ie, is, ir, ic, il, 1);
0167       unsigned int scm = theIndexer->stripChannelIndex(ie, is, ir, ic, il, nchan / 2);
0168       unsigned int scn = theIndexer->stripChannelIndex(ie, is, ir, ic, il, nchan);
0169 
0170       std::cout << "      1  " << std::setw(6) << sc1 << "      " << std::setw(2) << nchan / 2 << "  " << std::setw(6)
0171                 << scm << "      " << std::setw(2) << nchan << "  " << std::setw(6) << scn << std::endl;
0172 
0173       // Reset the values we changed
0174       std::cout << std::setprecision(ip) << std::setw(iw);
0175 
0176       // ASSERTS
0177       // =======
0178 
0179       // Check layer indices are consistent
0180 
0181       assert(cind2 == cind);
0182       assert(lind2 == lind);
0183 
0184       // Build CSCDetId from layer index and check it is same as original
0185 
0186       CSCDetId cscDetId2 = theIndexer->detIdFromLayerIndex(lind);  // folds ME1/1a into ME1/1
0187 
0188       testCSCDetId = cscDetId;
0189 
0190       if (ir == 4)
0191         testCSCDetId = CSCDetId(ie, is, 1, ic, il);
0192 
0193       assert(cscDetId2 == testCSCDetId);
0194 
0195       // Build CSCDetId from the strip-channel index for strip "nchan" and check
0196       // it matches Ganged ME1/1a returns ME1/1 CSCDetId and channel 65-80
0197 
0198       std::pair<CSCDetId, unsigned short int> p = theIndexer->detIdFromStripChannelIndex(scn);
0199       CSCDetId cscDetId3 = p.first;
0200       unsigned short iscn = p.second;
0201 
0202       if (ir == 4 && ganged)
0203         iscn -= 64;  // reset ganged ME1a channel from 65-80 to 1-16
0204 
0205       assert(iscn == nchan);
0206 
0207       if (ir == 4 && !ganged)
0208         testCSCDetId = cscDetId;  // unganged ME1/1a needs its own CSCDetId
0209 
0210       assert(cscDetId3 == testCSCDetId);
0211 
0212       // Check idToDetUnit
0213       const GeomDetUnit *gdu = pDD->idToDetUnit(detId);
0214       assert(gdu == layer);
0215       // Check idToDet
0216       const GeomDet *gd = pDD->idToDet(detId);
0217       assert(gd == layer);
0218     } else {
0219       std::cout << myName() << ": something wrong ... could not dynamic_cast Det* to CSCLayer* " << std::endl;
0220     }
0221   }
0222 
0223   std::cout << dashedLine_ << std::endl;
0224   std::cout << myName() << " end." << std::endl;
0225 }
0226 
0227 // define this as a plug-in
0228 DEFINE_FWK_MODULE(CSCIndexerAnalyzer2);