Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-07 06:12:39

0001 #include "CondTools/RunInfo/interface/LHCInfoPerFillPopConSourceHandler.h"
0002 #include "CondCore/CondDB/interface/ConnectionPool.h"
0003 #include "CondFormats/Common/interface/TimeConversions.h"
0004 #include "CondFormats/RunInfo/interface/LHCInfoPerFill.h"
0005 #include "CondTools/RunInfo/interface/LumiSectionFilter.h"
0006 #include "CondTools/RunInfo/interface/LHCInfoHelper.h"
0007 #include "CondTools/RunInfo/interface/OMSAccess.h"
0008 #include "CoralBase/Attribute.h"
0009 #include "CoralBase/AttributeList.h"
0010 #include "CoralBase/AttributeSpecification.h"
0011 #include "CoralBase/TimeStamp.h"
0012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "RelationalAccess/ICursor.h"
0015 #include "RelationalAccess/IQuery.h"
0016 #include "RelationalAccess/ISchema.h"
0017 #include "RelationalAccess/ISessionProxy.h"
0018 
0019 using std::make_pair;
0020 using std::pair;
0021 
0022 namespace cond {
0023   namespace theLHCInfoPerFillPopConImpl {
0024 
0025     static const pair<const char*, LHCInfoPerFill::FillType> s_fillTypeMap[] = {
0026         make_pair("PROTONS", LHCInfoPerFill::PROTONS),
0027         make_pair("IONS", LHCInfoPerFill::IONS),
0028         make_pair("COSMICS", LHCInfoPerFill::COSMICS),
0029         make_pair("GAP", LHCInfoPerFill::GAP)};
0030 
0031     static const pair<const char*, LHCInfoPerFill::ParticleType> s_particleTypeMap[] = {
0032         make_pair("PROTON", LHCInfoPerFill::PROTON),
0033         make_pair("PB82", LHCInfoPerFill::PB82),
0034         make_pair("AR18", LHCInfoPerFill::AR18),
0035         make_pair("D", LHCInfoPerFill::D),
0036         make_pair("XE54", LHCInfoPerFill::XE54)};
0037 
0038     LHCInfoPerFill::FillType fillTypeFromString(const std::string& s_fill_type) {
0039       for (auto const& i : s_fillTypeMap)
0040         if (s_fill_type == i.first)
0041           return i.second;
0042       return LHCInfoPerFill::UNKNOWN;
0043     }
0044 
0045     LHCInfoPerFill::ParticleType particleTypeFromString(const std::string& s_particle_type) {
0046       for (auto const& i : s_particleTypeMap)
0047         if (s_particle_type == i.first)
0048           return i.second;
0049       return LHCInfoPerFill::NONE;
0050     }
0051   }  // namespace theLHCInfoPerFillPopConImpl
0052 
0053   namespace impl {
0054 
0055     template <>
0056     LHCInfoPerFill::FillType from_string(const std::string& attributeValue) {
0057       return from_string_impl<LHCInfoPerFill::FillType, &theLHCInfoPerFillPopConImpl::fillTypeFromString>(
0058           attributeValue, LHCInfoPerFill::UNKNOWN);
0059     }
0060 
0061     template <>
0062     LHCInfoPerFill::ParticleType from_string(const std::string& attributeValue) {
0063       return from_string_impl<LHCInfoPerFill::ParticleType, &theLHCInfoPerFillPopConImpl::particleTypeFromString>(
0064           attributeValue, LHCInfoPerFill::NONE);
0065     }
0066 
0067   }  // namespace impl
0068 }  // namespace cond
0069 
0070 namespace theLHCInfoPerFillImpl {
0071 
0072   bool makeFillPayload(std::unique_ptr<LHCInfoPerFill>& targetPayload, const cond::OMSServiceResult& queryResult) {
0073     bool ret = false;
0074     if (!queryResult.empty()) {
0075       auto row = *queryResult.begin();
0076       auto currentFill = row.get<unsigned short>("fill_number");
0077       auto bunches1 = row.get<unsigned short>("bunches_beam1");
0078       auto bunches2 = row.get<unsigned short>("bunches_beam2");
0079       auto collidingBunches = row.get<unsigned short>("bunches_colliding");
0080       auto targetBunches = row.get<unsigned short>("bunches_target");
0081       auto fillType = row.get<LHCInfoPerFill::FillType>("fill_type_runtime");
0082       auto particleType1 = row.get<LHCInfoPerFill::ParticleType>("fill_type_party1");
0083       auto particleType2 = row.get<LHCInfoPerFill::ParticleType>("fill_type_party2");
0084       auto intensityBeam1 = row.get<float>("intensity_beam1");
0085       auto intensityBeam2 = row.get<float>("intensity_beam2");
0086       auto energy = row.get<float>("energy");
0087       auto creationTime = row.get<boost::posix_time::ptime>("start_time");
0088       auto stableBeamStartTime = row.get<boost::posix_time::ptime>("start_stable_beam");
0089       std::string endTimeStr = row.get<std::string>("end_time");
0090       auto beamDumpTime =
0091           (endTimeStr == "null") ? 0 : cond::time::from_boost(row.get<boost::posix_time::ptime>("end_time"));
0092       auto injectionScheme = row.get<std::string>("injection_scheme");
0093       targetPayload = std::make_unique<LHCInfoPerFill>();
0094       targetPayload->setFillNumber(currentFill);
0095       targetPayload->setBunchesInBeam1(bunches1);
0096       targetPayload->setBunchesInBeam2(bunches2);
0097       targetPayload->setCollidingBunches(collidingBunches);
0098       targetPayload->setTargetBunches(targetBunches);
0099       targetPayload->setFillType(fillType);
0100       targetPayload->setParticleTypeForBeam1(particleType1);
0101       targetPayload->setParticleTypeForBeam2(particleType2);
0102       targetPayload->setIntensityForBeam1(intensityBeam1);
0103       targetPayload->setIntensityForBeam2(intensityBeam2);
0104       targetPayload->setEnergy(energy);
0105       targetPayload->setCreationTime(cond::time::from_boost(creationTime));
0106       targetPayload->setBeginTime(cond::time::from_boost(stableBeamStartTime));
0107       targetPayload->setEndTime(beamDumpTime);
0108       targetPayload->setInjectionScheme(injectionScheme);
0109       ret = true;
0110     }
0111     return ret;
0112   }
0113 }  // namespace theLHCInfoPerFillImpl
0114 
0115 namespace theLHCInfoPerFillImpl {
0116   static const std::map<std::string, int> vecMap = {
0117       {"Beam1/beamPhaseMean", 1}, {"Beam2/beamPhaseMean", 2}, {"Beam1/cavPhaseMean", 3}, {"Beam2/cavPhaseMean", 4}};
0118   void setElementData(cond::Time_t since,
0119                       const std::string& dipVal,
0120                       unsigned int elementNr,
0121                       float value,
0122                       LHCInfoPerFill& payload,
0123                       std::set<cond::Time_t>& initList) {
0124     if (initList.find(since) == initList.end()) {
0125       payload.beam1VC().resize(LHCInfoPerFill::bunchSlots, 0.);
0126       payload.beam2VC().resize(LHCInfoPerFill::bunchSlots, 0.);
0127       payload.beam1RF().resize(LHCInfoPerFill::bunchSlots, 0.);
0128       payload.beam2RF().resize(LHCInfoPerFill::bunchSlots, 0.);
0129       initList.insert(since);
0130     }
0131     // set the current values to all of the payloads of the lumi section samples after the current since
0132     if (elementNr < LHCInfoPerFill::bunchSlots) {
0133       switch (vecMap.at(dipVal)) {
0134         case 1:
0135           payload.beam1VC()[elementNr] = value;
0136           break;
0137         case 2:
0138           payload.beam2VC()[elementNr] = value;
0139           break;
0140         case 3:
0141           payload.beam1RF()[elementNr] = value;
0142           break;
0143         case 4:
0144           payload.beam2RF()[elementNr] = value;
0145           break;
0146         default:
0147           break;
0148       }
0149     }
0150   }
0151 }  // namespace theLHCInfoPerFillImpl
0152 
0153 namespace theLHCInfoPerFillImpl {
0154   bool comparePayloads(const LHCInfoPerFill& rhs, const LHCInfoPerFill& lhs) {
0155     if (rhs.fillNumber() != lhs.fillNumber() || rhs.delivLumi() != lhs.delivLumi() || rhs.recLumi() != lhs.recLumi() ||
0156         rhs.instLumi() != lhs.instLumi() || rhs.instLumiError() != lhs.instLumiError() ||
0157         rhs.lhcState() != lhs.lhcState() || rhs.lhcComment() != lhs.lhcComment() ||
0158         rhs.ctppsStatus() != lhs.ctppsStatus()) {
0159       return false;
0160     }
0161     return true;
0162   }
0163 
0164   size_t transferPayloads(const std::vector<pair<cond::Time_t, std::shared_ptr<LHCInfoPerFill>>>& buffer,
0165                           std::map<cond::Time_t, std::shared_ptr<LHCInfoPerFill>>& iovsToTransfer,
0166                           std::shared_ptr<LHCInfoPerFill>& prevPayload) {
0167     size_t niovs = 0;
0168     std::stringstream condIovs;
0169     std::stringstream formattedIovs;
0170     for (auto& iov : buffer) {
0171       bool add = false;
0172       auto payload = iov.second;
0173       cond::Time_t since = iov.first;
0174       if (iovsToTransfer.empty()) {
0175         add = true;
0176       } else {
0177         LHCInfoPerFill& lastAdded = *iovsToTransfer.rbegin()->second;
0178         if (!comparePayloads(lastAdded, *payload)) {
0179           add = true;
0180         }
0181       }
0182       if (add) {
0183         niovs++;
0184         condIovs << since << " ";
0185         formattedIovs << boost::posix_time::to_iso_extended_string(cond::time::to_boost(since)) << " ";
0186         iovsToTransfer.insert(make_pair(since, payload));
0187         prevPayload = iov.second;
0188       }
0189     }
0190     edm::LogInfo("transferPayloads") << "TRANSFERED IOVS: " << condIovs.str();
0191     edm::LogInfo("transferPayloads") << "FORMATTED TRANSFERED IOVS: " << formattedIovs.str();
0192     return niovs;
0193   }
0194 
0195 }  // namespace theLHCInfoPerFillImpl
0196 
0197 LHCInfoPerFillPopConSourceHandler::LHCInfoPerFillPopConSourceHandler(edm::ParameterSet const& pset)
0198     : m_debug(pset.getUntrackedParameter<bool>("debug", false)),
0199       m_startTime(),
0200       m_endTime(),
0201       m_endFillMode(pset.getUntrackedParameter<bool>("endFill", true)),
0202       m_name(pset.getUntrackedParameter<std::string>("name", "LHCInfoPerFillPopConSourceHandler")),
0203       m_connectionString(pset.getUntrackedParameter<std::string>("connectionString", "")),
0204       m_ecalConnectionString(pset.getUntrackedParameter<std::string>("ecalConnectionString", "")),
0205       m_authpath(pset.getUntrackedParameter<std::string>("authenticationPath", "")),
0206       m_omsBaseUrl(pset.getUntrackedParameter<std::string>("omsBaseUrl", "")),
0207       m_fillPayload(),
0208       m_prevPayload(),
0209       m_tmpBuffer() {
0210   if (!pset.getUntrackedParameter<std::string>("startTime").empty()) {
0211     m_startTime = boost::posix_time::time_from_string(pset.getUntrackedParameter<std::string>("startTime"));
0212   }
0213   boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
0214   m_endTime = now;
0215   if (!pset.getUntrackedParameter<std::string>("endTime").empty()) {
0216     m_endTime = boost::posix_time::time_from_string(pset.getUntrackedParameter<std::string>("endTime"));
0217     if (m_endTime > now)
0218       m_endTime = now;
0219   }
0220 }
0221 
0222 void LHCInfoPerFillPopConSourceHandler::getNewObjects() {
0223   //if a new tag is created, transfer fake fill from 1 to the first fill for the first time
0224   if (tagInfo().size == 0) {
0225     edm::LogInfo(m_name) << "New tag " << tagInfo().name << "; from " << m_name << "::getNewObjects";
0226   } else {
0227     //check what is already inside the database
0228     edm::LogInfo(m_name) << "got info for tag " << tagInfo().name << ": size " << tagInfo().size
0229                          << ", last object valid since " << tagInfo().lastInterval.since << " ( "
0230                          << boost::posix_time::to_iso_extended_string(
0231                                 cond::time::to_boost(tagInfo().lastInterval.since))
0232                          << " ); from " << m_name << "::getNewObjects";
0233   }
0234 
0235   cond::Time_t lastSince = tagInfo().lastInterval.since;
0236   if (tagInfo().isEmpty()) {
0237     // for a new or empty tag in endFill mode, an empty payload should be added on top with since=1
0238     addEmptyPayload(1);
0239     lastSince = 1;
0240     if (!m_endFillMode) {
0241       edm::LogInfo(m_name) << "Empty or new tag: uploading a default payload and ending the job";
0242       return;
0243     }
0244   } else {
0245     edm::LogInfo(m_name) << "The last Iov in tag " << tagInfo().name << " valid since " << lastSince << "from "
0246                          << m_name << "::getNewObjects";
0247   }
0248 
0249   //retrieve the data from the relational database source
0250   cond::persistency::ConnectionPool connection;
0251   //configure the connection
0252   if (m_debug) {
0253     connection.setMessageVerbosity(coral::Debug);
0254   } else {
0255     connection.setMessageVerbosity(coral::Error);
0256   }
0257   connection.setAuthenticationPath(m_authpath);
0258   connection.configure();
0259   //create the sessions
0260   cond::persistency::Session session = connection.createSession(m_connectionString, false);
0261   cond::persistency::Session session2 = connection.createSession(m_ecalConnectionString, false);
0262   // fetch last payload when available
0263   if (!tagInfo().lastInterval.payloadId.empty()) {
0264     cond::persistency::Session session3 = dbSession();
0265     session3.transaction().start(true);
0266     m_prevPayload = session3.fetchPayload<LHCInfoPerFill>(tagInfo().lastInterval.payloadId);
0267     session3.transaction().commit();
0268   }
0269 
0270   boost::posix_time::ptime executionTime = boost::posix_time::second_clock::local_time();
0271   cond::Time_t executionTimeIov = cond::time::from_boost(executionTime);
0272 
0273   cond::Time_t startTimestamp = m_startTime.is_not_a_date_time() ? 0 : cond::time::from_boost(m_startTime);
0274   cond::Time_t nextFillSearchTimestamp =
0275       std::max(startTimestamp, m_endFillMode ? lastSince : m_prevPayload->createTime());
0276 
0277   edm::LogInfo(m_name) << "Starting sampling at "
0278                        << boost::posix_time::to_simple_string(cond::time::to_boost(nextFillSearchTimestamp));
0279 
0280   while (true) {
0281     if (nextFillSearchTimestamp >= executionTimeIov) {
0282       edm::LogInfo(m_name) << "Sampling ended at the time "
0283                            << boost::posix_time::to_simple_string(cond::time::to_boost(executionTimeIov));
0284       break;
0285     }
0286     boost::posix_time::ptime nextFillSearchTime = cond::time::to_boost(nextFillSearchTimestamp);
0287     boost::posix_time::ptime startSampleTime;
0288     boost::posix_time::ptime endSampleTime;
0289 
0290     cond::OMSService oms;
0291     oms.connect(m_omsBaseUrl);
0292     auto query = oms.query("fills");
0293 
0294     edm::LogInfo(m_name) << "Searching new fill after " << boost::posix_time::to_simple_string(nextFillSearchTime);
0295     query->filterNotNull("start_stable_beam").filterNotNull("fill_number");
0296     if (nextFillSearchTime > cond::time::to_boost(m_prevPayload->createTime())) {
0297       query->filterGE("start_time", nextFillSearchTime);
0298     } else {
0299       query->filterGT("start_time", nextFillSearchTime);
0300     }
0301 
0302     query->filterLT("start_time", m_endTime);
0303     if (m_endFillMode)
0304       query->filterNotNull("end_time");
0305     else
0306       query->filterEQ("end_time", cond::OMSServiceQuery::SNULL);
0307 
0308     bool foundFill = query->execute();
0309     if (foundFill)
0310       foundFill = theLHCInfoPerFillImpl::makeFillPayload(m_fillPayload, query->result());
0311     if (!foundFill) {
0312       edm::LogInfo(m_name) << "No fill found - END of job.";
0313       break;
0314     }
0315 
0316     startSampleTime = cond::time::to_boost(m_fillPayload->createTime());
0317     cond::Time_t startFillTime = m_fillPayload->createTime();
0318     cond::Time_t endFillTime = m_fillPayload->endTime();
0319     unsigned short lhcFill = m_fillPayload->fillNumber();
0320     bool ongoingFill = endFillTime == 0ULL;
0321     if (ongoingFill) {
0322       edm::LogInfo(m_name) << "Found ongoing fill " << lhcFill << " created at " << cond::time::to_boost(startFillTime);
0323       endSampleTime = executionTime;
0324       nextFillSearchTimestamp = executionTimeIov;
0325     } else {
0326       edm::LogInfo(m_name) << "Found fill " << lhcFill << " created at " << cond::time::to_boost(startFillTime)
0327                            << " ending at " << cond::time::to_boost(endFillTime);
0328       endSampleTime = cond::time::to_boost(endFillTime);
0329       nextFillSearchTimestamp = endFillTime;
0330     }
0331     if (m_endFillMode || ongoingFill) {
0332       getDipData(oms, startSampleTime, endSampleTime);
0333       getLumiData(oms, lhcFill, startSampleTime, endSampleTime);
0334       if (!m_tmpBuffer.empty()) {
0335         boost::posix_time::ptime flumiStart = cond::time::to_boost(m_tmpBuffer.front().first);
0336         boost::posix_time::ptime flumiStop = cond::time::to_boost(m_tmpBuffer.back().first);
0337         edm::LogInfo(m_name) << "First lumi starts at " << flumiStart << " last lumi starts at " << flumiStop;
0338         session.transaction().start(true);
0339         getCTPPSData(session, startSampleTime, endSampleTime);
0340         session.transaction().commit();
0341         session2.transaction().start(true);
0342         getEcalData(session2, startSampleTime, endSampleTime);
0343         session2.transaction().commit();
0344       }
0345     }
0346 
0347     if (!m_endFillMode) {
0348       if (m_tmpBuffer.size() > 1) {
0349         throw cms::Exception("LHCInfoPerFillPopConSourceHandler")
0350             << "More than 1 payload buffered for writing in duringFill mode.\
0351           In this mode only up to 1 payload can be written";
0352       } else if (m_tmpBuffer.size() == 1) {
0353         if (theLHCInfoPerFillImpl::comparePayloads(*(m_tmpBuffer.begin()->second), *m_prevPayload)) {
0354           m_tmpBuffer.clear();
0355           edm::LogInfo(m_name)
0356               << "The buffered payload has the same data as the previous payload in the tag. It will not be written.";
0357         }
0358       } else if (m_tmpBuffer.empty()) {
0359         addEmptyPayload(
0360             cond::lhcInfoHelper::getFillLastLumiIOV(oms, lhcFill));  //the IOV doesn't matter when using OnlinePopCon
0361       }
0362       // In duringFill mode, convert the timestamp-type IOVs to lumiid-type IOVs
0363       // before transferring the payloads from the buffer to the final collection
0364       convertBufferedIovsToLumiid(m_timestampToLumiid);
0365     }
0366 
0367     size_t niovs = theLHCInfoPerFillImpl::transferPayloads(m_tmpBuffer, m_iovs, m_prevPayload);
0368     edm::LogInfo(m_name) << "Added " << niovs << " iovs within the Fill time";
0369     m_tmpBuffer.clear();
0370     m_timestampToLumiid.clear();
0371 
0372     if (!m_endFillMode) {
0373       return;
0374     }
0375 
0376     // endFill mode only:
0377     if (m_prevPayload->fillNumber() and !ongoingFill) {
0378       if (m_endFillMode) {
0379         addEmptyPayload(endFillTime);
0380       } else {
0381         addEmptyPayload(cond::lhcInfoHelper::getFillLastLumiIOV(oms, lhcFill));
0382       }
0383     }
0384   }
0385 }
0386 
0387 std::string LHCInfoPerFillPopConSourceHandler::id() const { return m_name; }
0388 
0389 void LHCInfoPerFillPopConSourceHandler::addEmptyPayload(cond::Time_t iov) {
0390   bool add = false;
0391   if (m_iovs.empty()) {
0392     if (!m_lastPayloadEmpty)
0393       add = true;
0394   } else {
0395     auto lastAdded = m_iovs.rbegin()->second;
0396     if (lastAdded->fillNumber() != 0) {
0397       add = true;
0398     }
0399   }
0400   if (add) {
0401     auto newPayload = std::make_shared<LHCInfoPerFill>();
0402     m_iovs.insert(make_pair(iov, newPayload));
0403     m_prevPayload = newPayload;
0404     edm::LogInfo(m_name) << "Added empty payload with IOV " << iov << " ( "
0405                          << boost::posix_time::to_iso_extended_string(cond::time::to_boost(iov)) << " )";
0406   }
0407 }
0408 
0409 // Add payload to buffer and store corresponding lumiid IOV in m_timestampToLumiid map
0410 void LHCInfoPerFillPopConSourceHandler::addPayloadToBuffer(cond::OMSServiceResultRef& row) {
0411   auto startTime = row.get<boost::posix_time::ptime>("start_time");
0412   auto delivLumi = row.get<float>("delivered_lumi");
0413   auto recLumi = row.get<float>("recorded_lumi");
0414   auto runNumber = std::stoul(row.get<std::string>("run_number"));
0415   auto lsNumber = std::stoul(row.get<std::string>("lumisection_number"));
0416   auto lumiid = cond::time::lumiTime(runNumber, lsNumber);
0417 
0418   LHCInfoPerFill* thisLumiSectionInfo = m_fillPayload->cloneFill();
0419   m_tmpBuffer.emplace_back(make_pair(cond::time::from_boost(startTime), thisLumiSectionInfo));
0420   if (!m_endFillMode) {
0421     m_timestampToLumiid.insert(make_pair(cond::time::from_boost(startTime), lumiid));
0422   }
0423   LHCInfoPerFill& payload = *thisLumiSectionInfo;
0424   payload.setDelivLumi(delivLumi);
0425   payload.setRecLumi(recLumi);
0426 }
0427 
0428 void LHCInfoPerFillPopConSourceHandler::convertBufferedIovsToLumiid(
0429     std::map<cond::Time_t, cond::Time_t> timestampToLumiid) {
0430   for (auto& item : m_tmpBuffer) {
0431     // Check if the lumiid IOV corresponding to the timestamp is present in the map
0432     if (timestampToLumiid.find(item.first) == timestampToLumiid.end()) {
0433       throw cms::Exception("LHCInfoPerFillPopConSourceHandler")
0434           << "Can't find corresponding lumiid IOV for timestamp " << item.first << "\n";
0435     }
0436     // Update the buffer with the lumiid-type IOV
0437     item.first = timestampToLumiid.at(item.first);
0438   }
0439 }
0440 
0441 size_t LHCInfoPerFillPopConSourceHandler::getLumiData(const cond::OMSService& oms,
0442                                                       unsigned short fillId,
0443                                                       const boost::posix_time::ptime& beginFillTime,
0444                                                       const boost::posix_time::ptime& endFillTime) {
0445   auto query = oms.query("lumisections");
0446   query->addOutputVars(
0447       {"start_time", "delivered_lumi", "recorded_lumi", "beams_stable", "run_number", "lumisection_number"});
0448   query->filterEQ("fill_number", fillId);
0449   query->filterGT("start_time", beginFillTime).filterLT("start_time", endFillTime);
0450   query->filterEQ("beams_stable", "true");
0451   query->limit(cond::lhcInfoHelper::kLumisectionsQueryLimit);
0452   if (query->execute()) {
0453     auto queryResult = query->result();
0454     edm::LogInfo(m_name) << "Found " << queryResult.size() << " lumisections with STABLE BEAM during the fill "
0455                          << fillId;
0456 
0457     if (!queryResult.empty()) {
0458       if (m_endFillMode) {
0459         auto firstRow = queryResult.front();
0460         addPayloadToBuffer(firstRow);
0461       }
0462 
0463       auto lastRow = queryResult.back();
0464       addPayloadToBuffer(lastRow);
0465     }
0466   }
0467   return 0;
0468 }
0469 
0470 void LHCInfoPerFillPopConSourceHandler::getDipData(const cond::OMSService& oms,
0471                                                    const boost::posix_time::ptime& beginFillTime,
0472                                                    const boost::posix_time::ptime& endFillTime) {
0473   // unsure how to handle this.
0474   // the old implementation is not helping: apparently it is checking only the bunchconfiguration for the first diptime set of values...
0475   auto query1 = oms.query("diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam1");
0476   query1->filterGT("dip_time", beginFillTime).filterLT("dip_time", endFillTime);
0477   //This query is limited to 100 rows, but currently only one is used
0478   //If all this data is needed and saved properly the limit has to be set: query1->limit(...)
0479   if (query1->execute()) {
0480     auto res = query1->result();
0481     if (!res.empty()) {
0482       std::bitset<LHCInfoPerFill::bunchSlots + 1> bunchConfiguration1(0ULL);
0483       auto row = *res.begin();
0484       auto vbunchConf1 = row.getArray<unsigned short>("value");
0485       for (auto vb : vbunchConf1) {
0486         if (vb != 0) {
0487           unsigned short slot = (vb - 1) / 10 + 1;
0488           bunchConfiguration1[slot] = true;
0489         }
0490       }
0491       m_fillPayload->setBunchBitsetForBeam1(bunchConfiguration1);
0492     }
0493   }
0494   auto query2 = oms.query("diplogger/dip/acc/LHC/RunControl/CirculatingBunchConfig/Beam2");
0495   query2->filterGT("dip_time", beginFillTime).filterLT("dip_time", endFillTime);
0496   //This query is limited to 100 rows, but currently only one is used
0497   if (query2->execute()) {
0498     auto res = query2->result();
0499     if (!res.empty()) {
0500       std::bitset<LHCInfoPerFill::bunchSlots + 1> bunchConfiguration2(0ULL);
0501       auto row = *res.begin();
0502       auto vbunchConf2 = row.getArray<unsigned short>("value");
0503       for (auto vb : vbunchConf2) {
0504         if (vb != 0) {
0505           unsigned short slot = (vb - 1) / 10 + 1;
0506           bunchConfiguration2[slot] = true;
0507         }
0508       }
0509       m_fillPayload->setBunchBitsetForBeam2(bunchConfiguration2);
0510     }
0511   }
0512 
0513   auto query3 = oms.query("diplogger/dip/CMS/LHC/LumiPerBunch");
0514   query3->filterGT("dip_time", beginFillTime).filterLT("dip_time", endFillTime);
0515   //This query is limited to 100 rows, but currently only one is used
0516   if (query3->execute()) {
0517     auto res = query3->result();
0518     if (!res.empty()) {
0519       std::vector<float> lumiPerBX;
0520       auto row = *res.begin();
0521       auto lumiBunchInst = row.getArray<float>("lumi_bunch_inst");
0522       for (auto lb : lumiBunchInst) {
0523         if (lb != 0.) {
0524           lumiPerBX.push_back(lb);
0525         }
0526       }
0527       m_fillPayload->setLumiPerBX(lumiPerBX);
0528     }
0529   }
0530 }
0531 
0532 bool LHCInfoPerFillPopConSourceHandler::getCTPPSData(cond::persistency::Session& session,
0533                                                      const boost::posix_time::ptime& beginFillTime,
0534                                                      const boost::posix_time::ptime& endFillTime) {
0535   //run the fifth query against the CTPPS schema
0536   //Initializing the CMS_CTP_CTPPS_COND schema.
0537   coral::ISchema& CTPPS = session.coralSession().schema("CMS_PPS_SPECT_COND");
0538   //execute query for CTPPS Data
0539   std::unique_ptr<coral::IQuery> CTPPSDataQuery(CTPPS.newQuery());
0540   //FROM clause
0541   CTPPSDataQuery->addToTableList(std::string("PPS_LHC_MACHINE_PARAMS"));
0542   //SELECT clause
0543   CTPPSDataQuery->addToOutputList(std::string("DIP_UPDATE_TIME"));
0544   CTPPSDataQuery->addToOutputList(std::string("LHC_STATE"));
0545   CTPPSDataQuery->addToOutputList(std::string("LHC_COMMENT"));
0546   if (m_debug) {
0547     CTPPSDataQuery->addToOutputList(std::string("RUN_NUMBER"));
0548     CTPPSDataQuery->addToOutputList(std::string("LUMI_SECTION"));
0549   }
0550   //WHERE CLAUSE
0551   coral::AttributeList CTPPSDataBindVariables;
0552   CTPPSDataBindVariables.extend<coral::TimeStamp>(std::string("beginFillTime"));
0553   CTPPSDataBindVariables.extend<coral::TimeStamp>(std::string("endFillTime"));
0554   CTPPSDataBindVariables[std::string("beginFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(beginFillTime);
0555   CTPPSDataBindVariables[std::string("endFillTime")].data<coral::TimeStamp>() = coral::TimeStamp(endFillTime);
0556   std::string conditionStr = std::string("DIP_UPDATE_TIME>= :beginFillTime and DIP_UPDATE_TIME< :endFillTime");
0557   CTPPSDataQuery->setCondition(conditionStr, CTPPSDataBindVariables);
0558   //ORDER BY clause
0559   CTPPSDataQuery->addToOrderList(std::string("DIP_UPDATE_TIME"));
0560   //define query output
0561   coral::AttributeList CTPPSDataOutput;
0562   CTPPSDataOutput.extend<coral::TimeStamp>(std::string("DIP_UPDATE_TIME"));
0563   CTPPSDataOutput.extend<std::string>(std::string("LHC_STATE"));
0564   CTPPSDataOutput.extend<std::string>(std::string("LHC_COMMENT"));
0565   if (m_debug) {
0566     CTPPSDataOutput.extend<int>(std::string("RUN_NUMBER"));
0567     CTPPSDataOutput.extend<int>(std::string("LUMI_SECTION"));
0568   }
0569   CTPPSDataQuery->defineOutput(CTPPSDataOutput);
0570   //execute the query
0571   coral::ICursor& CTPPSDataCursor = CTPPSDataQuery->execute();
0572   cond::Time_t dipTime = 0;
0573   std::string lhcState = "", lhcComment = "", ctppsStatus = "";
0574 
0575   //debug informations
0576   unsigned int lumiSection = 0;
0577   cond::Time_t runNumber = 0;
0578   cond::Time_t savedDipTime = 0;
0579   unsigned int savedLumiSection = 0;
0580   cond::Time_t savedRunNumber = 0;
0581 
0582   bool ret = false;
0583   LumiSectionFilter<LHCInfoPerFill> filter(m_tmpBuffer);
0584   while (CTPPSDataCursor.next()) {
0585     if (m_debug) {
0586       std::ostringstream CTPPS;
0587       CTPPSDataCursor.currentRow().toOutputStream(CTPPS);
0588     }
0589     coral::Attribute const& dipTimeAttribute = CTPPSDataCursor.currentRow()[std::string("DIP_UPDATE_TIME")];
0590     if (!dipTimeAttribute.isNull()) {
0591       dipTime = cond::time::from_boost(dipTimeAttribute.data<coral::TimeStamp>().time());
0592       if (filter.process(dipTime)) {
0593         ret = true;
0594         coral::Attribute const& lhcStateAttribute = CTPPSDataCursor.currentRow()[std::string("LHC_STATE")];
0595         if (!lhcStateAttribute.isNull()) {
0596           lhcState = lhcStateAttribute.data<std::string>();
0597         }
0598         coral::Attribute const& lhcCommentAttribute = CTPPSDataCursor.currentRow()[std::string("LHC_COMMENT")];
0599         if (!lhcCommentAttribute.isNull()) {
0600           lhcComment = lhcCommentAttribute.data<std::string>();
0601         }
0602 
0603         if (m_debug) {
0604           coral::Attribute const& runNumberAttribute = CTPPSDataCursor.currentRow()[std::string("RUN_NUMBER")];
0605           if (!runNumberAttribute.isNull()) {
0606             runNumber = runNumberAttribute.data<int>();
0607           }
0608           coral::Attribute const& lumiSectionAttribute = CTPPSDataCursor.currentRow()[std::string("LUMI_SECTION")];
0609           if (!lumiSectionAttribute.isNull()) {
0610             lumiSection = lumiSectionAttribute.data<int>();
0611           }
0612         }
0613 
0614         for (auto it = filter.current(); it != m_tmpBuffer.end(); it++) {
0615           // set the current values to all of the payloads of the lumi section samples after the current since
0616           LHCInfoPerFill& payload = *(it->second);
0617           payload.setLhcState(lhcState);
0618           payload.setLhcComment(lhcComment);
0619           payload.setCtppsStatus(ctppsStatus);
0620 
0621           if (m_debug) {
0622             savedDipTime = dipTime;
0623             savedLumiSection = lumiSection;
0624             savedRunNumber = runNumber;
0625           }
0626         }
0627       }
0628     }
0629   }
0630   if (m_debug) {
0631     edm::LogInfo(m_name) << "Last assigned: "
0632                          << "DipTime: " << savedDipTime << " "
0633                          << "LumiSection: " << savedLumiSection << " "
0634                          << "RunNumber: " << savedRunNumber;
0635   }
0636   return ret;
0637 }
0638 
0639 bool LHCInfoPerFillPopConSourceHandler::getEcalData(cond::persistency::Session& session,
0640                                                     const boost::posix_time::ptime& lowerTime,
0641                                                     const boost::posix_time::ptime& upperTime) {
0642   //run the sixth query against the CMS_DCS_ENV_PVSS_COND schema
0643   //Initializing the CMS_DCS_ENV_PVSS_COND schema.
0644   coral::ISchema& ECAL = session.nominalSchema();
0645   //start the transaction against the fill logging schema
0646   //execute query for ECAL Data
0647   std::unique_ptr<coral::IQuery> ECALDataQuery(ECAL.newQuery());
0648   //FROM clause
0649   ECALDataQuery->addToTableList(std::string("BEAM_PHASE"));
0650   //SELECT clause
0651   ECALDataQuery->addToOutputList(std::string("CHANGE_DATE"));
0652   ECALDataQuery->addToOutputList(std::string("DIP_value"));
0653   ECALDataQuery->addToOutputList(std::string("element_nr"));
0654   ECALDataQuery->addToOutputList(std::string("VALUE_NUMBER"));
0655   //WHERE CLAUSE
0656   coral::AttributeList ECALDataBindVariables;
0657   ECALDataBindVariables.extend<coral::TimeStamp>(std::string("lowerTime"));
0658   ECALDataBindVariables.extend<coral::TimeStamp>(std::string("upperTime"));
0659   ECALDataBindVariables[std::string("lowerTime")].data<coral::TimeStamp>() = coral::TimeStamp(lowerTime);
0660   ECALDataBindVariables[std::string("upperTime")].data<coral::TimeStamp>() = coral::TimeStamp(upperTime);
0661   std::string conditionStr = std::string(
0662       "(DIP_value LIKE '%beamPhaseMean%' OR DIP_value LIKE '%cavPhaseMean%') AND CHANGE_DATE >= :lowerTime AND "
0663       "CHANGE_DATE < :upperTime");
0664 
0665   ECALDataQuery->setCondition(conditionStr, ECALDataBindVariables);
0666   //ORDER BY clause
0667   ECALDataQuery->addToOrderList(std::string("CHANGE_DATE"));
0668   ECALDataQuery->addToOrderList(std::string("DIP_value"));
0669   ECALDataQuery->addToOrderList(std::string("element_nr"));
0670   //define query output
0671   coral::AttributeList ECALDataOutput;
0672   ECALDataOutput.extend<coral::TimeStamp>(std::string("CHANGE_DATE"));
0673   ECALDataOutput.extend<std::string>(std::string("DIP_value"));
0674   ECALDataOutput.extend<unsigned int>(std::string("element_nr"));
0675   ECALDataOutput.extend<float>(std::string("VALUE_NUMBER"));
0676   //ECALDataQuery->limitReturnedRows( 14256 ); //3564 entries per vector.
0677   ECALDataQuery->defineOutput(ECALDataOutput);
0678   //execute the query
0679   coral::ICursor& ECALDataCursor = ECALDataQuery->execute();
0680   cond::Time_t changeTime = 0;
0681   cond::Time_t firstTime = 0;
0682   std::string dipVal = "";
0683   unsigned int elementNr = 0;
0684   float value = 0.;
0685   std::set<cond::Time_t> initializedVectors;
0686   LumiSectionFilter<LHCInfoPerFill> filter(m_tmpBuffer);
0687   bool ret = false;
0688   if (m_prevPayload.get()) {
0689     for (auto& lumiSlot : m_tmpBuffer) {
0690       lumiSlot.second->setBeam1VC(m_prevPayload->beam1VC());
0691       lumiSlot.second->setBeam2VC(m_prevPayload->beam2VC());
0692       lumiSlot.second->setBeam1RF(m_prevPayload->beam1RF());
0693       lumiSlot.second->setBeam2RF(m_prevPayload->beam2RF());
0694     }
0695   }
0696   std::map<cond::Time_t, cond::Time_t> iovMap;
0697   if (m_tmpBuffer.empty()) {
0698     return ret;
0699   }
0700   cond::Time_t lowerLumi = m_tmpBuffer.front().first;
0701   while (ECALDataCursor.next()) {
0702     if (m_debug) {
0703       std::ostringstream ECAL;
0704       ECALDataCursor.currentRow().toOutputStream(ECAL);
0705     }
0706     coral::Attribute const& changeDateAttribute = ECALDataCursor.currentRow()[std::string("CHANGE_DATE")];
0707     if (!changeDateAttribute.isNull()) {
0708       ret = true;
0709       boost::posix_time::ptime chTime = changeDateAttribute.data<coral::TimeStamp>().time();
0710       // move the first IOV found to the start of the fill interval selected
0711       if (changeTime == 0) {
0712         firstTime = cond::time::from_boost(chTime);
0713       }
0714       changeTime = cond::time::from_boost(chTime);
0715       cond::Time_t iovTime = changeTime;
0716       if (changeTime == firstTime)
0717         iovTime = lowerLumi;
0718       coral::Attribute const& dipValAttribute = ECALDataCursor.currentRow()[std::string("DIP_value")];
0719       coral::Attribute const& valueNumberAttribute = ECALDataCursor.currentRow()[std::string("VALUE_NUMBER")];
0720       coral::Attribute const& elementNrAttribute = ECALDataCursor.currentRow()[std::string("element_nr")];
0721       if (!dipValAttribute.isNull() and !valueNumberAttribute.isNull()) {
0722         dipVal = dipValAttribute.data<std::string>();
0723         elementNr = elementNrAttribute.data<unsigned int>();
0724         value = valueNumberAttribute.data<float>();
0725         if (std::isnan(value))
0726           value = 0.;
0727         if (filter.process(iovTime)) {
0728           iovMap.insert(make_pair(changeTime, filter.current()->first));
0729           for (auto it = filter.current(); it != m_tmpBuffer.end(); it++) {
0730             LHCInfoPerFill& payload = *(it->second);
0731             theLHCInfoPerFillImpl::setElementData(it->first, dipVal, elementNr, value, payload, initializedVectors);
0732           }
0733         }
0734       }
0735     }
0736   }
0737   if (m_debug) {
0738     for (auto& im : iovMap) {
0739       edm::LogInfo(m_name) << "Found iov=" << im.first << " (" << cond::time::to_boost(im.first) << " ) moved to "
0740                            << im.second << " ( " << cond::time::to_boost(im.second) << " )";
0741     }
0742   }
0743   return ret;
0744 }