SiStripDetVOffTkMapPlotter

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
#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 <sstream>

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

#include "Geometry/Records/interface/TrackerTopologyRcd.h"
#include "Geometry/TrackerNumberingBuilder/interface/utils.h"

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

#include "DQM/SiStripCommon/interface/TkHistoMap.h"
#include "CommonTools/TrackerMap/interface/TrackerMap.h"

class SiStripDetVOffTkMapPlotter : public edm::one::EDAnalyzer<> {
public:
  explicit SiStripDetVOffTkMapPlotter(const edm::ParameterSet& iConfig);
  ~SiStripDetVOffTkMapPlotter() 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");

  cond::persistency::ConnectionPool m_connectionPool;
  std::string m_condDb;
  std::string m_plotTag;

  // IOV of plotting.
  cond::Time_t m_IOV;
  // Or use datatime string. Format: "2002-01-20 23:59:59.000". Set IOV to 0 to use this.
  std::string m_Time;
  // Set the plot format. Default: png.
  std::string m_plotFormat;
  // Specify output root file name. Leave empty if do not want to save plots in a root file.
  std::string m_outputFile;

  edm::ESGetToken<TkDetMap, TrackerTopologyRcd> tkDetMapToken_;
  edm::ESGetToken<GeometricDet, IdealGeometryRecord> geomDetToken_;
};

SiStripDetVOffTkMapPlotter::SiStripDetVOffTkMapPlotter(const edm::ParameterSet& iConfig)
    : m_connectionPool(),
      m_condDb(iConfig.getParameter<std::string>("conditionDatabase")),
      m_plotTag(iConfig.getParameter<std::string>("Tag")),
      m_IOV(iConfig.getUntrackedParameter<cond::Time_t>("IOV", 0)),
      m_Time(iConfig.getUntrackedParameter<std::string>("Time", "")),
      m_plotFormat(iConfig.getUntrackedParameter<std::string>("plotFormat", "png")),
      m_outputFile(iConfig.getUntrackedParameter<std::string>("outputFile", "")),
      tkDetMapToken_(esConsumes()),
      geomDetToken_(esConsumes()) {
  m_connectionPool.setParameters(iConfig.getParameter<edm::ParameterSet>("DBParameters"));
  m_connectionPool.configure();
}

SiStripDetVOffTkMapPlotter::~SiStripDetVOffTkMapPlotter() = default;

void SiStripDetVOffTkMapPlotter::analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) {
  cond::Time_t theIov = 0;
  if (m_IOV != 0) {
    theIov = m_IOV;
  } else if (!m_Time.empty()) {
    theIov = cond::time::from_boost(boost::posix_time::time_from_string(m_Time));
  } else {
    // Use the current time if no input. Will get the last IOV.
    theIov = cond::time::from_boost(boost::posix_time::second_clock::universal_time());
  }

  // open db session
  edm::LogInfo("SiStripDetVOffMapPlotter") << "[SiStripDetVOffMapPlotter::" << __func__ << "] "
                                           << "Query the condition database " << m_condDb << " for tag " << m_plotTag;
  cond::persistency::Session condDbSession = m_connectionPool.createSession(m_condDb);
  condDbSession.transaction().start(true);
  cond::persistency::IOVProxy iovProxy = condDbSession.readIov(m_plotTag);
  auto iovs = iovProxy.selectAll();
  auto iiov = iovs.find(theIov);
  if (iiov == iovs.end())
    throw cms::Exception("Input IOV " + std::to_string(m_IOV) + "/" + m_Time + " is invalid!");

  theIov = (*iiov).since;
  edm::LogInfo("SiStripDetVOffMapPlotter") << "[SiStripDetVOffMapPlotter::" << __func__ << "] "
                                           << "Make tkMap for IOV " << theIov << " ("
                                           << boost::posix_time::to_simple_string(cond::time::to_boost(theIov)) << ")";
  auto payload = condDbSession.fetchPayload<SiStripDetVOff>((*iiov).payloadId);

  const TkDetMap* tkDetMap = &evtSetup.getData(tkDetMapToken_);
  TrackerMap lvmap, hvmap;
  TkHistoMap lvhisto(tkDetMap, "LV_Status", "LV_Status", -1);
  TkHistoMap hvhisto(tkDetMap, "HV_Status", "HV_Status", -1);

  const auto detids = TrackerGeometryUtils::getSiStripDetIds(evtSetup.getData(geomDetToken_));
  for (auto id : detids) {
    if (payload->IsModuleLVOff(id))
      lvhisto.fill(id, 1);  // RED
    else
      lvhisto.fill(id, 0.5);

    if (payload->IsModuleHVOff(id))
      hvhisto.fill(id, 1);  // RED
    else
      hvhisto.fill(id, 0.5);
  }

  lvhisto.dumpInTkMap(&lvmap);
  hvhisto.dumpInTkMap(&hvmap);
  lvmap.setPalette(1);
  hvmap.setPalette(1);
  lvmap.save(true, 0, 0, "LV_tkMap_" + formatIOV(theIov) + "." + m_plotFormat);
  hvmap.save(true, 0, 0, "HV_tkMap_" + formatIOV(theIov) + "." + m_plotFormat);

  if (!m_outputFile.empty()) {
    lvhisto.save(m_outputFile);
    hvhisto.save(m_outputFile);
  }
}

std::string SiStripDetVOffTkMapPlotter::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();
}

DEFINE_FWK_MODULE(SiStripDetVOffTkMapPlotter);