Skip to content

Commit

Permalink
Allow to retrieve the latest MO or QO with DatabaseInterface (#2089)
Browse files Browse the repository at this point in the history
  • Loading branch information
knopers8 authored Jan 17, 2024
1 parent 72a97e2 commit 0dd3dc3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
8 changes: 4 additions & 4 deletions Framework/include/QualityControl/CcdbDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ class CcdbDatabase : public DatabaseInterface
std::string const& detectorName, std::string const& taskName, long from = -1, long to = -1) override;

void* retrieveAny(std::type_info const& tinfo, std::string const& path,
std::map<std::string, std::string> const& metadata, long timestamp = -1,
std::map<std::string, std::string> const& metadata, long timestamp = Timestamp::Current,
std::map<std::string, std::string>* headers = nullptr,
const std::string& createdNotAfter = "", const std::string& createdNotBefore = "") override;

// retrieval - MO - deprecated
std::shared_ptr<o2::quality_control::core::MonitorObject> retrieveMO(std::string objectPath, std::string objectName, long timestamp = -1, const core::Activity& activity = {}) override;
std::shared_ptr<o2::quality_control::core::MonitorObject> retrieveMO(std::string objectPath, std::string objectName, long timestamp = Timestamp::Current, const core::Activity& activity = {}) override;
// retrieval - QO - deprecated
std::shared_ptr<o2::quality_control::core::QualityObject> retrieveQO(std::string qoPath, long timestamp = -1, const core::Activity& activity = {}) override;
std::shared_ptr<o2::quality_control::core::QualityObject> retrieveQO(std::string qoPath, long timestamp = Timestamp::Current, const core::Activity& activity = {}) override;
std::shared_ptr<o2::quality_control::TimeRangeFlagCollection> retrieveTRFC(const std::string& name, const std::string& detector, int runNumber = 0,
const std::string& passName = "", const std::string& periodName = "",
const std::string& provenance = "", long timestamp = -1) override;

// retrieval - general
std::string retrieveJson(std::string path, long timestamp, const std::map<std::string, std::string>& metadata) override;
TObject* retrieveTObject(std::string path, const std::map<std::string, std::string>& metadata, long timestamp = -1, std::map<std::string, std::string>* headers = nullptr) override;
TObject* retrieveTObject(std::string path, const std::map<std::string, std::string>& metadata, long timestamp = Timestamp::Current, std::map<std::string, std::string>* headers = nullptr) override;

void disconnect() override;
void prepareTaskDataContainer(std::string taskName) override;
Expand Down
13 changes: 9 additions & 4 deletions Framework/include/QualityControl/DatabaseInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class DatabaseInterface
public:
constexpr static framework::ServiceKind service_kind = framework::ServiceKind::Global;

enum Timestamp : long {
Current = -1,
Latest = 0
};

/// Default constructor
DatabaseInterface() = default;
/// Destructor
Expand Down Expand Up @@ -140,7 +145,7 @@ class DatabaseInterface
* @param activity Activity of the object
* @deprecated
*/
virtual std::shared_ptr<o2::quality_control::core::MonitorObject> retrieveMO(std::string objectPath, std::string objectName, long timestamp = -1, const core::Activity& activity = {}) = 0;
virtual std::shared_ptr<o2::quality_control::core::MonitorObject> retrieveMO(std::string objectPath, std::string objectName, long timestamp = Timestamp::Current, const core::Activity& activity = {}) = 0;
/**
* \brief Look up a quality object and return it.
* Look up a quality object and return it if found or nullptr if not.
Expand All @@ -149,14 +154,14 @@ class DatabaseInterface
* @param activity Activity of the object
* @deprecated
*/
virtual std::shared_ptr<o2::quality_control::core::QualityObject> retrieveQO(std::string qoPath, long timestamp = -1, const core::Activity& activity = {}) = 0;
virtual std::shared_ptr<o2::quality_control::core::QualityObject> retrieveQO(std::string qoPath, long timestamp = Timestamp::Current, const core::Activity& activity = {}) = 0;
/**
* \brief Look up a TimeRangeFlagCollection object and return it.
* Look up a TimeRangeFlagCollection and return it if found or nullptr if not.
*/
virtual std::shared_ptr<o2::quality_control::TimeRangeFlagCollection> retrieveTRFC(const std::string& name, const std::string& detector, int runNumber = 0,
const std::string& passName = "", const std::string& periodName = "",
const std::string& provenance = "", long timestamp = -1) = 0;
const std::string& provenance = "", long timestamp = Timestamp::Current) = 0;
/**
* \brief Look up an object and return it.
* Look up an object and return it if found or nullptr if not. It is a raw pointer because we might need it to build a MO.
Expand All @@ -165,7 +170,7 @@ class DatabaseInterface
* \param headers Map to be populated with the headers we received, if it is not null.
* \param metadata filters under the form of key-value pairs to select data
*/
virtual TObject* retrieveTObject(std::string path, const std::map<std::string, std::string>& metadata, long timestamp = -1, std::map<std::string, std::string>* headers = nullptr) = 0;
virtual TObject* retrieveTObject(std::string path, const std::map<std::string, std::string>& metadata, long timestamp = Timestamp::Current, std::map<std::string, std::string>* headers = nullptr) = 0;

/**
* \brief Look up an object and return it in JSON format.
Expand Down
14 changes: 14 additions & 0 deletions Framework/src/CcdbDatabase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ void CcdbDatabase::storeTRFC(std::shared_ptr<const o2::quality_control::TimeRang

TObject* CcdbDatabase::retrieveTObject(std::string path, std::map<std::string, std::string> const& metadata, long timestamp, std::map<std::string, std::string>* headers)
{
if (timestamp == Timestamp::Latest) {
auto latestValidity = getLatestObjectValidity(path, metadata);
if (latestValidity.isInvalid()) {
return nullptr;
}
timestamp = latestValidity.getMin();
}
// we try first to load a TFile
auto* object = ccdbApi->retrieveFromTFileAny<TObject>(path, metadata, timestamp, headers);
if (object == nullptr) {
Expand All @@ -348,6 +355,13 @@ TObject* CcdbDatabase::retrieveTObject(std::string path, std::map<std::string, s

void* CcdbDatabase::retrieveAny(const type_info& tinfo, const string& path, const map<std::string, std::string>& metadata, long timestamp, std::map<std::string, std::string>* headers, const string& createdNotAfter, const string& createdNotBefore)
{
if (timestamp == Timestamp::Latest) {
auto latestValidity = getLatestObjectValidity(path, metadata);
if (latestValidity.isInvalid()) {
return nullptr;
}
timestamp = latestValidity.getMin();
}
auto* object = ccdbApi->retrieveFromTFile(tinfo, path, metadata, timestamp, headers, "", createdNotAfter, createdNotBefore);
if (object == nullptr) {
ILOG(Warning, Support) << "We could NOT retrieve the object " << path << " with timestamp " << timestamp << "." << ENDM;
Expand Down
29 changes: 29 additions & 0 deletions Framework/test/testCcdbDatabase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <TH1F.h>
#include "QualityControl/RepoPathUtils.h"
#include "QualityControl/ObjectMetadataKeys.h"
#include "QualityControl/ActivityHelpers.h"
#include <DataFormatsQualityControl/TimeRangeFlagCollection.h>
#include <TROOT.h>
#include <CCDB/CcdbApi.h>
Expand Down Expand Up @@ -387,6 +388,34 @@ BOOST_AUTO_TEST_CASE(ccdb_store_retrieve_any)
BOOST_CHECK(h1_back->GetEntries() > 0);
}

BOOST_AUTO_TEST_CASE(ccdb_store_retrieve_latest)
{
test_fixture f;

TH1F* h1 = new TH1F("latest_test", "latest_test", 100, 0, 99);
h1->FillRandom("gaus", 10000);
shared_ptr<MonitorObject> mo1 = make_shared<MonitorObject>(h1, f.taskName, "TestClass", "TST");
mo1->updateActivity(1234, "LHC66", "passName1", "qc");
mo1->setValidity({ 30, 50 });
f.backend->storeMO(mo1);

h1->FillRandom("gaus", 10000);
mo1->updateActivity(1234, "LHC66", "passName1", "qc");
mo1->setValidity({ 10, 30 });
f.backend->storeMO(mo1); // this is going to be the latest version matching the provided Activity

h1->FillRandom("gaus", 10000);
mo1->updateActivity(1234, "LHC66", "differentPassName", "qc");
mo1->setValidity({ 10, 30 });
f.backend->storeMO(mo1);

std::shared_ptr<MonitorObject> moBack = f.backend->retrieveMO(f.getMoFolder("latest_test"), "latest_test", DatabaseInterface::Timestamp::Latest, { 1234, 0, "LHC66", "passName1", "qc" });
BOOST_REQUIRE(moBack != nullptr);
auto h1Back = dynamic_cast<TH1F*>(moBack->getObject());
BOOST_REQUIRE(h1Back != nullptr);
BOOST_CHECK_EQUAL(h1Back->GetEntries(), 20000);
}

BOOST_AUTO_TEST_CASE(ccdb_trfc)
{
test_fixture f;
Expand Down

0 comments on commit 0dd3dc3

Please sign in to comment.