Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:59:49

0001 #include "FWCore/Framework/interface/Event.h"
0002 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0003 #include "FWCore/Framework/interface/MakerMacros.h"
0004 #include "FWCore/ServiceRegistry/interface/Service.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 
0007 #include <iostream>
0008 #include <fstream>
0009 #include <sstream>
0010 
0011 #include "CondCore/CondDB/interface/ConnectionPool.h"
0012 
0013 #include "CondFormats/SiStripObjects/interface/SiStripDetVOff.h"
0014 #include "CondFormats/Common/interface/Time.h"
0015 #include "CondFormats/Common/interface/TimeConversions.h"
0016 
0017 #include "FWCore/Framework/interface/EventSetup.h"
0018 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0019 #include "Geometry/TrackerNumberingBuilder/interface/utils.h"
0020 
0021 #include <TROOT.h>
0022 #include <TSystem.h>
0023 #include <TCanvas.h>
0024 #include <TFile.h>
0025 #include <TLegend.h>
0026 #include <TGraph.h>
0027 #include <TH1.h>
0028 
0029 class SiStripDetVOffTrendPlotter : public edm::one::EDAnalyzer<> {
0030 public:
0031   const std::vector<Color_t> PLOT_COLORS{kRed, kBlue, kBlack, kOrange, kMagenta};
0032 
0033   explicit SiStripDetVOffTrendPlotter(const edm::ParameterSet &iConfig);
0034   ~SiStripDetVOffTrendPlotter() override;
0035   void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override;
0036 
0037 private:
0038   std::string formatIOV(cond::Time_t iov, std::string format = "%Y-%m-%d__%H_%M_%S");
0039   void prepGraph(TGraph *gr, TString name, TString title, Color_t color);
0040   void dumpCSV(bool isHV, std::size_t nModules);
0041 
0042   cond::persistency::ConnectionPool m_connectionPool;
0043   std::string m_condDb;
0044   std::vector<std::string> m_plotTags;
0045 
0046   // Time interval (in hours) to go back for the plotting.
0047   int m_interval;
0048   // Or manually specify the start/end time. Format: "2002-01-20 23:59:59.000". Set m_interval to non-positive number to use this.
0049   std::string m_startTime;
0050   std::string m_endTime;
0051   // Specify output plot file name. Will use the timestamps if left empty.
0052   std::string m_outputPlot;
0053   // Specify output root file name. Leave empty if do not want to save plots in a root file.
0054   std::string m_outputRootFile;
0055   // Specify output CSV file name. Leave empty if do not want to dump HV/LV counts in a CSV file.
0056   std::string m_outputCSV;
0057   TFile *fout;
0058 
0059   //          IOV                 TAG                #HV, #LV
0060   std::map<cond::Time_t, std::map<std::string, std::pair<int, int>>> iovMap;
0061 
0062   edm::ESGetToken<GeometricDet, IdealGeometryRecord> geomDetToken_;
0063 };
0064 
0065 SiStripDetVOffTrendPlotter::SiStripDetVOffTrendPlotter(const edm::ParameterSet &iConfig)
0066     : m_connectionPool(),
0067       m_condDb(iConfig.getParameter<std::string>("conditionDatabase")),
0068       m_plotTags(iConfig.getParameter<std::vector<std::string>>("plotTags")),
0069       m_interval(iConfig.getParameter<int>("timeInterval")),
0070       m_startTime(iConfig.getUntrackedParameter<std::string>("startTime", "")),
0071       m_endTime(iConfig.getUntrackedParameter<std::string>("endTime", "")),
0072       m_outputPlot(iConfig.getUntrackedParameter<std::string>("outputPlot", "")),
0073       m_outputRootFile(iConfig.getUntrackedParameter<std::string>("outputRootFile", "")),
0074       m_outputCSV(iConfig.getUntrackedParameter<std::string>("outputCSV", "")),
0075       fout(nullptr),
0076       geomDetToken_(esConsumes()) {
0077   m_connectionPool.setParameters(iConfig.getParameter<edm::ParameterSet>("DBParameters"));
0078   m_connectionPool.configure();
0079   if (!m_outputRootFile.empty())
0080     fout = new TFile(m_outputRootFile.data(), "RECREATE");
0081 }
0082 
0083 SiStripDetVOffTrendPlotter::~SiStripDetVOffTrendPlotter() {
0084   if (fout)
0085     fout->Close();
0086 }
0087 
0088 void SiStripDetVOffTrendPlotter::analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) {
0089   // get total number of modules
0090   const auto num_modules = TrackerGeometryUtils::getSiStripDetIds(evtSetup.getData(geomDetToken_)).size();
0091 
0092   // get start and end time for DB query
0093   boost::posix_time::ptime p_start, p_end;
0094   if (m_interval > 0) {
0095     // from m_interval hours ago to now
0096     p_end = boost::posix_time::second_clock::universal_time();
0097     p_start = p_end - boost::posix_time::hours(m_interval);
0098   } else {
0099     // use start and end time from config file
0100     p_start = boost::posix_time::time_from_string(m_startTime);
0101     p_end = boost::posix_time::time_from_string(m_endTime);
0102   }
0103   cond::Time_t startIov = cond::time::from_boost(p_start);
0104   cond::Time_t endIov = cond::time::from_boost(p_end);
0105   if (startIov > endIov)
0106     throw cms::Exception("endTime must be greater than startTime!");
0107   edm::LogInfo("SiStripDetVOffTrendPlotter")
0108       << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
0109       << "Set start IOV " << startIov << " (" << boost::posix_time::to_simple_string(p_start) << ")"
0110       << "\n ... Set end IOV " << endIov << " (" << boost::posix_time::to_simple_string(p_end) << ")";
0111 
0112   // open db session
0113   edm::LogInfo("SiStripDetVOffTrendPlotter") << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
0114                                              << "Query the condition database " << m_condDb;
0115   cond::persistency::Session condDbSession = m_connectionPool.createSession(m_condDb);
0116   condDbSession.transaction().start(true);
0117 
0118   // loop over all tags to plot
0119   std::vector<TGraph *> hvgraphs, lvgraphs;
0120   TLegend *leg_hv = new TLegend(0.6, 0.87, 0.99, 0.99);
0121   TLegend *leg_lv = new TLegend(0.6, 0.87, 0.99, 0.99);
0122   for (unsigned itag = 0; itag < m_plotTags.size(); ++itag) {
0123     auto tag = m_plotTags.at(itag);
0124     auto color = itag < PLOT_COLORS.size() ? PLOT_COLORS.at(itag) : kGreen + itag;
0125 
0126     std::vector<double> vTime;
0127     std::vector<double> vHVOffPercent, vLVOffPercent;
0128 
0129     // query the database
0130     edm::LogInfo("SiStripDetVOffTrendPlotter") << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
0131                                                << "Reading IOVs from tag " << tag;
0132     cond::persistency::IOVProxy iovProxy = condDbSession.readIov(tag);
0133     auto iovs = iovProxy.selectAll();
0134     auto iiov = iovs.find(startIov);
0135     auto eiov = iovs.find(endIov);
0136     int niov = 0;
0137     while (iiov != iovs.end() && (*iiov).since <= (*eiov).since) {
0138       // convert cond::Time_t to seconds since epoch
0139       if ((*iiov).since < startIov)
0140         vTime.push_back(cond::time::unpack(startIov).first);
0141       else
0142         vTime.push_back(cond::time::unpack((*iiov).since).first);
0143       auto payload = condDbSession.fetchPayload<SiStripDetVOff>((*iiov).payloadId);
0144       vHVOffPercent.push_back(1.0 * payload->getHVoffCounts() / num_modules);
0145       vLVOffPercent.push_back(1.0 * payload->getLVoffCounts() / num_modules);
0146       iovMap[(*iiov).since][tag] = {payload->getHVoffCounts(), payload->getLVoffCounts()};
0147       // debug
0148       std::cout << boost::posix_time::to_simple_string(cond::time::to_boost((*iiov).since)) << " (" << (*iiov).since
0149                 << ")"
0150                 << ", # HV Off=" << std::setw(6) << payload->getHVoffCounts() << ", # LV Off=" << std::setw(6)
0151                 << payload->getLVoffCounts() << std::endl;
0152       ++iiov;
0153       ++niov;
0154     }
0155     edm::LogInfo("SiStripDetVOffTrendPlotter")
0156         << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
0157         << "Read " << niov << " IOVs from tag " << tag << " in the specified interval.";
0158 
0159     TGraph *hv = new TGraph(vTime.size(), vTime.data(), vHVOffPercent.data());
0160     prepGraph(hv, TString("HVOff_") + tag, ";UTC;Fraction of HV off", color);
0161     leg_hv->AddEntry(hv, tag.data(), "LP");
0162 
0163     TGraph *lv = new TGraph(vTime.size(), vTime.data(), vLVOffPercent.data());
0164     prepGraph(lv, TString("LVOff_") + tag, ";UTC;Fraction of LV off", color);
0165     leg_lv->AddEntry(lv, tag.data(), "LP");
0166 
0167     hvgraphs.push_back(hv);
0168     lvgraphs.push_back(lv);
0169   }
0170 
0171   condDbSession.transaction().commit();
0172 
0173   // Make plots
0174   TCanvas c("c", "c", 1800, 1200);
0175   c.SetTopMargin(0.12);
0176   c.SetBottomMargin(0.08);
0177   c.SetGridx();
0178   c.SetGridy();
0179   for (const auto hv : hvgraphs) {
0180     if (hv == hvgraphs.front())
0181       hv->Draw("ALP");
0182     else
0183       hv->Draw("LPsame");
0184     if (fout) {
0185       fout->cd();
0186       hv->Write();
0187     }
0188   }
0189   leg_hv->Draw();
0190   std::string plot_postfix =
0191       !m_outputPlot.empty() ? m_outputPlot : "from_" + formatIOV(startIov) + "_to_" + formatIOV(endIov) + ".png";
0192   c.Print(("HVOff_" + plot_postfix).data());
0193 
0194   c.Clear();
0195   for (const auto lv : lvgraphs) {
0196     if (lv == lvgraphs.front())
0197       lv->Draw("ALP");
0198     else
0199       lv->Draw("LPsame");
0200     if (fout) {
0201       fout->cd();
0202       lv->Write();
0203     }
0204   }
0205   leg_lv->Draw();
0206   c.Print(("LVOff_" + plot_postfix).data());
0207 
0208   if (!m_outputCSV.empty()) {
0209     dumpCSV(true, num_modules);
0210     dumpCSV(false, num_modules);
0211   }
0212 }
0213 
0214 std::string SiStripDetVOffTrendPlotter::formatIOV(cond::Time_t iov, std::string format) {
0215   auto facet = new boost::posix_time::time_facet(format.c_str());
0216   std::ostringstream stream;
0217   stream.imbue(std::locale(stream.getloc(), facet));
0218   stream << cond::time::to_boost(iov);
0219   return stream.str();
0220 }
0221 
0222 void SiStripDetVOffTrendPlotter::prepGraph(TGraph *gr, TString name, TString title, Color_t color) {
0223   gr->SetName(name);
0224   gr->SetTitle(title);
0225   gr->SetLineColor(color);
0226   gr->SetLineWidth(2);
0227   gr->SetMarkerStyle(20);
0228   gr->SetMarkerSize(1.5);
0229   gr->SetMarkerColor(color);
0230   gr->GetXaxis()->SetTimeDisplay(1);
0231   gr->GetXaxis()->SetLabelOffset(0.02);
0232   gr->GetXaxis()->SetTimeFormat("#splitline{%b %d}{%H:%M}");
0233   gr->GetXaxis()->SetTimeOffset(0, "gmt");
0234   gr->GetXaxis()->SetLabelSize(0.025);
0235   gr->GetXaxis()->SetTitleSize(0.025);
0236   gr->GetXaxis()->SetTitleOffset(1.6);
0237   gr->GetYaxis()->SetRangeUser(0, 1.05);
0238 }
0239 
0240 void SiStripDetVOffTrendPlotter::dumpCSV(bool isHV, std::size_t nModules) {
0241   std::string outCSV = isHV ? "HVOff_table_" + m_outputCSV : "LVOff_table_" + m_outputCSV;
0242   std::ofstream csv;
0243   csv.open(outCSV);
0244   csv << "IOV,Timestamp(UTC),";
0245   for (const auto &tag : m_plotTags)
0246     csv << tag << ",";
0247   csv << std::endl;
0248 
0249   csv << std::fixed << std::setprecision(1);
0250 
0251   for (const auto &v : iovMap) {
0252     auto iov = v.first;
0253     auto timestamp = boost::posix_time::to_simple_string(cond::time::to_boost(iov));
0254     csv << iov << "," << timestamp << ",";
0255     for (const auto &tag : m_plotTags) {
0256       if (v.second.find(tag) != v.second.end()) {
0257         int count = isHV ? v.second.at(tag).first : v.second.at(tag).second;
0258         csv << count << " (" << 100. * count / nModules << "%)";
0259       }
0260       csv << ",";
0261     }
0262     csv << std::endl;
0263   }
0264 
0265   csv.close();
0266 }
0267 
0268 DEFINE_FWK_MODULE(SiStripDetVOffTrendPlotter);