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