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).filterGT(
"start_time", beginFillTime).filterLT(
"start_time", endFillTime);
161 if (
query->execute()) {
165 auto lumiTime =
r.get<boost::posix_time::ptime>(
"start_time");
166 auto delivLumi =
r.get<
float>(
"delivered_lumi");
167 auto recLumi =
r.get<
float>(
"recorded_lumi");
171 payload.setDelivLumi(delivLumi);
227 std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator
current() {
return currLow; }
228 std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator
currLow;
229 std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator
currUp;
230 std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator
end;
236 const boost::posix_time::ptime& beginFillTime,
237 const boost::posix_time::ptime& endFillTime) {
240 auto query1 = oms.
query(
"diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam1");
241 query1->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
242 if (query1->execute()) {
243 auto res = query1->result();
245 std::bitset<LHCInfo::bunchSlots + 1> bunchConfiguration1(0ULL);
246 auto row = *
res.begin();
247 auto vbunchConf1 = row.getArray<
unsigned short>(
"value");
248 for (
auto vb : vbunchConf1) {
250 unsigned short slot = (vb - 1) / 10 + 1;
251 bunchConfiguration1[slot] =
true;
257 auto query2 = oms.
query(
"diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam2");
258 query2->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
259 if (query2->execute()) {
260 auto res = query2->result();
262 std::bitset<LHCInfo::bunchSlots + 1> bunchConfiguration2(0ULL);
263 auto row = *
res.begin();
264 auto vbunchConf2 = row.getArray<
unsigned short>(
"value");
265 for (
auto vb : vbunchConf2) {
267 unsigned short slot = (vb - 1) / 10 + 1;
268 bunchConfiguration2[slot] =
true;
275 auto query3 = oms.
query(
"diplogger/dip/CMS/LHC/LumiPerBunch");
276 query3->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
277 if (query3->execute()) {
278 auto res = query3->result();
280 std::vector<float> lumiPerBX;
281 auto row = *
res.begin();
282 auto lumiBunchInst = row.getArray<
float>(
"lumi_bunch_inst");
283 for (
auto lb : lumiBunchInst) {
285 lumiPerBX.push_back(lb);
294 const boost::posix_time::ptime& beginFillTime,
295 const boost::posix_time::ptime& endFillTime) {
298 coral::ISchema& CTPPS = session.
coralSession().schema(
"CMS_PPS_SPECT_COND");
300 std::unique_ptr<coral::IQuery> CTPPSDataQuery(CTPPS.newQuery());
302 CTPPSDataQuery->addToTableList(
std::string(
"PPS_LHC_MACHINE_PARAMS"));
304 CTPPSDataQuery->addToOutputList(
std::string(
"DIP_UPDATE_TIME"));
305 CTPPSDataQuery->addToOutputList(
std::string(
"LHC_STATE"));
306 CTPPSDataQuery->addToOutputList(
std::string(
"LHC_COMMENT"));
307 CTPPSDataQuery->addToOutputList(
std::string(
"LUMI_SECTION"));
308 CTPPSDataQuery->addToOutputList(
std::string(
"XING_ANGLE_P5_X_URAD"));
309 CTPPSDataQuery->addToOutputList(
std::string(
"BETA_STAR_P5_X_M"));
311 coral::AttributeList CTPPSDataBindVariables;
312 CTPPSDataBindVariables.extend<coral::TimeStamp>(
std::string(
"beginFillTime"));
313 CTPPSDataBindVariables.extend<coral::TimeStamp>(
std::string(
"endFillTime"));
314 CTPPSDataBindVariables[
std::string(
"beginFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(beginFillTime);
315 CTPPSDataBindVariables[
std::string(
"endFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(endFillTime);
316 std::string conditionStr =
std::string(
"DIP_UPDATE_TIME>= :beginFillTime and DIP_UPDATE_TIME< :endFillTime");
317 CTPPSDataQuery->setCondition(conditionStr, CTPPSDataBindVariables);
319 CTPPSDataQuery->addToOrderList(
std::string(
"DIP_UPDATE_TIME"));
321 coral::AttributeList CTPPSDataOutput;
322 CTPPSDataOutput.extend<coral::TimeStamp>(
std::string(
"DIP_UPDATE_TIME"));
325 CTPPSDataOutput.extend<
int>(
std::string(
"LUMI_SECTION"));
326 CTPPSDataOutput.extend<
float>(
std::string(
"XING_ANGLE_P5_X_URAD"));
327 CTPPSDataOutput.extend<
float>(
std::string(
"BETA_STAR_P5_X_M"));
328 CTPPSDataQuery->defineOutput(CTPPSDataOutput);
330 coral::ICursor& CTPPSDataCursor = CTPPSDataQuery->execute();
332 std::string lhcState =
"", lhcComment =
"", ctppsStatus =
"";
333 unsigned int lumiSection = 0;
334 float crossingAngle = 0., betastar = 0.;
338 while (CTPPSDataCursor.next()) {
340 std::ostringstream CTPPS;
341 CTPPSDataCursor.currentRow().toOutputStream(CTPPS);
343 coral::Attribute
const& dipTimeAttribute = CTPPSDataCursor.currentRow()[
std::string(
"DIP_UPDATE_TIME")];
344 if (!dipTimeAttribute.isNull()) {
346 if (
filter.process(dipTime)) {
348 coral::Attribute
const& lhcStateAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LHC_STATE")];
349 if (!lhcStateAttribute.isNull()) {
352 coral::Attribute
const& lhcCommentAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LHC_COMMENT")];
353 if (!lhcCommentAttribute.isNull()) {
354 lhcComment = lhcCommentAttribute.data<
std::string>();
356 coral::Attribute
const& lumiSectionAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LUMI_SECTION")];
357 if (!lumiSectionAttribute.isNull()) {
358 lumiSection = lumiSectionAttribute.data<
int>();
360 coral::Attribute
const& crossingAngleXAttribute =
361 CTPPSDataCursor.currentRow()[
std::string(
"XING_ANGLE_P5_X_URAD")];
362 if (!crossingAngleXAttribute.isNull()) {
363 crossingAngle = crossingAngleXAttribute.data<
float>();
365 coral::Attribute
const& betaStarXAttribute = CTPPSDataCursor.currentRow()[
std::string(
"BETA_STAR_P5_X_M")];
366 if (!betaStarXAttribute.isNull()) {
367 betastar = betaStarXAttribute.data<
float>();
372 payload.setCrossingAngle(crossingAngle);
375 payload.setLhcComment(lhcComment);
376 payload.setCtppsStatus(ctppsStatus);
377 payload.setLumiSection(lumiSection);
386 static const std::map<std::string, int>
vecMap = {
387 {
"Beam1/beamPhaseMean", 1}, {
"Beam2/beamPhaseMean", 2}, {
"Beam1/cavPhaseMean", 3}, {
"Beam2/cavPhaseMean", 4}};
390 unsigned int elementNr,
393 std::set<cond::Time_t>& initList) {
394 if (initList.find(
since) == initList.end()) {
399 initList.insert(
since);
403 switch (
vecMap.at(dipVal)) {
424 const boost::posix_time::ptime& lowerTime,
425 const boost::posix_time::ptime& upperTime,
432 std::unique_ptr<coral::IQuery> ECALDataQuery(
ECAL.newQuery());
434 ECALDataQuery->addToTableList(
std::string(
"BEAM_PHASE"));
436 ECALDataQuery->addToOutputList(
std::string(
"CHANGE_DATE"));
437 ECALDataQuery->addToOutputList(
std::string(
"DIP_value"));
438 ECALDataQuery->addToOutputList(
std::string(
"element_nr"));
439 ECALDataQuery->addToOutputList(
std::string(
"VALUE_NUMBER"));
441 coral::AttributeList ECALDataBindVariables;
442 ECALDataBindVariables.extend<coral::TimeStamp>(
std::string(
"lowerTime"));
443 ECALDataBindVariables.extend<coral::TimeStamp>(
std::string(
"upperTime"));
444 ECALDataBindVariables[
std::string(
"lowerTime")].data<coral::TimeStamp>() = coral::TimeStamp(lowerTime);
445 ECALDataBindVariables[
std::string(
"upperTime")].data<coral::TimeStamp>() = coral::TimeStamp(upperTime);
447 "(DIP_value LIKE '%beamPhaseMean%' OR DIP_value LIKE '%cavPhaseMean%') AND CHANGE_DATE >= :lowerTime AND " 448 "CHANGE_DATE < :upperTime");
450 ECALDataQuery->setCondition(conditionStr, ECALDataBindVariables);
452 ECALDataQuery->addToOrderList(
std::string(
"CHANGE_DATE"));
453 ECALDataQuery->addToOrderList(
std::string(
"DIP_value"));
454 ECALDataQuery->addToOrderList(
std::string(
"element_nr"));
456 coral::AttributeList ECALDataOutput;
457 ECALDataOutput.extend<coral::TimeStamp>(
std::string(
"CHANGE_DATE"));
459 ECALDataOutput.extend<
unsigned int>(
std::string(
"element_nr"));
460 ECALDataOutput.extend<
float>(
std::string(
"VALUE_NUMBER"));
462 ECALDataQuery->defineOutput(ECALDataOutput);
464 coral::ICursor& ECALDataCursor = ECALDataQuery->execute();
468 unsigned int elementNr = 0;
470 std::set<cond::Time_t> initializedVectors;
481 std::map<cond::Time_t, cond::Time_t> iovMap;
483 while (ECALDataCursor.next()) {
485 std::ostringstream
ECAL;
486 ECALDataCursor.currentRow().toOutputStream(
ECAL);
488 coral::Attribute
const& changeDateAttribute = ECALDataCursor.currentRow()[
std::string(
"CHANGE_DATE")];
489 if (!changeDateAttribute.isNull()) {
491 boost::posix_time::ptime chTime = changeDateAttribute.data<coral::TimeStamp>().
time();
493 if (changeTime == 0) {
498 if (!
update and changeTime == firstTime)
500 coral::Attribute
const& dipValAttribute = ECALDataCursor.currentRow()[
std::string(
"DIP_value")];
501 coral::Attribute
const& valueNumberAttribute = ECALDataCursor.currentRow()[
std::string(
"VALUE_NUMBER")];
502 coral::Attribute
const& elementNrAttribute = ECALDataCursor.currentRow()[
std::string(
"element_nr")];
503 if (!dipValAttribute.isNull() and !valueNumberAttribute.isNull()) {
505 elementNr = elementNrAttribute.data<
unsigned int>();
506 value = valueNumberAttribute.data<
float>();
509 if (
filter.process(iovTime)) {
510 iovMap.insert(std::make_pair(changeTime,
filter.current()->first));
521 for (
auto& im : iovMap) {
535 auto lastAdded =
m_iovs.rbegin()->second;
536 if (lastAdded->fillNumber() != 0) {
541 auto newPayload = std::make_shared<LHCInfo>();
542 m_iovs.insert(std::make_pair(iov, newPayload));
574 std::shared_ptr<LHCInfo>& prevPayload) {
576 for (
auto& iov :
buffer) {
580 if (iovsToTransfer.empty()) {
583 LHCInfo& lastAdded = *iovsToTransfer.rbegin()->second;
591 prevPayload = iov.second;
610 << boost::posix_time::to_iso_extended_string(
612 <<
" ); from " <<
m_name <<
"::getNewObjects";
622 <<
m_name <<
"::getNewObjects";
625 boost::posix_time::ptime executionTime = boost::posix_time::second_clock::local_time();
631 if (lastSince > targetSince)
632 targetSince = lastSince;
651 if (!
tagInfo().lastInterval.payloadId.empty()) {
658 bool iovAdded =
false;
660 if (targetSince >= endIov) {
665 bool updateEcal =
false;
667 boost::posix_time::ptime startSampleTime;
668 boost::posix_time::ptime endSampleTime;
678 bool foundFill =
query->execute();
688 edm::LogInfo(
m_name) <<
"Searching new fill after " << boost::posix_time::to_simple_string(targetTime);
690 query->filterNotNull(
"start_stable_beam").filterGT(
"start_time",
startTime).filterNotNull(
"fill_number");
692 query->filterNotNull(
"end_time");
693 bool foundFill =
query->execute();
707 if (endFillTime == 0ULL) {
709 endSampleTime = executionTime;
710 targetSince = endIov;
715 targetSince = endFillTime;
718 getDipData(oms, startSampleTime, endSampleTime);
719 size_t nlumi =
getLumiData(oms, lhcFill, startSampleTime, endSampleTime);
720 edm::LogInfo(
m_name) <<
"Found " << nlumi <<
" lumisections during the fill " << lhcFill;
723 edm::LogInfo(
m_name) <<
"First lumi starts at " << flumiStart <<
" last lumi starts at " << flumiStop;
728 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)
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()