Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-11-17 01:40:47

0001 // system includes
0002 #include <vector>
0003 #include <cmath>
0004 #include <fstream>
0005 #include <iostream>
0006 
0007 // user includes
0008 #include "CommonTools/ConditionDBWriter/interface/ConditionDBWriter.h"
0009 #include "CondFormats/SiPixelObjects/interface/SiPixelQuality.h"
0010 #include "DataFormats/DetId/interface/DetId.h"
0011 #include "DataFormats/TrackerCommon/interface/PixelBarrelName.h"
0012 #include "DataFormats/TrackerCommon/interface/PixelEndcapName.h"
0013 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0014 #include "FWCore/Framework/interface/EventSetup.h"
0015 #include "FWCore/Framework/interface/MakerMacros.h"
0016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0017 #include "FWCore/ParameterSet/interface/FileInPath.h"
0018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0019 #include "FWCore/Utilities/interface/Exception.h"
0020 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0021 
0022 class SiPixelBadModuleByHandBuilder : public ConditionDBWriter<SiPixelQuality> {
0023 public:
0024   explicit SiPixelBadModuleByHandBuilder(const edm::ParameterSet&);
0025   ~SiPixelBadModuleByHandBuilder() override;
0026 
0027 private:
0028   std::unique_ptr<SiPixelQuality> getNewObject() override;
0029 
0030   void algoBeginRun(const edm::Run& run, const edm::EventSetup& es) override {
0031     if (!tTopo_) {
0032       tTopo_ = std::make_unique<TrackerTopology>(es.getData(tkTopoToken_));
0033     }
0034   };
0035 
0036 private:
0037   const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> tkTopoToken_;
0038   const bool printdebug_;
0039   typedef std::vector<edm::ParameterSet> Parameters;
0040   Parameters BadModuleList_;
0041   const std::string ROCListFile_;
0042   std::unique_ptr<TrackerTopology> tTopo_;
0043 };
0044 
0045 SiPixelBadModuleByHandBuilder::SiPixelBadModuleByHandBuilder(const edm::ParameterSet& iConfig)
0046     : ConditionDBWriter<SiPixelQuality>(iConfig),
0047       tkTopoToken_(esConsumes<edm::Transition::BeginRun>()),
0048       printdebug_(iConfig.getUntrackedParameter<bool>("printDebug", false)),
0049       BadModuleList_(iConfig.getUntrackedParameter<Parameters>("BadModuleList")),
0050       ROCListFile_(iConfig.getUntrackedParameter<std::string>("ROCListFile")) {}
0051 
0052 SiPixelBadModuleByHandBuilder::~SiPixelBadModuleByHandBuilder() = default;
0053 
0054 std::unique_ptr<SiPixelQuality> SiPixelBadModuleByHandBuilder::getNewObject() {
0055   auto obj = std::make_unique<SiPixelQuality>();
0056 
0057   for (Parameters::iterator it = BadModuleList_.begin(); it != BadModuleList_.end(); ++it) {
0058     if (printdebug_) {
0059       edm::LogInfo("SiPixelBadModuleByHandBuilder") << " BadModule " << *it << " \t" << std::endl;
0060     }
0061 
0062     SiPixelQuality::disabledModuleType BadModule;
0063     BadModule.errorType = 3;
0064     BadModule.BadRocs = 0;
0065     BadModule.DetID = it->getParameter<uint32_t>("detid");
0066     std::string errorstring = it->getParameter<std::string>("errortype");
0067     if (printdebug_) {
0068       edm::LogInfo("SiPixelBadModuleByHandBuilder")
0069           << "now looking at detid " << BadModule.DetID << ", string " << errorstring << std::endl;
0070     }
0071 
0072     //////////////////////////////////////
0073     //  errortype "whole" = int 0 in DB //
0074     //  errortype "tbmA" = int 1 in DB  //
0075     //  errortype "tbmB" = int 2 in DB  //
0076     //  errortype "none" = int 3 in DB  //
0077     //////////////////////////////////////
0078 
0079     /////////////////////////////////////////////////
0080     //each bad roc correspond to a bit to 1: num=  //
0081     // 0 <-> all good rocs                         //
0082     // 1 <-> only roc 0 bad                        //
0083     // 2<-> only roc 1 bad                         //
0084     // 3<->  roc 0 and 1 bad                       //
0085     // 4 <-> only roc 2 bad                        //
0086     //  ...                                        //
0087     /////////////////////////////////////////////////
0088 
0089     if (errorstring == "whole") {
0090       BadModule.errorType = 0;
0091       BadModule.BadRocs = 65535;
0092     }  //corresponds to all rocs being bad
0093     else if (errorstring == "tbmA") {
0094       BadModule.errorType = 1;
0095       BadModule.BadRocs = 255;
0096     }  //corresponds to Rocs 0-7 being bad
0097     else if (errorstring == "tbmB") {
0098       BadModule.errorType = 2;
0099       BadModule.BadRocs = 65280;
0100     }  //corresponds to Rocs 8-15 being bad
0101     else if (errorstring == "none") {
0102       BadModule.errorType = 3;
0103       //       badroclist_ = iConfig.getUntrackedParameter<std::vector<uint32_t> >("badroclist");
0104       std::vector<uint32_t> BadRocList = it->getParameter<std::vector<uint32_t> >("badroclist");
0105       short badrocs = 0;
0106       for (std::vector<uint32_t>::iterator iter = BadRocList.begin(); iter != BadRocList.end(); ++iter) {
0107         badrocs += 1 << *iter;  // 1 << *iter = 2^{*iter} using bitwise shift
0108       }
0109       BadModule.BadRocs = badrocs;
0110     }
0111 
0112     else
0113       edm::LogError("SiPixelQuality") << "trying to fill error type " << errorstring << ", which is not defined!";
0114     obj->addDisabledModule(BadModule);
0115   }
0116 
0117   // fill DB from DQM list
0118   if (!ROCListFile_.empty()) {
0119     std::map<uint32_t, uint32_t> disabledModules;
0120     std::ifstream aFile(ROCListFile_.c_str());
0121     std::string aLine;
0122     while (std::getline(aFile, aLine)) {
0123       char name[100];
0124       int roc;
0125       sscanf(aLine.c_str(), "%s %d", name, &roc);
0126       uint32_t detId;
0127       if (name[0] == 'B') {
0128         PixelBarrelName bn(name, true);
0129         detId = bn.getDetId(tTopo_.get());
0130       } else {
0131         PixelEndcapName en(name, true);
0132         detId = en.getDetId(tTopo_.get());
0133       }
0134       std::map<uint32_t, uint32_t>::iterator it = disabledModules.find(detId);
0135       if (it == disabledModules.end())
0136         it = disabledModules.insert(disabledModules.begin(), std::make_pair(detId, 0));
0137       it->second |= 1 << roc;
0138       //edm::LogPrint("SiPixelBadModuleByHandBuilder")<<"New module read "<<name<<" "<<roc<<" --> "<<detId<<" "<<std::bitset<32>(it->second)<<std::endl;
0139     }
0140 
0141     for (const auto& it : disabledModules) {
0142       SiPixelQuality::disabledModuleType BadModule;
0143       BadModule.DetID = it.first;
0144       if (it.second == 65535) {  // "whole"
0145         BadModule.errorType = 0;
0146         BadModule.BadRocs = 65535;
0147       }                             //corresponds to all rocs being bad
0148       else if (it.second == 255) {  // "tbmA"
0149         BadModule.errorType = 1;
0150         BadModule.BadRocs = 255;
0151       }                               //corresponds to Rocs 0-7 being bad
0152       else if (it.second == 65280) {  // "tbmB"
0153         BadModule.errorType = 2;
0154         BadModule.BadRocs = 65280;
0155       }       //corresponds to Rocs 8-15 being bad
0156       else {  // "none"
0157         BadModule.errorType = 3;
0158         BadModule.BadRocs = it.second;
0159       }
0160 
0161       obj->addDisabledModule(BadModule);
0162       if (printdebug_) {
0163         edm::LogVerbatim("SiPixelBadModuleByHandBuilder")
0164             << "New module added: " << tTopo_->print(BadModule.DetID) << ", errorType: " << BadModule.errorType
0165             << ", BadRocs: " << std::bitset<16>(it.second) << std::endl;
0166       }
0167     }
0168   }
0169 
0170   return obj;
0171 }
0172 DEFINE_FWK_MODULE(SiPixelBadModuleByHandBuilder);