CMS 3D CMS Logo

BeamSpotRcdPrinter.cc
Go to the documentation of this file.
6 
7 #include <iostream>
8 #include <fstream>
9 #include <sstream>
10 
15 
16 #include <TROOT.h>
17 #include <TSystem.h>
18 #include <TCanvas.h>
19 #include <TFile.h>
20 #include <TLegend.h>
21 #include <TGraph.h>
22 #include <TH1.h>
23 
24 namespace BSPrintUtils {
25  std::pair<unsigned int, unsigned int> unpack(cond::Time_t since) {
26  auto kLowMask = 0XFFFFFFFF;
27  auto run = (since >> 32);
28  auto lumi = (since & kLowMask);
29  return std::make_pair(run, lumi);
30  }
31 } // namespace BSPrintUtils
32 
34 public:
35  explicit BeamSpotRcdPrinter(const edm::ParameterSet& iConfig);
36  ~BeamSpotRcdPrinter() override;
37  void analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) override;
38  void endJob() override;
39  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
40 
41 private:
45 
46  // Manually specify the start/end time.
47  unsigned long long m_startTime;
48  unsigned long long m_endTime;
49  // Specify output text file name. Leave empty if do not want to dump beamspots in a file.
51 };
52 
54  : m_connectionPool(),
55  m_condDb(iConfig.getParameter<std::string>("conditionDatabase")),
56  m_tagName(iConfig.getParameter<std::string>("tagName")),
57  m_startTime(iConfig.getParameter<unsigned long long>("startIOV")),
58  m_endTime(iConfig.getParameter<unsigned long long>("endIOV")),
59  m_output(iConfig.getParameter<std::string>("output")) {
62 }
63 
65 
66 void BeamSpotRcdPrinter::analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) {
67  cond::Time_t startIov = m_startTime;
68  cond::Time_t endIov = m_endTime;
69  if (startIov > endIov)
70  throw cms::Exception("endTime must be greater than startTime!");
71  edm::LogInfo("BeamSpotRcdPrinter") << "[BeamSpotRcdPrinter::" << __func__ << "] "
72  << "Set start time " << startIov << "\n ... Set end time " << endIov;
73 
74  // open db session
75  edm::LogInfo("BeamSpotRcdPrinter") << "[BeamSpotRcdPrinter::" << __func__ << "] "
76  << "Query the condition database " << m_condDb;
78  condDbSession.transaction().start(true);
79 
80  std::stringstream ss;
81  // list of times with new IOVs within the time range
82  std::vector<cond::Time_t> vTime;
83 
84  // query the database
85  edm::LogInfo("BeamSpotRcdPrinter") << "[BeamSpotRcdPrinter::" << __func__ << "] "
86  << "Reading IOVs from tag " << m_tagName;
87  cond::persistency::IOVProxy iovProxy = condDbSession.readIov(m_tagName); // load all?
88  auto iovs = iovProxy.selectAll();
89  auto iiov = iovs.find(startIov);
90  auto eiov = iovs.find(endIov);
91  int niov = 0;
92  while (iiov != iovs.end() && (*iiov).since <= (*eiov).since) {
93  // convert cond::Time_t to seconds since epoch
94  if ((*iiov).since < startIov) {
95  vTime.push_back(startIov);
96  } else {
97  vTime.push_back((*iiov).since);
98  }
99  auto payload = condDbSession.fetchPayload<BeamSpotObjects>((*iiov).payloadId);
100  auto runLS = BSPrintUtils::unpack((*iiov).since);
101  // print IOVs summary
102  ss << runLS.first << "," << runLS.second << " (" << (*iiov).since << ")"
103  << " [hash: " << (*iiov).payloadId << "] \n"
104  << *payload << std::endl;
105 
106  ++iiov;
107  ++niov;
108  }
109 
110  vTime.push_back(endIov); // used to compute last IOV duration
111 
112  edm::LogInfo("BeamSpotRcdPrinter") << "[BeamSpotRcdPrinter::" << __func__ << "] "
113  << "Read " << niov << " IOVs from tag " << m_tagName
114  << " corresponding to the specified time interval.\n\n"
115  << ss.str();
116 
117  condDbSession.transaction().commit();
118 
119  if (!m_output.empty()) {
120  std::ofstream fout;
121  fout.open(m_output);
122  fout << ss.str();
123  fout.close();
124  }
125 }
126 
129  desc.add<std::string>("conditionDatabase", "frontier://FrontierProd/CMS_CONDITIONS");
130  desc.add<std::string>("tagName", "BeamSpotObjects_PCL_byLumi_v0_prompt");
131  desc.add<unsigned long long>("startIOV", 1406859487478481);
132  desc.add<unsigned long long>("endIOV", 1406876667347162);
133  desc.add<std::string>("output", "summary.txt");
134  desc.add<std::string>("connect", "");
135 
136  edm::ParameterSetDescription descDBParameters;
137  descDBParameters.addUntracked<std::string>("authenticationPath", "");
138  descDBParameters.addUntracked<int>("authenticationSystem", 0);
139  descDBParameters.addUntracked<std::string>("security", "");
140  descDBParameters.addUntracked<int>("messageLevel", 0);
141 
142  desc.add<edm::ParameterSetDescription>("DBParameters", descDBParameters);
143  descriptions.add("BeamSpotRcdPrinter", desc);
144 }
145 
147 
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
BeamSpotRcdPrinter(const edm::ParameterSet &iConfig)
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void start(bool readOnly=true)
Definition: Session.cc:18
const Time_t kLowMask(0xFFFFFFFF)
std::unique_ptr< T > fetchPayload(const cond::Hash &payloadHash)
Definition: Session.h:213
cond::persistency::ConnectionPool m_connectionPool
Transaction & transaction()
Definition: Session.cc:52
~BeamSpotRcdPrinter() override
void setParameters(const edm::ParameterSet &connectionPset)
unsigned long long Time_t
Definition: Time.h:14
unsigned long long m_startTime
Session createSession(const std::string &connectionString, bool writeCapable=false)
Iterator find(cond::Time_t time) const
Definition: IOVProxy.cc:101
unsigned long long m_endTime
IOVProxy readIov(const std::string &tag)
Definition: Session.cc:63
Log< level::Info, false > LogInfo
void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void endJob() override
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::pair< unsigned int, unsigned int > unpack(cond::Time_t since)