From e74ddb739d1e0a1e22dc1aa3f4c1076e2352e3ae Mon Sep 17 00:00:00 2001
From: francescobrivio <francesco.brivio@cern.ch>
Date: Thu, 22 Feb 2024 14:31:37 +0100
Subject: [PATCH] add conversion from timestamp to lumiid in
 LHCInfoPerFillPopConAnalyzer

---
 .../plugins/LHCInfoPerFillPopConAnalyzer.cc   | 38 +++++++++++++++----
 CondTools/RunInfo/src/LHCInfoHelper.cc        |  3 +-
 2 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/CondTools/RunInfo/plugins/LHCInfoPerFillPopConAnalyzer.cc b/CondTools/RunInfo/plugins/LHCInfoPerFillPopConAnalyzer.cc
index 45b40b6c3b87c..cce7fd953dfc9 100644
--- a/CondTools/RunInfo/plugins/LHCInfoPerFillPopConAnalyzer.cc
+++ b/CondTools/RunInfo/plugins/LHCInfoPerFillPopConAnalyzer.cc
@@ -360,9 +360,16 @@ class LHCInfoPerFillPopConSourceHandler : public popcon::PopConSourceHandler<LHC
         }
       }
 
+      // In duringFill mode, convert the timestamp-type IOVs to lumiid-type IOVs
+      // before transferring the payloads from the buffer to the final collection
+      if (!m_endFillMode) {
+        convertBufferedIovsToLumiid(m_timestampToLumiid);
+      }
+
       size_t niovs = theLHCInfoPerFillImpl::transferPayloads(m_tmpBuffer, m_iovs, m_prevPayload);
       edm::LogInfo(m_name) << "Added " << niovs << " iovs within the Fill time";
       m_tmpBuffer.clear();
+      m_timestampToLumiid.clear();
       if (m_prevPayload->fillNumber() and !ongoingFill) {
         if (m_endFillMode) {
           addEmptyPayload(endFillTime);
@@ -398,24 +405,37 @@ class LHCInfoPerFillPopConSourceHandler : public popcon::PopConSourceHandler<LHC
     }
   }
 
+  // Add payload to buffer and store corresponding lumiid IOV in m_timestampToLumiid map
   void addPayloadToBuffer(cond::OMSServiceResultRef& row) {
-    auto lumiTime = row.get<boost::posix_time::ptime>("start_time");
+    auto startTime = row.get<boost::posix_time::ptime>("start_time");
     auto delivLumi = row.get<float>("delivered_lumi");
     auto recLumi = row.get<float>("recorded_lumi");
+    auto runNumber = std::stoull(row.get<std::string>("run_number"));
+    auto lsNumber = std::stoul(row.get<std::string>("lumisection_number"));
+    auto lumiid = cond::time::lumiTime(runNumber, lsNumber);
+
     LHCInfoPerFill* thisLumiSectionInfo = m_fillPayload->cloneFill();
-    if (m_endFillMode) {
-      m_tmpBuffer.emplace_back(std::make_pair(cond::time::from_boost(lumiTime), thisLumiSectionInfo));
-    } else {
-      m_tmpBuffer.emplace_back(
-          std::make_pair(cond::time::lumiTime(std::stoull(row.get<std::string>("run_number")),
-                                              std::stoul(row.get<std::string>("lumisection_number"))),
-                         thisLumiSectionInfo));
+    m_tmpBuffer.emplace_back(std::make_pair(cond::time::from_boost(startTime), thisLumiSectionInfo));
+    if (!m_endFillMode) {
+      m_timestampToLumiid.insert(std::make_pair(cond::time::from_boost(startTime), lumiid));
     }
     LHCInfoPerFill& payload = *thisLumiSectionInfo;
     payload.setDelivLumi(delivLumi);
     payload.setRecLumi(recLumi);
   }
 
+  void convertBufferedIovsToLumiid(std::map<cond::Time_t, cond::Time_t> timestampToLumiid) {
+    for (auto& item : m_tmpBuffer) {
+      // Check if the lumiid IOV corresponding to the timestamp is present in the map
+      if (timestampToLumiid.find(item.first) == timestampToLumiid.end()) {
+        throw cms::Exception("LHCInfoPerFillPopConSourceHandler")
+            << "Can't find corresponding lumiid IOV for timestamp " << item.first << "\n";
+      }
+      // Update the buffer with the lumiid-type IOV
+      item.first = timestampToLumiid.at(item.first);
+    }
+  }
+
   size_t getLumiData(const cond::OMSService& oms,
                      unsigned short fillId,
                      const boost::posix_time::ptime& beginFillTime,
@@ -740,4 +760,6 @@ class LHCInfoPerFillPopConSourceHandler : public popcon::PopConSourceHandler<LHC
   std::shared_ptr<LHCInfoPerFill> m_prevPayload;
   std::vector<std::pair<cond::Time_t, std::shared_ptr<LHCInfoPerFill>>> m_tmpBuffer;
   bool m_lastPayloadEmpty = false;
+  // to hold correspondance between timestamp-type IOVs and lumiid-type IOVs
+  std::map<cond::Time_t, cond::Time_t> m_timestampToLumiid;
 };
diff --git a/CondTools/RunInfo/src/LHCInfoHelper.cc b/CondTools/RunInfo/src/LHCInfoHelper.cc
index 5ce44e21981ba..54503068b8924 100644
--- a/CondTools/RunInfo/src/LHCInfoHelper.cc
+++ b/CondTools/RunInfo/src/LHCInfoHelper.cc
@@ -20,8 +20,9 @@ cond::Time_t cond::lhcInfoHelper::getFillLastLumiIOV(const cond::OMSService& oms
 
   // Get query result
   auto queryResult = query->result();
-  if (queryResult.empty())
+  if (queryResult.empty()) {
     throw cms::Exception("OMSQueryFailure") << "OMS query of fill " << fillId << " returned empty result!\n";
+  }
 
   // Return the final IOV
   auto lastRun = queryResult.back().get<int>("run_number");