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