ATLAS Offline Software
Loading...
Searching...
No Matches
ZDCJSONConfig.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4#ifndef _ZDCJSONConfig_h
5#define _ZDCJSONConfig_h
6
7#include <nlohmann/json.hpp>
8#include <tuple>
9#include <map>
10#include <vector>
11#include <string>
12#include <cmath> //std::abs, std::floor
13
14
16{
17public:
18 using JSON = nlohmann::json;
19 using JSONParamDescr = std::tuple<JSON::value_t, unsigned int, bool, bool>;
20 using JSONParamList = std::map<std::string, JSONParamDescr>;
21
22private:
23
24 size_t m_nSides{0};
25 std::vector<std::string> m_sideLabels;
27 bool m_haveConfig{false};
29
30 std::vector<JSON> m_channelConfig;
31
32 template<typename T> bool
33 checkType(T value, JSON::value_t paramType, int paramSize = -1)
34 {
35 if (value.is_null()) return false;
36
37 if (value.type() == paramType) {
38 if (paramSize == -1) return true;
39 else if (value.size() == size_t(paramSize)) return true;
40 return false;
41 }
42
43 if (value.is_primitive()) {
44 if (paramType == JSON::value_t::number_float) {
45 if (value.type() == JSON::value_t::number_integer ||
46 value.type() == JSON::value_t::number_unsigned) return true;
47 }
48 else if (paramType == JSON::value_t::number_integer &&
49 value.type() == JSON::value_t::number_unsigned) return true;
50 else if (paramType == JSON::value_t::number_integer &&
51 value.type() == JSON::value_t::number_float) {
52 float fvalue = value;
53 if (std::abs(std::floor(fvalue)-fvalue) < 1e-6) return true;
54 }
55 }
56
57 return false;
58 }
59
60 template<typename T> void
61 setAllParameter(const std::string & key, T value)
62 {
63 for (auto& chanConfig : m_channelConfig) {
64 //
65 // Look to see if a value is already assigned, if so don't override
66 //
67 if (chanConfig.find(key) == chanConfig.end()) {
68 chanConfig[key] = value;
69 }
70 }
71 }
72
73 template<typename T> void
74 setChannelParameter(unsigned int side, unsigned int chanIndex, const std::string& key, T value)
75 {
76 unsigned int index = side*m_numChannelsPerSide + chanIndex;
77 m_channelConfig[index][key] = value;
78 }
79
80 template<typename T> void
81 setPerSideParameter(unsigned int side, std::string key, T value)
82 {
83 for (unsigned int chan = 0; chan < m_numChannelsPerSide; chan++) {
84 unsigned int index = side*m_numChannelsPerSide + chan;
85 //
86 // Look to see if a value is already assigned, if so don't override
87 //
88 if (m_channelConfig[index].find(key) == m_channelConfig[index].end()) {
89 m_channelConfig[index][key] = value;
90 }
91 }
92 }
93
94 template<typename T> std::pair<bool, std::string>
95 ParsePerChannelParams(const std::string& paramKey, const T& paramValue, JSON::value_t paramType, size_t paramSize);
96
97
98public:
99
100 ZDCJSONConfig(const std::vector<std::string>& sideNames, size_t numChannelsPerSide) :
101 m_nSides(sideNames.size()),
102 m_sideLabels(sideNames),
103 m_numChannelsPerSide(numChannelsPerSide)
104 {
106 }
107
109
110 std::pair<bool, std::string> ParseConfig(const JSON& config, const JSONParamList& JSONConfigParams);
111
112 bool haveConfig() const {return m_haveConfig;}
113
114 template<typename T> bool getGlobalParam(std::string key, T& returnValue)
115 {
116 if (!haveConfig()) return false;
117
118 try {
119 auto iter = m_globalConfig.find(key);
120 if (iter == m_globalConfig.end()) return false;
121 if (iter.value().is_null()) return false;
122
123 returnValue = iter.value().get_to(returnValue);
124 }
125 catch (...) {
126 return false;
127 }
128
129 return true;
130 }
131
132 auto dumpGlobal() {return m_globalConfig.dump(4);}
133
134 const JSON& getChannelConfig(size_t side, size_t channel) const {
135 if (!(side < m_nSides && channel < m_numChannelsPerSide)) throw std::runtime_error("ZDCJSONConfig::getChannelConfig(): bad arm or side index");
136 size_t index = side*m_numChannelsPerSide + channel;
137 return m_channelConfig[index];
138 }
139};
140
141#endif
ZDCJSONConfig::JSON JSON
std::pair< bool, std::string > ParseConfig(const JSON &config, const JSONParamList &JSONConfigParams)
bool haveConfig() const
std::map< std::string, JSONParamDescr > JSONParamList
bool getGlobalParam(std::string key, T &returnValue)
ZDCJSONConfig(const std::vector< std::string > &sideNames, size_t numChannelsPerSide)
std::vector< JSON > m_channelConfig
void setAllParameter(const std::string &key, T value)
bool checkType(T value, JSON::value_t paramType, int paramSize=-1)
const JSON & getChannelConfig(size_t side, size_t channel) const
void setChannelParameter(unsigned int side, unsigned int chanIndex, const std::string &key, T value)
size_t m_numChannelsPerSide
std::tuple< JSON::value_t, unsigned int, bool, bool > JSONParamDescr
nlohmann::json JSON
std::vector< std::string > m_sideLabels
void setPerSideParameter(unsigned int side, std::string key, T value)
std::pair< bool, std::string > ParsePerChannelParams(const std::string &paramKey, const T &paramValue, JSON::value_t paramType, size_t paramSize)
std::string find(const std::string &s)
return a remapped string
Definition hcg.cxx:138
Definition index.py:1