6 #include "CoralBase/Attribute.h" 7 #include "CoralBase/AttributeList.h" 8 #include "CoralBase/AttributeSpecification.h" 9 #include "CoralBase/TimeStamp.h" 12 #include "RelationalAccess/ICursor.h" 13 #include "RelationalAccess/IQuery.h" 14 #include "RelationalAccess/ISchema.h" 15 #include "RelationalAccess/ISessionProxy.h" 38 if (s_fill_type ==
i.first)
45 if (s_particle_type ==
i.first)
54 return from_string_impl<LHCInfo::FillType, &fillTypeFromString>(attributeValue,
LHCInfo::UNKNOWN);
59 return from_string_impl<LHCInfo::ParticleType, &particleTypeFromString>(attributeValue,
LHCInfo::NONE);
69 m_samplingInterval((unsigned
int)
pset.getUntrackedParameter<unsigned
int>(
"samplingInterval", 300)),
70 m_endFill(
pset.getUntrackedParameter<
bool>(
"endFill",
true)),
71 m_name(
pset.getUntrackedParameter<
std::
string>(
"name",
"LHCInfoPopConSourceHandler")),
72 m_connectionString(
pset.getUntrackedParameter<
std::
string>(
"connectionString",
"")),
73 m_ecalConnectionString(
pset.getUntrackedParameter<
std::
string>(
"ecalConnectionString",
"")),
74 m_dipSchema(
pset.getUntrackedParameter<
std::
string>(
"DIPSchema",
"")),
75 m_authpath(
pset.getUntrackedParameter<
std::
string>(
"authenticationPath",
"")),
76 m_omsBaseUrl(
pset.getUntrackedParameter<
std::
string>(
"omsBaseUrl",
"")),
80 if (
pset.exists(
"startTime")) {
83 boost::posix_time::ptime
now = boost::posix_time::second_clock::local_time();
85 if (
pset.exists(
"endTime")) {
104 std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfo>>>::const_iterator
search(
106 if (container.empty())
107 return container.end();
109 return (
p != container.begin()) ?
p - 1 : container.end();
114 if (!queryResult.
empty()) {
115 auto row = *queryResult.
begin();
116 auto currentFill = row.get<
unsigned short>(
"fill_number");
117 auto bunches1 = row.get<
unsigned short>(
"bunches_beam1");
118 auto bunches2 = row.get<
unsigned short>(
"bunches_beam2");
119 auto collidingBunches = row.get<
unsigned short>(
"bunches_colliding");
120 auto targetBunches = row.get<
unsigned short>(
"bunches_target");
124 auto intensityBeam1 = row.get<
float>(
"intensity_beam1");
125 auto intensityBeam2 = row.get<
float>(
"intensity_beam2");
126 auto energy = row.get<
float>(
"energy");
127 auto creationTime = row.get<boost::posix_time::ptime>(
"start_time");
128 auto stableBeamStartTime = row.get<boost::posix_time::ptime>(
"start_stable_beam");
129 auto beamDumpTime = row.get<boost::posix_time::ptime>(
"end_time");
130 auto injectionScheme = row.get<
std::string>(
"injection_scheme");
131 targetPayload = std::make_unique<LHCInfo>();
132 targetPayload->setFillNumber(currentFill);
133 targetPayload->setBunchesInBeam1(bunches1);
134 targetPayload->setBunchesInBeam2(bunches2);
135 targetPayload->setCollidingBunches(collidingBunches);
136 targetPayload->setTargetBunches(targetBunches);
137 targetPayload->setFillType(fillType);
138 targetPayload->setParticleTypeForBeam1(particleType1);
139 targetPayload->setParticleTypeForBeam2(particleType2);
140 targetPayload->setIntensityForBeam1(intensityBeam1);
141 targetPayload->setIntensityForBeam2(intensityBeam2);
142 targetPayload->setEnergy(
energy);
146 targetPayload->setInjectionScheme(injectionScheme);
155 unsigned short fillId,
156 const boost::posix_time::ptime& beginFillTime,
157 const boost::posix_time::ptime& endFillTime) {
159 query->addOutputVars({
"start_time",
"delivered_lumi",
"recorded_lumi"});
160 query->filterEQ(
"fill_number", fillId);
161 query->filterGT(
"start_time", beginFillTime).filterLT(
"start_time", endFillTime);
164 if (
query->execute()) {
168 auto lumiTime =
r.get<boost::posix_time::ptime>(
"start_time");
169 auto delivLumi =
r.get<
float>(
"delivered_lumi");
170 auto recLumi =
r.get<
float>(
"recorded_lumi");
174 payload.setDelivLumi(delivLumi);
182 const boost::posix_time::ptime& beginFillTime,
183 const boost::posix_time::ptime& endFillTime) {
186 auto query1 = oms.
query(
"diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam1");
187 query1->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
190 if (query1->execute()) {
191 auto res = query1->result();
193 std::bitset<LHCInfo::bunchSlots + 1> bunchConfiguration1(0ULL);
194 auto row = *
res.begin();
195 auto vbunchConf1 = row.getArray<
unsigned short>(
"value");
196 for (
auto vb : vbunchConf1) {
198 unsigned short slot = (vb - 1) / 10 + 1;
199 bunchConfiguration1[
slot] =
true;
205 auto query2 = oms.
query(
"diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam2");
206 query2->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
208 if (query2->execute()) {
209 auto res = query2->result();
211 std::bitset<LHCInfo::bunchSlots + 1> bunchConfiguration2(0ULL);
212 auto row = *
res.begin();
213 auto vbunchConf2 = row.getArray<
unsigned short>(
"value");
214 for (
auto vb : vbunchConf2) {
216 unsigned short slot = (vb - 1) / 10 + 1;
217 bunchConfiguration2[
slot] =
true;
224 auto query3 = oms.
query(
"diplogger/dip/CMS/LHC/LumiPerBunch");
225 query3->filterGT(
"dip_time", beginFillTime).filterLT(
"dip_time", endFillTime);
227 if (query3->execute()) {
228 auto res = query3->result();
230 std::vector<float> lumiPerBX;
231 auto row = *
res.begin();
232 auto lumiBunchInst = row.getArray<
float>(
"lumi_bunch_inst");
233 for (
auto lb : lumiBunchInst) {
235 lumiPerBX.push_back(lb);
244 const boost::posix_time::ptime& beginFillTime,
245 const boost::posix_time::ptime& endFillTime) {
248 coral::ISchema& CTPPS = session.
coralSession().schema(
"CMS_PPS_SPECT_COND");
250 std::unique_ptr<coral::IQuery> CTPPSDataQuery(CTPPS.newQuery());
252 CTPPSDataQuery->addToTableList(
std::string(
"PPS_LHC_MACHINE_PARAMS"));
254 CTPPSDataQuery->addToOutputList(
std::string(
"DIP_UPDATE_TIME"));
255 CTPPSDataQuery->addToOutputList(
std::string(
"LHC_STATE"));
256 CTPPSDataQuery->addToOutputList(
std::string(
"LHC_COMMENT"));
257 CTPPSDataQuery->addToOutputList(
std::string(
"LUMI_SECTION"));
258 CTPPSDataQuery->addToOutputList(
std::string(
"XING_ANGLE_P5_X_URAD"));
259 CTPPSDataQuery->addToOutputList(
std::string(
"BETA_STAR_P5_X_M"));
261 coral::AttributeList CTPPSDataBindVariables;
262 CTPPSDataBindVariables.extend<coral::TimeStamp>(
std::string(
"beginFillTime"));
263 CTPPSDataBindVariables.extend<coral::TimeStamp>(
std::string(
"endFillTime"));
264 CTPPSDataBindVariables[
std::string(
"beginFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(beginFillTime);
265 CTPPSDataBindVariables[
std::string(
"endFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(endFillTime);
266 std::string conditionStr =
std::string(
"DIP_UPDATE_TIME>= :beginFillTime and DIP_UPDATE_TIME< :endFillTime");
267 CTPPSDataQuery->setCondition(conditionStr, CTPPSDataBindVariables);
269 CTPPSDataQuery->addToOrderList(
std::string(
"DIP_UPDATE_TIME"));
271 coral::AttributeList CTPPSDataOutput;
272 CTPPSDataOutput.extend<coral::TimeStamp>(
std::string(
"DIP_UPDATE_TIME"));
275 CTPPSDataOutput.extend<
int>(
std::string(
"LUMI_SECTION"));
276 CTPPSDataOutput.extend<
float>(
std::string(
"XING_ANGLE_P5_X_URAD"));
277 CTPPSDataOutput.extend<
float>(
std::string(
"BETA_STAR_P5_X_M"));
278 CTPPSDataQuery->defineOutput(CTPPSDataOutput);
280 coral::ICursor& CTPPSDataCursor = CTPPSDataQuery->execute();
282 std::string lhcState =
"", lhcComment =
"", ctppsStatus =
"";
283 unsigned int lumiSection = 0;
284 float crossingAngle = 0., betastar = 0.;
288 while (CTPPSDataCursor.next()) {
290 std::ostringstream CTPPS;
291 CTPPSDataCursor.currentRow().toOutputStream(CTPPS);
293 coral::Attribute
const& dipTimeAttribute = CTPPSDataCursor.currentRow()[
std::string(
"DIP_UPDATE_TIME")];
294 if (!dipTimeAttribute.isNull()) {
296 if (
filter.process(dipTime)) {
298 coral::Attribute
const& lhcStateAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LHC_STATE")];
299 if (!lhcStateAttribute.isNull()) {
302 coral::Attribute
const& lhcCommentAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LHC_COMMENT")];
303 if (!lhcCommentAttribute.isNull()) {
304 lhcComment = lhcCommentAttribute.data<
std::string>();
306 coral::Attribute
const& lumiSectionAttribute = CTPPSDataCursor.currentRow()[
std::string(
"LUMI_SECTION")];
307 if (!lumiSectionAttribute.isNull()) {
308 lumiSection = lumiSectionAttribute.data<
int>();
310 coral::Attribute
const& crossingAngleXAttribute =
311 CTPPSDataCursor.currentRow()[
std::string(
"XING_ANGLE_P5_X_URAD")];
312 if (!crossingAngleXAttribute.isNull()) {
313 crossingAngle = crossingAngleXAttribute.data<
float>();
315 coral::Attribute
const& betaStarXAttribute = CTPPSDataCursor.currentRow()[
std::string(
"BETA_STAR_P5_X_M")];
316 if (!betaStarXAttribute.isNull()) {
317 betastar = betaStarXAttribute.data<
float>();
322 payload.setCrossingAngle(crossingAngle);
325 payload.setLhcComment(lhcComment);
326 payload.setCtppsStatus(ctppsStatus);
327 payload.setLumiSection(lumiSection);
336 static const std::map<std::string, int>
vecMap = {
337 {
"Beam1/beamPhaseMean", 1}, {
"Beam2/beamPhaseMean", 2}, {
"Beam1/cavPhaseMean", 3}, {
"Beam2/cavPhaseMean", 4}};
340 unsigned int elementNr,
343 std::set<cond::Time_t>& initList) {
344 if (initList.find(
since) == initList.end()) {
349 initList.insert(
since);
353 switch (
vecMap.at(dipVal)) {
374 const boost::posix_time::ptime& lowerTime,
375 const boost::posix_time::ptime& upperTime,
382 std::unique_ptr<coral::IQuery> ECALDataQuery(
ECAL.newQuery());
384 ECALDataQuery->addToTableList(
std::string(
"BEAM_PHASE"));
386 ECALDataQuery->addToOutputList(
std::string(
"CHANGE_DATE"));
387 ECALDataQuery->addToOutputList(
std::string(
"DIP_value"));
388 ECALDataQuery->addToOutputList(
std::string(
"element_nr"));
389 ECALDataQuery->addToOutputList(
std::string(
"VALUE_NUMBER"));
391 coral::AttributeList ECALDataBindVariables;
392 ECALDataBindVariables.extend<coral::TimeStamp>(
std::string(
"lowerTime"));
393 ECALDataBindVariables.extend<coral::TimeStamp>(
std::string(
"upperTime"));
394 ECALDataBindVariables[
std::string(
"lowerTime")].data<coral::TimeStamp>() = coral::TimeStamp(lowerTime);
395 ECALDataBindVariables[
std::string(
"upperTime")].data<coral::TimeStamp>() = coral::TimeStamp(upperTime);
397 "(DIP_value LIKE '%beamPhaseMean%' OR DIP_value LIKE '%cavPhaseMean%') AND CHANGE_DATE >= :lowerTime AND " 398 "CHANGE_DATE < :upperTime");
400 ECALDataQuery->setCondition(conditionStr, ECALDataBindVariables);
402 ECALDataQuery->addToOrderList(
std::string(
"CHANGE_DATE"));
403 ECALDataQuery->addToOrderList(
std::string(
"DIP_value"));
404 ECALDataQuery->addToOrderList(
std::string(
"element_nr"));
406 coral::AttributeList ECALDataOutput;
407 ECALDataOutput.extend<coral::TimeStamp>(
std::string(
"CHANGE_DATE"));
409 ECALDataOutput.extend<
unsigned int>(
std::string(
"element_nr"));
410 ECALDataOutput.extend<
float>(
std::string(
"VALUE_NUMBER"));
412 ECALDataQuery->defineOutput(ECALDataOutput);
414 coral::ICursor& ECALDataCursor = ECALDataQuery->execute();
418 unsigned int elementNr = 0;
420 std::set<cond::Time_t> initializedVectors;
431 std::map<cond::Time_t, cond::Time_t> iovMap;
433 while (ECALDataCursor.next()) {
435 std::ostringstream
ECAL;
436 ECALDataCursor.currentRow().toOutputStream(
ECAL);
438 coral::Attribute
const& changeDateAttribute = ECALDataCursor.currentRow()[
std::string(
"CHANGE_DATE")];
439 if (!changeDateAttribute.isNull()) {
441 boost::posix_time::ptime chTime = changeDateAttribute.data<coral::TimeStamp>().
time();
443 if (changeTime == 0) {
448 if (!
update and changeTime == firstTime)
450 coral::Attribute
const& dipValAttribute = ECALDataCursor.currentRow()[
std::string(
"DIP_value")];
451 coral::Attribute
const& valueNumberAttribute = ECALDataCursor.currentRow()[
std::string(
"VALUE_NUMBER")];
452 coral::Attribute
const& elementNrAttribute = ECALDataCursor.currentRow()[
std::string(
"element_nr")];
453 if (!dipValAttribute.isNull() and !valueNumberAttribute.isNull()) {
455 elementNr = elementNrAttribute.data<
unsigned int>();
456 value = valueNumberAttribute.data<
float>();
459 if (
filter.process(iovTime)) {
460 iovMap.insert(std::make_pair(changeTime,
filter.current()->first));
471 for (
auto& im : iovMap) {
485 auto lastAdded =
m_iovs.rbegin()->second;
486 if (lastAdded->fillNumber() != 0) {
491 auto newPayload = std::make_shared<LHCInfo>();
492 m_iovs.insert(std::make_pair(iov, newPayload));
511 std::shared_ptr<LHCInfo>& prevPayload) {
513 for (
auto& iov :
buffer) {
517 if (iovsToTransfer.empty()) {
520 LHCInfo& lastAdded = *iovsToTransfer.rbegin()->second;
528 prevPayload = iov.second;
547 << boost::posix_time::to_iso_extended_string(
549 <<
" ); from " <<
m_name <<
"::getNewObjects";
559 <<
m_name <<
"::getNewObjects";
562 boost::posix_time::ptime executionTime = boost::posix_time::second_clock::local_time();
568 if (lastSince > targetSince)
569 targetSince = lastSince;
588 if (!
tagInfo().lastInterval.payloadId.empty()) {
595 bool iovAdded =
false;
597 if (targetSince >= endIov) {
602 bool updateEcal =
false;
604 boost::posix_time::ptime startSampleTime;
605 boost::posix_time::ptime endSampleTime;
615 bool foundFill =
query->execute();
625 edm::LogInfo(
m_name) <<
"Searching new fill after " << boost::posix_time::to_simple_string(targetTime);
626 query->filterNotNull(
"start_stable_beam").filterNotNull(
"fill_number");
628 query->filterGE(
"start_time", targetTime);
630 query->filterGT(
"start_time", targetTime);
635 query->filterNotNull(
"end_time");
636 bool foundFill =
query->execute();
650 if (endFillTime == 0ULL) {
652 endSampleTime = executionTime;
653 targetSince = endIov;
658 targetSince = endFillTime;
661 getDipData(oms, startSampleTime, endSampleTime);
662 size_t nlumi =
getLumiData(oms, lhcFill, startSampleTime, endSampleTime);
663 edm::LogInfo(
m_name) <<
"Found " << nlumi <<
" lumisections during the fill " << lhcFill;
666 edm::LogInfo(
m_name) <<
"First lumi starts at " << flumiStart <<
" last lumi starts at " << flumiStop;
671 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
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)
coral::ISchema & nominalSchema()
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
coral::ISessionProxy & coralSession()
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)