CMS 3D CMS Logo

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