Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef CONDCORE_SIPIXELPLUGINS_SIPIXELTEMPLATEHELPER_H
0002 #define CONDCORE_SIPIXELPLUGINS_SIPIXELTEMPLATEHELPER_H
0003 
0004 #include "CondCore/Utilities/interface/PayloadInspectorModule.h"
0005 #include "CondCore/Utilities/interface/PayloadInspector.h"
0006 #include "CondCore/CondDB/interface/Time.h"
0007 #include "CondCore/SiPixelPlugins/interface/SiPixelPayloadInspectorHelper.h"
0008 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
0009 #include "CondFormats/SiPixelObjects/interface/SiPixelTemplateDBObject.h"
0010 #include "CondFormats/SiPixelObjects/interface/SiPixelGenErrorDBObject.h"
0011 #include "CondFormats/SiPixelTransient/interface/SiPixelTemplate.h"
0012 #include "DQM/TrackerRemapper/interface/Phase1PixelMaps.h"
0013 #include "DQM/TrackerRemapper/interface/Phase1PixelSummaryMap.h"
0014 
0015 #include <type_traits>
0016 #include <memory>
0017 #include <sstream>
0018 #include <fmt/printf.h>
0019 #include <boost/range/adaptor/indexed.hpp>
0020 
0021 // include ROOT
0022 #include "TH2F.h"
0023 #include "TH1F.h"
0024 #include "TLegend.h"
0025 #include "TCanvas.h"
0026 #include "TLine.h"
0027 #include "TStyle.h"
0028 #include "TLatex.h"
0029 #include "TPave.h"
0030 #include "TPaveStats.h"
0031 #include "TGaxis.h"
0032 
0033 namespace templateHelper {
0034 
0035   //************************************************
0036   // Display of Template/GenError Titles
0037   // *************************************************/
0038   template <class PayloadType, class StoreType, class TransientType>
0039   class SiPixelTitles_Display
0040       : public cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV> {
0041   public:
0042     SiPixelTitles_Display()
0043         : cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV>(
0044               "Table of SiPixelTemplate/GenError titles") {
0045       if constexpr (std::is_same_v<PayloadType, SiPixelGenErrorDBObject>) {
0046         isTemplate_ = false;
0047         label_ = "SiPixelGenErrorDBObject_PayloadInspector";
0048       } else {
0049         isTemplate_ = true;
0050         label_ = "SiPixelTemplateDBObject_PayloadInspector";
0051       }
0052     }
0053 
0054     bool fill() override {
0055       auto tag = cond::payloadInspector::PlotBase::getTag<0>();
0056       auto iov = tag.iovs.front();
0057       auto tagname = tag.name;
0058       std::vector<StoreType> thePixelTemp_;
0059       std::shared_ptr<PayloadType> payload = this->fetchPayload(std::get<1>(iov));
0060 
0061       std::string IOVsince = std::to_string(std::get<0>(iov));
0062 
0063       if (payload.get()) {
0064         if (!TransientType::pushfile(*payload, thePixelTemp_)) {
0065           throw cms::Exception(label_) << "\nERROR:" << (isTemplate_ ? "Templates" : "GenErrors")
0066                                        << " not filled correctly."
0067                                        << " Check the conditions. Using "
0068                                        << (isTemplate_ ? "SiPixelTemplateDBObject" : "SiPixelGenErrorDBObject")
0069                                        << " version " << payload->version() << "\n\n";
0070         }
0071 
0072         unsigned int mapsize = thePixelTemp_.size();
0073         float pitch = 1. / (mapsize * 1.1);
0074 
0075         float y, x1, x2;
0076         std::vector<float> y_x1, y_x2, y_line;
0077         std::vector<std::string> s_x1, s_x2, s_x3;
0078 
0079         // starting table at y=1.0 (top of the canvas)
0080         // first column is at 0.02, second column at 0.32 NDC
0081         y = 1.0;
0082         x1 = 0.02;
0083         x2 = x1 + 0.30;
0084 
0085         y -= pitch;
0086         y_x1.push_back(y);
0087         s_x1.push_back(Form("#scale[1.2]{%s}", (isTemplate_ ? "Template ID" : "GenError ID")));
0088         y_x2.push_back(y);
0089         s_x2.push_back(Form("#scale[1.2]{#color[4]{%s} in IOV: #color[4]{%s}}", tagname.c_str(), IOVsince.c_str()));
0090 
0091         y -= pitch / 2.;
0092         y_line.push_back(y);
0093 
0094         for (const auto& element : thePixelTemp_) {
0095           y -= pitch;
0096           y_x1.push_back(y);
0097           s_x1.push_back(std::to_string(element.head.ID));
0098 
0099           y_x2.push_back(y);
0100           s_x2.push_back(Form("#color[2]{%s}", element.head.title));
0101 
0102           y_line.push_back(y - (pitch / 2.));
0103         }
0104 
0105         const auto& c_title = fmt::sprintf("%s titles", (isTemplate_ ? "Template" : "GenError"));
0106         TCanvas canvas(c_title.c_str(), c_title.c_str(), 2000, std::max(y_x1.size(), y_x2.size()) * 40);
0107         TLatex l;
0108         // Draw the columns titles
0109         l.SetTextAlign(12);
0110 
0111         float newpitch = 1 / (std::max(y_x1.size(), y_x2.size()) * 1.1);
0112         float factor = newpitch / pitch;
0113         l.SetTextSize(newpitch - 0.002);
0114         canvas.cd();
0115         for (unsigned int i = 0; i < y_x1.size(); i++) {
0116           l.DrawLatexNDC(x1, 1 - (1 - y_x1[i]) * factor, s_x1[i].c_str());
0117         }
0118 
0119         for (unsigned int i = 0; i < y_x2.size(); i++) {
0120           l.DrawLatexNDC(x2, 1 - (1 - y_x2[i]) * factor, s_x2[i].c_str());
0121         }
0122 
0123         canvas.cd();
0124         canvas.Update();
0125 
0126         TLine lines[y_line.size()];
0127         unsigned int iL = 0;
0128         for (const auto& line : y_line) {
0129           lines[iL] = TLine(gPad->GetUxmin(), 1 - (1 - line) * factor, gPad->GetUxmax(), 1 - (1 - line) * factor);
0130           lines[iL].SetLineWidth(1);
0131           lines[iL].SetLineStyle(9);
0132           lines[iL].SetLineColor(2);
0133           lines[iL].Draw("same");
0134           iL++;
0135         }
0136 
0137         std::string fileName(this->m_imageFileName);
0138         canvas.SaveAs(fileName.c_str());
0139 
0140       }  // if payload.get()
0141       return true;
0142     }
0143 
0144   protected:
0145     bool isTemplate_;
0146     std::string label_;
0147   };
0148 
0149   /************************************************
0150   // header plotting
0151   *************************************************/
0152   template <class PayloadType, class StoreType, class TransientType>
0153   class SiPixelHeaderTable : public cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV> {
0154   public:
0155     SiPixelHeaderTable()
0156         : cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV>(
0157               "SiPixel CPE Conditions Header summary") {
0158       if constexpr (std::is_same_v<PayloadType, SiPixelGenErrorDBObject>) {
0159         isTemplate_ = false;
0160         label_ = "SiPixelGenErrorDBObject_PayloadInspector";
0161       } else {
0162         isTemplate_ = true;
0163         label_ = "SiPixelTemplateDBObject_PayloadInspector";
0164       }
0165     }
0166 
0167     bool fill() override {
0168       gStyle->SetHistMinimumZero();  // will display zero as zero in the text map
0169       gStyle->SetPalette(kMint);     // for the ghost plot (colored BPix and FPix bins)
0170 
0171       auto tag = cond::payloadInspector::PlotBase::getTag<0>();
0172       auto iov = tag.iovs.front();
0173       auto tagname = tag.name;
0174       std::vector<StoreType> thePixelTemp_;
0175       std::shared_ptr<PayloadType> payload = this->fetchPayload(std::get<1>(iov));
0176 
0177       if (payload.get()) {
0178         if (!TransientType::pushfile(*payload, thePixelTemp_)) {
0179           throw cms::Exception(label_) << "\nERROR:" << (isTemplate_ ? "Templates" : "GenErrors")
0180                                        << " not filled correctly."
0181                                        << " Check the conditions. Using "
0182                                        << (isTemplate_ ? "SiPixelTemplateDBObject" : "SiPixelGenErrorDBObject")
0183                                        << payload->version() << "\n\n";
0184         }
0185 
0186         // store the map of ID / interesting quantities
0187         TransientType templ(thePixelTemp_);
0188         TCanvas canvas("Header Summary", "Header summary", 1400, 1000);
0189         canvas.cd();
0190 
0191         unsigned int tempSize = thePixelTemp_.size();
0192 
0193         canvas.SetTopMargin(0.07);
0194         canvas.SetBottomMargin(0.06);
0195         canvas.SetLeftMargin(0.17);
0196         canvas.SetRightMargin(0.03);
0197         canvas.Modified();
0198         canvas.SetGrid();
0199 
0200         auto h2_Header = std::make_unique<TH2F>("Header", ";;", tempSize, 0, tempSize, 12, 0., 12.);
0201         auto h2_ghost = std::make_unique<TH2F>("ghost", ";;", tempSize, 0, tempSize, 12, 0., 12.);
0202         h2_Header->SetStats(false);
0203         h2_ghost->SetStats(false);
0204 
0205         int tempVersion = -999;
0206 
0207         for (const auto& theTemp : thePixelTemp_ | boost::adaptors::indexed(1)) {
0208           auto tempValue = theTemp.value();
0209           auto idx = theTemp.index();
0210           float uH = -99.;
0211           if (tempValue.head.Bfield != 0.) {
0212             uH = roundoff(tempValue.head.lorxwidth / tempValue.head.zsize / tempValue.head.Bfield, 4);
0213           }
0214 
0215           // clang-format off
0216           h2_Header->SetBinContent(idx, 12, tempValue.head.ID);     //!< template ID number
0217           h2_Header->SetBinContent(idx, 11, tempValue.head.Bfield); //!< Bfield in Tesla
0218           h2_Header->SetBinContent(idx, 10, uH);                    //!< hall mobility
0219           h2_Header->SetBinContent(idx, 9, tempValue.head.xsize);   //!< pixel size (for future use in upgraded geometry)
0220           h2_Header->SetBinContent(idx, 8, tempValue.head.ysize);   //!< pixel size (for future use in upgraded geometry)
0221           h2_Header->SetBinContent(idx, 7, tempValue.head.zsize);   //!< pixel size (for future use in upgraded geometry)
0222           h2_Header->SetBinContent(idx, 6, tempValue.head.NTy);     //!< number of Template y entries
0223           h2_Header->SetBinContent(idx, 5, tempValue.head.NTyx);    //!< number of Template y-slices of x entries
0224           h2_Header->SetBinContent(idx, 4, tempValue.head.NTxx);    //!< number of Template x-entries in each slice
0225           h2_Header->SetBinContent(idx, 3, tempValue.head.Dtype);   //!< detector type (0=BPix, 1=FPix)
0226           h2_Header->SetBinContent(idx, 2, tempValue.head.qscale);  //!< Charge scaling to match cmssw and pixelav
0227           h2_Header->SetBinContent(idx, 1, tempValue.head.Vbias);   //!< detector bias potential in Volts
0228           // clang-format on
0229 
0230           h2_Header->GetYaxis()->SetBinLabel(12, (isTemplate_ ? "TemplateID" : "GenErrorID"));
0231           h2_Header->GetYaxis()->SetBinLabel(11, "B-field [T]");
0232           h2_Header->GetYaxis()->SetBinLabel(10, "#mu_{H} [1/T]");
0233           h2_Header->GetYaxis()->SetBinLabel(9, "x-size [#mum]");
0234           h2_Header->GetYaxis()->SetBinLabel(8, "y-size [#mum]");
0235           h2_Header->GetYaxis()->SetBinLabel(7, "z-size [#mum]");
0236           h2_Header->GetYaxis()->SetBinLabel(6, "NTy");
0237           h2_Header->GetYaxis()->SetBinLabel(5, "NTyx");
0238           h2_Header->GetYaxis()->SetBinLabel(4, "NTxx");
0239           h2_Header->GetYaxis()->SetBinLabel(3, "DetectorType");
0240           h2_Header->GetYaxis()->SetBinLabel(2, "qScale");
0241           h2_Header->GetYaxis()->SetBinLabel(1, "VBias [V]");
0242           h2_Header->GetXaxis()->SetBinLabel(idx, "");
0243 
0244           for (unsigned int iy = 1; iy <= 12; iy++) {
0245             // Some of the Phase-2 templates have DType = 0 for all partitions (TBPX, TEPX, TFPX)
0246             // so they are distinguished by the uH strength value (<0).
0247             // To avoid changing the behaviour of 0T payload (uH=-99) that case is treated separately
0248             if (tempValue.head.Dtype != 0 || (uH < 0 && uH > -99)) {
0249               h2_ghost->SetBinContent(idx, iy, 1);
0250             } else {
0251               h2_ghost->SetBinContent(idx, iy, -1);
0252             }
0253             h2_ghost->GetYaxis()->SetBinLabel(iy, h2_Header->GetYaxis()->GetBinLabel(iy));
0254             h2_ghost->GetXaxis()->SetBinLabel(idx, "");
0255           }
0256 
0257           if (tempValue.head.templ_version != tempVersion) {
0258             tempVersion = tempValue.head.templ_version;
0259           }
0260         }
0261 
0262         h2_Header->GetXaxis()->LabelsOption("h");
0263         h2_Header->GetXaxis()->SetNdivisions(500 + tempSize, false);
0264         h2_Header->GetYaxis()->SetLabelSize(0.05);
0265         h2_Header->SetMarkerSize(1.5);
0266 
0267         h2_ghost->GetXaxis()->LabelsOption("h");
0268         h2_ghost->GetXaxis()->SetNdivisions(500 + tempSize, false);
0269         h2_ghost->GetYaxis()->SetLabelSize(0.05);
0270 
0271         canvas.cd();
0272         h2_ghost->Draw("col");
0273         h2_Header->Draw("TEXTsame");
0274 
0275         TPaveText ksPt(0, 0, 0.88, 0.04, "NDC");
0276         ksPt.SetBorderSize(0);
0277         ksPt.SetFillColor(0);
0278         const char* textToAdd = Form("%s Version: #color[2]{%i}. Payload hash: #color[2]{%s}",
0279                                      (isTemplate_ ? "Template" : "GenError"),
0280                                      tempVersion,
0281                                      (std::get<1>(iov)).c_str());
0282         ksPt.AddText(textToAdd);
0283         ksPt.Draw();
0284 
0285         auto ltx = TLatex();
0286         ltx.SetTextFont(62);
0287         ltx.SetTextSize(0.040);
0288         ltx.SetTextAlign(11);
0289         ltx.DrawLatexNDC(
0290             gPad->GetLeftMargin(),
0291             1 - gPad->GetTopMargin() + 0.01,
0292             ("#color[4]{" + tagname + "}, IOV: #color[4]{" + std::to_string(std::get<0>(iov)) + "}").c_str());
0293 
0294         std::string fileName(this->m_imageFileName);
0295         canvas.SaveAs(fileName.c_str());
0296       }
0297       return true;
0298     }
0299 
0300     float roundoff(float value, unsigned char prec) {
0301       float pow_10 = pow(10.0f, (float)prec);
0302       return round(value * pow_10) / pow_10;
0303     }
0304 
0305   protected:
0306     bool isTemplate_;
0307     std::string label_;
0308   };
0309 
0310   //***********************************************
0311   // TH2Poly Map of IDs
0312   //***********************************************/
0313   template <class PayloadType, SiPixelPI::DetType myType>
0314   class SiPixelIDs : public cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV> {
0315   public:
0316     SiPixelIDs()
0317         : cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV>(
0318               "SiPixelMap of Template / GenError ID Values") {
0319       if constexpr (std::is_same_v<PayloadType, SiPixelGenErrorDBObject>) {
0320         isTemplate_ = false;
0321         label_ = "SiPixelGenErrorDBObject_PayloadInspector";
0322       } else {
0323         isTemplate_ = true;
0324         label_ = "SiPixelTemplateDBObject_PayloadInspector";
0325       }
0326     }
0327 
0328     bool fill() override {
0329       gStyle->SetPalette(kRainBow);
0330 
0331       auto tag = cond::payloadInspector::PlotBase::getTag<0>();
0332       auto iov = tag.iovs.front();
0333       std::shared_ptr<PayloadType> payload = this->fetchPayload(std::get<1>(iov));
0334 
0335       std::string barrelName_ = fmt::sprintf("%sIDsBarrel", (isTemplate_ ? "template" : "genError"));
0336       std::string endcapName_ = fmt::sprintf("%sIDsForward", (isTemplate_ ? "template" : "genError"));
0337       std::string title_ = fmt::sprintf("%s IDs", (isTemplate_ ? "template" : "genError"));
0338 
0339       if (payload.get()) {
0340         // Book the TH2Poly
0341         Phase1PixelMaps theMaps("text");
0342         if (myType == SiPixelPI::t_barrel) {
0343           // book the barrel bins of the TH2Poly
0344           theMaps.bookBarrelHistograms(barrelName_, title_.c_str(), title_.c_str());
0345         } else if (myType == SiPixelPI::t_forward) {
0346           // book the forward bins of the TH2Poly
0347           theMaps.bookForwardHistograms(endcapName_, title_.c_str(), title_.c_str());
0348         }
0349 
0350         std::map<unsigned int, short> templMap;
0351         if constexpr (std::is_same_v<PayloadType, SiPixelGenErrorDBObject>) {
0352           templMap = payload->getGenErrorIDs();
0353         } else {
0354           templMap = payload->getTemplateIDs();
0355         }
0356 
0357         if (templMap.size() == SiPixelPI::phase0size || templMap.size() > SiPixelPI::phase1size) {
0358           edm::LogError(label_)
0359               << "There are " << templMap.size()
0360               << " DetIds in this payload. SiPixelIDs maps are not supported for non-Phase1 Pixel geometries !";
0361           TCanvas canvas("Canv", "Canv", 1200, 1000);
0362           SiPixelPI::displayNotSupported(canvas, templMap.size());
0363           std::string fileName(this->m_imageFileName);
0364           canvas.SaveAs(fileName.c_str());
0365           return false;
0366         } else {
0367           if (templMap.size() < SiPixelPI::phase1size) {
0368             edm::LogWarning(label_) << "\n ********* WARNING! ********* \n There are " << templMap.size()
0369                                     << " DetIds in this payload !"
0370                                     << "\n **************************** \n";
0371           }
0372         }
0373 
0374         /*
0375     std::vector<unsigned int> detids;
0376     std::transform(templMap.begin(),
0377                templMap.end(),
0378                std::back_inserter(detids),
0379                [](const std::map<unsigned int, short>::value_type& pair) { return pair.first; });
0380     */
0381 
0382         for (auto const& entry : templMap) {
0383           COUT << "DetID: " << entry.first << fmt::sprintf("%s ID ", (isTemplate_ ? "Template" : "GenError"))
0384                << entry.second << std::endl;
0385           auto detid = DetId(entry.first);
0386           if ((detid.subdetId() == PixelSubdetector::PixelBarrel) && (myType == SiPixelPI::t_barrel)) {
0387             theMaps.fillBarrelBin(barrelName_, entry.first, entry.second);
0388           } else if ((detid.subdetId() == PixelSubdetector::PixelEndcap) && (myType == SiPixelPI::t_forward)) {
0389             theMaps.fillForwardBin(endcapName_, entry.first, entry.second);
0390           }
0391         }
0392 
0393         theMaps.beautifyAllHistograms();
0394 
0395         TCanvas canvas("Canv", "Canv", (myType == SiPixelPI::t_barrel) ? 1200 : 1500, 1000);
0396         if (myType == SiPixelPI::t_barrel) {
0397           theMaps.drawBarrelMaps(barrelName_, canvas);
0398         } else if (myType == SiPixelPI::t_forward) {
0399           theMaps.drawForwardMaps(endcapName_, canvas);
0400         }
0401 
0402         canvas.cd();
0403 
0404         std::string fileName(this->m_imageFileName);
0405         canvas.SaveAs(fileName.c_str());
0406       }
0407       return true;
0408     }
0409 
0410   protected:
0411     bool isTemplate_;
0412     std::string label_;
0413   };
0414 
0415   /************************************************
0416    Full Pixel Tracker Map class
0417   *************************************************/
0418   template <class PayloadType, class StoreType, class TransientType>
0419   class SiPixelFullPixelIDMap
0420       : public cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV> {
0421   public:
0422     SiPixelFullPixelIDMap()
0423         : cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV>(
0424               "SiPixel CPE conditions Map of IDs") {
0425       if constexpr (std::is_same_v<PayloadType, SiPixelGenErrorDBObject>) {
0426         isTemplate_ = false;
0427         label_ = "SiPixelGenErrorDBObject_PayloadInspector";
0428       } else {
0429         isTemplate_ = true;
0430         label_ = "SiPixelTemplateDBObject_PayloadInspector";
0431       }
0432     }
0433 
0434     bool fill() override {
0435       gStyle->SetPalette(1);
0436       auto tag = cond::payloadInspector::PlotBase::getTag<0>();
0437       auto iov = tag.iovs.front();
0438       std::vector<StoreType> thePixelTemp_;
0439       std::shared_ptr<PayloadType> payload = this->fetchPayload(std::get<1>(iov));
0440 
0441       std::string payloadString = (isTemplate_ ? "Templates" : "GenErrors");
0442 
0443       if (payload.get()) {
0444         if (!TransientType::pushfile(*payload, thePixelTemp_)) {
0445           throw cms::Exception(label_) << "\nERROR: " << payloadString
0446                                        << " not filled correctly. Check the conditions. Using "
0447                                        << (isTemplate_ ? "SiPixelTemplateDBObject" : "SiPixelGenErrorDBObject")
0448                                        << payload->version() << "\n\n";
0449         }
0450 
0451         Phase1PixelSummaryMap fullMap("", fmt::sprintf("%s IDs", payloadString), fmt::sprintf("%s ID", payloadString));
0452         fullMap.createTrackerBaseMap();
0453 
0454         std::map<unsigned int, short> templMap;
0455         if constexpr (std::is_same_v<PayloadType, SiPixelGenErrorDBObject>) {
0456           templMap = payload->getGenErrorIDs();
0457         } else {
0458           templMap = payload->getTemplateIDs();
0459         }
0460 
0461         for (const auto& entry : templMap) {
0462           fullMap.fillTrackerMap(entry.first, entry.second);
0463         }
0464 
0465         if (templMap.size() == SiPixelPI::phase0size || templMap.size() > SiPixelPI::phase1size) {
0466           edm::LogError(label_)
0467               << "There are " << templMap.size()
0468               << " DetIds in this payload. SiPixelIDs maps are not supported for non-Phase1 Pixel geometries !";
0469           TCanvas canvas("Canv", "Canv", 1200, 1000);
0470           SiPixelPI::displayNotSupported(canvas, templMap.size());
0471           std::string fileName(this->m_imageFileName);
0472           canvas.SaveAs(fileName.c_str());
0473           return false;
0474         } else {
0475           if (templMap.size() < SiPixelPI::phase1size) {
0476             edm::LogWarning(label_) << "\n ********* WARNING! ********* \n There are " << templMap.size()
0477                                     << " DetIds in this payload !"
0478                                     << "\n **************************** \n";
0479           }
0480         }
0481 
0482         TCanvas canvas("Canv", "Canv", 3000, 2000);
0483         fullMap.printTrackerMap(canvas);
0484 
0485         //fmt::sprintf("#color[2]{%s, IOV %i}",tag.name,std::get<0>(iov));
0486 
0487         auto ltx = TLatex();
0488         ltx.SetTextFont(62);
0489         ltx.SetTextSize(0.025);
0490         ltx.SetTextAlign(11);
0491         ltx.DrawLatexNDC(
0492             gPad->GetLeftMargin() + 0.01,
0493             gPad->GetBottomMargin() + 0.01,
0494             ("#color[4]{" + tag.name + "}, IOV: #color[4]{" + std::to_string(std::get<0>(iov)) + "}").c_str());
0495 
0496         std::string fileName(this->m_imageFileName);
0497         canvas.SaveAs(fileName.c_str());
0498       }
0499       return true;
0500     }
0501 
0502   protected:
0503     bool isTemplate_;
0504     std::string label_;
0505   };
0506 
0507   enum headerParam {
0508     k_ID = 0,
0509     k_NTy = 1,
0510     k_NTyx = 2,
0511     k_NTxx = 3,
0512     k_Dtype = 4,
0513     k_qscale = 5,
0514     k_lorywidth = 6,
0515     k_lorxwidth = 7,
0516     k_lorybias = 8,
0517     k_lorxbias = 9,
0518     k_Vbias = 10,
0519     k_temperature = 11,
0520     k_fluence = 12,
0521     k_s50 = 13,
0522     k_ss50 = 14,
0523     k_title = 15,
0524     k_templ_version = 16,
0525     k_Bfield = 17,
0526     k_fbin = 18,
0527     k_END_OF_TYPES = 19,
0528   };
0529 
0530   static constexpr const char* header_types[] = {"ID;templated ID",
0531                                                  "NTy;number of template y entries",
0532                                                  "NTyx;number of template y-slices of x entries",
0533                                                  "NTxx;number of template x-entries in each slice",
0534                                                  "Dtype;detector type (0=BPix, 1=FPix)",
0535                                                  "qScale;charge scaling correction",
0536                                                  "lorxwidth;estimate of the y-Lorentz width",
0537                                                  "lorywidth;estimate of the x-Lorentz width",
0538                                                  "lorybias;estimate of the y-Lorentz bias",
0539                                                  "lorxbias;estimate of the x-Lorentz bias",
0540                                                  "Vbias;detector bias [V]",
0541                                                  "temperature;detector temperature [K]",
0542                                                  "fluence;radiation fluence [n_{eq}/cm^{2}] ",
0543                                                  "s50;1/2 of the multihit dcol threshold [e]",
0544                                                  "ss50;1/2 of the single hit dcol threshold [e]",
0545                                                  "title;title",
0546                                                  "template version;template version number",
0547                                                  "B-field;B-field [T]",
0548                                                  "fbin;Qbin in Q_{clus}/Q_{avg}",
0549                                                  "NOT HERE;NOT HERE"};
0550 
0551   // class to display values of the template header information in a Phase1 Pixel Map
0552   template <class PayloadType, class StoreType, class TransientType, SiPixelPI::DetType myType, headerParam myParam>
0553   class SiPixelTemplateHeaderInfo
0554       : public cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV> {
0555     struct header_info {
0556       int ID;             //!< template ID number
0557       int NTy;            //!< number of Template y entries
0558       int NTyx;           //!< number of Template y-slices of x entries
0559       int NTxx;           //!< number of Template x-entries in each slice
0560       int Dtype;          //!< detector type (0=BPix, 1=FPix)
0561       float qscale;       //!< Charge scaling to match cmssw and pixelav
0562       float lorywidth;    //!< estimate of y-lorentz width for optimal resolution
0563       float lorxwidth;    //!< estimate of x-lorentz width for optimal resolution
0564       float lorybias;     //!< estimate of y-lorentz bias
0565       float lorxbias;     //!< estimate of x-lorentz bias
0566       float Vbias;        //!< detector bias potential in Volts
0567       float temperature;  //!< detector temperature in deg K
0568       float fluence;      //!< radiation fluence in n_eq/cm^2
0569       float s50;          //!< 1/2 of the multihit dcol threshold in electrons
0570       float ss50;         //!< 1/2 of the single hit dcol threshold in electrons
0571       char title[80];     //!< template title
0572       int templ_version;  //!< Version number of the template to ensure code compatibility
0573       float Bfield;       //!< Bfield in Tesla
0574       float fbin[3];      //!< The QBin definitions in Q_clus/Q_avg
0575     };
0576 
0577   public:
0578     SiPixelTemplateHeaderInfo()
0579         : cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV>(
0580               "SiPixel CPE conditions Map of header quantities") {
0581       if constexpr (std::is_same_v<PayloadType, SiPixelGenErrorDBObject>) {
0582         isTemplate_ = false;
0583         label_ = "SiPixelGenErrorDBObject_PayloadInspector";
0584       } else {
0585         isTemplate_ = true;
0586         label_ = "SiPixelTemplateDBObject_PayloadInspector";
0587       }
0588     }
0589 
0590     bool fill() override {
0591       gStyle->SetPalette(kRainBow);
0592       if (!(myParam == headerParam::k_Vbias || myParam == headerParam::k_Dtype)) {
0593         TGaxis::SetMaxDigits(2);
0594       }
0595 
0596       auto tag = cond::payloadInspector::PlotBase::getTag<0>();
0597       auto iov = tag.iovs.front();
0598       const auto& tagname = tag.name;
0599       std::vector<StoreType> thePixelTemp_;
0600       std::shared_ptr<PayloadType> payload = this->fetchPayload(std::get<1>(iov));
0601 
0602       if (payload.get()) {
0603         if (!TransientType::pushfile(*payload, thePixelTemp_)) {
0604           throw cms::Exception(label_) << "\nERROR: Templates not filled correctly. Check the conditions. Using "
0605                                           "payload version "
0606                                        << payload->version() << "\n\n";
0607         }
0608 
0609         // store the map of ID / interesting quantities
0610         SiPixelTemplate templ(thePixelTemp_);
0611         for (const auto& theTemp : thePixelTemp_) {
0612           header_info info;
0613           info.ID = theTemp.head.ID;
0614           info.NTy = theTemp.head.NTy;
0615           info.NTyx = theTemp.head.NTyx;
0616           info.NTxx = theTemp.head.NTxx;
0617           info.Dtype = theTemp.head.Dtype;
0618           info.qscale = theTemp.head.qscale;
0619           info.lorywidth = theTemp.head.lorywidth;
0620           info.lorxwidth = theTemp.head.lorxwidth;
0621           info.lorybias = theTemp.head.lorybias;
0622           info.lorxbias = theTemp.head.lorxbias;
0623           info.Vbias = theTemp.head.Vbias;
0624           info.temperature = theTemp.head.temperature;
0625           info.fluence = theTemp.head.fluence;
0626           info.s50 = theTemp.head.s50;
0627           info.ss50 = theTemp.head.ss50;
0628           info.templ_version = theTemp.head.templ_version;
0629           info.Bfield = theTemp.head.Bfield;
0630           theInfos_[theTemp.head.ID] = info;
0631         }
0632 
0633         // Book the TH2Poly
0634         Phase1PixelMaps theMaps("");
0635         if (myType == SiPixelPI::t_all) {
0636           theMaps.resetOption("COLZA L");
0637         } else {
0638           theMaps.resetOption("COLZL");
0639         }
0640 
0641         std::string input{header_types[myParam]};
0642         std::string delimiter = ";";
0643         std::string first = input.substr(0, input.find(delimiter));
0644         std::string second = input.substr(input.find(delimiter) + 1);
0645 
0646         if (myType == SiPixelPI::t_barrel) {
0647           theMaps.bookBarrelHistograms("templateLABarrel", first.c_str(), second.c_str());
0648         } else if (myType == SiPixelPI::t_forward) {
0649           theMaps.bookForwardHistograms("templateLAForward", first.c_str(), second.c_str());
0650         } else if (myType == SiPixelPI::t_all) {
0651           theMaps.bookBarrelHistograms("templateLA", first.c_str(), second.c_str());
0652           theMaps.bookForwardHistograms("templateLA", first.c_str(), second.c_str());
0653         } else {
0654           edm::LogError(label_) << " un-recognized detector type " << myType << std::endl;
0655           return false;
0656         }
0657 
0658         std::map<unsigned int, short> templMap = payload->getTemplateIDs();
0659         if (templMap.size() == SiPixelPI::phase0size || templMap.size() > SiPixelPI::phase1size) {
0660           edm::LogError(label_)
0661               << "There are " << templMap.size()
0662               << " DetIds in this payload. SiPixelTempate Lorentz Angle maps are not supported for non-Phase1 Pixel "
0663                  "geometries !";
0664           TCanvas canvas("Canv", "Canv", 1200, 1000);
0665           SiPixelPI::displayNotSupported(canvas, templMap.size());
0666           std::string fileName(this->m_imageFileName);
0667           canvas.SaveAs(fileName.c_str());
0668           return false;
0669         } else {
0670           if (templMap.size() < SiPixelPI::phase1size) {
0671             edm::LogWarning(label_) << "\n ********* WARNING! ********* \n There are " << templMap.size()
0672                                     << " DetIds in this payload !"
0673                                     << "\n **************************** \n";
0674           }
0675         }
0676 
0677         for (auto const& entry : templMap) {
0678           //templ.interpolate(entry.second, 0.f, 0.f, 1.f, 1.f);
0679 
0680           const auto& theInfo = theInfos_[entry.second];
0681 
0682           std::function<float(headerParam, header_info)> cutFunctor = [](headerParam my_param, header_info myInfo) {
0683             float ret(-999.);
0684             switch (my_param) {
0685               case k_ID:
0686                 return (float)myInfo.ID;
0687               case k_NTy:
0688                 return (float)myInfo.NTy;
0689               case k_NTyx:
0690                 return (float)myInfo.NTyx;
0691               case k_NTxx:
0692                 return (float)myInfo.NTxx;
0693               case k_Dtype:
0694                 return (float)myInfo.Dtype;
0695               case k_qscale:
0696                 return (float)myInfo.qscale;
0697               case k_lorywidth:
0698                 return (float)myInfo.lorywidth;
0699               case k_lorxwidth:
0700                 return (float)myInfo.lorxwidth;
0701               case k_lorybias:
0702                 return (float)myInfo.lorybias;
0703               case k_lorxbias:
0704                 return (float)myInfo.lorxbias;
0705               case k_Vbias:
0706                 return (float)myInfo.Vbias;
0707               case k_temperature:
0708                 return (float)myInfo.temperature;
0709               case k_fluence:
0710                 return (float)myInfo.fluence;
0711               case k_s50:
0712                 return (float)myInfo.s50;
0713               case k_ss50:
0714                 return (float)myInfo.ss50;
0715               case k_title:
0716                 return (float)myInfo.templ_version;
0717               case k_Bfield:
0718                 return (float)myInfo.Bfield;
0719               case k_END_OF_TYPES:
0720                 return ret;
0721               default:
0722                 return ret;
0723             }
0724           };
0725 
0726           auto detid = DetId(entry.first);
0727           if (myType == SiPixelPI::t_all) {
0728             if ((detid.subdetId() == PixelSubdetector::PixelBarrel)) {
0729               theMaps.fillBarrelBin("templateLA", entry.first, cutFunctor(myParam, theInfo));
0730             }
0731             if ((detid.subdetId() == PixelSubdetector::PixelEndcap)) {
0732               theMaps.fillForwardBin("templateLA", entry.first, cutFunctor(myParam, theInfo));
0733             }
0734           } else if ((detid.subdetId() == PixelSubdetector::PixelBarrel) && (myType == SiPixelPI::t_barrel)) {
0735             theMaps.fillBarrelBin("templateLABarrel", entry.first, cutFunctor(myParam, theInfo));
0736           } else if ((detid.subdetId() == PixelSubdetector::PixelEndcap) && (myType == SiPixelPI::t_forward)) {
0737             theMaps.fillForwardBin("templateLAForward", entry.first, cutFunctor(myParam, theInfo));
0738           }
0739         }
0740 
0741         theMaps.beautifyAllHistograms();
0742 
0743         TCanvas canvas("Canv", "Canv", (myType == SiPixelPI::t_barrel) ? 1200 : 1600, 1000);
0744         if (myType == SiPixelPI::t_barrel) {
0745           theMaps.drawBarrelMaps("templateLABarrel", canvas);
0746         } else if (myType == SiPixelPI::t_forward) {
0747           theMaps.drawForwardMaps("templateLAForward", canvas);
0748         } else if (myType == SiPixelPI::t_all) {
0749           theMaps.drawSummaryMaps("templateLA", canvas);
0750         }
0751 
0752         canvas.cd();
0753         TPaveText ksPt(0, 0, 0.88, 0.04, "NDC");
0754         ksPt.SetBorderSize(0);
0755         ksPt.SetFillColor(0);
0756         const char* textToAdd = Form("%s Tag: #color[2]{%s}, IOV: #color[2]{%s}. Payload hash: #color[2]{%s}",
0757                                      (isTemplate_ ? "Template" : "GenError"),
0758                                      tagname.c_str(),
0759                                      std::to_string(std::get<0>(iov)).c_str(),
0760                                      (std::get<1>(iov)).c_str());
0761         ksPt.AddText(textToAdd);
0762         ksPt.Draw();
0763 
0764         std::string fileName(this->m_imageFileName);
0765         canvas.SaveAs(fileName.c_str());
0766       }
0767       return true;
0768     }  // fill
0769 
0770   protected:
0771     bool isTemplate_;
0772     std::string label_;
0773 
0774   private:
0775     std::map<int, header_info> theInfos_;
0776   };
0777 
0778 }  // namespace templateHelper
0779 
0780 #endif