CMS 3D CMS Logo

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