Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02: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 #include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h"
0013 #include "CondFormats/Common/interface/Time.h"
0014 #include "CondFormats/Common/interface/TimeConversions.h"
0015 
0016 #include <TROOT.h>
0017 #include <TSystem.h>
0018 #include <TCanvas.h>
0019 #include <TFile.h>
0020 #include <TLegend.h>
0021 #include <TGraph.h>
0022 #include <TH1.h>
0023 
0024 namespace BSPrintUtils {
0025   std::pair<unsigned int, unsigned int> unpack(cond::Time_t since) {
0026     auto kLowMask = 0XFFFFFFFF;
0027     auto run = (since >> 32);
0028     auto lumi = (since & kLowMask);
0029     return std::make_pair(run, lumi);
0030   }
0031 }  // namespace BSPrintUtils
0032 
0033 class BeamSpotRcdPrinter : public edm::one::EDAnalyzer<> {
0034 public:
0035   explicit BeamSpotRcdPrinter(const edm::ParameterSet& iConfig);
0036   ~BeamSpotRcdPrinter() override = default;
0037   void analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) override;
0038   void endJob() override;
0039   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0040 
0041 private:
0042   cond::persistency::ConnectionPool m_connectionPool;
0043   std::string m_condDb;
0044   std::string m_tagName;
0045 
0046   // Manually specify the start/end time.
0047   unsigned long long m_startTime;
0048   unsigned long long m_endTime;
0049   // Specify output text file name. Leave empty if do not want to dump beamspots in a file.
0050   std::string m_output;
0051   // decide if to print to screen the dump
0052   bool m_verbose;
0053 };
0054 
0055 BeamSpotRcdPrinter::BeamSpotRcdPrinter(const edm::ParameterSet& iConfig)
0056     : m_connectionPool(),
0057       m_condDb(iConfig.getParameter<std::string>("conditionDatabase")),
0058       m_tagName(iConfig.getParameter<std::string>("tagName")),
0059       m_startTime(iConfig.getParameter<unsigned long long>("startIOV")),
0060       m_endTime(iConfig.getParameter<unsigned long long>("endIOV")),
0061       m_output(iConfig.getParameter<std::string>("output")),
0062       m_verbose(iConfig.getParameter<bool>("verbose")) {
0063   m_connectionPool.setParameters(iConfig.getParameter<edm::ParameterSet>("DBParameters"));
0064   m_connectionPool.configure();
0065 }
0066 
0067 void BeamSpotRcdPrinter::analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) {
0068   cond::Time_t startIov = m_startTime;
0069   cond::Time_t endIov = m_endTime;
0070   if (startIov > endIov)
0071     throw cms::Exception("endTime must be greater than startTime!");
0072   edm::LogInfo("BeamSpotRcdPrinter") << "[BeamSpotRcdPrinter::" << __func__ << "] "
0073                                      << "Set start time " << startIov << "\n ... Set end time " << endIov;
0074 
0075   // open db session
0076   edm::LogInfo("BeamSpotRcdPrinter") << "[BeamSpotRcdPrinter::" << __func__ << "] "
0077                                      << "Query the condition database " << m_condDb;
0078   cond::persistency::Session condDbSession = m_connectionPool.createSession(m_condDb);
0079   condDbSession.transaction().start(true);
0080 
0081   std::stringstream ss;
0082   // list of times with new IOVs within the time range
0083   std::vector<cond::Time_t> vTime;
0084 
0085   // query the database
0086   edm::LogInfo("BeamSpotRcdPrinter") << "[BeamSpotRcdPrinter::" << __func__ << "] "
0087                                      << "Reading IOVs from tag " << m_tagName;
0088   cond::persistency::IOVProxy iovProxy = condDbSession.readIov(m_tagName);  // load all?
0089   auto iovs = iovProxy.selectAll();
0090   auto iiov = iovs.find(startIov);
0091   auto eiov = iovs.find(endIov);
0092   int niov = 0;
0093   while (iiov != iovs.end() && (*iiov).since <= (*eiov).since) {
0094     // convert cond::Time_t to seconds since epoch
0095     if ((*iiov).since < startIov) {
0096       vTime.push_back(startIov);
0097     } else {
0098       vTime.push_back((*iiov).since);
0099     }
0100     auto payload = condDbSession.fetchPayload<BeamSpotObjects>((*iiov).payloadId);
0101     auto runLS = BSPrintUtils::unpack((*iiov).since);
0102     // print IOVs summary
0103     ss << runLS.first << "," << runLS.second << " (" << (*iiov).since << ")"
0104        << " [hash: " << (*iiov).payloadId << "] \n"
0105        << *payload << std::endl;
0106 
0107     ++iiov;
0108     ++niov;
0109   }
0110 
0111   vTime.push_back(endIov);  // used to compute last IOV duration
0112 
0113   edm::LogInfo("BeamSpotRcdPrinter") << "[BeamSpotRcdPrinter::" << __func__ << "] "
0114                                      << "Read " << niov << " IOVs from tag " << m_tagName
0115                                      << " corresponding to the specified time interval.\n\n"
0116                                      << (m_verbose ? ss.str() : std::string{});
0117 
0118   condDbSession.transaction().commit();
0119 
0120   if (!m_output.empty()) {
0121     std::ofstream fout;
0122     fout.open(m_output);
0123     fout << ss.str();
0124     fout.close();
0125   }
0126 }
0127 
0128 void BeamSpotRcdPrinter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0129   edm::ParameterSetDescription desc;
0130   desc.add<std::string>("conditionDatabase", "frontier://FrontierProd/CMS_CONDITIONS");
0131   desc.add<std::string>("tagName", "BeamSpotObjects_PCL_byLumi_v0_prompt");
0132   desc.add<unsigned long long>("startIOV", 1406859487478481);
0133   desc.add<unsigned long long>("endIOV", 1406876667347162);
0134   desc.add<std::string>("output", "summary.txt")->setComment("ASCII file with the full tag dump information");
0135   desc.add<bool>("verbose", true)->setComment("print to screen the dump of all the payloads");
0136   desc.add<std::string>("connect", "");
0137 
0138   edm::ParameterSetDescription descDBParameters;
0139   descDBParameters.addUntracked<std::string>("authenticationPath", "");
0140   descDBParameters.addUntracked<int>("authenticationSystem", 0);
0141   descDBParameters.addUntracked<std::string>("security", "");
0142   descDBParameters.addUntracked<int>("messageLevel", 0);
0143 
0144   desc.add<edm::ParameterSetDescription>("DBParameters", descDBParameters);
0145   descriptions.add("BeamSpotRcdPrinter", desc);
0146 }
0147 
0148 void BeamSpotRcdPrinter::endJob() {}
0149 
0150 DEFINE_FWK_MODULE(BeamSpotRcdPrinter);