CMS 3D CMS Logo

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