Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:19:17

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