Skip to content

Commit 33a836d

Browse files
authored
[Common] Add unique label making utility to metadata (#12938)
1 parent 6d2a948 commit 33a836d

File tree

3 files changed

+106
-3
lines changed

3 files changed

+106
-3
lines changed

Common/Core/MetadataHelper.cxx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,19 @@ bool MetadataHelper::isInitialized() const
124124
}
125125
return mIsInitialized;
126126
}
127+
128+
std::string MetadataHelper::makeMetadataLabel() const
129+
{
130+
if (!mIsInitialized) {
131+
LOG(fatal) << "Metadata not initialized";
132+
}
133+
std::string label = get("DataType");
134+
label += "_" + get("LPMProductionTag");
135+
if (isMC()) {
136+
label += "_" + get("AnchorPassName");
137+
label += "_" + get("AnchorProduction");
138+
} else {
139+
label += "_" + get("RecoPassName");
140+
}
141+
return label;
142+
}

Common/Core/MetadataHelper.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
// In applying this license CERN does not waive the privileges and immunities
99
// granted to it by virtue of its status as an Intergovernmental Organization
1010
// or submit itself to any jurisdiction.
11-
/// \file TableHelper.h
11+
12+
/// \file MetadataHelper.h
13+
/// \brief Utility to handle the metadata from the AOD
1214
/// \author Nicolò Jacazio nicolo.jacazio@cern.ch
13-
/// \brief Utility to handle the metadata from the AOD
14-
///
1515

1616
#ifndef COMMON_CORE_METADATAHELPER_H_
1717
#define COMMON_CORE_METADATAHELPER_H_
@@ -61,6 +61,9 @@ struct MetadataHelper {
6161
/// @return true if the key is defined, false otherwise. Throws an exception if the key is not found
6262
bool isKeyDefined(const std::string& key) const;
6363

64+
/// @brief Function to create a label with the metadata information, useful e.g. for histogram naming
65+
std::string makeMetadataLabel() const;
66+
6467
private:
6568
std::map<std::string, std::string> mMetadata; /// < The metadata map
6669
bool mIsInitialized = false; /// < Flag to check if the metadata has been initialized
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)