Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-02 04:38:16

0001 /*!
0002   \file RunInfo_PayloadInspector
0003   \Payload Inspector Plugin for RunInfo
0004   \author M. Musich
0005   \version $Revision: 1.0 $
0006   \date $Date: 2018/03/18 10:01:00 $
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 // the data format of the condition to be inspected
0014 #include "CondFormats/RunInfo/interface/RunInfo.h"
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 
0017 // helper
0018 #include "CondCore/RunInfoPlugins/interface/RunInfoPayloadInspectoHelper.h"
0019 
0020 // system includes
0021 #include <memory>
0022 #include <sstream>
0023 #include <iostream>
0024 
0025 // include ROOT
0026 #include "TProfile.h"
0027 #include "TH2F.h"
0028 #include "TLegend.h"
0029 #include "TCanvas.h"
0030 #include "TLine.h"
0031 #include "TStyle.h"
0032 #include "TLatex.h"
0033 #include "TPave.h"
0034 #include "TPaveStats.h"
0035 #include "TPaletteAxis.h"
0036 
0037 namespace {
0038 
0039   using namespace cond::payloadInspector;
0040 
0041   /************************************************
0042      RunInfo Payload Inspector of 1 IOV 
0043   *************************************************/
0044   class RunInfoTest : public Histogram1D<RunInfo, SINGLE_IOV> {
0045   public:
0046     RunInfoTest() : Histogram1D<RunInfo, SINGLE_IOV>("Test RunInfo", "Test RunInfo", 1, 0.0, 1.0) {}
0047 
0048     bool fill() override {
0049       auto tag = PlotBase::getTag<0>();
0050       for (auto const& iov : tag.iovs) {
0051         std::shared_ptr<RunInfo> payload = Base::fetchPayload(std::get<1>(iov));
0052         if (payload.get()) {
0053           payload->printAllValues();
0054         }
0055       }
0056       return true;
0057     }
0058   };
0059 
0060   /************************************************
0061     Summary of RunInfo of 1 IOV 
0062   *************************************************/
0063   class RunInfoParameters : public PlotImage<RunInfo, SINGLE_IOV> {
0064   public:
0065     RunInfoParameters() : PlotImage<RunInfo, SINGLE_IOV>("Display of RunInfo parameters") {}
0066 
0067     bool fill() override {
0068       gStyle->SetPaintTextFormat("g");
0069       auto tag = PlotBase::getTag<0>();
0070       auto iovs = tag.iovs;
0071       auto iov = iovs.front();
0072       std::shared_ptr<RunInfo> payload = fetchPayload(std::get<1>(iov));
0073 
0074       TCanvas canvas("RunInfo Parameters Summary", "RunInfo Parameters summary", 1000, 1000);
0075       canvas.cd();
0076 
0077       gStyle->SetHistMinimumZero();
0078 
0079       canvas.SetTopMargin(0.13);
0080       canvas.SetBottomMargin(0.06);
0081       canvas.SetLeftMargin(0.3);
0082       canvas.SetRightMargin(0.02);
0083       canvas.Modified();
0084       canvas.SetGrid();
0085 
0086       auto h2_RunInfoParameters = std::make_unique<TH2F>("Parameters", "", 1, 0.0, 1.0, 11, 0, 11.);
0087       auto h2_RunInfoState = std::make_unique<TH2F>("State", "", 1, 0.0, 1.0, 11, 0, 11.);
0088       h2_RunInfoParameters->SetStats(false);
0089       h2_RunInfoState->SetStats(false);
0090 
0091       std::function<float(RunInfoPI::parameters)> cutFunctor = [&payload](RunInfoPI::parameters my_param) {
0092         float ret(-999.);
0093         switch (my_param) {
0094           case RunInfoPI::m_run:
0095             return float(payload->m_run);
0096           case RunInfoPI::m_start_time_ll:
0097             return float(payload->m_start_time_ll * 1.0e-6);
0098           case RunInfoPI::m_stop_time_ll:
0099             return float(payload->m_stop_time_ll * 1.0e-6);
0100           case RunInfoPI::m_start_current:
0101             return payload->m_start_current;
0102           case RunInfoPI::m_stop_current:
0103             return payload->m_stop_current;
0104           case RunInfoPI::m_avg_current:
0105             return payload->m_avg_current;
0106           case RunInfoPI::m_max_current:
0107             return payload->m_max_current;
0108           case RunInfoPI::m_min_current:
0109             return payload->m_min_current;
0110           case RunInfoPI::m_run_interval_seconds:
0111             return RunInfoPI::runDuration(payload);
0112           case RunInfoPI::m_BField:
0113             return RunInfoPI::theBField(payload->m_avg_current);  //fieldIntensity;
0114           case RunInfoPI::m_fedIN:
0115             return float((payload->m_fed_in).size());
0116           case RunInfoPI::END_OF_TYPES:
0117             return ret;
0118           default:
0119             return ret;
0120         }
0121       };
0122 
0123       h2_RunInfoParameters->GetXaxis()->SetBinLabel(1, "Value");
0124       h2_RunInfoState->GetXaxis()->SetBinLabel(1, "Value");
0125 
0126       RunInfoPI::state theState;
0127 
0128       unsigned int yBin = 11;
0129       for (int foo = RunInfoPI::m_run; foo != RunInfoPI::END_OF_TYPES; foo++) {
0130         RunInfoPI::parameters param = static_cast<RunInfoPI::parameters>(foo);
0131         std::string theLabel = RunInfoPI::getStringFromTypeEnum(param);
0132         h2_RunInfoState->GetYaxis()->SetBinLabel(yBin, theLabel.c_str());
0133         h2_RunInfoParameters->GetYaxis()->SetBinLabel(yBin, theLabel.c_str());
0134         h2_RunInfoParameters->SetBinContent(1, yBin, cutFunctor(param));
0135         // non-fake payload
0136         if ((payload->m_run) != -1) {
0137           if ((payload->m_avg_current) <= -1) {
0138             // go in error state
0139             h2_RunInfoState->SetBinContent(1, yBin, 0.);
0140             theState = RunInfoPI::k_invalid;
0141           } else {
0142             // all is OK
0143             h2_RunInfoState->SetBinContent(1, yBin, 1.);
0144             theState = RunInfoPI::k_valid;
0145           }
0146         } else {
0147           // this is a fake payload
0148           h2_RunInfoState->SetBinContent(1, yBin, 0.9);
0149           theState = RunInfoPI::k_fake;
0150         }
0151         yBin--;
0152       }
0153 
0154       h2_RunInfoParameters->GetXaxis()->LabelsOption("h");
0155       h2_RunInfoParameters->GetYaxis()->SetLabelSize(0.05);
0156       h2_RunInfoParameters->GetXaxis()->SetLabelSize(0.05);
0157       h2_RunInfoParameters->SetMarkerSize(1.5);
0158 
0159       h2_RunInfoState->GetXaxis()->LabelsOption("h");
0160       h2_RunInfoState->GetYaxis()->SetLabelSize(0.05);
0161       h2_RunInfoState->GetXaxis()->SetLabelSize(0.05);
0162       h2_RunInfoState->SetMarkerSize(1.5);
0163 
0164       RunInfoPI::reportSummaryMapPalette(h2_RunInfoState.get());
0165       h2_RunInfoState->Draw("col");
0166 
0167       h2_RunInfoParameters->Draw("TEXTsame");
0168 
0169       TLatex t1;
0170       t1.SetNDC();
0171       t1.SetTextAlign(12);
0172       t1.SetTextSize(0.03);
0173       t1.DrawLatex(0.1, 0.98, "RunInfo parameters:");
0174       t1.DrawLatex(0.1, 0.95, "payload:");
0175       t1.DrawLatex(0.1, 0.92, "start time:");
0176       t1.DrawLatex(0.1, 0.89, "end time:");
0177 
0178       t1.SetTextFont(42);
0179       t1.SetTextColor(4);
0180       t1.DrawLatex(0.37, 0.982, Form("IOV %s", std::to_string(+std::get<0>(iov)).c_str()));
0181       t1.DrawLatex(0.21, 0.952, Form(" %s", (std::get<1>(iov)).c_str()));
0182       t1.DrawLatex(0.23, 0.922, Form(" %s", (RunInfoPI::runStartTime(payload)).c_str()));
0183       t1.DrawLatex(0.23, 0.892, Form(" %s", (RunInfoPI::runEndTime(payload)).c_str()));
0184 
0185       TPaveText ksPt(0, 0, 0.35, 0.04, "NDC");
0186       ksPt.SetBorderSize(0);
0187       ksPt.SetFillColor(0);
0188       const char* textToAdd;
0189       switch (theState) {
0190         case RunInfoPI::k_fake:
0191           textToAdd = "This is a fake RunInfoPayload";
0192           break;
0193         case RunInfoPI::k_valid:
0194           textToAdd = "This is a valid RunInfoPayload";
0195           break;
0196         case RunInfoPI::k_invalid:
0197           textToAdd = "This is an invalid RunInfoPayload";
0198           break;
0199         default:
0200           throw cms::Exception("PayloadInspector") << "an invalid state has been found";
0201       }
0202 
0203       ksPt.AddText(textToAdd);
0204       ksPt.Draw();
0205 
0206       std::string fileName(m_imageFileName);
0207       canvas.SaveAs(fileName.c_str());
0208 
0209       return true;
0210     }
0211   };
0212 
0213   /************************************************
0214     time history of Magnet currents from RunInfo
0215   *************************************************/
0216   template <RunInfoPI::parameters param>
0217   class RunInfoCurrentHistory : public HistoryPlot<RunInfo, std::pair<bool, float> > {
0218   public:
0219     RunInfoCurrentHistory()
0220         : HistoryPlot<RunInfo, std::pair<bool, float> >(getStringFromTypeEnum(param),
0221                                                         getStringFromTypeEnum(param) + " value") {}
0222     ~RunInfoCurrentHistory() override = default;
0223 
0224     std::pair<bool, float> getFromPayload(RunInfo& payload) override {
0225       bool isRealRun = ((payload.m_run) != -1);
0226       float fieldIntensity = RunInfoPI::theBField(payload.m_avg_current);
0227 
0228       switch (param) {
0229         case RunInfoPI::m_start_current:
0230           return std::make_pair(isRealRun, payload.m_start_current);
0231         case RunInfoPI::m_stop_current:
0232           return std::make_pair(isRealRun, payload.m_stop_current);
0233         case RunInfoPI::m_avg_current:
0234           return std::make_pair(isRealRun, payload.m_avg_current);
0235         case RunInfoPI::m_max_current:
0236           return std::make_pair(isRealRun, payload.m_max_current);
0237         case RunInfoPI::m_min_current:
0238           return std::make_pair(isRealRun, payload.m_min_current);
0239         case RunInfoPI::m_BField:
0240           return std::make_pair(isRealRun, fieldIntensity);
0241         default:
0242           edm::LogWarning("LogicError") << "Unknown parameter: " << param;
0243           break;
0244       }
0245 
0246     }  // payload
0247 
0248     /************************************************/
0249     std::string getStringFromTypeEnum(const RunInfoPI::parameters& parameter) {
0250       switch (parameter) {
0251         case RunInfoPI::m_start_current:
0252           return "Magent start current [A]";
0253         case RunInfoPI::m_stop_current:
0254           return "Magnet stop current [A]";
0255         case RunInfoPI::m_avg_current:
0256           return "Magnet average current [A]";
0257         case RunInfoPI::m_max_current:
0258           return "Magnet max current [A]";
0259         case RunInfoPI::m_min_current:
0260           return "Magnet min current [A]";
0261         case RunInfoPI::m_BField:
0262           return "B-field intensity [T]";
0263         default:
0264           return "should never be here";
0265       }
0266     }
0267   };
0268 
0269   typedef RunInfoCurrentHistory<RunInfoPI::m_start_current> RunInfoStartCurrentHistory;
0270   typedef RunInfoCurrentHistory<RunInfoPI::m_stop_current> RunInfoStopCurrentHistory;
0271   typedef RunInfoCurrentHistory<RunInfoPI::m_avg_current> RunInfoAverageCurrentHistory;
0272   typedef RunInfoCurrentHistory<RunInfoPI::m_max_current> RunInfoMaxCurrentHistory;
0273   typedef RunInfoCurrentHistory<RunInfoPI::m_min_current> RunInfoMinCurrentHistory;
0274   typedef RunInfoCurrentHistory<RunInfoPI::m_BField> RunInfoBFieldHistory;
0275 
0276   /************************************************
0277     time history of Magnet currents from RunInfo
0278   *************************************************/
0279   template <RunInfoPI::DET theDet>
0280   class RunInfoDetHistory : public HistoryPlot<RunInfo, std::pair<bool, float> > {
0281   public:
0282     RunInfoDetHistory() : HistoryPlot<RunInfo, std::pair<bool, float> >("Det Status History", "Det Status History") {}
0283     ~RunInfoDetHistory() override = default;
0284 
0285     std::pair<bool, float> getFromPayload(RunInfo& payload) override {
0286       bool isRealRun = ((payload.m_run) != -1);
0287       float returnValue = 0.;
0288       // early return in case the run is fake
0289       if (!isRealRun) {
0290         return std::make_pair(isRealRun, returnValue);
0291       }
0292 
0293       const auto fedBounds = RunInfoPI::buildFEDBounds();
0294       const auto limits = fedBounds.at(theDet);
0295       const auto& FEDsIn = payload.m_fed_in;
0296       for (const auto& FED : FEDsIn) {
0297         if (FED > limits.first && FED < limits.second) {
0298           returnValue = 1.;
0299           break;  // break the loop to speed up time
0300         }
0301       }
0302       return std::make_pair(isRealRun, returnValue);
0303     }  // payload
0304   };
0305 
0306   using RunInfoTrackerHistory = RunInfoDetHistory<RunInfoPI::SISTRIP>;
0307   using RunInfoSiPixelHistory = RunInfoDetHistory<RunInfoPI::SIPIXEL>;
0308   using RunInfoSiPixelPhase1History = RunInfoDetHistory<RunInfoPI::SIPIXELPHASE1>;
0309   //.... other could be implemented
0310 }  // namespace
0311 
0312 PAYLOAD_INSPECTOR_MODULE(RunInfo) {
0313   PAYLOAD_INSPECTOR_CLASS(RunInfoTest);
0314   PAYLOAD_INSPECTOR_CLASS(RunInfoParameters);
0315   PAYLOAD_INSPECTOR_CLASS(RunInfoStopCurrentHistory);
0316   PAYLOAD_INSPECTOR_CLASS(RunInfoAverageCurrentHistory);
0317   PAYLOAD_INSPECTOR_CLASS(RunInfoMaxCurrentHistory);
0318   PAYLOAD_INSPECTOR_CLASS(RunInfoMinCurrentHistory);
0319   PAYLOAD_INSPECTOR_CLASS(RunInfoBFieldHistory);
0320   PAYLOAD_INSPECTOR_CLASS(RunInfoTrackerHistory);
0321   PAYLOAD_INSPECTOR_CLASS(RunInfoSiPixelHistory);
0322   PAYLOAD_INSPECTOR_CLASS(RunInfoSiPixelPhase1History);
0323 }