ATLAS Offline Software
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 
19 namespace JetHelper {
20 
23 
24 class JetContext {
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 
39 template <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 
46 template <typename T> T JetContext::getValue(const std::string& name) const {
47  T value;
49  return value;
50 }
51 
52 template <typename T> bool JetContext::setValue(const std::string& name, const T value, bool allowOverwrite) {
53  if(( !allowOverwrite && isAvailable(name)) ) return false;
54 
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 
JetHelper::JetContext
Class JetContext Designed to read AOD information related to the event, N vertices,...
Definition: JetContext.h:24
athena.value
value
Definition: athena.py:122
JetHelper
A tool interface class for tools implementing the IInputVariable interface.
Definition: IInputVariable.h:18
JetHelper::JetContext::isAvailable
bool isAvailable(const std::string &name) const
Definition: JetContext.h:31
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
JetHelper::JetContext::m_dict_
std::unordered_map< std::string, std::variant< int, float > > m_dict_
Definition: JetContext.h:33
JetHelper::JetContext::setValue
bool setValue(const std::string &name, const T value, bool allowOverwrite=false)
Definition: JetContext.h:52
JetHelper::JetContext::getValue
void getValue(const std::string &name, T &value) const
Definition: JetContext.h:39