CMS 3D CMS Logo

DIPLumiProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: LumiProducer
4 // Class: DIPLumiProducer
5 //
10 //#include <memory>
15 
22 
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 "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"
44 #include "DIPLumiProducer.h"
45 #include <iostream>
46 #include <sstream>
47 #include <string>
48 #include <memory>
49 #include <algorithm>
50 #include <vector>
51 #include <cstring>
52 #include <iterator>
53 #include <boost/tokenizer.hpp>
54 #include <xercesc/dom/DOM.hpp>
55 #include <xercesc/parsers/XercesDOMParser.hpp>
57 #include <xercesc/util/XMLString.hpp>
58 
59 #include "boost/filesystem/path.hpp"
60 #include "boost/filesystem/operations.hpp"
61 
62 DIPLumiProducer::DIPLumiProducer(const edm::ParameterSet& iConfig):m_connectStr(""),m_summarycachedrun(0),m_detailcachedrun(0),m_cachesize(0){
65  findingRecord<DIPLuminosityRcd>();
66  m_connectStr=iConfig.getParameter<std::string>("connect");
67  m_cachesize=iConfig.getUntrackedParameter<unsigned int>("ncacheEntries",3);
68 }
69 
72 {
73  unsigned int currentrun=m_pcurrentTime->eventID().run();
74  unsigned int currentls=m_pcurrentTime->luminosityBlockNumber();
75  if(currentls==0||currentls==4294967295){
76  return std::make_shared<const DIPLumiSummary>();
77  }
78  if(m_summarycachedrun!=currentrun){//i'm in a new run
79  fillsummarycache(currentrun,currentls);//starting ls
80  }else{ //i'm in an old run
81  if(m_summarycache.find(currentls)==m_summarycache.end()){//i'm not cached
82  fillsummarycache(currentrun,currentls);//
83  }
84  }
85  if(m_summarycache.empty()){
86  return std::make_shared<const DIPLumiSummary>();
87  }
88  if(m_summarycache.find(currentls)==m_summarycache.end()){
89  std::vector<unsigned int> v;
90  for(std::map<unsigned int,std::shared_ptr<const DIPLumiSummary> >::iterator it=m_summarycache.begin();it!=m_summarycache.end();++it){
91  v.push_back(it->first);
92  }
94  }else{
96  }
97  if(m_summaryresult.get()==nullptr){
98  return std::make_shared<const DIPLumiSummary>();
99  }
100  return m_summaryresult;
101 }
104 {
105  unsigned int currentrun=m_pcurrentTime->eventID().run();
106  unsigned int currentls=m_pcurrentTime->luminosityBlockNumber();
107  if(currentls==0||currentls==4294967295){
108  return std::make_shared<const DIPLumiDetail>();
109  }
110  if(m_detailcachedrun!=currentrun){//i'm in a new run
111  filldetailcache(currentrun,currentls);//starting ls
112  }else{ //i'm in an known run
113  if(m_detailcache.find(currentls)==m_detailcache.end()){//i'm in a unknown ls
114  filldetailcache(currentrun,currentls);//cache all ls>=currentls
115  }
116  }
117  if(m_detailcache.empty()){
118  return std::make_shared<const DIPLumiDetail>();
119  }
120  if(m_detailcache.find(currentls)==m_detailcache.end()){
121  std::vector<unsigned int> v;
122  for(std::map<unsigned int,std::shared_ptr<const DIPLumiDetail> >::iterator it=m_detailcache.begin();it!=m_detailcache.end();++it){
123  v.push_back(it->first);
124  }
125  m_detailresult=m_detailcache[v.back()];
126  }else{
127  m_detailresult=m_detailcache[currentls];
128  }
129  if(m_detailresult.get()==nullptr){
130  return std::make_shared<const DIPLumiDetail>();
131  }
132  return m_detailresult;
133 }
134 
135 void
137  const edm::IOVSyncValue& iTime,
138  edm::ValidityInterval& oValidity ) {
139  m_pcurrentTime=&iTime;
140  oValidity.setFirst(iTime);
141  oValidity.setLast(iTime);
142 }
143 
144 void
145 DIPLumiProducer::fillsummarycache(unsigned int runnumber,unsigned int currentlsnum){
146  m_summarycache.clear();
148  //
149  // queries once per cache refill
150  //
151  // select max(lumi_sections) as maxavailable from cms_runtime_logger.lumi_sections where runnumber=:runnumber;
152  //
153  // if maxavailable<currentls: get lsmax=maxavailable ; else: get lsmax=currentls
154  // lsmin=lsmax-cachesize
155  // select runnumber,lumisection,instlumi,delivlumi,livelumi from cms_runtime_logger.lumi_sections where lumisection>=:lsmin and lumisection<=:lsmax and runnumber=:runnumber;
156  //
158  if( !mydbservice.isAvailable() ){
159  throw cms::Exception("Non existing service lumi::service::DBService");
160  }
161  auto session=mydbservice->connectReadOnly(m_connectStr);
162  coral::ITypeConverter& tconverter=session->typeConverter();
163  tconverter.setCppTypeForSqlType(std::string("float"),std::string("FLOAT(63)"));
164  tconverter.setCppTypeForSqlType(std::string("unsigned int"),std::string("NUMBER(10)"));
165  tconverter.setCppTypeForSqlType(std::string("unsigned short"),std::string("NUMBER(1)"));
166  unsigned int lsmin=1;
167  unsigned int lsmax=currentlsnum;
168  try{
169  session->transaction().start(true);
170  coral::ISchema& schema=session->nominalSchema();
171  unsigned int maxavailableLS=maxavailableLSforRun(schema,std::string("LUMI_SECTIONS"),m_summarycachedrun);
172  if(maxavailableLS!=0 && maxavailableLS<currentlsnum){
173  lsmax=maxavailableLS;
174  }else if(maxavailableLS==0){
175  //this run not existing (yet)
176  session->transaction().commit();
177  return;
178  }
179  if(m_cachesize!=0){
180  lsmin=(lsmax-m_cachesize)>0 ? (lsmax-m_cachesize+1) : 1;
181  }
182  //std::cout<<"lsmin "<<lsmin<<" lsmax "<<lsmax<<std::endl;
183  coral::AttributeList lumisummaryBindVariables;
184  lumisummaryBindVariables.extend("lsmin",typeid(unsigned int));
185  lumisummaryBindVariables.extend("lsmax",typeid(unsigned int));
186  lumisummaryBindVariables.extend("runnumber",typeid(unsigned int));
187  lumisummaryBindVariables["runnumber"].data<unsigned int>()=m_summarycachedrun;
188  lumisummaryBindVariables["lsmin"].data<unsigned int>()=lsmin;
189  lumisummaryBindVariables["lsmax"].data<unsigned int>()=lsmax;
190  std::string conditionStr("RUNNUMBER=:runnumber AND LUMISECTION>=:lsmin AND LUMISECTION<=:lsmax");
191  coral::AttributeList lumisummaryOutput;
192  lumisummaryOutput.extend("LUMISECTION",typeid(unsigned int));
193  lumisummaryOutput.extend("INSTLUMI",typeid(float));
194  lumisummaryOutput.extend("DELIVLUMISECTION",typeid(float));
195  lumisummaryOutput.extend("LIVELUMISECTION",typeid(float));
196  lumisummaryOutput.extend("CMS_ACTIVE",typeid(unsigned short));
197  coral::IQuery* lumisummaryQuery=schema.newQuery();
198  lumisummaryQuery->addToTableList(std::string("LUMI_SECTIONS"));
199  lumisummaryQuery->addToOutputList("LUMISECTION");
200  lumisummaryQuery->addToOutputList("INSTLUMI");
201  lumisummaryQuery->addToOutputList("DELIVLUMISECTION");
202  lumisummaryQuery->addToOutputList("LIVELUMISECTION");
203  lumisummaryQuery->addToOutputList("CMS_ACTIVE");
204  lumisummaryQuery->setCondition(conditionStr,lumisummaryBindVariables);
205  lumisummaryQuery->defineOutput(lumisummaryOutput);
206  coral::ICursor& lumisummarycursor=lumisummaryQuery->execute();
207  while( lumisummarycursor.next() ){
208  const coral::AttributeList& row=lumisummarycursor.currentRow();
209  unsigned int lsnum=row["LUMISECTION"].data<unsigned int>();
210  float instlumi=0.0;
211  if(!row["INSTLUMI"].isNull()){
212  instlumi=row["INSTLUMI"].data<float>();//Hz/ub
213  }
214  float intgdellumi=0.0;
215  if(!row["DELIVLUMISECTION"].isNull()){
216  intgdellumi=row["DELIVLUMISECTION"].data<float>()*1000.0;//convert to /ub
217  }
218  float intgreclumi=0.0;
219  if(!row["LIVELUMISECTION"].isNull()){
220  intgreclumi=row["LIVELUMISECTION"].data<float>()*1000.0;//convert to /ub
221  }
222  unsigned short cmsalive=0;
223  if(!row["CMS_ACTIVE"].isNull()){
224  cmsalive=row["CMS_ACTIVE"].data<unsigned short>();
225  }
226  auto tmpls = std::make_unique<DIPLumiSummary>(instlumi,intgdellumi,intgreclumi,cmsalive);
227  tmpls->setOrigin(m_summarycachedrun,lsnum);
228  //std::cout<<"filling "<<lsnum<<std::endl;
229  std::shared_ptr<const DIPLumiSummary> const_tmpls = std::move(tmpls);
230  m_summarycache.insert(std::make_pair(lsnum,const_tmpls));
231  }
232  delete lumisummaryQuery;
233  session->transaction().commit();
234  }catch(const coral::Exception& er){
235  session->transaction().rollback();
236  throw cms::Exception("DatabaseError ")<<er.what();
237  }
238 }
239 unsigned int
240 DIPLumiProducer::maxavailableLSforRun(coral::ISchema& schema,const std::string&tablename,unsigned int runnumber){
244  unsigned int result=0;
245  coral::AttributeList bindVariables;
246  bindVariables.extend("runnumber",typeid(unsigned int));
247  bindVariables["runnumber"].data<unsigned int>()=runnumber;
248  std::string conditionStr("RUNNUMBER=:runnumber");
249  coral::AttributeList MyOutput;
250  MyOutput.extend("maxavailablels",typeid(unsigned int));
251  coral::IQuery* myQuery=schema.newQuery();
252  myQuery->addToTableList(tablename);
253  myQuery->addToOutputList("max(LUMISECTION)","maxavailablels");
254  myQuery->setCondition(conditionStr,bindVariables);
255  myQuery->defineOutput(MyOutput);
256  coral::ICursor& mycursor=myQuery->execute();
257  while( mycursor.next() ){
258  const coral::AttributeList& row=mycursor.currentRow();
259  if(!row["maxavailablels"].isNull()){
260  result=row["maxavailablels"].data<unsigned int>();
261  }
262  }
263  return result;
264 }
265 void
266 DIPLumiProducer::filldetailcache(unsigned int runnumber,unsigned int currentlsnum){
267  m_detailcache.clear();
269 
270  std::map< unsigned int,std::unique_ptr<DIPLumiDetail> > detailcache;
271  //
272  //queries once per cache refill
273  //
274  //select lumisection,bunch,bunchlumi from cms_runtime_logger.bunch_lumi_sections where lumisection>=:lsmin and lumisection<:lsmax and runnumber=:runnumber ;
275  //
277  if( !mydbservice.isAvailable() ){
278  throw cms::Exception("Non existing service lumi::service::DBService");
279  }
280  auto session=mydbservice->connectReadOnly(m_connectStr);
281  coral::ITypeConverter& tconverter=session->typeConverter();
282  tconverter.setCppTypeForSqlType(std::string("float"),std::string("FLOAT(63)"));
283  tconverter.setCppTypeForSqlType(std::string("unsigned int"),std::string("NUMBER(10)"));
284  unsigned int lsmin=1;
285  unsigned int lsmax=currentlsnum;
286  try{
287  session->transaction().start(true);
288  coral::ISchema& schema=session->nominalSchema();
289  unsigned int maxavailableLS=maxavailableLSforRun(schema,std::string("BUNCH_LUMI_SECTIONS"),m_summarycachedrun);
290  if(maxavailableLS!=0 &&maxavailableLS<currentlsnum ){
291  lsmax=maxavailableLS;
292  }else if(maxavailableLS==0){
293  //this run not existing (yet)
294  session->transaction().commit();
295  return;
296  }
297  if(m_cachesize!=0){
298  lsmin=(lsmax-m_cachesize)>0 ? (lsmax-m_cachesize+1) : 1;
299  }
300  coral::AttributeList lumidetailBindVariables;
301  lumidetailBindVariables.extend("lsmin",typeid(unsigned int));
302  lumidetailBindVariables.extend("lsmax",typeid(unsigned int));
303  lumidetailBindVariables.extend("runnumber",typeid(unsigned int));
304  lumidetailBindVariables["runnumber"].data<unsigned int>()=m_detailcachedrun;
305  lumidetailBindVariables["lsmin"].data<unsigned int>()=lsmin;
306  lumidetailBindVariables["lsmax"].data<unsigned int>()=lsmax;
307  std::string conditionStr("RUNNUMBER=:runnumber AND LUMISECTION>=:lsmin AND LUMISECTION<=:lsmax AND BUNCHLUMI>0 ");
308  coral::AttributeList lumidetailOutput;
309  lumidetailOutput.extend("LUMISECTION",typeid(unsigned int));
310  lumidetailOutput.extend("BUNCH",typeid(unsigned int));
311  lumidetailOutput.extend("BUNCHLUMI",typeid(float));
312  coral::IQuery* lumidetailQuery=schema.newQuery();
313  lumidetailQuery->addToTableList(std::string("BUNCH_LUMI_SECTIONS"));
314  lumidetailQuery->addToOutputList("LUMISECTION");
315  lumidetailQuery->addToOutputList("BUNCH");
316  lumidetailQuery->addToOutputList("BUNCHLUMI");
317  lumidetailQuery->setCondition(conditionStr,lumidetailBindVariables);
318  lumidetailQuery->defineOutput(lumidetailOutput);
319  coral::ICursor& lumidetailcursor=lumidetailQuery->execute();
320  while( lumidetailcursor.next() ){
321  const coral::AttributeList& row=lumidetailcursor.currentRow();
322  unsigned int lsnum=row["LUMISECTION"].data<unsigned int>();
323  if(detailcache.find(lsnum)==detailcache.end()){
324  detailcache.insert(std::make_pair(lsnum,std::make_unique<DIPLumiDetail>()));
325  detailcache[lsnum]->setOrigin(m_detailcachedrun,lsnum);
326  }
327  if(!row["BUNCH"].isNull()){
328  unsigned int bxidx=row["BUNCH"].data<unsigned int>();
329  float bxlumi=0.0;
330  if(!row["BUNCHLUMI"].isNull()){
331  bxlumi=row["BUNCHLUMI"].data<float>();//Hz/ub
332  }
333  detailcache[lsnum]->fillbxdata(bxidx,bxlumi);
334  }
335  }
336  for(auto & item : detailcache) {
337  m_detailcache[item.first] = std::move(item.second);
338  }
339  delete lumidetailQuery;
340  session->transaction().commit();
341  }catch(const coral::Exception& er){
342  session->transaction().rollback();
343  throw cms::Exception("DatabaseError ")<<er.what();
344  }
345 }
347 //define this as a plug-in
RunNumber_t run() const
Definition: EventID.h:39
void setFirst(const IOVSyncValue &iTime)
T getParameter(std::string const &) const
auto setWhatProduced(T *iThis, const es::Label &iLabel={})
Definition: ESProducer.h:124
T getUntrackedParameter(std::string const &, T const &) const
unsigned int m_detailcachedrun
const EventID & eventID() const
Definition: IOVSyncValue.h:40
std::shared_ptr< const DIPLumiSummary > m_summaryresult
DIPLumiProducer(const edm::ParameterSet &)
ISessionProxyPtr connectReadOnly(const std::string &connectstring)
Definition: DBService.cc:22
std::string m_connectStr
std::map< unsigned int, std::shared_ptr< const DIPLumiSummary > > m_summarycache
~DIPLumiProducer() override
ReturnDetailType produceDetail(const DIPLuminosityRcd &)
std::shared_ptr< const DIPLumiDetail > ReturnDetailType
void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &, const edm::IOVSyncValue &, edm::ValidityInterval &) override
bool isAvailable() const
Definition: Service.h:40
LuminosityBlockNumber_t luminosityBlockNumber() const
Definition: IOVSyncValue.h:41
void setLast(const IOVSyncValue &iTime)
unsigned int m_summarycachedrun
std::map< unsigned int, std::shared_ptr< const DIPLumiDetail > > m_detailcache
unsigned int m_cachesize
unsigned int maxavailableLSforRun(coral::ISchema &schema, const std::string &tablename, unsigned int runnumber)
void filldetailcache(unsigned int runnumber, unsigned int startlsnum)
const edm::IOVSyncValue * m_pcurrentTime
std::shared_ptr< const DIPLumiDetail > m_detailresult
std::shared_ptr< const DIPLumiSummary > ReturnSummaryType
#define DEFINE_FWK_EVENTSETUP_SOURCE(type)
Definition: SourceFactory.h:91
def move(src, dest)
Definition: eostools.py:511
void fillsummarycache(unsigned int runnumber, unsigned int startlsnum)
ReturnSummaryType produceSummary(const DIPLuminosityRcd &)