Skip to content

Commit 4ebc9bf

Browse files
committed
add timestap dates in human readable form in the BeamSpot DisplayParameters template class
1 parent c34a318 commit 4ebc9bf

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h

+75
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "FWCore/MessageLogger/interface/MessageLogger.h"
1010

1111
// system includes
12+
#include <ctime>
1213
#include <fmt/printf.h>
1314
#include <memory>
1415
#include <sstream>
@@ -60,6 +61,26 @@ namespace beamSpotPI {
6061
END_OF_TYPES = 25,
6162
};
6263

64+
/************************************************/
65+
// Function to convert cond::Time_t (in microseconds) to human-readable date string
66+
std::string convertTimeToDateString(cond::Time_t timeValue, bool hasMicros = false, bool toUTC = true) {
67+
// Convert microseconds to seconds
68+
std::time_t unixTime = static_cast<std::time_t>(hasMicros ? timeValue / 1000000 : timeValue);
69+
70+
// Convert std::time_t to struct tm (to UTC, or not)
71+
std::tm* timeInfo = toUTC ? std::gmtime(&unixTime) : std::localtime(&unixTime);
72+
73+
// Convert struct tm to human-readable string format
74+
char buffer[80];
75+
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeInfo);
76+
77+
// Append microseconds to the string
78+
std::string dateString(buffer);
79+
//dateString += "." + std::to_string(timeValue % 1000000);
80+
81+
return dateString;
82+
}
83+
6384
/************************************************/
6485
inline std::string getStringFromParamEnum(const parameters& parameter,
6586
const bool addUnits = false /*not used by default*/) {
@@ -432,6 +453,44 @@ namespace beamSpotPI {
432453
(tagname + " IOV: #color[4]{" + std::to_string(runLS.first) + "," + std::to_string(runLS.second) + "}")
433454
.c_str());
434455

456+
if constexpr (std::is_same_v<PayloadType, BeamSpotOnlineObjects>) {
457+
// protections needed against old payload that do not have these data members persisted
458+
const auto& creationTime = test_<cond::Time_t, std::out_of_range>(
459+
[&]() {
460+
return m_payload->creationTime();
461+
}, // Lambda function capturing m_payload and calling creationTime
462+
better_error);
463+
464+
const auto& startTime = test_<cond::Time_t, std::out_of_range>(
465+
[&]() {
466+
return m_payload->startTimeStamp();
467+
}, // Lambda function capturing m_payload and calling startTimeStamp
468+
better_error);
469+
470+
const auto& endTime = test_<cond::Time_t, std::out_of_range>(
471+
[&]() {
472+
return m_payload->endTimeStamp();
473+
}, // Lambda function capturing m_payload and calling endTimeStamp
474+
better_error);
475+
canvas.cd(2);
476+
ltx.SetTextSize(0.025);
477+
ltx.DrawLatexNDC(
478+
gPad->GetLeftMargin() + 0.01,
479+
gPad->GetBottomMargin() + 0.15,
480+
("#color[2]{(" + beamSpotPI::convertTimeToDateString(creationTime, /*has us*/ true) + ")}").c_str());
481+
482+
ltx.DrawLatexNDC(gPad->GetLeftMargin() + 0.01,
483+
gPad->GetBottomMargin() + 0.085,
484+
("#color[2]{(" + beamSpotPI::convertTimeToDateString(startTime) + ")}").c_str());
485+
486+
ltx.DrawLatexNDC(gPad->GetLeftMargin() + 0.01,
487+
gPad->GetBottomMargin() + 0.025,
488+
("#color[2]{(" + beamSpotPI::convertTimeToDateString(endTime) + ")}").c_str());
489+
490+
ltx.DrawLatexNDC(
491+
gPad->GetLeftMargin(), gPad->GetBottomMargin() - 0.05, "#color[4]{N.B.} TimeStamps are in UTC");
492+
}
493+
435494
std::string fileName(this->m_imageFileName);
436495
canvas.SaveAs(fileName.c_str());
437496

@@ -477,6 +536,22 @@ namespace beamSpotPI {
477536
return "should never be here";
478537
}
479538
}
539+
540+
// Slightly better error handler
541+
static void better_error(const std::exception& e) { edm::LogError("DisplayParameters") << e.what() << '\n'; }
542+
543+
// Method to catch exceptions
544+
template <typename T, class Except, class Func, class Response>
545+
T test_(Func f, Response r) const {
546+
try {
547+
LogDebug("DisplayParameters") << "I have tried" << std::endl;
548+
return f();
549+
} catch (const Except& e) {
550+
LogDebug("DisplayParameters") << "I have caught!" << std::endl;
551+
r(e);
552+
return static_cast<T>(1);
553+
}
554+
}
480555
};
481556

482557
/************************************************

0 commit comments

Comments
 (0)