Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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