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);
0030 }
0031
0032 explicit CSCReadBadWiresAnalyzer(int i) {}
0033 virtual ~CSCReadBadWiresAnalyzer() {}
0034 virtual void analyze(const edm::Event& e, const edm::EventSetup& c);
0035
0036
0037
0038
0039 bool readBadChannels() const { return readBadChannels_; }
0040
0041 void fillBadWireWords(edm::LogSystem&);
0042
0043
0044 const std::bitset<112>& badWireWord(const CSCDetId& id) const;
0045
0046 private:
0047 bool outputToFile;
0048 bool readBadChannels_;
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
0067 fillBadWireWords(log);
0068
0069 CSCIndexer indexer;
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
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
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
0097 CSCDetId id2 = CSCDetId(id.endcap(), id.station(), id.ring(), id.chamber(), lay);
0098 std::bitset<112> ibits = badWireWord(id2);
0099
0100
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
0111
0112
0113
0114
0115
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
0139 badWireWords.assign(3240, 0);
0140 if (readBadChannels()) {
0141
0142
0143
0144
0145
0146
0147
0148 CSCIndexer indexer;
0149
0150 int icount = 0;
0151
0152 for (size_t i = 0; i < theBadWires->chambers.size(); ++i) {
0153 int indexc = theBadWires->chambers[i].chamber_index;
0154
0155
0156 if (indexc == 0) {
0157 log << "WARNING: chamber index = 0. Quitting. " << std::endl;
0158 break;
0159 }
0160
0161 int start = theBadWires->chambers[i].pointer;
0162 int nbad = theBadWires->chambers[i].bad_channels;
0163
0164 CSCDetId id = indexer.detIdFromChamberIndex(indexc);
0165
0166 for (int j = start - 1; j < start + nbad - 1; ++j) {
0167 short lay = theBadWires->channels[j].layer;
0168
0169
0170 if (lay == 0) {
0171 log << "WARNING: layer index = 0. Quitting. " << std::endl;
0172 break;
0173 }
0174
0175 short chan = theBadWires->channels[j].channel;
0176
0177
0178
0179 int indexl = indexer.layerIndex(id.endcap(), id.station(), id.ring(), id.chamber(), lay);
0180
0181
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);
0186 }
0187 }
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 }