Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-03-24 23:46:06

0001 /*!
0002   \file SiStripBadStrip_PayloadInspector
0003   \Payload Inspector Plugin for SiStrip Bad Strip
0004   \author M. Musich
0005   \version $Revision: 1.0 $
0006   \date $Date: 2017/08/14 14:37:22 $
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 
0015 // the data format of the condition to be inspected
0016 #include "CondFormats/SiStripObjects/interface/SiStripBadStrip.h"
0017 #include "DataFormats/DetId/interface/DetId.h"
0018 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
0019 #include "CondFormats/SiStripObjects/interface/SiStripDetSummary.h"
0020 
0021 #include "CalibFormats/SiStripObjects/interface/SiStripQuality.h"
0022 
0023 // needed for the tracker map
0024 #include "CommonTools/TrackerMap/interface/TrackerMap.h"
0025 #include "DQM/TrackerRemapper/interface/SiStripTkMaps.h"
0026 
0027 // auxilliary functions
0028 #include "CondCore/SiStripPlugins/interface/SiStripPayloadInspectorHelper.h"
0029 #include "CalibTracker/StandaloneTrackerTopology/interface/StandaloneTrackerTopology.h"
0030 
0031 #include "CalibTracker/SiStripCommon/interface/SiStripDetInfoFileReader.h"
0032 #include "FWCore/ParameterSet/interface/FileInPath.h"
0033 
0034 #include <memory>
0035 #include <sstream>
0036 #include <iostream>
0037 
0038 // include ROOT
0039 #include "TH2F.h"
0040 #include "TLegend.h"
0041 #include "TCanvas.h"
0042 #include "TLine.h"
0043 #include "TStyle.h"
0044 #include "TLatex.h"
0045 #include "TPave.h"
0046 #include "TPaveStats.h"
0047 
0048 namespace {
0049 
0050   using namespace cond::payloadInspector;
0051 
0052   /************************************************
0053     test class
0054   *************************************************/
0055 
0056   class SiStripBadStripTest : public Histogram1D<SiStripBadStrip, SINGLE_IOV> {
0057   public:
0058     SiStripBadStripTest()
0059         : Histogram1D<SiStripBadStrip, SINGLE_IOV>("SiStrip Bad Strip test", "SiStrip Bad Strip test", 10, 0.0, 10.0) {}
0060 
0061     bool fill() override {
0062       auto tag = PlotBase::getTag<0>();
0063       for (auto const& iov : tag.iovs) {
0064         std::shared_ptr<SiStripBadStrip> payload = Base::fetchPayload(std::get<1>(iov));
0065         if (payload.get()) {
0066           fillWithValue(1.);
0067 
0068           std::stringstream ss;
0069           ss << "Summary of bad strips:" << std::endl;
0070 
0071           //payload->printDebug(ss);
0072           //payload->printSummary(ss);
0073           //std::cout<<ss.str()<<std::endl;
0074 
0075           std::vector<uint32_t> detid;
0076           payload->getDetIds(detid);
0077 
0078           for (const auto& d : detid) {
0079             SiStripBadStrip::Range range = payload->getRange(d);
0080             for (std::vector<unsigned int>::const_iterator badStrip = range.first; badStrip != range.second;
0081                  ++badStrip) {
0082               ss << "DetId=" << d << " Strip=" << payload->decode(*badStrip).firstStrip << ":"
0083                  << payload->decode(*badStrip).range << " flag=" << payload->decode(*badStrip).flag << std::endl;
0084             }
0085           }
0086 
0087           std::cout << ss.str() << std::endl;
0088 
0089         }  // payload
0090       }    // iovs
0091       return true;
0092     }  // fill
0093   };
0094 
0095   /************************************************
0096     TrackerMap of SiStripBadStrip (bad strip per detid)
0097   *************************************************/
0098   class SiStripBadModuleTrackerMap : public PlotImage<SiStripBadStrip, SINGLE_IOV> {
0099   public:
0100     SiStripBadModuleTrackerMap() : PlotImage<SiStripBadStrip, SINGLE_IOV>("Tracker Map of SiStrip Bad Strips") {}
0101 
0102     bool fill() override {
0103       auto tag = PlotBase::getTag<0>();
0104       auto iov = tag.iovs.front();
0105       auto tagname = PlotBase::getTag<0>().name;
0106       std::shared_ptr<SiStripBadStrip> payload = fetchPayload(std::get<1>(iov));
0107 
0108       auto theIOVsince = std::to_string(std::get<0>(iov));
0109 
0110       std::string titleMap = "Modules w/ at least 1 bad Strip, Run: " + theIOVsince + " (tag: " + tagname + ")";
0111 
0112       std::unique_ptr<TrackerMap> tmap = std::make_unique<TrackerMap>("SiStripBadStrips");
0113       tmap->setTitle(titleMap);
0114       tmap->setPalette(1);
0115 
0116       std::vector<uint32_t> detid;
0117       payload->getDetIds(detid);
0118 
0119       for (const auto& d : detid) {
0120         tmap->fill(d, 1.);
0121       }  // loop over detIds
0122 
0123       //=========================
0124 
0125       std::string fileName(m_imageFileName);
0126       tmap->save(true, 0, 1., fileName);
0127 
0128       return true;
0129     }
0130   };
0131 
0132   /************************************************
0133     TrackerMap of SiStripBadStrip (bad strips fraction)
0134   *************************************************/
0135   class SiStripBadStripFractionTrackerMap : public PlotImage<SiStripBadStrip, SINGLE_IOV> {
0136   public:
0137     SiStripBadStripFractionTrackerMap()
0138         : PlotImage<SiStripBadStrip, SINGLE_IOV>("Tracker Map of SiStrip Bad Components fraction") {}
0139 
0140     bool fill() override {
0141       auto tag = PlotBase::getTag<0>();
0142       auto iov = tag.iovs.front();
0143       auto tagname = PlotBase::getTag<0>().name;
0144       std::shared_ptr<SiStripBadStrip> payload = fetchPayload(std::get<1>(iov));
0145 
0146       const auto detInfo =
0147           SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0148 
0149       auto theIOVsince = std::to_string(std::get<0>(iov));
0150 
0151       std::string titleMap = "Fraction of bad Strips per module, Run: " + theIOVsince + " (tag: " + tagname + ")";
0152 
0153       std::unique_ptr<TrackerMap> tmap = std::make_unique<TrackerMap>("SiStripBadStrips");
0154       tmap->setTitle(titleMap);
0155       tmap->setPalette(1);
0156 
0157       std::vector<uint32_t> detid;
0158       payload->getDetIds(detid);
0159 
0160       std::map<uint32_t, int> badStripsPerDetId;
0161 
0162       for (const auto& d : detid) {
0163         SiStripBadStrip::Range range = payload->getRange(d);
0164         for (std::vector<unsigned int>::const_iterator badStrip = range.first; badStrip != range.second; ++badStrip) {
0165           badStripsPerDetId[d] += payload->decode(*badStrip).range;
0166           //ss << "DetId="<< d << " Strip=" << payload->decode(*badStrip).firstStrip <<":"<< payload->decode(*badStrip).range << " flag="<< payload->decode(*badStrip).flag << std::endl;
0167         }
0168         float fraction =
0169             badStripsPerDetId[d] / (sistrip::STRIPS_PER_APV * detInfo.getNumberOfApvsAndStripLength(d).first);
0170         tmap->fill(d, fraction);
0171       }  // loop over detIds
0172 
0173       //=========================
0174 
0175       std::pair<float, float> extrema = tmap->getAutomaticRange();
0176 
0177       std::string fileName(m_imageFileName);
0178 
0179       // protect against uniform values across the map (bad components fractions are defined positive)
0180       if (extrema.first != extrema.second) {
0181         tmap->save(true, 0, 0, fileName);
0182       } else {
0183         tmap->save(true, extrema.first * 0.95, extrema.first * 1.05, fileName);
0184       }
0185 
0186       return true;
0187     }
0188   };
0189 
0190   /************************************************
0191     TrackerMap of SiStripBadStrip (bad strips fraction)
0192   *************************************************/
0193   class SiStripBadStripFractionTkMap : public PlotImage<SiStripBadStrip, SINGLE_IOV> {
0194   public:
0195     SiStripBadStripFractionTkMap()
0196         : PlotImage<SiStripBadStrip, SINGLE_IOV>("Tracker Map of SiStrip Bad Components fraction") {}
0197 
0198     bool fill() override {
0199       //SiStripPI::setPaletteStyle(SiStripPI::DEFAULT);
0200       gStyle->SetPalette(1);
0201 
0202       auto tag = PlotBase::getTag<0>();
0203       auto iov = tag.iovs.front();
0204       auto tagname = PlotBase::getTag<0>().name;
0205       std::shared_ptr<SiStripBadStrip> payload = fetchPayload(std::get<1>(iov));
0206 
0207       const auto detInfo =
0208           SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0209 
0210       auto theIOVsince = std::to_string(std::get<0>(iov));
0211 
0212       std::string titleMap =
0213           "Fraction of bad Strips per module, Run: " + theIOVsince + " (tag:#color[2]{" + tagname + "})";
0214 
0215       SiStripTkMaps myMap("COLZA0 L");
0216       myMap.bookMap(titleMap, "Fraction of bad Strips per module");
0217 
0218       SiStripTkMaps ghost("AL");
0219       ghost.bookMap(titleMap, "");
0220 
0221       std::vector<uint32_t> detid;
0222       payload->getDetIds(detid);
0223 
0224       std::map<uint32_t, int> badStripsPerDetId;
0225 
0226       for (const auto& d : detid) {
0227         SiStripBadStrip::Range range = payload->getRange(d);
0228         for (std::vector<unsigned int>::const_iterator badStrip = range.first; badStrip != range.second; ++badStrip) {
0229           badStripsPerDetId[d] += payload->decode(*badStrip).range;
0230         }
0231         float fraction =
0232             badStripsPerDetId[d] / (sistrip::STRIPS_PER_APV * detInfo.getNumberOfApvsAndStripLength(d).first);
0233         if (fraction > 0.) {
0234           myMap.fill(d, fraction);
0235         }
0236       }  // loop over detIds
0237 
0238       //=========================
0239 
0240       std::string fileName(m_imageFileName);
0241       TCanvas canvas("Bad Components fraction", "bad components fraction");
0242       myMap.drawMap(canvas, "");
0243       ghost.drawMap(canvas, "same");
0244       canvas.SaveAs(fileName.c_str());
0245 
0246       return true;
0247     }
0248   };
0249 
0250   /************************************************
0251     time history histogram of bad components fraction
0252   *************************************************/
0253 
0254   class SiStripBadStripFractionByRun : public HistoryPlot<SiStripBadStrip, float> {
0255   public:
0256     SiStripBadStripFractionByRun()
0257         : HistoryPlot<SiStripBadStrip, float>("SiStrip Bad Strip fraction per run", "Bad Strip fraction [%]") {}
0258     ~SiStripBadStripFractionByRun() override = default;
0259 
0260     float getFromPayload(SiStripBadStrip& payload) override {
0261       const auto detInfo =
0262           SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0263 
0264       std::vector<uint32_t> detid;
0265       payload.getDetIds(detid);
0266 
0267       std::map<uint32_t, int> badStripsPerDetId;
0268 
0269       for (const auto& d : detid) {
0270         SiStripBadStrip::Range range = payload.getRange(d);
0271         int badStrips(0);
0272         for (std::vector<unsigned int>::const_iterator badStrip = range.first; badStrip != range.second; ++badStrip) {
0273           badStrips += payload.decode(*badStrip).range;
0274         }
0275         badStripsPerDetId[d] = badStrips;
0276       }  // loop over detIds
0277 
0278       float numerator(0.), denominator(0.);
0279       std::vector<uint32_t> all_detids = detInfo.getAllDetIds();
0280       for (const auto& det : all_detids) {
0281         denominator += sistrip::STRIPS_PER_APV * detInfo.getNumberOfApvsAndStripLength(det).first;
0282         if (badStripsPerDetId.count(det) != 0)
0283           numerator += badStripsPerDetId[det];
0284       }
0285 
0286       return (numerator / denominator) * 100.;
0287 
0288     }  // payload
0289   };
0290 
0291   /************************************************
0292     time history histogram of bad components fraction (TIB)
0293   *************************************************/
0294 
0295   class SiStripBadStripTIBFractionByRun : public HistoryPlot<SiStripBadStrip, float> {
0296   public:
0297     SiStripBadStripTIBFractionByRun()
0298         : HistoryPlot<SiStripBadStrip, float>("SiStrip Inner Barrel Bad Strip fraction per run",
0299                                               "TIB Bad Strip fraction [%]") {}
0300     ~SiStripBadStripTIBFractionByRun() override = default;
0301 
0302     float getFromPayload(SiStripBadStrip& payload) override {
0303       const auto detInfo =
0304           SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0305 
0306       std::vector<uint32_t> detid;
0307       payload.getDetIds(detid);
0308 
0309       std::map<uint32_t, int> badStripsPerDetId;
0310 
0311       for (const auto& d : detid) {
0312         SiStripBadStrip::Range range = payload.getRange(d);
0313         int badStrips(0);
0314         for (std::vector<unsigned int>::const_iterator badStrip = range.first; badStrip != range.second; ++badStrip) {
0315           badStrips += payload.decode(*badStrip).range;
0316         }
0317         badStripsPerDetId[d] = badStrips;
0318       }  // loop over detIds
0319 
0320       float numerator(0.), denominator(0.);
0321       std::vector<uint32_t> all_detids = detInfo.getAllDetIds();
0322       for (const auto& det : all_detids) {
0323         int subid = DetId(det).subdetId();
0324         if (subid != StripSubdetector::TIB)
0325           continue;
0326         denominator += sistrip::STRIPS_PER_APV * detInfo.getNumberOfApvsAndStripLength(det).first;
0327         if (badStripsPerDetId.count(det) != 0)
0328           numerator += badStripsPerDetId[det];
0329       }
0330 
0331       return (numerator / denominator) * 100.;
0332 
0333     }  // payload
0334   };
0335 
0336   /************************************************
0337     time history histogram of bad components fraction (TOB)
0338   *************************************************/
0339 
0340   class SiStripBadStripTOBFractionByRun : public HistoryPlot<SiStripBadStrip, float> {
0341   public:
0342     SiStripBadStripTOBFractionByRun()
0343         : HistoryPlot<SiStripBadStrip, float>("SiStrip Outer Barrel Bad Strip fraction per run",
0344                                               "TOB Bad Strip fraction [%]") {}
0345     ~SiStripBadStripTOBFractionByRun() override = default;
0346 
0347     float getFromPayload(SiStripBadStrip& payload) override {
0348       const auto detInfo =
0349           SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0350 
0351       std::vector<uint32_t> detid;
0352       payload.getDetIds(detid);
0353 
0354       std::map<uint32_t, int> badStripsPerDetId;
0355 
0356       for (const auto& d : detid) {
0357         SiStripBadStrip::Range range = payload.getRange(d);
0358         int badStrips(0);
0359         for (std::vector<unsigned int>::const_iterator badStrip = range.first; badStrip != range.second; ++badStrip) {
0360           badStrips += payload.decode(*badStrip).range;
0361         }
0362         badStripsPerDetId[d] = badStrips;
0363       }  // loop over detIds
0364 
0365       float numerator(0.), denominator(0.);
0366       std::vector<uint32_t> all_detids = detInfo.getAllDetIds();
0367       for (const auto& det : all_detids) {
0368         int subid = DetId(det).subdetId();
0369         if (subid != StripSubdetector::TOB)
0370           continue;
0371         denominator += sistrip::STRIPS_PER_APV * detInfo.getNumberOfApvsAndStripLength(det).first;
0372         if (badStripsPerDetId.count(det) != 0)
0373           numerator += badStripsPerDetId[det];
0374       }
0375 
0376       return (numerator / denominator) * 100.;
0377 
0378     }  // payload
0379   };
0380 
0381   /************************************************
0382     time history histogram of bad components fraction (TID)
0383    *************************************************/
0384 
0385   class SiStripBadStripTIDFractionByRun : public HistoryPlot<SiStripBadStrip, float> {
0386   public:
0387     SiStripBadStripTIDFractionByRun()
0388         : HistoryPlot<SiStripBadStrip, float>("SiStrip Inner Disks Bad Strip fraction per run",
0389                                               "TID Bad Strip fraction [%]") {}
0390     ~SiStripBadStripTIDFractionByRun() override = default;
0391 
0392     float getFromPayload(SiStripBadStrip& payload) override {
0393       const auto detInfo =
0394           SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0395 
0396       std::vector<uint32_t> detid;
0397       payload.getDetIds(detid);
0398 
0399       std::map<uint32_t, int> badStripsPerDetId;
0400 
0401       for (const auto& d : detid) {
0402         SiStripBadStrip::Range range = payload.getRange(d);
0403         int badStrips(0);
0404         for (std::vector<unsigned int>::const_iterator badStrip = range.first; badStrip != range.second; ++badStrip) {
0405           badStrips += payload.decode(*badStrip).range;
0406         }
0407         badStripsPerDetId[d] = badStrips;
0408       }  // loop over detIds
0409 
0410       float numerator(0.), denominator(0.);
0411       std::vector<uint32_t> all_detids = detInfo.getAllDetIds();
0412       for (const auto& det : all_detids) {
0413         int subid = DetId(det).subdetId();
0414         if (subid != StripSubdetector::TID)
0415           continue;
0416         denominator += sistrip::STRIPS_PER_APV * detInfo.getNumberOfApvsAndStripLength(det).first;
0417         if (badStripsPerDetId.count(det) != 0)
0418           numerator += badStripsPerDetId[det];
0419       }
0420 
0421       return (numerator / denominator) * 100.;
0422 
0423     }  // payload
0424   };
0425 
0426   /************************************************
0427     time history histogram of bad components fraction (TEC)
0428    *************************************************/
0429 
0430   class SiStripBadStripTECFractionByRun : public HistoryPlot<SiStripBadStrip, float> {
0431   public:
0432     SiStripBadStripTECFractionByRun()
0433         : HistoryPlot<SiStripBadStrip, float>("SiStrip Endcaps Bad Strip fraction per run",
0434                                               "TEC Bad Strip fraction [%]") {}
0435     ~SiStripBadStripTECFractionByRun() override = default;
0436 
0437     float getFromPayload(SiStripBadStrip& payload) override {
0438       const auto detInfo =
0439           SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0440 
0441       std::vector<uint32_t> detid;
0442       payload.getDetIds(detid);
0443 
0444       std::map<uint32_t, int> badStripsPerDetId;
0445 
0446       for (const auto& d : detid) {
0447         SiStripBadStrip::Range range = payload.getRange(d);
0448         int badStrips(0);
0449         for (std::vector<unsigned int>::const_iterator badStrip = range.first; badStrip != range.second; ++badStrip) {
0450           badStrips += payload.decode(*badStrip).range;
0451         }
0452         badStripsPerDetId[d] = badStrips;
0453       }  // loop over detIds
0454 
0455       float numerator(0.), denominator(0.);
0456       std::vector<uint32_t> all_detids = detInfo.getAllDetIds();
0457       for (const auto& det : all_detids) {
0458         int subid = DetId(det).subdetId();
0459         if (subid != StripSubdetector::TEC)
0460           continue;
0461         denominator += sistrip::STRIPS_PER_APV * detInfo.getNumberOfApvsAndStripLength(det).first;
0462         if (badStripsPerDetId.count(det) != 0)
0463           numerator += badStripsPerDetId[det];
0464       }
0465 
0466       return (numerator / denominator) * 100.;
0467 
0468     }  // payload
0469   };
0470 
0471   /************************************************
0472     Plot BadStrip by region 
0473   *************************************************/
0474 
0475   class SiStripBadStripByRegion : public PlotImage<SiStripBadStrip, SINGLE_IOV> {
0476   public:
0477     SiStripBadStripByRegion()
0478         : PlotImage<SiStripBadStrip, SINGLE_IOV>("SiStrip BadStrip By Region"),
0479           m_trackerTopo{StandaloneTrackerTopology::fromTrackerParametersXMLFile(
0480               edm::FileInPath("Geometry/TrackerCommonData/data/trackerParameters.xml").fullPath())} {}
0481 
0482     bool fill() override {
0483       auto tag = PlotBase::getTag<0>();
0484       auto iov = tag.iovs.front();
0485       std::shared_ptr<SiStripBadStrip> payload = fetchPayload(std::get<1>(iov));
0486 
0487       std::vector<uint32_t> detid;
0488       payload->getDetIds(detid);
0489 
0490       SiStripDetSummary summaryBadStrips{&m_trackerTopo};
0491       int totalBadStrips = 0;
0492 
0493       for (const auto& d : detid) {
0494         SiStripBadStrip::Range range = payload->getRange(d);
0495         int badStrips(0);
0496         for (std::vector<unsigned int>::const_iterator badStrip = range.first; badStrip != range.second; ++badStrip) {
0497           badStrips += payload->decode(*badStrip).range;
0498         }
0499         totalBadStrips += badStrips;
0500         summaryBadStrips.add(d, badStrips);
0501       }
0502       std::map<unsigned int, SiStripDetSummary::Values> mapBadStrips = summaryBadStrips.getCounts();
0503 
0504       //=========================
0505 
0506       TCanvas canvas("BadStrip Region summary", "SiStripBadStrip region summary", 1200, 1000);
0507       canvas.cd();
0508       auto h_BadStrips = std::make_unique<TH1F>("BadStripsbyRegion",
0509                                                 "SiStrip Bad Strip summary by region;; n. bad strips",
0510                                                 mapBadStrips.size(),
0511                                                 0.,
0512                                                 mapBadStrips.size());
0513       h_BadStrips->SetStats(false);
0514 
0515       canvas.SetBottomMargin(0.18);
0516       canvas.SetLeftMargin(0.12);
0517       canvas.SetRightMargin(0.05);
0518       canvas.Modified();
0519 
0520       std::vector<int> boundaries;
0521       unsigned int iBin = 0;
0522 
0523       std::string detector;
0524       std::string currentDetector;
0525 
0526       for (const auto& element : mapBadStrips) {
0527         iBin++;
0528         int countBadStrips = (element.second.mean);
0529 
0530         if (currentDetector.empty())
0531           currentDetector = "TIB";
0532 
0533         switch ((element.first) / 1000) {
0534           case 1:
0535             detector = "TIB";
0536             break;
0537           case 2:
0538             detector = "TOB";
0539             break;
0540           case 3:
0541             detector = "TEC";
0542             break;
0543           case 4:
0544             detector = "TID";
0545             break;
0546         }
0547 
0548         h_BadStrips->SetBinContent(iBin, countBadStrips);
0549         h_BadStrips->GetXaxis()->SetBinLabel(iBin, SiStripPI::regionType(element.first).second);
0550         h_BadStrips->GetXaxis()->LabelsOption("v");
0551 
0552         if (detector != currentDetector) {
0553           boundaries.push_back(iBin);
0554           currentDetector = detector;
0555         }
0556       }
0557 
0558       h_BadStrips->SetMarkerStyle(21);
0559       h_BadStrips->SetMarkerSize(1);
0560       h_BadStrips->SetLineColor(kBlue);
0561       h_BadStrips->SetLineStyle(9);
0562       h_BadStrips->SetMarkerColor(kBlue);
0563       h_BadStrips->GetYaxis()->SetRangeUser(0., h_BadStrips->GetMaximum() * 1.30);
0564       h_BadStrips->GetYaxis()->SetTitleOffset(1.7);
0565       h_BadStrips->Draw("HISTsame");
0566       h_BadStrips->Draw("TEXTsame");
0567 
0568       canvas.Update();
0569       canvas.cd();
0570 
0571       TLine l[boundaries.size()];
0572       unsigned int i = 0;
0573       for (const auto& line : boundaries) {
0574         l[i] = TLine(h_BadStrips->GetBinLowEdge(line),
0575                      canvas.cd()->GetUymin(),
0576                      h_BadStrips->GetBinLowEdge(line),
0577                      canvas.cd()->GetUymax());
0578         l[i].SetLineWidth(1);
0579         l[i].SetLineStyle(9);
0580         l[i].SetLineColor(2);
0581         l[i].Draw("same");
0582         i++;
0583       }
0584 
0585       TLegend legend = TLegend(0.52, 0.82, 0.95, 0.9);
0586       legend.SetHeader((std::get<1>(iov)).c_str(), "C");  // option "C" allows to center the header
0587       legend.AddEntry(
0588           h_BadStrips.get(),
0589           ("IOV: " + std::to_string(std::get<0>(iov)) + "| n. of bad strips:" + std::to_string(totalBadStrips)).c_str(),
0590           "PL");
0591       legend.SetTextSize(0.025);
0592       legend.Draw("same");
0593 
0594       std::string fileName(m_imageFileName);
0595       canvas.SaveAs(fileName.c_str());
0596 
0597       return true;
0598     }
0599 
0600   private:
0601     TrackerTopology m_trackerTopo;
0602   };
0603 
0604   /************************************************
0605     Plot BadStrip by region comparison
0606   *************************************************/
0607 
0608   template <int ntags, IOVMultiplicity nIOVs>
0609   class SiStripBadStripByRegionComparisonBase : public PlotImage<SiStripBadStrip, nIOVs, ntags> {
0610   public:
0611     SiStripBadStripByRegionComparisonBase()
0612         : PlotImage<SiStripBadStrip, nIOVs, ntags>("SiStrip BadStrip By Region Comparison"),
0613           m_trackerTopo{StandaloneTrackerTopology::fromTrackerParametersXMLFile(
0614               edm::FileInPath("Geometry/TrackerCommonData/data/trackerParameters.xml").fullPath())} {}
0615 
0616     bool fill() override {
0617       // trick to deal with the multi-ioved tag and two tag case at the same time
0618       auto theIOVs = PlotBase::getTag<0>().iovs;
0619       auto tagname1 = PlotBase::getTag<0>().name;
0620       std::string tagname2 = "";
0621       auto firstiov = theIOVs.front();
0622       std::tuple<cond::Time_t, cond::Hash> lastiov;
0623 
0624       // we don't support (yet) comparison with more than 2 tags
0625       assert(this->m_plotAnnotations.ntags < 3);
0626 
0627       if (this->m_plotAnnotations.ntags == 2) {
0628         auto tag2iovs = PlotBase::getTag<1>().iovs;
0629         tagname2 = PlotBase::getTag<1>().name;
0630         lastiov = tag2iovs.front();
0631       } else {
0632         lastiov = theIOVs.back();
0633       }
0634 
0635       std::shared_ptr<SiStripBadStrip> last_payload = this->fetchPayload(std::get<1>(lastiov));
0636       std::shared_ptr<SiStripBadStrip> first_payload = this->fetchPayload(std::get<1>(firstiov));
0637 
0638       std::string lastIOVsince = std::to_string(std::get<0>(lastiov));
0639       std::string firstIOVsince = std::to_string(std::get<0>(firstiov));
0640 
0641       // last payload
0642       std::vector<uint32_t> detid;
0643       last_payload->getDetIds(detid);
0644 
0645       SiStripDetSummary summaryLastBadStrips{&m_trackerTopo};
0646       int totalLastBadStrips = 0;
0647 
0648       for (const auto& d : detid) {
0649         SiStripBadStrip::Range range = last_payload->getRange(d);
0650         int badStrips(0);
0651         for (std::vector<unsigned int>::const_iterator badStrip = range.first; badStrip != range.second; ++badStrip) {
0652           badStrips += last_payload->decode(*badStrip).range;
0653         }
0654         totalLastBadStrips += badStrips;
0655         summaryLastBadStrips.add(d, badStrips);
0656       }
0657       std::map<unsigned int, SiStripDetSummary::Values> mapLastBadStrips = summaryLastBadStrips.getCounts();
0658 
0659       // first payload
0660       // needs to be cleared to avoid bias using only detIds of last payload
0661       detid.clear();
0662       first_payload->getDetIds(detid);
0663 
0664       SiStripDetSummary summaryFirstBadStrips{&m_trackerTopo};
0665       int totalFirstBadStrips = 0;
0666 
0667       for (const auto& d : detid) {
0668         SiStripBadStrip::Range range = first_payload->getRange(d);
0669         int badStrips(0);
0670         for (std::vector<unsigned int>::const_iterator badStrip = range.first; badStrip != range.second; ++badStrip) {
0671           badStrips += first_payload->decode(*badStrip).range;
0672         }
0673         totalFirstBadStrips += badStrips;
0674         summaryFirstBadStrips.add(d, badStrips);
0675       }
0676       std::map<unsigned int, SiStripDetSummary::Values> mapFirstBadStrips = summaryFirstBadStrips.getCounts();
0677 
0678       //=========================
0679 
0680       TCanvas canvas("BadStrip Partion summary", "SiStripBadStrip region summary", 1200, 1000);
0681       canvas.cd();
0682 
0683       auto h_LastBadStrips = std::make_unique<TH1F>("BadStripsbyRegion1",
0684                                                     "SiStrip Bad Strip summary by region;; n. bad strips",
0685                                                     mapLastBadStrips.size(),
0686                                                     0.,
0687                                                     mapLastBadStrips.size());
0688       h_LastBadStrips->SetStats(false);
0689 
0690       auto h_FirstBadStrips = std::make_unique<TH1F>("BadStripsbyRegion2",
0691                                                      "SiStrip Bad Strip summary by region;; n. bad strips",
0692                                                      mapFirstBadStrips.size(),
0693                                                      0.,
0694                                                      mapFirstBadStrips.size());
0695       h_FirstBadStrips->SetStats(false);
0696 
0697       canvas.SetBottomMargin(0.18);
0698       canvas.SetLeftMargin(0.12);
0699       canvas.SetRightMargin(0.05);
0700       canvas.Modified();
0701 
0702       std::vector<int> boundaries;
0703       unsigned int iBin = 0;
0704 
0705       std::string detector;
0706       std::string currentDetector;
0707 
0708       for (const auto& element : mapLastBadStrips) {
0709         iBin++;
0710         int countBadStrips = (element.second.mean);
0711 
0712         if (currentDetector.empty())
0713           currentDetector = "TIB";
0714 
0715         switch ((element.first) / 1000) {
0716           case 1:
0717             detector = "TIB";
0718             break;
0719           case 2:
0720             detector = "TOB";
0721             break;
0722           case 3:
0723             detector = "TEC";
0724             break;
0725           case 4:
0726             detector = "TID";
0727             break;
0728         }
0729 
0730         h_LastBadStrips->SetBinContent(iBin, countBadStrips);
0731         h_LastBadStrips->GetXaxis()->SetBinLabel(iBin, SiStripPI::regionType(element.first).second);
0732         h_LastBadStrips->GetXaxis()->LabelsOption("v");
0733 
0734         if (detector != currentDetector) {
0735           boundaries.push_back(iBin);
0736           currentDetector = detector;
0737         }
0738       }
0739 
0740       // reset the count
0741       iBin = 0;
0742 
0743       for (const auto& element : mapFirstBadStrips) {
0744         iBin++;
0745         int countBadStrips = (element.second.mean);
0746 
0747         h_FirstBadStrips->SetBinContent(iBin, countBadStrips);
0748         h_FirstBadStrips->GetXaxis()->SetBinLabel(iBin, SiStripPI::regionType(element.first).second);
0749         h_FirstBadStrips->GetXaxis()->LabelsOption("v");
0750       }
0751 
0752       auto extrema = SiStripPI::getExtrema(h_FirstBadStrips.get(), h_LastBadStrips.get());
0753       h_LastBadStrips->GetYaxis()->SetRangeUser(extrema.first, extrema.second);
0754 
0755       h_LastBadStrips->SetMarkerStyle(21);
0756       h_LastBadStrips->SetMarkerSize(1);
0757       h_LastBadStrips->SetLineColor(kBlue);
0758       h_LastBadStrips->SetFillColor(kBlue);
0759       h_LastBadStrips->SetLineStyle(9);
0760       h_LastBadStrips->SetMarkerColor(kBlue);
0761       h_LastBadStrips->GetYaxis()->SetRangeUser(0., h_LastBadStrips->GetMaximum() * 1.30);
0762       h_LastBadStrips->GetYaxis()->SetTitleOffset(1.7);
0763 
0764       h_LastBadStrips->SetBarWidth(0.45);
0765       h_LastBadStrips->SetBarOffset(0.1);
0766       h_LastBadStrips->Draw("bar2");
0767       h_LastBadStrips->Draw("TEXTsame");
0768 
0769       h_FirstBadStrips->SetMarkerStyle(20);
0770       h_FirstBadStrips->SetMarkerSize(1);
0771       h_FirstBadStrips->SetFillColor(kRed);
0772       h_FirstBadStrips->SetLineColor(kRed);
0773       h_FirstBadStrips->SetLineStyle(1);
0774       h_FirstBadStrips->SetMarkerColor(kRed);
0775       h_FirstBadStrips->GetYaxis()->SetTitleOffset(1.7);
0776 
0777       h_FirstBadStrips->SetBarWidth(0.4);
0778       h_FirstBadStrips->SetBarOffset(0.55);
0779 
0780       h_FirstBadStrips->Draw("bar2same");
0781       h_FirstBadStrips->Draw("TEXT45same");
0782 
0783       canvas.Update();
0784       canvas.cd();
0785 
0786       TLine l[boundaries.size()];
0787       unsigned int i = 0;
0788       for (const auto& line : boundaries) {
0789         l[i] = TLine(h_LastBadStrips->GetBinLowEdge(line),
0790                      canvas.cd()->GetUymin(),
0791                      h_LastBadStrips->GetBinLowEdge(line),
0792                      canvas.cd()->GetUymax());
0793         l[i].SetLineWidth(1);
0794         l[i].SetLineStyle(9);
0795         l[i].SetLineColor(kMagenta);
0796         l[i].Draw("same");
0797         i++;
0798       }
0799 
0800       TLegend legend = TLegend(0.52, 0.82, 0.95, 0.9);
0801       legend.SetHeader("Bad Components comparison", "C");  // option "C" allows to center the header
0802       legend.AddEntry(
0803           h_LastBadStrips.get(),
0804           ("IOV: " + std::to_string(std::get<0>(lastiov)) + "| n. of bad strips:" + std::to_string(totalLastBadStrips))
0805               .c_str(),
0806           "PL");
0807       legend.AddEntry(h_FirstBadStrips.get(),
0808                       ("IOV: " + std::to_string(std::get<0>(firstiov)) +
0809                        "| n. of bad strips:" + std::to_string(totalFirstBadStrips))
0810                           .c_str(),
0811                       "PL");
0812       legend.SetTextSize(0.025);
0813       legend.Draw("same");
0814 
0815       std::string fileName(this->m_imageFileName);
0816       canvas.SaveAs(fileName.c_str());
0817 
0818       return true;
0819     }
0820 
0821   private:
0822     TrackerTopology m_trackerTopo;
0823   };
0824 
0825   using SiStripBadStripByRegionComparisonSingleTag = SiStripBadStripByRegionComparisonBase<1, MULTI_IOV>;
0826   using SiStripBadStripByRegionComparisonTwoTags = SiStripBadStripByRegionComparisonBase<2, SINGLE_IOV>;
0827 
0828   /************************************************
0829     TrackerMap of SiStripBadStrip (bad strips fraction difference)
0830   *************************************************/
0831 
0832   template <int ntags, IOVMultiplicity nIOVs>
0833   class SiStripBadStripFractionComparisonTrackerMapBase : public PlotImage<SiStripBadStrip, nIOVs, ntags> {
0834   public:
0835     SiStripBadStripFractionComparisonTrackerMapBase()
0836         : PlotImage<SiStripBadStrip, nIOVs, ntags>("Tracker Map of SiStrip bad strip fraction difference") {}
0837 
0838     bool fill() override {
0839       // trick to deal with the multi-ioved tag and two tag case at the same time
0840       auto theIOVs = PlotBase::getTag<0>().iovs;
0841       auto tagname1 = PlotBase::getTag<0>().name;
0842       std::string tagname2 = "";
0843       auto firstiov = theIOVs.front();
0844       std::tuple<cond::Time_t, cond::Hash> lastiov;
0845 
0846       // we don't support (yet) comparison with more than 2 tags
0847       assert(this->m_plotAnnotations.ntags < 3);
0848 
0849       if (this->m_plotAnnotations.ntags == 2) {
0850         auto tag2iovs = PlotBase::getTag<1>().iovs;
0851         tagname2 = PlotBase::getTag<1>().name;
0852         lastiov = tag2iovs.front();
0853       } else {
0854         lastiov = theIOVs.back();
0855       }
0856 
0857       std::shared_ptr<SiStripBadStrip> last_payload = this->fetchPayload(std::get<1>(lastiov));
0858       std::shared_ptr<SiStripBadStrip> first_payload = this->fetchPayload(std::get<1>(firstiov));
0859 
0860       std::string lastIOVsince = std::to_string(std::get<0>(lastiov));
0861       std::string firstIOVsince = std::to_string(std::get<0>(firstiov));
0862 
0863       const auto detInfo =
0864           SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0865 
0866       std::string titleMap =
0867           "#Delta fraction of bad Strips per module (IOV:" + lastIOVsince + " - IOV:" + firstIOVsince + ")";
0868 
0869       std::unique_ptr<TrackerMap> tmap = std::make_unique<TrackerMap>("SiStripBadStrips");
0870       tmap->setTitle(titleMap);
0871       tmap->setPalette(1);
0872 
0873       std::vector<uint32_t> detid1;
0874       last_payload->getDetIds(detid1);
0875 
0876       std::map<uint32_t, float> FirstFractionPerDetId;
0877       std::map<uint32_t, float> LastFractionPerDetId;
0878 
0879       for (const auto& d : detid1) {
0880         SiStripBadStrip::Range range = last_payload->getRange(d);
0881         for (std::vector<unsigned int>::const_iterator badStrip = range.first; badStrip != range.second; ++badStrip) {
0882           LastFractionPerDetId[d] += last_payload->decode(*badStrip).range;
0883         }
0884         // normalize to the number of strips per module
0885         LastFractionPerDetId[d] /= (sistrip::STRIPS_PER_APV * detInfo.getNumberOfApvsAndStripLength(d).first);
0886       }  // loop over detIds
0887 
0888       std::vector<uint32_t> detid2;
0889       first_payload->getDetIds(detid2);
0890 
0891       //std::cout << "Size 2: " << detid1.size() << "| Size 1: "<< detid2.size() << std::endl;
0892 
0893       for (const auto& d : detid2) {
0894         SiStripBadStrip::Range range = first_payload->getRange(d);
0895         for (std::vector<unsigned int>::const_iterator badStrip = range.first; badStrip != range.second; ++badStrip) {
0896           FirstFractionPerDetId[d] += first_payload->decode(*badStrip).range;
0897         }
0898         // normalize to the number of strips per module
0899         FirstFractionPerDetId[d] /= (sistrip::STRIPS_PER_APV * detInfo.getNumberOfApvsAndStripLength(d).first);
0900       }  // loop over detIds
0901 
0902       std::vector<uint32_t> allDetIds = detInfo.getAllDetIds();
0903 
0904       int countLastButNotFirst(0);
0905       int countFirstButNotLast(0);
0906       int countBoth(0);
0907 
0908       for (const auto& d : allDetIds) {
0909         if (LastFractionPerDetId.find(d) != LastFractionPerDetId.end() &&
0910             FirstFractionPerDetId.find(d) == FirstFractionPerDetId.end()) {
0911           tmap->fill(d, LastFractionPerDetId[d]);
0912           countLastButNotFirst++;
0913         } else if (LastFractionPerDetId.find(d) == LastFractionPerDetId.end() &&
0914                    FirstFractionPerDetId.find(d) != FirstFractionPerDetId.end()) {
0915           tmap->fill(d, -FirstFractionPerDetId[d]);
0916           countFirstButNotLast++;
0917         } else if (LastFractionPerDetId.find(d) != LastFractionPerDetId.end() &&
0918                    FirstFractionPerDetId.find(d) != FirstFractionPerDetId.end()) {
0919           float delta = (LastFractionPerDetId[d] - FirstFractionPerDetId[d]);
0920           if (delta != 0.) {
0921             tmap->fill(d, delta);
0922           }
0923           countBoth++;
0924         }
0925       }
0926 
0927 #ifdef MMDEBUG
0928       std::cout << "In 2 but not in 1:" << countLastButNotFirst << std::endl;
0929       std::cout << "In 1 but not in 2:" << countFirstButNotLast << std::endl;
0930       std::cout << "In both:" << countBoth << std::endl;
0931 #endif
0932 
0933       //=========================
0934 
0935       std::string fileName(this->m_imageFileName);
0936       tmap->save(true, 0, 0, fileName);
0937 
0938       return true;
0939     }
0940   };
0941 
0942   using SiStripBadStripFractionComparisonTrackerMapSingleTag =
0943       SiStripBadStripFractionComparisonTrackerMapBase<1, MULTI_IOV>;
0944   using SiStripBadStripFractionComparisonTrackerMapTwoTags =
0945       SiStripBadStripFractionComparisonTrackerMapBase<2, SINGLE_IOV>;
0946 
0947   /************************************************
0948     Plot BadStrip Quality analysis 
0949   *************************************************/
0950 
0951   class SiStripBadStripQualityAnalysis : public PlotImage<SiStripBadStrip, SINGLE_IOV> {
0952   public:
0953     SiStripBadStripQualityAnalysis()
0954         : PlotImage<SiStripBadStrip, SINGLE_IOV>("SiStrip BadStrip Quality Analysis"),
0955           m_trackerTopo{StandaloneTrackerTopology::fromTrackerParametersXMLFile(
0956               edm::FileInPath("Geometry/TrackerCommonData/data/trackerParameters.xml").fullPath())} {}
0957 
0958     bool fill() override {
0959       auto tag = PlotBase::getTag<0>();
0960       auto iov = tag.iovs.front();
0961       std::shared_ptr<SiStripBadStrip> payload = fetchPayload(std::get<1>(iov));
0962 
0963       const auto detInfo =
0964           SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0965       SiStripQuality* siStripQuality_ = new SiStripQuality(detInfo);
0966       siStripQuality_->add(payload.get());
0967       siStripQuality_->cleanUp();
0968       siStripQuality_->fillBadComponents();
0969 
0970       // store global info
0971 
0972       //k: 0=BadModule, 1=BadFiber, 2=BadApv, 3=BadStrips
0973       int NTkBadComponent[4] = {0};
0974 
0975       //legend: NBadComponent[i][j][k]= SubSystem i, layer/disk/wheel j, BadModule/Fiber/Apv k
0976       //     i: 0=TIB, 1=TID, 2=TOB, 3=TEC
0977       //     k: 0=BadModule, 1=BadFiber, 2=BadApv, 3=BadStrips
0978       int NBadComponent[4][19][4] = {{{0}}};
0979 
0980       // call the filler
0981       SiStripPI::fillBCArrays(siStripQuality_, NTkBadComponent, NBadComponent, m_trackerTopo);
0982 
0983       //&&&&&&&&&&&&&&&&&&
0984       // printout
0985       //&&&&&&&&&&&&&&&&&&
0986 
0987       std::stringstream ss;
0988       ss.str("");
0989       ss << "\n-----------------\nGlobal Info\n-----------------";
0990       ss << "\nBadComponent \t   Modules \tFibers "
0991             "\tApvs\tStrips\n----------------------------------------------------------------";
0992       ss << "\nTracker:\t\t" << NTkBadComponent[0] << "\t" << NTkBadComponent[1] << "\t" << NTkBadComponent[2] << "\t"
0993          << NTkBadComponent[3];
0994       ss << "\n";
0995       ss << "\nTIB:\t\t\t" << NBadComponent[0][0][0] << "\t" << NBadComponent[0][0][1] << "\t" << NBadComponent[0][0][2]
0996          << "\t" << NBadComponent[0][0][3];
0997       ss << "\nTID:\t\t\t" << NBadComponent[1][0][0] << "\t" << NBadComponent[1][0][1] << "\t" << NBadComponent[1][0][2]
0998          << "\t" << NBadComponent[1][0][3];
0999       ss << "\nTOB:\t\t\t" << NBadComponent[2][0][0] << "\t" << NBadComponent[2][0][1] << "\t" << NBadComponent[2][0][2]
1000          << "\t" << NBadComponent[2][0][3];
1001       ss << "\nTEC:\t\t\t" << NBadComponent[3][0][0] << "\t" << NBadComponent[3][0][1] << "\t" << NBadComponent[3][0][2]
1002          << "\t" << NBadComponent[3][0][3];
1003       ss << "\n";
1004 
1005       for (int i = 1; i < 5; ++i)
1006         ss << "\nTIB Layer " << i << " :\t\t" << NBadComponent[0][i][0] << "\t" << NBadComponent[0][i][1] << "\t"
1007            << NBadComponent[0][i][2] << "\t" << NBadComponent[0][i][3];
1008       ss << "\n";
1009       for (int i = 1; i < 4; ++i)
1010         ss << "\nTID+ Disk " << i << " :\t\t" << NBadComponent[1][i][0] << "\t" << NBadComponent[1][i][1] << "\t"
1011            << NBadComponent[1][i][2] << "\t" << NBadComponent[1][i][3];
1012       for (int i = 4; i < 7; ++i)
1013         ss << "\nTID- Disk " << i - 3 << " :\t\t" << NBadComponent[1][i][0] << "\t" << NBadComponent[1][i][1] << "\t"
1014            << NBadComponent[1][i][2] << "\t" << NBadComponent[1][i][3];
1015       ss << "\n";
1016       for (int i = 1; i < 7; ++i)
1017         ss << "\nTOB Layer " << i << " :\t\t" << NBadComponent[2][i][0] << "\t" << NBadComponent[2][i][1] << "\t"
1018            << NBadComponent[2][i][2] << "\t" << NBadComponent[2][i][3];
1019       ss << "\n";
1020       for (int i = 1; i < 10; ++i)
1021         ss << "\nTEC+ Disk " << i << " :\t\t" << NBadComponent[3][i][0] << "\t" << NBadComponent[3][i][1] << "\t"
1022            << NBadComponent[3][i][2] << "\t" << NBadComponent[3][i][3];
1023       for (int i = 10; i < 19; ++i)
1024         ss << "\nTEC- Disk " << i - 9 << " :\t\t" << NBadComponent[3][i][0] << "\t" << NBadComponent[3][i][1] << "\t"
1025            << NBadComponent[3][i][2] << "\t" << NBadComponent[3][i][3];
1026       ss << "\n";
1027 
1028       edm::LogInfo("SiStripBadStrip_PayloadInspector") << ss.str() << std::endl;
1029       //std::cout<<  ss.str() << std::endl;
1030 
1031       auto masterTable = std::make_unique<TH2I>("table", "", 4, 0., 4., 39, 0., 39.);
1032 
1033       std::string labelsX[4] = {"Bad Modules", "Bad Fibers", "Bad APVs", "Bad Strips"};
1034       std::string labelsY[40] = {
1035           "Tracker",     "TIB",         "TID",         "TOB",         "TEC",         "TIB Layer 1", "TIB Layer 2",
1036           "TIB Layer 3", "TIB Layer 4", "TID+ Disk 1", "TID+ Disk 2", "TID+ Disk 3", "TID- Disk 1", "TID- Disk 2",
1037           "TID- Disk 3", "TOB Layer 1", "TOB Layer 2", "TOB Layer 3", "TOB Layer 4", "TOB Layer 5", "TOB Layer 6",
1038           "TEC+ Disk 1", "TEC+ Disk 2", "TEC+ Disk 3", "TEC+ Disk 4", "TEC+ Disk 5", "TEC+ Disk 6", "TEC+ Disk 7",
1039           "TEC+ Disk 8", "TEC+ Disk 9", "TEC- Disk 1", "TEC- Disk 2", "TEC- Disk 3", "TEC- Disk 4", "TEC- Disk 5",
1040           "TEC- Disk 6", "TEC- Disk 7", "TEC- Disk 8", "TEC- Disk 9"};
1041 
1042       for (int iX = 0; iX <= 3; iX++) {
1043         masterTable->GetXaxis()->SetBinLabel(iX + 1, labelsX[iX].c_str());
1044       }
1045 
1046       for (int iY = 39; iY >= 1; iY--) {
1047         masterTable->GetYaxis()->SetBinLabel(iY, labelsY[39 - iY].c_str());
1048       }
1049 
1050       //                        0 1 2  3
1051       int layerBoundaries[4] = {4, 6, 6, 18};
1052       std::vector<int> boundaries;
1053       boundaries.push_back(39);
1054       boundaries.push_back(35);
1055 
1056       int cursor = 0;
1057       int layerIndex = 0;
1058       for (int iY = 39; iY >= 1; iY--) {
1059         for (int iX = 0; iX <= 3; iX++) {
1060           if (iY == 39) {
1061             masterTable->SetBinContent(iX + 1, iY, NTkBadComponent[iX]);
1062           } else if (iY >= 35) {
1063             masterTable->SetBinContent(iX + 1, iY, NBadComponent[(39 - iY) - 1][0][iX]);
1064           } else {
1065             if (iX == 0)
1066               layerIndex++;
1067             //std::cout<<"iY:"<<iY << " cursor: "  <<cursor << " layerIndex: " << layerIndex << " layer check: "<< layerBoundaries[cursor] <<std::endl;
1068             masterTable->SetBinContent(iX + 1, iY, NBadComponent[cursor][layerIndex][iX]);
1069           }
1070         }
1071         if (layerIndex == layerBoundaries[cursor]) {
1072           // bring on the subdet counter and reset the layer count
1073           cursor++;
1074           layerIndex = 0;
1075           boundaries.push_back(iY);
1076         }
1077       }
1078 
1079       TCanvas canv("canv", "canv", 800, 800);
1080       canv.cd();
1081 
1082       canv.SetTopMargin(0.05);
1083       canv.SetBottomMargin(0.07);
1084       canv.SetLeftMargin(0.18);
1085       canv.SetRightMargin(0.05);
1086 
1087       masterTable->GetYaxis()->SetLabelSize(0.04);
1088       masterTable->GetXaxis()->SetLabelSize(0.05);
1089 
1090       masterTable->SetStats(false);
1091       canv.SetGrid();
1092 
1093       masterTable->Draw("text");
1094 
1095       canv.Update();
1096       canv.cd();
1097 
1098       TLine l[boundaries.size()];
1099       unsigned int i = 0;
1100       for (const auto& line : boundaries) {
1101         l[i] = TLine(canv.cd()->GetUxmin(),
1102                      masterTable->GetYaxis()->GetBinLowEdge(line),
1103                      canv.cd()->GetUxmax(),
1104                      masterTable->GetYaxis()->GetBinLowEdge(line));
1105         l[i].SetLineWidth(2);
1106         l[i].SetLineStyle(9);
1107         l[i].SetLineColor(kMagenta);
1108         l[i].Draw("same");
1109         i++;
1110       }
1111 
1112       canv.cd();
1113       TLatex title;
1114       title.SetTextSize(0.027);
1115       title.SetTextColor(kBlue);
1116       title.DrawLatexNDC(0.12, 0.97, ("IOV: " + std::to_string(std::get<0>(iov)) + "| " + std::get<1>(iov)).c_str());
1117       std::string fileName(m_imageFileName);
1118       canv.SaveAs(fileName.c_str());
1119 
1120       delete siStripQuality_;
1121       return true;
1122     }
1123 
1124   private:
1125     TrackerTopology m_trackerTopo;
1126   };
1127 
1128   /************************************************
1129     Plot BadStrip Quality Comparison
1130   *************************************************/
1131 
1132   template <int ntags, IOVMultiplicity nIOVs>
1133   class SiStripBadStripQualityComparisonBase : public PlotImage<SiStripBadStrip, nIOVs, ntags> {
1134   public:
1135     SiStripBadStripQualityComparisonBase()
1136         : PlotImage<SiStripBadStrip, nIOVs, ntags>("SiStrip BadStrip Quality Comparison Analysis"),
1137           m_trackerTopo{StandaloneTrackerTopology::fromTrackerParametersXMLFile(
1138               edm::FileInPath("Geometry/TrackerCommonData/data/trackerParameters.xml").fullPath())} {}
1139 
1140     bool fill() override {
1141       //SiStripPI::setPaletteStyle(SiStripPI::BLUERED);
1142       gStyle->SetPalette(kTemperatureMap);
1143 
1144       // trick to deal with the multi-ioved tag and two tag case at the same time
1145       auto theIOVs = PlotBase::getTag<0>().iovs;
1146       auto tagname1 = PlotBase::getTag<0>().name;
1147       std::string tagname2 = "";
1148       auto firstiov = theIOVs.front();
1149       std::tuple<cond::Time_t, cond::Hash> lastiov;
1150 
1151       // we don't support (yet) comparison with more than 2 tags
1152       assert(this->m_plotAnnotations.ntags < 3);
1153 
1154       if (this->m_plotAnnotations.ntags == 2) {
1155         auto tag2iovs = PlotBase::getTag<1>().iovs;
1156         tagname2 = PlotBase::getTag<1>().name;
1157         lastiov = tag2iovs.front();
1158       } else {
1159         lastiov = theIOVs.back();
1160       }
1161 
1162       std::shared_ptr<SiStripBadStrip> last_payload = this->fetchPayload(std::get<1>(lastiov));
1163       std::shared_ptr<SiStripBadStrip> first_payload = this->fetchPayload(std::get<1>(firstiov));
1164 
1165       std::string lastIOVsince = std::to_string(std::get<0>(lastiov));
1166       std::string firstIOVsince = std::to_string(std::get<0>(firstiov));
1167 
1168       // store global info
1169 
1170       //k: 0=BadModule, 1=BadFiber, 2=BadApv, 3=BadStrips
1171       int f_NTkBadComponent[4] = {0};
1172       int l_NTkBadComponent[4] = {0};
1173 
1174       // for the total
1175       int tot_NTkComponents[4] = {0};
1176 
1177       //legend: NBadComponent[i][j][k]= SubSystem i, layer/disk/wheel j, BadModule/Fiber/Apv k
1178       //     i: 0=TIB, 1=TID, 2=TOB, 3=TEC
1179       //     k: 0=BadModule, 1=BadFiber, 2=BadApv, 3=BadStrips
1180       int f_NBadComponent[4][19][4] = {{{0}}};
1181       int l_NBadComponent[4][19][4] = {{{0}}};
1182 
1183       // for the total
1184       int totNComponents[4][19][4] = {{{0}}};
1185 
1186       const auto detInfo =
1187           SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
1188       SiStripQuality* f_siStripQuality_ = new SiStripQuality(detInfo);
1189       f_siStripQuality_->add(first_payload.get());
1190       f_siStripQuality_->cleanUp();
1191       f_siStripQuality_->fillBadComponents();
1192 
1193       // call the filler
1194       SiStripPI::fillBCArrays(f_siStripQuality_, f_NTkBadComponent, f_NBadComponent, m_trackerTopo);
1195 
1196       SiStripQuality* l_siStripQuality_ = new SiStripQuality(detInfo);
1197       l_siStripQuality_->add(last_payload.get());
1198       l_siStripQuality_->cleanUp();
1199       l_siStripQuality_->fillBadComponents();
1200 
1201       // call the filler
1202       SiStripPI::fillBCArrays(l_siStripQuality_, l_NTkBadComponent, l_NBadComponent, m_trackerTopo);
1203 
1204       // fill the total number of components
1205       SiStripPI::fillTotalComponents(tot_NTkComponents, totNComponents, m_trackerTopo);
1206 
1207       // debug
1208       //SiStripPI::printBCDebug(f_NTkBadComponent,f_NBadComponent);
1209       //SiStripPI::printBCDebug(l_NTkBadComponent,l_NBadComponent);
1210 
1211       //SiStripPI::printBCDebug(tot_NTkComponents,totNComponents);
1212 
1213       // declare histograms
1214       auto masterTable = std::make_unique<TH2F>("table", "", 4, 0., 4., 39, 0., 39.);
1215       auto masterTableColor = std::make_unique<TH2F>("colortable", "", 4, 0., 4., 39, 0., 39.);
1216 
1217       std::string labelsX[4] = {"Bad Modules", "Bad Fibers", "Bad APVs", "Bad Strips"};
1218       std::string labelsY[40] = {
1219           "Tracker",     "TIB",         "TID",         "TOB",         "TEC",         "TIB Layer 1", "TIB Layer 2",
1220           "TIB Layer 3", "TIB Layer 4", "TID+ Disk 1", "TID+ Disk 2", "TID+ Disk 3", "TID- Disk 1", "TID- Disk 2",
1221           "TID- Disk 3", "TOB Layer 1", "TOB Layer 2", "TOB Layer 3", "TOB Layer 4", "TOB Layer 5", "TOB Layer 6",
1222           "TEC+ Disk 1", "TEC+ Disk 2", "TEC+ Disk 3", "TEC+ Disk 4", "TEC+ Disk 5", "TEC+ Disk 6", "TEC+ Disk 7",
1223           "TEC+ Disk 8", "TEC+ Disk 9", "TEC- Disk 1", "TEC- Disk 2", "TEC- Disk 3", "TEC- Disk 4", "TEC- Disk 5",
1224           "TEC- Disk 6", "TEC- Disk 7", "TEC- Disk 8", "TEC- Disk 9"};
1225 
1226       for (int iX = 0; iX <= 3; iX++) {
1227         masterTable->GetXaxis()->SetBinLabel(iX + 1, labelsX[iX].c_str());
1228         masterTableColor->GetXaxis()->SetBinLabel(iX + 1, labelsX[iX].c_str());
1229       }
1230 
1231       for (int iY = 39; iY >= 1; iY--) {
1232         masterTable->GetYaxis()->SetBinLabel(iY, labelsY[39 - iY].c_str());
1233         masterTableColor->GetYaxis()->SetBinLabel(iY, labelsY[39 - iY].c_str());
1234       }
1235 
1236       //                        0 1 2 3
1237       int layerBoundaries[4] = {4, 6, 6, 18};
1238       std::vector<int> boundaries;
1239       boundaries.push_back(39);
1240       boundaries.push_back(35);
1241 
1242       int cursor = 0;
1243       int layerIndex = 0;
1244       for (int iY = 39; iY >= 1; iY--) {
1245         for (int iX = 0; iX <= 3; iX++) {
1246           if (iY == 39) {
1247             masterTable->SetBinContent(iX + 1, iY, l_NTkBadComponent[iX] - f_NTkBadComponent[iX]);
1248             masterTableColor->SetBinContent(
1249                 iX + 1, iY, 100 * float(l_NTkBadComponent[iX] - f_NTkBadComponent[iX]) / tot_NTkComponents[iX]);
1250             //std::cout<< (l_NTkBadComponent[iX]-f_NTkBadComponent[iX]) << " " << tot_NTkComponents[iX] << " " << float(l_NTkBadComponent[iX]-f_NTkBadComponent[iX])/tot_NTkComponents[iX] << std::endl;
1251           } else if (iY >= 35) {
1252             masterTable->SetBinContent(
1253                 iX + 1, iY, (l_NBadComponent[(39 - iY) - 1][0][iX] - f_NBadComponent[(39 - iY) - 1][0][iX]));
1254             masterTableColor->SetBinContent(
1255                 iX + 1,
1256                 iY,
1257                 100 * float(l_NBadComponent[(39 - iY) - 1][0][iX] - f_NBadComponent[(39 - iY) - 1][0][iX]) /
1258                     totNComponents[(39 - iY) - 1][0][iX]);
1259           } else {
1260             if (iX == 0)
1261               layerIndex++;
1262             //std::cout<<"iY:"<<iY << " cursor: "  <<cursor << " layerIndex: " << layerIndex << " layer check: "<< layerBoundaries[cursor] <<std::endl;
1263             masterTable->SetBinContent(
1264                 iX + 1, iY, (l_NBadComponent[cursor][layerIndex][iX] - f_NBadComponent[cursor][layerIndex][iX]));
1265             masterTableColor->SetBinContent(
1266                 iX + 1,
1267                 iY,
1268                 100 * float(l_NBadComponent[cursor][layerIndex][iX] - f_NBadComponent[cursor][layerIndex][iX]) /
1269                     totNComponents[cursor][layerIndex][iX]);
1270           }
1271         }
1272         if (layerIndex == layerBoundaries[cursor]) {
1273           // bring on the subdet counter and reset the layer count
1274           cursor++;
1275           layerIndex = 0;
1276           boundaries.push_back(iY);
1277         }
1278       }
1279 
1280       TCanvas canv("canv", "canv", 1000, 800);
1281       canv.cd();
1282 
1283       canv.SetTopMargin(0.05);
1284       canv.SetBottomMargin(0.07);
1285       canv.SetLeftMargin(0.13);
1286       canv.SetRightMargin(0.16);
1287 
1288       masterTable->SetStats(false);
1289       masterTableColor->SetStats(false);
1290       canv.SetGrid();
1291 
1292       masterTable->SetMarkerColor(kBlack);
1293       masterTable->SetMarkerSize(1.5);
1294 
1295       float extremum = std::abs(masterTableColor->GetMaximum()) > std::abs(masterTableColor->GetMinimum())
1296                            ? std::abs(masterTableColor->GetMaximum())
1297                            : std::abs(masterTableColor->GetMinimum());
1298       //masterTableColor->Draw("text");
1299       masterTableColor->GetZaxis()->SetRangeUser(-extremum, extremum);
1300       masterTableColor->GetZaxis()->SetTitle("percent change [%]");
1301       masterTableColor->GetZaxis()->CenterTitle(true);
1302       masterTableColor->GetZaxis()->SetTitleSize(0.05);
1303 
1304       masterTableColor->GetYaxis()->SetLabelSize(0.04);
1305       masterTableColor->GetXaxis()->SetLabelSize(0.06);
1306 
1307       masterTable->GetYaxis()->SetLabelSize(0.04);
1308       masterTable->GetXaxis()->SetLabelSize(0.06);
1309 
1310       masterTableColor->Draw("COLZ");
1311       masterTable->Draw("textsame");
1312 
1313       canv.Update();
1314       canv.cd();
1315 
1316       TLine l[boundaries.size()];
1317       unsigned int i = 0;
1318       for (const auto& line : boundaries) {
1319         l[i] = TLine(canv.cd()->GetUxmin(),
1320                      masterTable->GetYaxis()->GetBinLowEdge(line),
1321                      canv.cd()->GetUxmax(),
1322                      masterTable->GetYaxis()->GetBinLowEdge(line));
1323         l[i].SetLineWidth(2);
1324         l[i].SetLineStyle(9);
1325         l[i].SetLineColor(kMagenta);
1326         l[i].Draw("same");
1327         i++;
1328       }
1329 
1330       canv.cd();
1331       TLatex title;
1332       title.SetTextSize(0.045);
1333       title.SetTextColor(kBlue);
1334       title.DrawLatexNDC(
1335           0.33,
1336           0.96,
1337           ("#DeltaIOV: " + std::to_string(std::get<0>(lastiov)) + " - " + std::to_string(std::get<0>(firstiov)))
1338               .c_str());
1339       std::string fileName(this->m_imageFileName);
1340       canv.SaveAs(fileName.c_str());
1341 
1342       delete f_siStripQuality_;
1343       delete l_siStripQuality_;
1344 
1345       return true;
1346     }
1347 
1348   private:
1349     TrackerTopology m_trackerTopo;
1350   };
1351 
1352   using SiStripBadStripQualityComparisonSingleTag = SiStripBadStripQualityComparisonBase<1, MULTI_IOV>;
1353   using SiStripBadStripQualityComparisonTwoTags = SiStripBadStripQualityComparisonBase<2, SINGLE_IOV>;
1354 
1355 }  // namespace
1356 
1357 // Register the classes as boost python plugin
1358 PAYLOAD_INSPECTOR_MODULE(SiStripBadStrip) {
1359   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripTest);
1360   PAYLOAD_INSPECTOR_CLASS(SiStripBadModuleTrackerMap);
1361   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripFractionTrackerMap);
1362   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripFractionTkMap);
1363   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripFractionByRun);
1364   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripTIBFractionByRun);
1365   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripTOBFractionByRun);
1366   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripTIDFractionByRun);
1367   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripTECFractionByRun);
1368   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripByRegion);
1369   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripByRegionComparisonSingleTag);
1370   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripByRegionComparisonTwoTags);
1371   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripFractionComparisonTrackerMapSingleTag);
1372   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripFractionComparisonTrackerMapTwoTags);
1373   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripQualityAnalysis);
1374   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripQualityComparisonSingleTag);
1375   PAYLOAD_INSPECTOR_CLASS(SiStripBadStripQualityComparisonTwoTags);
1376 }