Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-12-02 10:35:57

0001 // system include files
0002 #include <vector>
0003 #include <iostream>
0004 #include <fstream>
0005 #include <sstream>
0006 
0007 // user include files
0008 #include "CalibFormats/SiStripObjects/interface/SiStripDetCabling.h"
0009 #include "CalibTracker/Records/interface/SiStripDetCablingRcd.h"
0010 #include "CalibTracker/SiStripCommon/interface/SiStripDetInfoFileReader.h"
0011 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0012 #include "CondFormats/DataRecord/interface/SiStripBadStripRcd.h"
0013 #include "CondFormats/SiStripObjects/interface/SiStripBadStrip.h"
0014 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0015 #include "DataFormats/SiStripCommon/interface/ConstantsForHardwareSystems.h" /* for STRIPS_PER_APV*/
0016 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0018 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0019 #include "FWCore/ParameterSet/interface/FileInPath.h"
0020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0021 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0022 #include "FWCore/ServiceRegistry/interface/Service.h"
0023 #include "FWCore/Utilities/interface/Exception.h"
0024 
0025 namespace sistrip {
0026   static const uint16_t NOT_A_FEDID = static_cast<uint16_t>(FEDNumbering::NOT_A_FEDID);
0027 }
0028 
0029 class SiStripBadChannelPatcher : public edm::one::EDAnalyzer<> {
0030 public:
0031   explicit SiStripBadChannelPatcher(const edm::ParameterSet& iConfig)
0032       : record_(iConfig.getParameter<std::string>("Record")),
0033         printDebug_(iConfig.getParameter<bool>("printDebug")),
0034         detIdsToExclude_(iConfig.getParameter<std::vector<unsigned int>>("detIdsToExclude")),
0035         detIdsToInclude_(iConfig.getParameter<std::vector<unsigned int>>("detIdsToInclude")),
0036         fedsToExclude_(iConfig.getParameter<std::vector<unsigned int>>("FEDsToExclude")),
0037         fedsToInclude_(iConfig.getParameter<std::vector<unsigned int>>("FEDsToInclude")),
0038         badStripToken_(esConsumes()),
0039         cablingToken_(esConsumes()) {}
0040 
0041   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0042   ~SiStripBadChannelPatcher() override = default;
0043 
0044 private:
0045   void analyze(const edm::Event&, const edm::EventSetup&) override;
0046   std::unique_ptr<SiStripBadStrip> getNewObject(const edm::EventSetup& iSetup);
0047   unsigned int fedFromDetId(const uint32_t& detid);
0048   void addDetIdsFromExcludedFEDs(const std::vector<uint32_t>& allDetIds);
0049 
0050   // member data
0051   const SiStripDetCabling* detCabling_;
0052   const std::string record_;
0053   const bool printDebug_;
0054   std::vector<uint32_t> detIdsToExclude_;
0055   std::vector<uint32_t> detIdsToInclude_;
0056   std::vector<unsigned int> fedsToExclude_;
0057   std::vector<unsigned int> fedsToInclude_;
0058   const edm::ESGetToken<SiStripBadStrip, SiStripBadStripRcd> badStripToken_;    /*!< ES token for the bad strips */
0059   const edm::ESGetToken<SiStripDetCabling, SiStripDetCablingRcd> cablingToken_; /*!< ES token for the cabling */
0060 };
0061 
0062 //-----------------------------------------------------------------------------------------------//
0063 void SiStripBadChannelPatcher::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0064   edm::ParameterSetDescription desc;
0065   desc.setComment(
0066       "create a SiStripBadStrip payload starting from one in DB and excluding or adding entire modules from a list");
0067   desc.add<std::string>("Record", "SiStripBadStrip")->setComment("Record to match in the PoolDBOutputService");
0068   desc.add<bool>("printDebug", false)->setComment("full debug printout");
0069   desc.add<std::vector<unsigned int>>("detIdsToExclude", {})->setComment("list of detIds to exclude");
0070   desc.add<std::vector<unsigned int>>("detIdsToInclude", {})->setComment("list of detIds to include");
0071   desc.add<std::vector<unsigned int>>("FEDsToExclude", {})->setComment("list of FEDs to exclude");
0072   desc.add<std::vector<unsigned int>>("FEDsToInclude", {})->setComment("list of FEDs to include");
0073   descriptions.addWithDefaultLabel(desc);
0074 }
0075 
0076 //-----------------------------------------------------------------------------------------------//
0077 unsigned int SiStripBadChannelPatcher::fedFromDetId(const uint32_t& detid) {
0078   // For the cabled det_id retrieve the FEDid
0079   const std::vector<const FedChannelConnection*>& conns = detCabling_->getConnections(detid);
0080   if (conns.empty()) {
0081     edm::LogWarning("SiStripBadChannelPatcher")
0082         << " DetId " << detid << " appears to be uncabled, returning NOT_A_FEDID !";
0083     return sistrip::NOT_A_FEDID;
0084   }
0085   unsigned int lFedId = sistrip::NOT_A_FEDID;
0086   for (uint32_t ch = 0; ch < conns.size(); ch++) {
0087     if (conns[ch] && conns[ch]->isConnected()) {
0088       lFedId = conns[ch]->fedId();
0089       LogDebug("SiStripBadChannelPatcher") << "obtained FED id " << ch << " " << lFedId;
0090       if (lFedId < sistrip::FED_ID_MIN || lFedId > sistrip::FED_ID_MAX) {
0091         edm::LogWarning("SiStripBadChannelPatcher") << lFedId << " for detid " << detid << " connection " << ch;
0092         continue;
0093       } else {
0094         break;
0095       }
0096     }
0097   }
0098   return lFedId;
0099 }
0100 
0101 //-----------------------------------------------------------------------------------------------//
0102 void SiStripBadChannelPatcher::addDetIdsFromExcludedFEDs(const std::vector<uint32_t>& allDetIds) {
0103   for (const auto& detid : allDetIds) {
0104     const auto& currentFED = this->fedFromDetId(detid);
0105     if (std::count(fedsToInclude_.begin(), fedsToInclude_.end(), currentFED)) {
0106       detIdsToInclude_.push_back(detid);
0107     }
0108   }
0109 }
0110 
0111 //-----------------------------------------------------------------------------------------------//
0112 void SiStripBadChannelPatcher::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0113   using namespace edm;
0114 
0115   // create the patched bad strips payload
0116   std::unique_ptr<SiStripBadStrip> theBadStrips = this->getNewObject(iSetup);
0117 
0118   // write out the BadStrip record
0119   edm::Service<cond::service::PoolDBOutputService> poolDbService;
0120 
0121   if (poolDbService.isAvailable()) {
0122     poolDbService->writeOneIOV(*theBadStrips, poolDbService->currentTime(), record_);
0123   } else {
0124     throw std::runtime_error("PoolDBService required.");
0125   }
0126 }
0127 
0128 //-----------------------------------------------------------------------------------------------//
0129 std::unique_ptr<SiStripBadStrip> SiStripBadChannelPatcher::getNewObject(const edm::EventSetup& iSetup) {
0130   edm::LogInfo("SiStripBadChannelPatcher") << "... creating dummy SiStripBadStrip Data";
0131 
0132   // this is the output object
0133   auto obj = std::make_unique<SiStripBadStrip>();
0134 
0135   // get the cabling object
0136   detCabling_ = &iSetup.getData(cablingToken_);
0137 
0138   // get the strips to add
0139   std::vector<uint32_t> detids;
0140   const auto& payload = iSetup.getData(badStripToken_);
0141   payload.getDetIds(detids);
0142 
0143   // copy the exisiting channels (excluding the ones to remove)
0144   for (const auto& id : detids) {
0145     // check on the detids to exclude
0146     if (std::count(detIdsToExclude_.begin(), detIdsToExclude_.end(), id)) {
0147       edm::LogInfo("SiStripBadChannelPatcher") << "I AM GOING TO EXCLUDE DETID: " << id;
0148       continue;
0149     } else {
0150       LogDebug("SiStripBadChannelPatcher") << "I AM GOING TO KEEP DETID: " << id;
0151     }
0152 
0153     // check on the FEDs to exclude
0154     const auto& currentFED = this->fedFromDetId(id);
0155     if (std::count(fedsToExclude_.begin(), fedsToExclude_.end(), currentFED)) {
0156       edm::LogInfo("SiStripBadChannelPatcher") << "I AM GOING TO EXCLUDE DETID: " << id;
0157       continue;
0158     } else {
0159       LogDebug("SiStripBadChannelPatcher") << "I AM GOING TO KEEP DETID: " << id;
0160     }
0161 
0162     SiStripBadStrip::Range range = payload.getRange(id);
0163     std::vector<unsigned int> theSiStripVector;
0164     unsigned int theBadStripRange;
0165     for (std::vector<unsigned int>::const_iterator badStrip = range.first; badStrip != range.second; ++badStrip) {
0166       unsigned short firstBadStrip = payload.decode(*badStrip).firstStrip;
0167       unsigned short nConsecutiveBadStrips = payload.decode(*badStrip).range;
0168       theBadStripRange = obj->encode(firstBadStrip, nConsecutiveBadStrips);
0169       theSiStripVector.push_back(theBadStripRange);
0170     }
0171     SiStripBadStrip::Range outRange(theSiStripVector.begin(), theSiStripVector.end());
0172     if (!obj->put(id, outRange))
0173       edm::LogError("SiStripBadChannelPatcher") << "[SiStripBadChannelPatcher::analyze] detid already exists";
0174   }  // loop on the detids of the original payload
0175 
0176   const auto& detInfo =
0177       SiStripDetInfoFileReader::read(edm::FileInPath{SiStripDetInfoFileReader::kDefaultFile}.fullPath());
0178 
0179   // add to the list of DetIds to include also the one from the list of FEDs
0180   const std::vector<uint32_t>& allDetIds = detInfo.getAllDetIds();
0181   this->addDetIdsFromExcludedFEDs(allDetIds);
0182 
0183   // add more full bad detids
0184   if (!detIdsToInclude_.empty()) {
0185     edm::LogInfo("SiStripBadChannelPatcher") << "I AM GOING TO ADD MORE DETIDS";
0186 
0187     std::stringstream ss;
0188     for (const auto& detid : detIdsToInclude_) {
0189       edm::LogInfo("SiStripBadChannelPatcher") << "I AM GOING TO ADD DETID: " << detid;
0190       const auto& nAPVs = detInfo.getNumberOfApvsAndStripLength(detid).first;
0191 
0192       std::vector<unsigned int> theSiStripVector;
0193       unsigned short firstBadStrip{0}, nConsecutiveBadStrips{0};
0194       unsigned int theBadStripRange;
0195 
0196       for (unsigned int n = 0; n < nAPVs; n++) {
0197         firstBadStrip = n * sistrip::STRIPS_PER_APV;
0198         nConsecutiveBadStrips = sistrip::STRIPS_PER_APV;
0199         theBadStripRange = obj->encode(firstBadStrip, nConsecutiveBadStrips);
0200 
0201         if (printDebug_) {
0202           ss << "detid " << detid << " \t"
0203              << " firstBadStrip " << firstBadStrip << "\t "
0204              << " nConsecutiveBadStrips " << nConsecutiveBadStrips << "\t "
0205              << " packed integer " << std::hex << theBadStripRange << std::dec << std::endl;
0206         }
0207 
0208         theSiStripVector.push_back(theBadStripRange);
0209       }
0210 
0211       SiStripBadStrip::Range outRange(theSiStripVector.begin(), theSiStripVector.end());
0212       if (!obj->put(detid, outRange))
0213         edm::LogError("SiStripBadChannelPatcher") << "[SiStripBadChannelPatcher::analyze] detid already exists";
0214     }  // loop on detids to include
0215 
0216     if (printDebug_) {
0217       // print the added strips
0218       edm::LogInfo("SiStripBadChannelPatcher") << ss.str();
0219     }
0220 
0221   }  // if there is any new channel to append
0222 
0223   return obj;
0224 }
0225 
0226 #include "FWCore/PluginManager/interface/ModuleDef.h"
0227 #include "FWCore/Framework/interface/MakerMacros.h"
0228 
0229 DEFINE_FWK_MODULE(SiStripBadChannelPatcher);