Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:48

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