ATLAS Offline Software
Loading...
Searching...
No Matches
ZDCJSONConfig Class Reference

#include <ZDCJSONConfig.h>

Collaboration diagram for ZDCJSONConfig:

Public Types

using JSON = nlohmann::json
using JSONParamDescr = std::tuple<JSON::value_t, unsigned int, bool, bool>
using JSONParamList = std::map<std::string, JSONParamDescr>

Public Member Functions

 ZDCJSONConfig (const std::vector< std::string > &sideNames, size_t numChannelsPerSide)
 ~ZDCJSONConfig ()
std::pair< bool, std::string > ParseConfig (const JSON &config, const JSONParamList &JSONConfigParams)
bool haveConfig () const
template<typename T>
bool getGlobalParam (std::string key, T &returnValue)
auto dumpGlobal ()
const JSONgetChannelConfig (size_t side, size_t channel) const

Private Member Functions

template<typename T>
bool checkType (T value, JSON::value_t paramType, int paramSize=-1)
template<typename T>
void setAllParameter (const std::string &key, T value)
template<typename T>
void setChannelParameter (unsigned int side, unsigned int chanIndex, const std::string &key, T value)
template<typename T>
void setPerSideParameter (unsigned int side, std::string key, T value)
template<typename T>
std::pair< bool, std::string > ParsePerChannelParams (const std::string &paramKey, const T &paramValue, JSON::value_t paramType, size_t paramSize)

Private Attributes

size_t m_nSides {0}
std::vector< std::string > m_sideLabels
size_t m_numChannelsPerSide {0}
bool m_haveConfig {false}
JSON m_globalConfig
std::vector< JSONm_channelConfig

Detailed Description

Definition at line 15 of file ZDCJSONConfig.h.

Member Typedef Documentation

◆ JSON

using ZDCJSONConfig::JSON = nlohmann::json

Definition at line 18 of file ZDCJSONConfig.h.

◆ JSONParamDescr

using ZDCJSONConfig::JSONParamDescr = std::tuple<JSON::value_t, unsigned int, bool, bool>

Definition at line 19 of file ZDCJSONConfig.h.

◆ JSONParamList

using ZDCJSONConfig::JSONParamList = std::map<std::string, JSONParamDescr>

Definition at line 20 of file ZDCJSONConfig.h.

Constructor & Destructor Documentation

◆ ZDCJSONConfig()

ZDCJSONConfig::ZDCJSONConfig ( const std::vector< std::string > & sideNames,
size_t numChannelsPerSide )
inline

Definition at line 100 of file ZDCJSONConfig.h.

100 :
101 m_nSides(sideNames.size()),
102 m_sideLabels(sideNames),
103 m_numChannelsPerSide(numChannelsPerSide)
104 {
106 }
std::vector< JSON > m_channelConfig
size_t m_numChannelsPerSide
nlohmann::json JSON
std::vector< std::string > m_sideLabels

◆ ~ZDCJSONConfig()

ZDCJSONConfig::~ZDCJSONConfig ( )
inline

Definition at line 108 of file ZDCJSONConfig.h.

108{}

Member Function Documentation

◆ checkType()

template<typename T>
bool ZDCJSONConfig::checkType ( T value,
JSON::value_t paramType,
int paramSize = -1 )
inlineprivate

Definition at line 33 of file ZDCJSONConfig.h.

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 }

◆ dumpGlobal()

auto ZDCJSONConfig::dumpGlobal ( )
inline

Definition at line 132 of file ZDCJSONConfig.h.

132{return m_globalConfig.dump(4);}

◆ getChannelConfig()

const JSON & ZDCJSONConfig::getChannelConfig ( size_t side,
size_t channel ) const
inline

Definition at line 134 of file ZDCJSONConfig.h.

134 {
135 if (!(side < m_nSides && channel < m_numChannelsPerSide)) throw std::runtime_error("ZDCJSONConfig::getChannelConfig(): bad arm or side index");
137 return m_channelConfig[index];
138 }
str index
Definition DeMoScan.py:362

◆ getGlobalParam()

template<typename T>
bool ZDCJSONConfig::getGlobalParam ( std::string key,
T & returnValue )
inline

Definition at line 114 of file ZDCJSONConfig.h.

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 }
bool haveConfig() const

◆ haveConfig()

bool ZDCJSONConfig::haveConfig ( ) const
inline

Definition at line 112 of file ZDCJSONConfig.h.

112{return m_haveConfig;}

◆ ParseConfig()

std::pair< bool, std::string > ZDCJSONConfig::ParseConfig ( const JSON & config,
const JSONParamList & JSONConfigParams )

Definition at line 9 of file ZDCJSONConfig.cxx.

10{
11 bool result = true;
12 std::string resultString = "success";
13
14 for (auto [key, value] : config.items()) {
15
16 if (value.is_null()) {
17 result = false;
18 resultString = "Found null value for parameter " + key;
19 break;
20 }
21
22 //
23 // First we look up the parameter in the provided parameter list
24 //
25 auto iter = configParamList.find(key);
26 if (iter == configParamList.end()) {
27 result = false;
28 resultString = "Found unknown parameter: " + key +": JSON=" + config.dump();
29 break;
30 }
31
32 auto paramType = std::get<0>(iter->second);
33 auto paramSize = std::get<1>(iter->second);
34 auto paramSingleOnly = !std::get<2>(iter->second); // the boolean indicates whether the parameter is per-channel settable
35
36 // For single value only, we can do a type check here
37 //
38 if (paramSingleOnly) {
39 if (!checkType(value, paramType, paramSize)) {
40 result = false;
41 resultString = "Incorrect JSON type for parameter " + key;
42 break;
43 }
44
45 //
46 // Set the value for all channels
47 //
48 setAllParameter(key, value);
49
50 // Add it to the list of "global" parameters
51 //
53 }
54 else {
55 //
56 // If the value is per-channel settable, then either the value is an object with
57 // key, value pairs that provide the per-side or per-channel settings or it's
58 // a "single" value (possible an array) that appplies to all channels
59 //
60 if (value.type() == JSON::value_t::object) {
61 auto [ppcResult, ppcResultString] = ParsePerChannelParams(key, value, paramType, paramSize);
62 if (!ppcResult) {
63 result = ppcResult;
64 resultString = std::move(ppcResultString);
65 break;
66 }
67 }
68 else {
69 if (!checkType(value, paramType, paramSize)) {
70 result = false;
71 resultString = "Incorrect JSON type for parameter " + key;
72 break;
73 }
74
75 //
76 // Set the value for all channels
77 //
78 setAllParameter(key, value);
79 }
80 }
81 }
82
83 if (result) m_haveConfig = true;
84 return {result, resultString};
85}
void setAllParameter(const std::string &key, T value)
bool checkType(T value, JSON::value_t paramType, int paramSize=-1)
std::pair< bool, std::string > ParsePerChannelParams(const std::string &paramKey, const T &paramValue, JSON::value_t paramType, size_t paramSize)

◆ ParsePerChannelParams()

template<typename T>
std::pair< bool, std::string > ZDCJSONConfig::ParsePerChannelParams ( const std::string & paramKey,
const T & paramValue,
JSON::value_t paramType,
size_t paramSize )
private

Definition at line 88 of file ZDCJSONConfig.cxx.

89{
90 bool result = true;
91 std::string resultString = "success";
92
93 for (auto [key, value] : paramValue.items()) {
94 try {
95 //
96 // Compare the key against the side labels
97 //
98 //bool validKey = false;
99 for (unsigned int side = 0; side < m_nSides; side++) {
100 size_t srchIdx = key.find(m_sideLabels[side]);
101 if (srchIdx == 0) {
102 //validKey = true;
103
104 // Check to see whether the key length matches the
105 // side label length -- if it does, then we either
106 // have an array specifying the values for all channels
107 // or we have a single value that applies to all channels
108 //
109 if (key.size() == m_sideLabels[side].size()) {
110 if (value.size() == 1) {
111 //
112 // We have per-side value that we set for all channels on that side,
113 // unless it already has had a specific value provided (handled by SetSideParameter)
114 //
115 if (!checkType(value, paramType)) {
116 result = false;
117 resultString = "Incorrect JSON type for parameter " + key;
118 break;
119 }
120
121 setPerSideParameter(side, paramKey, value);
122 break;
123 }
124 else if (paramType == JSON::value_t::array) {
125
126 size_t elementSize = value[0].size();
127 size_t arrayLength = value.size();
128
129 if (arrayLength == m_numChannelsPerSide && elementSize == paramSize) {
130 for (unsigned int chan = 0; chan < m_numChannelsPerSide; chan++) {
131 setChannelParameter(side, chan, paramKey, value[chan]);
132 }
133 }
134 else if (arrayLength == paramSize) {
135 //
136 // We have a single array that is set for all channels
137 //
138 setPerSideParameter(side, paramKey, value);
139 }
140 else {
141 result = false;
142 resultString = "Invalid value format for array parameter " + paramKey + " on side " + m_sideLabels[side];
143 break;
144 }
145 }
146 else if (paramType == JSON::value_t::object) {
147 //
148 // For now do nothing -- need to work out how to handle objects
149 //
150 }
151 else {
152 //
153 // If we get here we must have an array of values
154 //
155 if (value.size() != m_numChannelsPerSide) {
156 result = false;
157 resultString = "Invalid array length for parameter " + paramKey + " on side " + m_sideLabels[side];
158 break;
159 }
160 else {
161 //
162 // The the values for each channel
163 //
164 for (unsigned int chan = 0; chan < m_numChannelsPerSide; chan++) {
165 if (!checkType(value[chan], paramType)) {
166 result = false;
167 resultString = "Incorrect JSON type for parameter " + paramKey + "paramType = " +
168 std::to_string((unsigned int) paramType) + ", value type = " + std::to_string((unsigned int) value[chan].type());
169 break;
170 }
171
172 setChannelParameter(side, chan, paramKey, value[chan]);
173 }
174 }
175 }
176 }
177 else {
178 // The key must be longer than the side label which should mean a specific channel
179 //
180 std::string keyRemainder = key.substr(m_sideLabels[side].size(), std::string::npos);
181 try {
182 int chanIndex = std::stoi(keyRemainder);
183 if (chanIndex < 0 || size_t(chanIndex) > m_numChannelsPerSide) {
184 result = false;
185 resultString = "Invalid channel specifier in values for parameter " + key;
186 break;
187 }
188
189 if (!checkType(value, paramType, paramSize)) {
190 result = false;
191 resultString = "Incorrect JSON type for parameter " + key;
192 break;
193 }
194
195 setChannelParameter(side, chanIndex, paramKey, value);
196 }
197 catch(...) {
198 result = false;
199 resultString = "Invalid channel specifier for parameter " + key;
200 break;
201 }
202 }
203 }
204
205 }
206 }
207 catch (...) {
208 result = false;
209 resultString = "Exception caught while parsing key " + key;
210 }
211 }
212
213 return {result, resultString};
214}
void setChannelParameter(unsigned int side, unsigned int chanIndex, const std::string &key, T value)
void setPerSideParameter(unsigned int side, std::string key, T value)

◆ setAllParameter()

template<typename T>
void ZDCJSONConfig::setAllParameter ( const std::string & key,
T value )
inlineprivate

Definition at line 61 of file ZDCJSONConfig.h.

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 }

◆ setChannelParameter()

template<typename T>
void ZDCJSONConfig::setChannelParameter ( unsigned int side,
unsigned int chanIndex,
const std::string & key,
T value )
inlineprivate

Definition at line 74 of file ZDCJSONConfig.h.

75 {
76 unsigned int index = side*m_numChannelsPerSide + chanIndex;
78 }

◆ setPerSideParameter()

template<typename T>
void ZDCJSONConfig::setPerSideParameter ( unsigned int side,
std::string key,
T value )
inlineprivate

Definition at line 81 of file ZDCJSONConfig.h.

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()) {
90 }
91 }
92 }
std::string find(const std::string &s)
return a remapped string
Definition hcg.cxx:138

Member Data Documentation

◆ m_channelConfig

std::vector<JSON> ZDCJSONConfig::m_channelConfig
private

Definition at line 30 of file ZDCJSONConfig.h.

◆ m_globalConfig

JSON ZDCJSONConfig::m_globalConfig
private

Definition at line 28 of file ZDCJSONConfig.h.

◆ m_haveConfig

bool ZDCJSONConfig::m_haveConfig {false}
private

Definition at line 27 of file ZDCJSONConfig.h.

27{false};

◆ m_nSides

size_t ZDCJSONConfig::m_nSides {0}
private

Definition at line 24 of file ZDCJSONConfig.h.

24{0};

◆ m_numChannelsPerSide

size_t ZDCJSONConfig::m_numChannelsPerSide {0}
private

Definition at line 26 of file ZDCJSONConfig.h.

26{0};

◆ m_sideLabels

std::vector<std::string> ZDCJSONConfig::m_sideLabels
private

Definition at line 25 of file ZDCJSONConfig.h.


The documentation for this class was generated from the following files: