Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /*!
0002   \file SiPixelQualityProbabilities_PayloadInspector
0003   \Payload Inspector Plugin for SiPixelQualityProbabilities
0004   \author M. Musich
0005   \version $Revision: 1.0 $
0006   \date $Date: 2019/10/22 19:16:00 $
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 #include "CondCore/SiPixelPlugins/interface/SiPixelPayloadInspectorHelper.h"
0015 #include "FWCore/ParameterSet/interface/FileInPath.h"
0016 #include "CalibTracker/StandaloneTrackerTopology/interface/StandaloneTrackerTopology.h"
0017 
0018 // the data format of the condition to be inspected
0019 #include "CondFormats/SiPixelObjects/interface/SiPixelQualityProbabilities.h"
0020 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
0021 #include "DataFormats/DetId/interface/DetId.h"
0022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0023 
0024 #include <memory>
0025 #include <sstream>
0026 #include <iostream>
0027 #include <fmt/printf.h>
0028 
0029 // include ROOT
0030 #include "TH2F.h"
0031 #include "TLegend.h"
0032 #include "TCanvas.h"
0033 #include "TLine.h"
0034 #include "TGraph.h"
0035 #include "TGaxis.h"
0036 #include "TStyle.h"
0037 #include "TLatex.h"
0038 #include "TPave.h"
0039 #include "TPaveStats.h"
0040 
0041 namespace {
0042 
0043   using namespace cond::payloadInspector;
0044 
0045   /************************************************
0046   1d histogram of n. scenarios per PU bin in 1 IOV of SiPixelQualityProbabilities
0047   *************************************************/
0048 
0049   class SiPixelQualityProbabilitiesScenariosCount : public PlotImage<SiPixelQualityProbabilities, SINGLE_IOV> {
0050   public:
0051     SiPixelQualityProbabilitiesScenariosCount()
0052         : PlotImage<SiPixelQualityProbabilities, SINGLE_IOV>("SiPixelQualityProbabilities scenarios count") {}
0053 
0054     bool fill() override {
0055       auto tag = PlotBase::getTag<0>();
0056       auto iov = tag.iovs.front();
0057       auto tagname = tag.name;
0058       std::shared_ptr<SiPixelQualityProbabilities> payload = fetchPayload(std::get<1>(iov));
0059       auto PUbins = payload->getPileUpBins();
0060       auto span = PUbins.back() - PUbins.front();
0061 
0062       TGaxis::SetMaxDigits(3);
0063 
0064       TCanvas canvas("Canv", "Canv", 1200, 1000);
0065       canvas.cd();
0066       auto h1 = std::make_unique<TH1F>("Count",
0067                                        "SiPixelQualityProbablities Scenarios count;PU bin;n. of scenarios per PU bin",
0068                                        span,
0069                                        PUbins.front(),
0070                                        PUbins.back());
0071       h1->SetStats(false);
0072 
0073       canvas.SetTopMargin(0.06);
0074       canvas.SetBottomMargin(0.12);
0075       canvas.SetLeftMargin(0.13);
0076       canvas.SetRightMargin(0.05);
0077       canvas.Modified();
0078 
0079       for (const auto& bin : PUbins) {
0080         h1->SetBinContent(bin + 1, payload->nelements(bin));
0081       }
0082 
0083       h1->SetTitle("");
0084       h1->GetYaxis()->SetRangeUser(0., h1->GetMaximum() * 1.30);
0085       h1->SetFillColor(kRed);
0086       h1->SetMarkerStyle(20);
0087       h1->SetMarkerSize(1);
0088       h1->Draw("bar2");
0089 
0090       SiPixelPI::makeNicePlotStyle(h1.get());
0091 
0092       canvas.Update();
0093 
0094       TLegend legend = TLegend(0.40, 0.88, 0.94, 0.93);
0095       legend.SetHeader(("Payload hash: #bf{" + (std::get<1>(iov)) + "}").c_str(),
0096                        "C");    // option "C" allows to center the header
0097       legend.SetBorderSize(0);  // Set the border size to zero to remove visible borders
0098       legend.SetTextSize(0.025);
0099       legend.Draw("same");
0100 
0101       auto ltx = TLatex();
0102       ltx.SetTextFont(62);
0103       //ltx.SetTextColor(kBlue);
0104       ltx.SetTextSize(0.037);
0105       ltx.SetTextAlign(11);
0106 
0107       const auto& headerText =
0108           fmt::sprintf("#color[4]{%s},IOV: #color[4]{%s}", tagname, std::to_string(std::get<0>(iov)));
0109 
0110       ltx.DrawLatexNDC(gPad->GetLeftMargin() + 0.1, 1 - gPad->GetTopMargin() + 0.01, headerText.c_str());
0111 
0112       std::string fileName(m_imageFileName);
0113       canvas.SaveAs(fileName.c_str());
0114 
0115       return true;
0116     }
0117   };
0118 
0119   /************************************************
0120   Probability density per PU bin of 1 IOV of SiPixelQualityProbabilities
0121   *************************************************/
0122 
0123   class SiPixelQualityProbabilityDensityPerPUbin : public PlotImage<SiPixelQualityProbabilities, SINGLE_IOV> {
0124   public:
0125     SiPixelQualityProbabilityDensityPerPUbin() : PlotImage<SiPixelQualityProbabilities, SINGLE_IOV>("") {
0126       PlotBase::addInputParam("PU bin");
0127     }
0128 
0129     bool fill() override {
0130       auto tag = PlotBase::getTag<0>();
0131       auto iov = tag.iovs.front();
0132       auto tagname = tag.name;
0133       std::shared_ptr<SiPixelQualityProbabilities> payload = fetchPayload(std::get<1>(iov));
0134       auto PUbins = payload->getPileUpBins();
0135 
0136       // initialize the PUbin
0137       unsigned int PUbin(0);
0138       auto paramValues = PlotBase::inputParamValues();
0139       auto ip = paramValues.find("PU bin");
0140       if (ip != paramValues.end()) {
0141         PUbin = std::stoul(ip->second);
0142       } else {
0143         edm::LogWarning("SiPixelQualityProbabilityDensityPerPUbin")
0144             << "\n WARNING!!!! \n The needed parameter 'PU bin' has not been passed. Will use all PU bins! \n";
0145         PUbin = k_ALLPUBINS;
0146       }
0147 
0148       // graphics
0149       TGaxis::SetMaxDigits(3);
0150 
0151       SiPixelQualityProbabilities::probabilityVec probVec;
0152       if (PUbin != k_ALLPUBINS) {
0153         probVec = payload->getProbabilities(PUbin);
0154       } else {
0155         if (PUbins.front() == 0) {
0156           // if a PU bin = 0 exist the PU-averaged is in bin=0
0157           probVec = payload->getProbabilities(0);
0158         } else {
0159           // we need to build the PDF by hand
0160           // create a list of the probabilities for all the PU bins
0161           std::vector<SiPixelQualityProbabilities::probabilityVec> listOfProbabilityVec;
0162           for (unsigned int bin = PUbins.front(); bin <= PUbins.back(); bin++) {
0163             const auto& probsForBin = payload->getProbabilities(bin);
0164             listOfProbabilityVec.push_back(probsForBin);
0165           }
0166 
0167           // Map to store string and pair of floats (sum and count)
0168           std::map<std::string, std::pair<float, int>> stringFloatMap;
0169 
0170           // Loop through the list of probabilityVec elements
0171           for (const auto& vec : listOfProbabilityVec) {
0172             // For each pair in the current vector
0173             for (const auto& pair : vec) {
0174               const std::string& currentScen = pair.first;
0175               const float& currentProb = pair.second;
0176 
0177               // Check if the string exists in the map
0178               auto it = stringFloatMap.find(currentScen);
0179               if (it != stringFloatMap.end()) {
0180                 // If the scenario already exists, update the probability sum and count
0181                 it->second.first += currentProb;
0182                 it->second.second++;
0183               } else {
0184                 // If the string doesn't exist, add it to the map
0185                 stringFloatMap[currentScen] = {currentProb, 1};  // Initialize sum and count
0186               }
0187             }
0188           }
0189 
0190           // Calculate the average and populate the new probabilityVec from the map
0191           for (const auto& pair : stringFloatMap) {
0192             float average = pair.second.first / pair.second.second;  // Calculate average
0193             probVec.emplace_back(pair.first, average);
0194           }
0195         }  // if the first PU bin is not 0
0196       }    // if we're asking for all the PU bins
0197 
0198       TCanvas canvas("Canv", "Canv", 1200, 1000);
0199       canvas.cd();
0200       auto h1 = std::make_unique<TH1F>("SiPixelQuality PDF",
0201                                        "probability density vs scenario; scenario serial ID number; probability",
0202                                        probVec.size(),
0203                                        -0.5,
0204                                        probVec.size() - 0.5);
0205       h1->SetStats(false);
0206 
0207       canvas.SetTopMargin(0.06);
0208       canvas.SetBottomMargin(0.12);
0209       canvas.SetLeftMargin(0.13);
0210       canvas.SetRightMargin(0.09);
0211       canvas.Modified();
0212 
0213       unsigned int count{0};
0214       for (const auto& [name, prob] : probVec) {
0215         h1->SetBinContent(count, prob);
0216         count++;
0217       }
0218 
0219       h1->SetTitle("");
0220       h1->GetYaxis()->SetRangeUser(0., h1->GetMaximum() * 1.30);
0221       h1->SetFillColor(kRed);
0222       h1->SetMarkerStyle(20);
0223       h1->SetMarkerSize(1);
0224       h1->Draw("bar2");
0225 
0226       SiPixelPI::makeNicePlotStyle(h1.get());
0227 
0228       canvas.Update();
0229 
0230       TLegend legend = TLegend(0.39, 0.88, 0.89, 0.93);
0231       std::string puBinString = (PUbin == k_ALLPUBINS) ? "PU bin: #bf{all}" : fmt::sprintf("PU bin: #bf{%u}", PUbin);
0232       legend.SetHeader(("#splitline{Payload hash: #bf{" + (std::get<1>(iov)) + "}}{" + puBinString + "}").c_str(),
0233                        "C");    // option "C" allows to center the header
0234       legend.SetBorderSize(0);  // Set the border size to zero to remove visible borders
0235       legend.SetTextSize(0.025);
0236       legend.Draw("same");
0237 
0238       auto ltx = TLatex();
0239       ltx.SetTextFont(62);
0240       ltx.SetTextSize(0.03);
0241       ltx.SetTextAlign(11);
0242 
0243       const auto& headerText =
0244           fmt::sprintf("#color[4]{%s}, IOV: #color[4]{%s}", tagname, std::to_string(std::get<0>(iov)));
0245 
0246       ltx.DrawLatexNDC(gPad->GetLeftMargin() + 0.1, 1 - gPad->GetTopMargin() + 0.01, headerText.c_str());
0247 
0248       std::string fileName(m_imageFileName);
0249       canvas.SaveAs(fileName.c_str());
0250 
0251       return true;
0252     }
0253 
0254   private:
0255     static constexpr unsigned int k_ALLPUBINS = 9999;
0256   };
0257 
0258 }  // namespace
0259 
0260 // Register the classes as boost python plugin
0261 PAYLOAD_INSPECTOR_MODULE(SiPixelQualityProbabilities) {
0262   PAYLOAD_INSPECTOR_CLASS(SiPixelQualityProbabilitiesScenariosCount);
0263   PAYLOAD_INSPECTOR_CLASS(SiPixelQualityProbabilityDensityPerPUbin);
0264 }