|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +/// \file testMetadataHelper.C |
| 13 | +/// \author Nicolò Jacazio nicolo.jacazio@cern.ch |
| 14 | +/// \brief Test the MetadataHelper functionality |
| 15 | + |
| 16 | +#include "Common/Core/MetadataHelper.h" |
| 17 | + |
| 18 | +#include <Framework/ConfigContext.h> |
| 19 | +#include <Framework/ConfigParamRegistry.h> |
| 20 | +#include <Framework/ConfigParamStore.h> |
| 21 | +#include <Framework/ServiceRegistry.h> |
| 22 | +#include <Framework/ServiceRegistryRef.h> |
| 23 | + |
| 24 | +#include <TFile.h> |
| 25 | +#include <TMap.h> |
| 26 | +#include <TObjString.h> |
| 27 | + |
| 28 | +#include <memory> |
| 29 | +#include <string> |
| 30 | +#include <utility> |
| 31 | +#include <vector> |
| 32 | + |
| 33 | +// Taken from O2/Framework/AnalysisSupport/src/Plugin.cxx |
| 34 | +auto readMetadata(std::unique_ptr<TFile>& currentFile) -> std::vector<o2::framework::ConfigParamSpec> |
| 35 | +{ |
| 36 | + // Get the metadata, if any |
| 37 | + auto m = (TMap*)currentFile->Get("metaData"); |
| 38 | + if (!m) { |
| 39 | + return {}; |
| 40 | + } |
| 41 | + std::vector<o2::framework::ConfigParamSpec> results; |
| 42 | + auto it = m->MakeIterator(); |
| 43 | + |
| 44 | + // Serialise metadata into a ; separated string with : separating key and value |
| 45 | + bool first = true; |
| 46 | + while (auto obj = it->Next()) { |
| 47 | + if (first) { |
| 48 | + LOGP(info, "Metadata for file \"{}\":", currentFile->GetName()); |
| 49 | + first = false; |
| 50 | + } |
| 51 | + auto objString = (TObjString*)m->GetValue(obj); |
| 52 | + std::string key = "aod-metadata-" + std::string(obj->GetName()); |
| 53 | + LOGP(info, "- {}: {} goes into key {}", obj->GetName(), objString->String().Data(), key); |
| 54 | + char const* value = strdup(objString->String()); |
| 55 | + results.push_back(o2::framework::ConfigParamSpec{key, o2::framework::VariantType::String, value, {"Metadata in AOD"}}); |
| 56 | + } |
| 57 | + return results; |
| 58 | +} |
| 59 | + |
| 60 | +void testMetadataHelper(std::string aod = "/tmp/AO2D.root") |
| 61 | +{ |
| 62 | + |
| 63 | + TFile* file = TFile::Open(aod.c_str()); |
| 64 | + if (!file || file->IsZombie()) { |
| 65 | + LOG(fatal) << "Could not open file " << aod; |
| 66 | + } |
| 67 | + std::unique_ptr<TFile> currentFile{file}; |
| 68 | + std::vector<o2::framework::ConfigParamSpec> specs = readMetadata(currentFile); |
| 69 | + |
| 70 | + std::vector<std::unique_ptr<o2::framework::ParamRetriever>> retrievers; |
| 71 | + auto paramStore = std::make_unique<o2::framework::ConfigParamStore>(specs, std::move(retrievers)); |
| 72 | + paramStore->preload(); |
| 73 | + paramStore->activate(); |
| 74 | + o2::framework::ConfigParamRegistry paramRegistry(std::move(paramStore)); |
| 75 | + o2::framework::ServiceRegistry serviceRegistry; |
| 76 | + o2::framework::ServiceRegistryRef services(serviceRegistry); |
| 77 | + o2::framework::ConfigContext aodCfg(paramRegistry, services, 0, nullptr); |
| 78 | + LOG(info) << "Loaded " << aodCfg.options().specs().size() << " configuration entries from file " << aod; |
| 79 | + aodCfg.options().get<std::string>("aod-metadata-DataType"); |
| 80 | + o2::common::core::MetadataHelper metadataInfo; |
| 81 | + metadataInfo.initMetadata(aodCfg); |
| 82 | + metadataInfo.print(); |
| 83 | + LOG(info) << "Metadata label: " << metadataInfo.makeMetadataLabel(); |
| 84 | +} |
0 commit comments