Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-12-17 02:41:00

0001 #include "CondCore/CondDB/interface/Time.h"
0002 #include "CondCore/Utilities/interface/PayloadInspector.h"
0003 #include "CondCore/Utilities/interface/PayloadInspectorModule.h"
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 
0006 // the data format of the condition to be inspected
0007 #include "CondFormats/PhysicsToolsObjects/interface/DeDxCalibration.h"
0008 
0009 #include <array>
0010 #include <fstream>
0011 #include <iomanip>
0012 #include <iostream>
0013 #include <map>
0014 #include <memory>
0015 #include <numeric>
0016 #include <sstream>
0017 
0018 // include ROOT
0019 #include "TCanvas.h"
0020 #include "TF1.h"
0021 #include "TH1F.h"
0022 #include "TH2F.h"
0023 #include "TLatex.h"
0024 #include "TLegend.h"
0025 #include "TLine.h"
0026 #include "TPad.h"
0027 #include "TPave.h"
0028 #include "TPaveStats.h"
0029 #include "TStyle.h"
0030 
0031 namespace {
0032 
0033   using namespace cond::payloadInspector;
0034 
0035   /************************************************
0036      DeDxCalibration Payload Inspector of 1 IOV 
0037   *************************************************/
0038   class DeDxCalibrationTest : public Histogram1D<DeDxCalibration, SINGLE_IOV> {
0039   public:
0040     DeDxCalibrationTest()
0041         : Histogram1D<DeDxCalibration, SINGLE_IOV>("Test DeDxCalibration", "Test DeDxCalibration", 1, 0.0, 1.0) {}
0042 
0043     bool fill() override {
0044       auto tag = PlotBase::getTag<0>();
0045       for (auto const& iov : tag.iovs) {
0046         std::shared_ptr<DeDxCalibration> payload = Base::fetchPayload(std::get<1>(iov));
0047         if (payload.get()) {
0048           const auto& thresholds = payload->thr();
0049           const auto& alphas = payload->alpha();
0050           const auto& sigmas = payload->sigma();
0051 
0052           assert(thresholds.size() == alphas.size());
0053           assert(alphas.size() == sigmas.size());
0054 
0055           for (unsigned int i = 0; i < thresholds.size(); i++) {
0056             std::cout << "threshold:" << thresholds[i] << " alpha: " << alphas[i] << " sigma: " << sigmas[i]
0057                       << std::endl;
0058           }
0059         }
0060       }
0061       return true;
0062     }
0063   };
0064 
0065   // Inspector to show the values of thresholds, alphas, sigmas, and gains in DeDxCalibration
0066   class DeDxCalibrationInspector : public PlotImage<DeDxCalibration, SINGLE_IOV> {
0067   public:
0068     DeDxCalibrationInspector() : PlotImage<DeDxCalibration, SINGLE_IOV>("DeDxCalibration Inspector") {}
0069 
0070     bool fill() override {
0071       gStyle->SetPalette(1);
0072       gStyle->SetOptStat(0);
0073 
0074       auto tag = PlotBase::getTag<0>();
0075       auto iov = tag.iovs.front();
0076 
0077       std::shared_ptr<DeDxCalibration> payload = fetchPayload(std::get<1>(iov));
0078 
0079       if (payload.get()) {
0080         const std::vector<double>& thr = payload->thr();
0081         const std::vector<double>& alpha = payload->alpha();
0082         const std::vector<double>& sigma = payload->sigma();
0083 
0084         // Ensure that thr, alpha, sigma have the same size
0085         assert(thr.size() == alpha.size());
0086         assert(alpha.size() == sigma.size());
0087 
0088         // Create a 2D histogram
0089         int nBinsX = 3;           // For thr, alpha, sigma, gain
0090         int nBinsY = thr.size();  // Number of elements in the vectors
0091 
0092         TH2D h2("h2", "DeDxCalibration Values;Variable Type;Value", nBinsX, 0, nBinsX, nBinsY, 0, nBinsY);
0093 
0094         // Label the x-axis with the variable names
0095         h2.GetXaxis()->SetBinLabel(1, "Threshold");
0096         h2.GetXaxis()->SetBinLabel(2, "#alpha");
0097         h2.GetXaxis()->SetBinLabel(3, "#sigma");
0098 
0099         // Fill the histogram
0100         for (size_t i = 0; i < thr.size(); ++i) {
0101           h2.Fill(0.5, i, thr[i]);
0102           h2.Fill(1.5, i, alpha[i]);
0103           h2.Fill(2.5, i, sigma[i]);
0104         }
0105 
0106         // Draw the histogram on a canvas
0107         TCanvas canvas("Canvas", "DeDxCalibration Values", 1200, 800);
0108         canvas.cd();
0109         h2.Draw("COLZ TEXT");
0110 
0111         std::string fileName(m_imageFileName);
0112         canvas.SaveAs(fileName.c_str());
0113 
0114         return true;
0115       } else {
0116         return false;
0117       }
0118     }
0119   };
0120 
0121   class DeDxCalibrationPlot : public cond::payloadInspector::PlotImage<DeDxCalibration, SINGLE_IOV> {
0122   public:
0123     DeDxCalibrationPlot()
0124         : cond::payloadInspector::PlotImage<DeDxCalibration, SINGLE_IOV>(
0125               "DeDxCalibration Thresholds, Alphas, Sigmas, and Gains") {}
0126 
0127     bool fill() override {
0128       gStyle->SetPalette(1);
0129       gStyle->SetOptStat(0);
0130 
0131       auto tag = PlotBase::getTag<0>();
0132       auto iov = tag.iovs.front();
0133 
0134       std::shared_ptr<DeDxCalibration> payload = fetchPayload(std::get<1>(iov));
0135 
0136       if (!payload) {
0137         return false;
0138       }
0139 
0140       // Prepare canvas
0141       TCanvas canvas("DeDxCalibration", "DeDxCalibration", 1200, 800);
0142       canvas.Divide(2, 2);
0143 
0144       // Extract data
0145       const auto& thresholds = payload->thr();
0146       const auto& alphas = payload->alpha();
0147       const auto& sigmas = payload->sigma();
0148       const auto& gains = payload->gain();
0149 
0150       // 1. Plot thresholds
0151       canvas.cd(1);
0152       auto h_thr = new TH1F("Thresholds", "Thresholds;Index;Value", thresholds.size(), 0, thresholds.size());
0153       for (size_t i = 0; i < thresholds.size(); ++i) {
0154         h_thr->SetBinContent(i + 1, thresholds[i]);
0155       }
0156       h_thr->SetFillColor(kBlue);
0157       h_thr->Draw();
0158 
0159       // 2. Plot alphas
0160       canvas.cd(2);
0161       auto h_alpha = new TH1F("Alphas", "Alphas;Index;Value", alphas.size(), 0, alphas.size());
0162       for (size_t i = 0; i < alphas.size(); ++i) {
0163         h_alpha->SetBinContent(i + 1, alphas[i]);
0164       }
0165       h_alpha->SetFillColor(kGreen);
0166       h_alpha->Draw();
0167 
0168       // 3. Plot sigmas
0169       canvas.cd(3);
0170       auto h_sigma = new TH1F("Sigmas", "Sigmas;Index;Value", sigmas.size(), 0, sigmas.size());
0171       for (size_t i = 0; i < sigmas.size(); ++i) {
0172         h_sigma->SetBinContent(i + 1, sigmas[i]);
0173       }
0174       h_sigma->SetFillColor(kRed);
0175       h_sigma->Draw();
0176 
0177       // 4. Plot aggregated gain values
0178       canvas.cd(4);
0179       const int numBins = 100;  // Set number of bins for aggregated gain
0180       auto h_gain = new TH1F("Gains", "Aggregated Gains;Gain Range;Count", numBins, 0, 1.0);  // Adjust range if needed
0181       for (const auto& [chip, gain] : gains) {
0182         h_gain->Fill(gain);
0183       }
0184       h_gain->SetFillColor(kYellow);
0185       h_gain->Draw();
0186 
0187       // Save the canvas to a file
0188       std::string fileName(m_imageFileName);
0189       canvas.SaveAs(fileName.c_str());
0190 
0191       return true;
0192     }
0193   };
0194 }  // namespace
0195 
0196 // Register the classes as boost python plugin
0197 PAYLOAD_INSPECTOR_MODULE(DeDxCalibration) {
0198   PAYLOAD_INSPECTOR_CLASS(DeDxCalibrationTest);
0199   PAYLOAD_INSPECTOR_CLASS(DeDxCalibrationInspector);
0200   PAYLOAD_INSPECTOR_CLASS(DeDxCalibrationPlot);
0201 }