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