Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-05 04:09:22

0001 // -*- C++ -*-
0002 //
0003 // Package:    CalibTracker/SiStripESProducers
0004 // Class:      SiStripBadModuleConfigurableFakeESSource
0005 //
0006 /**\class SiStripBadModuleConfigurableFakeESSource SiStripBadModuleConfigurableFakeESSource.h CalibTracker/SiStripESProducers/plugins/SiStripBadModuleConfigurableFakeESSource.cc
0007 
0008  Description: "fake" SiStripBadStrip ESProducer - configurable list of bad modules
0009 
0010  Implementation:
0011      Port of SiStripBadModuleGenerator and templated fake ESSource to an edm::ESProducer
0012 */
0013 
0014 // system include files
0015 #include <memory>
0016 
0017 // user include files
0018 #include "FWCore/Framework/interface/ESProducer.h"
0019 #include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
0020 
0021 #include "CondFormats/SiStripObjects/interface/SiStripBadStrip.h"
0022 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0023 #include "DataFormats/SiStripDetId/interface/SiStripDetId.h"
0024 #include "CondFormats/DataRecord/interface/SiStripCondDataRecords.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 
0027 #include "CalibTracker/SiStripCommon/interface/SiStripDetInfoFileReader.h"
0028 
0029 class SiStripBadModuleConfigurableFakeESSource : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
0030 public:
0031   SiStripBadModuleConfigurableFakeESSource(const edm::ParameterSet&);
0032   ~SiStripBadModuleConfigurableFakeESSource() override;
0033 
0034   void setIntervalFor(const edm::eventsetup::EventSetupRecordKey&,
0035                       const edm::IOVSyncValue& iov,
0036                       edm::ValidityInterval& iValidity) override;
0037 
0038   typedef std::unique_ptr<SiStripBadStrip> ReturnType;
0039   ReturnType produce(const SiStripBadModuleRcd&);
0040 
0041 private:
0042   using Parameters = std::vector<edm::ParameterSet>;
0043   Parameters m_badComponentList;
0044   Parameters m_badAPVsList;
0045   bool m_printDebug;
0046   bool m_doByAPVs;
0047   SiStripDetInfo m_detInfo;
0048   const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> trackTopoToken_;
0049 
0050   std::vector<uint32_t> selectDetectors(const TrackerTopology* tTopo, const std::vector<uint32_t>& detIds) const;
0051   std::vector<std::pair<uint32_t, std::vector<uint32_t>>> selectAPVs() const;
0052 };
0053 
0054 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0055 #include "CalibFormats/SiStripObjects/interface/SiStripQuality.h"
0056 
0057 SiStripBadModuleConfigurableFakeESSource::SiStripBadModuleConfigurableFakeESSource(const edm::ParameterSet& iConfig)
0058     : trackTopoToken_(setWhatProduced(this).consumes()) {
0059   findingRecord<SiStripBadModuleRcd>();
0060 
0061   m_badComponentList = iConfig.getUntrackedParameter<Parameters>("BadComponentList");
0062   m_doByAPVs = iConfig.getUntrackedParameter<bool>("doByAPVs", false);
0063   m_badAPVsList = iConfig.getUntrackedParameter<Parameters>("BadAPVList");
0064   m_printDebug = iConfig.getUntrackedParameter<bool>("printDebug", false);
0065   m_detInfo = SiStripDetInfoFileReader::read(iConfig.getParameter<edm::FileInPath>("SiStripDetInfoFile").fullPath());
0066 }
0067 
0068 SiStripBadModuleConfigurableFakeESSource::~SiStripBadModuleConfigurableFakeESSource() {}
0069 
0070 void SiStripBadModuleConfigurableFakeESSource::setIntervalFor(const edm::eventsetup::EventSetupRecordKey&,
0071                                                               const edm::IOVSyncValue& iov,
0072                                                               edm::ValidityInterval& iValidity) {
0073   iValidity = edm::ValidityInterval{iov.beginOfTime(), iov.endOfTime()};
0074 }
0075 
0076 // ------------ method called to produce the data  ------------
0077 SiStripBadModuleConfigurableFakeESSource::ReturnType SiStripBadModuleConfigurableFakeESSource::produce(
0078     const SiStripBadModuleRcd& iRecord) {
0079   using namespace edm::es;
0080 
0081   TrackerTopology const& tTopo = iRecord.get(trackTopoToken_);
0082 
0083   auto quality = std::make_unique<SiStripQuality>(m_detInfo);
0084 
0085   if (!m_doByAPVs) {
0086     std::vector<uint32_t> selDetIds{selectDetectors(&tTopo, m_detInfo.getAllDetIds())};
0087     edm::LogInfo("SiStripQualityConfigurableFakeESSource")
0088         << "[produce] number of selected dets to be removed " << selDetIds.size() << std::endl;
0089 
0090     std::stringstream ss;
0091     for (const auto selId : selDetIds) {
0092       SiStripQuality::InputVector theSiStripVector;
0093 
0094       unsigned short firstBadStrip{0};
0095       unsigned short NconsecutiveBadStrips = m_detInfo.getNumberOfApvsAndStripLength(selId).first * 128;
0096       unsigned int theBadStripRange{quality->encode(firstBadStrip, NconsecutiveBadStrips)};
0097 
0098       if (m_printDebug) {
0099         ss << "detid " << selId << " \t"
0100            << " firstBadStrip " << firstBadStrip << "\t "
0101            << " NconsecutiveBadStrips " << NconsecutiveBadStrips << "\t "
0102            << " packed integer " << std::hex << theBadStripRange << std::dec << std::endl;
0103       }
0104 
0105       theSiStripVector.push_back(theBadStripRange);
0106 
0107       if (!quality->put(selId, SiStripBadStrip::Range{theSiStripVector.begin(), theSiStripVector.end()})) {
0108         edm::LogError("SiStripQualityConfigurableFakeESSource") << "[produce] detid already exists";
0109       }
0110     }
0111     if (m_printDebug) {
0112       edm::LogInfo("SiStripQualityConfigurableFakeESSource") << ss.str();
0113     }
0114     quality->cleanUp();
0115     //quality->fillBadComponents();
0116   } else {
0117     std::vector<std::pair<uint32_t, std::vector<uint32_t>>> selAPVs{selectAPVs()};
0118     edm::LogInfo("SiStripQualityConfigurableFakeESSource")
0119         << "[produce] number of selected dets to be removed " << selAPVs.size() << std::endl;
0120 
0121     std::stringstream ss;
0122     for (const auto& selId : selAPVs) {
0123       SiStripQuality::InputVector theSiStripVector;
0124       auto the_detid = selId.first;
0125 
0126       for (const auto apv : selId.second) {
0127         unsigned short firstBadStrip = apv * 128;
0128         unsigned short NconsecutiveBadStrips = 128;
0129         unsigned int theBadStripRange{quality->encode(firstBadStrip, NconsecutiveBadStrips)};
0130 
0131         if (m_printDebug) {
0132           ss << "detid " << the_detid << " \t"
0133              << " firstBadStrip " << firstBadStrip << "\t "
0134              << " NconsecutiveBadStrips " << NconsecutiveBadStrips << "\t "
0135              << " packed integer " << std::hex << theBadStripRange << std::dec << std::endl;
0136         }
0137 
0138         theSiStripVector.push_back(theBadStripRange);
0139       }
0140 
0141       if (!quality->put(the_detid, SiStripBadStrip::Range{theSiStripVector.begin(), theSiStripVector.end()})) {
0142         edm::LogError("SiStripQualityConfigurableFakeESSource") << "[produce] detid already exists";
0143       }
0144     }  // loop on the packed list of detid/apvs
0145 
0146     if (m_printDebug) {
0147       edm::LogInfo("SiStripQualityConfigurableFakeESSource") << ss.str();
0148     }
0149     quality->cleanUp();
0150 
0151   }  // do it by APVs
0152 
0153   if (m_printDebug) {
0154     std::stringstream ss1;
0155     for (const auto& badComp : quality->getBadComponentList()) {
0156       ss1 << "bad module " << badComp.detid << " " << badComp.BadModule << "\n";
0157     }
0158     edm::LogInfo("SiStripQualityConfigurableFakeESSource") << ss1.str();
0159   }
0160 
0161   return quality;
0162 }
0163 
0164 namespace {
0165   bool _isSel(uint32_t requested,
0166               uint32_t i) {  // internal helper: accept all i if requested is 0, otherwise require match
0167     return (requested == 0) || (requested == i);
0168   }
0169 
0170   SiStripDetId::SubDetector subDetFromString(const std::string& subDetStr) {
0171     SiStripDetId::SubDetector subDet = SiStripDetId::UNKNOWN;
0172     if (subDetStr == "TIB")
0173       subDet = SiStripDetId::TIB;
0174     else if (subDetStr == "TID")
0175       subDet = SiStripDetId::TID;
0176     else if (subDetStr == "TOB")
0177       subDet = SiStripDetId::TOB;
0178     else if (subDetStr == "TEC")
0179       subDet = SiStripDetId::TEC;
0180     return subDet;
0181   }
0182 }  // namespace
0183 
0184 std::vector<std::pair<uint32_t, std::vector<uint32_t>>> SiStripBadModuleConfigurableFakeESSource::selectAPVs() const {
0185   std::vector<std::pair<uint32_t, std::vector<uint32_t>>> selList;
0186   selList.clear();
0187 
0188   for (const auto& badAPV : m_badAPVsList) {
0189     const uint32_t det{badAPV.getParameter<uint32_t>("DetId")};
0190     std::vector<uint32_t> apvs{badAPV.getParameter<std::vector<uint32_t>>("APVs")};
0191     auto pair = std::make_pair(det, apvs);
0192     selList.push_back(pair);
0193   }
0194   return selList;
0195 }
0196 
0197 std::vector<uint32_t> SiStripBadModuleConfigurableFakeESSource::selectDetectors(
0198     const TrackerTopology* tTopo, const std::vector<uint32_t>& detIds) const {
0199   std::vector<uint32_t> selList;
0200   std::stringstream ss;
0201   for (const auto& badComp : m_badComponentList) {
0202     const std::string subDetStr{badComp.getParameter<std::string>("SubDet")};
0203     if (m_printDebug)
0204       ss << "Bad SubDet " << subDetStr << " \t";
0205     const SiStripDetId::SubDetector subDet = subDetFromString(subDetStr);
0206 
0207     const std::vector<uint32_t> genericBadDetIds{
0208         badComp.getUntrackedParameter<std::vector<uint32_t>>("detidList", std::vector<uint32_t>())};
0209     const bool anySubDet{!genericBadDetIds.empty()};
0210 
0211     std::cout << "genericBadDetIds.size() = " << genericBadDetIds.size() << std::endl;
0212 
0213     using DetIdIt = std::vector<uint32_t>::const_iterator;
0214     const DetIdIt beginDetIt = std::lower_bound(
0215         detIds.begin(), detIds.end(), DetId(DetId::Tracker, anySubDet ? SiStripDetId::TIB : subDet).rawId());
0216     const DetIdIt endDetIt = std::lower_bound(
0217         detIds.begin(), detIds.end(), DetId(DetId::Tracker, anySubDet ? SiStripDetId::TEC + 1 : subDet + 1).rawId());
0218 
0219     if (anySubDet) {
0220       std::copy_if(beginDetIt, endDetIt, std::back_inserter(selList), [&genericBadDetIds](uint32_t detId) {
0221         std::cout << "AnySubDet" << detId << std::endl;
0222         return std::find(genericBadDetIds.begin(), genericBadDetIds.end(), detId) != genericBadDetIds.end();
0223       });
0224     } else {
0225       switch (subDet) {
0226         case SiStripDetId::TIB:
0227           std::copy_if(beginDetIt, endDetIt, std::back_inserter(selList), [tTopo, &badComp](uint32_t detectorId) {
0228             const DetId detId{detectorId};
0229             return ((detId.subdetId() == SiStripDetId::TIB) &&
0230                     _isSel(badComp.getParameter<uint32_t>("layer"), tTopo->tibLayer(detId)) &&
0231                     _isSel(badComp.getParameter<uint32_t>("bkw_frw"), tTopo->tibIsZPlusSide(detId) ? 2 : 1) &&
0232                     _isSel(badComp.getParameter<uint32_t>("int_ext"), tTopo->tibIsInternalString(detId) ? 1 : 2) &&
0233                     _isSel(badComp.getParameter<uint32_t>("ster"),
0234                            tTopo->tibIsStereo(detId) ? 1 : (tTopo->tibIsRPhi(detId) ? 2 : -1)) &&
0235                     _isSel(badComp.getParameter<uint32_t>("string_"), tTopo->tibString(detId)) &&
0236                     _isSel(badComp.getParameter<uint32_t>("detid"), detId.rawId()));
0237           });
0238           break;
0239         case SiStripDetId::TID:
0240           std::copy_if(beginDetIt, endDetIt, std::back_inserter(selList), [tTopo, &badComp](uint32_t detectorId) {
0241             const DetId detId{detectorId};
0242             return ((detId.subdetId() == SiStripDetId::TID) &&
0243                     _isSel(badComp.getParameter<uint32_t>("wheel"), tTopo->tidWheel(detId)) &&
0244                     _isSel(badComp.getParameter<uint32_t>("side"), tTopo->tidIsZPlusSide(detId) ? 2 : 1) &&
0245                     _isSel(badComp.getParameter<uint32_t>("ster"),
0246                            tTopo->tidIsStereo(detId) ? 1 : (tTopo->tidIsRPhi(detId) ? 2 : -1)) &&
0247                     _isSel(badComp.getParameter<uint32_t>("ring"), tTopo->tidRing(detId)) &&
0248                     _isSel(badComp.getParameter<uint32_t>("detid"), detId.rawId()));
0249           });
0250           break;
0251         case SiStripDetId::TOB:
0252           std::copy_if(beginDetIt, endDetIt, std::back_inserter(selList), [tTopo, &badComp](uint32_t detectorId) {
0253             const DetId detId{detectorId};
0254             return ((detId.subdetId() == SiStripDetId::TOB) &&
0255                     _isSel(badComp.getParameter<uint32_t>("layer"), tTopo->tobLayer(detId)) &&
0256                     _isSel(badComp.getParameter<uint32_t>("bkw_frw"), tTopo->tobIsZPlusSide(detId) ? 2 : 1) &&
0257                     _isSel(badComp.getParameter<uint32_t>("ster"),
0258                            tTopo->tobIsStereo(detId) ? 1 : (tTopo->tobIsRPhi(detId) ? 2 : -1)) &&
0259                     _isSel(badComp.getParameter<uint32_t>("rod"), tTopo->tobRod(detId)) &&
0260                     _isSel(badComp.getParameter<uint32_t>("detid"), detId.rawId()));
0261           });
0262           break;
0263         case SiStripDetId::TEC:
0264           std::copy_if(beginDetIt, endDetIt, std::back_inserter(selList), [tTopo, &badComp](uint32_t detectorId) {
0265             const DetId detId{detectorId};
0266             return ((detId.subdetId() == SiStripDetId::TEC) &&
0267                     _isSel(badComp.getParameter<uint32_t>("wheel"), tTopo->tecWheel(detId)) &&
0268                     _isSel(badComp.getParameter<uint32_t>("side"), tTopo->tecIsZPlusSide(detId) ? 2 : 1) &&
0269                     _isSel(badComp.getParameter<uint32_t>("ster"), tTopo->tecIsStereo(detId) ? 1 : 2) &&
0270                     _isSel(badComp.getParameter<uint32_t>("petal_bkw_frw"), tTopo->tecIsFrontPetal(detId) ? 2 : 2) &&
0271                     _isSel(badComp.getParameter<uint32_t>("petal"), tTopo->tecPetalNumber(detId)) &&
0272                     _isSel(badComp.getParameter<uint32_t>("ring"), tTopo->tecRing(detId)) &&
0273                     _isSel(badComp.getParameter<uint32_t>("detid"), detId.rawId()));
0274           });
0275           break;
0276         default:
0277           break;
0278       }
0279     }
0280   }
0281   if (m_printDebug) {
0282     edm::LogInfo("SiStripBadModuleGenerator") << ss.str();
0283   }
0284   return selList;
0285 }
0286 
0287 //define this as a plug-in
0288 #include "FWCore/Framework/interface/SourceFactory.h"
0289 DEFINE_FWK_EVENTSETUP_SOURCE(SiStripBadModuleConfigurableFakeESSource);