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 <sstream>
0009 
0010 #include "CondCore/CondDB/interface/ConnectionPool.h"
0011 
0012 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0013 #include "Geometry/TrackerNumberingBuilder/interface/utils.h"
0014 
0015 #include "CondFormats/SiStripObjects/interface/SiStripDetVOff.h"
0016 #include "CondFormats/Common/interface/Time.h"
0017 #include "CondFormats/Common/interface/TimeConversions.h"
0018 
0019 #include "DQM/SiStripCommon/interface/TkHistoMap.h"
0020 #include "CommonTools/TrackerMap/interface/TrackerMap.h"
0021 
0022 class SiStripDetVOffTkMapPlotter : public edm::one::EDAnalyzer<> {
0023 public:
0024   explicit SiStripDetVOffTkMapPlotter(const edm::ParameterSet& iConfig);
0025   ~SiStripDetVOffTkMapPlotter() override;
0026   void analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) override;
0027 
0028 private:
0029   std::string formatIOV(cond::Time_t iov, std::string format = "%Y-%m-%d__%H_%M_%S");
0030 
0031   cond::persistency::ConnectionPool m_connectionPool;
0032   std::string m_condDb;
0033   std::string m_plotTag;
0034 
0035   // IOV of plotting.
0036   cond::Time_t m_IOV;
0037   // Or use datatime string. Format: "2002-01-20 23:59:59.000". Set IOV to 0 to use this.
0038   std::string m_Time;
0039   // Set the plot format. Default: png.
0040   std::string m_plotFormat;
0041   // Specify output root file name. Leave empty if do not want to save plots in a root file.
0042   std::string m_outputFile;
0043 
0044   edm::ESGetToken<TkDetMap, TrackerTopologyRcd> tkDetMapToken_;
0045   edm::ESGetToken<GeometricDet, IdealGeometryRecord> geomDetToken_;
0046 };
0047 
0048 SiStripDetVOffTkMapPlotter::SiStripDetVOffTkMapPlotter(const edm::ParameterSet& iConfig)
0049     : m_connectionPool(),
0050       m_condDb(iConfig.getParameter<std::string>("conditionDatabase")),
0051       m_plotTag(iConfig.getParameter<std::string>("Tag")),
0052       m_IOV(iConfig.getUntrackedParameter<cond::Time_t>("IOV", 0)),
0053       m_Time(iConfig.getUntrackedParameter<std::string>("Time", "")),
0054       m_plotFormat(iConfig.getUntrackedParameter<std::string>("plotFormat", "png")),
0055       m_outputFile(iConfig.getUntrackedParameter<std::string>("outputFile", "")),
0056       tkDetMapToken_(esConsumes()),
0057       geomDetToken_(esConsumes()) {
0058   m_connectionPool.setParameters(iConfig.getParameter<edm::ParameterSet>("DBParameters"));
0059   m_connectionPool.configure();
0060 }
0061 
0062 SiStripDetVOffTkMapPlotter::~SiStripDetVOffTkMapPlotter() = default;
0063 
0064 void SiStripDetVOffTkMapPlotter::analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) {
0065   cond::Time_t theIov = 0;
0066   if (m_IOV != 0) {
0067     theIov = m_IOV;
0068   } else if (!m_Time.empty()) {
0069     theIov = cond::time::from_boost(boost::posix_time::time_from_string(m_Time));
0070   } else {
0071     // Use the current time if no input. Will get the last IOV.
0072     theIov = cond::time::from_boost(boost::posix_time::second_clock::universal_time());
0073   }
0074 
0075   // open db session
0076   edm::LogInfo("SiStripDetVOffMapPlotter") << "[SiStripDetVOffMapPlotter::" << __func__ << "] "
0077                                            << "Query the condition database " << m_condDb << " for tag " << m_plotTag;
0078   cond::persistency::Session condDbSession = m_connectionPool.createSession(m_condDb);
0079   condDbSession.transaction().start(true);
0080   cond::persistency::IOVProxy iovProxy = condDbSession.readIov(m_plotTag);
0081   auto iovs = iovProxy.selectAll();
0082   auto iiov = iovs.find(theIov);
0083   if (iiov == iovs.end())
0084     throw cms::Exception("Input IOV " + std::to_string(m_IOV) + "/" + m_Time + " is invalid!");
0085 
0086   theIov = (*iiov).since;
0087   edm::LogInfo("SiStripDetVOffMapPlotter") << "[SiStripDetVOffMapPlotter::" << __func__ << "] "
0088                                            << "Make tkMap for IOV " << theIov << " ("
0089                                            << boost::posix_time::to_simple_string(cond::time::to_boost(theIov)) << ")";
0090   auto payload = condDbSession.fetchPayload<SiStripDetVOff>((*iiov).payloadId);
0091 
0092   const TkDetMap* tkDetMap = &evtSetup.getData(tkDetMapToken_);
0093   TrackerMap lvmap, hvmap;
0094   TkHistoMap lvhisto(tkDetMap, "LV_Status", "LV_Status", -1);
0095   TkHistoMap hvhisto(tkDetMap, "HV_Status", "HV_Status", -1);
0096 
0097   const auto detids = TrackerGeometryUtils::getSiStripDetIds(evtSetup.getData(geomDetToken_));
0098   for (auto id : detids) {
0099     if (payload->IsModuleLVOff(id))
0100       lvhisto.fill(id, 1);  // RED
0101     else
0102       lvhisto.fill(id, 0.5);
0103 
0104     if (payload->IsModuleHVOff(id))
0105       hvhisto.fill(id, 1);  // RED
0106     else
0107       hvhisto.fill(id, 0.5);
0108   }
0109 
0110   lvhisto.dumpInTkMap(&lvmap);
0111   hvhisto.dumpInTkMap(&hvmap);
0112   lvmap.setPalette(1);
0113   hvmap.setPalette(1);
0114   lvmap.save(true, 0, 0, "LV_tkMap_" + formatIOV(theIov) + "." + m_plotFormat);
0115   hvmap.save(true, 0, 0, "HV_tkMap_" + formatIOV(theIov) + "." + m_plotFormat);
0116 
0117   if (!m_outputFile.empty()) {
0118     lvhisto.save(m_outputFile);
0119     hvhisto.save(m_outputFile);
0120   }
0121 }
0122 
0123 std::string SiStripDetVOffTkMapPlotter::formatIOV(cond::Time_t iov, std::string format) {
0124   auto facet = new boost::posix_time::time_facet(format.c_str());
0125   std::ostringstream stream;
0126   stream.imbue(std::locale(stream.getloc(), facet));
0127   stream << cond::time::to_boost(iov);
0128   return stream.str();
0129 }
0130 
0131 DEFINE_FWK_MODULE(SiStripDetVOffTkMapPlotter);