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