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