Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-11-23 04:28:42

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 <TROOT.h>
0018 #include <TSystem.h>
0019 #include <TCanvas.h>
0020 #include <TFile.h>
0021 #include <TLegend.h>
0022 #include <TGraph.h>
0023 #include <TH1.h>
0024 
0025 class SiStripDetVOffPrinter : public edm::one::EDAnalyzer<> {
0026 public:
0027   explicit SiStripDetVOffPrinter(const edm::ParameterSet& iConfig);
0028   ~SiStripDetVOffPrinter() override;
0029   void analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) override;
0030   void endJob() override;
0031   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0032 
0033 private:
0034   cond::persistency::ConnectionPool m_connectionPool;
0035   std::string m_condDb;
0036   std::string m_tagName;
0037 
0038   // Manually specify the start/end time. Format: "2002-01-20 23:59:59.000".
0039   std::string m_startTime;
0040   std::string m_endTime;
0041   // Specify output text file name. Leave empty if do not want to dump HV/LV counts in a file.
0042   std::string m_output;
0043 
0044   //          IOV                 DETIDs
0045   std::map<cond::Time_t, std::set<uint32_t> > iovMap_HVOff;
0046   std::map<cond::Time_t, std::set<uint32_t> > iovMap_LVOff;
0047   //          DETIDs              IOV
0048   std::map<uint32_t, std::vector<cond::Time_t> > detidMap;
0049 };
0050 
0051 SiStripDetVOffPrinter::SiStripDetVOffPrinter(const edm::ParameterSet& iConfig)
0052     : m_connectionPool(),
0053       m_condDb(iConfig.getParameter<std::string>("conditionDatabase")),
0054       m_tagName(iConfig.getParameter<std::string>("tagName")),
0055       m_startTime(iConfig.getParameter<std::string>("startTime")),
0056       m_endTime(iConfig.getParameter<std::string>("endTime")),
0057       m_output(iConfig.getParameter<std::string>("output")) {
0058   m_connectionPool.setParameters(iConfig.getParameter<edm::ParameterSet>("DBParameters"));
0059   m_connectionPool.configure();
0060 }
0061 
0062 SiStripDetVOffPrinter::~SiStripDetVOffPrinter() = default;
0063 
0064 void SiStripDetVOffPrinter::analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) {
0065   // use start and end time from config file
0066   boost::posix_time::ptime p_start, p_end;
0067   p_start = boost::posix_time::time_from_string(m_startTime);
0068   p_end = boost::posix_time::time_from_string(m_endTime);
0069   cond::Time_t startIov = cond::time::from_boost(p_start);
0070   cond::Time_t endIov = cond::time::from_boost(p_end);
0071   if (startIov > endIov)
0072     throw cms::Exception("endTime must be greater than startTime!");
0073   edm::LogInfo("SiStripDetVOffPrinter") << "[SiStripDetVOffPrinter::" << __func__ << "] "
0074                                         << "Set start time " << startIov << " ("
0075                                         << boost::posix_time::to_simple_string(p_start) << ")"
0076                                         << "\n ... Set end time " << endIov << " ("
0077                                         << boost::posix_time::to_simple_string(p_end) << ")";
0078 
0079   // open db session
0080   edm::LogInfo("SiStripDetVOffPrinter") << "[SiStripDetVOffPrinter::" << __func__ << "] "
0081                                         << "Query the condition database " << m_condDb;
0082   cond::persistency::Session condDbSession = m_connectionPool.createSession(m_condDb);
0083   condDbSession.transaction().start(true);
0084 
0085   std::stringstream ss;
0086   // list of times with new IOVs within the time range
0087   std::vector<cond::Time_t> vTime;
0088 
0089   // query the database
0090   edm::LogInfo("SiStripDetVOffPrinter") << "[SiStripDetVOffPrinter::" << __func__ << "] "
0091                                         << "Reading IOVs from tag " << m_tagName;
0092   cond::persistency::IOVProxy iovProxy = condDbSession.readIov(m_tagName);  // load all?
0093   auto iovs = iovProxy.selectAll();
0094   auto iiov = iovs.find(startIov);
0095   auto eiov = iovs.find(endIov);
0096   int niov = 0;
0097   while (iiov != iovs.end() && (*iiov).since <= (*eiov).since) {
0098     // convert cond::Time_t to seconds since epoch
0099     if ((*iiov).since < startIov) {
0100       vTime.push_back(startIov);
0101     } else {
0102       vTime.push_back((*iiov).since);
0103     }
0104     auto payload = condDbSession.fetchPayload<SiStripDetVOff>((*iiov).payloadId);
0105     // print IOVs summary
0106     ss << boost::posix_time::to_simple_string(cond::time::to_boost((*iiov).since)) << " (" << (*iiov).since << ")"
0107        << ", # HV Off=" << std::setw(6) << payload->getHVoffCounts() << ", # LV Off=" << std::setw(6)
0108        << payload->getLVoffCounts() << std::endl;
0109 
0110     // list of detids with HV/LV Off
0111     std::vector<uint32_t> detIds;
0112     payload->getDetIds(detIds);
0113     std::set<uint32_t> detIds_HVOff;
0114     std::set<uint32_t> detIds_LVOff;
0115     std::vector<uint32_t>::const_iterator it = detIds.begin();
0116     for (; it != detIds.end(); ++it) {
0117       if (payload->IsModuleHVOff(*it))
0118         detIds_HVOff.insert(*it);
0119       if (payload->IsModuleLVOff(*it))
0120         detIds_LVOff.insert(*it);
0121 
0122       if (detidMap.find(*it) == detidMap.end()) {
0123         std::vector<cond::Time_t> vec;
0124         detidMap[*it] = vec;
0125       }
0126 
0127       // for each module concerned by the IOV, add the time in an history vector
0128       detidMap[*it].push_back(vTime.back());
0129     }
0130 
0131     // fill list of channels Off at a given time
0132     iovMap_HVOff[vTime.back()] = detIds_HVOff;
0133     iovMap_LVOff[vTime.back()] = detIds_LVOff;
0134 
0135     /*std::vector<uint32_t>::const_iterator it = detIds.begin();
0136       for( ; it!=detIds.end(); ++it ) {
0137       std::cout << *it << std::endl;
0138       }*/
0139 
0140     ++iiov;
0141     ++niov;
0142   }
0143   vTime.push_back(endIov);  // used to compute last IOV duration
0144 
0145   edm::LogInfo("SiStripDetVOffPrinter") << "[SiStripDetVOffPrinter::" << __func__ << "] "
0146                                         << "Read " << niov << " IOVs from tag " << m_tagName
0147                                         << " corresponding to the specified time interval.\n"
0148                                         << ss.str();
0149 
0150   // Create a map of IOVs time_duration
0151   std::map<cond::Time_t, boost::posix_time::time_duration> mIOVsDuration;
0152   std::vector<cond::Time_t>::const_iterator itTime = ++vTime.begin();
0153   std::vector<cond::Time_t>::const_iterator itPreviousTime = vTime.begin();
0154   //std::vector< cond::Time_t >::const_iterator itLastTime = --vTime.end();
0155   for (; itTime != vTime.end(); ++itTime) {
0156     mIOVsDuration[*itPreviousTime] = cond::time::to_boost(*itTime) - cond::time::to_boost(*itPreviousTime);
0157     itPreviousTime = itTime;
0158   }
0159   boost::posix_time::time_duration time_period =
0160       cond::time::to_boost(*(--vTime.end())) - cond::time::to_boost(*(vTime.begin()));
0161 
0162   // debug
0163   /*for( itTime=vTime.begin(); itTime!=itLastTime; ++itTime ) {
0164     std::cout<<boost::posix_time::to_simple_string( cond::time::to_boost(*itTime) ) << "    " <<boost::posix_time::to_simple_string(mIOVsDuration[ *itTime ])<<std::endl;
0165   }*/
0166 
0167   // Print summary per module
0168   edm::LogInfo("SiStripDetVOffPrinter") << "[SiStripDetVOffPrinter::" << __func__ << "] " << detidMap.size()
0169                                         << " modules were Off at some point during the time interval";
0170   ss.str("");
0171 
0172   // Loop over detIds
0173   std::map<uint32_t, std::vector<cond::Time_t> >::const_iterator itMap = detidMap.begin();
0174   for (; itMap != detidMap.end(); ++itMap) {
0175     std::vector<cond::Time_t> vecTime = itMap->second;
0176 
0177     boost::posix_time::time_duration cumul_time_HVOff(0, 0, 0, 0);
0178     boost::posix_time::time_duration cumul_time_LVOff(0, 0, 0, 0);
0179     // Loop over IOVs
0180     std::vector<cond::Time_t>::const_iterator itTime = vecTime.begin();
0181     for (; itTime != vecTime.end(); ++itTime) {
0182       if (iovMap_HVOff[*itTime].find(itMap->first) != iovMap_HVOff[*itTime].end())
0183         cumul_time_HVOff += mIOVsDuration[*itTime];
0184       if (iovMap_LVOff[*itTime].find(itMap->first) != iovMap_LVOff[*itTime].end())
0185         cumul_time_LVOff += mIOVsDuration[*itTime];
0186     }
0187     ss << "detId " << itMap->first << " #IOVs: " << vecTime.size() << "  HVOff: " << cumul_time_HVOff << " "
0188        << cumul_time_HVOff.total_milliseconds() * 100.0 / time_period.total_milliseconds() << "% "
0189        << "  LVOff: " << cumul_time_LVOff << " "
0190        << cumul_time_LVOff.total_milliseconds() * 100.0 / time_period.total_milliseconds() << "%" << std::endl;
0191   }
0192 
0193   condDbSession.transaction().commit();
0194 
0195   if (!m_output.empty()) {
0196     std::ofstream fout;
0197     fout.open(m_output);
0198     fout << ss.str();
0199     fout.close();
0200   }
0201 }
0202 
0203 void SiStripDetVOffPrinter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0204   edm::ParameterSetDescription desc;
0205   desc.add<std::string>("conditionDatabase", "frontier://FrontierProd/CMS_CONDITIONS");
0206   desc.add<std::string>("tagName", "SiStripDetVOff_1hourDelay_v1_Validation");
0207   desc.add<std::string>("startTime", "2002-01-20 23:59:59.000");
0208   desc.add<std::string>("endTime", "2002-01-20 23:59:59.000");
0209   desc.add<std::string>("output", "PerModuleSummary.txt");
0210   desc.add<std::string>("connect", "");
0211 
0212   edm::ParameterSetDescription descDBParameters;
0213   descDBParameters.addUntracked<std::string>("authenticationPath", "");
0214   descDBParameters.addUntracked<int>("authenticationSystem", 0);
0215   descDBParameters.addUntracked<std::string>("security", "");
0216   descDBParameters.addUntracked<int>("messageLevel", 0);
0217 
0218   desc.add<edm::ParameterSetDescription>("DBParameters", descDBParameters);
0219   descriptions.add("siStripDetVOffPrinter", desc);
0220 }
0221 
0222 void SiStripDetVOffPrinter::endJob() {}
0223 
0224 DEFINE_FWK_MODULE(SiStripDetVOffPrinter);