CMS 3D CMS Logo

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