Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:23

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 
0123   // Iterate over the DetUnits in the CSCGeometry
0124   for (const auto &it : pDD->detUnits()) {
0125     // Check each DetUnit really is a CSC layer
0126     auto layer = dynamic_cast<const CSCLayer *>(it);
0127 
0128     if (layer) {
0129       // Get DetId in various ways
0130 
0131       DetId detId = layer->geographicalId();
0132       int id = detId();  // or detId.rawId()
0133       CSCDetId cscDetId = layer->id();
0134 
0135       // There is going to be a lot of messing with field width (and precision)
0136       // so save input values...
0137       int iw = std::cout.width();      // save current width
0138       int ip = std::cout.precision();  // save current precision
0139 
0140       short ie = CSCDetId::endcap(id);
0141       short is = CSCDetId::station(id);
0142       short ir = CSCDetId::ring(id);
0143       short ic = CSCDetId::chamber(id);
0144       short il = CSCDetId::layer(id);
0145 
0146       ++icount;
0147 
0148       std::cout << std::setw(4) << icount << std::setw(12) << id << std::oct << std::setw(12) << id << std::dec
0149                 << std::setw(iw) << "   E" << ie << " S" << is << " R" << ir << " C" << std::setw(2) << ic
0150                 << std::setw(iw) << " L" << il;
0151 
0152       unsigned cind = theIndexer->chamberIndex(ie, is, ir, ic);
0153       unsigned lind = theIndexer->layerIndex(ie, is, ir, ic, il);
0154       unsigned scind = theIndexer->startChamberIndexInEndcap(ie, is, ir) + ic - 1;
0155       unsigned cind2 = theIndexer->chamberIndex(cscDetId);
0156       unsigned lind2 = theIndexer->layerIndex(cscDetId);
0157 
0158       std::cout << std::setw(12) << lind << std::setw(12) << lind2 << std::setw(12) << scind << std::setw(12)
0159                 << theIndexer->chamberLabelFromChamberIndex(scind) << "     ";
0160 
0161       // Index a few strips
0162       unsigned short nchan = theIndexer->stripChannelsPerOnlineLayer(is, ir);
0163       unsigned int sc1 = theIndexer->stripChannelIndex(ie, is, ir, ic, il, 1);
0164       unsigned int scm = theIndexer->stripChannelIndex(ie, is, ir, ic, il, nchan / 2);
0165       unsigned int scn = theIndexer->stripChannelIndex(ie, is, ir, ic, il, nchan);
0166 
0167       std::cout << "      1  " << std::setw(6) << sc1 << "      " << std::setw(2) << nchan / 2 << "  " << std::setw(6)
0168                 << scm << "      " << std::setw(2) << nchan << "  " << std::setw(6) << scn << std::endl;
0169 
0170       // Reset the values we changed
0171       std::cout << std::setprecision(ip) << std::setw(iw);
0172 
0173       // ASSERTS
0174       // =======
0175 
0176       // Check layer indices are consistent
0177 
0178       assert(cind2 == cind);
0179       assert(lind2 == lind);
0180 
0181       // Build CSCDetId from layer index and check it is same as original
0182 
0183       CSCDetId cscDetId2 = theIndexer->detIdFromLayerIndex(lind);  // folds ME1/1a into ME1/1
0184 
0185       testCSCDetId = cscDetId;
0186 
0187       if (ir == 4)
0188         testCSCDetId = CSCDetId(ie, is, 1, ic, il);
0189 
0190       assert(cscDetId2 == testCSCDetId);
0191 
0192       // Build CSCDetId from the strip-channel index for strip "nchan" and check
0193       // it matches Ganged ME1/1a returns ME1/1 CSCDetId and channel 65-80
0194 
0195       std::pair<CSCDetId, unsigned short int> p = theIndexer->detIdFromStripChannelIndex(scn);
0196       CSCDetId cscDetId3 = p.first;
0197       unsigned short iscn = p.second;
0198 
0199       if (ir == 4 && ganged)
0200         iscn -= 64;  // reset ganged ME1a channel from 65-80 to 1-16
0201 
0202       assert(iscn == nchan);
0203 
0204       if (ir == 4 && !ganged)
0205         testCSCDetId = cscDetId;  // unganged ME1/1a needs its own CSCDetId
0206 
0207       assert(cscDetId3 == testCSCDetId);
0208 
0209       // Check idToDetUnit
0210       const GeomDetUnit *gdu = pDD->idToDetUnit(detId);
0211       assert(gdu == layer);
0212       // Check idToDet
0213       const GeomDet *gd = pDD->idToDet(detId);
0214       assert(gd == layer);
0215     } else {
0216       std::cout << myName() << ": something wrong ... could not dynamic_cast Det* to CSCLayer* " << std::endl;
0217     }
0218   }
0219 
0220   std::cout << dashedLine_ << std::endl;
0221   std::cout << myName() << " end." << std::endl;
0222 }
0223 
0224 // define this as a plug-in
0225 DEFINE_FWK_MODULE(CSCIndexerAnalyzer2);