ATLAS Offline Software
Loading...
Searching...
No Matches
JetContext.h
Go to the documentation of this file.
1/*
2 * @file JetContext.h
3 * @author A. Freeman (swissarthurfreeman@gmail.com)
4 * @brief a class for storing arbitrary event data.
5 * @date 2022-06-01
6 *
7 * Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
8 *
9 */
10
11#ifndef JETTOOLHELPERS_JETCONTEXT_H
12#define JETTOOLHELPERS_JETCONTEXT_H
13
14#include <unordered_map>
15#include <stdexcept>
16#include <variant>
17#include <type_traits>
18
19namespace JetHelper {
20
23
25 public:
26
27 template <typename T> bool setValue(const std::string& name, const T value, bool allowOverwrite = false);
28 template <typename T> void getValue(const std::string& name, T& value) const;
29 template <typename T> T getValue(const std::string& name) const;
30
31 bool isAvailable(const std::string& name) const {
32 return m_dict_.find(name) != m_dict_.end();
33 };
34
35 private:
36 std::unordered_map<std::string, std::variant<int, float>> m_dict_;
37};
38
39template <typename T> void JetContext::getValue(const std::string& name, T& value) const {
40 if(isAvailable(name))
41 value = std::get<T>(m_dict_.at(name));
42 else
43 throw std::invalid_argument(std::string("Key Error : ") + name + std::string(" not found in JetContext."));
44}
45
46template <typename T> T JetContext::getValue(const std::string& name) const {
47 T value;
48 getValue(name, value);
49 return value;
50}
51
52template <typename T> bool JetContext::setValue(const std::string& name, const T value, bool allowOverwrite) {
53 if(( !allowOverwrite && isAvailable(name)) ) return false;
54
55 if constexpr (!std::is_same<T, int>::value && !std::is_same<T, float>::value) {
56 static_assert(std::is_same<T, double>::value, "Unsupported type provided, please use integers or doubles.");
57 m_dict_.insert_or_assign(name, (float) value);
58 } else {
59 m_dict_.insert_or_assign(name, value); // insert returns pair with iterator and return code
60 }
61 return true; // if true => insertion, if false => assignement.
62}
63} // namespace JetHelper
64#endif
65
Class JetContext Designed to read AOD information related to the event, N vertices,...
Definition JetContext.h:24
std::unordered_map< std::string, std::variant< int, float > > m_dict_
Definition JetContext.h:36
bool setValue(const std::string &name, const T value, bool allowOverwrite=false)
Definition JetContext.h:52
bool isAvailable(const std::string &name) const
Definition JetContext.h:31
void getValue(const std::string &name, T &value) const
Definition JetContext.h:39
class IJetCalibStep