From 4ebc9bfa0b8b5582ec8a593e30f2f917f4b5bae3 Mon Sep 17 00:00:00 2001 From: mmusich Date: Fri, 23 Jun 2023 10:50:53 +0200 Subject: [PATCH] add timestap dates in human readable form in the BeamSpot DisplayParameters template class --- .../BeamSpotPayloadInspectorHelper.h | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h b/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h index a151687b95edb..2e03a2cbd1d5f 100644 --- a/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h +++ b/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h @@ -9,6 +9,7 @@ #include "FWCore/MessageLogger/interface/MessageLogger.h" // system includes +#include #include #include #include @@ -60,6 +61,26 @@ namespace beamSpotPI { END_OF_TYPES = 25, }; + /************************************************/ + // Function to convert cond::Time_t (in microseconds) to human-readable date string + std::string convertTimeToDateString(cond::Time_t timeValue, bool hasMicros = false, bool toUTC = true) { + // Convert microseconds to seconds + std::time_t unixTime = static_cast(hasMicros ? timeValue / 1000000 : timeValue); + + // Convert std::time_t to struct tm (to UTC, or not) + std::tm* timeInfo = toUTC ? std::gmtime(&unixTime) : std::localtime(&unixTime); + + // Convert struct tm to human-readable string format + char buffer[80]; + std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeInfo); + + // Append microseconds to the string + std::string dateString(buffer); + //dateString += "." + std::to_string(timeValue % 1000000); + + return dateString; + } + /************************************************/ inline std::string getStringFromParamEnum(const parameters& parameter, const bool addUnits = false /*not used by default*/) { @@ -432,6 +453,44 @@ namespace beamSpotPI { (tagname + " IOV: #color[4]{" + std::to_string(runLS.first) + "," + std::to_string(runLS.second) + "}") .c_str()); + if constexpr (std::is_same_v) { + // protections needed against old payload that do not have these data members persisted + const auto& creationTime = test_( + [&]() { + return m_payload->creationTime(); + }, // Lambda function capturing m_payload and calling creationTime + better_error); + + const auto& startTime = test_( + [&]() { + return m_payload->startTimeStamp(); + }, // Lambda function capturing m_payload and calling startTimeStamp + better_error); + + const auto& endTime = test_( + [&]() { + return m_payload->endTimeStamp(); + }, // Lambda function capturing m_payload and calling endTimeStamp + better_error); + canvas.cd(2); + ltx.SetTextSize(0.025); + ltx.DrawLatexNDC( + gPad->GetLeftMargin() + 0.01, + gPad->GetBottomMargin() + 0.15, + ("#color[2]{(" + beamSpotPI::convertTimeToDateString(creationTime, /*has us*/ true) + ")}").c_str()); + + ltx.DrawLatexNDC(gPad->GetLeftMargin() + 0.01, + gPad->GetBottomMargin() + 0.085, + ("#color[2]{(" + beamSpotPI::convertTimeToDateString(startTime) + ")}").c_str()); + + ltx.DrawLatexNDC(gPad->GetLeftMargin() + 0.01, + gPad->GetBottomMargin() + 0.025, + ("#color[2]{(" + beamSpotPI::convertTimeToDateString(endTime) + ")}").c_str()); + + ltx.DrawLatexNDC( + gPad->GetLeftMargin(), gPad->GetBottomMargin() - 0.05, "#color[4]{N.B.} TimeStamps are in UTC"); + } + std::string fileName(this->m_imageFileName); canvas.SaveAs(fileName.c_str()); @@ -477,6 +536,22 @@ namespace beamSpotPI { return "should never be here"; } } + + // Slightly better error handler + static void better_error(const std::exception& e) { edm::LogError("DisplayParameters") << e.what() << '\n'; } + + // Method to catch exceptions + template + T test_(Func f, Response r) const { + try { + LogDebug("DisplayParameters") << "I have tried" << std::endl; + return f(); + } catch (const Except& e) { + LogDebug("DisplayParameters") << "I have caught!" << std::endl; + r(e); + return static_cast(1); + } + } }; /************************************************