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