CMS 3D CMS Logo

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