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");
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);
118 targetPayload->setInjectionScheme(injectionScheme);
126 static const std::map<std::string, int>
vecMap = {
127 {
"Beam1/beamPhaseMean", 1}, {
"Beam2/beamPhaseMean", 2}, {
"Beam1/cavPhaseMean", 3}, {
"Beam2/cavPhaseMean", 4}};
130 unsigned int elementNr,
133 std::set<cond::Time_t>& initList) {
134 if (initList.find(
since) == initList.end()) {
139 initList.insert(
since);
143 switch (
vecMap.at(dipVal)) {
176 std::shared_ptr<LHCInfoPerFill>& prevPayload) {
178 std::stringstream condIovs;
179 std::stringstream formattedIovs;
180 for (
auto& iov :
buffer) {
184 if (iovsToTransfer.empty()) {
194 condIovs <<
since <<
" ";
197 prevPayload = iov.second;
200 edm::LogInfo(
"transferPayloads") <<
"TRANSFERED IOVS: " << condIovs.str();
201 edm::LogInfo(
"transferPayloads") <<
"FORMATTED TRANSFERED IOVS: " << formattedIovs.str();
214 m_name(
pset.getUntrackedParameter<
std::
string>(
"name",
"LHCInfoPerFillPopConSourceHandler")),
226 boost::posix_time::ptime
now = boost::posix_time::second_clock::local_time();
248 << boost::posix_time::to_iso_extended_string(
250 <<
" ); from " <<
m_name <<
"::getNewObjects";
260 <<
m_name <<
"::getNewObjects";
263 boost::posix_time::ptime executionTime = boost::posix_time::second_clock::local_time();
269 if (lastSince > targetSince)
270 targetSince = lastSince;
289 if (!
tagInfo().lastInterval.payloadId.empty()) {
298 if (targetSince >= executionTimeIov) {
303 bool updateEcal =
false;
305 boost::posix_time::ptime startSampleTime;
306 boost::posix_time::ptime endSampleTime;
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");
315 query->filterGE(
"start_time", targetTime);
317 query->filterGT(
"start_time", targetTime);
322 query->filterNotNull(
"end_time");
323 bool foundFill =
query->execute();
337 bool ongoingFill = endFillTime == 0ULL;
341 endSampleTime = executionTime;
342 targetSince = executionTimeIov;
347 targetSince = endFillTime;
350 getDipData(oms, startSampleTime, endSampleTime);
351 getLumiData(oms, lhcFill, startSampleTime, endSampleTime);
355 edm::LogInfo(
m_name) <<
"First lumi starts at " << flumiStart <<
" last lumi starts at " << flumiStop;
360 getEcalData(session2, startSampleTime, endSampleTime, updateEcal);
385 auto lastAdded =
m_iovs.rbegin()->second;
386 if (lastAdded->fillNumber() != 0) {
391 auto newPayload = std::make_shared<LHCInfoPerFill>();
392 m_iovs.insert(std::make_pair(iov, newPayload));
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");
406 payload.setDelivLumi(delivLumi);
411 unsigned short fillId,
412 const boost::posix_time::ptime& beginFillTime,
413 const boost::posix_time::ptime& endFillTime) {
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 " 425 if (!queryResult.empty()) {
427 auto firstRow = queryResult.front();
431 auto lastRow = queryResult.back();
439 const boost::posix_time::ptime& beginFillTime,
440 const boost::posix_time::ptime& endFillTime) {
443 auto query1 = oms.
query(
"diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam1");
444 query1->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
447 if (query1->execute()) {
448 auto res = query1->result();
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) {
455 unsigned short slot = (vb - 1) / 10 + 1;
456 bunchConfiguration1[slot] =
true;
462 auto query2 = oms.
query(
"diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam2");
463 query2->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
465 if (query2->execute()) {
466 auto res = query2->result();
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) {
473 unsigned short slot = (vb - 1) / 10 + 1;
474 bunchConfiguration2[slot] =
true;
481 auto query3 = oms.
query(
"diplogger/dip/CMS/LHC/LumiPerBunch");
482 query3->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
484 if (query3->execute()) {
485 auto res = query3->result();
487 std::vector<float> lumiPerBX;
488 auto row = *
res.begin();
489 auto lumiBunchInst = row.getArray<
float>(
"lumi_bunch_inst");
490 for (
auto lb : lumiBunchInst) {
492 lumiPerBX.push_back(lb);
501 const boost::posix_time::ptime& beginFillTime,
502 const boost::posix_time::ptime& endFillTime) {
505 coral::ISchema& CTPPS = session.
coralSession().schema(
"CMS_PPS_SPECT_COND");
507 std::unique_ptr<coral::IQuery> CTPPSDataQuery(CTPPS.newQuery());
509 CTPPSDataQuery->addToTableList(
std::string(
"PPS_LHC_MACHINE_PARAMS"));
511 CTPPSDataQuery->addToOutputList(
std::string(
"DIP_UPDATE_TIME"));
512 CTPPSDataQuery->addToOutputList(
std::string(
"LHC_STATE"));
513 CTPPSDataQuery->addToOutputList(
std::string(
"LHC_COMMENT"));
515 CTPPSDataQuery->addToOutputList(
std::string(
"RUN_NUMBER"));
516 CTPPSDataQuery->addToOutputList(
std::string(
"LUMI_SECTION"));
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);
527 CTPPSDataQuery->addToOrderList(
std::string(
"DIP_UPDATE_TIME"));
529 coral::AttributeList CTPPSDataOutput;
530 CTPPSDataOutput.extend<coral::TimeStamp>(
std::string(
"DIP_UPDATE_TIME"));
534 CTPPSDataOutput.extend<
int>(
std::string(
"RUN_NUMBER"));
535 CTPPSDataOutput.extend<
int>(
std::string(
"LUMI_SECTION"));
537 CTPPSDataQuery->defineOutput(CTPPSDataOutput);
539 coral::ICursor& CTPPSDataCursor = CTPPSDataQuery->execute();
541 std::string lhcState =
"", lhcComment =
"", ctppsStatus =
"";
544 unsigned int lumiSection = 0;
547 unsigned int savedLumiSection = 0;
552 while (CTPPSDataCursor.next()) {
554 std::ostringstream CTPPS;
555 CTPPSDataCursor.currentRow().toOutputStream(CTPPS);
557 coral::Attribute
const& dipTimeAttribute = CTPPSDataCursor.currentRow()[
std::string(
"DIP_UPDATE_TIME")];
558 if (!dipTimeAttribute.isNull()) {
560 if (
filter.process(dipTime)) {
562 coral::Attribute
const& lhcStateAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LHC_STATE")];
563 if (!lhcStateAttribute.isNull()) {
566 coral::Attribute
const& lhcCommentAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LHC_COMMENT")];
567 if (!lhcCommentAttribute.isNull()) {
568 lhcComment = lhcCommentAttribute.data<
std::string>();
572 coral::Attribute
const& runNumberAttribute = CTPPSDataCursor.currentRow()[
std::string(
"RUN_NUMBER")];
573 if (!runNumberAttribute.isNull()) {
574 runNumber = runNumberAttribute.data<
int>();
576 coral::Attribute
const& lumiSectionAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LUMI_SECTION")];
577 if (!lumiSectionAttribute.isNull()) {
578 lumiSection = lumiSectionAttribute.data<
int>();
586 payload.setLhcComment(lhcComment);
587 payload.setCtppsStatus(ctppsStatus);
590 savedDipTime = dipTime;
591 savedLumiSection = lumiSection;
600 <<
"DipTime: " << savedDipTime <<
" " 601 <<
"LumiSection: " << savedLumiSection <<
" " 602 <<
"RunNumber: " << savedRunNumber;
608 const boost::posix_time::ptime& lowerTime,
609 const boost::posix_time::ptime& upperTime,
616 std::unique_ptr<coral::IQuery> ECALDataQuery(
ECAL.newQuery());
618 ECALDataQuery->addToTableList(
std::string(
"BEAM_PHASE"));
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"));
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);
631 "(DIP_value LIKE '%beamPhaseMean%' OR DIP_value LIKE '%cavPhaseMean%') AND CHANGE_DATE >= :lowerTime AND " 632 "CHANGE_DATE < :upperTime");
634 ECALDataQuery->setCondition(conditionStr, ECALDataBindVariables);
636 ECALDataQuery->addToOrderList(
std::string(
"CHANGE_DATE"));
637 ECALDataQuery->addToOrderList(
std::string(
"DIP_value"));
638 ECALDataQuery->addToOrderList(
std::string(
"element_nr"));
640 coral::AttributeList ECALDataOutput;
641 ECALDataOutput.extend<coral::TimeStamp>(
std::string(
"CHANGE_DATE"));
643 ECALDataOutput.extend<
unsigned int>(
std::string(
"element_nr"));
644 ECALDataOutput.extend<
float>(
std::string(
"VALUE_NUMBER"));
646 ECALDataQuery->defineOutput(ECALDataOutput);
648 coral::ICursor& ECALDataCursor = ECALDataQuery->execute();
652 unsigned int elementNr = 0;
654 std::set<cond::Time_t> initializedVectors;
665 std::map<cond::Time_t, cond::Time_t> iovMap;
670 while (ECALDataCursor.next()) {
672 std::ostringstream
ECAL;
673 ECALDataCursor.currentRow().toOutputStream(
ECAL);
675 coral::Attribute
const& changeDateAttribute = ECALDataCursor.currentRow()[
std::string(
"CHANGE_DATE")];
676 if (!changeDateAttribute.isNull()) {
678 boost::posix_time::ptime chTime = changeDateAttribute.data<coral::TimeStamp>().
time();
680 if (changeTime == 0) {
685 if (!
update and changeTime == firstTime)
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()) {
692 elementNr = elementNrAttribute.data<
unsigned int>();
693 value = valueNumberAttribute.data<
float>();
696 if (
filter.process(iovTime)) {
697 iovMap.insert(std::make_pair(changeTime,
filter.current()->first));
708 for (
auto& im : iovMap) {
731 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 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)
bool getCTTPSData(cond::persistency::Session &session, const boost::posix_time::ptime &beginFillTime, const boost::posix_time::ptime &endFillTime)