diff --git a/Common/Core/MetadataHelper.cxx b/Common/Core/MetadataHelper.cxx index 79aa051a64f..b025c5df7a1 100644 --- a/Common/Core/MetadataHelper.cxx +++ b/Common/Core/MetadataHelper.cxx @@ -124,3 +124,19 @@ bool MetadataHelper::isInitialized() const } return mIsInitialized; } + +std::string MetadataHelper::makeMetadataLabel() const +{ + if (!mIsInitialized) { + LOG(fatal) << "Metadata not initialized"; + } + std::string label = get("DataType"); + label += "_" + get("LPMProductionTag"); + if (isMC()) { + label += "_" + get("AnchorPassName"); + label += "_" + get("AnchorProduction"); + } else { + label += "_" + get("RecoPassName"); + } + return label; +} diff --git a/Common/Core/MetadataHelper.h b/Common/Core/MetadataHelper.h index bab7b1f7eb0..3854820bd84 100644 --- a/Common/Core/MetadataHelper.h +++ b/Common/Core/MetadataHelper.h @@ -8,10 +8,10 @@ // In applying this license CERN does not waive the privileges and immunities // granted to it by virtue of its status as an Intergovernmental Organization // or submit itself to any jurisdiction. -/// \file TableHelper.h + +/// \file MetadataHelper.h +/// \brief Utility to handle the metadata from the AOD /// \author Nicolò Jacazio nicolo.jacazio@cern.ch -/// \brief Utility to handle the metadata from the AOD -/// #ifndef COMMON_CORE_METADATAHELPER_H_ #define COMMON_CORE_METADATAHELPER_H_ @@ -61,6 +61,9 @@ struct MetadataHelper { /// @return true if the key is defined, false otherwise. Throws an exception if the key is not found bool isKeyDefined(const std::string& key) const; + /// @brief Function to create a label with the metadata information, useful e.g. for histogram naming + std::string makeMetadataLabel() const; + private: std::map mMetadata; /// < The metadata map bool mIsInitialized = false; /// < Flag to check if the metadata has been initialized diff --git a/Common/Core/macros/testMetadataHelper.C b/Common/Core/macros/testMetadataHelper.C new file mode 100644 index 00000000000..30c109f7840 --- /dev/null +++ b/Common/Core/macros/testMetadataHelper.C @@ -0,0 +1,84 @@ +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. +// All rights not expressly granted are reserved. +// +// This software is distributed under the terms of the GNU General Public +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +/// \file testMetadataHelper.C +/// \author Nicolò Jacazio nicolo.jacazio@cern.ch +/// \brief Test the MetadataHelper functionality + +#include "Common/Core/MetadataHelper.h" + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +// Taken from O2/Framework/AnalysisSupport/src/Plugin.cxx +auto readMetadata(std::unique_ptr& currentFile) -> std::vector +{ + // Get the metadata, if any + auto m = (TMap*)currentFile->Get("metaData"); + if (!m) { + return {}; + } + std::vector results; + auto it = m->MakeIterator(); + + // Serialise metadata into a ; separated string with : separating key and value + bool first = true; + while (auto obj = it->Next()) { + if (first) { + LOGP(info, "Metadata for file \"{}\":", currentFile->GetName()); + first = false; + } + auto objString = (TObjString*)m->GetValue(obj); + std::string key = "aod-metadata-" + std::string(obj->GetName()); + LOGP(info, "- {}: {} goes into key {}", obj->GetName(), objString->String().Data(), key); + char const* value = strdup(objString->String()); + results.push_back(o2::framework::ConfigParamSpec{key, o2::framework::VariantType::String, value, {"Metadata in AOD"}}); + } + return results; +} + +void testMetadataHelper(std::string aod = "/tmp/AO2D.root") +{ + + TFile* file = TFile::Open(aod.c_str()); + if (!file || file->IsZombie()) { + LOG(fatal) << "Could not open file " << aod; + } + std::unique_ptr currentFile{file}; + std::vector specs = readMetadata(currentFile); + + std::vector> retrievers; + auto paramStore = std::make_unique(specs, std::move(retrievers)); + paramStore->preload(); + paramStore->activate(); + o2::framework::ConfigParamRegistry paramRegistry(std::move(paramStore)); + o2::framework::ServiceRegistry serviceRegistry; + o2::framework::ServiceRegistryRef services(serviceRegistry); + o2::framework::ConfigContext aodCfg(paramRegistry, services, 0, nullptr); + LOG(info) << "Loaded " << aodCfg.options().specs().size() << " configuration entries from file " << aod; + aodCfg.options().get("aod-metadata-DataType"); + o2::common::core::MetadataHelper metadataInfo; + metadataInfo.initMetadata(aodCfg); + metadataInfo.print(); + LOG(info) << "Metadata label: " << metadataInfo.makeMetadataLabel(); +}