7 #include "RelationalAccess/ISessionProxy.h" 8 #include "RelationalAccess/ISchema.h" 9 #include "RelationalAccess/IQuery.h" 10 #include "RelationalAccess/ICursor.h" 11 #include "CoralBase/AttributeList.h" 12 #include "CoralBase/Attribute.h" 13 #include "CoralBase/AttributeSpecification.h" 14 #include "CoralBase/TimeStamp.h" 37 if (s_fill_type ==
i.first)
44 if (s_particle_type ==
i.first)
53 return from_string_impl<LHCInfo::FillType, &fillTypeFromString>(attributeValue,
LHCInfo::UNKNOWN);
58 return from_string_impl<LHCInfo::ParticleType, &particleTypeFromString>(attributeValue,
LHCInfo::NONE);
68 m_samplingInterval((unsigned
int)
pset.getUntrackedParameter<unsigned
int>(
"samplingInterval", 300)),
69 m_endFill(
pset.getUntrackedParameter<
bool>(
"endFill",
true)),
70 m_name(
pset.getUntrackedParameter<
std::
string>(
"name",
"LHCInfoPopConSourceHandler")),
71 m_connectionString(
pset.getUntrackedParameter<
std::
string>(
"connectionString",
"")),
72 m_ecalConnectionString(
pset.getUntrackedParameter<
std::
string>(
"ecalConnectionString",
"")),
73 m_dipSchema(
pset.getUntrackedParameter<
std::
string>(
"DIPSchema",
"")),
74 m_authpath(
pset.getUntrackedParameter<
std::
string>(
"authenticationPath",
"")),
75 m_omsBaseUrl(
pset.getUntrackedParameter<
std::
string>(
"omsBaseUrl",
"")),
79 if (
pset.exists(
"startTime")) {
82 boost::posix_time::ptime
now = boost::posix_time::second_clock::local_time();
84 if (
pset.exists(
"endTime")) {
103 std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator
search(
105 if (container.empty())
106 return container.end();
108 return (
p != container.begin()) ?
p - 1 : container.end();
113 if (!queryResult.
empty()) {
114 auto row = *queryResult.
begin();
115 auto currentFill = row.get<
unsigned short>(
"fill_number");
116 auto bunches1 = row.get<
unsigned short>(
"bunches_beam1");
117 auto bunches2 = row.get<
unsigned short>(
"bunches_beam2");
118 auto collidingBunches = row.get<
unsigned short>(
"bunches_colliding");
119 auto targetBunches = row.get<
unsigned short>(
"bunches_target");
123 auto intensityBeam1 = row.get<
float>(
"intensity_beam1");
124 auto intensityBeam2 = row.get<
float>(
"intensity_beam2");
125 auto energy = row.get<
float>(
"energy");
126 auto creationTime = row.get<boost::posix_time::ptime>(
"start_time");
127 auto stableBeamStartTime = row.get<boost::posix_time::ptime>(
"start_stable_beam");
128 auto beamDumpTime = row.get<boost::posix_time::ptime>(
"end_time");
129 auto injectionScheme = row.get<
std::string>(
"injection_scheme");
130 targetPayload = std::make_unique<LHCInfo>();
131 targetPayload->setFillNumber(currentFill);
132 targetPayload->setBunchesInBeam1(bunches1);
133 targetPayload->setBunchesInBeam2(bunches2);
134 targetPayload->setCollidingBunches(collidingBunches);
135 targetPayload->setTargetBunches(targetBunches);
136 targetPayload->setFillType(fillType);
137 targetPayload->setParticleTypeForBeam1(particleType1);
138 targetPayload->setParticleTypeForBeam2(particleType2);
139 targetPayload->setIntensityForBeam1(intensityBeam1);
140 targetPayload->setIntensityForBeam2(intensityBeam2);
141 targetPayload->setEnergy(
energy);
145 targetPayload->setInjectionScheme(injectionScheme);
154 unsigned short fillId,
155 const boost::posix_time::ptime& beginFillTime,
156 const boost::posix_time::ptime& endFillTime) {
158 query->addOutputVars({
"start_time",
"delivered_lumi",
"recorded_lumi"});
159 query->filterEQ(
"fill_number", fillId);
160 query->filterGT(
"start_time", beginFillTime).filterLT(
"start_time", endFillTime);
163 if (
query->execute()) {
167 auto lumiTime =
r.get<boost::posix_time::ptime>(
"start_time");
168 auto delivLumi =
r.get<
float>(
"delivered_lumi");
169 auto recLumi =
r.get<
float>(
"recorded_lumi");
173 payload.setDelivLumi(delivLumi);
229 std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator
current() {
return currLow; }
230 std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator
currLow;
231 std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator
currUp;
232 std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator
end;
238 const boost::posix_time::ptime& beginFillTime,
239 const boost::posix_time::ptime& endFillTime) {
242 auto query1 = oms.
query(
"diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam1");
243 query1->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
246 if (query1->execute()) {
247 auto res = query1->result();
249 std::bitset<LHCInfo::bunchSlots + 1> bunchConfiguration1(0ULL);
250 auto row = *
res.begin();
251 auto vbunchConf1 = row.getArray<
unsigned short>(
"value");
252 for (
auto vb : vbunchConf1) {
254 unsigned short slot = (vb - 1) / 10 + 1;
255 bunchConfiguration1[slot] =
true;
261 auto query2 = oms.
query(
"diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam2");
262 query2->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
264 if (query2->execute()) {
265 auto res = query2->result();
267 std::bitset<LHCInfo::bunchSlots + 1> bunchConfiguration2(0ULL);
268 auto row = *
res.begin();
269 auto vbunchConf2 = row.getArray<
unsigned short>(
"value");
270 for (
auto vb : vbunchConf2) {
272 unsigned short slot = (vb - 1) / 10 + 1;
273 bunchConfiguration2[slot] =
true;
280 auto query3 = oms.
query(
"diplogger/dip/CMS/LHC/LumiPerBunch");
281 query3->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
283 if (query3->execute()) {
284 auto res = query3->result();
286 std::vector<float> lumiPerBX;
287 auto row = *
res.begin();
288 auto lumiBunchInst = row.getArray<
float>(
"lumi_bunch_inst");
289 for (
auto lb : lumiBunchInst) {
291 lumiPerBX.push_back(lb);
300 const boost::posix_time::ptime& beginFillTime,
301 const boost::posix_time::ptime& endFillTime) {
304 coral::ISchema& CTPPS = session.
coralSession().schema(
"CMS_PPS_SPECT_COND");
306 std::unique_ptr<coral::IQuery> CTPPSDataQuery(CTPPS.newQuery());
308 CTPPSDataQuery->addToTableList(
std::string(
"PPS_LHC_MACHINE_PARAMS"));
310 CTPPSDataQuery->addToOutputList(
std::string(
"DIP_UPDATE_TIME"));
311 CTPPSDataQuery->addToOutputList(
std::string(
"LHC_STATE"));
312 CTPPSDataQuery->addToOutputList(
std::string(
"LHC_COMMENT"));
313 CTPPSDataQuery->addToOutputList(
std::string(
"LUMI_SECTION"));
314 CTPPSDataQuery->addToOutputList(
std::string(
"XING_ANGLE_P5_X_URAD"));
315 CTPPSDataQuery->addToOutputList(
std::string(
"BETA_STAR_P5_X_M"));
317 coral::AttributeList CTPPSDataBindVariables;
318 CTPPSDataBindVariables.extend<coral::TimeStamp>(
std::string(
"beginFillTime"));
319 CTPPSDataBindVariables.extend<coral::TimeStamp>(
std::string(
"endFillTime"));
320 CTPPSDataBindVariables[
std::string(
"beginFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(beginFillTime);
321 CTPPSDataBindVariables[
std::string(
"endFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(endFillTime);
322 std::string conditionStr =
std::string(
"DIP_UPDATE_TIME>= :beginFillTime and DIP_UPDATE_TIME< :endFillTime");
323 CTPPSDataQuery->setCondition(conditionStr, CTPPSDataBindVariables);
325 CTPPSDataQuery->addToOrderList(
std::string(
"DIP_UPDATE_TIME"));
327 coral::AttributeList CTPPSDataOutput;
328 CTPPSDataOutput.extend<coral::TimeStamp>(
std::string(
"DIP_UPDATE_TIME"));
331 CTPPSDataOutput.extend<
int>(
std::string(
"LUMI_SECTION"));
332 CTPPSDataOutput.extend<
float>(
std::string(
"XING_ANGLE_P5_X_URAD"));
333 CTPPSDataOutput.extend<
float>(
std::string(
"BETA_STAR_P5_X_M"));
334 CTPPSDataQuery->defineOutput(CTPPSDataOutput);
336 coral::ICursor& CTPPSDataCursor = CTPPSDataQuery->execute();
338 std::string lhcState =
"", lhcComment =
"", ctppsStatus =
"";
339 unsigned int lumiSection = 0;
340 float crossingAngle = 0., betastar = 0.;
344 while (CTPPSDataCursor.next()) {
346 std::ostringstream CTPPS;
347 CTPPSDataCursor.currentRow().toOutputStream(CTPPS);
349 coral::Attribute
const& dipTimeAttribute = CTPPSDataCursor.currentRow()[
std::string(
"DIP_UPDATE_TIME")];
350 if (!dipTimeAttribute.isNull()) {
352 if (
filter.process(dipTime)) {
354 coral::Attribute
const& lhcStateAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LHC_STATE")];
355 if (!lhcStateAttribute.isNull()) {
358 coral::Attribute
const& lhcCommentAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LHC_COMMENT")];
359 if (!lhcCommentAttribute.isNull()) {
360 lhcComment = lhcCommentAttribute.data<
std::string>();
362 coral::Attribute
const& lumiSectionAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LUMI_SECTION")];
363 if (!lumiSectionAttribute.isNull()) {
364 lumiSection = lumiSectionAttribute.data<
int>();
366 coral::Attribute
const& crossingAngleXAttribute =
367 CTPPSDataCursor.currentRow()[
std::string(
"XING_ANGLE_P5_X_URAD")];
368 if (!crossingAngleXAttribute.isNull()) {
369 crossingAngle = crossingAngleXAttribute.data<
float>();
371 coral::Attribute
const& betaStarXAttribute = CTPPSDataCursor.currentRow()[
std::string(
"BETA_STAR_P5_X_M")];
372 if (!betaStarXAttribute.isNull()) {
373 betastar = betaStarXAttribute.data<
float>();
378 payload.setCrossingAngle(crossingAngle);
381 payload.setLhcComment(lhcComment);
382 payload.setCtppsStatus(ctppsStatus);
383 payload.setLumiSection(lumiSection);
392 static const std::map<std::string, int>
vecMap = {
393 {
"Beam1/beamPhaseMean", 1}, {
"Beam2/beamPhaseMean", 2}, {
"Beam1/cavPhaseMean", 3}, {
"Beam2/cavPhaseMean", 4}};
396 unsigned int elementNr,
399 std::set<cond::Time_t>& initList) {
400 if (initList.find(
since) == initList.end()) {
405 initList.insert(
since);
409 switch (
vecMap.at(dipVal)) {
430 const boost::posix_time::ptime& lowerTime,
431 const boost::posix_time::ptime& upperTime,
438 std::unique_ptr<coral::IQuery> ECALDataQuery(
ECAL.newQuery());
440 ECALDataQuery->addToTableList(
std::string(
"BEAM_PHASE"));
442 ECALDataQuery->addToOutputList(
std::string(
"CHANGE_DATE"));
443 ECALDataQuery->addToOutputList(
std::string(
"DIP_value"));
444 ECALDataQuery->addToOutputList(
std::string(
"element_nr"));
445 ECALDataQuery->addToOutputList(
std::string(
"VALUE_NUMBER"));
447 coral::AttributeList ECALDataBindVariables;
448 ECALDataBindVariables.extend<coral::TimeStamp>(
std::string(
"lowerTime"));
449 ECALDataBindVariables.extend<coral::TimeStamp>(
std::string(
"upperTime"));
450 ECALDataBindVariables[
std::string(
"lowerTime")].data<coral::TimeStamp>() = coral::TimeStamp(lowerTime);
451 ECALDataBindVariables[
std::string(
"upperTime")].data<coral::TimeStamp>() = coral::TimeStamp(upperTime);
453 "(DIP_value LIKE '%beamPhaseMean%' OR DIP_value LIKE '%cavPhaseMean%') AND CHANGE_DATE >= :lowerTime AND " 454 "CHANGE_DATE < :upperTime");
456 ECALDataQuery->setCondition(conditionStr, ECALDataBindVariables);
458 ECALDataQuery->addToOrderList(
std::string(
"CHANGE_DATE"));
459 ECALDataQuery->addToOrderList(
std::string(
"DIP_value"));
460 ECALDataQuery->addToOrderList(
std::string(
"element_nr"));
462 coral::AttributeList ECALDataOutput;
463 ECALDataOutput.extend<coral::TimeStamp>(
std::string(
"CHANGE_DATE"));
465 ECALDataOutput.extend<
unsigned int>(
std::string(
"element_nr"));
466 ECALDataOutput.extend<
float>(
std::string(
"VALUE_NUMBER"));
468 ECALDataQuery->defineOutput(ECALDataOutput);
470 coral::ICursor& ECALDataCursor = ECALDataQuery->execute();
474 unsigned int elementNr = 0;
476 std::set<cond::Time_t> initializedVectors;
487 std::map<cond::Time_t, cond::Time_t> iovMap;
489 while (ECALDataCursor.next()) {
491 std::ostringstream
ECAL;
492 ECALDataCursor.currentRow().toOutputStream(
ECAL);
494 coral::Attribute
const& changeDateAttribute = ECALDataCursor.currentRow()[
std::string(
"CHANGE_DATE")];
495 if (!changeDateAttribute.isNull()) {
497 boost::posix_time::ptime chTime = changeDateAttribute.data<coral::TimeStamp>().
time();
499 if (changeTime == 0) {
504 if (!
update and changeTime == firstTime)
506 coral::Attribute
const& dipValAttribute = ECALDataCursor.currentRow()[
std::string(
"DIP_value")];
507 coral::Attribute
const& valueNumberAttribute = ECALDataCursor.currentRow()[
std::string(
"VALUE_NUMBER")];
508 coral::Attribute
const& elementNrAttribute = ECALDataCursor.currentRow()[
std::string(
"element_nr")];
509 if (!dipValAttribute.isNull() and !valueNumberAttribute.isNull()) {
511 elementNr = elementNrAttribute.data<
unsigned int>();
512 value = valueNumberAttribute.data<
float>();
515 if (
filter.process(iovTime)) {
516 iovMap.insert(std::make_pair(changeTime,
filter.current()->first));
527 for (
auto& im : iovMap) {
541 auto lastAdded =
m_iovs.rbegin()->second;
542 if (lastAdded->fillNumber() != 0) {
547 auto newPayload = std::make_shared<LHCInfo>();
548 m_iovs.insert(std::make_pair(iov, newPayload));
580 std::shared_ptr<LHCInfo>& prevPayload) {
582 for (
auto& iov :
buffer) {
586 if (iovsToTransfer.empty()) {
589 LHCInfo& lastAdded = *iovsToTransfer.rbegin()->second;
597 prevPayload = iov.second;
616 << boost::posix_time::to_iso_extended_string(
618 <<
" ); from " <<
m_name <<
"::getNewObjects";
628 <<
m_name <<
"::getNewObjects";
631 boost::posix_time::ptime executionTime = boost::posix_time::second_clock::local_time();
637 if (lastSince > targetSince)
638 targetSince = lastSince;
657 if (!
tagInfo().lastInterval.payloadId.empty()) {
664 bool iovAdded =
false;
666 if (targetSince >= endIov) {
671 bool updateEcal =
false;
673 boost::posix_time::ptime startSampleTime;
674 boost::posix_time::ptime endSampleTime;
684 bool foundFill =
query->execute();
694 edm::LogInfo(
m_name) <<
"Searching new fill after " << boost::posix_time::to_simple_string(targetTime);
695 query->filterNotNull(
"start_stable_beam").filterNotNull(
"fill_number");
697 query->filterGE(
"start_time", targetTime);
699 query->filterGT(
"start_time", targetTime);
704 query->filterNotNull(
"end_time");
705 bool foundFill =
query->execute();
719 if (endFillTime == 0ULL) {
721 endSampleTime = executionTime;
722 targetSince = endIov;
727 targetSince = endFillTime;
730 getDipData(oms, startSampleTime, endSampleTime);
731 size_t nlumi =
getLumiData(oms, lhcFill, startSampleTime, endSampleTime);
732 edm::LogInfo(
m_name) <<
"Found " << nlumi <<
" lumisections during the fill " << lhcFill;
735 edm::LogInfo(
m_name) <<
"First lumi starts at " << flumiStart <<
" last lumi starts at " << flumiStop;
740 getEcalData(session2, startSampleTime, endSampleTime, updateEcal);
cond::Time_t currentDipTime
edm::ErrorSummaryEntry Error
std::string const & ctppsStatus() const
static const std::map< std::string, int > vecMap
float const instLumiError() const
boost::posix_time::ptime m_startTime
bool getCTTPSData(cond::persistency::Session &session, const boost::posix_time::ptime &beginFillTime, const boost::posix_time::ptime &endFillTime)
bool makeFillPayload(std::unique_ptr< LHCInfo > &targetPayload, const cond::OMSServiceResult &queryResult)
std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo > > > m_tmpBuffer
std::unique_ptr< OMSServiceQuery > query(const std::string &function) const
std::string m_connectionString
std::unique_ptr< LHCInfo > m_fillPayload
ret
prodAgent to be discontinued
void start(bool readOnly=true)
T from_string(const std::string &attributeValue)
bool comparePayloads(const LHCInfo &rhs, const LHCInfo &lhs)
void reset(const std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo >>> &samples)
std::string const & lhcComment() const
bool getEcalData(cond::persistency::Session &session, const boost::posix_time::ptime &lowerTime, const boost::posix_time::ptime &upperTime, bool update)
cond::Time_t currentSince()
std::unique_ptr< T > fetchPayload(const cond::Hash &payloadHash)
unsigned short const fillNumber() const
Log< level::Error, false > LogError
std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo > > >::const_iterator currUp
std::shared_ptr< LHCInfo > m_prevPayload
float const recLumi() const
std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo > > >::const_iterator currLow
bool operator()(const cond::Time_t &x, const std::pair< cond::Time_t, std::shared_ptr< LHCInfo >> &y)
Transaction & transaction()
cond::TagInfo_t const & tagInfo() const
std::string id() const override
void getDipData(const cond::OMSService &service, const boost::posix_time::ptime &beginFillTime, const boost::posix_time::ptime &endFillTime)
unsigned long long Time_t
Time_t lumiTime(unsigned int run, unsigned int lumiId)
float const betaStar() const
boost::posix_time::ptime m_endTime
LumiSectionFilter(const std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo >>> &samples)
LHCInfo::FillType fillTypeFromString(const std::string &s_fill_type)
coral::ISchema & nominalSchema()
float const instLumi() const
Session createSession(const std::string &connectionString, bool writeCapable=false)
std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo > > >::const_iterator end
float const crossingAngle() const
size_t transferPayloads(const std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo >>> &buffer, std::map< cond::Time_t, std::shared_ptr< LHCInfo >> &iovsToTransfer, std::shared_ptr< LHCInfo > &prevPayload)
static const std::pair< const char *, LHCInfo::ParticleType > s_particleTypeMap[]
void setMessageVerbosity(coral::MsgLevel level)
Time_t from_boost(boost::posix_time::ptime bt)
static constexpr unsigned int kLumisectionsQueryLimit
std::string const & lhcState() const
Log< level::Info, false > LogInfo
std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo > > >::const_iterator search(const cond::Time_t &val, const std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo >>> &container)
size_t getLumiData(const cond::OMSService &service, unsigned short fillId, const boost::posix_time::ptime &beginFillTime, const boost::posix_time::ptime &endFillTime)
void setElementData(cond::Time_t since, const std::string &dipVal, unsigned int elementNr, float value, LHCInfo &payload, std::set< cond::Time_t > &initList)
void addEmptyPayload(cond::Time_t iov)
cond::persistency::Session & dbSession() const
coral::ISessionProxy & coralSession()
std::unique_ptr< LHCInfo > Ref
void add(std::map< std::string, TH1 *> &h, TH1 *hist)
LHCInfo::ParticleType particleTypeFromString(const std::string &s_particle_type)
~LHCInfoPopConSourceHandler() override
static const std::pair< const char *, LHCInfo::FillType > s_fillTypeMap[]
LHCInfo & currentPayload()
void connect(const std::string &baseUrl)
float const delivLumi() const
OMSServiceResultIterator begin() const
std::string m_ecalConnectionString
void setAuthenticationPath(const std::string &p)
void getNewObjects() override
const Time_t MAX_VAL(std::numeric_limits< Time_t >::max())
bool process(cond::Time_t dipTime)
boost::posix_time::ptime to_boost(Time_t iValue)
static size_t const bunchSlots
LHCInfoPopConSourceHandler(const edm::ParameterSet &pset)
std::vector< std::pair< cond::Time_t, std::shared_ptr< LHCInfo > > >::const_iterator current()