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.4 2013/05/17 20:54:13 chrjones 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 //
63 class ExpressLumiProducer : public edm::one::EDProducer<edm::BeginLuminosityBlockProducer> {
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&) override final;
85 
86  virtual void beginLuminosityBlockProduce(edm::LuminosityBlock & iLBlock,
87  edm::EventSetup const& iSetup) override final;
88 
89  bool fillLumi(edm::LuminosityBlock & iLBlock);
90  void fillLSCache(unsigned int runnum,unsigned int luminum);
91  void writeProductsForEntry(edm::LuminosityBlock & iLBlock,unsigned int luminum);
92  void writeEmptyProductForEntry(edm::LuminosityBlock &iLBlock);
93  unsigned int maxavailableLSforRun(coral::ISchema& schema,const std::string&tablename,unsigned int runnumber);
94  std::string m_connectStr;
95  unsigned int m_cachedrun;
96  bool m_isNullRun; //if lumi data exist for this run
97  unsigned int m_cachesize;
98  std::map< unsigned int,PerLSData > m_lscache;
99 };
100 
102 ExpressLumiProducer::ExpressLumiProducer(const edm::ParameterSet& iConfig):m_cachedrun(0),m_isNullRun(false),m_cachesize(0)
103 {
104  // register your products
105  produces<LumiSummary, edm::InLumi>();
106  produces<LumiDetails, edm::InLumi>();
107  // set up cache
108  m_connectStr=iConfig.getParameter<std::string>("connect");
109  m_cachesize=iConfig.getUntrackedParameter<unsigned int>("ncacheEntries",5);
110 }
111 
113 }
114 
115 //
116 // member functions
117 //
118 void
120 {
121 }
122 
123 void
125  std::auto_ptr<LumiSummary> pOut1;
126  std::auto_ptr<LumiDetails> pOut2;
127  LumiSummary* pIn1=new LumiSummary;
128  LumiDetails* pIn2=new LumiDetails;
129  pOut1.reset(pIn1);
130  iLBlock.put(pOut1);
131  pOut2.reset(pIn2);
132  iLBlock.put(pOut2);
133 }
134 void
136 {
137  unsigned int currentrun=iLBlock.run();
138  unsigned int currentls=iLBlock.luminosityBlock();
139  //if is null run, fill empty values and return
140  if(m_isNullRun){
141  writeEmptyProductForEntry(iLBlock);
142  return;
143  }
144  if(m_cachedrun!=currentrun){
145  fillLSCache(currentrun,currentls);
146  }else{
147  if(m_lscache.find(currentls)==m_lscache.end()){
148  //if runnumber is cached but LS is not, this is the first LS, fill LS cache to full capacity
149  fillLSCache(currentrun,currentls);
150  }
151  }
152  if( m_lscache.empty() ){
153  writeEmptyProductForEntry(iLBlock);
154  return;
155  }
156  unsigned int lstowriteout=0;
157  if(m_lscache.find(currentls)==m_lscache.end()){//if the currentls is not in the cache
158  std::vector<unsigned int> v;
159  for(std::map<unsigned int,ExpressLumiProducer::PerLSData >::iterator it=m_lscache.begin();it!=m_lscache.end();++it){
160  v.push_back(it->first);
161  }
162  lstowriteout=v.back();//last available
163  }else{//if the current ls is cached
164  lstowriteout=currentls;
165  }
166  //here the presence of ls is guaranteed
167  //std::cout<<"writing "<<runnumber<<" "<<luminum<<std::endl;
168  if(lstowriteout==0){
169  writeEmptyProductForEntry(iLBlock);
170  }else{
171  writeProductsForEntry(iLBlock,lstowriteout);
172  }
173 }
174 
175 unsigned int
180  unsigned int result=0;
181  coral::AttributeList bindVariables;
182  bindVariables.extend("runnumber",typeid(unsigned int));
183  bindVariables["runnumber"].data<unsigned int>()=runnumber;
184  std::string conditionStr("RUNNUMBER=:runnumber");
185  coral::AttributeList MyOutput;
186  MyOutput.extend("maxavailablels",typeid(unsigned int));
187  coral::IQuery* myQuery=schema.newQuery();
188  myQuery->addToTableList(tablename);
189  myQuery->addToOutputList("max(LUMISECTION)","maxavailablels");
190  myQuery->setCondition(conditionStr,bindVariables);
191  myQuery->defineOutput(MyOutput);
192  coral::ICursor& mycursor=myQuery->execute();
193  while( mycursor.next() ){
194  const coral::AttributeList& row=mycursor.currentRow();
195  if(!row["maxavailablels"].isNull()){
196  result=row["maxavailablels"].data<unsigned int>();
197  }
198  }
199  return result;
200 }
201 void
202 ExpressLumiProducer::fillLSCache(unsigned int runnumber,unsigned int currentlsnum){
203  m_lscache.clear();
205  //
206  //queries once per cache refill
207  //
208  //select lumisection,instlumi,delivlumi,livelumi from cms_runtime_logger.lumi_sections where lumisection>=:lsmin and lumisection<:lsmax and runnumber=:runnumber;
209  //
211  if( !mydbservice.isAvailable() ){
212  throw cms::Exception("Non existing service lumi::service::DBService");
213  }
214  coral::ISessionProxy* session=mydbservice->connectReadOnly(m_connectStr);
215  coral::ITypeConverter& tconverter=session->typeConverter();
216  tconverter.setCppTypeForSqlType(std::string("float"),std::string("FLOAT(63)"));
217  tconverter.setCppTypeForSqlType(std::string("unsigned int"),std::string("NUMBER(10)"));
218  tconverter.setCppTypeForSqlType(std::string("unsigned short"),std::string("NUMBER(1)"));
219  unsigned int lsmin=1;
220  unsigned int lsmax=currentlsnum;
221  try{
222  session->transaction().start(true);
223  coral::ISchema& schema=session->nominalSchema();
224  unsigned int maxavailableLS=maxavailableLSforRun(schema,std::string("LUMI_SECTIONS"),m_cachedrun);
225  if(maxavailableLS!=0 && maxavailableLS<currentlsnum){
226  lsmax=maxavailableLS;
227  }else if(maxavailableLS==0){
228  //this run not existing (yet)
229  session->transaction().commit();
230  mydbservice->disconnect(session);
231  return;
232  }
233  if(m_cachesize!=0){
234  lsmin=(lsmax-m_cachesize)>0 ? (lsmax-m_cachesize+1) : 1;
235  }
236  for(unsigned int n=lsmin;n<=lsmax;++n){
237  PerLSData l;
238  std::vector<float> mytmp(3564,0.0);
239  l.bunchlumivalue.swap(mytmp);
240  std::vector<float> myerrtmp(3564,0.0);
241  l.bunchlumierror.swap(myerrtmp);
242  std::vector<short> myqtmp(3564,0);
243  l.bunchlumiquality.swap(myqtmp);
244  m_lscache.insert(std::make_pair(n,l));
245  }
246 
247  coral::AttributeList lumisummaryBindVariables;
248  lumisummaryBindVariables.extend("lsmin",typeid(unsigned int));
249  lumisummaryBindVariables.extend("runnumber",typeid(unsigned int));
250  lumisummaryBindVariables["runnumber"].data<unsigned int>()=m_cachedrun;
251  lumisummaryBindVariables["lsmin"].data<unsigned int>()=lsmin;
252  std::string conditionStr(" RUNNUMBER=:runnumber AND LUMISECTION>=:lsmin ");
253  coral::AttributeList lumisummaryOutput;
254  lumisummaryOutput.extend("LUMISECTION",typeid(unsigned int));
255  lumisummaryOutput.extend("INSTLUMI",typeid(float));
256  lumisummaryOutput.extend("DELIVLUMISECTION",typeid(float));
257  lumisummaryOutput.extend("LIVELUMISECTION",typeid(float));
258  lumisummaryOutput.extend("STARTORBIT",typeid(unsigned long long));
259  if(m_cachesize!=0){
260  lumisummaryBindVariables.extend("lsmax",typeid(unsigned int));
261  conditionStr=conditionStr+"AND LUMISECTION<=:lsmax";
262  lumisummaryBindVariables["lsmax"].data<unsigned int>()=lsmax;
263  }
264  coral::IQuery* lumisummaryQuery=schema.newQuery();
265  lumisummaryQuery->addToTableList(std::string("LUMI_SECTIONS"));
266  lumisummaryQuery->addToOutputList("LUMISECTION");
267  lumisummaryQuery->addToOutputList("INSTLUMI");
268  lumisummaryQuery->addToOutputList("DELIVLUMISECTION");
269  lumisummaryQuery->addToOutputList("LIVELUMISECTION");
270  lumisummaryQuery->addToOutputList("STARTORBIT");
271  lumisummaryQuery->setCondition(conditionStr,lumisummaryBindVariables);
272  lumisummaryQuery->defineOutput(lumisummaryOutput);
273  coral::ICursor& lumisummarycursor=lumisummaryQuery->execute();
274  unsigned int rowcounter=0;
275  while( lumisummarycursor.next() ){
276  const coral::AttributeList& row=lumisummarycursor.currentRow();
277  unsigned int lsnum=row["LUMISECTION"].data<unsigned int>();
278  float instlumi=0.0;
279  if(!row["INSTLUMI"].isNull()){
280  instlumi=row["INSTLUMI"].data<float>();//Hz/ub
281  }
282  float deadfrac=1.0;
283  float intgdellumi=0.0;
284  float intgreclumi=0.0;
285  unsigned long long startorbit=0;
286  if(!row["DELIVLUMISECTION"].isNull()){
287  intgdellumi=row["DELIVLUMISECTION"].data<float>()*1000.0;//convert to /ub
288  }
289  if(!row["LIVELUMISECTION"].isNull()){
290  intgreclumi=row["LIVELUMISECTION"].data<float>()*1000.0;//convert to /ub
291  }
292  if(intgdellumi>0){
293  deadfrac=1.0-intgreclumi/intgdellumi;
294  }
295  if(!row["STARTORBIT"].isNull()){
296  startorbit=row["STARTORBIT"].data<unsigned long long>();//convert to /ub
297  }
298  unsigned long long deadcount=deadfrac*10000.0;
299  unsigned long long bitzerocount=10000.0;
300  PerLSData& lsdata=m_lscache[lsnum];
301  lsdata.lsnum=lsnum;
302  lsdata.lumivalue=instlumi;
303  lsdata.deadcount=deadcount;
304  lsdata.bitzerocount=bitzerocount;
305  lsdata.startorbit=startorbit;
306  lsdata.numorbit=262144;
307  ++rowcounter;
308  }
309  if (rowcounter==0){
310  m_isNullRun=true;
311  }
312  delete lumisummaryQuery;
313  if(m_isNullRun) return;
314  //
315  //queries once per cache refill
316  //
317  //select lumisection,bunch,bunchlumi from cms_runtime_logger.bunch_lumi_sections where lumisection>=:lsmin and lumisection<:lsmax and runnumber=:runnumber;
318  //
319  coral::AttributeList lumidetailBindVariables;
320  lumidetailBindVariables.extend("lsmin",typeid(unsigned int));
321  lumidetailBindVariables.extend("runnumber",typeid(unsigned int));
322  lumidetailBindVariables["runnumber"].data<unsigned int>()=m_cachedrun;
323  lumidetailBindVariables["lsmin"].data<unsigned int>()=lsmin;
324  std::string detailconditionStr(" RUNNUMBER=:runnumber AND LUMISECTION>=:lsmin AND BUNCHLUMI>0 ");
325  coral::AttributeList lumidetailOutput;
326  lumidetailOutput.extend("LUMISECTION",typeid(unsigned int));
327  lumidetailOutput.extend("BUNCH",typeid(unsigned int));
328  lumidetailOutput.extend("BUNCHLUMI",typeid(float));
329  if(m_cachesize!=0){
330  lumidetailBindVariables.extend("lsmax",typeid(unsigned int));
331  detailconditionStr=detailconditionStr+"AND LUMISECTION<=:lsmax";
332  lumidetailBindVariables["lsmax"].data<unsigned int>()=lsmax;
333  }
334  coral::IQuery* lumidetailQuery=schema.newQuery();
335  lumidetailQuery->addToTableList(std::string("BUNCH_LUMI_SECTIONS"));
336  lumidetailQuery->addToOutputList("LUMISECTION");
337  lumidetailQuery->addToOutputList("BUNCH");
338  lumidetailQuery->addToOutputList("BUNCHLUMI");
339  lumidetailQuery->setCondition(detailconditionStr,lumidetailBindVariables);
340  lumidetailQuery->defineOutput(lumidetailOutput);
341  coral::ICursor& lumidetailcursor=lumidetailQuery->execute();
342  while( lumidetailcursor.next() ){
343  const coral::AttributeList& row=lumidetailcursor.currentRow();
344  unsigned int lsnum=row["LUMISECTION"].data<unsigned int>();
345  unsigned int bxidx=row["BUNCH"].data<unsigned int>();
346  float bxlumi=row["BUNCHLUMI"].data<float>();//Hz/ub
347  m_lscache[lsnum].bunchlumivalue[bxidx]=bxlumi;
348  }
349  delete lumidetailQuery;
350  session->transaction().commit();
351  }catch(const coral::Exception& er){
352  session->transaction().rollback();
353  mydbservice->disconnect(session);
354  throw cms::Exception("DatabaseError ")<<er.what();
355  }
356  mydbservice->disconnect(session);
357 }
358 void
360  //std::cout<<"writing runnumber,luminum "<<runnumber<<" "<<luminum<<std::endl;
361  std::auto_ptr<LumiSummary> pOut1;
362  std::auto_ptr<LumiDetails> pOut2;
363  LumiSummary* pIn1=new LumiSummary;
364  LumiDetails* pIn2=new LumiDetails;
365  if(m_isNullRun){
366  pIn1->setLumiVersion("DIP");
367  pIn2->setLumiVersion("DIP");
368  pOut1.reset(pIn1);
369  iLBlock.put(pOut1);
370  pOut2.reset(pIn2);
371  iLBlock.put(pOut2);
372  return;
373  }
374  PerLSData& lsdata=m_lscache[luminum];
375  pIn1->setLumiVersion("DIP");
376  pIn1->setLumiData(lsdata.lumivalue,0.0,0.0);
377  pIn1->setDeadCount(lsdata.deadcount);
378  pIn1->setBitZeroCount(lsdata.bitzerocount);
379  pIn1->setlsnumber(lsdata.lsnum);
380  pIn1->setOrbitData(lsdata.startorbit,lsdata.numorbit);
381 
382  pIn2->setLumiVersion("DIP");
384  pOut1.reset(pIn1);
385  iLBlock.put(pOut1);
386  pOut2.reset(pIn2);
387  iLBlock.put(pOut2);
388 }
virtual void produce(edm::Event &, const edm::EventSetup &) overridefinal
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual void beginLuminosityBlockProduce(edm::LuminosityBlock &iLBlock, edm::EventSetup const &iSetup) overridefinal
std::vector< short > bunchlumiquality
dictionary map
Definition: Association.py:205
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:142
list tablename
Definition: asciidump.py:411
void setOrbitData(unsigned int startorbit, unsigned int numorbit)
Definition: LumiSummary.cc:146
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:124
void setLumiData(float instlumi, float instlumierr, short lumiquality)
Definition: LumiSummary.cc:128
void setBitZeroCount(unsigned long long bitzerocount)
Definition: LumiSummary.cc:138
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:134
coral::ISessionProxy * connectReadOnly(const std::string &connectstring)
Definition: DBService.cc:42
ExpressLumiProducer(const edm::ParameterSet &)
tuple runnum
Definition: summaryLumi.py:210
bool fillLumi(edm::LuminosityBlock &iLBlock)
void put(std::auto_ptr< PROD > product)
Put a new product.