CMS 3D CMS Logo

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 
78  ~ExpressLumiProducer() override;
79 
80 private:
81 
82 
83  void produce(edm::Event&, const edm::EventSetup&) final;
84 
85  void beginLuminosityBlockProduce(edm::LuminosityBlock & iLBlock,
86  edm::EventSetup const& iSetup) 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::Transition::BeginLuminosityBlock>();
105  produces<LumiDetails, edm::Transition::BeginLuminosityBlock>();
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  iLBlock.put(std::make_unique<LumiSummary>());
125  iLBlock.put(std::make_unique<LumiDetails>());
126 }
127 void
129 {
130  unsigned int currentrun=iLBlock.run();
131  unsigned int currentls=iLBlock.luminosityBlock();
132  //if is null run, fill empty values and return
133  if(m_isNullRun){
134  writeEmptyProductForEntry(iLBlock);
135  return;
136  }
137  if(m_cachedrun!=currentrun){
138  fillLSCache(currentrun,currentls);
139  }else{
140  if(m_lscache.find(currentls)==m_lscache.end()){
141  //if runnumber is cached but LS is not, this is the first LS, fill LS cache to full capacity
142  fillLSCache(currentrun,currentls);
143  }
144  }
145  if( m_lscache.empty() ){
146  writeEmptyProductForEntry(iLBlock);
147  return;
148  }
149  unsigned int lstowriteout=0;
150  if(m_lscache.find(currentls)==m_lscache.end()){//if the currentls is not in the cache
151  std::vector<unsigned int> v;
152  for(std::map<unsigned int,ExpressLumiProducer::PerLSData >::iterator it=m_lscache.begin();it!=m_lscache.end();++it){
153  v.push_back(it->first);
154  }
155  lstowriteout=v.back();//last available
156  }else{//if the current ls is cached
157  lstowriteout=currentls;
158  }
159  //here the presence of ls is guaranteed
160  //std::cout<<"writing "<<runnumber<<" "<<luminum<<std::endl;
161  if(lstowriteout==0){
162  writeEmptyProductForEntry(iLBlock);
163  }else{
164  writeProductsForEntry(iLBlock,lstowriteout);
165  }
166 }
167 
168 unsigned int
169 ExpressLumiProducer::maxavailableLSforRun(coral::ISchema& schema,const std::string&tablename,unsigned int runnumber){
173  unsigned int result=0;
174  coral::AttributeList bindVariables;
175  bindVariables.extend("runnumber",typeid(unsigned int));
176  bindVariables["runnumber"].data<unsigned int>()=runnumber;
177  std::string conditionStr("RUNNUMBER=:runnumber");
178  coral::AttributeList MyOutput;
179  MyOutput.extend("maxavailablels",typeid(unsigned int));
180  coral::IQuery* myQuery=schema.newQuery();
181  myQuery->addToTableList(tablename);
182  myQuery->addToOutputList("max(LUMISECTION)","maxavailablels");
183  myQuery->setCondition(conditionStr,bindVariables);
184  myQuery->defineOutput(MyOutput);
185  coral::ICursor& mycursor=myQuery->execute();
186  while( mycursor.next() ){
187  const coral::AttributeList& row=mycursor.currentRow();
188  if(!row["maxavailablels"].isNull()){
189  result=row["maxavailablels"].data<unsigned int>();
190  }
191  }
192  return result;
193 }
194 void
195 ExpressLumiProducer::fillLSCache(unsigned int runnumber,unsigned int currentlsnum){
196  m_lscache.clear();
197  m_cachedrun=runnumber;
198  //
199  //queries once per cache refill
200  //
201  //select lumisection,instlumi,delivlumi,livelumi from cms_runtime_logger.lumi_sections where lumisection>=:lsmin and lumisection<:lsmax and runnumber=:runnumber;
202  //
204  if( !mydbservice.isAvailable() ){
205  throw cms::Exception("Non existing service lumi::service::DBService");
206  }
207  auto session=mydbservice->connectReadOnly(m_connectStr);
208  coral::ITypeConverter& tconverter=session->typeConverter();
209  tconverter.setCppTypeForSqlType(std::string("float"),std::string("FLOAT(63)"));
210  tconverter.setCppTypeForSqlType(std::string("unsigned int"),std::string("NUMBER(10)"));
211  tconverter.setCppTypeForSqlType(std::string("unsigned short"),std::string("NUMBER(1)"));
212  unsigned int lsmin=1;
213  unsigned int lsmax=currentlsnum;
214  try{
215  session->transaction().start(true);
216  coral::ISchema& schema=session->nominalSchema();
217  unsigned int maxavailableLS=maxavailableLSforRun(schema,std::string("LUMI_SECTIONS"),m_cachedrun);
218  if(maxavailableLS!=0 && maxavailableLS<currentlsnum){
219  lsmax=maxavailableLS;
220  }else if(maxavailableLS==0){
221  //this run not existing (yet)
222  session->transaction().commit();
223  return;
224  }
225  if(m_cachesize!=0){
226  lsmin=(lsmax-m_cachesize)>0 ? (lsmax-m_cachesize+1) : 1;
227  }
228  for(unsigned int n=lsmin;n<=lsmax;++n){
229  PerLSData l;
230  std::vector<float> mytmp(3564,0.0);
231  l.bunchlumivalue.swap(mytmp);
232  std::vector<float> myerrtmp(3564,0.0);
233  l.bunchlumierror.swap(myerrtmp);
234  std::vector<short> myqtmp(3564,0);
235  l.bunchlumiquality.swap(myqtmp);
236  m_lscache.insert(std::make_pair(n,l));
237  }
238 
239  coral::AttributeList lumisummaryBindVariables;
240  lumisummaryBindVariables.extend("lsmin",typeid(unsigned int));
241  lumisummaryBindVariables.extend("runnumber",typeid(unsigned int));
242  lumisummaryBindVariables["runnumber"].data<unsigned int>()=m_cachedrun;
243  lumisummaryBindVariables["lsmin"].data<unsigned int>()=lsmin;
244  std::string conditionStr(" RUNNUMBER=:runnumber AND LUMISECTION>=:lsmin ");
245  coral::AttributeList lumisummaryOutput;
246  lumisummaryOutput.extend("LUMISECTION",typeid(unsigned int));
247  lumisummaryOutput.extend("INSTLUMI",typeid(float));
248  lumisummaryOutput.extend("DELIVLUMISECTION",typeid(float));
249  lumisummaryOutput.extend("LIVELUMISECTION",typeid(float));
250  lumisummaryOutput.extend("STARTORBIT",typeid(unsigned long long));
251  if(m_cachesize!=0){
252  lumisummaryBindVariables.extend("lsmax",typeid(unsigned int));
253  conditionStr=conditionStr+"AND LUMISECTION<=:lsmax";
254  lumisummaryBindVariables["lsmax"].data<unsigned int>()=lsmax;
255  }
256  coral::IQuery* lumisummaryQuery=schema.newQuery();
257  lumisummaryQuery->addToTableList(std::string("LUMI_SECTIONS"));
258  lumisummaryQuery->addToOutputList("LUMISECTION");
259  lumisummaryQuery->addToOutputList("INSTLUMI");
260  lumisummaryQuery->addToOutputList("DELIVLUMISECTION");
261  lumisummaryQuery->addToOutputList("LIVELUMISECTION");
262  lumisummaryQuery->addToOutputList("STARTORBIT");
263  lumisummaryQuery->setCondition(conditionStr,lumisummaryBindVariables);
264  lumisummaryQuery->defineOutput(lumisummaryOutput);
265  coral::ICursor& lumisummarycursor=lumisummaryQuery->execute();
266  unsigned int rowcounter=0;
267  while( lumisummarycursor.next() ){
268  const coral::AttributeList& row=lumisummarycursor.currentRow();
269  unsigned int lsnum=row["LUMISECTION"].data<unsigned int>();
270  float instlumi=0.0;
271  if(!row["INSTLUMI"].isNull()){
272  instlumi=row["INSTLUMI"].data<float>();//Hz/ub
273  }
274  float deadfrac=1.0;
275  float intgdellumi=0.0;
276  float intgreclumi=0.0;
277  unsigned long long startorbit=0;
278  if(!row["DELIVLUMISECTION"].isNull()){
279  intgdellumi=row["DELIVLUMISECTION"].data<float>()*1000.0;//convert to /ub
280  }
281  if(!row["LIVELUMISECTION"].isNull()){
282  intgreclumi=row["LIVELUMISECTION"].data<float>()*1000.0;//convert to /ub
283  }
284  if(intgdellumi>0){
285  deadfrac=1.0-intgreclumi/intgdellumi;
286  }
287  if(!row["STARTORBIT"].isNull()){
288  startorbit=row["STARTORBIT"].data<unsigned long long>();//convert to /ub
289  }
290  unsigned long long deadcount=deadfrac*10000.0;
291  unsigned long long bitzerocount=10000.0;
292  PerLSData& lsdata=m_lscache[lsnum];
293  lsdata.lsnum=lsnum;
294  lsdata.lumivalue=instlumi;
295  lsdata.deadcount=deadcount;
296  lsdata.bitzerocount=bitzerocount;
297  lsdata.startorbit=startorbit;
298  lsdata.numorbit=262144;
299  ++rowcounter;
300  }
301  if (rowcounter==0){
302  m_isNullRun=true;
303  }
304  delete lumisummaryQuery;
305  if(m_isNullRun) return;
306  //
307  //queries once per cache refill
308  //
309  //select lumisection,bunch,bunchlumi from cms_runtime_logger.bunch_lumi_sections where lumisection>=:lsmin and lumisection<:lsmax and runnumber=:runnumber;
310  //
311  coral::AttributeList lumidetailBindVariables;
312  lumidetailBindVariables.extend("lsmin",typeid(unsigned int));
313  lumidetailBindVariables.extend("runnumber",typeid(unsigned int));
314  lumidetailBindVariables["runnumber"].data<unsigned int>()=m_cachedrun;
315  lumidetailBindVariables["lsmin"].data<unsigned int>()=lsmin;
316  std::string detailconditionStr(" RUNNUMBER=:runnumber AND LUMISECTION>=:lsmin AND BUNCHLUMI>0 ");
317  coral::AttributeList lumidetailOutput;
318  lumidetailOutput.extend("LUMISECTION",typeid(unsigned int));
319  lumidetailOutput.extend("BUNCH",typeid(unsigned int));
320  lumidetailOutput.extend("BUNCHLUMI",typeid(float));
321  if(m_cachesize!=0){
322  lumidetailBindVariables.extend("lsmax",typeid(unsigned int));
323  detailconditionStr=detailconditionStr+"AND LUMISECTION<=:lsmax";
324  lumidetailBindVariables["lsmax"].data<unsigned int>()=lsmax;
325  }
326  coral::IQuery* lumidetailQuery=schema.newQuery();
327  lumidetailQuery->addToTableList(std::string("BUNCH_LUMI_SECTIONS"));
328  lumidetailQuery->addToOutputList("LUMISECTION");
329  lumidetailQuery->addToOutputList("BUNCH");
330  lumidetailQuery->addToOutputList("BUNCHLUMI");
331  lumidetailQuery->setCondition(detailconditionStr,lumidetailBindVariables);
332  lumidetailQuery->defineOutput(lumidetailOutput);
333  coral::ICursor& lumidetailcursor=lumidetailQuery->execute();
334  while( lumidetailcursor.next() ){
335  const coral::AttributeList& row=lumidetailcursor.currentRow();
336  unsigned int lsnum=row["LUMISECTION"].data<unsigned int>();
337  unsigned int bxidx=row["BUNCH"].data<unsigned int>();
338  float bxlumi=row["BUNCHLUMI"].data<float>();//Hz/ub
339  m_lscache[lsnum].bunchlumivalue[bxidx]=bxlumi;
340  }
341  delete lumidetailQuery;
342  session->transaction().commit();
343  }catch(const coral::Exception& er){
344  session->transaction().rollback();
345  throw cms::Exception("DatabaseError ")<<er.what();
346  }
347 }
348 void
350  //std::cout<<"writing runnumber,luminum "<<runnumber<<" "<<luminum<<std::endl;
351  auto pIn1 = std::make_unique<LumiSummary>();
352  auto pIn2 = std::make_unique<LumiDetails>();
353  if(m_isNullRun){
354  pIn1->setLumiVersion("DIP");
355  pIn2->setLumiVersion("DIP");
356  iLBlock.put(std::move(pIn1));
357  iLBlock.put(std::move(pIn2));
358  return;
359  }
360  PerLSData& lsdata=m_lscache[luminum];
361  pIn1->setLumiVersion("DIP");
362  pIn1->setLumiData(lsdata.lumivalue,0.0,0.0);
363  pIn1->setDeadCount(lsdata.deadcount);
364  pIn1->setBitZeroCount(lsdata.bitzerocount);
365  pIn1->setlsnumber(lsdata.lsnum);
366  pIn1->setOrbitData(lsdata.startorbit,lsdata.numorbit);
367 
368  pIn2->setLumiVersion("DIP");
369  pIn2->fill(LumiDetails::kOCC1,lsdata.bunchlumivalue,lsdata.bunchlumierror,lsdata.bunchlumiquality);
370  iLBlock.put(std::move(pIn1));
371  iLBlock.put(std::move(pIn2));
372 }
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
ISessionProxyPtr connectReadOnly(const std::string &connectstring)
Definition: DBService.cc:22
std::vector< short > bunchlumiquality
LuminosityBlockNumber_t luminosityBlock() const
void put(std::unique_ptr< PROD > product)
Put a new product.
void fillLSCache(unsigned int runnum, unsigned int luminum)
bool isAvailable() const
Definition: Service.h:46
RunNumber_t run() const
void produce(edm::Event &, const edm::EventSetup &) final
Definition: Binary.h:9
void writeProductsForEntry(edm::LuminosityBlock &iLBlock, unsigned int luminum)
void writeEmptyProductForEntry(edm::LuminosityBlock &iLBlock)
void beginLuminosityBlockProduce(edm::LuminosityBlock &iLBlock, edm::EventSetup const &iSetup) final
unsigned int maxavailableLSforRun(coral::ISchema &schema, const std::string &tablename, unsigned int runnumber)
HLT enums.
def move(src, dest)
Definition: eostools.py:511