7 #include "CoralBase/Attribute.h" 8 #include "CoralBase/AttributeList.h" 9 #include "CoralBase/AttributeSpecification.h" 10 #include "CoralBase/TimeStamp.h" 13 #include "RelationalAccess/ICursor.h" 14 #include "RelationalAccess/IQuery.h" 15 #include "RelationalAccess/ISchema.h" 16 #include "RelationalAccess/ISessionProxy.h" 17 #include <boost/date_time/posix_time/posix_time.hpp> 35 std::shared_ptr<LHCInfoPerLS>& prevPayload,
36 const std::map<pair<cond::Time_t, unsigned int>, pair<cond::Time_t, unsigned int>>& lsIdMap,
39 int lsMissingInPPS = 0;
40 int xAngleBothZero = 0, xAngleBothNonZero = 0, xAngleNegative = 0;
43 std::stringstream condIovs;
44 std::stringstream missingLsList;
49 if (iovsToTransfer.empty()) {
52 LHCInfoPerLS& lastAdded = *iovsToTransfer.rbegin()->second;
59 bool isMissing = lsIdMap.find(
id) != lsIdMap.end() &&
id != lsIdMap.at(
id);
61 missingLsList <<
id.first <<
"_" <<
id.second <<
" ";
62 lsMissingInPPS += isMissing;
64 if (
add && !isMissing) {
67 if (
payload->crossingAngleX() == 0 &&
payload->crossingAngleY() == 0)
69 if (
payload->crossingAngleX() != 0 &&
payload->crossingAngleY() != 0)
71 if (
payload->crossingAngleX() < 0 ||
payload->crossingAngleY() < 0)
77 condIovs <<
since <<
" ";
79 prevPayload = iov.second;
82 unsigned short fillNumber = (!
buffer.empty()) ?
buffer.front().second->fillNumber() : 0;
83 if (lsMissingInPPS > 0) {
85 <<
"Number of stable beam LS in OMS without corresponding record in PPS DB for fill " << fillNumber <<
": " 88 <<
"Stable beam LS in OMS without corresponding record in PPS DB (run_LS): " << missingLsList.str();
90 if (xAngleBothZero > 0) {
92 <<
"Number of payloads written with crossingAngle == 0 for both X and Y for fill " << fillNumber <<
": " 95 if (xAngleBothNonZero > 0) {
97 <<
"Number of payloads written with crossingAngle != 0 for both X and Y for fill " << fillNumber <<
": " 100 if (xAngleNegative > 0) {
102 <<
"Number of payloads written with negative crossingAngle for fill " << fillNumber <<
": " << xAngleNegative;
104 if (betaNegative > 0) {
106 <<
"Number of payloads written with negative betaSta for fill " << fillNumber <<
": " << betaNegative;
109 edm::LogInfo(
"transferPayloads") <<
"TRANSFERED COND IOVS: " << condIovs.str();
119 m_endFillMode(
pset.getUntrackedParameter<
bool>(
"endFill",
true)),
120 m_name(
pset.getUntrackedParameter<
std::
string>(
"name",
"LHCInfoPerLSPopConSourceHandler")),
121 m_connectionString(
pset.getUntrackedParameter<
std::
string>(
"connectionString",
"")),
122 m_authpath(
pset.getUntrackedParameter<
std::
string>(
"authenticationPath",
"")),
123 m_omsBaseUrl(
pset.getUntrackedParameter<
std::
string>(
"omsBaseUrl",
"")),
124 m_debugLogic(
pset.getUntrackedParameter<
bool>(
"debugLogic",
false)),
125 m_defaultCrossingAngleX(
pset.getUntrackedParameter<double>(
"defaultCrossingAngleX", 0)),
126 m_defaultCrossingAngleY(
pset.getUntrackedParameter<double>(
"defaultCrossingAngleY", 0)),
127 m_defaultBetaStarX(
pset.getUntrackedParameter<double>(
"defaultBetaStarX", 0)),
128 m_defaultBetaStarY(
pset.getUntrackedParameter<double>(
"defaultBetaStarY", 0)),
129 m_minBetaStar(
pset.getUntrackedParameter<double>(
"minBetaStar", 0.1)),
130 m_maxBetaStar(
pset.getUntrackedParameter<double>(
"maxBetaStar", 100.)),
131 m_minCrossingAngle(
pset.getUntrackedParameter<double>(
"minCrossingAngle", 10.)),
132 m_maxCrossingAngle(
pset.getUntrackedParameter<double>(
"maxCrossingAngle", 500.)),
139 boost::posix_time::ptime
now = boost::posix_time::second_clock::local_time();
147 throw cms::Exception(
"invalid argument") <<
"debugLogic == true not supported for endFillMode == true";
165 std::stringstream payloadData;
166 payloadData <<
"LS = " <<
it->second->lumiSection() <<
", run = " <<
it->second->runNumber() <<
", " 167 <<
"xAngleX = " <<
it->second->crossingAngleX() <<
" urad, " 168 <<
"xAngleY = " <<
it->second->crossingAngleY() <<
" urad, " 169 <<
"beta*X = " <<
it->second->betaStarX() <<
" m, " 170 <<
"beta*Y = " <<
it->second->betaStarY() <<
" m";
182 if ((
payload.crossingAngleX() == 0. &&
payload.crossingAngleY() == 0.) ||
183 (
payload.crossingAngleX() != 0. &&
payload.crossingAngleY() != 0.))
185 auto non0CrossingAngle =
payload.crossingAngleX() != 0. ?
payload.crossingAngleX() :
payload.crossingAngleY();
201 << boost::posix_time::to_iso_extended_string(
203 <<
" ); from " <<
m_name <<
"::getNewObjects";
213 edm::LogInfo(
m_name) <<
"Empty or new tag: uploading a default payload and ending the job";
221 <<
m_name <<
"::getNewObjects";
237 if (!
tagInfo().lastInterval.payloadId.empty()) {
248 query->addOutputVar(
"end_time");
249 query->addOutputVar(
"start_time");
251 bool foundFill =
query->execute();
260 auto startFillTime = (*
result.begin()).get<boost::posix_time::ptime>(
"start_time");
275 boost::posix_time::ptime executionTime = boost::posix_time::second_clock::local_time();
285 if (nextFillSearchTimestamp >= executionTimeIov) {
291 boost::posix_time::ptime startSampleTime;
292 boost::posix_time::ptime endSampleTime;
305 bool foundFill =
query->execute();
313 edm::LogInfo(
m_name) <<
"Searching new fill after " << boost::posix_time::to_simple_string(nextFillSearchTime);
314 query->filterNotNull(
"start_stable_beam").filterNotNull(
"fill_number");
316 query->filterGE(
"start_time", nextFillSearchTime);
318 query->filterGT(
"start_time", nextFillSearchTime);
323 query->filterNotNull(
"end_time");
327 bool querySuccess =
query->execute();
350 endSampleTime = executionTime;
351 nextFillSearchTimestamp = executionTimeIov;
360 getLumiData(oms, lhcFill, startSampleTime, endSampleTime);
365 edm::LogInfo(
m_name) <<
"First buffered lumi starts at " << flumiStart <<
" last lumi starts at " << flumiStop;
375 <<
"More than 1 payload buffered for writing in duringFill mode.\ 376 In this mode only up to 1 payload can be written";
381 <<
"The buffered payload has the same data as the previous payload in the tag. It will not be written.";
420 auto lastAdded =
m_iovs.rbegin()->second;
421 if (lastAdded->fillNumber() != 0) {
426 auto newPayload = std::make_shared<LHCInfoPerLS>();
427 m_iovs.insert(make_pair(iov, newPayload));
439 auto defaultPayload = std::make_shared<LHCInfoPerLS>();
440 defaultPayload->setFillNumber(
fill);
448 unsigned short lumi) {
449 auto defaultPayload = std::make_shared<LHCInfoPerLS>();
450 defaultPayload->setFillNumber(
fill);
451 defaultPayload->setRunNumber(
run);
452 defaultPayload->setLumiSection(
lumi);
460 <<
"The default payload has the same data as the previous payload in the tag. It will not be written.";
462 m_iovs.insert(make_pair(iov, defaultPayload));
470 if (!queryResult.
empty()) {
471 auto row = *queryResult.
begin();
472 auto currentFill = row.get<
unsigned short>(
"fill_number");
483 targetPayload = std::make_unique<LHCInfoPerLS>();
484 targetPayload->setFillNumber(currentFill);
491 auto lumiTime = row.
get<boost::posix_time::ptime>(
"start_time");
493 thisLumiSectionInfo->setLumiSection(std::stoul(row.
get<
std::string>(
"lumisection_number")));
494 thisLumiSectionInfo->setRunNumber(std::stoul(row.
get<
std::string>(
"run_number")));
495 m_lsIdMap[make_pair(thisLumiSectionInfo->runNumber(), thisLumiSectionInfo->lumiSection())] = make_pair(-1, -1);
500 make_pair(
cond::time::lumiTime(thisLumiSectionInfo->runNumber(), thisLumiSectionInfo->lumiSection()),
501 thisLumiSectionInfo));
506 for (
auto r : queryResult) {
509 return queryResult.size();
513 unsigned short fillId,
514 const boost::posix_time::ptime& beginFillTime,
515 const boost::posix_time::ptime& endFillTime) {
517 query->addOutputVars({
"start_time",
"run_number",
"beams_stable",
"lumisection_number"});
518 query->filterEQ(
"fill_number", fillId);
519 query->filterGT(
"start_time", beginFillTime).filterLT(
"start_time", endFillTime);
522 if (
query->execute()) {
523 auto queryResult =
query->result();
526 }
else if (!queryResult.empty()) {
527 auto newestPayload = queryResult.back();
532 <<
" LS: " << newestPayload.get<
std::string>(
"lumisection_number")
533 <<
" run: " << newestPayload.get<
std::string>(
"run_number");
536 edm::LogInfo(
m_name) <<
"Found " << queryResult.size() <<
" lumisections during the fill " << fillId;
538 edm::LogInfo(
m_name) <<
"OMS query for lumisections of fill " << fillId <<
"failed, status:" <<
query->status();
543 const boost::posix_time::ptime& beginFillTime,
544 const boost::posix_time::ptime& endFillTime) {
547 coral::ISchema& CTPPS = session.
coralSession().schema(
"CMS_PPS_SPECT_COND");
549 std::unique_ptr<coral::IQuery> CTPPSDataQuery(CTPPS.newQuery());
551 CTPPSDataQuery->addToTableList(
std::string(
"PPS_LHC_MACHINE_PARAMS"));
553 CTPPSDataQuery->addToOutputList(
std::string(
"DIP_UPDATE_TIME"));
554 CTPPSDataQuery->addToOutputList(
std::string(
"LUMI_SECTION"));
555 CTPPSDataQuery->addToOutputList(
std::string(
"RUN_NUMBER"));
556 CTPPSDataQuery->addToOutputList(
std::string(
"FILL_NUMBER"));
557 CTPPSDataQuery->addToOutputList(
std::string(
"XING_ANGLE_P5_X_URAD"));
558 CTPPSDataQuery->addToOutputList(
std::string(
"XING_ANGLE_P5_Y_URAD"));
559 CTPPSDataQuery->addToOutputList(
std::string(
"BETA_STAR_P5_X_M"));
560 CTPPSDataQuery->addToOutputList(
std::string(
"BETA_STAR_P5_Y_M"));
562 coral::AttributeList CTPPSDataBindVariables;
563 CTPPSDataBindVariables.extend<coral::TimeStamp>(
std::string(
"beginFillTime"));
564 CTPPSDataBindVariables.extend<coral::TimeStamp>(
std::string(
"endFillTime"));
565 CTPPSDataBindVariables[
std::string(
"beginFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(beginFillTime);
566 CTPPSDataBindVariables[
std::string(
"endFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(endFillTime);
567 std::string conditionStr =
std::string(
"DIP_UPDATE_TIME>= :beginFillTime and DIP_UPDATE_TIME< :endFillTime");
568 CTPPSDataQuery->setCondition(conditionStr, CTPPSDataBindVariables);
570 CTPPSDataQuery->addToOrderList(
std::string(
"DIP_UPDATE_TIME"));
572 coral::AttributeList CTPPSDataOutput;
573 CTPPSDataOutput.extend<coral::TimeStamp>(
std::string(
"DIP_UPDATE_TIME"));
574 CTPPSDataOutput.extend<
int>(
std::string(
"LUMI_SECTION"));
575 CTPPSDataOutput.extend<
int>(
std::string(
"RUN_NUMBER"));
576 CTPPSDataOutput.extend<
int>(
std::string(
"FILL_NUMBER"));
577 CTPPSDataOutput.extend<
float>(
std::string(
"XING_ANGLE_P5_X_URAD"));
578 CTPPSDataOutput.extend<
float>(
std::string(
"XING_ANGLE_P5_Y_URAD"));
579 CTPPSDataOutput.extend<
float>(
std::string(
"BETA_STAR_P5_X_M"));
580 CTPPSDataOutput.extend<
float>(
std::string(
"BETA_STAR_P5_Y_M"));
581 CTPPSDataQuery->defineOutput(CTPPSDataOutput);
583 coral::ICursor& CTPPSDataCursor = CTPPSDataQuery->execute();
584 unsigned int lumiSection = 0;
587 float crossingAngleX = 0., betaStarX = 0.;
588 float crossingAngleY = 0., betaStarY = 0.;
591 int wrongFillNumbers = 0;
592 std::stringstream wrongFills;
593 std::vector<pair<cond::Time_t, std::shared_ptr<LHCInfoPerLS>>>::iterator current =
m_tmpBuffer.begin();
594 while (CTPPSDataCursor.next()) {
596 std::ostringstream CTPPS;
597 CTPPSDataCursor.currentRow().toOutputStream(CTPPS);
599 coral::Attribute
const& dipTimeAttribute = CTPPSDataCursor.currentRow()[
std::string(
"DIP_UPDATE_TIME")];
600 if (!dipTimeAttribute.isNull()) {
602 coral::Attribute
const& lumiSectionAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LUMI_SECTION")];
603 if (!lumiSectionAttribute.isNull()) {
604 lumiSection = lumiSectionAttribute.data<
int>();
606 coral::Attribute
const& runNumberAttribute = CTPPSDataCursor.currentRow()[
std::string(
"RUN_NUMBER")];
607 if (!runNumberAttribute.isNull()) {
608 runNumber = runNumberAttribute.data<
int>();
610 coral::Attribute
const& fillNumberAttribute = CTPPSDataCursor.currentRow()[
std::string(
"FILL_NUMBER")];
611 if (!fillNumberAttribute.isNull()) {
612 fillNumber = fillNumberAttribute.data<
int>();
614 coral::Attribute
const& crossingAngleXAttribute =
615 CTPPSDataCursor.currentRow()[
std::string(
"XING_ANGLE_P5_X_URAD")];
616 if (!crossingAngleXAttribute.isNull()) {
617 crossingAngleX = crossingAngleXAttribute.data<
float>();
619 coral::Attribute
const& crossingAngleYAttribute =
620 CTPPSDataCursor.currentRow()[
std::string(
"XING_ANGLE_P5_Y_URAD")];
621 if (!crossingAngleYAttribute.isNull()) {
622 crossingAngleY = crossingAngleYAttribute.data<
float>();
624 coral::Attribute
const& betaStarXAttribute = CTPPSDataCursor.currentRow()[
std::string(
"BETA_STAR_P5_X_M")];
625 if (!betaStarXAttribute.isNull()) {
626 betaStarX = betaStarXAttribute.data<
float>();
628 coral::Attribute
const& betaStarYAttribute = CTPPSDataCursor.currentRow()[
std::string(
"BETA_STAR_P5_Y_M")];
629 if (!betaStarYAttribute.isNull()) {
630 betaStarY = betaStarYAttribute.data<
float>();
632 if (current !=
m_tmpBuffer.end() && current->second->fillNumber() != fillNumber) {
633 wrongFills <<
"( " <<
runNumber <<
"_" << lumiSection <<
" fill: OMS: " << current->second->fillNumber()
634 <<
" PPSdb: " << fillNumber <<
" ) ";
637 for (; current !=
m_tmpBuffer.end() && make_pair(current->second->runNumber(), current->second->lumiSection()) <=
641 payload.setCrossingAngleX(crossingAngleX);
642 payload.setCrossingAngleY(crossingAngleY);
643 payload.setBetaStarX(betaStarX);
644 payload.setBetaStarY(betaStarY);
645 payload.setLumiSection(lumiSection);
653 if (wrongFillNumbers) {
654 edm::LogWarning(
"getCTPPSData") <<
"Number of records from PPS DB with fillNumber different from OMS: " 656 edm::LogWarning(
"getCTPPSData") <<
"Records from PPS DB with fillNumber different from OMS: " << wrongFills.str();
cond::Time_t m_startFillTime
void addDefaultPayload(cond::Time_t iov, unsigned short fill, const cond::OMSService &oms)
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(edm::ParameterSet const &pset)
std::unique_ptr< T > fetchPayload(const cond::Hash &payloadHash)
std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfoPerLS > > > m_tmpBuffer
Log< level::Error, false > LogError
Transaction & transaction()
float const betaStarY() const
cond::Time_t m_prevEndFillTime
std::pair< int, unsigned short > getFillLastRunAndLS(const cond::OMSService &oms, unsigned short fillId)
bool isPayloadValid(const LHCInfoPerLS &payload) const
~LHCInfoPerLSPopConSourceHandler() override
cond::TagInfo_t const & tagInfo() const
float const crossingAngleX() const
float m_defaultCrossingAngleX
float m_defaultCrossingAngleY
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)
std::string m_connectionString
void addPayloadToBuffer(cond::OMSServiceResultRef &row)
void setMessageVerbosity(coral::MsgLevel level)
Time_t from_boost(boost::posix_time::ptime bt)
std::map< std::pair< cond::Time_t, unsigned int >, std::pair< cond::Time_t, unsigned int > > m_lsIdMap
static constexpr unsigned int kLumisectionsQueryLimit
float const crossingAngleY() const
Log< level::Info, false > LogInfo
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
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
static constexpr const char *const SNULL
void setAuthenticationPath(const std::string &p)
cond::Time_t m_startStableBeamTime
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)
void filterInvalidPayloads()