Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-11-23 04:28: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 <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;
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 };
0052 
0053 BeamSpotRcdPrinter::BeamSpotRcdPrinter(const edm::ParameterSet& iConfig)
0054     : m_connectionPool(),
0055       m_condDb(iConfig.getParameter<std::string>("conditionDatabase")),
0056       m_tagName(iConfig.getParameter<std::string>("tagName")),
0057       m_startTime(iConfig.getParameter<unsigned long long>("startIOV")),
0058       m_endTime(iConfig.getParameter<unsigned long long>("endIOV")),
0059       m_output(iConfig.getParameter<std::string>("output")) {
0060   m_connectionPool.setParameters(iConfig.getParameter<edm::ParameterSet>("DBParameters"));
0061   m_connectionPool.configure();
0062 }
0063 
0064 BeamSpotRcdPrinter::~BeamSpotRcdPrinter() {}
0065 
0066 void BeamSpotRcdPrinter::analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) {
0067   cond::Time_t startIov = m_startTime;
0068   cond::Time_t endIov = m_endTime;
0069   if (startIov > endIov)
0070     throw cms::Exception("endTime must be greater than startTime!");
0071   edm::LogInfo("BeamSpotRcdPrinter") << "[BeamSpotRcdPrinter::" << __func__ << "] "
0072                                      << "Set start time " << startIov << "\n ... Set end time " << endIov;
0073 
0074   // open db session
0075   edm::LogInfo("BeamSpotRcdPrinter") << "[BeamSpotRcdPrinter::" << __func__ << "] "
0076                                      << "Query the condition database " << m_condDb;
0077   cond::persistency::Session condDbSession = m_connectionPool.createSession(m_condDb);
0078   condDbSession.transaction().start(true);
0079 
0080   std::stringstream ss;
0081   // list of times with new IOVs within the time range
0082   std::vector<cond::Time_t> vTime;
0083 
0084   // query the database
0085   edm::LogInfo("BeamSpotRcdPrinter") << "[BeamSpotRcdPrinter::" << __func__ << "] "
0086                                      << "Reading IOVs from tag " << m_tagName;
0087   cond::persistency::IOVProxy iovProxy = condDbSession.readIov(m_tagName);  // load all?
0088   auto iovs = iovProxy.selectAll();
0089   auto iiov = iovs.find(startIov);
0090   auto eiov = iovs.find(endIov);
0091   int niov = 0;
0092   while (iiov != iovs.end() && (*iiov).since <= (*eiov).since) {
0093     // convert cond::Time_t to seconds since epoch
0094     if ((*iiov).since < startIov) {
0095       vTime.push_back(startIov);
0096     } else {
0097       vTime.push_back((*iiov).since);
0098     }
0099     auto payload = condDbSession.fetchPayload<BeamSpotObjects>((*iiov).payloadId);
0100     auto runLS = BSPrintUtils::unpack((*iiov).since);
0101     // print IOVs summary
0102     ss << runLS.first << "," << runLS.second << " (" << (*iiov).since << ")"
0103        << " [hash: " << (*iiov).payloadId << "] \n"
0104        << *payload << std::endl;
0105 
0106     ++iiov;
0107     ++niov;
0108   }
0109 
0110   vTime.push_back(endIov);  // used to compute last IOV duration
0111 
0112   edm::LogInfo("BeamSpotRcdPrinter") << "[BeamSpotRcdPrinter::" << __func__ << "] "
0113                                      << "Read " << niov << " IOVs from tag " << m_tagName
0114                                      << " corresponding to the specified time interval.\n\n"
0115                                      << ss.str();
0116 
0117   condDbSession.transaction().commit();
0118 
0119   if (!m_output.empty()) {
0120     std::ofstream fout;
0121     fout.open(m_output);
0122     fout << ss.str();
0123     fout.close();
0124   }
0125 }
0126 
0127 void BeamSpotRcdPrinter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0128   edm::ParameterSetDescription desc;
0129   desc.add<std::string>("conditionDatabase", "frontier://FrontierProd/CMS_CONDITIONS");
0130   desc.add<std::string>("tagName", "BeamSpotObjects_PCL_byLumi_v0_prompt");
0131   desc.add<unsigned long long>("startIOV", 1406859487478481);
0132   desc.add<unsigned long long>("endIOV", 1406876667347162);
0133   desc.add<std::string>("output", "summary.txt");
0134   desc.add<std::string>("connect", "");
0135 
0136   edm::ParameterSetDescription descDBParameters;
0137   descDBParameters.addUntracked<std::string>("authenticationPath", "");
0138   descDBParameters.addUntracked<int>("authenticationSystem", 0);
0139   descDBParameters.addUntracked<std::string>("security", "");
0140   descDBParameters.addUntracked<int>("messageLevel", 0);
0141 
0142   desc.add<edm::ParameterSetDescription>("DBParameters", descDBParameters);
0143   descriptions.add("BeamSpotRcdPrinter", desc);
0144 }
0145 
0146 void BeamSpotRcdPrinter::endJob() {}
0147 
0148 DEFINE_FWK_MODULE(BeamSpotRcdPrinter);