File indexing completed on 2025-07-14 08:56:25
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 template <typename T> class MetaDataHandle {
0028 public:
0029 MetaDataHandle(const std::string& descriptor, Gaudi::DataHandle::Mode a);
0030 MetaDataHandle(const Gaudi::DataHandle& handle, const std::string& descriptor, Gaudi::DataHandle::Mode a);
0031
0032
0033
0034
0035
0036
0037 const T get() const;
0038
0039
0040
0041
0042
0043 std::optional<T> get_optional() const;
0044
0045
0046
0047
0048
0049 const T get(const T& defaultValue) const;
0050
0051
0052
0053
0054
0055 void put(T);
0056
0057 private:
0058 std::string fullDescriptor() const;
0059
0060 void checkPodioDataSvc();
0061
0062 private:
0063 ServiceHandle<IDataProviderSvc> m_eds;
0064 std::string m_descriptor;
0065 PodioDataSvc* m_podio_data_service{nullptr};
0066 const Gaudi::DataHandle* m_dataHandle{nullptr};
0067 Gaudi::DataHandle::Mode m_mode;
0068 };
0069
0070
0071 template <typename T>
0072 MetaDataHandle<T>::MetaDataHandle(const std::string& descriptor, Gaudi::DataHandle::Mode a)
0073 : m_eds("EventDataSvc", "DataHandle"), m_descriptor(descriptor), m_mode(a) {
0074 m_eds.retrieve().ignore();
0075 m_podio_data_service = dynamic_cast<PodioDataSvc*>(m_eds.get());
0076 checkPodioDataSvc();
0077 }
0078
0079
0080 template <typename T>
0081 MetaDataHandle<T>::MetaDataHandle(const Gaudi::DataHandle& handle, const std::string& descriptor,
0082 Gaudi::DataHandle::Mode a)
0083 : m_eds("EventDataSvc", "DataHandle"), m_descriptor(descriptor), m_dataHandle(&handle), m_mode(a) {
0084 m_eds.retrieve().ignore();
0085 m_podio_data_service = dynamic_cast<PodioDataSvc*>(m_eds.get());
0086 checkPodioDataSvc();
0087 }
0088
0089
0090 template <typename T> std::optional<T> MetaDataHandle<T>::get_optional() const {
0091 if (m_podio_data_service) {
0092 return m_podio_data_service->getMetaDataFrame().getParameter<T>(fullDescriptor());
0093 }
0094 return k4FWCore::getParameter<T>(fullDescriptor());
0095 }
0096
0097
0098 template <typename T> const T MetaDataHandle<T>::get() const {
0099 auto optional_parameter = get_optional();
0100 if (!optional_parameter.has_value()) {
0101 throw GaudiException("MetaDataHandle empty handle access",
0102 "MetaDataHandle " + fullDescriptor() + " not (yet?) available", StatusCode::FAILURE);
0103 }
0104 return optional_parameter.value();
0105 }
0106
0107
0108 template <typename T> const T MetaDataHandle<T>::get(const T& defaultValue) const {
0109 return get_optional().value_or(defaultValue);
0110 }
0111
0112
0113 template <typename T> void MetaDataHandle<T>::put(T value) {
0114 if (m_mode != Gaudi::DataHandle::Writer)
0115 throw GaudiException("MetaDataHandle policy violation", "Put for non-writing MetaDataHandle not allowed",
0116 StatusCode::FAILURE);
0117
0118
0119
0120 std::string full_descriptor = fullDescriptor();
0121
0122 if (m_podio_data_service) {
0123 if (m_podio_data_service->targetFSMState() == Gaudi::StateMachine::RUNNING) {
0124 throw GaudiException("MetaDataHandle policy violation", "Put cannot be used during the event loop",
0125 StatusCode::FAILURE);
0126 }
0127 podio::Frame& frame = m_podio_data_service->getMetaDataFrame();
0128 frame.putParameter(full_descriptor, value);
0129
0130 } else {
0131 k4FWCore::putParameter(full_descriptor, value);
0132 }
0133 }
0134
0135
0136 template <typename T> std::string MetaDataHandle<T>::fullDescriptor() const {
0137 if (nullptr != m_dataHandle) {
0138 auto full_descriptor = podio::collMetadataParamName(m_dataHandle->objKey(), m_descriptor);
0139
0140 if (m_mode == Gaudi::DataHandle::Reader && full_descriptor.find("/Event/") == 0u) {
0141 full_descriptor.erase(0, 7);
0142 }
0143 return full_descriptor;
0144 }
0145
0146 return m_descriptor;
0147 }
0148
0149
0150 template <typename T> void MetaDataHandle<T>::checkPodioDataSvc() {
0151
0152 const std::string cmd = System::cmdLineArgs()[0];
0153 if (cmd.find("genconf") != std::string::npos)
0154 return;
0155
0156 if (!m_podio_data_service && !Gaudi::svcLocator()->service<IMetadataSvc>("MetadataSvc", false)) {
0157 std::cout << "Warning: MetaDataHandles require the PodioDataSvc or for compatibility the MetadataSvc" << std::endl;
0158 }
0159 }
0160
0161 #endif