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  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();
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  coral::ISessionProxy* 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  mydbservice->disconnect(session);
224  return;
225  }
226  if(m_cachesize!=0){
227  lsmin=(lsmax-m_cachesize)>0 ? (lsmax-m_cachesize+1) : 1;
228  }
229  for(unsigned int n=lsmin;n<=lsmax;++n){
230  PerLSData l;
231  std::vector<float> mytmp(3564,0.0);
232  l.bunchlumivalue.swap(mytmp);
233  std::vector<float> myerrtmp(3564,0.0);
234  l.bunchlumierror.swap(myerrtmp);
235  std::vector<short> myqtmp(3564,0);
236  l.bunchlumiquality.swap(myqtmp);
237  m_lscache.insert(std::make_pair(n,l));
238  }
239 
240  coral::AttributeList lumisummaryBindVariables;
241  lumisummaryBindVariables.extend("lsmin",typeid(unsigned int));
242  lumisummaryBindVariables.extend("runnumber",typeid(unsigned int));
243  lumisummaryBindVariables["runnumber"].data<unsigned int>()=m_cachedrun;
244  lumisummaryBindVariables["lsmin"].data<unsigned int>()=lsmin;
245  std::string conditionStr(" RUNNUMBER=:runnumber AND LUMISECTION>=:lsmin ");
246  coral::AttributeList lumisummaryOutput;
247  lumisummaryOutput.extend("LUMISECTION",typeid(unsigned int));
248  lumisummaryOutput.extend("INSTLUMI",typeid(float));
249  lumisummaryOutput.extend("DELIVLUMISECTION",typeid(float));
250  lumisummaryOutput.extend("LIVELUMISECTION",typeid(float));
251  lumisummaryOutput.extend("STARTORBIT",typeid(unsigned long long));
252  if(m_cachesize!=0){
253  lumisummaryBindVariables.extend("lsmax",typeid(unsigned int));
254  conditionStr=conditionStr+"AND LUMISECTION<=:lsmax";
255  lumisummaryBindVariables["lsmax"].data<unsigned int>()=lsmax;
256  }
257  coral::IQuery* lumisummaryQuery=schema.newQuery();
258  lumisummaryQuery->addToTableList(std::string("LUMI_SECTIONS"));
259  lumisummaryQuery->addToOutputList("LUMISECTION");
260  lumisummaryQuery->addToOutputList("INSTLUMI");
261  lumisummaryQuery->addToOutputList("DELIVLUMISECTION");
262  lumisummaryQuery->addToOutputList("LIVELUMISECTION");
263  lumisummaryQuery->addToOutputList("STARTORBIT");
264  lumisummaryQuery->setCondition(conditionStr,lumisummaryBindVariables);
265  lumisummaryQuery->defineOutput(lumisummaryOutput);
266  coral::ICursor& lumisummarycursor=lumisummaryQuery->execute();
267  unsigned int rowcounter=0;
268  while( lumisummarycursor.next() ){
269  const coral::AttributeList& row=lumisummarycursor.currentRow();
270  unsigned int lsnum=row["LUMISECTION"].data<unsigned int>();
271  float instlumi=0.0;
272  if(!row["INSTLUMI"].isNull()){
273  instlumi=row["INSTLUMI"].data<float>();//Hz/ub
274  }
275  float deadfrac=1.0;
276  float intgdellumi=0.0;
277  float intgreclumi=0.0;
278  unsigned long long startorbit=0;
279  if(!row["DELIVLUMISECTION"].isNull()){
280  intgdellumi=row["DELIVLUMISECTION"].data<float>()*1000.0;//convert to /ub
281  }
282  if(!row["LIVELUMISECTION"].isNull()){
283  intgreclumi=row["LIVELUMISECTION"].data<float>()*1000.0;//convert to /ub
284  }
285  if(intgdellumi>0){
286  deadfrac=1.0-intgreclumi/intgdellumi;
287  }
288  if(!row["STARTORBIT"].isNull()){
289  startorbit=row["STARTORBIT"].data<unsigned long long>();//convert to /ub
290  }
291  unsigned long long deadcount=deadfrac*10000.0;
292  unsigned long long bitzerocount=10000.0;
293  PerLSData& lsdata=m_lscache[lsnum];
294  lsdata.lsnum=lsnum;
295  lsdata.lumivalue=instlumi;
296  lsdata.deadcount=deadcount;
297  lsdata.bitzerocount=bitzerocount;
298  lsdata.startorbit=startorbit;
299  lsdata.numorbit=262144;
300  ++rowcounter;
301  }
302  if (rowcounter==0){
303  m_isNullRun=true;
304  }
305  delete lumisummaryQuery;
306  if(m_isNullRun) return;
307  //
308  //queries once per cache refill
309  //
310  //select lumisection,bunch,bunchlumi from cms_runtime_logger.bunch_lumi_sections where lumisection>=:lsmin and lumisection<:lsmax and runnumber=:runnumber;
311  //
312  coral::AttributeList lumidetailBindVariables;
313  lumidetailBindVariables.extend("lsmin",typeid(unsigned int));
314  lumidetailBindVariables.extend("runnumber",typeid(unsigned int));
315  lumidetailBindVariables["runnumber"].data<unsigned int>()=m_cachedrun;
316  lumidetailBindVariables["lsmin"].data<unsigned int>()=lsmin;
317  std::string detailconditionStr(" RUNNUMBER=:runnumber AND LUMISECTION>=:lsmin AND BUNCHLUMI>0 ");
318  coral::AttributeList lumidetailOutput;
319  lumidetailOutput.extend("LUMISECTION",typeid(unsigned int));
320  lumidetailOutput.extend("BUNCH",typeid(unsigned int));
321  lumidetailOutput.extend("BUNCHLUMI",typeid(float));
322  if(m_cachesize!=0){
323  lumidetailBindVariables.extend("lsmax",typeid(unsigned int));
324  detailconditionStr=detailconditionStr+"AND LUMISECTION<=:lsmax";
325  lumidetailBindVariables["lsmax"].data<unsigned int>()=lsmax;
326  }
327  coral::IQuery* lumidetailQuery=schema.newQuery();
328  lumidetailQuery->addToTableList(std::string("BUNCH_LUMI_SECTIONS"));
329  lumidetailQuery->addToOutputList("LUMISECTION");
330  lumidetailQuery->addToOutputList("BUNCH");
331  lumidetailQuery->addToOutputList("BUNCHLUMI");
332  lumidetailQuery->setCondition(detailconditionStr,lumidetailBindVariables);
333  lumidetailQuery->defineOutput(lumidetailOutput);
334  coral::ICursor& lumidetailcursor=lumidetailQuery->execute();
335  while( lumidetailcursor.next() ){
336  const coral::AttributeList& row=lumidetailcursor.currentRow();
337  unsigned int lsnum=row["LUMISECTION"].data<unsigned int>();
338  unsigned int bxidx=row["BUNCH"].data<unsigned int>();
339  float bxlumi=row["BUNCHLUMI"].data<float>();//Hz/ub
340  m_lscache[lsnum].bunchlumivalue[bxidx]=bxlumi;
341  }
342  delete lumidetailQuery;
343  session->transaction().commit();
344  }catch(const coral::Exception& er){
345  session->transaction().rollback();
346  mydbservice->disconnect(session);
347  throw cms::Exception("DatabaseError ")<<er.what();
348  }
349  mydbservice->disconnect(session);
350 }
351 void
353  //std::cout<<"writing runnumber,luminum "<<runnumber<<" "<<luminum<<std::endl;
354  auto pIn1 = std::make_unique<LumiSummary>();
355  auto pIn2 = std::make_unique<LumiDetails>();
356  if(m_isNullRun){
357  pIn1->setLumiVersion("DIP");
358  pIn2->setLumiVersion("DIP");
359  iLBlock.put(std::move(pIn1));
360  iLBlock.put(std::move(pIn2));
361  return;
362  }
363  PerLSData& lsdata=m_lscache[luminum];
364  pIn1->setLumiVersion("DIP");
365  pIn1->setLumiData(lsdata.lumivalue,0.0,0.0);
366  pIn1->setDeadCount(lsdata.deadcount);
367  pIn1->setBitZeroCount(lsdata.bitzerocount);
368  pIn1->setlsnumber(lsdata.lsnum);
369  pIn1->setOrbitData(lsdata.startorbit,lsdata.numorbit);
370 
371  pIn2->setLumiVersion("DIP");
372  pIn2->fill(LumiDetails::kOCC1,lsdata.bunchlumivalue,lsdata.bunchlumierror,lsdata.bunchlumiquality);
373  iLBlock.put(std::move(pIn1));
374  iLBlock.put(std::move(pIn2));
375 }
virtual void produce(edm::Event &, const edm::EventSetup &) overridefinal
tuple session
Definition: dataDML.py:2333
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
std::map< unsigned int, PerLSData > m_lscache
void put(std::unique_ptr< PROD > product)
Put a new product.
void fillLSCache(unsigned int runnum, unsigned int luminum)
def move
Definition: eostools.py:510
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)
unsigned int maxavailableLSforRun(coral::ISchema &schema, const std::string &tablename, unsigned int runnumber)
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
bool fillLumi(edm::LuminosityBlock &iLBlock)