Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:04:37

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 // GAUDI
0023 #include "GaudiAlg/GaudiAlgorithm.h"
0024 
0025 #include "k4FWCore/PodioDataSvc.h"
0026 #include "podio/GenericParameters.h"
0027 
0028 #include "GaudiKernel/MsgStream.h"
0029 
0030 #include <type_traits>
0031 
0032 template <typename T> class MetaDataHandle {
0033 public:
0034   MetaDataHandle();
0035   MetaDataHandle(const std::string& descriptor, Gaudi::DataHandle::Mode a);
0036   MetaDataHandle(const Gaudi::DataHandle& handle, const std::string& descriptor, Gaudi::DataHandle::Mode a);
0037   ~MetaDataHandle();
0038 
0039   const T get() const;
0040   void    put(T);
0041 
0042 private:
0043   std::string fullDescriptor() const;
0044   void        checkPodioDataSvc();
0045 
0046 private:
0047   ServiceHandle<IDataProviderSvc> m_eds;
0048   std::string                     m_descriptor;
0049   PodioDataSvc*                   m_podio_data_service{nullptr};
0050   const Gaudi::DataHandle*        m_dataHandle{nullptr};  // holds the identifier in case we do collection metadata
0051   Gaudi::DataHandle::Mode         m_mode;
0052 };
0053 
0054 template <typename T> MetaDataHandle<T>::~MetaDataHandle() {}
0055 
0056 //---------------------------------------------------------------------------
0057 template <typename T>
0058 MetaDataHandle<T>::MetaDataHandle(const std::string& descriptor, Gaudi::DataHandle::Mode a)
0059     : m_eds("EventDataSvc", "DataHandle"), m_descriptor(descriptor), m_mode(a) {
0060   StatusCode sc        = m_eds.retrieve();
0061   m_podio_data_service = dynamic_cast<PodioDataSvc*>(m_eds.get());
0062   checkPodioDataSvc();
0063 }
0064 
0065 //---------------------------------------------------------------------------
0066 template <typename T>
0067 MetaDataHandle<T>::MetaDataHandle(const Gaudi::DataHandle& handle, const std::string& descriptor,
0068                                   Gaudi::DataHandle::Mode a)
0069     : m_eds("EventDataSvc", "DataHandle"), m_descriptor(descriptor), m_dataHandle(&handle), m_mode(a) {
0070   StatusCode sc        = m_eds.retrieve();
0071   m_podio_data_service = dynamic_cast<PodioDataSvc*>(m_eds.get());
0072   checkPodioDataSvc();
0073 }
0074 
0075 //---------------------------------------------------------------------------
0076 template <typename T> const T MetaDataHandle<T>::get() const {
0077   const auto& frame = m_podio_data_service->getMetaDataFrame();
0078   return frame.getParameter<T>(fullDescriptor());
0079 }
0080 
0081 //---------------------------------------------------------------------------
0082 template <typename T> void MetaDataHandle<T>::put(T value) {
0083   if (m_mode != Gaudi::DataHandle::Writer)
0084     throw GaudiException("MetaDataHandle policy violation", "Put for non-writing MetaDataHandle not allowed",
0085                          StatusCode::FAILURE);
0086   // check whether we are in the proper State
0087   // put is only allowed in the initalization
0088   if (m_podio_data_service->targetFSMState() == Gaudi::StateMachine::RUNNING) {
0089     throw GaudiException("MetaDataHandle policy violation", "Put cannot be used during the event loop",
0090                          StatusCode::FAILURE);
0091   }
0092   std::string   full_descriptor = fullDescriptor();
0093   podio::Frame& frame           = m_podio_data_service->getMetaDataFrame();
0094   frame.putParameter(full_descriptor, value);
0095 }
0096 
0097 //---------------------------------------------------------------------------
0098 template <typename T> std::string MetaDataHandle<T>::fullDescriptor() const {
0099   if (nullptr != m_dataHandle) {
0100     auto full_descriptor = podio::collMetadataParamName(m_dataHandle->objKey(), m_descriptor);
0101     // remove the "/Event/" part of the collections' object key if in read mode
0102     if (m_mode == Gaudi::DataHandle::Reader && full_descriptor.find("/Event/") == 0u) {
0103       full_descriptor.erase(0, 7);
0104     }
0105     return full_descriptor;
0106   }
0107 
0108   return m_descriptor;
0109 }
0110 
0111 //---------------------------------------------------------------------------
0112 template <typename T> void MetaDataHandle<T>::checkPodioDataSvc() {
0113   // do not do this check during the genconf step
0114   const std::string cmd = System::cmdLineArgs()[0];
0115   if (cmd.find("genconf") != std::string::npos)
0116     return;
0117 
0118   if (nullptr == m_podio_data_service) {
0119     std::cout << "ERROR: MetaDataHandles require the PodioDataSvc" << std::endl;
0120   }
0121 }
0122 
0123 #endif