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