9 #include "CoralBase/Attribute.h" 10 #include "CoralBase/AttributeList.h" 11 #include "CoralBase/AttributeSpecification.h" 12 #include "CoralBase/TimeStamp.h" 17 #include "RelationalAccess/ICursor.h" 18 #include "RelationalAccess/IQuery.h" 19 #include "RelationalAccess/ISchema.h" 20 #include "RelationalAccess/ISessionProxy.h" 29 namespace theLHCInfoPerFillPopConImpl {
31 static const std::pair<const char*, LHCInfoPerFill::FillType>
s_fillTypeMap[] = {
46 if (s_fill_type ==
i.first)
53 if (s_particle_type ==
i.first)
63 return from_string_impl<LHCInfoPerFill::FillType, &theLHCInfoPerFillPopConImpl::fillTypeFromString>(
69 return from_string_impl<LHCInfoPerFill::ParticleType, &theLHCInfoPerFillPopConImpl::particleTypeFromString>(
80 if (!queryResult.
empty()) {
81 auto row = *queryResult.
begin();
82 auto currentFill = row.get<
unsigned short>(
"fill_number");
83 auto bunches1 = row.get<
unsigned short>(
"bunches_beam1");
84 auto bunches2 = row.get<
unsigned short>(
"bunches_beam2");
85 auto collidingBunches = row.get<
unsigned short>(
"bunches_colliding");
86 auto targetBunches = row.get<
unsigned short>(
"bunches_target");
90 auto intensityBeam1 = row.get<
float>(
"intensity_beam1");
91 auto intensityBeam2 = row.get<
float>(
"intensity_beam2");
92 auto energy = row.get<
float>(
"energy");
93 auto creationTime = row.get<boost::posix_time::ptime>(
"start_time");
94 auto stableBeamStartTime = row.get<boost::posix_time::ptime>(
"start_stable_beam");
98 auto injectionScheme = row.get<
std::string>(
"injection_scheme");
99 targetPayload = std::make_unique<LHCInfoPerFill>();
100 targetPayload->setFillNumber(currentFill);
101 targetPayload->setBunchesInBeam1(bunches1);
102 targetPayload->setBunchesInBeam2(bunches2);
103 targetPayload->setCollidingBunches(collidingBunches);
104 targetPayload->setTargetBunches(targetBunches);
105 targetPayload->setFillType(fillType);
106 targetPayload->setParticleTypeForBeam1(particleType1);
107 targetPayload->setParticleTypeForBeam2(particleType2);
108 targetPayload->setIntensityForBeam1(intensityBeam1);
109 targetPayload->setIntensityForBeam2(intensityBeam2);
110 targetPayload->setEnergy(
energy);
113 targetPayload->setEndTime(beamDumpTime);
114 targetPayload->setInjectionScheme(injectionScheme);
122 static const std::map<std::string, int>
vecMap = {
123 {
"Beam1/beamPhaseMean", 1}, {
"Beam2/beamPhaseMean", 2}, {
"Beam1/cavPhaseMean", 3}, {
"Beam2/cavPhaseMean", 4}};
126 unsigned int elementNr,
129 std::set<cond::Time_t>& initList) {
130 if (initList.find(
since) == initList.end()) {
135 initList.insert(
since);
139 switch (
vecMap.at(dipVal)) {
172 std::shared_ptr<LHCInfoPerFill>& prevPayload) {
174 std::stringstream condIovs;
175 std::stringstream formattedIovs;
176 for (
auto& iov :
buffer) {
180 if (iovsToTransfer.empty()) {
190 condIovs <<
since <<
" ";
193 prevPayload = iov.second;
196 edm::LogInfo(
"transferPayloads") <<
"TRANSFERED IOVS: " << condIovs.str();
197 edm::LogInfo(
"transferPayloads") <<
"FORMATTED TRANSFERED IOVS: " << formattedIovs.str();
209 m_name(
pset.getUntrackedParameter<
std::
string>(
"name",
"LHCInfoPerFillPopConSourceHandler")),
220 boost::posix_time::ptime
now = boost::posix_time::second_clock::local_time();
239 << boost::posix_time::to_iso_extended_string(
241 <<
" ); from " <<
m_name <<
"::getNewObjects";
256 <<
m_name <<
"::getNewObjects";
273 if (!
tagInfo().lastInterval.payloadId.empty()) {
280 boost::posix_time::ptime executionTime = boost::posix_time::second_clock::local_time();
291 if (nextFillSearchTimestamp >= executionTimeIov) {
297 boost::posix_time::ptime startSampleTime;
298 boost::posix_time::ptime endSampleTime;
304 edm::LogInfo(
m_name) <<
"Searching new fill after " << boost::posix_time::to_simple_string(nextFillSearchTime);
305 query->filterNotNull(
"start_stable_beam").filterNotNull(
"fill_number");
307 query->filterGE(
"start_time", nextFillSearchTime);
309 query->filterGT(
"start_time", nextFillSearchTime);
314 query->filterNotNull(
"end_time");
315 bool foundFill =
query->execute();
327 bool ongoingFill = endFillTime == 0ULL;
331 endSampleTime = executionTime;
332 nextFillSearchTimestamp = executionTimeIov;
337 nextFillSearchTimestamp = endFillTime;
340 getDipData(oms, startSampleTime, endSampleTime);
341 getLumiData(oms, lhcFill, startSampleTime, endSampleTime);
345 edm::LogInfo(
m_name) <<
"First lumi starts at " << flumiStart <<
" last lumi starts at " << flumiStop;
350 getEcalData(session2, startSampleTime, endSampleTime);
386 auto lastAdded =
m_iovs.rbegin()->second;
387 if (lastAdded->fillNumber() != 0) {
392 auto newPayload = std::make_shared<LHCInfoPerFill>();
393 m_iovs.insert(std::make_pair(iov, newPayload));
402 auto startTime = row.
get<boost::posix_time::ptime>(
"start_time");
403 auto delivLumi = row.
get<
float>(
"delivered_lumi");
404 auto recLumi = row.
get<
float>(
"recorded_lumi");
406 auto lsNumber = std::stoul(row.
get<
std::string>(
"lumisection_number"));
415 payload.setDelivLumi(delivLumi);
422 if (timestampToLumiid.find(
item.first) == timestampToLumiid.end()) {
424 <<
"Can't find corresponding lumiid IOV for timestamp " <<
item.first <<
"\n";
427 item.first = timestampToLumiid.at(
item.first);
432 unsigned short fillId,
433 const boost::posix_time::ptime& beginFillTime,
434 const boost::posix_time::ptime& endFillTime) {
436 query->addOutputVars(
437 {
"start_time",
"delivered_lumi",
"recorded_lumi",
"beams_stable",
"run_number",
"lumisection_number"});
438 query->filterEQ(
"fill_number", fillId);
439 query->filterGT(
"start_time", beginFillTime).filterLT(
"start_time", endFillTime);
440 query->filterEQ(
"beams_stable",
"true");
442 if (
query->execute()) {
443 auto queryResult =
query->result();
444 edm::LogInfo(
m_name) <<
"Found " << queryResult.size() <<
" lumisections with STABLE BEAM during the fill " 447 if (!queryResult.empty()) {
449 auto firstRow = queryResult.front();
453 auto lastRow = queryResult.back();
461 const boost::posix_time::ptime& beginFillTime,
462 const boost::posix_time::ptime& endFillTime) {
465 auto query1 = oms.
query(
"diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam1");
466 query1->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
469 if (query1->execute()) {
470 auto res = query1->result();
472 std::bitset<LHCInfoPerFill::bunchSlots + 1> bunchConfiguration1(0ULL);
473 auto row = *
res.begin();
474 auto vbunchConf1 = row.getArray<
unsigned short>(
"value");
475 for (
auto vb : vbunchConf1) {
477 unsigned short slot = (vb - 1) / 10 + 1;
478 bunchConfiguration1[
slot] =
true;
484 auto query2 = oms.
query(
"diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam2");
485 query2->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
487 if (query2->execute()) {
488 auto res = query2->result();
490 std::bitset<LHCInfoPerFill::bunchSlots + 1> bunchConfiguration2(0ULL);
491 auto row = *
res.begin();
492 auto vbunchConf2 = row.getArray<
unsigned short>(
"value");
493 for (
auto vb : vbunchConf2) {
495 unsigned short slot = (vb - 1) / 10 + 1;
496 bunchConfiguration2[
slot] =
true;
503 auto query3 = oms.
query(
"diplogger/dip/CMS/LHC/LumiPerBunch");
504 query3->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
506 if (query3->execute()) {
507 auto res = query3->result();
509 std::vector<float> lumiPerBX;
510 auto row = *
res.begin();
511 auto lumiBunchInst = row.getArray<
float>(
"lumi_bunch_inst");
512 for (
auto lb : lumiBunchInst) {
514 lumiPerBX.push_back(lb);
523 const boost::posix_time::ptime& beginFillTime,
524 const boost::posix_time::ptime& endFillTime) {
527 coral::ISchema& CTPPS = session.
coralSession().schema(
"CMS_PPS_SPECT_COND");
529 std::unique_ptr<coral::IQuery> CTPPSDataQuery(CTPPS.newQuery());
531 CTPPSDataQuery->addToTableList(
std::string(
"PPS_LHC_MACHINE_PARAMS"));
533 CTPPSDataQuery->addToOutputList(
std::string(
"DIP_UPDATE_TIME"));
534 CTPPSDataQuery->addToOutputList(
std::string(
"LHC_STATE"));
535 CTPPSDataQuery->addToOutputList(
std::string(
"LHC_COMMENT"));
537 CTPPSDataQuery->addToOutputList(
std::string(
"RUN_NUMBER"));
538 CTPPSDataQuery->addToOutputList(
std::string(
"LUMI_SECTION"));
541 coral::AttributeList CTPPSDataBindVariables;
542 CTPPSDataBindVariables.extend<coral::TimeStamp>(
std::string(
"beginFillTime"));
543 CTPPSDataBindVariables.extend<coral::TimeStamp>(
std::string(
"endFillTime"));
544 CTPPSDataBindVariables[
std::string(
"beginFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(beginFillTime);
545 CTPPSDataBindVariables[
std::string(
"endFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(endFillTime);
546 std::string conditionStr =
std::string(
"DIP_UPDATE_TIME>= :beginFillTime and DIP_UPDATE_TIME< :endFillTime");
547 CTPPSDataQuery->setCondition(conditionStr, CTPPSDataBindVariables);
549 CTPPSDataQuery->addToOrderList(
std::string(
"DIP_UPDATE_TIME"));
551 coral::AttributeList CTPPSDataOutput;
552 CTPPSDataOutput.extend<coral::TimeStamp>(
std::string(
"DIP_UPDATE_TIME"));
556 CTPPSDataOutput.extend<
int>(
std::string(
"RUN_NUMBER"));
557 CTPPSDataOutput.extend<
int>(
std::string(
"LUMI_SECTION"));
559 CTPPSDataQuery->defineOutput(CTPPSDataOutput);
561 coral::ICursor& CTPPSDataCursor = CTPPSDataQuery->execute();
563 std::string lhcState =
"", lhcComment =
"", ctppsStatus =
"";
566 unsigned int lumiSection = 0;
569 unsigned int savedLumiSection = 0;
574 while (CTPPSDataCursor.next()) {
576 std::ostringstream CTPPS;
577 CTPPSDataCursor.currentRow().toOutputStream(CTPPS);
579 coral::Attribute
const& dipTimeAttribute = CTPPSDataCursor.currentRow()[
std::string(
"DIP_UPDATE_TIME")];
580 if (!dipTimeAttribute.isNull()) {
582 if (
filter.process(dipTime)) {
584 coral::Attribute
const& lhcStateAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LHC_STATE")];
585 if (!lhcStateAttribute.isNull()) {
588 coral::Attribute
const& lhcCommentAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LHC_COMMENT")];
589 if (!lhcCommentAttribute.isNull()) {
590 lhcComment = lhcCommentAttribute.data<
std::string>();
594 coral::Attribute
const& runNumberAttribute = CTPPSDataCursor.currentRow()[
std::string(
"RUN_NUMBER")];
595 if (!runNumberAttribute.isNull()) {
596 runNumber = runNumberAttribute.data<
int>();
598 coral::Attribute
const& lumiSectionAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LUMI_SECTION")];
599 if (!lumiSectionAttribute.isNull()) {
600 lumiSection = lumiSectionAttribute.data<
int>();
608 payload.setLhcComment(lhcComment);
609 payload.setCtppsStatus(ctppsStatus);
612 savedDipTime = dipTime;
613 savedLumiSection = lumiSection;
622 <<
"DipTime: " << savedDipTime <<
" " 623 <<
"LumiSection: " << savedLumiSection <<
" " 624 <<
"RunNumber: " << savedRunNumber;
630 const boost::posix_time::ptime& lowerTime,
631 const boost::posix_time::ptime& upperTime) {
637 std::unique_ptr<coral::IQuery> ECALDataQuery(
ECAL.newQuery());
639 ECALDataQuery->addToTableList(
std::string(
"BEAM_PHASE"));
641 ECALDataQuery->addToOutputList(
std::string(
"CHANGE_DATE"));
642 ECALDataQuery->addToOutputList(
std::string(
"DIP_value"));
643 ECALDataQuery->addToOutputList(
std::string(
"element_nr"));
644 ECALDataQuery->addToOutputList(
std::string(
"VALUE_NUMBER"));
646 coral::AttributeList ECALDataBindVariables;
647 ECALDataBindVariables.extend<coral::TimeStamp>(
std::string(
"lowerTime"));
648 ECALDataBindVariables.extend<coral::TimeStamp>(
std::string(
"upperTime"));
649 ECALDataBindVariables[
std::string(
"lowerTime")].data<coral::TimeStamp>() = coral::TimeStamp(lowerTime);
650 ECALDataBindVariables[
std::string(
"upperTime")].data<coral::TimeStamp>() = coral::TimeStamp(upperTime);
652 "(DIP_value LIKE '%beamPhaseMean%' OR DIP_value LIKE '%cavPhaseMean%') AND CHANGE_DATE >= :lowerTime AND " 653 "CHANGE_DATE < :upperTime");
655 ECALDataQuery->setCondition(conditionStr, ECALDataBindVariables);
657 ECALDataQuery->addToOrderList(
std::string(
"CHANGE_DATE"));
658 ECALDataQuery->addToOrderList(
std::string(
"DIP_value"));
659 ECALDataQuery->addToOrderList(
std::string(
"element_nr"));
661 coral::AttributeList ECALDataOutput;
662 ECALDataOutput.extend<coral::TimeStamp>(
std::string(
"CHANGE_DATE"));
664 ECALDataOutput.extend<
unsigned int>(
std::string(
"element_nr"));
665 ECALDataOutput.extend<
float>(
std::string(
"VALUE_NUMBER"));
667 ECALDataQuery->defineOutput(ECALDataOutput);
669 coral::ICursor& ECALDataCursor = ECALDataQuery->execute();
673 unsigned int elementNr = 0;
675 std::set<cond::Time_t> initializedVectors;
686 std::map<cond::Time_t, cond::Time_t> iovMap;
691 while (ECALDataCursor.next()) {
693 std::ostringstream
ECAL;
694 ECALDataCursor.currentRow().toOutputStream(
ECAL);
696 coral::Attribute
const& changeDateAttribute = ECALDataCursor.currentRow()[
std::string(
"CHANGE_DATE")];
697 if (!changeDateAttribute.isNull()) {
699 boost::posix_time::ptime chTime = changeDateAttribute.data<coral::TimeStamp>().
time();
701 if (changeTime == 0) {
706 if (changeTime == firstTime)
708 coral::Attribute
const& dipValAttribute = ECALDataCursor.currentRow()[
std::string(
"DIP_value")];
709 coral::Attribute
const& valueNumberAttribute = ECALDataCursor.currentRow()[
std::string(
"VALUE_NUMBER")];
710 coral::Attribute
const& elementNrAttribute = ECALDataCursor.currentRow()[
std::string(
"element_nr")];
711 if (!dipValAttribute.isNull() and !valueNumberAttribute.isNull()) {
713 elementNr = elementNrAttribute.data<
unsigned int>();
714 value = valueNumberAttribute.data<
float>();
717 if (
filter.process(iovTime)) {
718 iovMap.insert(std::make_pair(changeTime,
filter.current()->first));
728 for (
auto& im : iovMap) {
749 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)
bool getEcalData(cond::persistency::Session &session, const boost::posix_time::ptime &lowerTime, const boost::posix_time::ptime &upperTime)
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
Log< level::Info, false > LogInfo
void convertBufferedIovsToLumiid(std::map< cond::Time_t, cond::Time_t > timestampToLumiid)
std::string const & lhcComment() const
unsigned short const fillNumber() const
float const instLumi() const
cond::persistency::Session & dbSession() const
coral::ISessionProxy & coralSession()
void add(std::map< std::string, TH1 *> &h, TH1 *hist)
LHCInfoPerFillPopConSourceHandler(edm::ParameterSet const &pset)
std::string m_ecalConnectionString
std::map< cond::Time_t, cond::Time_t > m_timestampToLumiid
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)
void setAuthenticationPath(const std::string &p)
float const instLumiError() const
cond::Time_t getFillLastLumiIOV(const cond::OMSService &oms, unsigned short fillId)
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)