Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:35:37

0001 #include <stdexcept>
0002 #include <string>
0003 #include <iostream>
0004 #include <fstream>
0005 #include <vector>
0006 #include <bitset>
0007 
0008 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0009 #include "FWCore/Framework/interface/Event.h"
0010 #include "FWCore/Framework/interface/MakerMacros.h"
0011 
0012 #include "FWCore/Framework/interface/EventSetup.h"
0013 #include "FWCore/Utilities/interface/ESGetToken.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 
0017 #include "CondFormats/CSCObjects/interface/CSCBadWires.h"
0018 #include "CondFormats/DataRecord/interface/CSCBadWiresRcd.h"
0019 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
0020 #include "DataFormats/MuonDetId/interface/CSCIndexer.h"
0021 
0022 namespace edmtest {
0023   class CSCReadBadWiresAnalyzer : public edm::one::EDAnalyzer<> {
0024   public:
0025     explicit CSCReadBadWiresAnalyzer(edm::ParameterSet const& ps)
0026         : outputToFile(ps.getParameter<bool>("outputToFile")),
0027           readBadChannels_(ps.getParameter<bool>("readBadChannels")),
0028           badWiresToken_{esConsumes()} {
0029       badWireWords.resize(3240, 0);  // incl. ME42
0030     }
0031 
0032     explicit CSCReadBadWiresAnalyzer(int i) {}
0033     virtual ~CSCReadBadWiresAnalyzer() {}
0034     virtual void analyze(const edm::Event& e, const edm::EventSetup& c);
0035 
0036     // Test code from CSCConditions
0037 
0038     /// did we request reading bad channel info from db?
0039     bool readBadChannels() const { return readBadChannels_; }
0040 
0041     void fillBadWireWords(edm::LogSystem&);
0042 
0043     /// return  bad channel words per CSCLayer - 1 bit per channel
0044     const std::bitset<112>& badWireWord(const CSCDetId& id) const;
0045 
0046   private:
0047     bool outputToFile;
0048     bool readBadChannels_;  // flag whether or not to even attempt reading bad channel info from db
0049     const edm::ESGetToken<CSCBadWires, CSCBadWiresRcd> badWiresToken_;
0050     const CSCBadWires* theBadWires;
0051 
0052     std::vector<std::bitset<112> > badWireWords;
0053   };
0054 
0055   void CSCReadBadWiresAnalyzer::analyze(const edm::Event& e, const edm::EventSetup& context) {
0056     using namespace edm::eventsetup;
0057 
0058     edm::LogSystem log("CSCBadWires");
0059 
0060     int counter = 0;
0061     log << " RUN# " << e.id().run() << std::endl;
0062     log << " EVENT# " << e.id().event() << std::endl;
0063 
0064     theBadWires = &context.getData(badWiresToken_);
0065 
0066     // Create the vectors of bitsets, one per layer, as done in CSCConditions
0067     fillBadWireWords(log);  // code from CSCConditions pasted into this file!
0068 
0069     CSCIndexer indexer;  // just to build a CSCDetId from chamber index
0070 
0071     std::vector<CSCBadWires::BadChamber>::const_iterator itcham;
0072     std::vector<CSCBadWires::BadChannel>::const_iterator itchan;
0073 
0074     log << "Bad Chambers:" << std::endl;
0075 
0076     int ibad = 0;
0077     int ifailed = 0;
0078 
0079     // Iterate over the list of bad chambers
0080 
0081     for (itcham = theBadWires->chambers.begin(); itcham != theBadWires->chambers.end(); ++itcham) {
0082       counter++;
0083       int indexc = itcham->chamber_index;
0084       int badstart = itcham->pointer;
0085       int nbad = itcham->bad_channels;
0086       log << counter << "  " << itcham->chamber_index << "  " << itcham->pointer << "  " << itcham->bad_channels
0087           << std::endl;
0088       CSCDetId id = indexer.detIdFromChamberIndex(indexc);
0089 
0090       // Iterate over the bad channels in this chamber
0091 
0092       for (int ichan = badstart - 1; ichan != badstart - 1 + nbad; ++ichan) {
0093         short lay = theBadWires->channels[ichan].layer;
0094         short chan = theBadWires->channels[ichan].channel;
0095 
0096         // create a CSCDetId for this layer, just because that it is needed for the interface to CSCConditions::badWireWord
0097         CSCDetId id2 = CSCDetId(id.endcap(), id.station(), id.ring(), id.chamber(), lay);
0098         std::bitset<112> ibits = badWireWord(id2);
0099 
0100         // Test whether this bad channel has indeed been flagged in the badWireWord
0101         if (ibits.test(chan - 1)) {
0102           log << "count " << ++ibad << " found bad channel " << chan << " in layer " << id2 << std::endl;
0103         } else {
0104           log << "count " << +ifailed << " failed to see bad channel " << chan << " in layer " << id2 << std::endl;
0105         }
0106       }
0107     }
0108 
0109     /*
0110     log<< "Bad Channels:" << std::endl;
0111     counter = 0; // reset it!
0112 
0113     for( itchan=theBadWires->channels.begin();itchan!=theBadWires->channels.end(); ++itchan ){    
0114       counter++;
0115       log<<counter<<"  "<<itchan->layer<<"  "<<itchan->channel<<"  "<<itchan->flag1<<std::endl;
0116     }
0117     */
0118 
0119     if (outputToFile) {
0120       std::ofstream BadWireFile("dbBadWire.dat", std::ios::app);
0121 
0122       counter = 0;
0123       for (itcham = theBadWires->chambers.begin(); itcham != theBadWires->chambers.end(); ++itcham) {
0124         counter++;
0125         BadWireFile << counter << "  " << itcham->chamber_index << "  " << itcham->pointer << "  "
0126                     << itcham->bad_channels << std::endl;
0127       }
0128       counter = 0;
0129       for (itchan = theBadWires->channels.begin(); itchan != theBadWires->channels.end(); ++itchan) {
0130         counter++;
0131         BadWireFile << counter << "  " << itchan->layer << "  " << itchan->channel << "  " << itchan->flag1
0132                     << std::endl;
0133       }
0134     }
0135   }
0136 
0137   void CSCReadBadWiresAnalyzer::fillBadWireWords(edm::LogSystem& log) {
0138     // reset existing values
0139     badWireWords.assign(3240, 0);
0140     if (readBadChannels()) {
0141       // unpack what we read from theBadWires
0142 
0143       // chambers is a vector<BadChamber>
0144       // channels is a vector<BadChannel>
0145       // Each BadChamber contains its index (1-468 or 540 w. ME42), the no. of bad channels,
0146       // and the index within vector<BadChannel> where this chamber's bad channels start.
0147 
0148       CSCIndexer indexer;
0149 
0150       int icount = 0;
0151 
0152       for (size_t i = 0; i < theBadWires->chambers.size(); ++i) {  // loop over bad chambers
0153         int indexc = theBadWires->chambers[i].chamber_index;
0154 
0155         // The following is not in standard CSCConditions version but was required for our prototype bad strip db
0156         if (indexc == 0) {
0157           log << "WARNING: chamber index = 0. Quitting. " << std::endl;
0158           break;  // prototype db has zero line at end
0159         }
0160 
0161         int start = theBadWires->chambers[i].pointer;  // where this chamber's bad channels start in vector<BadChannel>
0162         int nbad = theBadWires->chambers[i].bad_channels;
0163 
0164         CSCDetId id = indexer.detIdFromChamberIndex(indexc);  // We need this to build layer index (1-3240)
0165 
0166         for (int j = start - 1; j < start + nbad - 1; ++j) {  // bad channels in this chamber
0167           short lay = theBadWires->channels[j].layer;         // value 1-6
0168 
0169           // The following is not in standard CSCConditions version but was required for our prototype bad strip db
0170           if (lay == 0) {
0171             log << "WARNING: layer index = 0. Quitting. " << std::endl;
0172             break;
0173           }
0174 
0175           short chan = theBadWires->channels[j].channel;  // value 1-112
0176                                                           //    short f1 = theBadWires->channels[j].flag1;
0177                                                           //    short f2 = theBadWires->channels[j].flag2;
0178                                                           //    short f3 = theBadWires->channels[j].flag3;
0179           int indexl = indexer.layerIndex(id.endcap(), id.station(), id.ring(), id.chamber(), lay);
0180 
0181           // Test output to monitor filling
0182           log << "count " << ++icount << " bad channel " << chan << " in layer " << lay << " of chamber=" << id
0183               << " chamber index=" << indexc << " layer index=" << indexl << std::endl;
0184 
0185           badWireWords[indexl - 1].set(chan - 1, 1);  // set bit in 112-bit bitset representing this layer
0186         }  // j
0187       }  // i
0188     }
0189   }
0190 
0191   const std::bitset<112>& CSCReadBadWiresAnalyzer::badWireWord(const CSCDetId& id) const {
0192     CSCIndexer indexer;
0193     return badWireWords[indexer.layerIndex(id) - 1];
0194   }
0195 
0196   DEFINE_FWK_MODULE(CSCReadBadWiresAnalyzer);
0197 }  // namespace edmtest