test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ExpressLumiProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: LumiProducer
4 // Class: ExpressLumiProducer
5 //
9 // read lumi from dip database and dump to express stream
10 
23 
24 #include "CoralBase/Exception.h"
25 #include "CoralBase/AttributeList.h"
26 #include "CoralBase/Attribute.h"
27 #include "CoralBase/AttributeSpecification.h"
28 #include "CoralBase/Exception.h"
29 #include "CoralBase/Blob.h"
30 #include "RelationalAccess/ISessionProxy.h"
31 #include "RelationalAccess/ITransaction.h"
32 #include "RelationalAccess/AccessMode.h"
33 #include "RelationalAccess/ITypeConverter.h"
34 #include "RelationalAccess/IQuery.h"
35 #include "RelationalAccess/ICursor.h"
36 #include "RelationalAccess/ISchema.h"
37 #include "RelationalAccess/ITable.h"
38 
42 #include <iostream>
43 #include <sstream>
44 #include <string>
45 #include <memory>
46 #include <algorithm>
47 #include <vector>
48 #include <cstring>
49 #include <iterator>
50 #include <boost/foreach.hpp>
51 #include <boost/tokenizer.hpp>
52 #include "boost/filesystem/path.hpp"
53 #include "boost/filesystem/operations.hpp"
54 
55 namespace edm {
56  class EventSetup;
57 }
58 
59 //
60 // class declaration
61 //
62 class ExpressLumiProducer : public edm::one::EDProducer<edm::BeginLuminosityBlockProducer> {
63 public:
64  struct PerLSData{
65  unsigned int lsnum;
66  float lumivalue;
67  unsigned long long deadcount;
68  unsigned int numorbit;
69  unsigned int startorbit;
70  unsigned int bitzerocount;
71  std::vector<float> bunchlumivalue;
72  std::vector<float> bunchlumierror;
73  std::vector<short> bunchlumiquality;
74  };
75 
76  explicit ExpressLumiProducer(const edm::ParameterSet&);
77 
79 
80 private:
81 
82 
83  virtual void produce(edm::Event&, const edm::EventSetup&) override final;
84 
85  virtual void beginLuminosityBlockProduce(edm::LuminosityBlock & iLBlock,
86  edm::EventSetup const& iSetup) override final;
87 
88  bool fillLumi(edm::LuminosityBlock & iLBlock);
89  void fillLSCache(unsigned int runnum,unsigned int luminum);
90  void writeProductsForEntry(edm::LuminosityBlock & iLBlock,unsigned int luminum);
91  void writeEmptyProductForEntry(edm::LuminosityBlock &iLBlock);
92  unsigned int maxavailableLSforRun(coral::ISchema& schema,const std::string&tablename,unsigned int runnumber);
93  std::string m_connectStr;
94  unsigned int m_cachedrun;
95  bool m_isNullRun; //if lumi data exist for this run
96  unsigned int m_cachesize;
97  std::map< unsigned int,PerLSData > m_lscache;
98 };
99 
101 ExpressLumiProducer::ExpressLumiProducer(const edm::ParameterSet& iConfig):m_cachedrun(0),m_isNullRun(false),m_cachesize(0)
102 {
103  // register your products
104  produces<LumiSummary, edm::InLumi>();
105  produces<LumiDetails, edm::InLumi>();
106  // set up cache
107  m_connectStr=iConfig.getParameter<std::string>("connect");
108  m_cachesize=iConfig.getUntrackedParameter<unsigned int>("ncacheEntries",5);
109 }
110 
112 }
113 
114 //
115 // member functions
116 //
117 void
119 {
120 }
121 
122 void
124  std::auto_ptr<LumiSummary> pOut1;
125  std::auto_ptr<LumiDetails> pOut2;
126  LumiSummary* pIn1=new LumiSummary;
127  LumiDetails* pIn2=new LumiDetails;
128  pOut1.reset(pIn1);
129  iLBlock.put(pOut1);
130  pOut2.reset(pIn2);
131  iLBlock.put(pOut2);
132 }
133 void
135 {
136  unsigned int currentrun=iLBlock.run();
137  unsigned int currentls=iLBlock.luminosityBlock();
138  //if is null run, fill empty values and return
139  if(m_isNullRun){
140  writeEmptyProductForEntry(iLBlock);
141  return;
142  }
143  if(m_cachedrun!=currentrun){
144  fillLSCache(currentrun,currentls);
145  }else{
146  if(m_lscache.find(currentls)==m_lscache.end()){
147  //if runnumber is cached but LS is not, this is the first LS, fill LS cache to full capacity
148  fillLSCache(currentrun,currentls);
149  }
150  }
151  if( m_lscache.empty() ){
152  writeEmptyProductForEntry(iLBlock);
153  return;
154  }
155  unsigned int lstowriteout=0;
156  if(m_lscache.find(currentls)==m_lscache.end()){//if the currentls is not in the cache
157  std::vector<unsigned int> v;
158  for(std::map<unsigned int,ExpressLumiProducer::PerLSData >::iterator it=m_lscache.begin();it!=m_lscache.end();++it){
159  v.push_back(it->first);
160  }
161  lstowriteout=v.back();//last available
162  }else{//if the current ls is cached
163  lstowriteout=currentls;
164  }
165  //here the presence of ls is guaranteed
166  //std::cout<<"writing "<<runnumber<<" "<<luminum<<std::endl;
167  if(lstowriteout==0){
168  writeEmptyProductForEntry(iLBlock);
169  }else{
170  writeProductsForEntry(iLBlock,lstowriteout);
171  }
172 }
173 
174 unsigned int
175 ExpressLumiProducer::maxavailableLSforRun(coral::ISchema& schema,const std::string&tablename,unsigned int runnumber){
179  unsigned int result=0;
180  coral::AttributeList bindVariables;
181  bindVariables.extend("runnumber",typeid(unsigned int));
182  bindVariables["runnumber"].data<unsigned int>()=runnumber;
183  std::string conditionStr("RUNNUMBER=:runnumber");
184  coral::AttributeList MyOutput;
185  MyOutput.extend("maxavailablels",typeid(unsigned int));
186  coral::IQuery* myQuery=schema.newQuery();
187  myQuery->addToTableList(tablename);
188  myQuery->addToOutputList("max(LUMISECTION)","maxavailablels");
189  myQuery->setCondition(conditionStr,bindVariables);
190  myQuery->defineOutput(MyOutput);
191  coral::ICursor& mycursor=myQuery->execute();
192  while( mycursor.next() ){
193  const coral::AttributeList& row=mycursor.currentRow();
194  if(!row["maxavailablels"].isNull()){
195  result=row["maxavailablels"].data<unsigned int>();
196  }
197  }
198  return result;
199 }
200 void
201 ExpressLumiProducer::fillLSCache(unsigned int runnumber,unsigned int currentlsnum){
202  m_lscache.clear();
204  //
205  //queries once per cache refill
206  //
207  //select lumisection,instlumi,delivlumi,livelumi from cms_runtime_logger.lumi_sections where lumisection>=:lsmin and lumisection<:lsmax and runnumber=:runnumber;
208  //
210  if( !mydbservice.isAvailable() ){
211  throw cms::Exception("Non existing service lumi::service::DBService");
212  }
213  coral::ISessionProxy* session=mydbservice->connectReadOnly(m_connectStr);
214  coral::ITypeConverter& tconverter=session->typeConverter();
215  tconverter.setCppTypeForSqlType(std::string("float"),std::string("FLOAT(63)"));
216  tconverter.setCppTypeForSqlType(std::string("unsigned int"),std::string("NUMBER(10)"));
217  tconverter.setCppTypeForSqlType(std::string("unsigned short"),std::string("NUMBER(1)"));
218  unsigned int lsmin=1;
219  unsigned int lsmax=currentlsnum;
220  try{
221  session->transaction().start(true);
222  coral::ISchema& schema=session->nominalSchema();
223  unsigned int maxavailableLS=maxavailableLSforRun(schema,std::string("LUMI_SECTIONS"),m_cachedrun);
224  if(maxavailableLS!=0 && maxavailableLS<currentlsnum){
225  lsmax=maxavailableLS;
226  }else if(maxavailableLS==0){
227  //this run not existing (yet)
228  session->transaction().commit();
229  mydbservice->disconnect(session);
230  return;
231  }
232  if(m_cachesize!=0){
233  lsmin=(lsmax-m_cachesize)>0 ? (lsmax-m_cachesize+1) : 1;
234  }
235  for(unsigned int n=lsmin;n<=lsmax;++n){
236  PerLSData l;
237  std::vector<float> mytmp(3564,0.0);
238  l.bunchlumivalue.swap(mytmp);
239  std::vector<float> myerrtmp(3564,0.0);
240  l.bunchlumierror.swap(myerrtmp);
241  std::vector<short> myqtmp(3564,0);
242  l.bunchlumiquality.swap(myqtmp);
243  m_lscache.insert(std::make_pair(n,l));
244  }
245 
246  coral::AttributeList lumisummaryBindVariables;
247  lumisummaryBindVariables.extend("lsmin",typeid(unsigned int));
248  lumisummaryBindVariables.extend("runnumber",typeid(unsigned int));
249  lumisummaryBindVariables["runnumber"].data<unsigned int>()=m_cachedrun;
250  lumisummaryBindVariables["lsmin"].data<unsigned int>()=lsmin;
251  std::string conditionStr(" RUNNUMBER=:runnumber AND LUMISECTION>=:lsmin ");
252  coral::AttributeList lumisummaryOutput;
253  lumisummaryOutput.extend("LUMISECTION",typeid(unsigned int));
254  lumisummaryOutput.extend("INSTLUMI",typeid(float));
255  lumisummaryOutput.extend("DELIVLUMISECTION",typeid(float));
256  lumisummaryOutput.extend("LIVELUMISECTION",typeid(float));
257  lumisummaryOutput.extend("STARTORBIT",typeid(unsigned long long));
258  if(m_cachesize!=0){
259  lumisummaryBindVariables.extend("lsmax",typeid(unsigned int));
260  conditionStr=conditionStr+"AND LUMISECTION<=:lsmax";
261  lumisummaryBindVariables["lsmax"].data<unsigned int>()=lsmax;
262  }
263  coral::IQuery* lumisummaryQuery=schema.newQuery();
264  lumisummaryQuery->addToTableList(std::string("LUMI_SECTIONS"));
265  lumisummaryQuery->addToOutputList("LUMISECTION");
266  lumisummaryQuery->addToOutputList("INSTLUMI");
267  lumisummaryQuery->addToOutputList("DELIVLUMISECTION");
268  lumisummaryQuery->addToOutputList("LIVELUMISECTION");
269  lumisummaryQuery->addToOutputList("STARTORBIT");
270  lumisummaryQuery->setCondition(conditionStr,lumisummaryBindVariables);
271  lumisummaryQuery->defineOutput(lumisummaryOutput);
272  coral::ICursor& lumisummarycursor=lumisummaryQuery->execute();
273  unsigned int rowcounter=0;
274  while( lumisummarycursor.next() ){
275  const coral::AttributeList& row=lumisummarycursor.currentRow();
276  unsigned int lsnum=row["LUMISECTION"].data<unsigned int>();
277  float instlumi=0.0;
278  if(!row["INSTLUMI"].isNull()){
279  instlumi=row["INSTLUMI"].data<float>();//Hz/ub
280  }
281  float deadfrac=1.0;
282  float intgdellumi=0.0;
283  float intgreclumi=0.0;
284  unsigned long long startorbit=0;
285  if(!row["DELIVLUMISECTION"].isNull()){
286  intgdellumi=row["DELIVLUMISECTION"].data<float>()*1000.0;//convert to /ub
287  }
288  if(!row["LIVELUMISECTION"].isNull()){
289  intgreclumi=row["LIVELUMISECTION"].data<float>()*1000.0;//convert to /ub
290  }
291  if(intgdellumi>0){
292  deadfrac=1.0-intgreclumi/intgdellumi;
293  }
294  if(!row["STARTORBIT"].isNull()){
295  startorbit=row["STARTORBIT"].data<unsigned long long>();//convert to /ub
296  }
297  unsigned long long deadcount=deadfrac*10000.0;
298  unsigned long long bitzerocount=10000.0;
299  PerLSData& lsdata=m_lscache[lsnum];
300  lsdata.lsnum=lsnum;
301  lsdata.lumivalue=instlumi;
302  lsdata.deadcount=deadcount;
303  lsdata.bitzerocount=bitzerocount;
304  lsdata.startorbit=startorbit;
305  lsdata.numorbit=262144;
306  ++rowcounter;
307  }
308  if (rowcounter==0){
309  m_isNullRun=true;
310  }
311  delete lumisummaryQuery;
312  if(m_isNullRun) return;
313  //
314  //queries once per cache refill
315  //
316  //select lumisection,bunch,bunchlumi from cms_runtime_logger.bunch_lumi_sections where lumisection>=:lsmin and lumisection<:lsmax and runnumber=:runnumber;
317  //
318  coral::AttributeList lumidetailBindVariables;
319  lumidetailBindVariables.extend("lsmin",typeid(unsigned int));
320  lumidetailBindVariables.extend("runnumber",typeid(unsigned int));
321  lumidetailBindVariables["runnumber"].data<unsigned int>()=m_cachedrun;
322  lumidetailBindVariables["lsmin"].data<unsigned int>()=lsmin;
323  std::string detailconditionStr(" RUNNUMBER=:runnumber AND LUMISECTION>=:lsmin AND BUNCHLUMI>0 ");
324  coral::AttributeList lumidetailOutput;
325  lumidetailOutput.extend("LUMISECTION",typeid(unsigned int));
326  lumidetailOutput.extend("BUNCH",typeid(unsigned int));
327  lumidetailOutput.extend("BUNCHLUMI",typeid(float));
328  if(m_cachesize!=0){
329  lumidetailBindVariables.extend("lsmax",typeid(unsigned int));
330  detailconditionStr=detailconditionStr+"AND LUMISECTION<=:lsmax";
331  lumidetailBindVariables["lsmax"].data<unsigned int>()=lsmax;
332  }
333  coral::IQuery* lumidetailQuery=schema.newQuery();
334  lumidetailQuery->addToTableList(std::string("BUNCH_LUMI_SECTIONS"));
335  lumidetailQuery->addToOutputList("LUMISECTION");
336  lumidetailQuery->addToOutputList("BUNCH");
337  lumidetailQuery->addToOutputList("BUNCHLUMI");
338  lumidetailQuery->setCondition(detailconditionStr,lumidetailBindVariables);
339  lumidetailQuery->defineOutput(lumidetailOutput);
340  coral::ICursor& lumidetailcursor=lumidetailQuery->execute();
341  while( lumidetailcursor.next() ){
342  const coral::AttributeList& row=lumidetailcursor.currentRow();
343  unsigned int lsnum=row["LUMISECTION"].data<unsigned int>();
344  unsigned int bxidx=row["BUNCH"].data<unsigned int>();
345  float bxlumi=row["BUNCHLUMI"].data<float>();//Hz/ub
346  m_lscache[lsnum].bunchlumivalue[bxidx]=bxlumi;
347  }
348  delete lumidetailQuery;
349  session->transaction().commit();
350  }catch(const coral::Exception& er){
351  session->transaction().rollback();
352  mydbservice->disconnect(session);
353  throw cms::Exception("DatabaseError ")<<er.what();
354  }
355  mydbservice->disconnect(session);
356 }
357 void
359  //std::cout<<"writing runnumber,luminum "<<runnumber<<" "<<luminum<<std::endl;
360  std::auto_ptr<LumiSummary> pOut1;
361  std::auto_ptr<LumiDetails> pOut2;
362  LumiSummary* pIn1=new LumiSummary;
363  LumiDetails* pIn2=new LumiDetails;
364  if(m_isNullRun){
365  pIn1->setLumiVersion("DIP");
366  pIn2->setLumiVersion("DIP");
367  pOut1.reset(pIn1);
368  iLBlock.put(pOut1);
369  pOut2.reset(pIn2);
370  iLBlock.put(pOut2);
371  return;
372  }
373  PerLSData& lsdata=m_lscache[luminum];
374  pIn1->setLumiVersion("DIP");
375  pIn1->setLumiData(lsdata.lumivalue,0.0,0.0);
376  pIn1->setDeadCount(lsdata.deadcount);
377  pIn1->setBitZeroCount(lsdata.bitzerocount);
378  pIn1->setlsnumber(lsdata.lsnum);
379  pIn1->setOrbitData(lsdata.startorbit,lsdata.numorbit);
380 
381  pIn2->setLumiVersion("DIP");
383  pOut1.reset(pIn1);
384  iLBlock.put(pOut1);
385  pOut2.reset(pIn2);
386  iLBlock.put(pOut2);
387 }
virtual void produce(edm::Event &, const edm::EventSetup &) overridefinal
tuple schema
Definition: dataDML.py:2334
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual void beginLuminosityBlockProduce(edm::LuminosityBlock &iLBlock, edm::EventSetup const &iSetup) overridefinal
std::vector< short > bunchlumiquality
tuple result
Definition: mps_fire.py:84
LuminosityBlockNumber_t luminosityBlock() const
void setLumiVersion(std::string const &lumiVersion)
Definition: LumiDetails.cc:47
std::map< unsigned int, PerLSData > m_lscache
void setlsnumber(unsigned int lsnumber)
Definition: LumiSummary.cc:141
void setOrbitData(unsigned int startorbit, unsigned int numorbit)
Definition: LumiSummary.cc:145
void fill(AlgoType algo, std::vector< float > const &values, std::vector< float > const &errors, std::vector< short > const &qualities)
Definition: LumiDetails.cc:62
void fillLSCache(unsigned int runnum, unsigned int luminum)
bool isAvailable() const
Definition: Service.h:46
RunNumber_t run() const
void writeProductsForEntry(edm::LuminosityBlock &iLBlock, unsigned int luminum)
void disconnect(coral::ISessionProxy *session)
Definition: DBService.cc:27
void writeEmptyProductForEntry(edm::LuminosityBlock &iLBlock)
void setLumiVersion(const std::string &lumiversion)
Definition: LumiSummary.cc:123
void setLumiData(float instlumi, float instlumierr, short lumiquality)
Definition: LumiSummary.cc:127
void setBitZeroCount(unsigned long long bitzerocount)
Definition: LumiSummary.cc:137
unsigned int maxavailableLSforRun(coral::ISchema &schema, const std::string &tablename, unsigned int runnumber)
string const
Definition: compareJSON.py:14
void setDeadCount(unsigned long long deadcount)
Definition: LumiSummary.cc:133
coral::ISessionProxy * connectReadOnly(const std::string &connectstring)
Definition: DBService.cc:23
ExpressLumiProducer(const edm::ParameterSet &)
tuple runnum
Definition: summaryLumi.py:210
volatile std::atomic< bool > shutdown_flag false
session
Definition: models.py:201
bool fillLumi(edm::LuminosityBlock &iLBlock)
void put(std::auto_ptr< PROD > product)
Put a new product.