Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-15 22:42:24

0001 /*!
0002   \file SiPixelDynamicInefficiency_PayloadInspector
0003   \Payload Inspector Plugin for SiPixelDynamicInefficiency
0004   \author M. Musich
0005   \version $Revision: 1.0 $
0006   \date $Date: 2018/10/18 14:48:00 $
0007 */
0008 
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 
0011 #include "CondCore/Utilities/interface/PayloadInspectorModule.h"
0012 #include "CondCore/Utilities/interface/PayloadInspector.h"
0013 #include "CondCore/CondDB/interface/Time.h"
0014 #include "CondCore/SiPixelPlugins/interface/SiPixelPayloadInspectorHelper.h"
0015 #include "FWCore/ParameterSet/interface/FileInPath.h"
0016 #include "CalibTracker/StandaloneTrackerTopology/interface/StandaloneTrackerTopology.h"
0017 #include "CalibTracker/SiPixelESProducers/interface/SiPixelDetInfoFileReader.h"
0018 
0019 // the data format of the condition to be inspected
0020 #include "CondFormats/SiPixelObjects/interface/SiPixelDynamicInefficiency.h"
0021 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
0022 #include "DataFormats/DetId/interface/DetId.h"
0023 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0024 #include "DQM/TrackerRemapper/interface/Phase1PixelROCMaps.h"
0025 
0026 #include <memory>
0027 #include <sstream>
0028 #include <iostream>
0029 
0030 // include ROOT
0031 #include "TH2F.h"
0032 #include "TLegend.h"
0033 #include "TCanvas.h"
0034 #include "TLine.h"
0035 #include "TGraph.h"
0036 #include "TStyle.h"
0037 #include "TLatex.h"
0038 #include "TPave.h"
0039 #include "TPaveStats.h"
0040 
0041 namespace {
0042 
0043   using namespace cond::payloadInspector;
0044   namespace SiPixDynIneff {
0045 
0046     // constants for ROC level simulation for Phase1
0047     enum shiftEnumerator { FPixRocIdShift = 3, BPixRocIdShift = 6 };
0048     static const int rocIdMaskBits = 0x1F;
0049 
0050     struct packedBadRocFraction {
0051       std::vector<int> badRocNumber;
0052       std::vector<float> badRocFrac;
0053     };
0054 
0055     using BRFractions = std::unordered_map<uint32_t, packedBadRocFraction>;
0056 
0057     //_________________________________________________
0058     BRFractions pbrf(std::shared_ptr<SiPixelDynamicInefficiency> payload) {
0059       BRFractions f;
0060       const std::map<uint32_t, double>& PixelGeomFactorsDBIn = payload->getPixelGeomFactors();
0061 
0062       // first fill
0063       for (const auto db_factor : PixelGeomFactorsDBIn) {
0064         int subid = DetId(db_factor.first).subdetId();
0065         int shift = (subid == static_cast<int>(PixelSubdetector::PixelBarrel)) ? BPixRocIdShift : FPixRocIdShift;
0066         unsigned int rocMask = rocIdMaskBits << shift;
0067         unsigned int rocId = (((db_factor.first) & rocMask) >> shift);
0068         uint32_t rawid = db_factor.first & (~rocMask);
0069 
0070         if (f.find(rawid) == f.end()) {
0071           packedBadRocFraction p;
0072           f.insert(std::make_pair(rawid, p));
0073         }
0074 
0075         if (rocId != 0) {
0076           rocId--;
0077           double factor = db_factor.second;
0078           double badFraction = 1 - factor;
0079 
0080           f.at(rawid).badRocNumber.emplace_back(rocId);
0081           f.at(rawid).badRocFrac.emplace_back(badFraction);
0082         }
0083       }
0084       return f;
0085     }
0086 
0087     //_________________________________________________
0088     bool isPhase0(const BRFractions& fractions) {
0089       SiPixelDetInfoFileReader reader =
0090           SiPixelDetInfoFileReader(edm::FileInPath(SiPixelDetInfoFileReader::kPh0DefaultFile).fullPath());
0091       const auto& p0detIds = reader.getAllDetIds();
0092       std::vector<uint32_t> ownDetIds;
0093 
0094       std::transform(fractions.begin(),
0095                      fractions.end(),
0096                      std::back_inserter(ownDetIds),
0097                      [](std::pair<uint32_t, packedBadRocFraction> d) -> uint32_t { return d.first; });
0098 
0099       for (const auto& det : ownDetIds) {
0100         // if found at least one phase-0 detId early return
0101         if (std::find(p0detIds.begin(), p0detIds.end(), det) != p0detIds.end()) {
0102           return true;
0103         }
0104       }
0105       return false;
0106     }
0107   }  // namespace SiPixDynIneff
0108 
0109   /************************************************
0110     test class
0111   *************************************************/
0112 
0113   class SiPixelDynamicInefficiencyTest : public Histogram1D<SiPixelDynamicInefficiency, SINGLE_IOV> {
0114   public:
0115     SiPixelDynamicInefficiencyTest()
0116         : Histogram1D<SiPixelDynamicInefficiency, SINGLE_IOV>(
0117               "SiPixelDynamicInefficiency test", "SiPixelDynamicInefficiency test", 1, 0.0, 1.0) {}
0118 
0119     bool fill() override {
0120       auto tag = PlotBase::getTag<0>();
0121       for (auto const& iov : tag.iovs) {
0122         std::shared_ptr<SiPixelDynamicInefficiency> payload = Base::fetchPayload(std::get<1>(iov));
0123         if (payload.get()) {
0124           fillWithValue(1.);
0125 
0126           const auto geomFactors = payload->getPixelGeomFactors();
0127           for (const auto [ID, value] : geomFactors) {
0128             std::cout << ID << " : " << value << std::endl;
0129             ;
0130           }
0131         }  // payload
0132       }    // iovs
0133       return true;
0134     }  // fill
0135   };
0136 
0137   /************************************************
0138    occupancy style map whole Pixel of inefficient ROCs
0139   *************************************************/
0140   template <SiPixelPI::DetType myType>
0141   class SiPixelIneffROCfromDynIneffMap : public PlotImage<SiPixelDynamicInefficiency, SINGLE_IOV> {
0142   public:
0143     SiPixelIneffROCfromDynIneffMap()
0144         : PlotImage<SiPixelDynamicInefficiency, SINGLE_IOV>("SiPixel Inefficient ROC from Dyn Ineff Pixel Map"),
0145           m_trackerTopo{StandaloneTrackerTopology::fromTrackerParametersXMLFile(
0146               edm::FileInPath("Geometry/TrackerCommonData/data/PhaseI/trackerParameters.xml").fullPath())} {}
0147 
0148     bool fill() override {
0149       auto tag = PlotBase::getTag<0>();
0150       auto iov = tag.iovs.front();
0151       auto tagname = tag.name;
0152       std::shared_ptr<SiPixelDynamicInefficiency> payload = fetchPayload(std::get<1>(iov));
0153 
0154       const auto fr = SiPixDynIneff::pbrf(payload);
0155 
0156       if (SiPixDynIneff::isPhase0(fr)) {
0157         edm::LogError("SiPixelDynamicInefficiency_PayloadInspector")
0158             << "SiPixelIneffROCfromDynIneff maps are not supported for non-Phase1 Pixel geometries !";
0159         TCanvas canvas("Canv", "Canv", 1200, 1000);
0160         SiPixelPI::displayNotSupported(canvas, 0);
0161         std::string fileName(m_imageFileName);
0162         canvas.SaveAs(fileName.c_str());
0163         return false;
0164       }
0165 
0166       Phase1PixelROCMaps theMap("", "bad pixel fraction in ROC [%]");
0167 
0168       for (const auto& element : fr) {
0169         auto rawid = element.first;
0170         int subid = DetId(rawid).subdetId();
0171         auto packedinfo = element.second;
0172         auto badRocs = packedinfo.badRocNumber;
0173         auto badRocsF = packedinfo.badRocFrac;
0174 
0175         for (size_t i = 0; i < badRocs.size(); i++) {
0176           std::bitset<16> rocToMark;
0177           rocToMark.set(badRocs[i]);
0178           if ((subid == PixelSubdetector::PixelBarrel && myType == SiPixelPI::t_barrel) ||
0179               (subid == PixelSubdetector::PixelEndcap && myType == SiPixelPI::t_forward) ||
0180               (myType == SiPixelPI::t_all)) {
0181             theMap.fillSelectedRocs(rawid, rocToMark, badRocsF[i] * 100.f);
0182           }
0183         }
0184       }
0185 
0186       gStyle->SetOptStat(0);
0187       //=========================
0188       TCanvas canvas("Summary", "Summary", 1200, k_height[myType]);
0189       canvas.cd();
0190 
0191       auto unpacked = SiPixelPI::unpack(std::get<0>(iov));
0192 
0193       std::string IOVstring = (unpacked.first == 0)
0194                                   ? std::to_string(unpacked.second)
0195                                   : (std::to_string(unpacked.first) + "," + std::to_string(unpacked.second));
0196 
0197       const auto headerText = fmt::sprintf("#color[4]{%s},  IOV: #color[4]{%s}", tagname, IOVstring);
0198 
0199       switch (myType) {
0200         case SiPixelPI::t_barrel:
0201           theMap.drawBarrelMaps(canvas, headerText);
0202           break;
0203         case SiPixelPI::t_forward:
0204           theMap.drawForwardMaps(canvas, headerText);
0205           break;
0206         case SiPixelPI::t_all:
0207           theMap.drawMaps(canvas, headerText);
0208           break;
0209         default:
0210           throw cms::Exception("SiPixelIneffROCfromDynIneffMap")
0211               << "\nERROR: unrecognized Pixel Detector part " << std::endl;
0212       }
0213 
0214       std::string fileName(m_imageFileName);
0215       canvas.SaveAs(fileName.c_str());
0216 #ifdef MMDEBUG
0217       canvas.SaveAs("outAll.root");
0218 #endif
0219 
0220       return true;
0221     }
0222 
0223   private:
0224     static constexpr std::array<int, 3> k_height = {{1200, 600, 1600}};
0225     TrackerTopology m_trackerTopo;
0226   };
0227 
0228   using SiPixelBPixIneffROCfromDynIneffMap = SiPixelIneffROCfromDynIneffMap<SiPixelPI::t_barrel>;
0229   using SiPixelFPixIneffROCfromDynIneffMap = SiPixelIneffROCfromDynIneffMap<SiPixelPI::t_forward>;
0230   using SiPixelFullIneffROCfromDynIneffMap = SiPixelIneffROCfromDynIneffMap<SiPixelPI::t_all>;
0231 
0232   /************************************************
0233    occupancy style map whole Pixel, difference of payloads
0234   *************************************************/
0235   template <SiPixelPI::DetType myType, IOVMultiplicity nIOVs, int ntags>
0236   class SiPixelIneffROCComparisonBase : public PlotImage<SiPixelDynamicInefficiency, nIOVs, ntags> {
0237   public:
0238     SiPixelIneffROCComparisonBase()
0239         : PlotImage<SiPixelDynamicInefficiency, nIOVs, ntags>(
0240               Form("SiPixelDynamicInefficiency %s Pixel Map", SiPixelPI::DetNames[myType].c_str())),
0241           m_trackerTopo{StandaloneTrackerTopology::fromTrackerParametersXMLFile(
0242               edm::FileInPath("Geometry/TrackerCommonData/data/PhaseI/trackerParameters.xml").fullPath())} {}
0243 
0244     bool fill() override {
0245       // trick to deal with the multi-ioved tag and two tag case at the same time
0246       auto theIOVs = PlotBase::getTag<0>().iovs;
0247       auto f_tagname = PlotBase::getTag<0>().name;
0248       std::string l_tagname = "";
0249       auto firstiov = theIOVs.front();
0250       std::tuple<cond::Time_t, cond::Hash> lastiov;
0251 
0252       // we don't support (yet) comparison with more than 2 tags
0253       assert(this->m_plotAnnotations.ntags < 3);
0254 
0255       if (this->m_plotAnnotations.ntags == 2) {
0256         auto tag2iovs = PlotBase::getTag<1>().iovs;
0257         l_tagname = PlotBase::getTag<1>().name;
0258         lastiov = tag2iovs.front();
0259       } else {
0260         lastiov = theIOVs.back();
0261       }
0262 
0263       std::shared_ptr<SiPixelDynamicInefficiency> last_payload = this->fetchPayload(std::get<1>(lastiov));
0264       std::shared_ptr<SiPixelDynamicInefficiency> first_payload = this->fetchPayload(std::get<1>(firstiov));
0265 
0266       const auto fp = SiPixDynIneff::pbrf(last_payload);
0267       const auto lp = SiPixDynIneff::pbrf(first_payload);
0268 
0269       if (SiPixDynIneff::isPhase0(fp) || SiPixDynIneff::isPhase0(lp)) {
0270         edm::LogError("SiPixelDynamicInefficiency_PayloadInspector")
0271             << "SiPixelDynamicInefficiency comparison maps are not supported for non-Phase1 Pixel geometries !";
0272         TCanvas canvas("Canv", "Canv", 1200, 1000);
0273         SiPixelPI::displayNotSupported(canvas, 0);
0274         std::string fileName(this->m_imageFileName);
0275         canvas.SaveAs(fileName.c_str());
0276         return false;
0277       }
0278 
0279       Phase1PixelROCMaps theMap("", "#Delta payload A - payload B");
0280 
0281       gStyle->SetOptStat(0);
0282       //=========================
0283       TCanvas canvas("Summary", "Summary", 1200, k_height[myType]);
0284       canvas.cd();
0285 
0286       auto f_unpacked = SiPixelPI::unpack(std::get<0>(firstiov));
0287       auto l_unpacked = SiPixelPI::unpack(std::get<0>(lastiov));
0288 
0289       std::string f_IOVstring = (f_unpacked.first == 0)
0290                                     ? std::to_string(f_unpacked.second)
0291                                     : (std::to_string(f_unpacked.first) + "," + std::to_string(f_unpacked.second));
0292 
0293       std::string l_IOVstring = (l_unpacked.first == 0)
0294                                     ? std::to_string(l_unpacked.second)
0295                                     : (std::to_string(l_unpacked.first) + "," + std::to_string(l_unpacked.second));
0296 
0297       std::string headerText;
0298 
0299       if (this->m_plotAnnotations.ntags == 2) {
0300         headerText =
0301             fmt::sprintf("#color[2]{A: %s, %s} - #color[4]{B: %s, %s}", f_tagname, f_IOVstring, l_tagname, l_IOVstring);
0302       } else {
0303         headerText = fmt::sprintf("%s,IOV #color[2]{A: %s} - #color[4]{B: %s} ", f_tagname, f_IOVstring, l_IOVstring);
0304       }
0305 
0306       switch (myType) {
0307         case SiPixelPI::t_barrel:
0308           theMap.drawBarrelMaps(canvas, headerText);
0309           break;
0310         case SiPixelPI::t_forward:
0311           theMap.drawForwardMaps(canvas, headerText);
0312           break;
0313         case SiPixelPI::t_all:
0314           theMap.drawMaps(canvas, headerText);
0315           break;
0316         default:
0317           throw cms::Exception("SiPixelDynamicInefficiencyMapComparison")
0318               << "\nERROR: unrecognized Pixel Detector part " << std::endl;
0319       }
0320 
0321       // first loop on the first payload (newest)
0322       fillTheMapFromPayload(theMap, fp, false);
0323 
0324       // then loop on the second payload (oldest)
0325       fillTheMapFromPayload(theMap, lp, true);  // true will subtract
0326 
0327       std::string fileName(this->m_imageFileName);
0328       canvas.SaveAs(fileName.c_str());
0329 #ifdef MMDEBUG
0330       canvas.SaveAs("outAll.root");
0331 #endif
0332 
0333       return true;
0334     }
0335 
0336   private:
0337     static constexpr std::array<int, 3> k_height = {{1200, 600, 1600}};
0338     TrackerTopology m_trackerTopo;
0339 
0340     //____________________________________________________________________________________________
0341     void fillTheMapFromPayload(Phase1PixelROCMaps& theMap, const SiPixDynIneff::BRFractions& fr, bool subtract) {
0342       for (const auto& element : fr) {
0343         auto rawid = element.first;
0344         int subid = DetId(rawid).subdetId();
0345         auto packedinfo = element.second;
0346         auto badRocs = packedinfo.badRocNumber;
0347         auto badRocsF = packedinfo.badRocFrac;
0348 
0349         for (size_t i = 0; i < badRocs.size(); i++) {
0350           std::bitset<16> rocToMark;
0351           rocToMark.set(badRocs[i]);
0352           if ((subid == PixelSubdetector::PixelBarrel && myType == SiPixelPI::t_barrel) ||
0353               (subid == PixelSubdetector::PixelEndcap && myType == SiPixelPI::t_forward) ||
0354               (myType == SiPixelPI::t_all)) {
0355             theMap.fillSelectedRocs(rawid, rocToMark, badRocsF[i] * (subtract ? -1. : 1.));
0356           }
0357         }
0358       }
0359     }
0360   };
0361 
0362   /*
0363   using SiPixelBPixIneffROCsMapCompareSingleTag = SiPixelIneffROCComparisonBase<SiPixelPI::t_barrel, MULTI_IOV, 1>;
0364   using SiPixelFPixIneffROCsMapCompareSingleTag = SiPixelIneffROCComparisonBase<SiPixelPI::t_forward, MULTI_IOV, 1>;
0365   using SiPixelFullIneffROCsMapCompareSingleTag = SiPixelIneffROCComparisonBase<SiPixelPI::t_all, MULTI_IOV, 1>;
0366   */
0367   using SiPixelBPixIneffROCsMapCompareTwoTags = SiPixelIneffROCComparisonBase<SiPixelPI::t_barrel, SINGLE_IOV, 2>;
0368   using SiPixelFPixIneffROCsMapCompareTwoTags = SiPixelIneffROCComparisonBase<SiPixelPI::t_forward, SINGLE_IOV, 2>;
0369   using SiPixelFullIneffROCsMapCompareTwoTags = SiPixelIneffROCComparisonBase<SiPixelPI::t_all, SINGLE_IOV, 2>;
0370 
0371 }  // namespace
0372 
0373 // Register the classes as boost python plugin
0374 PAYLOAD_INSPECTOR_MODULE(SiPixelDynamicInefficiency) {
0375   PAYLOAD_INSPECTOR_CLASS(SiPixelDynamicInefficiencyTest);
0376   PAYLOAD_INSPECTOR_CLASS(SiPixelBPixIneffROCfromDynIneffMap);
0377   PAYLOAD_INSPECTOR_CLASS(SiPixelFPixIneffROCfromDynIneffMap);
0378   PAYLOAD_INSPECTOR_CLASS(SiPixelFullIneffROCfromDynIneffMap);
0379   PAYLOAD_INSPECTOR_CLASS(SiPixelBPixIneffROCsMapCompareTwoTags);
0380   PAYLOAD_INSPECTOR_CLASS(SiPixelFPixIneffROCsMapCompareTwoTags);
0381   PAYLOAD_INSPECTOR_CLASS(SiPixelFullIneffROCsMapCompareTwoTags);
0382 }