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