CMS 3D CMS Logo

LumiProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: LumiProducer
4 // Class: LumiProducer
5 //
16 //
17 // Original Author: Valerie Halyo
18 // David Dagenhart
19 // Zhen Xie
20 // Created: Tue Jun 12 00:47:28 CEST 2007
21 
34 
35 #include "CoralBase/Exception.h"
36 #include "CoralBase/AttributeList.h"
37 #include "CoralBase/Attribute.h"
38 #include "CoralBase/AttributeSpecification.h"
39 #include "CoralBase/Exception.h"
40 #include "CoralBase/Blob.h"
41 #include "RelationalAccess/ISessionProxy.h"
42 #include "RelationalAccess/ITransaction.h"
43 #include "RelationalAccess/AccessMode.h"
44 #include "RelationalAccess/ITypeConverter.h"
45 #include "RelationalAccess/IQuery.h"
46 #include "RelationalAccess/ICursor.h"
47 #include "RelationalAccess/ISchema.h"
48 #include "RelationalAccess/ITable.h"
49 
51 
56 #include <iostream>
57 #include <sstream>
58 #include <string>
59 #include <memory>
60 #include <algorithm>
61 #include <vector>
62 #include <cstring>
63 #include <iterator>
64 #include <filesystem>
65 
66 #include <boost/tokenizer.hpp>
67 #include <xercesc/dom/DOM.hpp>
68 #include <xercesc/parsers/XercesDOMParser.hpp>
70 #include <xercesc/util/XMLString.hpp>
71 
72 namespace edm {
73  class EventSetup;
74 }
75 
76 //
77 // class declaration
78 //
80  : public edm::one::EDProducer<edm::one::WatchRuns, edm::BeginLuminosityBlockProducer, edm::EndRunProducer> {
81 public:
82  struct HLTData {
84  unsigned int prescale;
85  unsigned int l1passcount;
86  unsigned int acceptcount;
87  };
88  struct L1Data {
90  unsigned int prescale;
91  unsigned int ratecount;
92  };
93  struct PerRunData {
94  std::string bitzeroname; //norm bit name
95  std::map<std::string, unsigned int> TRGBitNameToIndex;
96  std::map<std::string, unsigned int> HLTPathNameToIndex;
97  std::vector<std::string> TRGBitNames;
98  std::vector<std::string> HLTPathNames;
99  };
100  struct PerLSData {
101  float lumivalue;
102  float lumierror;
103  short lumiquality;
104  unsigned long long deadcount;
105  unsigned int numorbit;
106  unsigned int startorbit;
107  unsigned int bitzerocount;
108  unsigned int bitzeroprescale;
109  std::vector<HLTData> hltdata;
110  std::vector<L1Data> l1data;
111  std::vector<std::pair<std::string, std::vector<float> > > bunchlumivalue;
112  std::vector<std::pair<std::string, std::vector<float> > > bunchlumierror;
113  std::vector<std::pair<std::string, std::vector<short> > > bunchlumiquality;
114  std::vector<float> beam1intensity;
115  std::vector<float> beam2intensity;
116  };
117 
118  explicit LumiProducer(const edm::ParameterSet&);
119 
120  ~LumiProducer() override;
121 
122 private:
123  void produce(edm::Event&, const edm::EventSetup&) final;
124 
125  void beginRun(edm::Run const&, edm::EventSetup const&) final;
126 
127  void beginLuminosityBlockProduce(edm::LuminosityBlock& iLBlock, edm::EventSetup const& iSetup) final;
128 
129  void endRun(edm::Run const&, edm::EventSetup const&) final;
130  void endRunProduce(edm::Run&, edm::EventSetup const&) final;
131 
132  bool fillLumi(edm::LuminosityBlock& iLBlock);
133  void fillRunCache(const coral::ISchema& schema, unsigned int runnumber);
134  void fillLSCache(unsigned int luminum);
135  void writeProductsForEntry(edm::LuminosityBlock& iLBlock, unsigned int runnumber, unsigned int luminum);
136  const std::string servletTranslation(const std::string& servlet) const;
137  std::string x2s(const XMLCh* input) const;
138  XMLCh* s2x(const std::string& input) const;
139  std::string toParentString(const xercesc::DOMNode& nodeToConvert) const;
140  unsigned long long getLumiDataId(const coral::ISchema& schema, unsigned int runnumber);
141  unsigned long long getTrgDataId(const coral::ISchema& schema, unsigned int runnumber);
142  unsigned long long getHltDataId(const coral::ISchema& schema, unsigned int runnumber);
143  std::string getCurrentDataTag(const coral::ISchema& schema);
147  unsigned int m_cachedrun;
148  unsigned long long m_cachedlumidataid;
149  unsigned long long m_cachedtrgdataid;
150  unsigned long long m_cachedhltdataid;
152  std::map<unsigned int, PerLSData> m_lscache;
154  unsigned int m_cachesize;
155 };
156 
157 //
158 // constructors and destructor
159 //
160 
161 std::string LumiProducer::x2s(const XMLCh* toTranscode) const {
162  std::string tmp(xercesc::XMLString::transcode(toTranscode));
163  return tmp;
164 }
165 
166 XMLCh* LumiProducer::s2x(const std::string& temp) const {
167  XMLCh* buff = xercesc::XMLString::transcode(temp.c_str());
168  return buff;
169 }
170 
171 std::string LumiProducer::toParentString(const xercesc::DOMNode& nodeToConvert) const {
172  std::ostringstream oss;
173  xercesc::DOMNodeList* childList = nodeToConvert.getChildNodes();
174 
175  unsigned int numNodes = childList->getLength();
176  for (unsigned int i = 0; i < numNodes; ++i) {
177  xercesc::DOMNode* childNode = childList->item(i);
178  if (childNode->getNodeType() != xercesc::DOMNode::ELEMENT_NODE)
179  continue;
180  xercesc::DOMElement* child = static_cast<xercesc::DOMElement*>(childNode);
181  xercesc::DOMNamedNodeMap* attributes = child->getAttributes();
182  unsigned int numAttributes = attributes->getLength();
183  for (unsigned int j = 0; j < numAttributes; ++j) {
184  xercesc::DOMNode* attributeNode = attributes->item(j);
185  if (attributeNode->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
186  continue;
187  xercesc::DOMAttr* attribute = static_cast<xercesc::DOMAttr*>(attributeNode);
188 
189  oss << "(" << x2s(child->getTagName()) << x2s(attribute->getName()) << "=" << x2s(attribute->getValue()) << ")";
190  }
191  }
192  return oss.str();
193 }
194 
196  std::string frontierConnect;
197  std::string realconnect;
199  std::unique_ptr<xercesc::XercesDOMParser> parser(new xercesc::XercesDOMParser);
200  try {
201  parser->setValidationScheme(xercesc::XercesDOMParser::Val_Auto);
202  parser->setDoNamespaces(false);
203  parser->parse(m_siteconfpath.c_str());
204  xercesc::DOMDocument* doc = parser->getDocument();
205  if (!doc) {
206  return "";
207  }
208 
209  xercesc::DOMNodeList* frontierConnectList = doc->getElementsByTagName(s2x("frontier-connect"));
210  if (frontierConnectList->getLength() > 0) {
211  xercesc::DOMElement* frontierConnectElement = static_cast<xercesc::DOMElement*>(frontierConnectList->item(0));
212  frontierConnect = toParentString(*frontierConnectElement);
213  }
214  // Replace the last component of every "serverurl=" piece (up to the
215  // next close-paren) with the servlet
216  std::string::size_type nextparen = 0;
217  std::string::size_type serverurl, lastslash;
218  std::string complexstr = "";
219  while ((serverurl = frontierConnect.find("(serverurl=", nextparen)) != std::string::npos) {
220  realconnect.append(frontierConnect, nextparen, serverurl - nextparen);
221  nextparen = frontierConnect.find(')', serverurl);
222  lastslash = frontierConnect.rfind('/', nextparen);
223  realconnect.append(frontierConnect, serverurl, lastslash - serverurl + 1);
224  realconnect.append(servlet);
225  }
226  realconnect.append(frontierConnect, nextparen, frontierConnect.length() - nextparen);
227  } catch (xercesc::DOMException& e) {
228  }
229  return realconnect;
230 }
231 
232 LumiProducer::LumiProducer::LumiProducer(const edm::ParameterSet& iConfig)
233  : m_cachedrun(0), m_isNullRun(false), m_cachesize(0) {
234  // register your products
235  produces<LumiSummaryRunHeader, edm::Transition::EndRun>();
236  produces<LumiSummary, edm::Transition::BeginLuminosityBlock>();
237  produces<LumiDetails, edm::Transition::BeginLuminosityBlock>();
238  // set up cache
239  std::string connectStr = iConfig.getParameter<std::string>("connect");
240  m_cachesize = iConfig.getUntrackedParameter<unsigned int>("ncacheEntries", 5);
241  m_lumiversion = iConfig.getUntrackedParameter<std::string>("lumiversion", "");
242  const std::string fproto("frontier://");
243  //test if need frontier servlet site-local translation
244  if (connectStr.substr(0, fproto.length()) == fproto) {
245  std::string::size_type startservlet = fproto.length();
246  std::string::size_type endservlet = connectStr.find('(', startservlet);
247  if (endservlet == std::string::npos) {
248  endservlet = connectStr.rfind('/', connectStr.length());
249  }
250  std::string servlet = connectStr.substr(startservlet, endservlet - startservlet);
251  if ((!servlet.empty()) && (servlet.find_first_of(":/)[]") == std::string::npos)) {
252  if (servlet == "cms_conditions_data")
253  servlet = "";
254 
255  std::string siteconfpath = iConfig.getUntrackedParameter<std::string>("siteconfpath", "");
256  if (siteconfpath.length() == 0) {
257  std::string url = (std::filesystem::path("SITECONF") / std::filesystem::path("local") /
258  std::filesystem::path("JobConfig") / std::filesystem::path("site-local-config.xml"))
259  .string();
260  char* tmp = std::getenv("CMS_PATH");
261  if (tmp) {
263  }
264  } else {
265  if (!std::filesystem::exists(std::filesystem::path(siteconfpath))) {
266  throw cms::Exception("Non existing path ") << siteconfpath;
267  }
269  (std::filesystem::path(siteconfpath) / std::filesystem::path("site-local-config.xml")).string();
270  }
271  //std::cout<<"servlet : "<<servlet<<std::endl;
272  m_connectStr = fproto + servletTranslation(servlet) + connectStr.substr(endservlet);
273  } else {
274  m_connectStr = connectStr;
275  }
276  } else {
277  m_connectStr = connectStr;
278  }
279  //std::cout<<"connect string "<< m_connectStr<<std::endl;
280 }
281 
283 
284 //
285 // member functions
286 //
288 unsigned long long LumiProducer::getLumiDataId(const coral::ISchema& schema, unsigned int runnumber) {
289  //
290  //select max(data_id) from lumidata where runnum=:runnum
291  //
292  //std::count<<"entering getLumiDataId "<<std::endl;
293  unsigned long long lumidataid = 0;
294  coral::AttributeList bindVariables;
295  bindVariables.extend("runnum", typeid(unsigned int));
296  bindVariables["runnum"].data<unsigned int>() = runnumber;
297  coral::AttributeList lumiidOutput;
298  lumiidOutput.extend("lumidataid", typeid(unsigned long long));
299  coral::IQuery* lumiQuery = schema.newQuery();
300  lumiQuery->addToTableList(lumi::LumiNames::lumidataTableName());
301  lumiQuery->addToOutputList("MAX(DATA_ID)", "lumidataid");
302  lumiQuery->setCondition("RUNNUM=:runnum", bindVariables);
303  lumiQuery->defineOutput(lumiidOutput);
304  coral::ICursor& lumicursor = lumiQuery->execute();
305  while (lumicursor.next()) {
306  const coral::AttributeList& row = lumicursor.currentRow();
307  if (!row["lumidataid"].isNull()) {
308  lumidataid = row["lumidataid"].data<unsigned long long>();
309  }
310  }
311  delete lumiQuery;
312  return lumidataid;
313 }
314 unsigned long long LumiProducer::getTrgDataId(const coral::ISchema& schema, unsigned int runnumber) {
315  //
316  //select max(data_id) from trgdata where runnum=:runnum
317  //
318  unsigned long long trgdataid = 0;
319  coral::AttributeList bindVariables;
320  bindVariables.extend("runnum", typeid(unsigned int));
321  bindVariables["runnum"].data<unsigned int>() = runnumber;
322  coral::AttributeList trgidOutput;
323  trgidOutput.extend("trgdataid", typeid(unsigned long long));
324  coral::IQuery* trgQuery = schema.newQuery();
325  trgQuery->addToTableList(lumi::LumiNames::trgdataTableName());
326  trgQuery->addToOutputList("MAX(DATA_ID)", "trgdataid");
327  trgQuery->setCondition("RUNNUM=:runnum", bindVariables);
328  trgQuery->defineOutput(trgidOutput);
329  coral::ICursor& trgcursor = trgQuery->execute();
330  while (trgcursor.next()) {
331  const coral::AttributeList& row = trgcursor.currentRow();
332  if (!row["trgdataid"].isNull()) {
333  trgdataid = row["trgdataid"].data<unsigned long long>();
334  }
335  }
336  delete trgQuery;
337  return trgdataid;
338 }
339 unsigned long long LumiProducer::getHltDataId(const coral::ISchema& schema, unsigned int runnumber) {
340  //
341  //select max(data_id) from hltdata where runnum=:runnum
342  //
343  unsigned long long hltdataid = 0;
344  coral::AttributeList bindVariables;
345  bindVariables.extend("runnum", typeid(unsigned int));
346  bindVariables["runnum"].data<unsigned int>() = runnumber;
347  coral::AttributeList hltidOutput;
348  hltidOutput.extend("hltdataid", typeid(unsigned long long));
349  coral::IQuery* hltQuery = schema.newQuery();
350  hltQuery->addToTableList(lumi::LumiNames::hltdataTableName());
351  hltQuery->addToOutputList("MAX(DATA_ID)", "hltdataid");
352  hltQuery->setCondition("RUNNUM=:runnum", bindVariables);
353  hltQuery->defineOutput(hltidOutput);
354  coral::ICursor& hltcursor = hltQuery->execute();
355  while (hltcursor.next()) {
356  const coral::AttributeList& row = hltcursor.currentRow();
357  if (!row["hltdataid"].isNull()) {
358  hltdataid = row["hltdataid"].data<unsigned long long>();
359  }
360  }
361  delete hltQuery;
362  return hltdataid;
363 }
364 
365 std::string LumiProducer::getCurrentDataTag(const coral::ISchema& schema) {
366  //select tagid,tagname from tags
368  std::map<unsigned long long, std::string> alltags;
369  coral::IQuery* tagQuery = schema.newQuery();
370  tagQuery->addToTableList(lumi::LumiNames::tagsTableName());
371  tagQuery->addToOutputList("TAGID");
372  tagQuery->addToOutputList("TAGNAME");
373  coral::AttributeList tagoutput;
374  tagoutput.extend("TAGID", typeid(unsigned long long));
375  tagoutput.extend("TAGNAME", typeid(std::string));
376  tagQuery->defineOutput(tagoutput);
377  coral::ICursor& tagcursor = tagQuery->execute();
378  while (tagcursor.next()) {
379  const coral::AttributeList& row = tagcursor.currentRow();
380  unsigned long long tagid = row["TAGID"].data<unsigned long long>();
381  const std::string tagname = row["TAGNAME"].data<std::string>();
382  alltags.insert(std::make_pair(tagid, tagname));
383  }
384  delete tagQuery;
385  unsigned long long maxid = 0;
386  for (std::map<unsigned long long, std::string>::iterator it = alltags.begin(); it != alltags.end(); ++it) {
387  if (it->first > maxid) {
388  maxid = it->first;
389  }
390  }
391  result = alltags[maxid];
392  return result;
393 }
394 
395 void LumiProducer::beginRun(edm::Run const& run, edm::EventSetup const& iSetup) {
396  unsigned int runnumber = run.run();
397  if (m_cachedrun != runnumber) {
398  //queries once per run
401  if (!mydbservice.isAvailable()) {
402  throw cms::Exception("Non existing service lumi::service::DBService");
403  }
404  auto session = mydbservice->connectReadOnly(m_connectStr);
405  try {
406  session->transaction().start(true);
407  m_cachedlumidataid = getLumiDataId(session->nominalSchema(), runnumber);
408  if (m_cachedlumidataid != 0) { //if no lumi, do not bother other info
409  m_cachedtrgdataid = getTrgDataId(session->nominalSchema(), runnumber);
410  m_cachedhltdataid = getHltDataId(session->nominalSchema(), runnumber);
411  fillRunCache(session->nominalSchema(), runnumber);
412  } else {
413  m_isNullRun = true;
414  }
415  session->transaction().commit();
416  } catch (const coral::Exception& er) {
417  session->transaction().rollback();
418  throw cms::Exception("DatabaseError ") << er.what();
419  }
420  }
421  //std::cout<<"end of beginRun "<<runnumber<<std::endl;
422 }
423 
425  unsigned int runnumber = iLBlock.run();
426  unsigned int luminum = iLBlock.luminosityBlock();
427  //std::cout<<"beg of beginLuminosityBlock "<<luminum<<std::endl;
428  //if is null run, fill empty values and return
429  if (m_isNullRun) {
430  iLBlock.put(std::make_unique<LumiSummary>());
431  iLBlock.put(std::make_unique<LumiDetails>());
432  return;
433  }
434  if (m_lscache.find(luminum) == m_lscache.end()) {
435  //if runnumber is cached but LS is not, this is the first LS, fill LS cache to full capacity
436  fillLSCache(luminum);
437  }
438  //here the presence of ls is guaranteed
439  writeProductsForEntry(iLBlock, runnumber, luminum);
440 }
441 void LumiProducer::endRun(edm::Run const& run, edm::EventSetup const& iSetup) {}
443  auto lsrh = std::make_unique<LumiSummaryRunHeader>();
444  lsrh->swapL1Names(m_runcache.TRGBitNames);
445  lsrh->swapHLTNames(m_runcache.HLTPathNames);
446  run.put(std::move(lsrh));
449 }
450 void LumiProducer::fillRunCache(const coral::ISchema& schema, unsigned int runnumber) {
451  if (m_lumiversion.empty()) {
453  }
454  std::cout << "lumi tag version 2 " << m_lumiversion << std::endl;
455  if (m_cachedtrgdataid != 0) {
456  coral::AttributeList trgBindVariables;
457  trgBindVariables.extend("trgdataid", typeid(unsigned long long));
458  trgBindVariables["trgdataid"].data<unsigned long long>() = m_cachedtrgdataid;
459  //std::cout<<"cached trgdataid "<<m_cachedtrgdataid<<std::endl;
460  coral::AttributeList trgOutput;
461  trgOutput.extend("bitzeroname", typeid(std::string));
462  trgOutput.extend("bitnameclob", typeid(std::string));
463  coral::IQuery* trgQuery = schema.newQuery();
464  trgQuery->addToTableList(lumi::LumiNames::trgdataTableName());
465  trgQuery->addToOutputList("BITZERONAME");
466  trgQuery->addToOutputList("BITNAMECLOB");
467  trgQuery->setCondition("DATA_ID=:trgdataid", trgBindVariables);
468  trgQuery->defineOutput(trgOutput);
469  coral::ICursor& trgcursor = trgQuery->execute();
470  while (trgcursor.next()) {
471  const coral::AttributeList& row = trgcursor.currentRow();
472  m_runcache.bitzeroname = row["bitzeroname"].data<std::string>();
473  //std::cout<<"bitzeroname "<<m_runcache.bitzeroname<<std::endl;
474  std::string bitnames = row["bitnameclob"].data<std::string>();
475  boost::char_separator<char> sep(",");
476  boost::tokenizer<boost::char_separator<char> > tokens(bitnames, sep);
477  for (boost::tokenizer<boost::char_separator<char> >::iterator tok_it = tokens.begin(); tok_it != tokens.end();
478  ++tok_it) {
479  m_runcache.TRGBitNames.push_back(*tok_it);
480  }
481  for (unsigned int i = 0; i < m_runcache.TRGBitNames.size(); ++i) {
482  m_runcache.TRGBitNameToIndex.insert(std::make_pair(m_runcache.TRGBitNames.at(i), i));
483  }
484  }
485  delete trgQuery;
486  }
487  if (m_cachedhltdataid != 0) {
488  //
489  //select pathnameclob from hltdata where data_id=:hltdataid
490  //
491  coral::AttributeList hltBindVariables;
492  hltBindVariables.extend("hltdataid", typeid(unsigned long long));
493  hltBindVariables["hltdataid"].data<unsigned long long>() = m_cachedhltdataid;
494  coral::AttributeList hltOutput;
495  hltOutput.extend("PATHNAMECLOB", typeid(std::string));
496  coral::IQuery* hltQuery = schema.newQuery();
497  hltQuery->addToTableList(lumi::LumiNames::hltdataTableName());
498  hltQuery->addToOutputList("PATHNAMECLOB");
499  hltQuery->setCondition("DATA_ID=:hltdataid", hltBindVariables);
500  hltQuery->defineOutput(hltOutput);
501  coral::ICursor& hltcursor = hltQuery->execute();
502  while (hltcursor.next()) {
503  const coral::AttributeList& row = hltcursor.currentRow();
504  std::string pathnames = row["PATHNAMECLOB"].data<std::string>();
505  boost::char_separator<char> sep(",");
506  boost::tokenizer<boost::char_separator<char> > tokens(pathnames, sep);
507  for (boost::tokenizer<boost::char_separator<char> >::iterator tok_it = tokens.begin(); tok_it != tokens.end();
508  ++tok_it) {
509  m_runcache.HLTPathNames.push_back(*tok_it);
510  }
511  for (unsigned int i = 0; i < m_runcache.HLTPathNames.size(); ++i) {
512  m_runcache.HLTPathNameToIndex.insert(std::make_pair(m_runcache.HLTPathNames.at(i), i));
513  }
514  }
515  delete hltQuery;
516  }
517 }
518 void LumiProducer::fillLSCache(unsigned int luminum) {
519  //initialize cache
520  if (m_isNullRun)
521  return;
522  m_lscache.clear();
523  for (unsigned int n = luminum; n < luminum + m_cachesize; ++n) {
524  PerLSData l;
525  l.hltdata.reserve(250);
526  l.l1data.reserve(192);
527  l.bunchlumivalue.reserve(5);
528  l.bunchlumierror.reserve(5);
529  l.bunchlumiquality.reserve(5);
530  l.beam1intensity.resize(3564, 0.0);
531  l.beam2intensity.resize(3564, 0.0);
532  m_lscache.insert(std::make_pair(n, l));
533  }
534  //queries once per cache refill
535  //
536  //select cmslsnum,instlumi,startorbit,numorbit,bxindex,beam1intensity,beam2intensity,bxlumivalue_occ1,bxlumivalue_occ2,bxlumivalue_et from lumisummaryv2 where cmslsnum>=:lsmin and cmslsnum<:lsmax and data_id=:lumidataid;
537  //
539  if (!mydbservice.isAvailable()) {
540  throw cms::Exception("Non existing service lumi::service::DBService");
541  }
542  auto session = mydbservice->connectReadOnly(m_connectStr);
543  try {
544  session->transaction().start(true);
545  coral::ISchema& schema = session->nominalSchema();
546  coral::AttributeList lumisummaryBindVariables;
547  lumisummaryBindVariables.extend("lsmin", typeid(unsigned int));
548  lumisummaryBindVariables.extend("lsmax", typeid(unsigned int));
549  lumisummaryBindVariables.extend("lumidataid", typeid(unsigned long long));
550  lumisummaryBindVariables["lumidataid"].data<unsigned long long>() = m_cachedlumidataid;
551  lumisummaryBindVariables["lsmin"].data<unsigned int>() = luminum;
552  lumisummaryBindVariables["lsmax"].data<unsigned int>() = luminum + m_cachesize;
553  coral::AttributeList lumisummaryOutput;
554  lumisummaryOutput.extend("CMSLSNUM", typeid(unsigned int));
555  lumisummaryOutput.extend("INSTLUMI", typeid(float));
556  lumisummaryOutput.extend("STARTORBIT", typeid(unsigned int));
557  lumisummaryOutput.extend("NUMORBIT", typeid(unsigned int));
558  lumisummaryOutput.extend("CMSBXINDEXBLOB", typeid(coral::Blob));
559  lumisummaryOutput.extend("BEAMINTENSITYBLOB_1", typeid(coral::Blob));
560  lumisummaryOutput.extend("BEAMINTENSITYBLOB_2", typeid(coral::Blob));
561  lumisummaryOutput.extend("BXLUMIVALUE_OCC1", typeid(coral::Blob));
562  lumisummaryOutput.extend("BXLUMIVALUE_OCC2", typeid(coral::Blob));
563  lumisummaryOutput.extend("BXLUMIVALUE_ET", typeid(coral::Blob));
564  coral::IQuery* lumisummaryQuery = schema.newQuery();
565  lumisummaryQuery->addToTableList(lumi::LumiNames::lumisummaryv2TableName());
566  lumisummaryQuery->addToOutputList("CMSLSNUM");
567  lumisummaryQuery->addToOutputList("INSTLUMI");
568  lumisummaryQuery->addToOutputList("STARTORBIT");
569  lumisummaryQuery->addToOutputList("NUMORBIT");
570  lumisummaryQuery->addToOutputList("CMSBXINDEXBLOB");
571  lumisummaryQuery->addToOutputList("BEAMINTENSITYBLOB_1");
572  lumisummaryQuery->addToOutputList("BEAMINTENSITYBLOB_2");
573  lumisummaryQuery->addToOutputList("BXLUMIVALUE_OCC1");
574  lumisummaryQuery->addToOutputList("BXLUMIVALUE_OCC2");
575  lumisummaryQuery->addToOutputList("BXLUMIVALUE_ET");
576  lumisummaryQuery->setCondition("CMSLSNUM>=:lsmin AND CMSLSNUM<:lsmax AND DATA_ID=:lumidataid",
577  lumisummaryBindVariables);
578  lumisummaryQuery->defineOutput(lumisummaryOutput);
579  coral::ICursor& lumisummarycursor = lumisummaryQuery->execute();
580  unsigned int rowcounter = 0;
581  while (lumisummarycursor.next()) {
582  const coral::AttributeList& row = lumisummarycursor.currentRow();
583  unsigned int cmslsnum = row["CMSLSNUM"].data<unsigned int>();
584  //std::cout<<"cmslsnum "<<cmslsnum<<std::endl;
585  PerLSData& lsdata = m_lscache[cmslsnum];
586  lsdata.lumivalue = row["INSTLUMI"].data<float>();
587  lsdata.lumierror = 0.0;
588  lsdata.lumiquality = 0;
589  lsdata.startorbit = row["STARTORBIT"].data<unsigned int>();
590  lsdata.numorbit = row["NUMORBIT"].data<unsigned int>();
591 
592  if (!row["CMSBXINDEXBLOB"].isNull() && !row["BXLUMIVALUE_OCC1"].isNull()) {
593  const coral::Blob& bxindexBlob = row["CMSBXINDEXBLOB"].data<coral::Blob>();
594  const void* bxindex_StartAddress = bxindexBlob.startingAddress();
595  short* bxindex = (short*)::malloc(bxindexBlob.size());
596  const coral::Blob& beam1intensityBlob = row["BEAMINTENSITYBLOB_1"].data<coral::Blob>();
597  const void* beam1intensityBlob_StartAddress = beam1intensityBlob.startingAddress();
598  float* beam1intensity = (float*)::malloc(beam1intensityBlob.size());
599  const coral::Blob& beam2intensityBlob = row["BEAMINTENSITYBLOB_2"].data<coral::Blob>();
600  const void* beam2intensityBlob_StartAddress = beam2intensityBlob.startingAddress();
601  float* beam2intensity = (float*)::malloc(beam2intensityBlob.size());
602  std::memmove(bxindex, bxindex_StartAddress, bxindexBlob.size());
603  std::memmove(beam1intensity, beam1intensityBlob_StartAddress, beam1intensityBlob.size());
604  std::memmove(beam2intensity, beam2intensityBlob_StartAddress, beam2intensityBlob.size());
605 
606  unsigned int iMax = bxindexBlob.size() / sizeof(short);
607  unsigned int lsb1Max = lsdata.beam1intensity.size();
608  unsigned int lsb2Max = lsdata.beam2intensity.size();
609  unsigned int ib1Max = beam1intensityBlob.size() / sizeof(float);
610  unsigned int ib2Max = beam2intensityBlob.size() / sizeof(float);
611  for (unsigned int i = 0; i < iMax; ++i) {
612  unsigned int idx = bxindex[i];
613  if (ib1Max > i && lsb1Max > idx) {
614  lsdata.beam1intensity.at(idx) = beam1intensity[i];
615  }
616  if (ib2Max > i && lsb2Max > idx) {
617  lsdata.beam2intensity.at(idx) = beam2intensity[i];
618  }
619  }
620  ::free(bxindex);
621  ::free(beam1intensity);
622  ::free(beam2intensity);
623 
624  const coral::Blob& bxlumivalBlob_occ1 = row["BXLUMIVALUE_OCC1"].data<coral::Blob>();
625  const void* bxlumival_occ1_StartAddress = bxlumivalBlob_occ1.startingAddress();
626  float* bxlumival_occ1 = (float*)::malloc(bxlumivalBlob_occ1.size());
627  std::memmove(bxlumival_occ1, bxlumival_occ1_StartAddress, bxlumivalBlob_occ1.size());
628  std::vector<float> bxlumivalVec_occ1(bxlumival_occ1,
629  bxlumival_occ1 + bxlumivalBlob_occ1.size() / sizeof(float));
630  ::free(bxlumival_occ1);
631  lsdata.bunchlumivalue.push_back(std::make_pair(std::string("OCC1"), bxlumivalVec_occ1));
632  lsdata.bunchlumierror.push_back(std::make_pair(std::string("OCC1"), std::vector<float>(3564)));
633  lsdata.bunchlumiquality.push_back(std::make_pair(std::string("OCC1"), std::vector<short>(3564)));
634  const coral::Blob& bxlumivalBlob_occ2 = row["BXLUMIVALUE_OCC2"].data<coral::Blob>();
635  const void* bxlumival_occ2_StartAddress = bxlumivalBlob_occ2.startingAddress();
636  float* bxlumival_occ2 = (float*)::malloc(bxlumivalBlob_occ2.size());
637  std::memmove(bxlumival_occ2, bxlumival_occ2_StartAddress, bxlumivalBlob_occ2.size());
638  std::vector<float> bxlumivalVec_occ2(bxlumival_occ2,
639  bxlumival_occ2 + bxlumivalBlob_occ1.size() / sizeof(float));
640  ::free(bxlumival_occ2);
641  lsdata.bunchlumivalue.push_back(std::make_pair(std::string("OCC2"), bxlumivalVec_occ2));
642  lsdata.bunchlumierror.push_back(std::make_pair(std::string("OCC2"), std::vector<float>(3564)));
643  lsdata.bunchlumiquality.push_back(std::make_pair(std::string("OCC2"), std::vector<short>(3564)));
644 
645  const coral::Blob& bxlumivalBlob_et = row["BXLUMIVALUE_ET"].data<coral::Blob>();
646  const void* bxlumival_et_StartAddress = bxlumivalBlob_et.startingAddress();
647  float* bxlumival_et = (float*)::malloc(bxlumivalBlob_et.size());
648  std::memmove(bxlumival_et, bxlumival_et_StartAddress, bxlumivalBlob_et.size());
649  std::vector<float> bxlumivalVec_et(bxlumival_et, bxlumival_et + bxlumivalBlob_et.size() / sizeof(float));
650  ::free(bxlumival_et);
651  lsdata.bunchlumivalue.push_back(std::make_pair(std::string("ET"), bxlumivalVec_et));
652  lsdata.bunchlumierror.push_back(std::make_pair(std::string("ET"), std::vector<float>(3564)));
653  lsdata.bunchlumiquality.push_back(std::make_pair(std::string("ET"), std::vector<short>(3564)));
654  }
655  ++rowcounter;
656  }
657  if (rowcounter == 0) {
658  m_isNullRun = true;
659  return;
660  }
661  delete lumisummaryQuery;
662 
663  //
664  //select cmslsnum,deadtimecount,bitzerocount,bitzeroprescale,prescaleblob,trgcountblob from lstrg where cmslsnum >=:luminum and cmslsnum<:luminum+cachesize AND data_id=:trgdataid;
665  //
666  coral::AttributeList trgBindVariables;
667  trgBindVariables.extend("lsmin", typeid(unsigned int));
668  trgBindVariables.extend("lsmax", typeid(unsigned int));
669  trgBindVariables.extend("trgdataid", typeid(unsigned long long));
670  trgBindVariables["lsmin"].data<unsigned int>() = luminum;
671  trgBindVariables["lsmax"].data<unsigned int>() = luminum + m_cachesize;
672  trgBindVariables["trgdataid"].data<unsigned long long>() = m_cachedtrgdataid;
673  coral::AttributeList trgOutput;
674  trgOutput.extend("CMSLSNUM", typeid(unsigned int));
675  trgOutput.extend("DEADTIMECOUNT", typeid(unsigned long long));
676  trgOutput.extend("BITZEROCOUNT", typeid(unsigned int));
677  trgOutput.extend("BITZEROPRESCALE", typeid(unsigned int));
678  trgOutput.extend("PRESCALEBLOB", typeid(coral::Blob));
679  trgOutput.extend("TRGCOUNTBLOB", typeid(coral::Blob));
680 
681  coral::IQuery* trgQuery = schema.newQuery();
682  trgQuery->addToTableList(lumi::LumiNames::lstrgTableName());
683  trgQuery->addToOutputList("CMSLSNUM");
684  trgQuery->addToOutputList("DEADTIMECOUNT");
685  trgQuery->addToOutputList("BITZEROCOUNT");
686  trgQuery->addToOutputList("BITZEROPRESCALE");
687  trgQuery->addToOutputList("PRESCALEBLOB");
688  trgQuery->addToOutputList("TRGCOUNTBLOB");
689  trgQuery->setCondition("CMSLSNUM>=:lsmin AND CMSLSNUM<:lsmax AND DATA_ID=:trgdataid", trgBindVariables);
690  trgQuery->defineOutput(trgOutput);
691  coral::ICursor& trgcursor = trgQuery->execute();
692  while (trgcursor.next()) {
693  const coral::AttributeList& row = trgcursor.currentRow();
694  unsigned int cmslsnum = row["CMSLSNUM"].data<unsigned int>();
695  PerLSData& lsdata = m_lscache[cmslsnum];
696  lsdata.deadcount = row["DEADTIMECOUNT"].data<unsigned long long>();
697  lsdata.bitzerocount = row["BITZEROCOUNT"].data<unsigned int>();
698  lsdata.bitzeroprescale = row["BITZEROPRESCALE"].data<unsigned int>();
699  if (!row["PRESCALEBLOB"].isNull()) {
700  const coral::Blob& prescaleblob = row["PRESCALEBLOB"].data<coral::Blob>();
701  const void* prescaleblob_StartAddress = prescaleblob.startingAddress();
702  unsigned int* prescales = (unsigned int*)::malloc(prescaleblob.size());
703  std::memmove(prescales, prescaleblob_StartAddress, prescaleblob.size());
704  const coral::Blob& trgcountblob = row["TRGCOUNTBLOB"].data<coral::Blob>();
705  const void* trgcountblob_StartAddress = trgcountblob.startingAddress();
706  unsigned int* trgcounts = (unsigned int*)::malloc(trgcountblob.size());
707  std::memmove(trgcounts, trgcountblob_StartAddress, trgcountblob.size());
708  for (unsigned int i = 0; i < trgcountblob.size() / sizeof(unsigned int); ++i) {
709  L1Data l1tmp;
710  l1tmp.bitname = m_runcache.TRGBitNames[i];
711  l1tmp.prescale = prescales[i];
712  l1tmp.ratecount = trgcounts[i];
713  lsdata.l1data.push_back(l1tmp);
714  }
715  ::free(prescales);
716  ::free(trgcounts);
717  }
718  }
719  delete trgQuery;
720  //
721  //select cmslsnum,hltcountblob,hltacceptblob,prescaleblob from hlt where cmslsnum >=:luminum and cmslsnum<=:luminum+cachesize and data_id=:hltdataid
722  //
723  coral::AttributeList hltBindVariables;
724  hltBindVariables.extend("lsmin", typeid(unsigned int));
725  hltBindVariables.extend("lsmax", typeid(unsigned int));
726  hltBindVariables.extend("hltdataid", typeid(unsigned long long));
727  hltBindVariables["lsmin"].data<unsigned int>() = luminum;
728  hltBindVariables["lsmax"].data<unsigned int>() = luminum + m_cachesize;
729  hltBindVariables["hltdataid"].data<unsigned long long>() = m_cachedhltdataid;
730  coral::AttributeList hltOutput;
731  hltOutput.extend("CMSLSNUM", typeid(unsigned int));
732  hltOutput.extend("HLTCOUNTBLOB", typeid(coral::Blob));
733  hltOutput.extend("HLTACCEPTBLOB", typeid(coral::Blob));
734  hltOutput.extend("PRESCALEBLOB", typeid(coral::Blob));
735  coral::IQuery* hltQuery = schema.newQuery();
736  hltQuery->addToTableList(lumi::LumiNames::lshltTableName());
737  hltQuery->addToOutputList("CMSLSNUM");
738  hltQuery->addToOutputList("HLTCOUNTBLOB");
739  hltQuery->addToOutputList("HLTACCEPTBLOB");
740  hltQuery->addToOutputList("PRESCALEBLOB");
741  hltQuery->setCondition("CMSLSNUM>=:lsmin AND CMSLSNUM<:lsmax AND DATA_ID=:hltdataid", hltBindVariables);
742  hltQuery->defineOutput(hltOutput);
743  coral::ICursor& hltcursor = hltQuery->execute();
744  while (hltcursor.next()) {
745  const coral::AttributeList& row = hltcursor.currentRow();
746  unsigned int cmslsnum = row["CMSLSNUM"].data<unsigned int>();
747  PerLSData& lsdata = m_lscache[cmslsnum];
748  if (!row["PRESCALEBLOB"].isNull()) {
749  const coral::Blob& hltprescaleblob = row["PRESCALEBLOB"].data<coral::Blob>();
750  const void* hltprescaleblob_StartAddress = hltprescaleblob.startingAddress();
751  unsigned int* hltprescales = (unsigned int*)::malloc(hltprescaleblob.size());
752  std::memmove(hltprescales, hltprescaleblob_StartAddress, hltprescaleblob.size());
753  const coral::Blob& hltcountblob = row["HLTCOUNTBLOB"].data<coral::Blob>();
754  const void* hltcountblob_StartAddress = hltcountblob.startingAddress();
755  unsigned int* hltcounts = (unsigned int*)::malloc(hltcountblob.size());
756  std::memmove(hltcounts, hltcountblob_StartAddress, hltcountblob.size());
757  const coral::Blob& hltacceptblob = row["HLTACCEPTBLOB"].data<coral::Blob>();
758  const void* hltacceptblob_StartAddress = hltacceptblob.startingAddress();
759  unsigned int* hltaccepts = (unsigned int*)::malloc(hltacceptblob.size());
760  std::memmove(hltaccepts, hltacceptblob_StartAddress, hltacceptblob.size());
761  unsigned int nhltaccepts = hltacceptblob.size() / sizeof(unsigned int);
762  if (nhltaccepts > 0 && m_runcache.HLTPathNames.empty()) {
763  edm::LogWarning("CorruptOrMissingHLTData")
764  << "Got " << nhltaccepts << " hltaccepts, but the run chache is empty. hltdata will not be written";
765  break;
766  }
767 
768  for (unsigned int i = 0; i < hltacceptblob.size() / sizeof(unsigned int); ++i) {
769  HLTData hlttmp;
770  hlttmp.pathname = m_runcache.HLTPathNames[i];
771  hlttmp.prescale = hltprescales[i];
772  hlttmp.l1passcount = hltcounts[i];
773  hlttmp.acceptcount = hltaccepts[i];
774  lsdata.hltdata.push_back(hlttmp);
775  }
776  ::free(hltprescales);
777  ::free(hltcounts);
778  ::free(hltaccepts);
779  }
780  }
781  delete hltQuery;
782  session->transaction().commit();
783  } catch (const coral::Exception& er) {
784  session->transaction().rollback();
785  throw cms::Exception("DatabaseError ") << er.what();
786  }
787 }
788 void LumiProducer::writeProductsForEntry(edm::LuminosityBlock& iLBlock, unsigned int runnumber, unsigned int luminum) {
789  //std::cout<<"writing runnumber,luminum "<<runnumber<<" "<<luminum<<std::endl;
790  auto pIn1 = std::make_unique<LumiSummary>();
791  auto pIn2 = std::make_unique<LumiDetails>();
792  if (m_isNullRun) {
793  pIn1->setLumiVersion("-1");
794  pIn2->setLumiVersion("-1");
795  iLBlock.put(std::move(pIn1));
796  iLBlock.put(std::move(pIn2));
797  return;
798  }
799  PerLSData& lsdata = m_lscache[luminum];
800  pIn1->setLumiData(lsdata.lumivalue, lsdata.lumierror, lsdata.lumiquality);
801  pIn1->setDeadCount(lsdata.deadcount);
802  if (!lsdata.l1data.empty()) {
803  //std::cout<<"bitzerocount "<<lsdata.bitzerocount<<std::endl;
804  //std::cout<<"bitzeroprescale "<<lsdata.bitzeroprescale<<std::endl;
805  //std::cout<<"product "<<lsdata.bitzerocount*lsdata.bitzeroprescale<<std::endl;
806  pIn1->setBitZeroCount(lsdata.bitzerocount * lsdata.bitzeroprescale);
807  }
808  pIn1->setlsnumber(luminum);
809  pIn1->setOrbitData(lsdata.startorbit, lsdata.numorbit);
810  std::vector<LumiSummary::L1> l1temp;
811  for (std::vector<L1Data>::iterator it = lsdata.l1data.begin(); it != lsdata.l1data.end(); ++it) {
812  LumiSummary::L1 trgtmp;
813  trgtmp.triggernameidx = m_runcache.TRGBitNameToIndex[it->bitname];
814  trgtmp.prescale = it->prescale;
815  l1temp.push_back(trgtmp);
816  }
817  std::vector<LumiSummary::HLT> hlttemp;
818  for (std::vector<HLTData>::iterator it = lsdata.hltdata.begin(); it != lsdata.hltdata.end(); ++it) {
819  LumiSummary::HLT hlttmp;
820  hlttmp.pathnameidx = m_runcache.HLTPathNameToIndex[it->pathname];
821  ;
822  hlttmp.prescale = it->prescale;
823  hlttemp.push_back(hlttmp);
824  }
825  pIn1->swapL1Data(l1temp);
826  pIn1->swapHLTData(hlttemp);
827  pIn1->setLumiVersion(m_lumiversion);
828  pIn2->fillBeamIntensities(lsdata.beam1intensity, lsdata.beam2intensity);
829  for (unsigned int i = 0; i < lsdata.bunchlumivalue.size(); ++i) {
830  std::string algoname = lsdata.bunchlumivalue[i].first;
831  if (algoname == "OCC1") {
832  pIn2->fill(LumiDetails::kOCC1,
833  lsdata.bunchlumivalue[i].second,
834  lsdata.bunchlumierror[i].second,
835  lsdata.bunchlumiquality[i].second);
836  } else if (algoname == "OCC2") {
837  pIn2->fill(LumiDetails::kOCC2,
838  lsdata.bunchlumivalue[i].second,
839  lsdata.bunchlumierror[i].second,
840  lsdata.bunchlumiquality[i].second);
841  } else if (algoname == "ET") {
842  pIn2->fill(LumiDetails::kET,
843  lsdata.bunchlumivalue[i].second,
844  lsdata.bunchlumierror[i].second,
845  lsdata.bunchlumiquality[i].second);
846  } else if (algoname == "PLT") {
847  pIn2->fill(LumiDetails::kPLT,
848  lsdata.bunchlumivalue[i].second,
849  lsdata.bunchlumierror[i].second,
850  lsdata.bunchlumiquality[i].second);
851  }
852  }
853  pIn2->setLumiVersion(m_lumiversion);
854  iLBlock.put(std::move(pIn1));
855  iLBlock.put(std::move(pIn2));
856 }
void writeProductsForEntry(edm::LuminosityBlock &iLBlock, unsigned int runnumber, unsigned int luminum)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
static const std::string trgdataTableName()
Definition: LumiNames.cc:11
unsigned int bitzeroprescale
std::vector< std::pair< std::string, std::vector< float > > > bunchlumivalue
std::string x2s(const XMLCh *input) const
unsigned int prescale
Definition: LumiProducer.cc:84
std::vector< float > beam1intensity
void produce(edm::Event &, const edm::EventSetup &) final
static const std::string lshltTableName()
Definition: LumiNames.cc:16
unsigned long long m_cachedhltdataid
LumiProducer(const edm::ParameterSet &)
unsigned long long getLumiDataId(const coral::ISchema &schema, unsigned int runnumber)
unsigned int ratecount
Definition: LumiProducer.cc:91
ISessionProxyPtr connectReadOnly(const std::string &connectstring)
Definition: DBService.cc:19
void beginLuminosityBlockProduce(edm::LuminosityBlock &iLBlock, edm::EventSetup const &iSetup) final
~LumiProducer() override
void xercesInitialize()
Definition: Xerces.cc:18
uint16_t size_type
static const std::string lstrgTableName()
Definition: LumiNames.cc:12
static std::string const input
Definition: EdmProvDump.cc:50
T getUntrackedParameter(std::string const &, T const &) const
static const std::string hltdataTableName()
Definition: LumiNames.cc:15
void free(void *ptr) noexcept
std::string m_connectStr
std::map< std::string, unsigned int > HLTPathNameToIndex
Definition: LumiProducer.cc:96
unsigned long long getHltDataId(const coral::ISchema &schema, unsigned int runnumber)
void beginRun(edm::Run const &, edm::EventSetup const &) final
unsigned int prescale
Definition: LumiSummary.h:26
void endRun(edm::Run const &, edm::EventSetup const &) final
void put(std::unique_ptr< PROD > product)
Put a new product.
unsigned int prescale
Definition: LumiProducer.cc:90
void fillLSCache(unsigned int luminum)
unsigned int l1passcount
Definition: LumiProducer.cc:85
static const std::string tagsTableName()
Definition: LumiNames.cc:18
unsigned int m_cachedrun
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
unsigned long long m_cachedtrgdataid
unsigned long long m_cachedlumidataid
static const std::string lumidataTableName()
Definition: LumiNames.cc:3
void * malloc(size_t size) noexcept
unsigned long long getTrgDataId(const coral::ISchema &schema, unsigned int runnumber)
std::string m_siteconfpath
std::vector< HLTData > hltdata
std::vector< std::string > TRGBitNames
Definition: LumiProducer.cc:97
static const std::string lumisummaryv2TableName()
Definition: LumiNames.cc:6
unsigned int m_cachesize
unsigned long long deadcount
std::vector< std::pair< std::string, std::vector< short > > > bunchlumiquality
HLT enums.
std::vector< L1Data > l1data
std::string m_lumiversion
std::string getCurrentDataTag(const coral::ISchema &schema)
bool fillLumi(edm::LuminosityBlock &iLBlock)
unsigned int acceptcount
Definition: LumiProducer.cc:86
std::string toParentString(const xercesc::DOMNode &nodeToConvert) const
bool isAvailable() const
Definition: Service.h:40
void fillRunCache(const coral::ISchema &schema, unsigned int runnumber)
std::map< unsigned int, PerLSData > m_lscache
Log< level::Warning, false > LogWarning
std::vector< std::string > HLTPathNames
Definition: LumiProducer.cc:98
tmp
align.sh
Definition: createJobs.py:716
LuminosityBlockNumber_t luminosityBlock() const
const std::string servletTranslation(const std::string &servlet) const
def move(src, dest)
Definition: eostools.py:511
std::map< std::string, unsigned int > TRGBitNameToIndex
Definition: LumiProducer.cc:95
void endRunProduce(edm::Run &, edm::EventSetup const &) final
Definition: Run.h:45
std::vector< float > beam2intensity
PerRunData m_runcache
unsigned int prescale
Definition: LumiSummary.h:32
std::vector< std::pair< std::string, std::vector< float > > > bunchlumierror
XMLCh * s2x(const std::string &input) const