CMS 3D CMS Logo

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);
160  query->filterGT("start_time", beginFillTime).filterLT("start_time", endFillTime);
162  size_t nlumi = 0;
163  if (query->execute()) {
164  auto res = query->result();
165  for (auto r : res) {
166  nlumi++;
167  auto lumiTime = r.get<boost::posix_time::ptime>("start_time");
168  auto delivLumi = r.get<float>("delivered_lumi");
169  auto recLumi = r.get<float>("recorded_lumi");
170  LHCInfo* thisLumiSectionInfo = m_fillPayload->cloneFill();
171  m_tmpBuffer.emplace_back(std::make_pair(cond::time::from_boost(lumiTime), thisLumiSectionInfo));
172  LHCInfo& payload = *thisLumiSectionInfo;
173  payload.setDelivLumi(delivLumi);
174  payload.setRecLumi(recLumi);
175  }
176  }
177  return nlumi;
178 }
179 
180 namespace LHCInfoImpl {
182  LumiSectionFilter(const std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>& samples)
183  : currLow(samples.begin()), currUp(samples.begin()), end(samples.end()) {
184  currUp++;
185  }
186 
187  void reset(const std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>& samples) {
188  currLow = samples.begin();
189  currUp = samples.begin();
190  currUp++;
191  end = samples.end();
192  currentDipTime = 0;
193  }
194 
195  bool process(cond::Time_t dipTime) {
196  if (currLow == end)
197  return false;
198  bool search = false;
199  if (currentDipTime == 0) {
200  search = true;
201  } else {
202  if (dipTime == currentDipTime)
203  return true;
204  else {
206  if (currUp != end)
207  upper = currUp->first;
208  if (dipTime < upper)
209  return false;
210  else {
211  search = true;
212  }
213  }
214  }
215  if (search) {
216  while (currUp != end and currUp->first < dipTime) {
217  currLow++;
218  currUp++;
219  }
220  currentDipTime = dipTime;
221  return currLow != end;
222  }
223  return false;
224  }
225 
226  cond::Time_t currentSince() { return currLow->first; }
227  LHCInfo& currentPayload() { return *currLow->second; }
228 
229  std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator current() { return currLow; }
230  std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator currLow;
231  std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator currUp;
232  std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator end;
234  };
235 } // namespace LHCInfoImpl
236 
238  const boost::posix_time::ptime& beginFillTime,
239  const boost::posix_time::ptime& endFillTime) {
240  // unsure how to handle this.
241  // the old implementation is not helping: apparently it is checking only the bunchconfiguration for the first diptime set of values...
242  auto query1 = oms.query("diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam1");
243  query1->filterGT("dip_time", beginFillTime).filterLT("dip_time", endFillTime);
244  //This query is limited to 100 rows, but currently only one is used
245  //If all this data is needed and saved properly the limit has to be set: query1->limit(...)
246  if (query1->execute()) {
247  auto res = query1->result();
248  if (!res.empty()) {
249  std::bitset<LHCInfo::bunchSlots + 1> bunchConfiguration1(0ULL);
250  auto row = *res.begin();
251  auto vbunchConf1 = row.getArray<unsigned short>("value");
252  for (auto vb : vbunchConf1) {
253  if (vb != 0) {
254  unsigned short slot = (vb - 1) / 10 + 1;
255  bunchConfiguration1[slot] = true;
256  }
257  }
258  m_fillPayload->setBunchBitsetForBeam1(bunchConfiguration1);
259  }
260  }
261  auto query2 = oms.query("diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam2");
262  query2->filterGT("dip_time", beginFillTime).filterLT("dip_time", endFillTime);
263  //This query is limited to 100 rows, but currently only one is used
264  if (query2->execute()) {
265  auto res = query2->result();
266  if (!res.empty()) {
267  std::bitset<LHCInfo::bunchSlots + 1> bunchConfiguration2(0ULL);
268  auto row = *res.begin();
269  auto vbunchConf2 = row.getArray<unsigned short>("value");
270  for (auto vb : vbunchConf2) {
271  if (vb != 0) {
272  unsigned short slot = (vb - 1) / 10 + 1;
273  bunchConfiguration2[slot] = true;
274  }
275  }
276  m_fillPayload->setBunchBitsetForBeam2(bunchConfiguration2);
277  }
278  }
279 
280  auto query3 = oms.query("diplogger/dip/CMS/LHC/LumiPerBunch");
281  query3->filterGT("dip_time", beginFillTime).filterLT("dip_time", endFillTime);
282  //This query is limited to 100 rows, but currently only one is used
283  if (query3->execute()) {
284  auto res = query3->result();
285  if (!res.empty()) {
286  std::vector<float> lumiPerBX;
287  auto row = *res.begin();
288  auto lumiBunchInst = row.getArray<float>("lumi_bunch_inst");
289  for (auto lb : lumiBunchInst) {
290  if (lb != 0.) {
291  lumiPerBX.push_back(lb);
292  }
293  }
294  m_fillPayload->setLumiPerBX(lumiPerBX);
295  }
296  }
297 }
298 
300  const boost::posix_time::ptime& beginFillTime,
301  const boost::posix_time::ptime& endFillTime) {
302  //run the fifth query against the CTPPS schema
303  //Initializing the CMS_CTP_CTPPS_COND schema.
304  coral::ISchema& CTPPS = session.coralSession().schema("CMS_PPS_SPECT_COND");
305  //execute query for CTPPS Data
306  std::unique_ptr<coral::IQuery> CTPPSDataQuery(CTPPS.newQuery());
307  //FROM clause
308  CTPPSDataQuery->addToTableList(std::string("PPS_LHC_MACHINE_PARAMS"));
309  //SELECT clause
310  CTPPSDataQuery->addToOutputList(std::string("DIP_UPDATE_TIME"));
311  CTPPSDataQuery->addToOutputList(std::string("LHC_STATE"));
312  CTPPSDataQuery->addToOutputList(std::string("LHC_COMMENT"));
313  CTPPSDataQuery->addToOutputList(std::string("LUMI_SECTION"));
314  CTPPSDataQuery->addToOutputList(std::string("XING_ANGLE_P5_X_URAD"));
315  CTPPSDataQuery->addToOutputList(std::string("BETA_STAR_P5_X_M"));
316  //WHERE CLAUSE
317  coral::AttributeList CTPPSDataBindVariables;
318  CTPPSDataBindVariables.extend<coral::TimeStamp>(std::string("beginFillTime"));
319  CTPPSDataBindVariables.extend<coral::TimeStamp>(std::string("endFillTime"));
320  CTPPSDataBindVariables[std::string("beginFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(beginFillTime);
321  CTPPSDataBindVariables[std::string("endFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(endFillTime);
322  std::string conditionStr = std::string("DIP_UPDATE_TIME>= :beginFillTime and DIP_UPDATE_TIME< :endFillTime");
323  CTPPSDataQuery->setCondition(conditionStr, CTPPSDataBindVariables);
324  //ORDER BY clause
325  CTPPSDataQuery->addToOrderList(std::string("DIP_UPDATE_TIME"));
326  //define query output
327  coral::AttributeList CTPPSDataOutput;
328  CTPPSDataOutput.extend<coral::TimeStamp>(std::string("DIP_UPDATE_TIME"));
329  CTPPSDataOutput.extend<std::string>(std::string("LHC_STATE"));
330  CTPPSDataOutput.extend<std::string>(std::string("LHC_COMMENT"));
331  CTPPSDataOutput.extend<int>(std::string("LUMI_SECTION"));
332  CTPPSDataOutput.extend<float>(std::string("XING_ANGLE_P5_X_URAD"));
333  CTPPSDataOutput.extend<float>(std::string("BETA_STAR_P5_X_M"));
334  CTPPSDataQuery->defineOutput(CTPPSDataOutput);
335  //execute the query
336  coral::ICursor& CTPPSDataCursor = CTPPSDataQuery->execute();
337  cond::Time_t dipTime = 0;
338  std::string lhcState = "", lhcComment = "", ctppsStatus = "";
339  unsigned int lumiSection = 0;
340  float crossingAngle = 0., betastar = 0.;
341 
342  bool ret = false;
344  while (CTPPSDataCursor.next()) {
345  if (m_debug) {
346  std::ostringstream CTPPS;
347  CTPPSDataCursor.currentRow().toOutputStream(CTPPS);
348  }
349  coral::Attribute const& dipTimeAttribute = CTPPSDataCursor.currentRow()[std::string("DIP_UPDATE_TIME")];
350  if (!dipTimeAttribute.isNull()) {
351  dipTime = cond::time::from_boost(dipTimeAttribute.data<coral::TimeStamp>().time());
352  if (filter.process(dipTime)) {
353  ret = true;
354  coral::Attribute const& lhcStateAttribute = CTPPSDataCursor.currentRow()[std::string("LHC_STATE")];
355  if (!lhcStateAttribute.isNull()) {
356  lhcState = lhcStateAttribute.data<std::string>();
357  }
358  coral::Attribute const& lhcCommentAttribute = CTPPSDataCursor.currentRow()[std::string("LHC_COMMENT")];
359  if (!lhcCommentAttribute.isNull()) {
360  lhcComment = lhcCommentAttribute.data<std::string>();
361  }
362  coral::Attribute const& lumiSectionAttribute = CTPPSDataCursor.currentRow()[std::string("LUMI_SECTION")];
363  if (!lumiSectionAttribute.isNull()) {
364  lumiSection = lumiSectionAttribute.data<int>();
365  }
366  coral::Attribute const& crossingAngleXAttribute =
367  CTPPSDataCursor.currentRow()[std::string("XING_ANGLE_P5_X_URAD")];
368  if (!crossingAngleXAttribute.isNull()) {
369  crossingAngle = crossingAngleXAttribute.data<float>();
370  }
371  coral::Attribute const& betaStarXAttribute = CTPPSDataCursor.currentRow()[std::string("BETA_STAR_P5_X_M")];
372  if (!betaStarXAttribute.isNull()) {
373  betastar = betaStarXAttribute.data<float>();
374  }
375  for (auto it = filter.current(); it != m_tmpBuffer.end(); it++) {
376  // set the current values to all of the payloads of the lumi section samples after the current since
377  LHCInfo& payload = *(it->second);
378  payload.setCrossingAngle(crossingAngle);
379  payload.setBetaStar(betastar);
380  payload.setLhcState(lhcState);
381  payload.setLhcComment(lhcComment);
382  payload.setCtppsStatus(ctppsStatus);
383  payload.setLumiSection(lumiSection);
384  }
385  }
386  }
387  }
388  return ret;
389 }
390 
391 namespace LHCInfoImpl {
392  static const std::map<std::string, int> vecMap = {
393  {"Beam1/beamPhaseMean", 1}, {"Beam2/beamPhaseMean", 2}, {"Beam1/cavPhaseMean", 3}, {"Beam2/cavPhaseMean", 4}};
395  const std::string& dipVal,
396  unsigned int elementNr,
397  float value,
398  LHCInfo& payload,
399  std::set<cond::Time_t>& initList) {
400  if (initList.find(since) == initList.end()) {
401  payload.beam1VC().resize(LHCInfo::bunchSlots, 0.);
402  payload.beam2VC().resize(LHCInfo::bunchSlots, 0.);
403  payload.beam1RF().resize(LHCInfo::bunchSlots, 0.);
404  payload.beam2RF().resize(LHCInfo::bunchSlots, 0.);
405  initList.insert(since);
406  }
407  // set the current values to all of the payloads of the lumi section samples after the current since
408  if (elementNr < LHCInfo::bunchSlots) {
409  switch (vecMap.at(dipVal)) {
410  case 1:
411  payload.beam1VC()[elementNr] = value;
412  break;
413  case 2:
414  payload.beam2VC()[elementNr] = value;
415  break;
416  case 3:
417  payload.beam1RF()[elementNr] = value;
418  break;
419  case 4:
420  payload.beam2RF()[elementNr] = value;
421  break;
422  default:
423  break;
424  }
425  }
426  }
427 } // namespace LHCInfoImpl
428 
430  const boost::posix_time::ptime& lowerTime,
431  const boost::posix_time::ptime& upperTime,
432  bool update) {
433  //run the sixth query against the CMS_DCS_ENV_PVSS_COND schema
434  //Initializing the CMS_DCS_ENV_PVSS_COND schema.
435  coral::ISchema& ECAL = session.nominalSchema();
436  //start the transaction against the fill logging schema
437  //execute query for ECAL Data
438  std::unique_ptr<coral::IQuery> ECALDataQuery(ECAL.newQuery());
439  //FROM clause
440  ECALDataQuery->addToTableList(std::string("BEAM_PHASE"));
441  //SELECT clause
442  ECALDataQuery->addToOutputList(std::string("CHANGE_DATE"));
443  ECALDataQuery->addToOutputList(std::string("DIP_value"));
444  ECALDataQuery->addToOutputList(std::string("element_nr"));
445  ECALDataQuery->addToOutputList(std::string("VALUE_NUMBER"));
446  //WHERE CLAUSE
447  coral::AttributeList ECALDataBindVariables;
448  ECALDataBindVariables.extend<coral::TimeStamp>(std::string("lowerTime"));
449  ECALDataBindVariables.extend<coral::TimeStamp>(std::string("upperTime"));
450  ECALDataBindVariables[std::string("lowerTime")].data<coral::TimeStamp>() = coral::TimeStamp(lowerTime);
451  ECALDataBindVariables[std::string("upperTime")].data<coral::TimeStamp>() = coral::TimeStamp(upperTime);
452  std::string conditionStr = std::string(
453  "(DIP_value LIKE '%beamPhaseMean%' OR DIP_value LIKE '%cavPhaseMean%') AND CHANGE_DATE >= :lowerTime AND "
454  "CHANGE_DATE < :upperTime");
455 
456  ECALDataQuery->setCondition(conditionStr, ECALDataBindVariables);
457  //ORDER BY clause
458  ECALDataQuery->addToOrderList(std::string("CHANGE_DATE"));
459  ECALDataQuery->addToOrderList(std::string("DIP_value"));
460  ECALDataQuery->addToOrderList(std::string("element_nr"));
461  //define query output
462  coral::AttributeList ECALDataOutput;
463  ECALDataOutput.extend<coral::TimeStamp>(std::string("CHANGE_DATE"));
464  ECALDataOutput.extend<std::string>(std::string("DIP_value"));
465  ECALDataOutput.extend<unsigned int>(std::string("element_nr"));
466  ECALDataOutput.extend<float>(std::string("VALUE_NUMBER"));
467  //ECALDataQuery->limitReturnedRows( 14256 ); //3564 entries per vector.
468  ECALDataQuery->defineOutput(ECALDataOutput);
469  //execute the query
470  coral::ICursor& ECALDataCursor = ECALDataQuery->execute();
471  cond::Time_t changeTime = 0;
472  cond::Time_t firstTime = 0;
473  std::string dipVal = "";
474  unsigned int elementNr = 0;
475  float value = 0.;
476  std::set<cond::Time_t> initializedVectors;
478  bool ret = false;
479  if (m_prevPayload.get()) {
480  for (auto& lumiSlot : m_tmpBuffer) {
481  lumiSlot.second->setBeam1VC(m_prevPayload->beam1VC());
482  lumiSlot.second->setBeam2VC(m_prevPayload->beam2VC());
483  lumiSlot.second->setBeam1RF(m_prevPayload->beam1RF());
484  lumiSlot.second->setBeam2RF(m_prevPayload->beam2RF());
485  }
486  }
487  std::map<cond::Time_t, cond::Time_t> iovMap;
488  cond::Time_t lowerLumi = m_tmpBuffer.front().first;
489  while (ECALDataCursor.next()) {
490  if (m_debug) {
491  std::ostringstream ECAL;
492  ECALDataCursor.currentRow().toOutputStream(ECAL);
493  }
494  coral::Attribute const& changeDateAttribute = ECALDataCursor.currentRow()[std::string("CHANGE_DATE")];
495  if (!changeDateAttribute.isNull()) {
496  ret = true;
497  boost::posix_time::ptime chTime = changeDateAttribute.data<coral::TimeStamp>().time();
498  // move the first IOV found to the start of the fill interval selected
499  if (changeTime == 0) {
500  firstTime = cond::time::from_boost(chTime);
501  }
502  changeTime = cond::time::from_boost(chTime);
503  cond::Time_t iovTime = changeTime;
504  if (!update and changeTime == firstTime)
505  iovTime = lowerLumi;
506  coral::Attribute const& dipValAttribute = ECALDataCursor.currentRow()[std::string("DIP_value")];
507  coral::Attribute const& valueNumberAttribute = ECALDataCursor.currentRow()[std::string("VALUE_NUMBER")];
508  coral::Attribute const& elementNrAttribute = ECALDataCursor.currentRow()[std::string("element_nr")];
509  if (!dipValAttribute.isNull() and !valueNumberAttribute.isNull()) {
510  dipVal = dipValAttribute.data<std::string>();
511  elementNr = elementNrAttribute.data<unsigned int>();
512  value = valueNumberAttribute.data<float>();
513  if (std::isnan(value))
514  value = 0.;
515  if (filter.process(iovTime)) {
516  iovMap.insert(std::make_pair(changeTime, filter.current()->first));
517  for (auto it = filter.current(); it != m_tmpBuffer.end(); it++) {
518  LHCInfo& payload = *(it->second);
519  LHCInfoImpl::setElementData(it->first, dipVal, elementNr, value, payload, initializedVectors);
520  }
521  }
522  //}
523  }
524  }
525  }
526  if (m_debug) {
527  for (auto& im : iovMap) {
528  edm::LogInfo(m_name) << "Found iov=" << im.first << " (" << cond::time::to_boost(im.first) << " ) moved to "
529  << im.second << " ( " << cond::time::to_boost(im.second) << " )";
530  }
531  }
532  return ret;
533 }
534 
536  bool add = false;
537  if (m_iovs.empty()) {
538  if (!m_lastPayloadEmpty)
539  add = true;
540  } else {
541  auto lastAdded = m_iovs.rbegin()->second;
542  if (lastAdded->fillNumber() != 0) {
543  add = true;
544  }
545  }
546  if (add) {
547  auto newPayload = std::make_shared<LHCInfo>();
548  m_iovs.insert(std::make_pair(iov, newPayload));
549  m_prevPayload = newPayload;
550  }
551 }
552 
553 namespace LHCInfoImpl {
554  bool comparePayloads(const LHCInfo& rhs, const LHCInfo& lhs) {
555  if (rhs.fillNumber() != lhs.fillNumber())
556  return false;
557  if (rhs.delivLumi() != lhs.delivLumi())
558  return false;
559  if (rhs.recLumi() != lhs.recLumi())
560  return false;
561  if (rhs.instLumi() != lhs.instLumi())
562  return false;
563  if (rhs.instLumiError() != lhs.instLumiError())
564  return false;
565  if (rhs.crossingAngle() != rhs.crossingAngle())
566  return false;
567  if (rhs.betaStar() != rhs.betaStar())
568  return false;
569  if (rhs.lhcState() != rhs.lhcState())
570  return false;
571  if (rhs.lhcComment() != rhs.lhcComment())
572  return false;
573  if (rhs.ctppsStatus() != rhs.ctppsStatus())
574  return false;
575  return true;
576  }
577 
578  size_t transferPayloads(const std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>& buffer,
579  std::map<cond::Time_t, std::shared_ptr<LHCInfo>>& iovsToTransfer,
580  std::shared_ptr<LHCInfo>& prevPayload) {
581  size_t niovs = 0;
582  for (auto& iov : buffer) {
583  bool add = false;
584  auto payload = iov.second;
585  cond::Time_t since = iov.first;
586  if (iovsToTransfer.empty()) {
587  add = true;
588  } else {
589  LHCInfo& lastAdded = *iovsToTransfer.rbegin()->second;
590  if (!comparePayloads(lastAdded, *payload)) {
591  add = true;
592  }
593  }
594  if (add) {
595  niovs++;
596  iovsToTransfer.insert(std::make_pair(since, payload));
597  prevPayload = iov.second;
598  }
599  }
600  return niovs;
601  }
602 
603 } // namespace LHCInfoImpl
604 
606  //reference to the last payload in the tag
607  Ref previousFill;
608 
609  //if a new tag is created, transfer fake fill from 1 to the first fill for the first time
610  if (tagInfo().size == 0) {
611  edm::LogInfo(m_name) << "New tag " << tagInfo().name << "; from " << m_name << "::getNewObjects";
612  } else {
613  //check what is already inside the database
614  edm::LogInfo(m_name) << "got info for tag " << tagInfo().name << ": size " << tagInfo().size
615  << ", last object valid since " << tagInfo().lastInterval.since << " ( "
616  << boost::posix_time::to_iso_extended_string(
617  cond::time::to_boost(tagInfo().lastInterval.since))
618  << " ); from " << m_name << "::getNewObjects";
619  }
620 
621  cond::Time_t lastSince = tagInfo().lastInterval.since;
622  if (tagInfo().isEmpty()) {
623  // for a new or empty tag, an empty payload should be added on top with since=1
624  addEmptyPayload(1);
625  lastSince = 1;
626  } else {
627  edm::LogInfo(m_name) << "The last Iov in tag " << tagInfo().name << " valid since " << lastSince << "from "
628  << m_name << "::getNewObjects";
629  }
630 
631  boost::posix_time::ptime executionTime = boost::posix_time::second_clock::local_time();
632  cond::Time_t targetSince = 0;
633  cond::Time_t endIov = cond::time::from_boost(executionTime);
634  if (!m_startTime.is_not_a_date_time()) {
635  targetSince = cond::time::from_boost(m_startTime);
636  }
637  if (lastSince > targetSince)
638  targetSince = lastSince;
639 
640  edm::LogInfo(m_name) << "Starting sampling at "
641  << boost::posix_time::to_simple_string(cond::time::to_boost(targetSince));
642 
643  //retrieve the data from the relational database source
645  //configure the connection
646  if (m_debug) {
647  connection.setMessageVerbosity(coral::Debug);
648  } else {
649  connection.setMessageVerbosity(coral::Error);
650  }
651  connection.setAuthenticationPath(m_authpath);
652  connection.configure();
653  //create the sessions
654  cond::persistency::Session session = connection.createSession(m_connectionString, false);
656  // fetch last payload when available
657  if (!tagInfo().lastInterval.payloadId.empty()) {
659  session3.transaction().start(true);
661  session3.transaction().commit();
662  }
663 
664  bool iovAdded = false;
665  while (true) {
666  if (targetSince >= endIov) {
667  edm::LogInfo(m_name) << "Sampling ended at the time "
668  << boost::posix_time::to_simple_string(cond::time::to_boost(endIov));
669  break;
670  }
671  bool updateEcal = false;
672  boost::posix_time::ptime targetTime = cond::time::to_boost(targetSince);
673  boost::posix_time::ptime startSampleTime;
674  boost::posix_time::ptime endSampleTime;
675 
676  cond::OMSService oms;
677  oms.connect(m_omsBaseUrl);
678  auto query = oms.query("fills");
679 
680  if (!m_endFill and m_prevPayload->fillNumber() and m_prevPayload->endTime() == 0ULL) {
681  // execute the query for the current fill
682  edm::LogInfo(m_name) << "Searching started fill #" << m_prevPayload->fillNumber();
683  query->filterEQ("fill_number", m_prevPayload->fillNumber());
684  bool foundFill = query->execute();
685  if (foundFill)
686  foundFill = LHCInfoImpl::makeFillPayload(m_fillPayload, query->result());
687  if (!foundFill) {
688  edm::LogError(m_name) << "Could not find fill #" << m_prevPayload->fillNumber();
689  break;
690  }
691  updateEcal = true;
692  startSampleTime = cond::time::to_boost(lastSince);
693  } else {
694  edm::LogInfo(m_name) << "Searching new fill after " << boost::posix_time::to_simple_string(targetTime);
695  query->filterNotNull("start_stable_beam").filterNotNull("fill_number");
696  if (targetTime > cond::time::to_boost(m_prevPayload->createTime())) {
697  query->filterGE("start_time", targetTime);
698  } else {
699  query->filterGT("start_time", targetTime);
700  }
701 
702  if (m_endFill)
703  query->filterNotNull("end_time");
704  bool foundFill = query->execute();
705  if (foundFill)
706  foundFill = LHCInfoImpl::makeFillPayload(m_fillPayload, query->result());
707  if (!foundFill) {
708  edm::LogInfo(m_name) << "No fill found - END of job.";
709  if (iovAdded)
710  addEmptyPayload(targetSince);
711  break;
712  }
713  startSampleTime = cond::time::to_boost(m_fillPayload->createTime());
714  }
715  cond::Time_t startFillTime = m_fillPayload->createTime();
716  cond::Time_t endFillTime = m_fillPayload->endTime();
717  unsigned short lhcFill = m_fillPayload->fillNumber();
718  if (endFillTime == 0ULL) {
719  edm::LogInfo(m_name) << "Found ongoing fill " << lhcFill << " created at " << cond::time::to_boost(startFillTime);
720  endSampleTime = executionTime;
721  targetSince = endIov;
722  } else {
723  edm::LogInfo(m_name) << "Found fill " << lhcFill << " created at " << cond::time::to_boost(startFillTime)
724  << " ending at " << cond::time::to_boost(endFillTime);
725  endSampleTime = cond::time::to_boost(endFillTime);
726  targetSince = endFillTime;
727  }
728 
729  getDipData(oms, startSampleTime, endSampleTime);
730  size_t nlumi = getLumiData(oms, lhcFill, startSampleTime, endSampleTime);
731  edm::LogInfo(m_name) << "Found " << nlumi << " lumisections during the fill " << lhcFill;
732  boost::posix_time::ptime flumiStart = cond::time::to_boost(m_tmpBuffer.front().first);
733  boost::posix_time::ptime flumiStop = cond::time::to_boost(m_tmpBuffer.back().first);
734  edm::LogInfo(m_name) << "First lumi starts at " << flumiStart << " last lumi starts at " << flumiStop;
735  session.transaction().start(true);
736  getCTTPSData(session, startSampleTime, endSampleTime);
737  session.transaction().commit();
738  session2.transaction().start(true);
739  getEcalData(session2, startSampleTime, endSampleTime, updateEcal);
740  session2.transaction().commit();
741  //
743  edm::LogInfo(m_name) << "Added " << niovs << " iovs within the Fill time";
744  m_tmpBuffer.clear();
745  iovAdded = true;
746  if (m_prevPayload->fillNumber() and m_fillPayload->endTime() != 0ULL)
747  addEmptyPayload(m_fillPayload->endTime());
748  }
749 }
750 
size
Write out results.
edm::ErrorSummaryEntry Error
def isnan(num)
std::string const & ctppsStatus() const
Definition: LHCInfo.cc:216
static const std::map< std::string, int > vecMap
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)
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< OMSServiceQuery > query(const std::string &function) const
Definition: OMSAccess.cc:119
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:59
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
std::string const & lhcComment() const
Definition: LHCInfo.cc:214
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::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo > > >::const_iterator currUp
std::shared_ptr< LHCInfo > m_prevPayload
float const recLumi() const
Definition: LHCInfo.cc:194
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
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
LumiSectionFilter(const std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo >>> &samples)
LHCInfo::FillType fillTypeFromString(const std::string &s_fill_type)
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)
std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo > > >::const_iterator end
float const crossingAngle() const
Definition: LHCInfo.cc:182
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[]
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
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)
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)
void setElementData(cond::Time_t since, const std::string &dipVal, unsigned int elementNr, float value, LHCInfo &payload, std::set< cond::Time_t > &initList)
cond::persistency::Session & dbSession() const
coral::ISessionProxy & coralSession()
Definition: Session.cc:218
Definition: plugin.cc:23
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[]
void connect(const std::string &baseUrl)
Definition: OMSAccess.cc:118
float const delivLumi() const
Definition: LHCInfo.cc:192
OMSServiceResultIterator begin() const
Definition: OMSAccess.cc:32
void setAuthenticationPath(const std::string &p)
const bool Debug
const Time_t MAX_VAL(std::numeric_limits< Time_t >::max())
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)
std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo > > >::const_iterator current()