CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 //
20 // Created: Tue Jun 12 00:47:28 CEST 2007
21 // $Id: LumiProducer.cc,v 1.26.2.2 2012/10/23 14:21:21 xiezhen Exp $
22 
35 
36 #include "CoralBase/Exception.h"
37 #include "CoralBase/AttributeList.h"
38 #include "CoralBase/Attribute.h"
39 #include "CoralBase/AttributeSpecification.h"
40 #include "CoralBase/Exception.h"
41 #include "CoralBase/Blob.h"
42 #include "RelationalAccess/ISessionProxy.h"
43 #include "RelationalAccess/ITransaction.h"
44 #include "RelationalAccess/AccessMode.h"
45 #include "RelationalAccess/ITypeConverter.h"
46 #include "RelationalAccess/IQuery.h"
47 #include "RelationalAccess/ICursor.h"
48 #include "RelationalAccess/ISchema.h"
49 #include "RelationalAccess/ITable.h"
50 
52 
57 #include <iostream>
58 #include <sstream>
59 #include <string>
60 #include <memory>
61 #include <algorithm>
62 #include <vector>
63 #include <cstring>
64 #include <iterator>
65 #include <boost/foreach.hpp>
66 #include <boost/tokenizer.hpp>
67 #include <xercesc/dom/DOM.hpp>
68 #include <xercesc/parsers/XercesDOMParser.hpp>
69 #include <xercesc/util/PlatformUtils.hpp>
70 #include <xercesc/util/XMLString.hpp>
71 
72 #include "boost/filesystem/path.hpp"
73 #include "boost/filesystem/operations.hpp"
74 
75 namespace edm {
76  class EventSetup;
77 }
78 
79 //
80 // class declaration
81 //
82 class LumiProducer : public edm::EDProducer {
83 
84 public:
85 
86  struct HLTData{
87  std::string pathname;
88  unsigned int prescale;
89  unsigned int l1passcount;
90  unsigned int acceptcount;
91  };
92  struct L1Data{
93  std::string bitname;
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();
125 
126 private:
127 
128  virtual void produce(edm::Event&, const edm::EventSetup&);
129 
130  virtual void beginRun(edm::Run&, edm::EventSetup const &);
131 
132  virtual void beginLuminosityBlock(edm::LuminosityBlock & iLBlock,
133  edm::EventSetup const& iSetup);
134  virtual void endLuminosityBlock(edm::LuminosityBlock& lumiBlock,
135  edm::EventSetup const& c);
136 
137  virtual void endRun(edm::Run&, edm::EventSetup const &);
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 
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;
159  std::map< unsigned int,PerLSData > m_lscache;
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*
175 LumiProducer::s2x( const std::string& temp )const{
176  XMLCh* buff = xercesc::XMLString::transcode(temp.c_str());
177  return buff;
178 }
179 
180 std::string
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
208 LumiProducer::servletTranslation(const std::string& servlet) const{
209  std::string frontierConnect;
210  std::string realconnect;
211  xercesc::XMLPlatformUtils::Initialize();
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::InRun>();
250  produces<LumiSummary, edm::InLumi>();
251  produces<LumiDetails, edm::InLumi>();
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="v2"; /*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){
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 void
382 {
383  unsigned int runnumber=run.run();
384  if(m_cachedrun!=runnumber){
385  //queries once per run
388  if( !mydbservice.isAvailable() ){
389  throw cms::Exception("Non existing service lumi::service::DBService");
390  }
391  coral::ISessionProxy* session=mydbservice->connectReadOnly(m_connectStr);
392  try{
393  session->transaction().start(true);
394  m_cachedlumidataid=getLumiDataId(session->nominalSchema(),runnumber);
395  if(m_cachedlumidataid!=0){//if no lumi, do not bother other info
396  m_cachedtrgdataid=getTrgDataId(session->nominalSchema(),runnumber);
397  m_cachedhltdataid=getHltDataId(session->nominalSchema(),runnumber);
398  fillRunCache(session->nominalSchema(),runnumber);
399  }else{
400  m_isNullRun=true;
401  }
402  session->transaction().commit();
403  }catch(const coral::Exception& er){
404  session->transaction().rollback();
405  mydbservice->disconnect(session);
406  throw cms::Exception("DatabaseError ")<<er.what();
407  }
408  mydbservice->disconnect(session);
409  }
410  //std::cout<<"end of beginRun "<<runnumber<<std::endl;
411 }
412 
414 {
415  unsigned int runnumber=iLBlock.run();
416  unsigned int luminum=iLBlock.luminosityBlock();
417  //std::cout<<"beg of beginLuminosityBlock "<<luminum<<std::endl;
418  //if is null run, fill empty values and return
419  if(m_isNullRun){
420  std::auto_ptr<LumiSummary> pOut1;
421  std::auto_ptr<LumiDetails> pOut2;
422  LumiSummary* pIn1=new LumiSummary;
423  LumiDetails* pIn2=new LumiDetails;
424  pOut1.reset(pIn1);
425  iLBlock.put(pOut1);
426  pOut2.reset(pIn2);
427  iLBlock.put(pOut2);
428  return;
429  }
430  if(m_lscache.find(luminum)==m_lscache.end()){
431  //if runnumber is cached but LS is not, this is the first LS, fill LS cache to full capacity
432  fillLSCache(luminum);
433  }
434  //here the presence of ls is guaranteed
435  writeProductsForEntry(iLBlock,runnumber,luminum);
436 }
437 void
439 {
440 }
441 void
443 {
444  std::auto_ptr<LumiSummaryRunHeader> lsrh(new LumiSummaryRunHeader());
445  lsrh->swapL1Names(m_runcache.TRGBitNames);
446  lsrh->swapHLTNames(m_runcache.HLTPathNames);
447  run.put(lsrh);
450 }
451 void
452 LumiProducer::fillRunCache(const coral::ISchema& schema,unsigned int runnumber){
453  if(m_cachedtrgdataid!=0){
454  coral::AttributeList trgBindVariables;
455  trgBindVariables.extend("trgdataid",typeid(unsigned long long));
456  trgBindVariables["trgdataid"].data<unsigned long long>()=m_cachedtrgdataid;
457  //std::cout<<"cached trgdataid "<<m_cachedtrgdataid<<std::endl;
458  coral::AttributeList trgOutput;
459  trgOutput.extend("bitzeroname",typeid(std::string));
460  trgOutput.extend("bitnameclob",typeid(std::string));
461  coral::IQuery* trgQuery=schema.newQuery();
462  trgQuery->addToTableList(lumi::LumiNames::trgdataTableName());
463  trgQuery->addToOutputList("BITZERONAME");
464  trgQuery->addToOutputList("BITNAMECLOB");
465  trgQuery->setCondition("DATA_ID=:trgdataid",trgBindVariables);
466  trgQuery->defineOutput(trgOutput);
467  coral::ICursor& trgcursor=trgQuery->execute();
468  while( trgcursor.next() ){
469  const coral::AttributeList& row=trgcursor.currentRow();
470  m_runcache.bitzeroname=row["bitzeroname"].data<std::string>();
471  //std::cout<<"bitzeroname "<<m_runcache.bitzeroname<<std::endl;
472  std::string bitnames=row["bitnameclob"].data<std::string>();
473  boost::char_separator<char> sep(",");
474  boost::tokenizer<boost::char_separator<char> > tokens(bitnames,sep);
475  for(boost::tokenizer<boost::char_separator<char> >::iterator tok_it=tokens.begin();tok_it!=tokens.end();++tok_it){
476  m_runcache.TRGBitNames.push_back(*tok_it);
477  }
478  for(unsigned int i=0;i<m_runcache.TRGBitNames.size();++i){
479  m_runcache.TRGBitNameToIndex.insert(std::make_pair(m_runcache.TRGBitNames.at(i),i) );
480  }
481  }
482  delete trgQuery;
483  }
484  if(m_cachedhltdataid!=0){
485  //
486  //select pathnameclob from hltdata where data_id=:hltdataid
487  //
488  coral::AttributeList hltBindVariables;
489  hltBindVariables.extend("hltdataid",typeid(unsigned long long));
490  hltBindVariables["hltdataid"].data<unsigned long long>()=m_cachedhltdataid;
491  coral::AttributeList hltOutput;
492  hltOutput.extend("PATHNAMECLOB",typeid(std::string));
493  coral::IQuery* hltQuery=schema.newQuery();
494  hltQuery->addToTableList(lumi::LumiNames::hltdataTableName());
495  hltQuery->addToOutputList("PATHNAMECLOB");
496  hltQuery->setCondition("DATA_ID=:hltdataid",hltBindVariables);
497  hltQuery->defineOutput(hltOutput);
498  coral::ICursor& hltcursor=hltQuery->execute();
499  while( hltcursor.next() ){
500  const coral::AttributeList& row=hltcursor.currentRow();
501  std::string pathnames=row["PATHNAMECLOB"].data<std::string>();
502  boost::char_separator<char> sep(",");
503  boost::tokenizer<boost::char_separator<char> > tokens(pathnames,sep);
504  for(boost::tokenizer<boost::char_separator<char> >::iterator tok_it=tokens.begin();tok_it!=tokens.end();++tok_it){
505  m_runcache.HLTPathNames.push_back(*tok_it);
506  }
507  for(unsigned int i=0;i<m_runcache.HLTPathNames.size();++i){
508  m_runcache.HLTPathNameToIndex.insert(std::make_pair(m_runcache.HLTPathNames.at(i),i));
509  }
510  }
511  delete hltQuery;
512  }
513 }
514 void
515 LumiProducer::fillLSCache(unsigned int luminum){
516  //initialize cache
517  if(m_isNullRun) return;
518  m_lscache.clear();
519  for(unsigned int n=luminum;n<luminum+m_cachesize;++n){
520  PerLSData l;
521  l.hltdata.reserve(250);
522  l.l1data.reserve(192);
523  l.bunchlumivalue.reserve(5);
524  l.bunchlumierror.reserve(5);
525  l.bunchlumiquality.reserve(5);
526  l.beam1intensity.resize(3564,0.0);
527  l.beam2intensity.resize(3564,0.0);
528  m_lscache.insert(std::make_pair(n,l));
529  }
530  //queries once per cache refill
531  //
532  //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;
533  //
535  if( !mydbservice.isAvailable() ){
536  throw cms::Exception("Non existing service lumi::service::DBService");
537  }
538  coral::ISessionProxy* session=mydbservice->connectReadOnly(m_connectStr);
539  try{
540  session->transaction().start(true);
541  coral::ISchema& schema=session->nominalSchema();
542  coral::AttributeList lumisummaryBindVariables;
543  lumisummaryBindVariables.extend("lsmin",typeid(unsigned int));
544  lumisummaryBindVariables.extend("lsmax",typeid(unsigned int));
545  lumisummaryBindVariables.extend("lumidataid",typeid(unsigned long long));
546  lumisummaryBindVariables["lumidataid"].data<unsigned long long>()=m_cachedlumidataid;
547  lumisummaryBindVariables["lsmin"].data<unsigned int>()=luminum;
548  lumisummaryBindVariables["lsmax"].data<unsigned int>()=luminum+m_cachesize;
549  coral::AttributeList lumisummaryOutput;
550  lumisummaryOutput.extend("CMSLSNUM",typeid(unsigned int));
551  lumisummaryOutput.extend("INSTLUMI",typeid(float));
552  lumisummaryOutput.extend("STARTORBIT",typeid(unsigned int));
553  lumisummaryOutput.extend("NUMORBIT",typeid(unsigned int));
554  lumisummaryOutput.extend("CMSBXINDEXBLOB",typeid(coral::Blob));
555  lumisummaryOutput.extend("BEAMINTENSITYBLOB_1",typeid(coral::Blob));
556  lumisummaryOutput.extend("BEAMINTENSITYBLOB_2",typeid(coral::Blob));
557  lumisummaryOutput.extend("BXLUMIVALUE_OCC1",typeid(coral::Blob));
558  lumisummaryOutput.extend("BXLUMIVALUE_OCC2",typeid(coral::Blob));
559  lumisummaryOutput.extend("BXLUMIVALUE_ET",typeid(coral::Blob));
560  coral::IQuery* lumisummaryQuery=schema.newQuery();
561  lumisummaryQuery->addToTableList(lumi::LumiNames::lumisummaryv2TableName());
562  lumisummaryQuery->addToOutputList("CMSLSNUM");
563  lumisummaryQuery->addToOutputList("INSTLUMI");
564  lumisummaryQuery->addToOutputList("STARTORBIT");
565  lumisummaryQuery->addToOutputList("NUMORBIT");
566  lumisummaryQuery->addToOutputList("CMSBXINDEXBLOB");
567  lumisummaryQuery->addToOutputList("BEAMINTENSITYBLOB_1");
568  lumisummaryQuery->addToOutputList("BEAMINTENSITYBLOB_2");
569  lumisummaryQuery->addToOutputList("BXLUMIVALUE_OCC1");
570  lumisummaryQuery->addToOutputList("BXLUMIVALUE_OCC2");
571  lumisummaryQuery->addToOutputList("BXLUMIVALUE_ET");
572  lumisummaryQuery->setCondition("CMSLSNUM>=:lsmin AND CMSLSNUM<:lsmax AND DATA_ID=:lumidataid",lumisummaryBindVariables);
573  lumisummaryQuery->defineOutput(lumisummaryOutput);
574  coral::ICursor& lumisummarycursor=lumisummaryQuery->execute();
575  unsigned int rowcounter=0;
576  while( lumisummarycursor.next() ){
577  const coral::AttributeList& row=lumisummarycursor.currentRow();
578  unsigned int cmslsnum=row["CMSLSNUM"].data<unsigned int>();
579  //std::cout<<"cmslsnum "<<cmslsnum<<std::endl;
580  PerLSData& lsdata=m_lscache[cmslsnum];
581  lsdata.lumivalue=row["INSTLUMI"].data<float>();
582  lsdata.lumierror=0.0;
583  lsdata.lumiquality=0;
584  lsdata.startorbit=row["STARTORBIT"].data<unsigned int>();
585  lsdata.numorbit=row["NUMORBIT"].data<unsigned int>();
586 
587  if(!row["CMSBXINDEXBLOB"].isNull() && !row["BXLUMIVALUE_OCC1"].isNull() ){
588  const coral::Blob& bxindexBlob=row["CMSBXINDEXBLOB"].data<coral::Blob>();
589  const void* bxindex_StartAddress=bxindexBlob.startingAddress();
590  short* bxindex=(short*)::malloc(bxindexBlob.size());
591  const coral::Blob& beam1intensityBlob=row["BEAMINTENSITYBLOB_1"].data<coral::Blob>();
592  const void* beam1intensityBlob_StartAddress=beam1intensityBlob.startingAddress();
593  float* beam1intensity=(float*)::malloc(beam1intensityBlob.size());
594  const coral::Blob& beam2intensityBlob=row["BEAMINTENSITYBLOB_2"].data<coral::Blob>();
595  const void* beam2intensityBlob_StartAddress=beam2intensityBlob.startingAddress();
596  float* beam2intensity=(float*)::malloc(beam2intensityBlob.size());
597  std::memmove(bxindex,bxindex_StartAddress,bxindexBlob.size());
598  std::memmove(beam1intensity,beam1intensityBlob_StartAddress,beam1intensityBlob.size());
599  std::memmove(beam2intensity,beam2intensityBlob_StartAddress,beam2intensityBlob.size());
600 
601  unsigned int iMax = bxindexBlob.size()/sizeof(short);
602  unsigned int lsb1Max = lsdata.beam1intensity.size();
603  unsigned int lsb2Max = lsdata.beam2intensity.size();
604  unsigned int ib1Max = beam1intensityBlob.size()/sizeof(float);
605  unsigned int ib2Max = beam2intensityBlob.size()/sizeof(float);
606  for(unsigned int i=0;i<iMax;++i){
607  unsigned int idx=bxindex[i];
608  if(ib1Max>i && lsb1Max>idx){
609  lsdata.beam1intensity.at(idx)=beam1intensity[i];
610  }
611  if(ib2Max>i && lsb2Max>idx){
612  lsdata.beam2intensity.at(idx)=beam2intensity[i];
613  }
614  }
615  ::free(bxindex);
616  ::free(beam1intensity);
617  ::free(beam2intensity);
618 
619  const coral::Blob& bxlumivalBlob_occ1=row["BXLUMIVALUE_OCC1"].data<coral::Blob>();
620  const void* bxlumival_occ1_StartAddress=bxlumivalBlob_occ1.startingAddress();
621  float* bxlumival_occ1=(float*)::malloc(bxlumivalBlob_occ1.size());
622  std::memmove(bxlumival_occ1,bxlumival_occ1_StartAddress,bxlumivalBlob_occ1.size());
623  std::vector<float> bxlumivalVec_occ1(bxlumival_occ1,bxlumival_occ1+bxlumivalBlob_occ1.size()/sizeof(float));
624  ::free(bxlumival_occ1);
625  lsdata.bunchlumivalue.push_back(std::make_pair(std::string("OCC1"),bxlumivalVec_occ1));
626  lsdata.bunchlumierror.push_back(std::make_pair(std::string("OCC1"),std::vector<float>(3564)));
627  lsdata.bunchlumiquality.push_back(std::make_pair(std::string("OCC1"),std::vector<short>(3564)));
628  const coral::Blob& bxlumivalBlob_occ2=row["BXLUMIVALUE_OCC2"].data<coral::Blob>();
629  const void* bxlumival_occ2_StartAddress=bxlumivalBlob_occ2.startingAddress();
630  float* bxlumival_occ2=(float*)::malloc(bxlumivalBlob_occ2.size());
631  std::memmove(bxlumival_occ2,bxlumival_occ2_StartAddress,bxlumivalBlob_occ2.size());
632  std::vector<float> bxlumivalVec_occ2(bxlumival_occ2,bxlumival_occ2+bxlumivalBlob_occ1.size()/sizeof(float));
633  ::free(bxlumival_occ2);
634  lsdata.bunchlumivalue.push_back(std::make_pair(std::string("OCC2"),bxlumivalVec_occ2));
635  lsdata.bunchlumierror.push_back(std::make_pair(std::string("OCC2"),std::vector<float>(3564)));
636  lsdata.bunchlumiquality.push_back(std::make_pair(std::string("OCC2"),std::vector<short>(3564)));
637 
638  const coral::Blob& bxlumivalBlob_et=row["BXLUMIVALUE_ET"].data<coral::Blob>();
639  const void* bxlumival_et_StartAddress=bxlumivalBlob_et.startingAddress();
640  float* bxlumival_et=(float*)::malloc(bxlumivalBlob_et.size());
641  std::memmove(bxlumival_et,bxlumival_et_StartAddress,bxlumivalBlob_et.size());
642  std::vector<float> bxlumivalVec_et(bxlumival_et,bxlumival_et+bxlumivalBlob_et.size()/sizeof(float));
643  ::free(bxlumival_et);
644  lsdata.bunchlumivalue.push_back(std::make_pair(std::string("ET"),bxlumivalVec_et));
645  lsdata.bunchlumierror.push_back(std::make_pair(std::string("ET"),std::vector<float>(3564)));
646  lsdata.bunchlumiquality.push_back(std::make_pair(std::string("ET"),std::vector<short>(3564)));
647  }
648  ++rowcounter;
649  }
650  if (rowcounter==0){
651  m_isNullRun=true;
652  return;
653  }
654  delete lumisummaryQuery;
655 
656  //
657  //select cmslsnum,deadtimecount,bitzerocount,bitzeroprescale,prescaleblob,trgcountblob from lstrg where cmslsnum >=:luminum and cmslsnum<:luminum+cachesize AND data_id=:trgdataid;
658  //
659  coral::AttributeList trgBindVariables;
660  trgBindVariables.extend("lsmin",typeid(unsigned int));
661  trgBindVariables.extend("lsmax",typeid(unsigned int));
662  trgBindVariables.extend("trgdataid",typeid(unsigned long long));
663  trgBindVariables["lsmin"].data<unsigned int>()=luminum;
664  trgBindVariables["lsmax"].data<unsigned int>()=luminum+m_cachesize;
665  trgBindVariables["trgdataid"].data<unsigned long long>()=m_cachedtrgdataid;
666  coral::AttributeList trgOutput;
667  trgOutput.extend("CMSLSNUM",typeid(unsigned int));
668  trgOutput.extend("DEADTIMECOUNT",typeid(unsigned long long));
669  trgOutput.extend("BITZEROCOUNT",typeid(unsigned int));
670  trgOutput.extend("BITZEROPRESCALE",typeid(unsigned int));
671  trgOutput.extend("PRESCALEBLOB",typeid(coral::Blob));
672  trgOutput.extend("TRGCOUNTBLOB",typeid(coral::Blob));
673 
674  coral::IQuery* trgQuery=schema.newQuery();
675  trgQuery->addToTableList(lumi::LumiNames::lstrgTableName());
676  trgQuery->addToOutputList("CMSLSNUM");
677  trgQuery->addToOutputList("DEADTIMECOUNT");
678  trgQuery->addToOutputList("BITZEROCOUNT");
679  trgQuery->addToOutputList("BITZEROPRESCALE");
680  trgQuery->addToOutputList("PRESCALEBLOB");
681  trgQuery->addToOutputList("TRGCOUNTBLOB");
682  trgQuery->setCondition("CMSLSNUM>=:lsmin AND CMSLSNUM<:lsmax AND DATA_ID=:trgdataid",trgBindVariables);
683  trgQuery->defineOutput(trgOutput);
684  coral::ICursor& trgcursor=trgQuery->execute();
685  while( trgcursor.next() ){
686  const coral::AttributeList& row=trgcursor.currentRow();
687  unsigned int cmslsnum=row["CMSLSNUM"].data<unsigned int>();
688  PerLSData& lsdata=m_lscache[cmslsnum];
689  lsdata.deadcount=row["DEADTIMECOUNT"].data<unsigned long long>();
690  lsdata.bitzerocount=row["BITZEROCOUNT"].data<unsigned int>();
691  lsdata.bitzeroprescale=row["BITZEROPRESCALE"].data<unsigned int>();
692  if(!row["PRESCALEBLOB"].isNull()){
693  const coral::Blob& prescaleblob=row["PRESCALEBLOB"].data<coral::Blob>();
694  const void* prescaleblob_StartAddress=prescaleblob.startingAddress();
695  unsigned int* prescales=(unsigned int*)::malloc(prescaleblob.size());
696  std::memmove(prescales,prescaleblob_StartAddress,prescaleblob.size());
697  const coral::Blob& trgcountblob=row["TRGCOUNTBLOB"].data<coral::Blob>();
698  const void* trgcountblob_StartAddress=trgcountblob.startingAddress();
699  unsigned int* trgcounts=(unsigned int*)::malloc(trgcountblob.size());
700  std::memmove(trgcounts,trgcountblob_StartAddress,trgcountblob.size());
701  for(unsigned int i=0;i<sizeof(trgcounts)/sizeof(unsigned int);++i){
702  L1Data l1tmp;
704  l1tmp.prescale=prescales[i];
705  l1tmp.ratecount=trgcounts[i];
706  lsdata.l1data.push_back(l1tmp);
707  }
708  ::free(prescales);
709  ::free(trgcounts);
710  }
711  }
712  delete trgQuery;
713  //
714  //select cmslsnum,hltcountblob,hltacceptblob,prescaleblob from hlt where cmslsnum >=:luminum and cmslsnum<=:luminum+cachesize and data_id=:hltdataid
715  //
716  coral::AttributeList hltBindVariables;
717  hltBindVariables.extend("lsmin",typeid(unsigned int));
718  hltBindVariables.extend("lsmax",typeid(unsigned int));
719  hltBindVariables.extend("hltdataid",typeid(unsigned long long));
720  hltBindVariables["lsmin"].data<unsigned int>()=luminum;
721  hltBindVariables["lsmax"].data<unsigned int>()=luminum+m_cachesize;
722  hltBindVariables["hltdataid"].data<unsigned long long>()=m_cachedhltdataid;
723  coral::AttributeList hltOutput;
724  hltOutput.extend("CMSLSNUM",typeid(unsigned int));
725  hltOutput.extend("HLTCOUNTBLOB",typeid(coral::Blob));
726  hltOutput.extend("HLTACCEPTBLOB",typeid(coral::Blob));
727  hltOutput.extend("PRESCALEBLOB",typeid(coral::Blob));
728  coral::IQuery* hltQuery=schema.newQuery();
729  hltQuery->addToTableList(lumi::LumiNames::lshltTableName());
730  hltQuery->addToOutputList("CMSLSNUM");
731  hltQuery->addToOutputList("HLTCOUNTBLOB");
732  hltQuery->addToOutputList("HLTACCEPTBLOB");
733  hltQuery->addToOutputList("PRESCALEBLOB");
734  hltQuery->setCondition("CMSLSNUM>=:lsmin AND CMSLSNUM<:lsmax AND DATA_ID=:hltdataid",hltBindVariables);
735  hltQuery->defineOutput(hltOutput);
736  coral::ICursor& hltcursor=hltQuery->execute();
737  while( hltcursor.next() ){
738  const coral::AttributeList& row=hltcursor.currentRow();
739  unsigned int cmslsnum=row["CMSLSNUM"].data<unsigned int>();
740  PerLSData& lsdata=m_lscache[cmslsnum];
741  if(!row["PRESCALEBLOB"].isNull()){
742  const coral::Blob& hltprescaleblob=row["PRESCALEBLOB"].data<coral::Blob>();
743  const void* hltprescaleblob_StartAddress=hltprescaleblob.startingAddress();
744  unsigned int* hltprescales=(unsigned int*)::malloc(hltprescaleblob.size());
745  std::memmove(hltprescales,hltprescaleblob_StartAddress,hltprescaleblob.size());
746  const coral::Blob& hltcountblob=row["HLTCOUNTBLOB"].data<coral::Blob>();
747  const void* hltcountblob_StartAddress=hltcountblob.startingAddress();
748  unsigned int* hltcounts=(unsigned int*)::malloc(hltcountblob.size());
749  std::memmove(hltcounts,hltcountblob_StartAddress,hltcountblob.size());
750  const coral::Blob& hltacceptblob=row["HLTACCEPTBLOB"].data<coral::Blob>();
751  const void* hltacceptblob_StartAddress=hltacceptblob.startingAddress();
752  unsigned int* hltaccepts=(unsigned int*)::malloc(hltacceptblob.size());
753  std::memmove(hltaccepts,hltacceptblob_StartAddress,hltacceptblob.size());
754  unsigned int nhltaccepts = sizeof(hltaccepts)/sizeof(unsigned int);
755  if(nhltaccepts > 0 && m_runcache.HLTPathNames.size() == 0){
756  edm::LogWarning("CorruptOrMissingHLTData")<<"Got "<<nhltaccepts
757 <<" hltaccepts, but the run chache is empty. hltdata will not be written";
758  break;
759  }
760 
761  for(unsigned int i=0;i<sizeof(hltaccepts)/sizeof(unsigned int);++i){
762  HLTData hlttmp;
764  hlttmp.prescale=hltprescales[i];
765  hlttmp.l1passcount=hltcounts[i];
766  hlttmp.acceptcount=hltaccepts[i];
767  lsdata.hltdata.push_back(hlttmp);
768  }
769  ::free(hltprescales);
770  ::free(hltcounts);
771  ::free(hltaccepts);
772  }
773  }
774  delete hltQuery;
775  session->transaction().commit();
776  }catch(const coral::Exception& er){
777  session->transaction().rollback();
778  mydbservice->disconnect(session);
779  throw cms::Exception("DatabaseError ")<<er.what();
780  }
781  mydbservice->disconnect(session);
782 }
783 void
784 LumiProducer::writeProductsForEntry(edm::LuminosityBlock & iLBlock,unsigned int runnumber,unsigned int luminum){
785  //std::cout<<"writing runnumber,luminum "<<runnumber<<" "<<luminum<<std::endl;
786  std::auto_ptr<LumiSummary> pOut1;
787  std::auto_ptr<LumiDetails> pOut2;
788  LumiSummary* pIn1=new LumiSummary;
789  LumiDetails* pIn2=new LumiDetails;
790  if(m_isNullRun){
791  pIn1->setLumiVersion("-1");
792  pIn2->setLumiVersion("-1");
793  pOut1.reset(pIn1);
794  iLBlock.put(pOut1);
795  pOut2.reset(pIn2);
796  iLBlock.put(pOut2);
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  hlttmp.prescale=it->prescale;
822  hlttemp.push_back(hlttmp);
823  }
824  pIn1->swapL1Data(l1temp);
825  pIn1->swapHLTData(hlttemp);
826  pIn1->setLumiVersion(m_lumiversion);
828  for(unsigned int i=0;i<lsdata.bunchlumivalue.size();++i){
829  std::string algoname=lsdata.bunchlumivalue[i].first;
830  if(algoname=="OCC1"){
831  pIn2->fill(LumiDetails::kOCC1,lsdata.bunchlumivalue[i].second,lsdata.bunchlumierror[i].second,lsdata.bunchlumiquality[i].second);
832  }else if(algoname=="OCC2"){
833  pIn2->fill(LumiDetails::kOCC2,lsdata.bunchlumivalue[i].second,lsdata.bunchlumierror[i].second,lsdata.bunchlumiquality[i].second);
834  }else if(algoname=="ET"){
835  pIn2->fill(LumiDetails::kET,lsdata.bunchlumivalue[i].second,lsdata.bunchlumierror[i].second,lsdata.bunchlumiquality[i].second);
836  }else if(algoname=="PLT"){
837  pIn2->fill(LumiDetails::kPLT,lsdata.bunchlumivalue[i].second,lsdata.bunchlumierror[i].second,lsdata.bunchlumiquality[i].second);
838  }
839  }
840  pIn2->setLumiVersion(m_lumiversion);
841  pOut1.reset(pIn1);
842  iLBlock.put(pOut1);
843  pOut2.reset(pIn2);
844  iLBlock.put(pOut2);
845 }
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
int i
Definition: DBlmapReader.cc:9
std::vector< std::pair< std::string, std::vector< float > > > bunchlumivalue
static const std::string trgdataTableName()
Definition: LumiNames.cc:50
RunNumber_t run() const
Definition: RunBase.h:42
unsigned int bitzeroprescale
unsigned int prescale
Definition: LumiProducer.cc:88
std::vector< float > beam1intensity
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
static const std::string lshltTableName()
Definition: LumiNames.cc:59
unsigned long long m_cachedhltdataid
LumiProducer(const edm::ParameterSet &)
void swapHLTData(std::vector< HLT > &hltdata)
Definition: LumiSummary.cc:154
std::string x2s(const XMLCh *input) const
XMLCh * s2x(const std::string &input) const
boost::tokenizer< boost::char_separator< char > > tokenizer
unsigned long long getLumiDataId(const coral::ISchema &schema, unsigned int runnumber)
unsigned int ratecount
Definition: LumiProducer.cc:95
uint16_t size_type
static const std::string lstrgTableName()
Definition: LumiNames.cc:53
LuminosityBlockNumber_t luminosityBlock() const
void setLumiVersion(std::string const &lumiVersion)
Definition: LumiDetails.cc:36
static const std::string hltdataTableName()
Definition: LumiNames.cc:56
void setlsnumber(unsigned int lsnumber)
Definition: LumiSummary.cc:141
std::string m_connectStr
std::map< std::string, unsigned int > HLTPathNameToIndex
unsigned long long getHltDataId(const coral::ISchema &schema, unsigned int runnumber)
list path
Definition: scaleCards.py:51
unsigned int prescale
Definition: LumiSummary.h:27
void setOrbitData(unsigned int startorbit, unsigned int numorbit)
Definition: LumiSummary.cc:145
std::string toParentString(const xercesc::DOMNode &nodeToConvert) const
std::vector< std::pair< std::string, std::vector< float > > > bunchlumierror
void fill(AlgoType algo, std::vector< float > const &values, std::vector< float > const &errors, std::vector< short > const &qualities)
Definition: LumiDetails.cc:51
unsigned int prescale
Definition: LumiProducer.cc:94
void fillLSCache(unsigned int luminum)
unsigned int l1passcount
Definition: LumiProducer.cc:89
std::vector< HLTData > hltdata
list attributes
Definition: asciidump.py:415
bool isAvailable() const
Definition: Service.h:47
int j
Definition: DBlmapReader.cc:9
RunNumber_t run() const
tuple pathnames
Definition: lumiPlot.py:411
virtual void beginRun(edm::Run &, edm::EventSetup const &)
unsigned int m_cachedrun
tuple doc
Definition: asciidump.py:381
unsigned long long m_cachedtrgdataid
void disconnect(coral::ISessionProxy *session)
Definition: DBService.cc:46
unsigned long long m_cachedlumidataid
virtual void endRun(edm::Run &, edm::EventSetup const &)
void setLumiVersion(const std::string &lumiversion)
Definition: LumiSummary.cc:123
static const std::string lumidataTableName()
Definition: LumiNames.cc:41
virtual void endLuminosityBlock(edm::LuminosityBlock &lumiBlock, edm::EventSetup const &c)
void setLumiData(float instlumi, float instlumierr, short lumiquality)
Definition: LumiSummary.cc:127
void setBitZeroCount(unsigned long long bitzerocount)
Definition: LumiSummary.cc:137
unsigned long long getTrgDataId(const coral::ISchema &schema, unsigned int runnumber)
std::string m_siteconfpath
std::map< unsigned int, PerLSData > m_lscache
std::vector< L1Data > l1data
void setDeadCount(unsigned long long deadcount)
Definition: LumiSummary.cc:133
std::vector< std::string > TRGBitNames
static const std::string lumisummaryv2TableName()
Definition: LumiNames.cc:44
void swapL1Data(std::vector< L1 > &l1data)
Definition: LumiSummary.cc:150
unsigned int m_cachesize
unsigned long long deadcount
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
virtual void beginLuminosityBlock(edm::LuminosityBlock &iLBlock, edm::EventSetup const &iSetup)
void fillBeamIntensities(std::vector< float > const &beam1Intensities, std::vector< float > const &beam2Intensities)
Definition: LumiDetails.cc:74
std::string m_lumiversion
bool fillLumi(edm::LuminosityBlock &iLBlock)
unsigned int acceptcount
Definition: LumiProducer.cc:90
coral::ISessionProxy * connectReadOnly(const std::string &connectstring)
Definition: DBService.cc:42
void put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Run.h:81
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 produce(edm::Event &, const edm::EventSetup &)
std::vector< std::pair< std::string, std::vector< short > > > bunchlumiquality
std::map< std::string, unsigned int > TRGBitNameToIndex
Definition: LumiProducer.cc:99
Definition: Run.h:33
std::vector< float > beam2intensity
PerRunData m_runcache
unsigned int prescale
Definition: LumiSummary.h:33
void put(std::auto_ptr< PROD > product)
Put a new product.