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" 35 namespace theLHCInfoPerFillPopConImpl {
37 static const std::pair<const char*, LHCInfoPerFill::FillType>
s_fillTypeMap[] = {
52 if (s_fill_type ==
i.first)
59 if (s_particle_type ==
i.first)
69 return from_string_impl<LHCInfoPerFill::FillType, &theLHCInfoPerFillPopConImpl::fillTypeFromString>(
75 return from_string_impl<LHCInfoPerFill::ParticleType, &theLHCInfoPerFillPopConImpl::particleTypeFromString>(
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");
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");
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);
119 targetPayload->setEndTime(beamDumpTime);
120 targetPayload->setInjectionScheme(injectionScheme);
128 static const std::map<std::string, int>
vecMap = {
129 {
"Beam1/beamPhaseMean", 1}, {
"Beam2/beamPhaseMean", 2}, {
"Beam1/cavPhaseMean", 3}, {
"Beam2/cavPhaseMean", 4}};
132 unsigned int elementNr,
135 std::set<cond::Time_t>& initList) {
136 if (initList.find(
since) == initList.end()) {
141 initList.insert(
since);
145 switch (
vecMap.at(dipVal)) {
178 std::shared_ptr<LHCInfoPerFill>& prevPayload) {
180 std::stringstream condIovs;
181 std::stringstream formattedIovs;
182 for (
auto& iov :
buffer) {
186 if (iovsToTransfer.empty()) {
196 condIovs <<
since <<
" ";
199 prevPayload = iov.second;
202 edm::LogInfo(
"transferPayloads") <<
"TRANSFERED IOVS: " << condIovs.str();
203 edm::LogInfo(
"transferPayloads") <<
"FORMATTED TRANSFERED IOVS: " << formattedIovs.str();
216 m_name(
pset.getUntrackedParameter<
std::
string>(
"name",
"LHCInfoPerFillPopConSourceHandler")),
228 boost::posix_time::ptime
now = boost::posix_time::second_clock::local_time();
250 << boost::posix_time::to_iso_extended_string(
252 <<
" ); from " <<
m_name <<
"::getNewObjects";
262 <<
m_name <<
"::getNewObjects";
265 boost::posix_time::ptime executionTime = boost::posix_time::second_clock::local_time();
271 if (lastSince > targetSince)
272 targetSince = lastSince;
291 if (!
tagInfo().lastInterval.payloadId.empty()) {
300 if (targetSince >= executionTimeIov) {
305 bool updateEcal =
false;
307 boost::posix_time::ptime startSampleTime;
308 boost::posix_time::ptime endSampleTime;
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");
317 query->filterGE(
"start_time", targetTime);
319 query->filterGT(
"start_time", targetTime);
324 query->filterNotNull(
"end_time");
325 bool foundFill =
query->execute();
339 bool ongoingFill = endFillTime == 0ULL;
343 endSampleTime = executionTime;
344 targetSince = executionTimeIov;
349 targetSince = endFillTime;
352 getDipData(oms, startSampleTime, endSampleTime);
353 getLumiData(oms, lhcFill, startSampleTime, endSampleTime);
357 edm::LogInfo(
m_name) <<
"First lumi starts at " << flumiStart <<
" last lumi starts at " << flumiStop;
362 getEcalData(session2, startSampleTime, endSampleTime, updateEcal);
387 auto lastAdded =
m_iovs.rbegin()->second;
388 if (lastAdded->fillNumber() != 0) {
393 auto newPayload = std::make_shared<LHCInfoPerFill>();
394 m_iovs.insert(std::make_pair(iov, newPayload));
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");
408 payload.setDelivLumi(delivLumi);
413 unsigned short fillId,
414 const boost::posix_time::ptime& beginFillTime,
415 const boost::posix_time::ptime& endFillTime) {
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 " 427 if (!queryResult.empty()) {
429 auto firstRow = queryResult.front();
433 auto lastRow = queryResult.back();
441 const boost::posix_time::ptime& beginFillTime,
442 const boost::posix_time::ptime& endFillTime) {
445 auto query1 = oms.
query(
"diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam1");
446 query1->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
449 if (query1->execute()) {
450 auto res = query1->result();
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) {
457 unsigned short slot = (vb - 1) / 10 + 1;
458 bunchConfiguration1[slot] =
true;
464 auto query2 = oms.
query(
"diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam2");
465 query2->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
467 if (query2->execute()) {
468 auto res = query2->result();
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) {
475 unsigned short slot = (vb - 1) / 10 + 1;
476 bunchConfiguration2[slot] =
true;
483 auto query3 = oms.
query(
"diplogger/dip/CMS/LHC/LumiPerBunch");
484 query3->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
486 if (query3->execute()) {
487 auto res = query3->result();
489 std::vector<float> lumiPerBX;
490 auto row = *
res.begin();
491 auto lumiBunchInst = row.getArray<
float>(
"lumi_bunch_inst");
492 for (
auto lb : lumiBunchInst) {
494 lumiPerBX.push_back(lb);
503 const boost::posix_time::ptime& beginFillTime,
504 const boost::posix_time::ptime& endFillTime) {
507 coral::ISchema& CTPPS = session.
coralSession().schema(
"CMS_PPS_SPECT_COND");
509 std::unique_ptr<coral::IQuery> CTPPSDataQuery(CTPPS.newQuery());
511 CTPPSDataQuery->addToTableList(
std::string(
"PPS_LHC_MACHINE_PARAMS"));
513 CTPPSDataQuery->addToOutputList(
std::string(
"DIP_UPDATE_TIME"));
514 CTPPSDataQuery->addToOutputList(
std::string(
"LHC_STATE"));
515 CTPPSDataQuery->addToOutputList(
std::string(
"LHC_COMMENT"));
517 CTPPSDataQuery->addToOutputList(
std::string(
"RUN_NUMBER"));
518 CTPPSDataQuery->addToOutputList(
std::string(
"LUMI_SECTION"));
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);
529 CTPPSDataQuery->addToOrderList(
std::string(
"DIP_UPDATE_TIME"));
531 coral::AttributeList CTPPSDataOutput;
532 CTPPSDataOutput.extend<coral::TimeStamp>(
std::string(
"DIP_UPDATE_TIME"));
536 CTPPSDataOutput.extend<
int>(
std::string(
"RUN_NUMBER"));
537 CTPPSDataOutput.extend<
int>(
std::string(
"LUMI_SECTION"));
539 CTPPSDataQuery->defineOutput(CTPPSDataOutput);
541 coral::ICursor& CTPPSDataCursor = CTPPSDataQuery->execute();
543 std::string lhcState =
"", lhcComment =
"", ctppsStatus =
"";
546 unsigned int lumiSection = 0;
549 unsigned int savedLumiSection = 0;
554 while (CTPPSDataCursor.next()) {
556 std::ostringstream CTPPS;
557 CTPPSDataCursor.currentRow().toOutputStream(CTPPS);
559 coral::Attribute
const& dipTimeAttribute = CTPPSDataCursor.currentRow()[
std::string(
"DIP_UPDATE_TIME")];
560 if (!dipTimeAttribute.isNull()) {
562 if (
filter.process(dipTime)) {
564 coral::Attribute
const& lhcStateAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LHC_STATE")];
565 if (!lhcStateAttribute.isNull()) {
568 coral::Attribute
const& lhcCommentAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LHC_COMMENT")];
569 if (!lhcCommentAttribute.isNull()) {
570 lhcComment = lhcCommentAttribute.data<
std::string>();
574 coral::Attribute
const& runNumberAttribute = CTPPSDataCursor.currentRow()[
std::string(
"RUN_NUMBER")];
575 if (!runNumberAttribute.isNull()) {
576 runNumber = runNumberAttribute.data<
int>();
578 coral::Attribute
const& lumiSectionAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LUMI_SECTION")];
579 if (!lumiSectionAttribute.isNull()) {
580 lumiSection = lumiSectionAttribute.data<
int>();
588 payload.setLhcComment(lhcComment);
589 payload.setCtppsStatus(ctppsStatus);
592 savedDipTime = dipTime;
593 savedLumiSection = lumiSection;
602 <<
"DipTime: " << savedDipTime <<
" " 603 <<
"LumiSection: " << savedLumiSection <<
" " 604 <<
"RunNumber: " << savedRunNumber;
610 const boost::posix_time::ptime& lowerTime,
611 const boost::posix_time::ptime& upperTime,
618 std::unique_ptr<coral::IQuery> ECALDataQuery(
ECAL.newQuery());
620 ECALDataQuery->addToTableList(
std::string(
"BEAM_PHASE"));
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"));
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);
633 "(DIP_value LIKE '%beamPhaseMean%' OR DIP_value LIKE '%cavPhaseMean%') AND CHANGE_DATE >= :lowerTime AND " 634 "CHANGE_DATE < :upperTime");
636 ECALDataQuery->setCondition(conditionStr, ECALDataBindVariables);
638 ECALDataQuery->addToOrderList(
std::string(
"CHANGE_DATE"));
639 ECALDataQuery->addToOrderList(
std::string(
"DIP_value"));
640 ECALDataQuery->addToOrderList(
std::string(
"element_nr"));
642 coral::AttributeList ECALDataOutput;
643 ECALDataOutput.extend<coral::TimeStamp>(
std::string(
"CHANGE_DATE"));
645 ECALDataOutput.extend<
unsigned int>(
std::string(
"element_nr"));
646 ECALDataOutput.extend<
float>(
std::string(
"VALUE_NUMBER"));
648 ECALDataQuery->defineOutput(ECALDataOutput);
650 coral::ICursor& ECALDataCursor = ECALDataQuery->execute();
654 unsigned int elementNr = 0;
656 std::set<cond::Time_t> initializedVectors;
667 std::map<cond::Time_t, cond::Time_t> iovMap;
672 while (ECALDataCursor.next()) {
674 std::ostringstream
ECAL;
675 ECALDataCursor.currentRow().toOutputStream(
ECAL);
677 coral::Attribute
const& changeDateAttribute = ECALDataCursor.currentRow()[
std::string(
"CHANGE_DATE")];
678 if (!changeDateAttribute.isNull()) {
680 boost::posix_time::ptime chTime = changeDateAttribute.data<coral::TimeStamp>().
time();
682 if (changeTime == 0) {
687 if (!
update and changeTime == firstTime)
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()) {
694 elementNr = elementNrAttribute.data<
unsigned int>();
695 value = valueNumberAttribute.data<
float>();
698 if (
filter.process(iovTime)) {
699 iovMap.insert(std::make_pair(changeTime,
filter.current()->first));
710 for (
auto& im : iovMap) {
733 std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfoPerFill>>>
m_tmpBuffer;
edm::ErrorSummaryEntry Error
static constexpr unsigned int kLumisectionsQueryLimit
void getNewObjects() override
boost::posix_time::ptime m_startTime
boost::posix_time::ptime m_endTime
std::unique_ptr< OMSServiceQuery > query(const std::string &function) const
T get(const std::string &attributeName)
static const std::pair< const char *, LHCInfoPerFill::FillType > s_fillTypeMap[]
ret
prodAgent to be discontinued
void start(bool readOnly=true)
std::shared_ptr< LHCInfoPerFill > m_prevPayload
T from_string(const std::string &attributeValue)
std::unique_ptr< T > fetchPayload(const cond::Hash &payloadHash)
float const delivLumi() const
float const recLumi() const
Transaction & transaction()
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
LHCInfoPerFill::FillType fillTypeFromString(const std::string &s_fill_type)
cond::TagInfo_t const & tagInfo() const
void setElementData(cond::Time_t since, const std::string &dipVal, unsigned int elementNr, float value, LHCInfoPerFill &payload, std::set< cond::Time_t > &initList)
void addEmptyPayload(cond::Time_t iov)
static size_t const bunchSlots
unsigned long long Time_t
Time_t lumiTime(unsigned int run, unsigned int lumiId)
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()
std::unique_ptr< LHCInfoPerFill > m_fillPayload
Session createSession(const std::string &connectionString, bool writeCapable=false)
std::string m_connectionString
#define DEFINE_FWK_MODULE(type)
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
std::string id() const override
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()
std::unique_ptr< LHCInfoPerFill > Ref
void add(std::map< std::string, TH1 *> &h, TH1 *hist)
LHCInfoPerFillPopConSourceHandler(edm::ParameterSet const &pset)
std::string m_ecalConnectionString
void connect(const std::string &baseUrl)
OMSServiceResultIterator begin() const
size_t getLumiData(const cond::OMSService &oms, unsigned short fillId, const boost::posix_time::ptime &beginFillTime, const boost::posix_time::ptime &endFillTime)
unsigned int m_samplingInterval
void setAuthenticationPath(const std::string &p)
float const instLumiError() const
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)