Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-05-11 08:57:27

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 FWCORE_METADATAUTILS_H
0020 #define FWCORE_METADATAUTILS_H
0021 
0022 #include <GaudiKernel/Service.h>
0023 #include "Gaudi/Algorithm.h"
0024 
0025 #include "k4FWCore/IMetadataSvc.h"
0026 
0027 namespace k4FWCore {
0028 
0029   /// @brief Save a metadata parameter in the metadata frame
0030   /// @param name The name of the parameter
0031   /// @param value The value of the parameter
0032   /// @param alg The algorithm that is saving the parameter, typically "this"
0033   template <typename T> void putParameter(const std::string& name, const T& value, const Gaudi::Algorithm* alg) {
0034     auto metadataSvc = alg->service<IMetadataSvc>("MetadataSvc", false);
0035     if (!metadataSvc) {
0036       alg->error() << "MetadataSvc not found" << endmsg;
0037       return;
0038     }
0039     metadataSvc->put<T>(name, value);
0040   }
0041   /// @brief Save a metadata parameter in the metadata frame. Overload for compatibility
0042   /// with the MetadataHandle, don't use!
0043   template <typename T> void putParameter(const std::string& name, const T& value) {
0044     auto metadataSvc = Gaudi::svcLocator()->service<IMetadataSvc>("MetadataSvc", false);
0045     if (!metadataSvc) {
0046       std::cout << "MetadataSvc not found" << std::endl;
0047       return;
0048     }
0049     return metadataSvc->put<T>(name, value);
0050   }
0051   /// @brief Get a metadata parameter from the metadata frame
0052   /// @param name The name of the parameter
0053   /// @param alg The algorithm that is saving the parameter, typically "this"
0054   /// @return std::optional<T> The value of the parameter, if it exists or std::nullopt
0055   template <typename T> std::optional<T> getParameter(const std::string& name, const Gaudi::Algorithm* alg) {
0056     auto metadataSvc = alg->service<IMetadataSvc>("MetadataSvc", false);
0057     if (!metadataSvc) {
0058       alg->error() << "MetadataSvc not found" << endmsg;
0059       return std::nullopt;
0060     }
0061     return metadataSvc->get<T>(name);
0062   }
0063   /// @brief Get a metadata parameter from the metadata frame. Overload for compatibility
0064   /// with the MetadataHandle, don't use!
0065   template <typename T> std::optional<T> getParameter(const std::string& name) {
0066     auto metadataSvc = Gaudi::svcLocator()->service<IMetadataSvc>("MetadataSvc", false);
0067     if (!metadataSvc) {
0068       return std::nullopt;
0069     }
0070     return metadataSvc->get<T>(name);
0071   }
0072 }  // namespace k4FWCore
0073 
0074 #endif  // FWCORE_METADATAUTILS_H