-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a metadata service and a test (#215)
- Loading branch information
Showing
16 changed files
with
579 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2014-2024 Key4hep-Project. | ||
* | ||
* This file is part of Key4hep. | ||
* See https://key4hep.github.io/key4hep-doc/ for further info. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "MetadataSvc.h" | ||
|
||
#include "podio/Frame.h" | ||
|
||
#include <GaudiKernel/AnyDataWrapper.h> | ||
#include <GaudiKernel/IDataProviderSvc.h> | ||
#include <GaudiKernel/Service.h> | ||
|
||
StatusCode MetadataSvc::initialize() { | ||
StatusCode sc = Service::initialize(); | ||
if (sc.isFailure()) { | ||
error() << "Unable to initialize base class Service." << endmsg; | ||
return sc; | ||
} | ||
m_dataSvc = service("EventDataSvc"); | ||
if (!m_dataSvc) { | ||
error() << "Unable to locate the EventDataSvc" << endmsg; | ||
return StatusCode::FAILURE; | ||
} | ||
|
||
m_frame.reset(new podio::Frame()); | ||
|
||
return StatusCode::SUCCESS; | ||
} | ||
|
||
StatusCode MetadataSvc::finalize() { return Service::finalize(); } | ||
|
||
void MetadataSvc::setFrame(podio::Frame&& fr) { m_frame.reset(new podio::Frame(std::move(fr))); } | ||
|
||
DECLARE_COMPONENT(MetadataSvc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 2014-2024 Key4hep-Project. | ||
* | ||
* This file is part of Key4hep. | ||
* See https://key4hep.github.io/key4hep-doc/ for further info. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#ifndef FWCORE_METADATASVC_H | ||
#define FWCORE_METADATASVC_H | ||
|
||
#include "GaudiKernel/IDataProviderSvc.h" | ||
#include "GaudiKernel/Service.h" | ||
|
||
#include "podio/Frame.h" | ||
|
||
#include "k4FWCore/IMetadataSvc.h" | ||
|
||
class MetadataSvc : public extends<Service, IMetadataSvc> { | ||
using extends::extends; | ||
|
||
public: | ||
~MetadataSvc() override = default; | ||
|
||
StatusCode initialize() override; | ||
StatusCode finalize() override; | ||
|
||
protected: | ||
SmartIF<IDataProviderSvc> m_dataSvc; | ||
|
||
std::unique_ptr<podio::Frame> m_frame; | ||
|
||
void setFrame(podio::Frame&& frame) override; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) 2014-2024 Key4hep-Project. | ||
* | ||
* This file is part of Key4hep. | ||
* See https://key4hep.github.io/key4hep-doc/ for further info. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#ifndef FWCORE_IMETADATASERVICE_H | ||
#define FWCORE_IMETADATASERVICE_H | ||
|
||
#include "GaudiKernel/IInterface.h" | ||
|
||
#include "podio/Frame.h" | ||
|
||
class IMetadataSvc : virtual public IInterface { | ||
public: | ||
DeclareInterfaceID(IMetadataSvc, 1, 0); | ||
|
||
std::unique_ptr<podio::Frame> m_frame; | ||
|
||
virtual void setFrame(podio::Frame&& fr) = 0; | ||
template <typename T> void put(const std::string& name, const T& obj) { | ||
if (!m_frame) { | ||
m_frame.reset(new podio::Frame()); | ||
} | ||
m_frame->putParameter(name, obj); | ||
} | ||
template <typename T> std::optional<T> get(const std::string& name) { return m_frame->getParameter<T>(name); } | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) 2014-2024 Key4hep-Project. | ||
* | ||
* This file is part of Key4hep. | ||
* See https://key4hep.github.io/key4hep-doc/ for further info. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#ifndef FWCORE_METADATAUTILS_H | ||
#define FWCORE_METADATAUTILS_H | ||
|
||
#include <GaudiKernel/Service.h> | ||
#include "Gaudi/Algorithm.h" | ||
|
||
#include "k4FWCore/IMetadataSvc.h" | ||
|
||
namespace k4FWCore { | ||
|
||
/// @brief Save a metadata parameter in the metadata frame | ||
/// @param name The name of the parameter | ||
/// @param value The value of the parameter | ||
/// @param alg The algorithm that is saving the parameter, typically "this" | ||
template <typename T> void putParameter(const std::string& name, const T& value, const Gaudi::Algorithm* alg) { | ||
auto metadataSvc = alg->service<IMetadataSvc>("MetadataSvc", false); | ||
if (!metadataSvc) { | ||
alg->error() << "MetadataSvc not found" << endmsg; | ||
return; | ||
} | ||
metadataSvc->put<T>(name, value); | ||
} | ||
/// @brief Save a metadata parameter in the metadata frame. Overload for compatibility | ||
/// with the MetadataHandle, don't use! | ||
template <typename T> void putParameter(const std::string& name, const T& value) { | ||
auto metadataSvc = Gaudi::svcLocator()->service<IMetadataSvc>("MetadataSvc", false); | ||
if (!metadataSvc) { | ||
std::cout << "MetadataSvc not found" << std::endl; | ||
return; | ||
} | ||
return metadataSvc->put<T>(name, value); | ||
} | ||
/// @brief Get a metadata parameter from the metadata frame | ||
/// @param name The name of the parameter | ||
/// @param alg The algorithm that is saving the parameter, typically "this" | ||
/// @return std::optional<T> The value of the parameter, if it exists or std::nullopt | ||
template <typename T> std::optional<T> getParameter(const std::string& name, const Gaudi::Algorithm* alg) { | ||
auto metadataSvc = alg->service<IMetadataSvc>("MetadataSvc", false); | ||
if (!metadataSvc) { | ||
alg->error() << "MetadataSvc not found" << endmsg; | ||
return std::nullopt; | ||
} | ||
return metadataSvc->get<T>(name); | ||
} | ||
/// @brief Get a metadata parameter from the metadata frame. Overload for compatibility | ||
/// with the MetadataHandle, don't use! | ||
template <typename T> std::optional<T> getParameter(const std::string& name) { | ||
auto metadataSvc = Gaudi::svcLocator()->service<IMetadataSvc>("MetadataSvc", false); | ||
if (!metadataSvc) { | ||
return std::nullopt; | ||
} | ||
return metadataSvc->get<T>(name); | ||
} | ||
} // namespace k4FWCore | ||
|
||
#endif // FWCORE_METADATAUTILS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.