CMS 3D CMS Logo

LHCInfoPopConSourceHandler.cc
Go to the documentation of this file.
7 #include "CoralBase/Attribute.h"
8 #include "CoralBase/AttributeList.h"
9 #include "CoralBase/AttributeSpecification.h"
10 #include "CoralBase/TimeStamp.h"
14 #include "RelationalAccess/ICursor.h"
15 #include "RelationalAccess/IQuery.h"
16 #include "RelationalAccess/ISchema.h"
17 #include "RelationalAccess/ISessionProxy.h"
18 #include <memory>
19 #include <sstream>
20 #include <utility>
21 #include <vector>
22 #include <cmath>
23 
24 namespace cond {
25  static const std::pair<const char*, LHCInfo::FillType> s_fillTypeMap[] = {std::make_pair("PROTONS", LHCInfo::PROTONS),
26  std::make_pair("IONS", LHCInfo::IONS),
27  std::make_pair("COSMICS", LHCInfo::COSMICS),
28  std::make_pair("GAP", LHCInfo::GAP)};
29 
30  static const std::pair<const char*, LHCInfo::ParticleType> s_particleTypeMap[] = {
31  std::make_pair("PROTON", LHCInfo::PROTON),
32  std::make_pair("PB82", LHCInfo::PB82),
33  std::make_pair("AR18", LHCInfo::AR18),
34  std::make_pair("D", LHCInfo::D),
35  std::make_pair("XE54", LHCInfo::XE54)};
36 
38  for (auto const& i : s_fillTypeMap)
39  if (s_fill_type == i.first)
40  return i.second;
41  return LHCInfo::UNKNOWN;
42  }
43 
45  for (auto const& i : s_particleTypeMap)
46  if (s_particle_type == i.first)
47  return i.second;
48  return LHCInfo::NONE;
49  }
50 
51  namespace impl {
52 
53  template <>
54  LHCInfo::FillType from_string(const std::string& attributeValue) {
55  return from_string_impl<LHCInfo::FillType, &fillTypeFromString>(attributeValue, LHCInfo::UNKNOWN);
56  }
57 
58  template <>
60  return from_string_impl<LHCInfo::ParticleType, &particleTypeFromString>(attributeValue, LHCInfo::NONE);
61  }
62 
63  } // namespace impl
64 } // namespace cond
65 
67  : m_debug(pset.getUntrackedParameter<bool>("debug", false)),
68  m_startTime(),
69  m_endTime(),
70  m_samplingInterval((unsigned int)pset.getUntrackedParameter<unsigned int>("samplingInterval", 300)),
71  m_endFill(pset.getUntrackedParameter<bool>("endFill", true)),
72  m_name(pset.getUntrackedParameter<std::string>("name", "LHCInfoPopConSourceHandler")),
73  m_connectionString(pset.getUntrackedParameter<std::string>("connectionString", "")),
74  m_ecalConnectionString(pset.getUntrackedParameter<std::string>("ecalConnectionString", "")),
75  m_dipSchema(pset.getUntrackedParameter<std::string>("DIPSchema", "")),
76  m_authpath(pset.getUntrackedParameter<std::string>("authenticationPath", "")),
77  m_omsBaseUrl(pset.getUntrackedParameter<std::string>("omsBaseUrl", "")),
78  m_fillPayload(),
79  m_prevPayload(),
80  m_tmpBuffer() {
81  if (pset.exists("startTime")) {
82  m_startTime = boost::posix_time::time_from_string(pset.getUntrackedParameter<std::string>("startTime"));
83  }
84  boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
85  m_endTime = now;
86  if (pset.exists("endTime")) {
87  m_endTime = boost::posix_time::time_from_string(pset.getUntrackedParameter<std::string>("endTime"));
88  if (m_endTime > now)
89  m_endTime = now;
90  }
91 }
92 //L1: try with different m_dipSchema
93 //L2: try with different m_name
95 
96 namespace theLHCInfoImpl {
97 
98  struct IOVComp {
99  bool operator()(const cond::Time_t& x, const std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>& y) {
100  return (x < y.first);
101  }
102  };
103 
104  // function to search in the vector the target time
105  std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator search(
106  const cond::Time_t& val, const std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>& container) {
107  if (container.empty())
108  return container.end();
109  auto p = std::upper_bound(container.begin(), container.end(), val, IOVComp());
110  return (p != container.begin()) ? p - 1 : container.end();
111  }
112 
113  bool makeFillPayload(std::unique_ptr<LHCInfo>& targetPayload, const cond::OMSServiceResult& queryResult) {
114  bool ret = false;
115  if (!queryResult.empty()) {
116  auto row = *queryResult.begin();
117  auto currentFill = row.get<unsigned short>("fill_number");
118  auto bunches1 = row.get<unsigned short>("bunches_beam1");
119  auto bunches2 = row.get<unsigned short>("bunches_beam2");
120  auto collidingBunches = row.get<unsigned short>("bunches_colliding");
121  auto targetBunches = row.get<unsigned short>("bunches_target");
122  auto fillType = row.get<LHCInfo::FillType>("fill_type_runtime");
123  auto particleType1 = row.get<LHCInfo::ParticleType>("fill_type_party1");
124  auto particleType2 = row.get<LHCInfo::ParticleType>("fill_type_party2");
125  auto intensityBeam1 = row.get<float>("intensity_beam1");
126  auto intensityBeam2 = row.get<float>("intensity_beam2");
127  auto energy = row.get<float>("energy");
128  auto creationTime = row.get<boost::posix_time::ptime>("start_time");
129  auto stableBeamStartTime = row.get<boost::posix_time::ptime>("start_stable_beam");
130  auto beamDumpTime = row.get<boost::posix_time::ptime>("end_time");
131  auto injectionScheme = row.get<std::string>("injection_scheme");
132  targetPayload = std::make_unique<LHCInfo>();
133  targetPayload->setFillNumber(currentFill);
134  targetPayload->setBunchesInBeam1(bunches1);
135  targetPayload->setBunchesInBeam2(bunches2);
136  targetPayload->setCollidingBunches(collidingBunches);
137  targetPayload->setTargetBunches(targetBunches);
138  targetPayload->setFillType(fillType);
139  targetPayload->setParticleTypeForBeam1(particleType1);
140  targetPayload->setParticleTypeForBeam2(particleType2);
141  targetPayload->setIntensityForBeam1(intensityBeam1);
142  targetPayload->setIntensityForBeam2(intensityBeam2);
143  targetPayload->setEnergy(energy);
144  targetPayload->setCreationTime(cond::time::from_boost(creationTime));
145  targetPayload->setBeginTime(cond::time::from_boost(stableBeamStartTime));
146  targetPayload->setEndTime(cond::time::from_boost(beamDumpTime));
147  targetPayload->setInjectionScheme(injectionScheme);
148  ret = true;
149  }
150  return ret;
151  }
152 
153 } // namespace theLHCInfoImpl
154 
156  unsigned short fillId,
157  const boost::posix_time::ptime& beginFillTime,
158  const boost::posix_time::ptime& endFillTime) {
159  auto query = oms.query("lumisections");
160  query->addOutputVars({"start_time", "delivered_lumi", "recorded_lumi"});
161  query->filterEQ("fill_number", fillId);
162  query->filterGT("start_time", beginFillTime).filterLT("start_time", endFillTime);
164  size_t nlumi = 0;
165  if (query->execute()) {
166  auto res = query->result();
167  for (auto r : res) {
168  nlumi++;
169  auto lumiTime = r.get<boost::posix_time::ptime>("start_time");
170  auto delivLumi = r.get<float>("delivered_lumi");
171  auto recLumi = r.get<float>("recorded_lumi");
172  LHCInfo* thisLumiSectionInfo = m_fillPayload->cloneFill();
173  m_tmpBuffer.emplace_back(std::make_pair(cond::time::from_boost(lumiTime), thisLumiSectionInfo));
174  LHCInfo& payload = *thisLumiSectionInfo;
175  payload.setDelivLumi(delivLumi);
176  payload.setRecLumi(recLumi);
177  }
178  }
179  return nlumi;
180 }
181 
183  const boost::posix_time::ptime& beginFillTime,
184  const boost::posix_time::ptime& endFillTime) {
185  // unsure how to handle this.
186  // the old implementation is not helping: apparently it is checking only the bunchconfiguration for the first diptime set of values...
187  auto query1 = oms.query("diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam1");
188  query1->filterGT("dip_time", beginFillTime).filterLT("dip_time", endFillTime);
189  //This query is limited to 100 rows, but currently only one is used
190  //If all this data is needed and saved properly the limit has to be set: query1->limit(...)
191  if (query1->execute()) {
192  auto res = query1->result();
193  if (!res.empty()) {
194  std::bitset<LHCInfo::bunchSlots + 1> bunchConfiguration1(0ULL);
195  auto row = *res.begin();
196  auto vbunchConf1 = row.getArray<unsigned short>("value");
197  for (auto vb : vbunchConf1) {
198  if (vb != 0) {
199  unsigned short slot = (vb - 1) / 10 + 1;
200  bunchConfiguration1[slot] = true;
201  }
202  }
203  m_fillPayload->setBunchBitsetForBeam1(bunchConfiguration1);
204  }
205  }
206  auto query2 = oms.query("diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam2");
207  query2->filterGT("dip_time", beginFillTime).filterLT("dip_time", endFillTime);
208  //This query is limited to 100 rows, but currently only one is used
209  if (query2->execute()) {
210  auto res = query2->result();
211  if (!res.empty()) {
212  std::bitset<LHCInfo::bunchSlots + 1> bunchConfiguration2(0ULL);
213  auto row = *res.begin();
214  auto vbunchConf2 = row.getArray<unsigned short>("value");
215  for (auto vb : vbunchConf2) {
216  if (vb != 0) {
217  unsigned short slot = (vb - 1) / 10 + 1;
218  bunchConfiguration2[slot] = true;
219  }
220  }
221  m_fillPayload->setBunchBitsetForBeam2(bunchConfiguration2);
222  }
223  }
224 
225  auto query3 = oms.query("diplogger/dip/CMS/LHC/LumiPerBunch");
226  query3->filterGT("dip_time", beginFillTime).filterLT("dip_time", endFillTime);
227  //This query is limited to 100 rows, but currently only one is used
228  if (query3->execute()) {
229  auto res = query3->result();
230  if (!res.empty()) {
231  std::vector<float> lumiPerBX;
232  auto row = *res.begin();
233  auto lumiBunchInst = row.getArray<float>("lumi_bunch_inst");
234  for (auto lb : lumiBunchInst) {
235  if (lb != 0.) {
236  lumiPerBX.push_back(lb);
237  }
238  }
239  m_fillPayload->setLumiPerBX(lumiPerBX);
240  }
241  }
242 }
243 
245  const boost::posix_time::ptime& beginFillTime,
246  const boost::posix_time::ptime& endFillTime) {
247  //run the fifth query against the CTPPS schema
248  //Initializing the CMS_CTP_CTPPS_COND schema.
249  coral::ISchema& CTPPS = session.coralSession().schema("CMS_PPS_SPECT_COND");
250  //execute query for CTPPS Data
251  std::unique_ptr<coral::IQuery> CTPPSDataQuery(CTPPS.newQuery());
252  //FROM clause
253  CTPPSDataQuery->addToTableList(std::string("PPS_LHC_MACHINE_PARAMS"));
254  //SELECT clause
255  CTPPSDataQuery->addToOutputList(std::string("DIP_UPDATE_TIME"));
256  CTPPSDataQuery->addToOutputList(std::string("LHC_STATE"));
257  CTPPSDataQuery->addToOutputList(std::string("LHC_COMMENT"));
258  CTPPSDataQuery->addToOutputList(std::string("LUMI_SECTION"));
259  CTPPSDataQuery->addToOutputList(std::string("XING_ANGLE_P5_X_URAD"));
260  CTPPSDataQuery->addToOutputList(std::string("BETA_STAR_P5_X_M"));
261  //WHERE CLAUSE
262  coral::AttributeList CTPPSDataBindVariables;
263  CTPPSDataBindVariables.extend<coral::TimeStamp>(std::string("beginFillTime"));
264  CTPPSDataBindVariables.extend<coral::TimeStamp>(std::string("endFillTime"));
265  CTPPSDataBindVariables[std::string("beginFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(beginFillTime);
266  CTPPSDataBindVariables[std::string("endFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(endFillTime);
267  std::string conditionStr = std::string("DIP_UPDATE_TIME>= :beginFillTime and DIP_UPDATE_TIME< :endFillTime");
268  CTPPSDataQuery->setCondition(conditionStr, CTPPSDataBindVariables);
269  //ORDER BY clause
270  CTPPSDataQuery->addToOrderList(std::string("DIP_UPDATE_TIME"));
271  //define query output
272  coral::AttributeList CTPPSDataOutput;
273  CTPPSDataOutput.extend<coral::TimeStamp>(std::string("DIP_UPDATE_TIME"));
274  CTPPSDataOutput.extend<std::string>(std::string("LHC_STATE"));
275  CTPPSDataOutput.extend<std::string>(std::string("LHC_COMMENT"));
276  CTPPSDataOutput.extend<int>(std::string("LUMI_SECTION"));
277  CTPPSDataOutput.extend<float>(std::string("XING_ANGLE_P5_X_URAD"));
278  CTPPSDataOutput.extend<float>(std::string("BETA_STAR_P5_X_M"));
279  CTPPSDataQuery->defineOutput(CTPPSDataOutput);
280  //execute the query
281  coral::ICursor& CTPPSDataCursor = CTPPSDataQuery->execute();
282  cond::Time_t dipTime = 0;
283  std::string lhcState = "", lhcComment = "", ctppsStatus = "";
284  unsigned int lumiSection = 0;
285  float crossingAngle = 0., betastar = 0.;
286 
287  bool ret = false;
289  while (CTPPSDataCursor.next()) {
290  if (m_debug) {
291  std::ostringstream CTPPS;
292  CTPPSDataCursor.currentRow().toOutputStream(CTPPS);
293  }
294  coral::Attribute const& dipTimeAttribute = CTPPSDataCursor.currentRow()[std::string("DIP_UPDATE_TIME")];
295  if (!dipTimeAttribute.isNull()) {
296  dipTime = cond::time::from_boost(dipTimeAttribute.data<coral::TimeStamp>().time());
297  if (filter.process(dipTime)) {
298  ret = true;
299  coral::Attribute const& lhcStateAttribute = CTPPSDataCursor.currentRow()[std::string("LHC_STATE")];
300  if (!lhcStateAttribute.isNull()) {
301  lhcState = lhcStateAttribute.data<std::string>();
302  }
303  coral::Attribute const& lhcCommentAttribute = CTPPSDataCursor.currentRow()[std::string("LHC_COMMENT")];
304  if (!lhcCommentAttribute.isNull()) {
305  lhcComment = lhcCommentAttribute.data<std::string>();
306  }
307  coral::Attribute const& lumiSectionAttribute = CTPPSDataCursor.currentRow()[std::string("LUMI_SECTION")];
308  if (!lumiSectionAttribute.isNull()) {
309  lumiSection = lumiSectionAttribute.data<int>();
310  }
311  coral::Attribute const& crossingAngleXAttribute =
312  CTPPSDataCursor.currentRow()[std::string("XING_ANGLE_P5_X_URAD")];
313  if (!crossingAngleXAttribute.isNull()) {
314  crossingAngle = crossingAngleXAttribute.data<float>();
315  }
316  coral::Attribute const& betaStarXAttribute = CTPPSDataCursor.currentRow()[std::string("BETA_STAR_P5_X_M")];
317  if (!betaStarXAttribute.isNull()) {
318  betastar = betaStarXAttribute.data<float>();
319  }
320  for (auto it = filter.current(); it != m_tmpBuffer.end(); it++) {
321  // set the current values to all of the payloads of the lumi section samples after the current since
322  LHCInfo& payload = *(it->second);
323  payload.setCrossingAngle(crossingAngle);
324  payload.setBetaStar(betastar);
325  payload.setLhcState(lhcState);
326  payload.setLhcComment(lhcComment);
327  payload.setCtppsStatus(ctppsStatus);
328  payload.setLumiSection(lumiSection);
329  }
330  }
331  }
332  }
333  return ret;
334 }
335 
336 namespace theLHCInfoImpl {
337  static const std::map<std::string, int> vecMap = {
338  {"Beam1/beamPhaseMean", 1}, {"Beam2/beamPhaseMean", 2}, {"Beam1/cavPhaseMean", 3}, {"Beam2/cavPhaseMean", 4}};
340  const std::string& dipVal,
341  unsigned int elementNr,
342  float value,
343  LHCInfo& payload,
344  std::set<cond::Time_t>& initList) {
345  if (initList.find(since) == initList.end()) {
346  payload.beam1VC().resize(LHCInfo::bunchSlots, 0.);
347  payload.beam2VC().resize(LHCInfo::bunchSlots, 0.);
348  payload.beam1RF().resize(LHCInfo::bunchSlots, 0.);
349  payload.beam2RF().resize(LHCInfo::bunchSlots, 0.);
350  initList.insert(since);
351  }
352  // set the current values to all of the payloads of the lumi section samples after the current since
353  if (elementNr < LHCInfo::bunchSlots) {
354  switch (vecMap.at(dipVal)) {
355  case 1:
356  payload.beam1VC()[elementNr] = value;
357  break;
358  case 2:
359  payload.beam2VC()[elementNr] = value;
360  break;
361  case 3:
362  payload.beam1RF()[elementNr] = value;
363  break;
364  case 4:
365  payload.beam2RF()[elementNr] = value;
366  break;
367  default:
368  break;
369  }
370  }
371  }
372 } // namespace theLHCInfoImpl
373 
375  const boost::posix_time::ptime& lowerTime,
376  const boost::posix_time::ptime& upperTime,
377  bool update) {
378  //run the sixth query against the CMS_DCS_ENV_PVSS_COND schema
379  //Initializing the CMS_DCS_ENV_PVSS_COND schema.
380  coral::ISchema& ECAL = session.nominalSchema();
381  //start the transaction against the fill logging schema
382  //execute query for ECAL Data
383  std::unique_ptr<coral::IQuery> ECALDataQuery(ECAL.newQuery());
384  //FROM clause
385  ECALDataQuery->addToTableList(std::string("BEAM_PHASE"));
386  //SELECT clause
387  ECALDataQuery->addToOutputList(std::string("CHANGE_DATE"));
388  ECALDataQuery->addToOutputList(std::string("DIP_value"));
389  ECALDataQuery->addToOutputList(std::string("element_nr"));
390  ECALDataQuery->addToOutputList(std::string("VALUE_NUMBER"));
391  //WHERE CLAUSE
392  coral::AttributeList ECALDataBindVariables;
393  ECALDataBindVariables.extend<coral::TimeStamp>(std::string("lowerTime"));
394  ECALDataBindVariables.extend<coral::TimeStamp>(std::string("upperTime"));
395  ECALDataBindVariables[std::string("lowerTime")].data<coral::TimeStamp>() = coral::TimeStamp(lowerTime);
396  ECALDataBindVariables[std::string("upperTime")].data<coral::TimeStamp>() = coral::TimeStamp(upperTime);
397  std::string conditionStr = std::string(
398  "(DIP_value LIKE '%beamPhaseMean%' OR DIP_value LIKE '%cavPhaseMean%') AND CHANGE_DATE >= :lowerTime AND "
399  "CHANGE_DATE < :upperTime");
400 
401  ECALDataQuery->setCondition(conditionStr, ECALDataBindVariables);
402  //ORDER BY clause
403  ECALDataQuery->addToOrderList(std::string("CHANGE_DATE"));
404  ECALDataQuery->addToOrderList(std::string("DIP_value"));
405  ECALDataQuery->addToOrderList(std::string("element_nr"));
406  //define query output
407  coral::AttributeList ECALDataOutput;
408  ECALDataOutput.extend<coral::TimeStamp>(std::string("CHANGE_DATE"));
409  ECALDataOutput.extend<std::string>(std::string("DIP_value"));
410  ECALDataOutput.extend<unsigned int>(std::string("element_nr"));
411  ECALDataOutput.extend<float>(std::string("VALUE_NUMBER"));
412  //ECALDataQuery->limitReturnedRows( 14256 ); //3564 entries per vector.
413  ECALDataQuery->defineOutput(ECALDataOutput);
414  //execute the query
415  coral::ICursor& ECALDataCursor = ECALDataQuery->execute();
416  cond::Time_t changeTime = 0;
417  cond::Time_t firstTime = 0;
418  std::string dipVal = "";
419  unsigned int elementNr = 0;
420  float value = 0.;
421  std::set<cond::Time_t> initializedVectors;
423  bool ret = false;
424  if (m_prevPayload.get()) {
425  for (auto& lumiSlot : m_tmpBuffer) {
426  lumiSlot.second->setBeam1VC(m_prevPayload->beam1VC());
427  lumiSlot.second->setBeam2VC(m_prevPayload->beam2VC());
428  lumiSlot.second->setBeam1RF(m_prevPayload->beam1RF());
429  lumiSlot.second->setBeam2RF(m_prevPayload->beam2RF());
430  }
431  }
432  std::map<cond::Time_t, cond::Time_t> iovMap;
433  cond::Time_t lowerLumi = m_tmpBuffer.front().first;
434  while (ECALDataCursor.next()) {
435  if (m_debug) {
436  std::ostringstream ECAL;
437  ECALDataCursor.currentRow().toOutputStream(ECAL);
438  }
439  coral::Attribute const& changeDateAttribute = ECALDataCursor.currentRow()[std::string("CHANGE_DATE")];
440  if (!changeDateAttribute.isNull()) {
441  ret = true;
442  boost::posix_time::ptime chTime = changeDateAttribute.data<coral::TimeStamp>().time();
443  // move the first IOV found to the start of the fill interval selected
444  if (changeTime == 0) {
445  firstTime = cond::time::from_boost(chTime);
446  }
447  changeTime = cond::time::from_boost(chTime);
448  cond::Time_t iovTime = changeTime;
449  if (!update and changeTime == firstTime)
450  iovTime = lowerLumi;
451  coral::Attribute const& dipValAttribute = ECALDataCursor.currentRow()[std::string("DIP_value")];
452  coral::Attribute const& valueNumberAttribute = ECALDataCursor.currentRow()[std::string("VALUE_NUMBER")];
453  coral::Attribute const& elementNrAttribute = ECALDataCursor.currentRow()[std::string("element_nr")];
454  if (!dipValAttribute.isNull() and !valueNumberAttribute.isNull()) {
455  dipVal = dipValAttribute.data<std::string>();
456  elementNr = elementNrAttribute.data<unsigned int>();
457  value = valueNumberAttribute.data<float>();
458  if (edm::isNotFinite(value))
459  value = 0.;
460  if (filter.process(iovTime)) {
461  iovMap.insert(std::make_pair(changeTime, filter.current()->first));
462  for (auto it = filter.current(); it != m_tmpBuffer.end(); it++) {
463  LHCInfo& payload = *(it->second);
464  theLHCInfoImpl::setElementData(it->first, dipVal, elementNr, value, payload, initializedVectors);
465  }
466  }
467  //}
468  }
469  }
470  }
471  if (m_debug) {
472  for (auto& im : iovMap) {
473  edm::LogInfo(m_name) << "Found iov=" << im.first << " (" << cond::time::to_boost(im.first) << " ) moved to "
474  << im.second << " ( " << cond::time::to_boost(im.second) << " )";
475  }
476  }
477  return ret;
478 }
479 
481  bool add = false;
482  if (m_iovs.empty()) {
483  if (!m_lastPayloadEmpty)
484  add = true;
485  } else {
486  auto lastAdded = m_iovs.rbegin()->second;
487  if (lastAdded->fillNumber() != 0) {
488  add = true;
489  }
490  }
491  if (add) {
492  auto newPayload = std::make_shared<LHCInfo>();
493  m_iovs.insert(std::make_pair(iov, newPayload));
494  m_prevPayload = newPayload;
495  }
496 }
497 
498 namespace theLHCInfoImpl {
499  bool comparePayloads(const LHCInfo& rhs, const LHCInfo& lhs) {
500  if (rhs.fillNumber() != lhs.fillNumber() || rhs.delivLumi() != lhs.delivLumi() || rhs.recLumi() != lhs.recLumi() ||
501  rhs.instLumi() != lhs.instLumi() || rhs.instLumiError() != lhs.instLumiError() ||
502  rhs.crossingAngle() != lhs.crossingAngle() || rhs.betaStar() != lhs.betaStar() ||
503  rhs.lhcState() != lhs.lhcState() || rhs.lhcComment() != lhs.lhcComment() ||
504  rhs.ctppsStatus() != lhs.ctppsStatus()) {
505  return false;
506  }
507  return true;
508  }
509 
510  size_t transferPayloads(const std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>& buffer,
511  std::map<cond::Time_t, std::shared_ptr<LHCInfo>>& iovsToTransfer,
512  std::shared_ptr<LHCInfo>& prevPayload) {
513  size_t niovs = 0;
514  for (auto& iov : buffer) {
515  bool add = false;
516  auto payload = iov.second;
517  cond::Time_t since = iov.first;
518  if (iovsToTransfer.empty()) {
519  add = true;
520  } else {
521  LHCInfo& lastAdded = *iovsToTransfer.rbegin()->second;
522  if (!comparePayloads(lastAdded, *payload)) {
523  add = true;
524  }
525  }
526  if (add) {
527  niovs++;
528  iovsToTransfer.insert(std::make_pair(since, payload));
529  prevPayload = iov.second;
530  }
531  }
532  return niovs;
533  }
534 
535 } // namespace theLHCInfoImpl
536 
538  //reference to the last payload in the tag
539  Ref previousFill;
540 
541  //if a new tag is created, transfer fake fill from 1 to the first fill for the first time
542  if (tagInfo().size == 0) {
543  edm::LogInfo(m_name) << "New tag " << tagInfo().name << "; from " << m_name << "::getNewObjects";
544  } else {
545  //check what is already inside the database
546  edm::LogInfo(m_name) << "got info for tag " << tagInfo().name << ": size " << tagInfo().size
547  << ", last object valid since " << tagInfo().lastInterval.since << " ( "
548  << boost::posix_time::to_iso_extended_string(
549  cond::time::to_boost(tagInfo().lastInterval.since))
550  << " ); from " << m_name << "::getNewObjects";
551  }
552 
553  cond::Time_t lastSince = tagInfo().lastInterval.since;
554  if (tagInfo().isEmpty()) {
555  // for a new or empty tag, an empty payload should be added on top with since=1
556  addEmptyPayload(1);
557  lastSince = 1;
558  } else {
559  edm::LogInfo(m_name) << "The last Iov in tag " << tagInfo().name << " valid since " << lastSince << "from "
560  << m_name << "::getNewObjects";
561  }
562 
563  boost::posix_time::ptime executionTime = boost::posix_time::second_clock::local_time();
564  cond::Time_t targetSince = 0;
565  cond::Time_t endIov = cond::time::from_boost(executionTime);
566  if (!m_startTime.is_not_a_date_time()) {
567  targetSince = cond::time::from_boost(m_startTime);
568  }
569  if (lastSince > targetSince)
570  targetSince = lastSince;
571 
572  edm::LogInfo(m_name) << "Starting sampling at "
573  << boost::posix_time::to_simple_string(cond::time::to_boost(targetSince));
574 
575  //retrieve the data from the relational database source
577  //configure the connection
578  if (m_debug) {
579  connection.setMessageVerbosity(coral::Debug);
580  } else {
581  connection.setMessageVerbosity(coral::Error);
582  }
583  connection.setAuthenticationPath(m_authpath);
584  connection.configure();
585  //create the sessions
588  // fetch last payload when available
589  if (!tagInfo().lastInterval.payloadId.empty()) {
591  session3.transaction().start(true);
593  session3.transaction().commit();
594  }
595 
596  bool iovAdded = false;
597  while (true) {
598  if (targetSince >= endIov) {
599  edm::LogInfo(m_name) << "Sampling ended at the time "
600  << boost::posix_time::to_simple_string(cond::time::to_boost(endIov));
601  break;
602  }
603  bool updateEcal = false;
604  boost::posix_time::ptime targetTime = cond::time::to_boost(targetSince);
605  boost::posix_time::ptime startSampleTime;
606  boost::posix_time::ptime endSampleTime;
607 
608  cond::OMSService oms;
609  oms.connect(m_omsBaseUrl);
610  auto query = oms.query("fills");
611 
612  if (!m_endFill and m_prevPayload->fillNumber() and m_prevPayload->endTime() == 0ULL) {
613  // execute the query for the current fill
614  edm::LogInfo(m_name) << "Searching started fill #" << m_prevPayload->fillNumber();
615  query->filterEQ("fill_number", m_prevPayload->fillNumber());
616  bool foundFill = query->execute();
617  if (foundFill)
618  foundFill = theLHCInfoImpl::makeFillPayload(m_fillPayload, query->result());
619  if (!foundFill) {
620  edm::LogError(m_name) << "Could not find fill #" << m_prevPayload->fillNumber();
621  break;
622  }
623  updateEcal = true;
624  startSampleTime = cond::time::to_boost(lastSince);
625  } else {
626  edm::LogInfo(m_name) << "Searching new fill after " << boost::posix_time::to_simple_string(targetTime);
627  query->filterNotNull("start_stable_beam").filterNotNull("fill_number");
628  if (targetTime > cond::time::to_boost(m_prevPayload->createTime())) {
629  query->filterGE("start_time", targetTime);
630  } else {
631  query->filterGT("start_time", targetTime);
632  }
633 
634  query->filterLT("start_time", m_endTime);
635  if (m_endFill)
636  query->filterNotNull("end_time");
637  bool foundFill = query->execute();
638  if (foundFill)
639  foundFill = theLHCInfoImpl::makeFillPayload(m_fillPayload, query->result());
640  if (!foundFill) {
641  edm::LogInfo(m_name) << "No fill found - END of job.";
642  if (iovAdded)
643  addEmptyPayload(targetSince);
644  break;
645  }
646  startSampleTime = cond::time::to_boost(m_fillPayload->createTime());
647  }
648  cond::Time_t startFillTime = m_fillPayload->createTime();
649  cond::Time_t endFillTime = m_fillPayload->endTime();
650  unsigned short lhcFill = m_fillPayload->fillNumber();
651  if (endFillTime == 0ULL) {
652  edm::LogInfo(m_name) << "Found ongoing fill " << lhcFill << " created at " << cond::time::to_boost(startFillTime);
653  endSampleTime = executionTime;
654  targetSince = endIov;
655  } else {
656  edm::LogInfo(m_name) << "Found fill " << lhcFill << " created at " << cond::time::to_boost(startFillTime)
657  << " ending at " << cond::time::to_boost(endFillTime);
658  endSampleTime = cond::time::to_boost(endFillTime);
659  targetSince = endFillTime;
660  }
661 
662  getDipData(oms, startSampleTime, endSampleTime);
663  size_t nlumi = getLumiData(oms, lhcFill, startSampleTime, endSampleTime);
664  edm::LogInfo(m_name) << "Found " << nlumi << " lumisections during the fill " << lhcFill;
665  boost::posix_time::ptime flumiStart = cond::time::to_boost(m_tmpBuffer.front().first);
666  boost::posix_time::ptime flumiStop = cond::time::to_boost(m_tmpBuffer.back().first);
667  edm::LogInfo(m_name) << "First lumi starts at " << flumiStart << " last lumi starts at " << flumiStop;
668  session.transaction().start(true);
669  getCTPPSData(session, startSampleTime, endSampleTime);
670  session.transaction().commit();
671  session2.transaction().start(true);
672  getEcalData(session2, startSampleTime, endSampleTime, updateEcal);
673  session2.transaction().commit();
674  //
676  edm::LogInfo(m_name) << "Added " << niovs << " iovs within the Fill time";
677  m_tmpBuffer.clear();
678  iovAdded = true;
679  if (m_prevPayload->fillNumber() and m_fillPayload->endTime() != 0ULL)
680  addEmptyPayload(m_fillPayload->endTime());
681  }
682 }
683 
size
Write out results.
edm::ErrorSummaryEntry Error
bool comparePayloads(const LHCInfo &rhs, const LHCInfo &lhs)
std::string const & ctppsStatus() const
Definition: LHCInfo.cc:216
float const instLumiError() const
Definition: LHCInfo.cc:198
boost::posix_time::ptime m_startTime
Iov_t lastInterval
Definition: Types.h:73
std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo > > > m_tmpBuffer
std::unique_ptr< OMSServiceQuery > query(const std::string &function) const
Definition: OMSAccess.cc:129
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
std::unique_ptr< LHCInfo > m_fillPayload
ret
prodAgent to be discontinued
void start(bool readOnly=true)
Definition: Session.cc:18
T from_string(const std::string &attributeValue)
Definition: OMSAccess.h:28
bool empty() const
Definition: OMSAccess.cc:69
Time_t since
Definition: Types.h:53
std::string const & lhcComment() const
Definition: LHCInfo.cc:214
bool operator()(const cond::Time_t &x, const std::pair< cond::Time_t, std::shared_ptr< LHCInfo >> &y)
bool getEcalData(cond::persistency::Session &session, const boost::posix_time::ptime &lowerTime, const boost::posix_time::ptime &upperTime, bool update)
size_t size
Definition: Types.h:74
std::unique_ptr< T > fetchPayload(const cond::Hash &payloadHash)
Definition: Session.h:213
unsigned short const fillNumber() const
Definition: LHCInfo.cc:158
Log< level::Error, false > LogError
bool getCTPPSData(cond::persistency::Session &session, const boost::posix_time::ptime &beginFillTime, const boost::posix_time::ptime &endFillTime)
std::shared_ptr< LHCInfo > m_prevPayload
float const recLumi() const
Definition: LHCInfo.cc:194
Transaction & transaction()
Definition: Session.cc:52
Definition: Electron.h:6
std::string name
Definition: Types.h:72
Definition: query.py:1
cond::TagInfo_t const & tagInfo() const
std::string id() const override
void getDipData(const cond::OMSService &service, const boost::posix_time::ptime &beginFillTime, const boost::posix_time::ptime &endFillTime)
unsigned long long Time_t
Definition: Time.h:14
Time_t lumiTime(unsigned int run, unsigned int lumiId)
Definition: Time.cc:66
float const betaStar() const
Definition: LHCInfo.cc:184
boost::posix_time::ptime m_endTime
LHCInfo::FillType fillTypeFromString(const std::string &s_fill_type)
std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo > > >::const_iterator search(const cond::Time_t &val, const std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo >>> &container)
float const instLumi() const
Definition: LHCInfo.cc:196
Hash payloadId
Definition: Types.h:55
Session createSession(const std::string &connectionString, bool writeCapable=false)
float const crossingAngle() const
Definition: LHCInfo.cc:182
static const std::pair< const char *, LHCInfo::ParticleType > s_particleTypeMap[]
Definition: value.py:1
void setMessageVerbosity(coral::MsgLevel level)
Time_t from_boost(boost::posix_time::ptime bt)
static constexpr unsigned int kLumisectionsQueryLimit
Definition: LHCInfoHelper.h:12
std::string const & lhcState() const
Definition: LHCInfo.cc:212
Log< level::Info, false > LogInfo
ParticleType
Definition: LHCInfo.h:15
size_t getLumiData(const cond::OMSService &service, unsigned short fillId, const boost::posix_time::ptime &beginFillTime, const boost::posix_time::ptime &endFillTime)
bool makeFillPayload(std::unique_ptr< LHCInfo > &targetPayload, const cond::OMSServiceResult &queryResult)
size_t transferPayloads(const std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo >>> &buffer, std::map< cond::Time_t, std::shared_ptr< LHCInfo >> &iovsToTransfer, std::shared_ptr< LHCInfo > &prevPayload)
cond::persistency::Session & dbSession() const
void setElementData(cond::Time_t since, const std::string &dipVal, unsigned int elementNr, float value, LHCInfo &payload, std::set< cond::Time_t > &initList)
void add(std::map< std::string, TH1 *> &h, TH1 *hist)
#define update(a, b)
LHCInfo::ParticleType particleTypeFromString(const std::string &s_particle_type)
float x
static const std::pair< const char *, LHCInfo::FillType > s_fillTypeMap[]
static const std::map< std::string, int > vecMap
void connect(const std::string &baseUrl)
Definition: OMSAccess.cc:128
float const delivLumi() const
Definition: LHCInfo.cc:192
OMSServiceResultIterator begin() const
Definition: OMSAccess.cc:32
void setAuthenticationPath(const std::string &p)
const bool Debug
boost::posix_time::ptime to_boost(Time_t iValue)
static size_t const bunchSlots
Definition: LHCInfo.h:58
FillType
Definition: LHCInfo.h:14
LHCInfoPopConSourceHandler(const edm::ParameterSet &pset)