|
9 | 9 | #include "FWCore/MessageLogger/interface/MessageLogger.h"
|
10 | 10 |
|
11 | 11 | // system includes
|
| 12 | +#include <ctime> |
12 | 13 | #include <fmt/printf.h>
|
13 | 14 | #include <memory>
|
14 | 15 | #include <sstream>
|
@@ -60,6 +61,26 @@ namespace beamSpotPI {
|
60 | 61 | END_OF_TYPES = 25,
|
61 | 62 | };
|
62 | 63 |
|
| 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 | + |
63 | 84 | /************************************************/
|
64 | 85 | inline std::string getStringFromParamEnum(const parameters& parameter,
|
65 | 86 | const bool addUnits = false /*not used by default*/) {
|
@@ -432,6 +453,44 @@ namespace beamSpotPI {
|
432 | 453 | (tagname + " IOV: #color[4]{" + std::to_string(runLS.first) + "," + std::to_string(runLS.second) + "}")
|
433 | 454 | .c_str());
|
434 | 455 |
|
| 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 | + |
435 | 494 | std::string fileName(this->m_imageFileName);
|
436 | 495 | canvas.SaveAs(fileName.c_str());
|
437 | 496 |
|
@@ -477,6 +536,22 @@ namespace beamSpotPI {
|
477 | 536 | return "should never be here";
|
478 | 537 | }
|
479 | 538 | }
|
| 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 | + } |
480 | 555 | };
|
481 | 556 |
|
482 | 557 | /************************************************
|
|
0 commit comments