File indexing completed on 2024-04-06 12:26:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include "FWCore/Framework/interface/one/EDProducer.h"
0023 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0024 #include "FWCore/Framework/interface/Event.h"
0025 #include "FWCore/Framework/interface/LuminosityBlock.h"
0026 #include "FWCore/Framework/interface/Run.h"
0027 #include "DataFormats/Provenance/interface/BranchType.h"
0028 #include "DataFormats/Provenance/interface/LuminosityBlockID.h"
0029 #include "DataFormats/Luminosity/interface/LumiSummaryRunHeader.h"
0030 #include "DataFormats/Luminosity/interface/LumiSummary.h"
0031 #include "DataFormats/Luminosity/interface/LumiDetails.h"
0032 #include "FWCore/ServiceRegistry/interface/Service.h"
0033 #include "FWCore/Framework/interface/EventSetup.h"
0034
0035 #include "CoralBase/Exception.h"
0036 #include "CoralBase/AttributeList.h"
0037 #include "CoralBase/Attribute.h"
0038 #include "CoralBase/AttributeSpecification.h"
0039 #include "CoralBase/Exception.h"
0040 #include "CoralBase/Blob.h"
0041 #include "RelationalAccess/ISessionProxy.h"
0042 #include "RelationalAccess/ITransaction.h"
0043 #include "RelationalAccess/AccessMode.h"
0044 #include "RelationalAccess/ITypeConverter.h"
0045 #include "RelationalAccess/IQuery.h"
0046 #include "RelationalAccess/ICursor.h"
0047 #include "RelationalAccess/ISchema.h"
0048 #include "RelationalAccess/ITable.h"
0049
0050 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0051
0052 #include "RecoLuminosity/LumiProducer/interface/DBService.h"
0053 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
0054 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
0055 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.h"
0056 #include <iostream>
0057 #include <sstream>
0058 #include <string>
0059 #include <memory>
0060 #include <algorithm>
0061 #include <vector>
0062 #include <cstring>
0063 #include <iterator>
0064 #include <filesystem>
0065
0066 #include <boost/tokenizer.hpp>
0067 #include <xercesc/dom/DOM.hpp>
0068 #include <xercesc/parsers/XercesDOMParser.hpp>
0069 #include "Utilities/Xerces/interface/Xerces.h"
0070 #include <xercesc/util/XMLString.hpp>
0071
0072 namespace edm {
0073 class EventSetup;
0074 }
0075
0076
0077
0078
0079 class LumiProducer
0080 : public edm::one::EDProducer<edm::one::WatchRuns, edm::BeginLuminosityBlockProducer, edm::EndRunProducer> {
0081 public:
0082 struct HLTData {
0083 std::string pathname;
0084 unsigned int prescale;
0085 unsigned int l1passcount;
0086 unsigned int acceptcount;
0087 };
0088 struct L1Data {
0089 std::string bitname;
0090 unsigned int prescale;
0091 unsigned int ratecount;
0092 };
0093 struct PerRunData {
0094 std::string bitzeroname;
0095 std::map<std::string, unsigned int> TRGBitNameToIndex;
0096 std::map<std::string, unsigned int> HLTPathNameToIndex;
0097 std::vector<std::string> TRGBitNames;
0098 std::vector<std::string> HLTPathNames;
0099 };
0100 struct PerLSData {
0101 float lumivalue;
0102 float lumierror;
0103 short lumiquality;
0104 unsigned long long deadcount;
0105 unsigned int numorbit;
0106 unsigned int startorbit;
0107 unsigned int bitzerocount;
0108 unsigned int bitzeroprescale;
0109 std::vector<HLTData> hltdata;
0110 std::vector<L1Data> l1data;
0111 std::vector<std::pair<std::string, std::vector<float> > > bunchlumivalue;
0112 std::vector<std::pair<std::string, std::vector<float> > > bunchlumierror;
0113 std::vector<std::pair<std::string, std::vector<short> > > bunchlumiquality;
0114 std::vector<float> beam1intensity;
0115 std::vector<float> beam2intensity;
0116 };
0117
0118 explicit LumiProducer(const edm::ParameterSet&);
0119
0120 ~LumiProducer() override;
0121
0122 private:
0123 void produce(edm::Event&, const edm::EventSetup&) final;
0124
0125 void beginRun(edm::Run const&, edm::EventSetup const&) final;
0126
0127 void beginLuminosityBlockProduce(edm::LuminosityBlock& iLBlock, edm::EventSetup const& iSetup) final;
0128
0129 void endRun(edm::Run const&, edm::EventSetup const&) final;
0130 void endRunProduce(edm::Run&, edm::EventSetup const&) final;
0131
0132 bool fillLumi(edm::LuminosityBlock& iLBlock);
0133 void fillRunCache(const coral::ISchema& schema, unsigned int runnumber);
0134 void fillLSCache(unsigned int luminum);
0135 void writeProductsForEntry(edm::LuminosityBlock& iLBlock, unsigned int runnumber, unsigned int luminum);
0136 const std::string servletTranslation(const std::string& servlet) const;
0137 std::string x2s(const XMLCh* input) const;
0138 XMLCh* s2x(const std::string& input) const;
0139 std::string toParentString(const xercesc::DOMNode& nodeToConvert) const;
0140 unsigned long long getLumiDataId(const coral::ISchema& schema, unsigned int runnumber);
0141 unsigned long long getTrgDataId(const coral::ISchema& schema, unsigned int runnumber);
0142 unsigned long long getHltDataId(const coral::ISchema& schema, unsigned int runnumber);
0143 std::string getCurrentDataTag(const coral::ISchema& schema);
0144 std::string m_connectStr;
0145 std::string m_lumiversion;
0146 std::string m_siteconfpath;
0147 unsigned int m_cachedrun;
0148 unsigned long long m_cachedlumidataid;
0149 unsigned long long m_cachedtrgdataid;
0150 unsigned long long m_cachedhltdataid;
0151 PerRunData m_runcache;
0152 std::map<unsigned int, PerLSData> m_lscache;
0153 bool m_isNullRun;
0154 unsigned int m_cachesize;
0155 };
0156
0157
0158
0159
0160
0161 std::string LumiProducer::x2s(const XMLCh* toTranscode) const {
0162 std::string tmp(xercesc::XMLString::transcode(toTranscode));
0163 return tmp;
0164 }
0165
0166 XMLCh* LumiProducer::s2x(const std::string& temp) const {
0167 XMLCh* buff = xercesc::XMLString::transcode(temp.c_str());
0168 return buff;
0169 }
0170
0171 std::string LumiProducer::toParentString(const xercesc::DOMNode& nodeToConvert) const {
0172 std::ostringstream oss;
0173 xercesc::DOMNodeList* childList = nodeToConvert.getChildNodes();
0174
0175 unsigned int numNodes = childList->getLength();
0176 for (unsigned int i = 0; i < numNodes; ++i) {
0177 xercesc::DOMNode* childNode = childList->item(i);
0178 if (childNode->getNodeType() != xercesc::DOMNode::ELEMENT_NODE)
0179 continue;
0180 xercesc::DOMElement* child = static_cast<xercesc::DOMElement*>(childNode);
0181 xercesc::DOMNamedNodeMap* attributes = child->getAttributes();
0182 unsigned int numAttributes = attributes->getLength();
0183 for (unsigned int j = 0; j < numAttributes; ++j) {
0184 xercesc::DOMNode* attributeNode = attributes->item(j);
0185 if (attributeNode->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
0186 continue;
0187 xercesc::DOMAttr* attribute = static_cast<xercesc::DOMAttr*>(attributeNode);
0188
0189 oss << "(" << x2s(child->getTagName()) << x2s(attribute->getName()) << "=" << x2s(attribute->getValue()) << ")";
0190 }
0191 }
0192 return oss.str();
0193 }
0194
0195 const std::string LumiProducer::servletTranslation(const std::string& servlet) const {
0196 std::string frontierConnect;
0197 std::string realconnect;
0198 cms::concurrency::xercesInitialize();
0199 std::unique_ptr<xercesc::XercesDOMParser> parser(new xercesc::XercesDOMParser);
0200 try {
0201 parser->setValidationScheme(xercesc::XercesDOMParser::Val_Auto);
0202 parser->setDoNamespaces(false);
0203 parser->parse(m_siteconfpath.c_str());
0204 xercesc::DOMDocument* doc = parser->getDocument();
0205 if (!doc) {
0206 return "";
0207 }
0208
0209 xercesc::DOMNodeList* frontierConnectList = doc->getElementsByTagName(s2x("frontier-connect"));
0210 if (frontierConnectList->getLength() > 0) {
0211 xercesc::DOMElement* frontierConnectElement = static_cast<xercesc::DOMElement*>(frontierConnectList->item(0));
0212 frontierConnect = toParentString(*frontierConnectElement);
0213 }
0214
0215
0216 std::string::size_type nextparen = 0;
0217 std::string::size_type serverurl, lastslash;
0218 std::string complexstr = "";
0219 while ((serverurl = frontierConnect.find("(serverurl=", nextparen)) != std::string::npos) {
0220 realconnect.append(frontierConnect, nextparen, serverurl - nextparen);
0221 nextparen = frontierConnect.find(')', serverurl);
0222 lastslash = frontierConnect.rfind('/', nextparen);
0223 realconnect.append(frontierConnect, serverurl, lastslash - serverurl + 1);
0224 realconnect.append(servlet);
0225 }
0226 realconnect.append(frontierConnect, nextparen, frontierConnect.length() - nextparen);
0227 } catch (xercesc::DOMException& e) {
0228 }
0229 return realconnect;
0230 }
0231
0232 LumiProducer::LumiProducer::LumiProducer(const edm::ParameterSet& iConfig)
0233 : m_cachedrun(0), m_isNullRun(false), m_cachesize(0) {
0234
0235 produces<LumiSummaryRunHeader, edm::Transition::EndRun>();
0236 produces<LumiSummary, edm::Transition::BeginLuminosityBlock>();
0237 produces<LumiDetails, edm::Transition::BeginLuminosityBlock>();
0238
0239 std::string connectStr = iConfig.getParameter<std::string>("connect");
0240 m_cachesize = iConfig.getUntrackedParameter<unsigned int>("ncacheEntries", 5);
0241 m_lumiversion = iConfig.getUntrackedParameter<std::string>("lumiversion", "");
0242 const std::string fproto("frontier://");
0243
0244 if (connectStr.substr(0, fproto.length()) == fproto) {
0245 std::string::size_type startservlet = fproto.length();
0246 std::string::size_type endservlet = connectStr.find('(', startservlet);
0247 if (endservlet == std::string::npos) {
0248 endservlet = connectStr.rfind('/', connectStr.length());
0249 }
0250 std::string servlet = connectStr.substr(startservlet, endservlet - startservlet);
0251 if ((!servlet.empty()) && (servlet.find_first_of(":/)[]") == std::string::npos)) {
0252 if (servlet == "cms_conditions_data")
0253 servlet = "";
0254
0255 std::string siteconfpath = iConfig.getUntrackedParameter<std::string>("siteconfpath", "");
0256 if (siteconfpath.length() == 0) {
0257 std::string url = (std::filesystem::path("SITECONF") / std::filesystem::path("local") /
0258 std::filesystem::path("JobConfig") / std::filesystem::path("site-local-config.xml"))
0259 .string();
0260 char* tmp = std::getenv("CMS_PATH");
0261 if (tmp) {
0262 m_siteconfpath = (std::filesystem::path(tmp) / std::filesystem::path(url)).string();
0263 }
0264 } else {
0265 if (!std::filesystem::exists(std::filesystem::path(siteconfpath))) {
0266 throw cms::Exception("Non existing path ") << siteconfpath;
0267 }
0268 m_siteconfpath =
0269 (std::filesystem::path(siteconfpath) / std::filesystem::path("site-local-config.xml")).string();
0270 }
0271
0272 m_connectStr = fproto + servletTranslation(servlet) + connectStr.substr(endservlet);
0273 } else {
0274 m_connectStr = connectStr;
0275 }
0276 } else {
0277 m_connectStr = connectStr;
0278 }
0279
0280 }
0281
0282 LumiProducer::~LumiProducer() {}
0283
0284
0285
0286
0287 void LumiProducer::produce(edm::Event& e, const edm::EventSetup& iSetup) {}
0288 unsigned long long LumiProducer::getLumiDataId(const coral::ISchema& schema, unsigned int runnumber) {
0289
0290
0291
0292
0293 unsigned long long lumidataid = 0;
0294 coral::AttributeList bindVariables;
0295 bindVariables.extend("runnum", typeid(unsigned int));
0296 bindVariables["runnum"].data<unsigned int>() = runnumber;
0297 coral::AttributeList lumiidOutput;
0298 lumiidOutput.extend("lumidataid", typeid(unsigned long long));
0299 coral::IQuery* lumiQuery = schema.newQuery();
0300 lumiQuery->addToTableList(lumi::LumiNames::lumidataTableName());
0301 lumiQuery->addToOutputList("MAX(DATA_ID)", "lumidataid");
0302 lumiQuery->setCondition("RUNNUM=:runnum", bindVariables);
0303 lumiQuery->defineOutput(lumiidOutput);
0304 coral::ICursor& lumicursor = lumiQuery->execute();
0305 while (lumicursor.next()) {
0306 const coral::AttributeList& row = lumicursor.currentRow();
0307 if (!row["lumidataid"].isNull()) {
0308 lumidataid = row["lumidataid"].data<unsigned long long>();
0309 }
0310 }
0311 delete lumiQuery;
0312 return lumidataid;
0313 }
0314 unsigned long long LumiProducer::getTrgDataId(const coral::ISchema& schema, unsigned int runnumber) {
0315
0316
0317
0318 unsigned long long trgdataid = 0;
0319 coral::AttributeList bindVariables;
0320 bindVariables.extend("runnum", typeid(unsigned int));
0321 bindVariables["runnum"].data<unsigned int>() = runnumber;
0322 coral::AttributeList trgidOutput;
0323 trgidOutput.extend("trgdataid", typeid(unsigned long long));
0324 coral::IQuery* trgQuery = schema.newQuery();
0325 trgQuery->addToTableList(lumi::LumiNames::trgdataTableName());
0326 trgQuery->addToOutputList("MAX(DATA_ID)", "trgdataid");
0327 trgQuery->setCondition("RUNNUM=:runnum", bindVariables);
0328 trgQuery->defineOutput(trgidOutput);
0329 coral::ICursor& trgcursor = trgQuery->execute();
0330 while (trgcursor.next()) {
0331 const coral::AttributeList& row = trgcursor.currentRow();
0332 if (!row["trgdataid"].isNull()) {
0333 trgdataid = row["trgdataid"].data<unsigned long long>();
0334 }
0335 }
0336 delete trgQuery;
0337 return trgdataid;
0338 }
0339 unsigned long long LumiProducer::getHltDataId(const coral::ISchema& schema, unsigned int runnumber) {
0340
0341
0342
0343 unsigned long long hltdataid = 0;
0344 coral::AttributeList bindVariables;
0345 bindVariables.extend("runnum", typeid(unsigned int));
0346 bindVariables["runnum"].data<unsigned int>() = runnumber;
0347 coral::AttributeList hltidOutput;
0348 hltidOutput.extend("hltdataid", typeid(unsigned long long));
0349 coral::IQuery* hltQuery = schema.newQuery();
0350 hltQuery->addToTableList(lumi::LumiNames::hltdataTableName());
0351 hltQuery->addToOutputList("MAX(DATA_ID)", "hltdataid");
0352 hltQuery->setCondition("RUNNUM=:runnum", bindVariables);
0353 hltQuery->defineOutput(hltidOutput);
0354 coral::ICursor& hltcursor = hltQuery->execute();
0355 while (hltcursor.next()) {
0356 const coral::AttributeList& row = hltcursor.currentRow();
0357 if (!row["hltdataid"].isNull()) {
0358 hltdataid = row["hltdataid"].data<unsigned long long>();
0359 }
0360 }
0361 delete hltQuery;
0362 return hltdataid;
0363 }
0364
0365 std::string LumiProducer::getCurrentDataTag(const coral::ISchema& schema) {
0366
0367 std::string result;
0368 std::map<unsigned long long, std::string> alltags;
0369 coral::IQuery* tagQuery = schema.newQuery();
0370 tagQuery->addToTableList(lumi::LumiNames::tagsTableName());
0371 tagQuery->addToOutputList("TAGID");
0372 tagQuery->addToOutputList("TAGNAME");
0373 coral::AttributeList tagoutput;
0374 tagoutput.extend("TAGID", typeid(unsigned long long));
0375 tagoutput.extend("TAGNAME", typeid(std::string));
0376 tagQuery->defineOutput(tagoutput);
0377 coral::ICursor& tagcursor = tagQuery->execute();
0378 while (tagcursor.next()) {
0379 const coral::AttributeList& row = tagcursor.currentRow();
0380 unsigned long long tagid = row["TAGID"].data<unsigned long long>();
0381 const std::string tagname = row["TAGNAME"].data<std::string>();
0382 alltags.insert(std::make_pair(tagid, tagname));
0383 }
0384 delete tagQuery;
0385 unsigned long long maxid = 0;
0386 for (std::map<unsigned long long, std::string>::iterator it = alltags.begin(); it != alltags.end(); ++it) {
0387 if (it->first > maxid) {
0388 maxid = it->first;
0389 }
0390 }
0391 result = alltags[maxid];
0392 return result;
0393 }
0394
0395 void LumiProducer::beginRun(edm::Run const& run, edm::EventSetup const& iSetup) {
0396 unsigned int runnumber = run.run();
0397 if (m_cachedrun != runnumber) {
0398
0399 m_cachedrun = runnumber;
0400 edm::Service<lumi::service::DBService> mydbservice;
0401 if (!mydbservice.isAvailable()) {
0402 throw cms::Exception("Non existing service lumi::service::DBService");
0403 }
0404 auto session = mydbservice->connectReadOnly(m_connectStr);
0405 try {
0406 session->transaction().start(true);
0407 m_cachedlumidataid = getLumiDataId(session->nominalSchema(), runnumber);
0408 if (m_cachedlumidataid != 0) {
0409 m_cachedtrgdataid = getTrgDataId(session->nominalSchema(), runnumber);
0410 m_cachedhltdataid = getHltDataId(session->nominalSchema(), runnumber);
0411 fillRunCache(session->nominalSchema(), runnumber);
0412 } else {
0413 m_isNullRun = true;
0414 }
0415 session->transaction().commit();
0416 } catch (const coral::Exception& er) {
0417 session->transaction().rollback();
0418 throw cms::Exception("DatabaseError ") << er.what();
0419 }
0420 }
0421
0422 }
0423
0424 void LumiProducer::beginLuminosityBlockProduce(edm::LuminosityBlock& iLBlock, edm::EventSetup const& iSetup) {
0425 unsigned int runnumber = iLBlock.run();
0426 unsigned int luminum = iLBlock.luminosityBlock();
0427
0428
0429 if (m_isNullRun) {
0430 iLBlock.put(std::make_unique<LumiSummary>());
0431 iLBlock.put(std::make_unique<LumiDetails>());
0432 return;
0433 }
0434 if (m_lscache.find(luminum) == m_lscache.end()) {
0435
0436 fillLSCache(luminum);
0437 }
0438
0439 writeProductsForEntry(iLBlock, runnumber, luminum);
0440 }
0441 void LumiProducer::endRun(edm::Run const& run, edm::EventSetup const& iSetup) {}
0442 void LumiProducer::endRunProduce(edm::Run& run, edm::EventSetup const& iSetup) {
0443 auto lsrh = std::make_unique<LumiSummaryRunHeader>();
0444 lsrh->swapL1Names(m_runcache.TRGBitNames);
0445 lsrh->swapHLTNames(m_runcache.HLTPathNames);
0446 run.put(std::move(lsrh));
0447 m_runcache.TRGBitNameToIndex.clear();
0448 m_runcache.HLTPathNameToIndex.clear();
0449 }
0450 void LumiProducer::fillRunCache(const coral::ISchema& schema, unsigned int runnumber) {
0451 if (m_lumiversion.empty()) {
0452 m_lumiversion = getCurrentDataTag(schema);
0453 }
0454 std::cout << "lumi tag version 2 " << m_lumiversion << std::endl;
0455 if (m_cachedtrgdataid != 0) {
0456 coral::AttributeList trgBindVariables;
0457 trgBindVariables.extend("trgdataid", typeid(unsigned long long));
0458 trgBindVariables["trgdataid"].data<unsigned long long>() = m_cachedtrgdataid;
0459
0460 coral::AttributeList trgOutput;
0461 trgOutput.extend("bitzeroname", typeid(std::string));
0462 trgOutput.extend("bitnameclob", typeid(std::string));
0463 coral::IQuery* trgQuery = schema.newQuery();
0464 trgQuery->addToTableList(lumi::LumiNames::trgdataTableName());
0465 trgQuery->addToOutputList("BITZERONAME");
0466 trgQuery->addToOutputList("BITNAMECLOB");
0467 trgQuery->setCondition("DATA_ID=:trgdataid", trgBindVariables);
0468 trgQuery->defineOutput(trgOutput);
0469 coral::ICursor& trgcursor = trgQuery->execute();
0470 while (trgcursor.next()) {
0471 const coral::AttributeList& row = trgcursor.currentRow();
0472 m_runcache.bitzeroname = row["bitzeroname"].data<std::string>();
0473
0474 std::string bitnames = row["bitnameclob"].data<std::string>();
0475 boost::char_separator<char> sep(",");
0476 boost::tokenizer<boost::char_separator<char> > tokens(bitnames, sep);
0477 for (boost::tokenizer<boost::char_separator<char> >::iterator tok_it = tokens.begin(); tok_it != tokens.end();
0478 ++tok_it) {
0479 m_runcache.TRGBitNames.push_back(*tok_it);
0480 }
0481 for (unsigned int i = 0; i < m_runcache.TRGBitNames.size(); ++i) {
0482 m_runcache.TRGBitNameToIndex.insert(std::make_pair(m_runcache.TRGBitNames.at(i), i));
0483 }
0484 }
0485 delete trgQuery;
0486 }
0487 if (m_cachedhltdataid != 0) {
0488
0489
0490
0491 coral::AttributeList hltBindVariables;
0492 hltBindVariables.extend("hltdataid", typeid(unsigned long long));
0493 hltBindVariables["hltdataid"].data<unsigned long long>() = m_cachedhltdataid;
0494 coral::AttributeList hltOutput;
0495 hltOutput.extend("PATHNAMECLOB", typeid(std::string));
0496 coral::IQuery* hltQuery = schema.newQuery();
0497 hltQuery->addToTableList(lumi::LumiNames::hltdataTableName());
0498 hltQuery->addToOutputList("PATHNAMECLOB");
0499 hltQuery->setCondition("DATA_ID=:hltdataid", hltBindVariables);
0500 hltQuery->defineOutput(hltOutput);
0501 coral::ICursor& hltcursor = hltQuery->execute();
0502 while (hltcursor.next()) {
0503 const coral::AttributeList& row = hltcursor.currentRow();
0504 std::string pathnames = row["PATHNAMECLOB"].data<std::string>();
0505 boost::char_separator<char> sep(",");
0506 boost::tokenizer<boost::char_separator<char> > tokens(pathnames, sep);
0507 for (boost::tokenizer<boost::char_separator<char> >::iterator tok_it = tokens.begin(); tok_it != tokens.end();
0508 ++tok_it) {
0509 m_runcache.HLTPathNames.push_back(*tok_it);
0510 }
0511 for (unsigned int i = 0; i < m_runcache.HLTPathNames.size(); ++i) {
0512 m_runcache.HLTPathNameToIndex.insert(std::make_pair(m_runcache.HLTPathNames.at(i), i));
0513 }
0514 }
0515 delete hltQuery;
0516 }
0517 }
0518 void LumiProducer::fillLSCache(unsigned int luminum) {
0519
0520 if (m_isNullRun)
0521 return;
0522 m_lscache.clear();
0523 for (unsigned int n = luminum; n < luminum + m_cachesize; ++n) {
0524 PerLSData l;
0525 l.hltdata.reserve(250);
0526 l.l1data.reserve(192);
0527 l.bunchlumivalue.reserve(5);
0528 l.bunchlumierror.reserve(5);
0529 l.bunchlumiquality.reserve(5);
0530 l.beam1intensity.resize(3564, 0.0);
0531 l.beam2intensity.resize(3564, 0.0);
0532 m_lscache.insert(std::make_pair(n, l));
0533 }
0534
0535
0536
0537
0538 edm::Service<lumi::service::DBService> mydbservice;
0539 if (!mydbservice.isAvailable()) {
0540 throw cms::Exception("Non existing service lumi::service::DBService");
0541 }
0542 auto session = mydbservice->connectReadOnly(m_connectStr);
0543 try {
0544 session->transaction().start(true);
0545 coral::ISchema& schema = session->nominalSchema();
0546 coral::AttributeList lumisummaryBindVariables;
0547 lumisummaryBindVariables.extend("lsmin", typeid(unsigned int));
0548 lumisummaryBindVariables.extend("lsmax", typeid(unsigned int));
0549 lumisummaryBindVariables.extend("lumidataid", typeid(unsigned long long));
0550 lumisummaryBindVariables["lumidataid"].data<unsigned long long>() = m_cachedlumidataid;
0551 lumisummaryBindVariables["lsmin"].data<unsigned int>() = luminum;
0552 lumisummaryBindVariables["lsmax"].data<unsigned int>() = luminum + m_cachesize;
0553 coral::AttributeList lumisummaryOutput;
0554 lumisummaryOutput.extend("CMSLSNUM", typeid(unsigned int));
0555 lumisummaryOutput.extend("INSTLUMI", typeid(float));
0556 lumisummaryOutput.extend("STARTORBIT", typeid(unsigned int));
0557 lumisummaryOutput.extend("NUMORBIT", typeid(unsigned int));
0558 lumisummaryOutput.extend("CMSBXINDEXBLOB", typeid(coral::Blob));
0559 lumisummaryOutput.extend("BEAMINTENSITYBLOB_1", typeid(coral::Blob));
0560 lumisummaryOutput.extend("BEAMINTENSITYBLOB_2", typeid(coral::Blob));
0561 lumisummaryOutput.extend("BXLUMIVALUE_OCC1", typeid(coral::Blob));
0562 lumisummaryOutput.extend("BXLUMIVALUE_OCC2", typeid(coral::Blob));
0563 lumisummaryOutput.extend("BXLUMIVALUE_ET", typeid(coral::Blob));
0564 coral::IQuery* lumisummaryQuery = schema.newQuery();
0565 lumisummaryQuery->addToTableList(lumi::LumiNames::lumisummaryv2TableName());
0566 lumisummaryQuery->addToOutputList("CMSLSNUM");
0567 lumisummaryQuery->addToOutputList("INSTLUMI");
0568 lumisummaryQuery->addToOutputList("STARTORBIT");
0569 lumisummaryQuery->addToOutputList("NUMORBIT");
0570 lumisummaryQuery->addToOutputList("CMSBXINDEXBLOB");
0571 lumisummaryQuery->addToOutputList("BEAMINTENSITYBLOB_1");
0572 lumisummaryQuery->addToOutputList("BEAMINTENSITYBLOB_2");
0573 lumisummaryQuery->addToOutputList("BXLUMIVALUE_OCC1");
0574 lumisummaryQuery->addToOutputList("BXLUMIVALUE_OCC2");
0575 lumisummaryQuery->addToOutputList("BXLUMIVALUE_ET");
0576 lumisummaryQuery->setCondition("CMSLSNUM>=:lsmin AND CMSLSNUM<:lsmax AND DATA_ID=:lumidataid",
0577 lumisummaryBindVariables);
0578 lumisummaryQuery->defineOutput(lumisummaryOutput);
0579 coral::ICursor& lumisummarycursor = lumisummaryQuery->execute();
0580 unsigned int rowcounter = 0;
0581 while (lumisummarycursor.next()) {
0582 const coral::AttributeList& row = lumisummarycursor.currentRow();
0583 unsigned int cmslsnum = row["CMSLSNUM"].data<unsigned int>();
0584
0585 PerLSData& lsdata = m_lscache[cmslsnum];
0586 lsdata.lumivalue = row["INSTLUMI"].data<float>();
0587 lsdata.lumierror = 0.0;
0588 lsdata.lumiquality = 0;
0589 lsdata.startorbit = row["STARTORBIT"].data<unsigned int>();
0590 lsdata.numorbit = row["NUMORBIT"].data<unsigned int>();
0591
0592 if (!row["CMSBXINDEXBLOB"].isNull() && !row["BXLUMIVALUE_OCC1"].isNull()) {
0593 const coral::Blob& bxindexBlob = row["CMSBXINDEXBLOB"].data<coral::Blob>();
0594 const void* bxindex_StartAddress = bxindexBlob.startingAddress();
0595 short* bxindex = (short*)::malloc(bxindexBlob.size());
0596 const coral::Blob& beam1intensityBlob = row["BEAMINTENSITYBLOB_1"].data<coral::Blob>();
0597 const void* beam1intensityBlob_StartAddress = beam1intensityBlob.startingAddress();
0598 float* beam1intensity = (float*)::malloc(beam1intensityBlob.size());
0599 const coral::Blob& beam2intensityBlob = row["BEAMINTENSITYBLOB_2"].data<coral::Blob>();
0600 const void* beam2intensityBlob_StartAddress = beam2intensityBlob.startingAddress();
0601 float* beam2intensity = (float*)::malloc(beam2intensityBlob.size());
0602 std::memmove(bxindex, bxindex_StartAddress, bxindexBlob.size());
0603 std::memmove(beam1intensity, beam1intensityBlob_StartAddress, beam1intensityBlob.size());
0604 std::memmove(beam2intensity, beam2intensityBlob_StartAddress, beam2intensityBlob.size());
0605
0606 unsigned int iMax = bxindexBlob.size() / sizeof(short);
0607 unsigned int lsb1Max = lsdata.beam1intensity.size();
0608 unsigned int lsb2Max = lsdata.beam2intensity.size();
0609 unsigned int ib1Max = beam1intensityBlob.size() / sizeof(float);
0610 unsigned int ib2Max = beam2intensityBlob.size() / sizeof(float);
0611 for (unsigned int i = 0; i < iMax; ++i) {
0612 unsigned int idx = bxindex[i];
0613 if (ib1Max > i && lsb1Max > idx) {
0614 lsdata.beam1intensity.at(idx) = beam1intensity[i];
0615 }
0616 if (ib2Max > i && lsb2Max > idx) {
0617 lsdata.beam2intensity.at(idx) = beam2intensity[i];
0618 }
0619 }
0620 ::free(bxindex);
0621 ::free(beam1intensity);
0622 ::free(beam2intensity);
0623
0624 const coral::Blob& bxlumivalBlob_occ1 = row["BXLUMIVALUE_OCC1"].data<coral::Blob>();
0625 const void* bxlumival_occ1_StartAddress = bxlumivalBlob_occ1.startingAddress();
0626 float* bxlumival_occ1 = (float*)::malloc(bxlumivalBlob_occ1.size());
0627 std::memmove(bxlumival_occ1, bxlumival_occ1_StartAddress, bxlumivalBlob_occ1.size());
0628 std::vector<float> bxlumivalVec_occ1(bxlumival_occ1,
0629 bxlumival_occ1 + bxlumivalBlob_occ1.size() / sizeof(float));
0630 ::free(bxlumival_occ1);
0631 lsdata.bunchlumivalue.push_back(std::make_pair(std::string("OCC1"), bxlumivalVec_occ1));
0632 lsdata.bunchlumierror.push_back(std::make_pair(std::string("OCC1"), std::vector<float>(3564)));
0633 lsdata.bunchlumiquality.push_back(std::make_pair(std::string("OCC1"), std::vector<short>(3564)));
0634 const coral::Blob& bxlumivalBlob_occ2 = row["BXLUMIVALUE_OCC2"].data<coral::Blob>();
0635 const void* bxlumival_occ2_StartAddress = bxlumivalBlob_occ2.startingAddress();
0636 float* bxlumival_occ2 = (float*)::malloc(bxlumivalBlob_occ2.size());
0637 std::memmove(bxlumival_occ2, bxlumival_occ2_StartAddress, bxlumivalBlob_occ2.size());
0638 std::vector<float> bxlumivalVec_occ2(bxlumival_occ2,
0639 bxlumival_occ2 + bxlumivalBlob_occ1.size() / sizeof(float));
0640 ::free(bxlumival_occ2);
0641 lsdata.bunchlumivalue.push_back(std::make_pair(std::string("OCC2"), bxlumivalVec_occ2));
0642 lsdata.bunchlumierror.push_back(std::make_pair(std::string("OCC2"), std::vector<float>(3564)));
0643 lsdata.bunchlumiquality.push_back(std::make_pair(std::string("OCC2"), std::vector<short>(3564)));
0644
0645 const coral::Blob& bxlumivalBlob_et = row["BXLUMIVALUE_ET"].data<coral::Blob>();
0646 const void* bxlumival_et_StartAddress = bxlumivalBlob_et.startingAddress();
0647 float* bxlumival_et = (float*)::malloc(bxlumivalBlob_et.size());
0648 std::memmove(bxlumival_et, bxlumival_et_StartAddress, bxlumivalBlob_et.size());
0649 std::vector<float> bxlumivalVec_et(bxlumival_et, bxlumival_et + bxlumivalBlob_et.size() / sizeof(float));
0650 ::free(bxlumival_et);
0651 lsdata.bunchlumivalue.push_back(std::make_pair(std::string("ET"), bxlumivalVec_et));
0652 lsdata.bunchlumierror.push_back(std::make_pair(std::string("ET"), std::vector<float>(3564)));
0653 lsdata.bunchlumiquality.push_back(std::make_pair(std::string("ET"), std::vector<short>(3564)));
0654 }
0655 ++rowcounter;
0656 }
0657 if (rowcounter == 0) {
0658 m_isNullRun = true;
0659 return;
0660 }
0661 delete lumisummaryQuery;
0662
0663
0664
0665
0666 coral::AttributeList trgBindVariables;
0667 trgBindVariables.extend("lsmin", typeid(unsigned int));
0668 trgBindVariables.extend("lsmax", typeid(unsigned int));
0669 trgBindVariables.extend("trgdataid", typeid(unsigned long long));
0670 trgBindVariables["lsmin"].data<unsigned int>() = luminum;
0671 trgBindVariables["lsmax"].data<unsigned int>() = luminum + m_cachesize;
0672 trgBindVariables["trgdataid"].data<unsigned long long>() = m_cachedtrgdataid;
0673 coral::AttributeList trgOutput;
0674 trgOutput.extend("CMSLSNUM", typeid(unsigned int));
0675 trgOutput.extend("DEADTIMECOUNT", typeid(unsigned long long));
0676 trgOutput.extend("BITZEROCOUNT", typeid(unsigned int));
0677 trgOutput.extend("BITZEROPRESCALE", typeid(unsigned int));
0678 trgOutput.extend("PRESCALEBLOB", typeid(coral::Blob));
0679 trgOutput.extend("TRGCOUNTBLOB", typeid(coral::Blob));
0680
0681 coral::IQuery* trgQuery = schema.newQuery();
0682 trgQuery->addToTableList(lumi::LumiNames::lstrgTableName());
0683 trgQuery->addToOutputList("CMSLSNUM");
0684 trgQuery->addToOutputList("DEADTIMECOUNT");
0685 trgQuery->addToOutputList("BITZEROCOUNT");
0686 trgQuery->addToOutputList("BITZEROPRESCALE");
0687 trgQuery->addToOutputList("PRESCALEBLOB");
0688 trgQuery->addToOutputList("TRGCOUNTBLOB");
0689 trgQuery->setCondition("CMSLSNUM>=:lsmin AND CMSLSNUM<:lsmax AND DATA_ID=:trgdataid", trgBindVariables);
0690 trgQuery->defineOutput(trgOutput);
0691 coral::ICursor& trgcursor = trgQuery->execute();
0692 while (trgcursor.next()) {
0693 const coral::AttributeList& row = trgcursor.currentRow();
0694 unsigned int cmslsnum = row["CMSLSNUM"].data<unsigned int>();
0695 PerLSData& lsdata = m_lscache[cmslsnum];
0696 lsdata.deadcount = row["DEADTIMECOUNT"].data<unsigned long long>();
0697 lsdata.bitzerocount = row["BITZEROCOUNT"].data<unsigned int>();
0698 lsdata.bitzeroprescale = row["BITZEROPRESCALE"].data<unsigned int>();
0699 if (!row["PRESCALEBLOB"].isNull()) {
0700 const coral::Blob& prescaleblob = row["PRESCALEBLOB"].data<coral::Blob>();
0701 const void* prescaleblob_StartAddress = prescaleblob.startingAddress();
0702 unsigned int* prescales = (unsigned int*)::malloc(prescaleblob.size());
0703 std::memmove(prescales, prescaleblob_StartAddress, prescaleblob.size());
0704 const coral::Blob& trgcountblob = row["TRGCOUNTBLOB"].data<coral::Blob>();
0705 const void* trgcountblob_StartAddress = trgcountblob.startingAddress();
0706 unsigned int* trgcounts = (unsigned int*)::malloc(trgcountblob.size());
0707 std::memmove(trgcounts, trgcountblob_StartAddress, trgcountblob.size());
0708 for (unsigned int i = 0; i < trgcountblob.size() / sizeof(unsigned int); ++i) {
0709 L1Data l1tmp;
0710 l1tmp.bitname = m_runcache.TRGBitNames[i];
0711 l1tmp.prescale = prescales[i];
0712 l1tmp.ratecount = trgcounts[i];
0713 lsdata.l1data.push_back(l1tmp);
0714 }
0715 ::free(prescales);
0716 ::free(trgcounts);
0717 }
0718 }
0719 delete trgQuery;
0720
0721
0722
0723 coral::AttributeList hltBindVariables;
0724 hltBindVariables.extend("lsmin", typeid(unsigned int));
0725 hltBindVariables.extend("lsmax", typeid(unsigned int));
0726 hltBindVariables.extend("hltdataid", typeid(unsigned long long));
0727 hltBindVariables["lsmin"].data<unsigned int>() = luminum;
0728 hltBindVariables["lsmax"].data<unsigned int>() = luminum + m_cachesize;
0729 hltBindVariables["hltdataid"].data<unsigned long long>() = m_cachedhltdataid;
0730 coral::AttributeList hltOutput;
0731 hltOutput.extend("CMSLSNUM", typeid(unsigned int));
0732 hltOutput.extend("HLTCOUNTBLOB", typeid(coral::Blob));
0733 hltOutput.extend("HLTACCEPTBLOB", typeid(coral::Blob));
0734 hltOutput.extend("PRESCALEBLOB", typeid(coral::Blob));
0735 coral::IQuery* hltQuery = schema.newQuery();
0736 hltQuery->addToTableList(lumi::LumiNames::lshltTableName());
0737 hltQuery->addToOutputList("CMSLSNUM");
0738 hltQuery->addToOutputList("HLTCOUNTBLOB");
0739 hltQuery->addToOutputList("HLTACCEPTBLOB");
0740 hltQuery->addToOutputList("PRESCALEBLOB");
0741 hltQuery->setCondition("CMSLSNUM>=:lsmin AND CMSLSNUM<:lsmax AND DATA_ID=:hltdataid", hltBindVariables);
0742 hltQuery->defineOutput(hltOutput);
0743 coral::ICursor& hltcursor = hltQuery->execute();
0744 while (hltcursor.next()) {
0745 const coral::AttributeList& row = hltcursor.currentRow();
0746 unsigned int cmslsnum = row["CMSLSNUM"].data<unsigned int>();
0747 PerLSData& lsdata = m_lscache[cmslsnum];
0748 if (!row["PRESCALEBLOB"].isNull()) {
0749 const coral::Blob& hltprescaleblob = row["PRESCALEBLOB"].data<coral::Blob>();
0750 const void* hltprescaleblob_StartAddress = hltprescaleblob.startingAddress();
0751 unsigned int* hltprescales = (unsigned int*)::malloc(hltprescaleblob.size());
0752 std::memmove(hltprescales, hltprescaleblob_StartAddress, hltprescaleblob.size());
0753 const coral::Blob& hltcountblob = row["HLTCOUNTBLOB"].data<coral::Blob>();
0754 const void* hltcountblob_StartAddress = hltcountblob.startingAddress();
0755 unsigned int* hltcounts = (unsigned int*)::malloc(hltcountblob.size());
0756 std::memmove(hltcounts, hltcountblob_StartAddress, hltcountblob.size());
0757 const coral::Blob& hltacceptblob = row["HLTACCEPTBLOB"].data<coral::Blob>();
0758 const void* hltacceptblob_StartAddress = hltacceptblob.startingAddress();
0759 unsigned int* hltaccepts = (unsigned int*)::malloc(hltacceptblob.size());
0760 std::memmove(hltaccepts, hltacceptblob_StartAddress, hltacceptblob.size());
0761 unsigned int nhltaccepts = hltacceptblob.size() / sizeof(unsigned int);
0762 if (nhltaccepts > 0 && m_runcache.HLTPathNames.empty()) {
0763 edm::LogWarning("CorruptOrMissingHLTData")
0764 << "Got " << nhltaccepts << " hltaccepts, but the run chache is empty. hltdata will not be written";
0765 break;
0766 }
0767
0768 for (unsigned int i = 0; i < hltacceptblob.size() / sizeof(unsigned int); ++i) {
0769 HLTData hlttmp;
0770 hlttmp.pathname = m_runcache.HLTPathNames[i];
0771 hlttmp.prescale = hltprescales[i];
0772 hlttmp.l1passcount = hltcounts[i];
0773 hlttmp.acceptcount = hltaccepts[i];
0774 lsdata.hltdata.push_back(hlttmp);
0775 }
0776 ::free(hltprescales);
0777 ::free(hltcounts);
0778 ::free(hltaccepts);
0779 }
0780 }
0781 delete hltQuery;
0782 session->transaction().commit();
0783 } catch (const coral::Exception& er) {
0784 session->transaction().rollback();
0785 throw cms::Exception("DatabaseError ") << er.what();
0786 }
0787 }
0788 void LumiProducer::writeProductsForEntry(edm::LuminosityBlock& iLBlock, unsigned int runnumber, unsigned int luminum) {
0789
0790 auto pIn1 = std::make_unique<LumiSummary>();
0791 auto pIn2 = std::make_unique<LumiDetails>();
0792 if (m_isNullRun) {
0793 pIn1->setLumiVersion("-1");
0794 pIn2->setLumiVersion("-1");
0795 iLBlock.put(std::move(pIn1));
0796 iLBlock.put(std::move(pIn2));
0797 return;
0798 }
0799 PerLSData& lsdata = m_lscache[luminum];
0800 pIn1->setLumiData(lsdata.lumivalue, lsdata.lumierror, lsdata.lumiquality);
0801 pIn1->setDeadCount(lsdata.deadcount);
0802 if (!lsdata.l1data.empty()) {
0803
0804
0805
0806 pIn1->setBitZeroCount(lsdata.bitzerocount * lsdata.bitzeroprescale);
0807 }
0808 pIn1->setlsnumber(luminum);
0809 pIn1->setOrbitData(lsdata.startorbit, lsdata.numorbit);
0810 std::vector<LumiSummary::L1> l1temp;
0811 for (std::vector<L1Data>::iterator it = lsdata.l1data.begin(); it != lsdata.l1data.end(); ++it) {
0812 LumiSummary::L1 trgtmp;
0813 trgtmp.triggernameidx = m_runcache.TRGBitNameToIndex[it->bitname];
0814 trgtmp.prescale = it->prescale;
0815 l1temp.push_back(trgtmp);
0816 }
0817 std::vector<LumiSummary::HLT> hlttemp;
0818 for (std::vector<HLTData>::iterator it = lsdata.hltdata.begin(); it != lsdata.hltdata.end(); ++it) {
0819 LumiSummary::HLT hlttmp;
0820 hlttmp.pathnameidx = m_runcache.HLTPathNameToIndex[it->pathname];
0821 ;
0822 hlttmp.prescale = it->prescale;
0823 hlttemp.push_back(hlttmp);
0824 }
0825 pIn1->swapL1Data(l1temp);
0826 pIn1->swapHLTData(hlttemp);
0827 pIn1->setLumiVersion(m_lumiversion);
0828 pIn2->fillBeamIntensities(lsdata.beam1intensity, lsdata.beam2intensity);
0829 for (unsigned int i = 0; i < lsdata.bunchlumivalue.size(); ++i) {
0830 std::string algoname = lsdata.bunchlumivalue[i].first;
0831 if (algoname == "OCC1") {
0832 pIn2->fill(LumiDetails::kOCC1,
0833 lsdata.bunchlumivalue[i].second,
0834 lsdata.bunchlumierror[i].second,
0835 lsdata.bunchlumiquality[i].second);
0836 } else if (algoname == "OCC2") {
0837 pIn2->fill(LumiDetails::kOCC2,
0838 lsdata.bunchlumivalue[i].second,
0839 lsdata.bunchlumierror[i].second,
0840 lsdata.bunchlumiquality[i].second);
0841 } else if (algoname == "ET") {
0842 pIn2->fill(LumiDetails::kET,
0843 lsdata.bunchlumivalue[i].second,
0844 lsdata.bunchlumierror[i].second,
0845 lsdata.bunchlumiquality[i].second);
0846 } else if (algoname == "PLT") {
0847 pIn2->fill(LumiDetails::kPLT,
0848 lsdata.bunchlumivalue[i].second,
0849 lsdata.bunchlumierror[i].second,
0850 lsdata.bunchlumiquality[i].second);
0851 }
0852 }
0853 pIn2->setLumiVersion(m_lumiversion);
0854 iLBlock.put(std::move(pIn1));
0855 iLBlock.put(std::move(pIn2));
0856 }
0857 #include "FWCore/Framework/interface/MakerMacros.h"
0858 DEFINE_FWK_MODULE(LumiProducer);