Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-04 09:46:42

0001 /*
0002  * Copyright (c) 2014-2024 Key4hep-Project.
0003  *
0004  * This file is part of Key4hep.
0005  * See https://key4hep.github.io/key4hep-doc/ for further info.
0006  *
0007  * Licensed under the Apache License, Version 2.0 (the "License");
0008  * you may not use this file except in compliance with the License.
0009  * You may obtain a copy of the License at
0010  *
0011  *     http://www.apache.org/licenses/LICENSE-2.0
0012  *
0013  * Unless required by applicable law or agreed to in writing, software
0014  * distributed under the License is distributed on an "AS IS" BASIS,
0015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016  * See the License for the specific language governing permissions and
0017  * limitations under the License.
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   /// Get the value that is stored in this MetaDataHandle
0036   ///
0037   /// @returns The value for this MetaDataHandle
0038   ///
0039   /// @throws GaudiException in case the value is not (yet) available
0040   const T get() const;
0041 
0042   /// Get the (optional) value that is stored in this MetaDataHandle
0043   ///
0044   /// @returns An optional that contains the value if it was available from the
0045   ///          data store and is not engaged otherwise
0046   std::optional<T> get_optional() const;
0047 
0048   /// Get the value that is stored in the MetaDataHandle or the provided default
0049   /// value in case that is not available
0050   ///
0051   /// @returns The value stored in the Handle or the default value
0052   const T get(const T& defaultValue) const;
0053 
0054   /// Set the value for this MetaDataHandle
0055   ///
0056   /// @note This can only be called during initialize and/or finalize but not
0057   /// during execute for algorithms that use it
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}; // holds the identifier in case we do collection metadata
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   // check whether we are in the proper State
0125   // put is only allowed in the initialization
0126 
0127   std::string full_descriptor = fullDescriptor();
0128   // DataHandle based algorithms
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     // Functional algorithms
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     // remove the "/Event/" part of the collections' object key if in read mode
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   // do not do this check during the genconf step
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 } // namespace k4FWCore
0170 
0171 template <typename T>
0172 using MetaDataHandle [[deprecated("Use k4FWCore::MetaDataHandle instead")]] = k4FWCore::MetaDataHandle<T>;
0173 
0174 #endif