File indexing completed on 2026-01-04 09:46:42
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 #ifndef K4FWCORE_METADATAHANDLE_H
0020 #define K4FWCORE_METADATAHANDLE_H
0021
0022 #include <GaudiKernel/DataHandle.h>
0023
0024 #include "k4FWCore/MetadataUtils.h"
0025 #include "k4FWCore/PodioDataSvc.h"
0026
0027 namespace k4FWCore {
0028
0029 template <typename T>
0030 class MetaDataHandle {
0031 public:
0032 MetaDataHandle(const std::string& descriptor, Gaudi::DataHandle::Mode a);
0033 MetaDataHandle(const Gaudi::DataHandle& handle, const std::string& descriptor, Gaudi::DataHandle::Mode a);
0034
0035
0036
0037
0038
0039
0040 const T get() const;
0041
0042
0043
0044
0045
0046 std::optional<T> get_optional() const;
0047
0048
0049
0050
0051
0052 const T get(const T& defaultValue) const;
0053
0054
0055
0056
0057
0058 void put(T);
0059
0060 private:
0061 std::string fullDescriptor() const;
0062
0063 void checkPodioDataSvc();
0064
0065 private:
0066 ServiceHandle<IDataProviderSvc> m_eds;
0067 std::string m_descriptor;
0068 PodioDataSvc* m_podio_data_service{nullptr};
0069 const Gaudi::DataHandle* m_dataHandle{nullptr};
0070 Gaudi::DataHandle::Mode m_mode;
0071 };
0072
0073
0074 template <typename T>
0075 MetaDataHandle<T>::MetaDataHandle(const std::string& descriptor, Gaudi::DataHandle::Mode a)
0076 : m_eds("EventDataSvc", "DataHandle"), m_descriptor(descriptor), m_mode(a) {
0077 m_eds.retrieve().ignore();
0078 m_podio_data_service = dynamic_cast<PodioDataSvc*>(m_eds.get());
0079 checkPodioDataSvc();
0080 }
0081
0082
0083 template <typename T>
0084 MetaDataHandle<T>::MetaDataHandle(const Gaudi::DataHandle& handle, const std::string& descriptor,
0085 Gaudi::DataHandle::Mode a)
0086 : m_eds("EventDataSvc", "DataHandle"), m_descriptor(descriptor), m_dataHandle(&handle), m_mode(a) {
0087 m_eds.retrieve().ignore();
0088 m_podio_data_service = dynamic_cast<PodioDataSvc*>(m_eds.get());
0089 checkPodioDataSvc();
0090 }
0091
0092
0093 template <typename T>
0094 std::optional<T> MetaDataHandle<T>::get_optional() const {
0095 if (m_podio_data_service) {
0096 return m_podio_data_service->getMetaDataFrame().getParameter<T>(fullDescriptor());
0097 }
0098 return k4FWCore::getParameter<T>(fullDescriptor());
0099 }
0100
0101
0102 template <typename T>
0103 const T MetaDataHandle<T>::get() const {
0104 auto optional_parameter = get_optional();
0105 if (!optional_parameter.has_value()) {
0106 throw GaudiException("MetaDataHandle empty handle access",
0107 "MetaDataHandle " + fullDescriptor() + " not (yet?) available", StatusCode::FAILURE);
0108 }
0109 return optional_parameter.value();
0110 }
0111
0112
0113 template <typename T>
0114 const T MetaDataHandle<T>::get(const T& defaultValue) const {
0115 return get_optional().value_or(defaultValue);
0116 }
0117
0118
0119 template <typename T>
0120 void MetaDataHandle<T>::put(T value) {
0121 if (m_mode != Gaudi::DataHandle::Writer)
0122 throw GaudiException("MetaDataHandle policy violation", "Put for non-writing MetaDataHandle not allowed",
0123 StatusCode::FAILURE);
0124
0125
0126
0127 std::string full_descriptor = fullDescriptor();
0128
0129 if (m_podio_data_service) {
0130 if (m_podio_data_service->targetFSMState() == Gaudi::StateMachine::RUNNING) {
0131 throw GaudiException("MetaDataHandle policy violation", "Put cannot be used during the event loop",
0132 StatusCode::FAILURE);
0133 }
0134 podio::Frame& frame = m_podio_data_service->getMetaDataFrame();
0135 frame.putParameter(full_descriptor, value);
0136
0137 } else {
0138 k4FWCore::putParameter(full_descriptor, value);
0139 }
0140 }
0141
0142
0143 template <typename T>
0144 std::string MetaDataHandle<T>::fullDescriptor() const {
0145 if (nullptr != m_dataHandle) {
0146 auto full_descriptor = podio::collMetadataParamName(m_dataHandle->objKey(), m_descriptor);
0147
0148 if (m_mode == Gaudi::DataHandle::Reader && full_descriptor.find("/Event/") == 0u) {
0149 full_descriptor.erase(0, 7);
0150 }
0151 return full_descriptor;
0152 }
0153
0154 return m_descriptor;
0155 }
0156
0157
0158 template <typename T>
0159 void MetaDataHandle<T>::checkPodioDataSvc() {
0160
0161 const std::string cmd = System::cmdLineArgs()[0];
0162 if (cmd.find("genconf") != std::string::npos)
0163 return;
0164
0165 if (!m_podio_data_service && !Gaudi::svcLocator()->service<IMetadataSvc>("MetadataSvc", false)) {
0166 std::cout << "Warning: MetaDataHandles require the PodioDataSvc or for compatibility the MetadataSvc" << std::endl;
0167 }
0168 }
0169 }
0170
0171 template <typename T>
0172 using MetaDataHandle [[deprecated("Use k4FWCore::MetaDataHandle instead")]] = k4FWCore::MetaDataHandle<T>;
0173
0174 #endif