7 #include "CoralBase/Attribute.h" 8 #include "CoralBase/AttributeList.h" 9 #include "CoralBase/AttributeSpecification.h" 10 #include "CoralBase/TimeStamp.h" 14 #include "RelationalAccess/ICursor.h" 15 #include "RelationalAccess/IQuery.h" 16 #include "RelationalAccess/ISchema.h" 17 #include "RelationalAccess/ISessionProxy.h" 39 if (s_fill_type ==
i.first)
46 if (s_particle_type ==
i.first)
55 return from_string_impl<LHCInfo::FillType, &fillTypeFromString>(attributeValue,
LHCInfo::UNKNOWN);
60 return from_string_impl<LHCInfo::ParticleType, &particleTypeFromString>(attributeValue,
LHCInfo::NONE);
70 m_samplingInterval((unsigned
int)
pset.getUntrackedParameter<unsigned
int>(
"samplingInterval", 300)),
71 m_endFill(
pset.getUntrackedParameter<
bool>(
"endFill",
true)),
72 m_name(
pset.getUntrackedParameter<
std::
string>(
"name",
"LHCInfoPopConSourceHandler")),
73 m_connectionString(
pset.getUntrackedParameter<
std::
string>(
"connectionString",
"")),
74 m_ecalConnectionString(
pset.getUntrackedParameter<
std::
string>(
"ecalConnectionString",
"")),
75 m_dipSchema(
pset.getUntrackedParameter<
std::
string>(
"DIPSchema",
"")),
76 m_authpath(
pset.getUntrackedParameter<
std::
string>(
"authenticationPath",
"")),
77 m_omsBaseUrl(
pset.getUntrackedParameter<
std::
string>(
"omsBaseUrl",
"")),
81 if (
pset.exists(
"startTime")) {
84 boost::posix_time::ptime
now = boost::posix_time::second_clock::local_time();
86 if (
pset.exists(
"endTime")) {
100 return (
x < y.first);
105 std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator
search(
107 if (container.empty())
108 return container.end();
110 return (
p != container.begin()) ?
p - 1 : container.end();
115 if (!queryResult.
empty()) {
116 auto row = *queryResult.
begin();
117 auto currentFill = row.get<
unsigned short>(
"fill_number");
118 auto bunches1 = row.get<
unsigned short>(
"bunches_beam1");
119 auto bunches2 = row.get<
unsigned short>(
"bunches_beam2");
120 auto collidingBunches = row.get<
unsigned short>(
"bunches_colliding");
121 auto targetBunches = row.get<
unsigned short>(
"bunches_target");
125 auto intensityBeam1 = row.get<
float>(
"intensity_beam1");
126 auto intensityBeam2 = row.get<
float>(
"intensity_beam2");
127 auto energy = row.get<
float>(
"energy");
128 auto creationTime = row.get<boost::posix_time::ptime>(
"start_time");
129 auto stableBeamStartTime = row.get<boost::posix_time::ptime>(
"start_stable_beam");
130 auto beamDumpTime = row.get<boost::posix_time::ptime>(
"end_time");
131 auto injectionScheme = row.get<
std::string>(
"injection_scheme");
132 targetPayload = std::make_unique<LHCInfo>();
133 targetPayload->setFillNumber(currentFill);
134 targetPayload->setBunchesInBeam1(bunches1);
135 targetPayload->setBunchesInBeam2(bunches2);
136 targetPayload->setCollidingBunches(collidingBunches);
137 targetPayload->setTargetBunches(targetBunches);
138 targetPayload->setFillType(fillType);
139 targetPayload->setParticleTypeForBeam1(particleType1);
140 targetPayload->setParticleTypeForBeam2(particleType2);
141 targetPayload->setIntensityForBeam1(intensityBeam1);
142 targetPayload->setIntensityForBeam2(intensityBeam2);
143 targetPayload->setEnergy(
energy);
147 targetPayload->setInjectionScheme(injectionScheme);
156 unsigned short fillId,
157 const boost::posix_time::ptime& beginFillTime,
158 const boost::posix_time::ptime& endFillTime) {
160 query->addOutputVars({
"start_time",
"delivered_lumi",
"recorded_lumi"});
161 query->filterEQ(
"fill_number", fillId);
162 query->filterGT(
"start_time", beginFillTime).filterLT(
"start_time", endFillTime);
165 if (
query->execute()) {
169 auto lumiTime =
r.get<boost::posix_time::ptime>(
"start_time");
170 auto delivLumi =
r.get<
float>(
"delivered_lumi");
171 auto recLumi =
r.get<
float>(
"recorded_lumi");
183 const boost::posix_time::ptime& beginFillTime,
184 const boost::posix_time::ptime& endFillTime) {
187 auto query1 = oms.
query(
"diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam1");
188 query1->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
191 if (query1->execute()) {
192 auto res = query1->result();
194 std::bitset<LHCInfo::bunchSlots + 1> bunchConfiguration1(0ULL);
195 auto row = *
res.begin();
196 auto vbunchConf1 = row.getArray<
unsigned short>(
"value");
197 for (
auto vb : vbunchConf1) {
199 unsigned short slot = (vb - 1) / 10 + 1;
200 bunchConfiguration1[
slot] =
true;
206 auto query2 = oms.
query(
"diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam2");
207 query2->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
209 if (query2->execute()) {
210 auto res = query2->result();
212 std::bitset<LHCInfo::bunchSlots + 1> bunchConfiguration2(0ULL);
213 auto row = *
res.begin();
214 auto vbunchConf2 = row.getArray<
unsigned short>(
"value");
215 for (
auto vb : vbunchConf2) {
217 unsigned short slot = (vb - 1) / 10 + 1;
218 bunchConfiguration2[
slot] =
true;
225 auto query3 = oms.
query(
"diplogger/dip/CMS/LHC/LumiPerBunch");
226 query3->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
228 if (query3->execute()) {
229 auto res = query3->result();
231 std::vector<float> lumiPerBX;
232 auto row = *
res.begin();
233 auto lumiBunchInst = row.getArray<
float>(
"lumi_bunch_inst");
234 for (
auto lb : lumiBunchInst) {
236 lumiPerBX.push_back(lb);
245 const boost::posix_time::ptime& beginFillTime,
246 const boost::posix_time::ptime& endFillTime) {
249 coral::ISchema& CTPPS =
session.coralSession().schema(
"CMS_PPS_SPECT_COND");
251 std::unique_ptr<coral::IQuery> CTPPSDataQuery(CTPPS.newQuery());
253 CTPPSDataQuery->addToTableList(
std::string(
"PPS_LHC_MACHINE_PARAMS"));
255 CTPPSDataQuery->addToOutputList(
std::string(
"DIP_UPDATE_TIME"));
256 CTPPSDataQuery->addToOutputList(
std::string(
"LHC_STATE"));
257 CTPPSDataQuery->addToOutputList(
std::string(
"LHC_COMMENT"));
258 CTPPSDataQuery->addToOutputList(
std::string(
"LUMI_SECTION"));
259 CTPPSDataQuery->addToOutputList(
std::string(
"XING_ANGLE_P5_X_URAD"));
260 CTPPSDataQuery->addToOutputList(
std::string(
"BETA_STAR_P5_X_M"));
262 coral::AttributeList CTPPSDataBindVariables;
263 CTPPSDataBindVariables.extend<coral::TimeStamp>(
std::string(
"beginFillTime"));
264 CTPPSDataBindVariables.extend<coral::TimeStamp>(
std::string(
"endFillTime"));
265 CTPPSDataBindVariables[
std::string(
"beginFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(beginFillTime);
266 CTPPSDataBindVariables[
std::string(
"endFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(endFillTime);
267 std::string conditionStr =
std::string(
"DIP_UPDATE_TIME>= :beginFillTime and DIP_UPDATE_TIME< :endFillTime");
268 CTPPSDataQuery->setCondition(conditionStr, CTPPSDataBindVariables);
270 CTPPSDataQuery->addToOrderList(
std::string(
"DIP_UPDATE_TIME"));
272 coral::AttributeList CTPPSDataOutput;
273 CTPPSDataOutput.extend<coral::TimeStamp>(
std::string(
"DIP_UPDATE_TIME"));
276 CTPPSDataOutput.extend<
int>(
std::string(
"LUMI_SECTION"));
277 CTPPSDataOutput.extend<
float>(
std::string(
"XING_ANGLE_P5_X_URAD"));
278 CTPPSDataOutput.extend<
float>(
std::string(
"BETA_STAR_P5_X_M"));
279 CTPPSDataQuery->defineOutput(CTPPSDataOutput);
281 coral::ICursor& CTPPSDataCursor = CTPPSDataQuery->execute();
283 std::string lhcState =
"", lhcComment =
"", ctppsStatus =
"";
284 unsigned int lumiSection = 0;
289 while (CTPPSDataCursor.next()) {
291 std::ostringstream CTPPS;
292 CTPPSDataCursor.currentRow().toOutputStream(CTPPS);
294 coral::Attribute
const& dipTimeAttribute = CTPPSDataCursor.currentRow()[
std::string(
"DIP_UPDATE_TIME")];
295 if (!dipTimeAttribute.isNull()) {
297 if (
filter.process(dipTime)) {
299 coral::Attribute
const& lhcStateAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LHC_STATE")];
300 if (!lhcStateAttribute.isNull()) {
303 coral::Attribute
const& lhcCommentAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LHC_COMMENT")];
304 if (!lhcCommentAttribute.isNull()) {
305 lhcComment = lhcCommentAttribute.data<
std::string>();
307 coral::Attribute
const& lumiSectionAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LUMI_SECTION")];
308 if (!lumiSectionAttribute.isNull()) {
309 lumiSection = lumiSectionAttribute.data<
int>();
311 coral::Attribute
const& crossingAngleXAttribute =
312 CTPPSDataCursor.currentRow()[
std::string(
"XING_ANGLE_P5_X_URAD")];
313 if (!crossingAngleXAttribute.isNull()) {
316 coral::Attribute
const& betaStarXAttribute = CTPPSDataCursor.currentRow()[
std::string(
"BETA_STAR_P5_X_M")];
317 if (!betaStarXAttribute.isNull()) {
318 betastar = betaStarXAttribute.data<
float>();
326 payload.setLhcComment(lhcComment);
327 payload.setCtppsStatus(ctppsStatus);
328 payload.setLumiSection(lumiSection);
337 static const std::map<std::string, int>
vecMap = {
338 {
"Beam1/beamPhaseMean", 1}, {
"Beam2/beamPhaseMean", 2}, {
"Beam1/cavPhaseMean", 3}, {
"Beam2/cavPhaseMean", 4}};
341 unsigned int elementNr,
344 std::set<cond::Time_t>& initList) {
345 if (initList.find(
since) == initList.end()) {
350 initList.insert(
since);
354 switch (
vecMap.at(dipVal)) {
375 const boost::posix_time::ptime& lowerTime,
376 const boost::posix_time::ptime& upperTime,
383 std::unique_ptr<coral::IQuery> ECALDataQuery(
ECAL.newQuery());
385 ECALDataQuery->addToTableList(
std::string(
"BEAM_PHASE"));
387 ECALDataQuery->addToOutputList(
std::string(
"CHANGE_DATE"));
388 ECALDataQuery->addToOutputList(
std::string(
"DIP_value"));
389 ECALDataQuery->addToOutputList(
std::string(
"element_nr"));
390 ECALDataQuery->addToOutputList(
std::string(
"VALUE_NUMBER"));
392 coral::AttributeList ECALDataBindVariables;
393 ECALDataBindVariables.extend<coral::TimeStamp>(
std::string(
"lowerTime"));
394 ECALDataBindVariables.extend<coral::TimeStamp>(
std::string(
"upperTime"));
395 ECALDataBindVariables[
std::string(
"lowerTime")].data<coral::TimeStamp>() = coral::TimeStamp(lowerTime);
396 ECALDataBindVariables[
std::string(
"upperTime")].data<coral::TimeStamp>() = coral::TimeStamp(upperTime);
398 "(DIP_value LIKE '%beamPhaseMean%' OR DIP_value LIKE '%cavPhaseMean%') AND CHANGE_DATE >= :lowerTime AND " 399 "CHANGE_DATE < :upperTime");
401 ECALDataQuery->setCondition(conditionStr, ECALDataBindVariables);
403 ECALDataQuery->addToOrderList(
std::string(
"CHANGE_DATE"));
404 ECALDataQuery->addToOrderList(
std::string(
"DIP_value"));
405 ECALDataQuery->addToOrderList(
std::string(
"element_nr"));
407 coral::AttributeList ECALDataOutput;
408 ECALDataOutput.extend<coral::TimeStamp>(
std::string(
"CHANGE_DATE"));
410 ECALDataOutput.extend<
unsigned int>(
std::string(
"element_nr"));
411 ECALDataOutput.extend<
float>(
std::string(
"VALUE_NUMBER"));
413 ECALDataQuery->defineOutput(ECALDataOutput);
415 coral::ICursor& ECALDataCursor = ECALDataQuery->execute();
419 unsigned int elementNr = 0;
421 std::set<cond::Time_t> initializedVectors;
432 std::map<cond::Time_t, cond::Time_t> iovMap;
434 while (ECALDataCursor.next()) {
436 std::ostringstream
ECAL;
437 ECALDataCursor.currentRow().toOutputStream(
ECAL);
439 coral::Attribute
const& changeDateAttribute = ECALDataCursor.currentRow()[
std::string(
"CHANGE_DATE")];
440 if (!changeDateAttribute.isNull()) {
442 boost::posix_time::ptime chTime = changeDateAttribute.data<coral::TimeStamp>().
time();
444 if (changeTime == 0) {
449 if (!
update and changeTime == firstTime)
451 coral::Attribute
const& dipValAttribute = ECALDataCursor.currentRow()[
std::string(
"DIP_value")];
452 coral::Attribute
const& valueNumberAttribute = ECALDataCursor.currentRow()[
std::string(
"VALUE_NUMBER")];
453 coral::Attribute
const& elementNrAttribute = ECALDataCursor.currentRow()[
std::string(
"element_nr")];
454 if (!dipValAttribute.isNull() and !valueNumberAttribute.isNull()) {
456 elementNr = elementNrAttribute.data<
unsigned int>();
457 value = valueNumberAttribute.data<
float>();
460 if (
filter.process(iovTime)) {
461 iovMap.insert(std::make_pair(changeTime,
filter.current()->first));
472 for (
auto& im : iovMap) {
486 auto lastAdded =
m_iovs.rbegin()->second;
487 if (lastAdded->fillNumber() != 0) {
492 auto newPayload = std::make_shared<LHCInfo>();
493 m_iovs.insert(std::make_pair(iov, newPayload));
512 std::shared_ptr<LHCInfo>& prevPayload) {
514 for (
auto& iov :
buffer) {
518 if (iovsToTransfer.empty()) {
521 LHCInfo& lastAdded = *iovsToTransfer.rbegin()->second;
529 prevPayload = iov.second;
548 << boost::posix_time::to_iso_extended_string(
550 <<
" ); from " <<
m_name <<
"::getNewObjects";
560 <<
m_name <<
"::getNewObjects";
563 boost::posix_time::ptime executionTime = boost::posix_time::second_clock::local_time();
569 if (lastSince > targetSince)
570 targetSince = lastSince;
589 if (!
tagInfo().lastInterval.payloadId.empty()) {
596 bool iovAdded =
false;
598 if (targetSince >= endIov) {
603 bool updateEcal =
false;
605 boost::posix_time::ptime startSampleTime;
606 boost::posix_time::ptime endSampleTime;
616 bool foundFill =
query->execute();
626 edm::LogInfo(
m_name) <<
"Searching new fill after " << boost::posix_time::to_simple_string(targetTime);
627 query->filterNotNull(
"start_stable_beam").filterNotNull(
"fill_number");
629 query->filterGE(
"start_time", targetTime);
631 query->filterGT(
"start_time", targetTime);
636 query->filterNotNull(
"end_time");
637 bool foundFill =
query->execute();
651 if (endFillTime == 0ULL) {
653 endSampleTime = executionTime;
654 targetSince = endIov;
659 targetSince = endFillTime;
662 getDipData(oms, startSampleTime, endSampleTime);
663 size_t nlumi =
getLumiData(oms, lhcFill, startSampleTime, endSampleTime);
664 edm::LogInfo(
m_name) <<
"Found " << nlumi <<
" lumisections during the fill " << lhcFill;
667 edm::LogInfo(
m_name) <<
"First lumi starts at " << flumiStart <<
" last lumi starts at " << flumiStop;
668 session.transaction().start(
true);
670 session.transaction().commit();
672 getEcalData(session2, startSampleTime, endSampleTime, updateEcal);
edm::ErrorSummaryEntry Error
bool comparePayloads(const LHCInfo &rhs, const LHCInfo &lhs)
std::string const & ctppsStatus() const
float const instLumiError() const
boost::posix_time::ptime m_startTime
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
constexpr bool isNotFinite(T x)
std::unique_ptr< LHCInfo > m_fillPayload
ret
prodAgent to be discontinued
void start(bool readOnly=true)
T from_string(const std::string &attributeValue)
std::string const & lhcComment() const
bool operator()(const cond::Time_t &x, const std::pair< cond::Time_t, std::shared_ptr< LHCInfo >> &y)
bool getEcalData(cond::persistency::Session &session, const boost::posix_time::ptime &lowerTime, const boost::posix_time::ptime &upperTime, bool update)
std::unique_ptr< T > fetchPayload(const cond::Hash &payloadHash)
unsigned short const fillNumber() const
Log< level::Error, false > LogError
bool getCTPPSData(cond::persistency::Session &session, const boost::posix_time::ptime &beginFillTime, const boost::posix_time::ptime &endFillTime)
std::shared_ptr< LHCInfo > m_prevPayload
float const recLumi() const
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
LHCInfo::FillType fillTypeFromString(const std::string &s_fill_type)
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)
float const instLumi() const
Session createSession(const std::string &connectionString, bool writeCapable=false)
float const crossingAngle() const
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
size_t getLumiData(const cond::OMSService &service, unsigned short fillId, const boost::posix_time::ptime &beginFillTime, const boost::posix_time::ptime &endFillTime)
bool makeFillPayload(std::unique_ptr< LHCInfo > &targetPayload, const cond::OMSServiceResult &queryResult)
void addEmptyPayload(cond::Time_t iov)
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)
cond::persistency::Session & dbSession() const
std::unique_ptr< LHCInfo > Ref
void setElementData(cond::Time_t since, const std::string &dipVal, unsigned int elementNr, float value, LHCInfo &payload, std::set< cond::Time_t > &initList)
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[]
static const std::map< std::string, int > vecMap
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
boost::posix_time::ptime to_boost(Time_t iValue)
static size_t const bunchSlots
LHCInfoPopConSourceHandler(const edm::ParameterSet &pset)