SiStripDetVOffTrendPlotter

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"

#include <iostream>
#include <fstream>
#include <sstream>

#include "CondCore/CondDB/interface/ConnectionPool.h"

#include "CondFormats/SiStripObjects/interface/SiStripDetVOff.h"
#include "CondFormats/Common/interface/Time.h"
#include "CondFormats/Common/interface/TimeConversions.h"

#include "FWCore/Framework/interface/EventSetup.h"
#include "Geometry/Records/interface/IdealGeometryRecord.h"
#include "Geometry/TrackerNumberingBuilder/interface/utils.h"

#include <TROOT.h>
#include <TSystem.h>
#include <TCanvas.h>
#include <TFile.h>
#include <TLegend.h>
#include <TGraph.h>
#include <TH1.h>

class SiStripDetVOffTrendPlotter : public edm::one::EDAnalyzer<> {
public:
  const std::vector<Color_t> PLOT_COLORS{kRed, kBlue, kBlack, kOrange, kMagenta};

  explicit SiStripDetVOffTrendPlotter(const edm::ParameterSet &iConfig);
  ~SiStripDetVOffTrendPlotter() override;
  void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override;

private:
  std::string formatIOV(cond::Time_t iov, std::string format = "%Y-%m-%d__%H_%M_%S");
  void prepGraph(TGraph *gr, TString name, TString title, Color_t color);
  void dumpCSV(bool isHV, std::size_t nModules);

  cond::persistency::ConnectionPool m_connectionPool;
  std::string m_condDb;
  std::vector<std::string> m_plotTags;

  // Time interval (in hours) to go back for the plotting.
  int m_interval;
  // 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.
  std::string m_startTime;
  std::string m_endTime;
  // Specify output plot file name. Will use the timestamps if left empty.
  std::string m_outputPlot;
  // Specify output root file name. Leave empty if do not want to save plots in a root file.
  std::string m_outputRootFile;
  // Specify output CSV file name. Leave empty if do not want to dump HV/LV counts in a CSV file.
  std::string m_outputCSV;
  TFile *fout;

  //          IOV                 TAG                #HV, #LV
  std::map<cond::Time_t, std::map<std::string, std::pair<int, int>>> iovMap;

  edm::ESGetToken<GeometricDet, IdealGeometryRecord> geomDetToken_;
};

SiStripDetVOffTrendPlotter::SiStripDetVOffTrendPlotter(const edm::ParameterSet &iConfig)
    : m_connectionPool(),
      m_condDb(iConfig.getParameter<std::string>("conditionDatabase")),
      m_plotTags(iConfig.getParameter<std::vector<std::string>>("plotTags")),
      m_interval(iConfig.getParameter<int>("timeInterval")),
      m_startTime(iConfig.getUntrackedParameter<std::string>("startTime", "")),
      m_endTime(iConfig.getUntrackedParameter<std::string>("endTime", "")),
      m_outputPlot(iConfig.getUntrackedParameter<std::string>("outputPlot", "")),
      m_outputRootFile(iConfig.getUntrackedParameter<std::string>("outputRootFile", "")),
      m_outputCSV(iConfig.getUntrackedParameter<std::string>("outputCSV", "")),
      fout(nullptr),
      geomDetToken_(esConsumes()) {
  m_connectionPool.setParameters(iConfig.getParameter<edm::ParameterSet>("DBParameters"));
  m_connectionPool.configure();
  if (!m_outputRootFile.empty())
    fout = new TFile(m_outputRootFile.data(), "RECREATE");
}

SiStripDetVOffTrendPlotter::~SiStripDetVOffTrendPlotter() {
  if (fout)
    fout->Close();
}

void SiStripDetVOffTrendPlotter::analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) {
  // get total number of modules
  const auto num_modules = TrackerGeometryUtils::getSiStripDetIds(evtSetup.getData(geomDetToken_)).size();

  // get start and end time for DB query
  boost::posix_time::ptime p_start, p_end;
  if (m_interval > 0) {
    // from m_interval hours ago to now
    p_end = boost::posix_time::second_clock::universal_time();
    p_start = p_end - boost::posix_time::hours(m_interval);
  } else {
    // use start and end time from config file
    p_start = boost::posix_time::time_from_string(m_startTime);
    p_end = boost::posix_time::time_from_string(m_endTime);
  }
  cond::Time_t startIov = cond::time::from_boost(p_start);
  cond::Time_t endIov = cond::time::from_boost(p_end);
  if (startIov > endIov)
    throw cms::Exception("endTime must be greater than startTime!");
  edm::LogInfo("SiStripDetVOffTrendPlotter")
      << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
      << "Set start IOV " << startIov << " (" << boost::posix_time::to_simple_string(p_start) << ")"
      << "\n ... Set end IOV " << endIov << " (" << boost::posix_time::to_simple_string(p_end) << ")";

  // open db session
  edm::LogInfo("SiStripDetVOffTrendPlotter") << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
                                             << "Query the condition database " << m_condDb;
  cond::persistency::Session condDbSession = m_connectionPool.createSession(m_condDb);
  condDbSession.transaction().start(true);

  // loop over all tags to plot
  std::vector<TGraph *> hvgraphs, lvgraphs;
  TLegend *leg_hv = new TLegend(0.6, 0.87, 0.99, 0.99);
  TLegend *leg_lv = new TLegend(0.6, 0.87, 0.99, 0.99);
  for (unsigned itag = 0; itag < m_plotTags.size(); ++itag) {
    auto tag = m_plotTags.at(itag);
    auto color = itag < PLOT_COLORS.size() ? PLOT_COLORS.at(itag) : kGreen + itag;

    std::vector<double> vTime;
    std::vector<double> vHVOffPercent, vLVOffPercent;

    // query the database
    edm::LogInfo("SiStripDetVOffTrendPlotter") << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
                                               << "Reading IOVs from tag " << tag;
    cond::persistency::IOVProxy iovProxy = condDbSession.readIov(tag);
    auto iovs = iovProxy.selectAll();
    auto iiov = iovs.find(startIov);
    auto eiov = iovs.find(endIov);
    int niov = 0;
    while (iiov != iovs.end() && (*iiov).since <= (*eiov).since) {
      // convert cond::Time_t to seconds since epoch
      if ((*iiov).since < startIov)
        vTime.push_back(cond::time::unpack(startIov).first);
      else
        vTime.push_back(cond::time::unpack((*iiov).since).first);
      auto payload = condDbSession.fetchPayload<SiStripDetVOff>((*iiov).payloadId);
      vHVOffPercent.push_back(1.0 * payload->getHVoffCounts() / num_modules);
      vLVOffPercent.push_back(1.0 * payload->getLVoffCounts() / num_modules);
      iovMap[(*iiov).since][tag] = {payload->getHVoffCounts(), payload->getLVoffCounts()};
      // debug
      std::cout << boost::posix_time::to_simple_string(cond::time::to_boost((*iiov).since)) << " (" << (*iiov).since
                << ")"
                << ", # HV Off=" << std::setw(6) << payload->getHVoffCounts() << ", # LV Off=" << std::setw(6)
                << payload->getLVoffCounts() << std::endl;
      ++iiov;
      ++niov;
    }
    edm::LogInfo("SiStripDetVOffTrendPlotter")
        << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
        << "Read " << niov << " IOVs from tag " << tag << " in the specified interval.";

    TGraph *hv = new TGraph(vTime.size(), vTime.data(), vHVOffPercent.data());
    prepGraph(hv, TString("HVOff_") + tag, ";UTC;Fraction of HV off", color);
    leg_hv->AddEntry(hv, tag.data(), "LP");

    TGraph *lv = new TGraph(vTime.size(), vTime.data(), vLVOffPercent.data());
    prepGraph(lv, TString("LVOff_") + tag, ";UTC;Fraction of LV off", color);
    leg_lv->AddEntry(lv, tag.data(), "LP");

    hvgraphs.push_back(hv);
    lvgraphs.push_back(lv);
  }

  condDbSession.transaction().commit();

  // Make plots
  TCanvas c("c", "c", 1800, 1200);
  c.SetTopMargin(0.12);
  c.SetBottomMargin(0.08);
  c.SetGridx();
  c.SetGridy();
  for (const auto hv : hvgraphs) {
    if (hv == hvgraphs.front())
      hv->Draw("ALP");
    else
      hv->Draw("LPsame");
    if (fout) {
      fout->cd();
      hv->Write();
    }
  }
  leg_hv->Draw();
  std::string plot_postfix =
      !m_outputPlot.empty() ? m_outputPlot : "from_" + formatIOV(startIov) + "_to_" + formatIOV(endIov) + ".png";
  c.Print(("HVOff_" + plot_postfix).data());

  c.Clear();
  for (const auto lv : lvgraphs) {
    if (lv == lvgraphs.front())
      lv->Draw("ALP");
    else
      lv->Draw("LPsame");
    if (fout) {
      fout->cd();
      lv->Write();
    }
  }
  leg_lv->Draw();
  c.Print(("LVOff_" + plot_postfix).data());

  if (!m_outputCSV.empty()) {
    dumpCSV(true, num_modules);
    dumpCSV(false, num_modules);
  }
}

std::string SiStripDetVOffTrendPlotter::formatIOV(cond::Time_t iov, std::string format) {
  auto facet = new boost::posix_time::time_facet(format.c_str());
  std::ostringstream stream;
  stream.imbue(std::locale(stream.getloc(), facet));
  stream << cond::time::to_boost(iov);
  return stream.str();
}

void SiStripDetVOffTrendPlotter::prepGraph(TGraph *gr, TString name, TString title, Color_t color) {
  gr->SetName(name);
  gr->SetTitle(title);
  gr->SetLineColor(color);
  gr->SetLineWidth(2);
  gr->SetMarkerStyle(20);
  gr->SetMarkerSize(1.5);
  gr->SetMarkerColor(color);
  gr->GetXaxis()->SetTimeDisplay(1);
  gr->GetXaxis()->SetLabelOffset(0.02);
  gr->GetXaxis()->SetTimeFormat("#splitline{%b %d}{%H:%M}");
  gr->GetXaxis()->SetTimeOffset(0, "gmt");
  gr->GetXaxis()->SetLabelSize(0.025);
  gr->GetXaxis()->SetTitleSize(0.025);
  gr->GetXaxis()->SetTitleOffset(1.6);
  gr->GetYaxis()->SetRangeUser(0, 1.05);
}

void SiStripDetVOffTrendPlotter::dumpCSV(bool isHV, std::size_t nModules) {
  std::string outCSV = isHV ? "HVOff_table_" + m_outputCSV : "LVOff_table_" + m_outputCSV;
  std::ofstream csv;
  csv.open(outCSV);
  csv << "IOV,Timestamp(UTC),";
  for (const auto &tag : m_plotTags)
    csv << tag << ",";
  csv << std::endl;

  csv << std::fixed << std::setprecision(1);

  for (const auto &v : iovMap) {
    auto iov = v.first;
    auto timestamp = boost::posix_time::to_simple_string(cond::time::to_boost(iov));
    csv << iov << "," << timestamp << ",";
    for (const auto &tag : m_plotTags) {
      if (v.second.find(tag) != v.second.end()) {
        int count = isHV ? v.second.at(tag).first : v.second.at(tag).second;
        csv << count << " (" << 100. * count / nModules << "%)";
      }
      csv << ",";
    }
    csv << std::endl;
  }

  csv.close();
}

DEFINE_FWK_MODULE(SiStripDetVOffTrendPlotter);