Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-14 08:56:25

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 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   /// Get the value that is stored in this MetaDataHandle
0033   ///
0034   /// @returns The value for this MetaDataHandle
0035   ///
0036   /// @throws GaudiException in case the value is not (yet) available
0037   const T get() const;
0038 
0039   /// Get the (optional) value that is stored in this MetaDataHandle
0040   ///
0041   /// @returns An optional that contains the value if it was available from the
0042   ///          data store and is not engaged otherwise
0043   std::optional<T> get_optional() const;
0044 
0045   /// Get the value that is stored in the MetaDataHandle or the provided default
0046   /// value in case that is not available
0047   ///
0048   /// @returns The value stored in the Handle or the default value
0049   const T get(const T& defaultValue) const;
0050 
0051   /// Set the value for this MetaDataHandle
0052   ///
0053   /// @note This can only be called during initialize and/or finalize but not
0054   /// during execute for algorithms that use it
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};  // holds the identifier in case we do collection metadata
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   // check whether we are in the proper State
0118   // put is only allowed in the initialization
0119 
0120   std::string full_descriptor = fullDescriptor();
0121   // DataHandle based algorithms
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     // Functional algorithms
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     // remove the "/Event/" part of the collections' object key if in read mode
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   // do not do this check during the genconf step
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