Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-11 04:32:10

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