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" 43 std::shared_ptr<LHCInfoPerLS>& prevPayload,
44 const std::map<pair<cond::Time_t, unsigned int>, pair<cond::Time_t, unsigned int>>& lsIdMap,
47 int lsMissingInPPS = 0;
48 int xAngleBothZero = 0, xAngleBothNonZero = 0, xAngleNegative = 0;
51 std::stringstream condIovs;
52 std::stringstream missingLsList;
57 if (iovsToTransfer.empty()) {
60 LHCInfoPerLS& lastAdded = *iovsToTransfer.rbegin()->second;
67 bool isMissing = lsIdMap.find(
id) != lsIdMap.end() &&
id != lsIdMap.at(
id);
69 missingLsList <<
id.first <<
"_" <<
id.second <<
" ";
70 lsMissingInPPS += isMissing;
72 if (
add && !isMissing) {
75 if (
payload->crossingAngleX() == 0 &&
payload->crossingAngleY() == 0)
77 if (
payload->crossingAngleX() != 0 &&
payload->crossingAngleY() != 0)
79 if (
payload->crossingAngleX() < 0 ||
payload->crossingAngleY() < 0)
85 condIovs <<
since <<
" ";
87 prevPayload = iov.second;
90 unsigned short fillNumber = (!
buffer.empty()) ?
buffer.front().second->fillNumber() : 0;
91 if (lsMissingInPPS > 0) {
93 <<
"Number of stable beam LS in OMS without corresponding record in PPS DB for fill " << fillNumber <<
": " 96 <<
"Stable beam LS in OMS without corresponding record in PPS DB (run_LS): " << missingLsList.str();
98 if (xAngleBothZero > 0) {
100 <<
"Number of payloads written with crossingAngle == 0 for both X and Y for fill " << fillNumber <<
": " 103 if (xAngleBothNonZero > 0) {
105 <<
"Number of payloads written with crossingAngle != 0 for both X and Y for fill " << fillNumber <<
": " 106 << xAngleBothNonZero;
108 if (xAngleNegative > 0) {
110 <<
"Number of payloads written with negative crossingAngle for fill " << fillNumber <<
": " << xAngleNegative;
112 if (betaNegative > 0) {
114 <<
"Number of payloads written with negative betaSta for fill " << fillNumber <<
": " << betaNegative;
117 edm::LogInfo(
"transferPayloads") <<
"TRANSFERED COND IOVS: " << condIovs.str();
130 m_name(
pset.getUntrackedParameter<
std::
string>(
"name",
"LHCInfoPerLSPopConSourceHandler")),
140 boost::posix_time::ptime
now = boost::posix_time::second_clock::local_time();
159 << boost::posix_time::to_iso_extended_string(
161 <<
" ); from " <<
m_name <<
"::getNewObjects";
176 <<
m_name <<
"::getNewObjects";
192 if (!
tagInfo().lastInterval.payloadId.empty()) {
203 query->addOutputVar(
"end_time");
204 query->addOutputVar(
"start_time");
206 bool foundFill =
query->execute();
213 (endTimeStr ==
"null")
216 auto startFillTime = (*
result.begin()).get<boost::posix_time::ptime>(
"start_time");
231 boost::posix_time::ptime executionTime = boost::posix_time::second_clock::local_time();
241 if (nextFillSearchTimestamp >= executionTimeIov) {
247 boost::posix_time::ptime startSampleTime;
248 boost::posix_time::ptime endSampleTime;
258 bool foundFill =
query->execute();
267 edm::LogInfo(
m_name) <<
"Searching new fill after " << boost::posix_time::to_simple_string(nextFillSearchTime);
268 query->filterNotNull(
"start_stable_beam").filterNotNull(
"fill_number");
270 query->filterGE(
"start_time", nextFillSearchTime);
272 query->filterGT(
"start_time", nextFillSearchTime);
277 query->filterNotNull(
"end_time");
278 bool foundFill =
query->execute();
293 endSampleTime = executionTime;
294 nextFillSearchTimestamp = executionTimeIov;
303 getLumiData(oms, lhcFill, startSampleTime, endSampleTime);
308 edm::LogInfo(
m_name) <<
"First buffered lumi starts at " << flumiStart <<
" last lumi starts at " 345 auto lastAdded =
m_iovs.rbegin()->second;
346 if (lastAdded->fillNumber() != 0) {
351 auto newPayload = std::make_shared<LHCInfoPerLS>();
352 m_iovs.insert(make_pair(iov, newPayload));
363 if (!queryResult.
empty()) {
364 auto row = *queryResult.
begin();
365 auto currentFill = row.get<
unsigned short>(
"fill_number");
372 targetPayload = std::make_unique<LHCInfoPerLS>();
373 targetPayload->setFillNumber(currentFill);
380 auto lumiTime = row.
get<boost::posix_time::ptime>(
"start_time");
382 thisLumiSectionInfo->setLumiSection(std::stoul(row.
get<
std::string>(
"lumisection_number")));
383 thisLumiSectionInfo->setRunNumber(std::stoul(row.
get<
std::string>(
"run_number")));
384 m_lsIdMap[make_pair(thisLumiSectionInfo->runNumber(), thisLumiSectionInfo->lumiSection())] = make_pair(-1, -1);
389 make_pair(
cond::time::lumiTime(thisLumiSectionInfo->runNumber(), thisLumiSectionInfo->lumiSection()),
390 thisLumiSectionInfo));
395 for (
auto r : queryResult) {
398 return queryResult.size();
402 for (
auto r : queryResult) {
415 unsigned short fillId,
416 const boost::posix_time::ptime& beginFillTime,
417 const boost::posix_time::ptime& endFillTime) {
419 query->addOutputVars({
"start_time",
"run_number",
"beams_stable",
"lumisection_number"});
420 query->filterEQ(
"fill_number", fillId);
421 query->filterGT(
"start_time", beginFillTime).filterLT(
"start_time", endFillTime);
424 if (
query->execute()) {
425 auto queryResult =
query->result();
428 }
else if (!queryResult.empty()) {
429 auto newestPayload = queryResult.back();
430 if (newestPayload.get<
std::string>(
"beams_stable") ==
"true") {
434 <<
" LS: " << newestPayload.get<
std::string>(
"lumisection_number")
435 <<
" run: " << newestPayload.get<
std::string>(
"run_number");
438 edm::LogInfo(
m_name) <<
"Found " << queryResult.size() <<
" lumisections during the fill " << fillId;
440 edm::LogInfo(
m_name) <<
"OMS query for lumisections of fill " << fillId <<
"failed, status:" <<
query->status();
446 const boost::posix_time::ptime& beginFillTime,
447 const boost::posix_time::ptime& endFillTime) {
450 coral::ISchema& CTPPS = session.
coralSession().schema(
"CMS_PPS_SPECT_COND");
452 std::unique_ptr<coral::IQuery> CTPPSDataQuery(CTPPS.newQuery());
454 CTPPSDataQuery->addToTableList(
std::string(
"PPS_LHC_MACHINE_PARAMS"));
456 CTPPSDataQuery->addToOutputList(
std::string(
"DIP_UPDATE_TIME"));
457 CTPPSDataQuery->addToOutputList(
std::string(
"LUMI_SECTION"));
458 CTPPSDataQuery->addToOutputList(
std::string(
"RUN_NUMBER"));
459 CTPPSDataQuery->addToOutputList(
std::string(
"FILL_NUMBER"));
460 CTPPSDataQuery->addToOutputList(
std::string(
"XING_ANGLE_P5_X_URAD"));
461 CTPPSDataQuery->addToOutputList(
std::string(
"XING_ANGLE_P5_Y_URAD"));
462 CTPPSDataQuery->addToOutputList(
std::string(
"BETA_STAR_P5_X_M"));
463 CTPPSDataQuery->addToOutputList(
std::string(
"BETA_STAR_P5_Y_M"));
465 coral::AttributeList CTPPSDataBindVariables;
466 CTPPSDataBindVariables.extend<coral::TimeStamp>(
std::string(
"beginFillTime"));
467 CTPPSDataBindVariables.extend<coral::TimeStamp>(
std::string(
"endFillTime"));
468 CTPPSDataBindVariables[
std::string(
"beginFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(beginFillTime);
469 CTPPSDataBindVariables[
std::string(
"endFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(endFillTime);
470 std::string conditionStr =
std::string(
"DIP_UPDATE_TIME>= :beginFillTime and DIP_UPDATE_TIME< :endFillTime");
471 CTPPSDataQuery->setCondition(conditionStr, CTPPSDataBindVariables);
473 CTPPSDataQuery->addToOrderList(
std::string(
"DIP_UPDATE_TIME"));
475 coral::AttributeList CTPPSDataOutput;
476 CTPPSDataOutput.extend<coral::TimeStamp>(
std::string(
"DIP_UPDATE_TIME"));
477 CTPPSDataOutput.extend<
int>(
std::string(
"LUMI_SECTION"));
478 CTPPSDataOutput.extend<
int>(
std::string(
"RUN_NUMBER"));
479 CTPPSDataOutput.extend<
int>(
std::string(
"FILL_NUMBER"));
480 CTPPSDataOutput.extend<
float>(
std::string(
"XING_ANGLE_P5_X_URAD"));
481 CTPPSDataOutput.extend<
float>(
std::string(
"XING_ANGLE_P5_Y_URAD"));
482 CTPPSDataOutput.extend<
float>(
std::string(
"BETA_STAR_P5_X_M"));
483 CTPPSDataOutput.extend<
float>(
std::string(
"BETA_STAR_P5_Y_M"));
484 CTPPSDataQuery->defineOutput(CTPPSDataOutput);
486 coral::ICursor& CTPPSDataCursor = CTPPSDataQuery->execute();
487 unsigned int lumiSection = 0;
490 float crossingAngleX = 0., betaStarX = 0.;
491 float crossingAngleY = 0., betaStarY = 0.;
494 int wrongFillNumbers = 0;
495 std::stringstream wrongFills;
496 std::vector<pair<cond::Time_t, std::shared_ptr<LHCInfoPerLS>>>::iterator current =
m_tmpBuffer.begin();
497 while (CTPPSDataCursor.next()) {
499 std::ostringstream CTPPS;
500 CTPPSDataCursor.currentRow().toOutputStream(CTPPS);
502 coral::Attribute
const& dipTimeAttribute = CTPPSDataCursor.currentRow()[
std::string(
"DIP_UPDATE_TIME")];
503 if (!dipTimeAttribute.isNull()) {
505 coral::Attribute
const& lumiSectionAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LUMI_SECTION")];
506 if (!lumiSectionAttribute.isNull()) {
507 lumiSection = lumiSectionAttribute.data<
int>();
509 coral::Attribute
const& runNumberAttribute = CTPPSDataCursor.currentRow()[
std::string(
"RUN_NUMBER")];
510 if (!runNumberAttribute.isNull()) {
511 runNumber = runNumberAttribute.data<
int>();
513 coral::Attribute
const& fillNumberAttribute = CTPPSDataCursor.currentRow()[
std::string(
"FILL_NUMBER")];
514 if (!fillNumberAttribute.isNull()) {
515 fillNumber = fillNumberAttribute.data<
int>();
517 coral::Attribute
const& crossingAngleXAttribute =
518 CTPPSDataCursor.currentRow()[
std::string(
"XING_ANGLE_P5_X_URAD")];
519 if (!crossingAngleXAttribute.isNull()) {
520 crossingAngleX = crossingAngleXAttribute.data<
float>();
522 coral::Attribute
const& crossingAngleYAttribute =
523 CTPPSDataCursor.currentRow()[
std::string(
"XING_ANGLE_P5_Y_URAD")];
524 if (!crossingAngleYAttribute.isNull()) {
525 crossingAngleY = crossingAngleYAttribute.data<
float>();
527 coral::Attribute
const& betaStarXAttribute = CTPPSDataCursor.currentRow()[
std::string(
"BETA_STAR_P5_X_M")];
528 if (!betaStarXAttribute.isNull()) {
529 betaStarX = betaStarXAttribute.data<
float>();
531 coral::Attribute
const& betaStarYAttribute = CTPPSDataCursor.currentRow()[
std::string(
"BETA_STAR_P5_Y_M")];
532 if (!betaStarYAttribute.isNull()) {
533 betaStarY = betaStarYAttribute.data<
float>();
535 if (current !=
m_tmpBuffer.end() && current->second->fillNumber() != fillNumber) {
536 wrongFills <<
"( " <<
runNumber <<
"_" << lumiSection <<
" fill: OMS: " << current->second->fillNumber()
537 <<
" PPSdb: " << fillNumber <<
" ) ";
541 current !=
m_tmpBuffer.end() && make_pair(current->second->runNumber(), current->second->lumiSection()) <=
545 payload.setCrossingAngleX(crossingAngleX);
546 payload.setCrossingAngleY(crossingAngleY);
547 payload.setBetaStarX(betaStarX);
548 payload.setBetaStarY(betaStarY);
549 payload.setLumiSection(lumiSection);
557 if (wrongFillNumbers) {
558 edm::LogWarning(
"getCTPPSData") <<
"Number of records from PPS DB with fillNumber different from OMS: " 560 edm::LogWarning(
"getCTPPSData") <<
"Records from PPS DB with fillNumber different from OMS: " << wrongFills.str();
584 std::vector<pair<cond::Time_t, std::shared_ptr<LHCInfoPerLS>>>
m_tmpBuffer;
588 std::map<pair<cond::Time_t, unsigned int>, pair<cond::Time_t, unsigned int>>
m_lsIdMap;
cond::Time_t m_startFillTime
edm::ErrorSummaryEntry Error
size_t transferPayloads(const std::vector< pair< cond::Time_t, std::shared_ptr< LHCInfoPerLS >>> &buffer, std::map< cond::Time_t, std::shared_ptr< LHCInfoPerLS >> &iovsToTransfer, std::shared_ptr< LHCInfoPerLS > &prevPayload, const std::map< pair< cond::Time_t, unsigned int >, pair< cond::Time_t, unsigned int >> &lsIdMap, cond::Time_t startStableBeamTime, cond::Time_t endStableBeamTime)
bool makeFillPayload(std::unique_ptr< LHCInfoPerLS > &targetPayload, const cond::OMSServiceResult &queryResult)
cond::Time_t runNumber() const
boost::posix_time::ptime m_startTime
void getNewObjects() override
std::unique_ptr< OMSServiceQuery > query(const std::string &function) const
T get(const std::string &attributeName)
std::shared_ptr< LHCInfoPerLS > m_prevPayload
ret
prodAgent to be discontinued
void start(bool readOnly=true)
std::string id() const override
~LHCInfoPerLSPopConSourceHandler() override=default
size_t bufferFirstStableBeamLS(const cond::OMSServiceResult &queryResult)
LHCInfoPerLSPopConSourceHandler(edm::ParameterSet const &pset)
std::unique_ptr< T > fetchPayload(const cond::Hash &payloadHash)
Log< level::Error, false > LogError
Transaction & transaction()
float const betaStarY() const
cond::Time_t m_prevEndFillTime
cond::TagInfo_t const & tagInfo() const
popcon::PopConAnalyzer< LHCInfoPerLSPopConSourceHandler > LHCInfoPerLSPopConAnalyzer
float const crossingAngleX() const
unsigned long long Time_t
Time_t lumiTime(unsigned int run, unsigned int lumiId)
size_t bufferAllLS(const cond::OMSServiceResult &queryResult)
cond::Time_t m_endFillTime
Session createSession(const std::string &connectionString, bool writeCapable=false)
#define DEFINE_FWK_MODULE(type)
std::string m_connectionString
void addPayloadToBuffer(cond::OMSServiceResultRef &row)
void setMessageVerbosity(coral::MsgLevel level)
Time_t from_boost(boost::posix_time::ptime bt)
float const crossingAngleY() const
Log< level::Info, false > LogInfo
std::map< pair< cond::Time_t, unsigned int >, pair< cond::Time_t, unsigned int > > m_lsIdMap
boost::posix_time::ptime m_endTime
cond::persistency::Session & dbSession() const
coral::ISessionProxy & coralSession()
cond::Time_t m_endStableBeamTime
void add(std::map< std::string, TH1 *> &h, TH1 *hist)
std::unique_ptr< LHCInfoPerLS > m_fillPayload
static constexpr unsigned int kLumisectionsQueryLimit
void connect(const std::string &baseUrl)
void addEmptyPayload(cond::Time_t iov)
OMSServiceResultIterator begin() const
Log< level::Warning, false > LogWarning
bool getCTPPSData(cond::persistency::Session &session, const boost::posix_time::ptime &beginFillTime, const boost::posix_time::ptime &endFillTime)
unsigned short const fillNumber() const
float const betaStarX() const
void setAuthenticationPath(const std::string &p)
cond::Time_t m_startStableBeamTime
std::vector< pair< cond::Time_t, std::shared_ptr< LHCInfoPerLS > > > m_tmpBuffer
cond::Time_t getFillLastLumiIOV(const cond::OMSService &oms, unsigned short fillId)
boost::posix_time::ptime to_boost(Time_t iValue)
bool comparePayloads(const LHCInfoPerLS &rhs, const LHCInfoPerLS &lhs)
cond::Time_t m_prevStartFillTime
size_t getLumiData(const cond::OMSService &oms, unsigned short fillId, const boost::posix_time::ptime &beginFillTime, const boost::posix_time::ptime &endFillTime)