Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /*!
0002   \file SiStripFedCabling_PayloadInspector
0003   \Payload Inspector Plugin for SiStrip Fed Cabling
0004   \author M. Musich
0005   \version $Revision: 1.0 $
0006   \date $Date: 2018/11/02 17:05:56 $
0007 */
0008 
0009 #include "CondCore/Utilities/interface/PayloadInspectorModule.h"
0010 #include "CondCore/Utilities/interface/PayloadInspector.h"
0011 #include "CondCore/CondDB/interface/Time.h"
0012 
0013 #include "CondFormats/SiStripObjects/interface/SiStripDetSummary.h"
0014 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
0015 #include "CalibFormats/SiStripObjects/interface/SiStripDetCabling.h"
0016 
0017 #include "CommonTools/TrackerMap/interface/TrackerMap.h"
0018 #include "CondCore/SiStripPlugins/interface/SiStripPayloadInspectorHelper.h"
0019 #include "CalibTracker/StandaloneTrackerTopology/interface/StandaloneTrackerTopology.h"
0020 #include "CalibTracker/SiStripCommon/interface/SiStripDetInfoFileReader.h"
0021 
0022 #include <fmt/format.h>
0023 #include <memory>
0024 #include <sstream>
0025 
0026 #include "TCanvas.h"
0027 #include "TH2D.h"
0028 #include "TLatex.h"
0029 
0030 namespace {
0031 
0032   using namespace cond::payloadInspector;
0033 
0034   /************************************************
0035     TrackerMap of SiStrip FED Cabling
0036   *************************************************/
0037   class SiStripFedCabling_TrackerMap : public PlotImage<SiStripFedCabling, SINGLE_IOV> {
0038   public:
0039     SiStripFedCabling_TrackerMap() : PlotImage<SiStripFedCabling, SINGLE_IOV>("Tracker Map SiStrip Fed Cabling") {}
0040 
0041     bool fill() override {
0042       auto tag = PlotBase::getTag<0>();
0043       auto iov = tag.iovs.front();
0044       std::shared_ptr<SiStripFedCabling> payload = fetchPayload(std::get<1>(iov));
0045 
0046       std::unique_ptr<TrackerMap> tmap = std::make_unique<TrackerMap>("SiStripFedCabling");
0047       tmap->setPalette(1);
0048       std::string titleMap = "TrackerMap of SiStrip Fed Cabling per module, IOV : " + std::to_string(std::get<0>(iov));
0049       tmap->setTitle(titleMap);
0050 
0051       TrackerTopology tTopo = StandaloneTrackerTopology::fromTrackerParametersXMLFile(
0052           edm::FileInPath("Geometry/TrackerCommonData/data/trackerParameters.xml").fullPath());
0053       std::unique_ptr<SiStripDetCabling> detCabling_ = std::make_unique<SiStripDetCabling>(*(payload.get()), &tTopo);
0054 
0055       std::vector<uint32_t> activeDetIds;
0056       detCabling_->addActiveDetectorsRawIds(activeDetIds);
0057 
0058       for (const auto& detId : activeDetIds) {
0059         int32_t n_conn = 0;
0060         for (uint32_t connDet_i = 0; connDet_i < detCabling_->getConnections(detId).size(); connDet_i++) {
0061           if (detCabling_->getConnections(detId)[connDet_i] != nullptr &&
0062               detCabling_->getConnections(detId)[connDet_i]->isConnected() != 0)
0063             n_conn++;
0064         }
0065         if (n_conn != 0) {
0066           tmap->fill(detId, n_conn * 2);
0067         }
0068       }
0069 
0070       std::string fileName(m_imageFileName);
0071       tmap->save(true, 0., 6., fileName, 4500, 2400);  // max 6 APVs per module
0072 
0073       return true;
0074     }
0075   };
0076 
0077   /************************************************
0078     TrackerMap of uncabled modules
0079   *************************************************/
0080   class SiStripUncabledChannels_TrackerMap : public PlotImage<SiStripFedCabling, SINGLE_IOV> {
0081   public:
0082     SiStripUncabledChannels_TrackerMap()
0083         : PlotImage<SiStripFedCabling, SINGLE_IOV>("Tracker Map SiStrip Fed Cabling") {}
0084 
0085     bool fill() override {
0086       auto tag = PlotBase::getTag<0>();
0087       auto iov = tag.iovs.front();
0088       std::shared_ptr<SiStripFedCabling> payload = fetchPayload(std::get<1>(iov));
0089 
0090       std::unique_ptr<TrackerMap> tmap = std::make_unique<TrackerMap>("SiStripFedCabling");
0091       tmap->setPalette(1);
0092       std::string titleMap =
0093           "TrackerMap of SiStrip Fraction of uncabled channels per module, IOV : " + std::to_string(std::get<0>(iov));
0094       tmap->setTitle(titleMap);
0095 
0096       TrackerTopology tTopo = StandaloneTrackerTopology::fromTrackerParametersXMLFile(
0097           edm::FileInPath("Geometry/TrackerCommonData/data/trackerParameters.xml").fullPath());
0098       std::unique_ptr<SiStripDetCabling> detCabling_ = std::make_unique<SiStripDetCabling>(*(payload.get()), &tTopo);
0099 
0100       std::vector<uint32_t> activeDetIds;
0101       detCabling_->addActiveDetectorsRawIds(activeDetIds);
0102 
0103       const auto detInfo =
0104           SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0105       std::vector<uint32_t> all_detids = detInfo.getAllDetIds();
0106 
0107       // first add the fully unconnected modules
0108       for (const auto& detId : all_detids) {
0109         if (!detCabling_->IsConnected(detId)) {
0110           tmap->fill(detId, 1);
0111         }
0112       }
0113 
0114       // then add the partially unconnected ones
0115       for (const auto& detId : activeDetIds) {
0116         float frac = calculateConnectedFraction(detCabling_.get(), detId);
0117         if (frac != 1.f) {
0118           tmap->fill(detId, frac);
0119         }
0120       }
0121 
0122       std::string fileName(m_imageFileName);
0123       tmap->save(true, 0., 1., fileName, 4500, 2400);
0124 
0125       return true;
0126     }
0127 
0128   private:
0129     // Function to calculate the number of connections for a given detId
0130     int32_t calculateConnectedFraction(const SiStripDetCabling* detCabling, const uint32_t detId) {
0131       float totAPVs = detCabling->nApvPairs(detId);
0132       float n_conn{0};
0133       for (uint32_t connDet_i = 0; connDet_i < detCabling->getConnections(detId).size(); connDet_i++) {
0134         if (detCabling->getConnections(detId)[connDet_i] != nullptr &&
0135             detCabling->getConnections(detId)[connDet_i]->isConnected() != 0) {
0136           n_conn++;
0137         }
0138       }
0139       return n_conn / totAPVs;
0140     }
0141   };
0142 
0143   /************************************************
0144     TrackerMap of SiStrip FED Cabling difference between 2 payloads
0145   *************************************************/
0146 
0147   template <int ntags, IOVMultiplicity nIOVs>
0148   class SiStripFedCablingComparisonTrackerMapBase : public PlotImage<SiStripFedCabling, nIOVs, ntags> {
0149   public:
0150     SiStripFedCablingComparisonTrackerMapBase()
0151         : PlotImage<SiStripFedCabling, nIOVs, ntags>("Tracker Map SiStrip Fed Cabling difference") {}
0152 
0153     bool fill() override {
0154       // trick to deal with the multi-ioved tag and two tag case at the same time
0155       auto theIOVs = PlotBase::getTag<0>().iovs;
0156       auto tagname1 = PlotBase::getTag<0>().name;
0157       std::string tagname2 = "";
0158       auto firstiov = theIOVs.front();
0159       std::tuple<cond::Time_t, cond::Hash> lastiov;
0160 
0161       // we don't support (yet) comparison with more than 2 tags
0162       assert(this->m_plotAnnotations.ntags < 3);
0163 
0164       if (this->m_plotAnnotations.ntags == 2) {
0165         auto tag2iovs = PlotBase::getTag<1>().iovs;
0166         tagname2 = PlotBase::getTag<1>().name;
0167         lastiov = tag2iovs.front();
0168       } else {
0169         lastiov = theIOVs.back();
0170       }
0171 
0172       std::shared_ptr<SiStripFedCabling> last_payload = this->fetchPayload(std::get<1>(lastiov));
0173       std::shared_ptr<SiStripFedCabling> first_payload = this->fetchPayload(std::get<1>(firstiov));
0174 
0175       std::string lastIOVsince = std::to_string(std::get<0>(lastiov));
0176       std::string firstIOVsince = std::to_string(std::get<0>(firstiov));
0177 
0178       std::unique_ptr<TrackerMap> tmap = std::make_unique<TrackerMap>("SiStripFedCabling Difference");
0179       tmap->setPalette(1);
0180 
0181       std::string titleMap{};
0182       std::string commonPart = "SiStrip Fed Cabling Map: #Delta connections per module";
0183       if (this->m_plotAnnotations.ntags == 2) {
0184         titleMap = fmt::format("{}: {} - {}", commonPart, tagname2, tagname1);
0185       } else {
0186         titleMap = fmt::format("{}: IOV : {} - {}", commonPart, std::get<0>(lastiov), std::get<0>(firstiov));
0187       }
0188       tmap->setTitle(titleMap);
0189 
0190       const std::string k_TrackerParams =
0191           edm::FileInPath("Geometry/TrackerCommonData/data/trackerParameters.xml").fullPath();
0192       TrackerTopology tTopo = StandaloneTrackerTopology::fromTrackerParametersXMLFile(k_TrackerParams);
0193 
0194       std::unique_ptr<SiStripDetCabling> l_detCabling =
0195           std::make_unique<SiStripDetCabling>(*(last_payload.get()), &tTopo);
0196       std::unique_ptr<SiStripDetCabling> f_detCabling =
0197           std::make_unique<SiStripDetCabling>(*(first_payload.get()), &tTopo);
0198 
0199       std::vector<uint32_t> f_activeDetIds;
0200       f_detCabling->addActiveDetectorsRawIds(f_activeDetIds);
0201 
0202       std::vector<uint32_t> l_activeDetIds;
0203       l_detCabling->addActiveDetectorsRawIds(l_activeDetIds);
0204 
0205       const auto& setsToPlot = prepareSets(f_activeDetIds, l_activeDetIds);
0206 
0207       edm::LogPrint("SiStripFedCablingComparisonTrackerMapBase")
0208           << "Common Detids: " << setsToPlot.commonElements.size()
0209           << " | only in last payload: " << setsToPlot.lExclusiveElements.size()
0210           << " | only in first payload: " << setsToPlot.fExclusiveElements.size() << std::endl;
0211 
0212       // Process common elements
0213       for (const auto& detId : setsToPlot.commonElements) {
0214         int32_t f_n_conn = calculateConnections(f_detCabling.get(), detId);
0215         int32_t l_n_conn = calculateConnections(l_detCabling.get(), detId);
0216 
0217         if (l_n_conn != f_n_conn) {
0218           tmap->fill(detId, (l_n_conn - f_n_conn) * 2);  // 2 APVs per channel
0219         }
0220       }
0221 
0222       // Process elements only in the last
0223       for (const auto& detId : setsToPlot.lExclusiveElements) {
0224         int32_t l_n_conn = calculateConnections(l_detCabling.get(), detId);
0225 
0226         if (l_n_conn != 0) {
0227           tmap->fill(detId, l_n_conn * 2);  // 2 APVs per channel
0228         }
0229       }
0230 
0231       // Process elements only in the first
0232       for (const auto& detId : setsToPlot.fExclusiveElements) {
0233         int32_t f_n_conn = calculateConnections(f_detCabling.get(), detId);
0234 
0235         if (f_n_conn != 0) {
0236           tmap->fill(detId, -f_n_conn * 2);  // 2 APVs per channel
0237         }
0238       }
0239 
0240       std::string fileName(this->m_imageFileName);
0241       tmap->save(true, -6., 6., fileName, 4500, 2400);  // max 6 APVs per module
0242 
0243       return true;
0244     }
0245 
0246   private:
0247     struct setsOfDetids {
0248       std::vector<uint32_t> commonElements;
0249       std::vector<uint32_t> fExclusiveElements;
0250       std::vector<uint32_t> lExclusiveElements;
0251     };
0252 
0253     // Function to calculate the number of connections for a given detId
0254     int32_t calculateConnections(const SiStripDetCabling* detCabling, const uint32_t detId) {
0255       int32_t n_conn = 0;
0256       for (uint32_t connDet_i = 0; connDet_i < detCabling->getConnections(detId).size(); connDet_i++) {
0257         if (detCabling->getConnections(detId)[connDet_i] != nullptr &&
0258             detCabling->getConnections(detId)[connDet_i]->isConnected() != 0) {
0259           n_conn++;
0260         }
0261       }
0262       return n_conn;
0263     }
0264 
0265     // Function to calculate the interesection and exclusive elements out of two vectors of DetIds
0266     setsOfDetids prepareSets(std::vector<uint32_t> f_activeDetIds, std::vector<uint32_t> l_activeDetIds) {
0267       setsOfDetids output;
0268       // Sort the input vectors if they are not already sorted
0269       std::sort(f_activeDetIds.begin(), f_activeDetIds.end());
0270       std::sort(l_activeDetIds.begin(), l_activeDetIds.end());
0271 
0272       // Common elements
0273       output.commonElements.reserve(std::min(f_activeDetIds.size(), l_activeDetIds.size()));
0274       std::set_intersection(f_activeDetIds.begin(),
0275                             f_activeDetIds.end(),
0276                             l_activeDetIds.begin(),
0277                             l_activeDetIds.end(),
0278                             std::back_inserter(output.commonElements));
0279 
0280       // Elements only in f_activeDetIds
0281       output.fExclusiveElements.reserve(f_activeDetIds.size() - output.commonElements.size());
0282       std::set_difference(f_activeDetIds.begin(),
0283                           f_activeDetIds.end(),
0284                           l_activeDetIds.begin(),
0285                           l_activeDetIds.end(),
0286                           std::back_inserter(output.fExclusiveElements));
0287 
0288       // Elements only in l_activeDetIds
0289       output.lExclusiveElements.reserve(l_activeDetIds.size() - output.commonElements.size());
0290       std::set_difference(l_activeDetIds.begin(),
0291                           l_activeDetIds.end(),
0292                           f_activeDetIds.begin(),
0293                           f_activeDetIds.end(),
0294                           std::back_inserter(output.lExclusiveElements));
0295 
0296       return output;
0297     }
0298   };
0299 
0300   using SiStripFedCablingComparisonTrackerMapSingleTag = SiStripFedCablingComparisonTrackerMapBase<1, MULTI_IOV>;
0301   using SiStripFedCablingComparisonTrackerMapTwoTags = SiStripFedCablingComparisonTrackerMapBase<2, SINGLE_IOV>;
0302 
0303   /************************************************
0304     Summary Plot of SiStrip FED Cabling
0305   *************************************************/
0306   class SiStripFedCabling_Summary : public PlotImage<SiStripFedCabling, SINGLE_IOV> {
0307   public:
0308     SiStripFedCabling_Summary() : PlotImage<SiStripFedCabling, SINGLE_IOV>("SiStrip Fed Cabling Summary") {}
0309 
0310     bool fill() override {
0311       auto tag = PlotBase::getTag<0>();
0312       auto iov = tag.iovs.front();
0313       std::shared_ptr<SiStripFedCabling> payload = fetchPayload(std::get<1>(iov));
0314       int IOV = std::get<0>(iov);
0315       std::vector<uint32_t> activeDetIds;
0316 
0317       TrackerTopology tTopo = StandaloneTrackerTopology::fromTrackerParametersXMLFile(
0318           edm::FileInPath("Geometry/TrackerCommonData/data/trackerParameters.xml").fullPath());
0319       std::unique_ptr<SiStripDetCabling> detCabling_ = std::make_unique<SiStripDetCabling>(*(payload.get()), &tTopo);
0320 
0321       detCabling_->addActiveDetectorsRawIds(activeDetIds);
0322 
0323       containers myCont;
0324       containers allCounts;
0325 
0326       const auto detInfo =
0327           SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0328       for (const auto& it : detInfo.getAllData()) {
0329         // check if det id is correct and if it is actually cabled in the detector
0330         if (it.first == 0 || it.first == 0xFFFFFFFF) {
0331           edm::LogError("DetIdNotGood") << "@SUB=analyze"
0332                                         << "Wrong det id: " << it.first << "  ... neglecting!" << std::endl;
0333           continue;
0334         }
0335         updateCounters(it.first, allCounts, tTopo);
0336       }
0337 
0338       for (const auto& detId : activeDetIds) {
0339         updateCounters(detId, myCont, tTopo);
0340       }
0341 
0342       TH2D* ME = new TH2D("SummaryOfCabling", "SummaryOfCabling", 6, 0.5, 6.5, 9, 0.5, 9.5);
0343       ME->GetXaxis()->SetTitle("Sub Det");
0344       ME->GetYaxis()->SetTitle("Layer");
0345 
0346       ME->SetTitle("");
0347 
0348       ME->GetXaxis()->SetBinLabel(1, "TIB");
0349       ME->GetXaxis()->SetBinLabel(2, "TID F");
0350       ME->GetXaxis()->SetBinLabel(3, "TID B");
0351       ME->GetXaxis()->SetBinLabel(4, "TOB");
0352       ME->GetXaxis()->SetBinLabel(5, "TEC F");
0353       ME->GetXaxis()->SetBinLabel(6, "TEC B");
0354 
0355       for (int i = 0; i < 4; i++) {
0356         ME->Fill(1, i + 1, float(myCont.counterTIB[i]) / allCounts.counterTIB[i]);
0357       }
0358 
0359       for (int i = 0; i < 2; i++) {
0360         for (int j = 0; j < 3; j++) {
0361           ME->Fill(i + 2, j + 1, float(myCont.counterTID[i][j]) / allCounts.counterTID[i][j]);
0362         }
0363       }
0364 
0365       for (int i = 0; i < 6; i++) {
0366         ME->Fill(4, i + 1, float(myCont.counterTOB[i]) / allCounts.counterTOB[i]);
0367       }
0368 
0369       for (int i = 0; i < 2; i++) {
0370         for (int j = 0; j < 9; j++) {
0371           ME->Fill(i + 5, j + 1, float(myCont.counterTEC[i][j]) / allCounts.counterTEC[i][j]);
0372         }
0373       }
0374 
0375       TCanvas c1("SiStrip FED cabling summary", "SiStrip FED cabling summary", 800, 600);
0376       c1.SetTopMargin(0.07);
0377       c1.SetBottomMargin(0.10);
0378       c1.SetLeftMargin(0.07);
0379       c1.SetRightMargin(0.10);
0380 
0381       ME->Draw("colz");
0382       ME->Draw("TEXTsame");
0383       ME->SetStats(kFALSE);
0384 
0385       TLatex t1;
0386       t1.SetNDC();
0387       t1.SetTextAlign(26);
0388       t1.SetTextSize(0.05);
0389       t1.DrawLatex(0.5, 0.96, Form("SiStrip FedCabling, IOV %i", IOV));
0390 
0391       std::string fileName(m_imageFileName);
0392       c1.SaveAs(fileName.c_str());
0393 
0394       return true;
0395     }
0396 
0397   private:
0398     struct containers {
0399     public:
0400       int counterTIB[4] = {0};
0401       int counterTID[2][3] = {{0}};
0402       int counterTOB[6] = {0};
0403       int counterTEC[2][9] = {{0}};
0404     };
0405 
0406     void updateCounters(int detId, containers& cont, const TrackerTopology& tTopo) {
0407       StripSubdetector subdet(detId);
0408 
0409       switch (subdet.subdetId()) {
0410         case StripSubdetector::TIB: {
0411           int i = tTopo.tibLayer(detId) - 1;
0412           cont.counterTIB[i]++;
0413           break;
0414         }
0415         case StripSubdetector::TID: {
0416           int j = tTopo.tidWheel(detId) - 1;
0417           int side = tTopo.tidSide(detId);
0418           if (side == 2) {
0419             cont.counterTID[0][j]++;
0420           } else if (side == 1) {
0421             cont.counterTID[1][j]++;
0422           }
0423           break;
0424         }
0425         case StripSubdetector::TOB: {
0426           int i = tTopo.tobLayer(detId) - 1;
0427           cont.counterTOB[i]++;
0428           break;
0429         }
0430         case StripSubdetector::TEC: {
0431           int j = tTopo.tecWheel(detId) - 1;
0432           int side = tTopo.tecSide(detId);
0433           if (side == 2) {
0434             cont.counterTEC[0][j]++;
0435           } else if (side == 1) {
0436             cont.counterTEC[1][j]++;
0437           }
0438           break;
0439         }
0440       }
0441     }
0442   };
0443 
0444 }  // namespace
0445 
0446 PAYLOAD_INSPECTOR_MODULE(SiStripFedCabling) {
0447   PAYLOAD_INSPECTOR_CLASS(SiStripFedCabling_TrackerMap);
0448   PAYLOAD_INSPECTOR_CLASS(SiStripUncabledChannels_TrackerMap);
0449   PAYLOAD_INSPECTOR_CLASS(SiStripFedCablingComparisonTrackerMapSingleTag);
0450   PAYLOAD_INSPECTOR_CLASS(SiStripFedCablingComparisonTrackerMapTwoTags);
0451   PAYLOAD_INSPECTOR_CLASS(SiStripFedCabling_Summary);
0452 }