ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
ForwardDetectors
ZDC
ZdcAnalysis
ZdcAnalysis
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
15
class
ZDCJSONConfig
16
{
17
public
:
18
using
JSON
= nlohmann::json;
19
using
JSONParamDescr
= std::tuple<JSON::value_t, int, bool, bool>;
20
using
JSONParamList
= std::map<std::string, JSONParamDescr>;
21
22
private
:
23
24
size_t
m_nSides
{0};
25
std::vector<std::string>
m_sideLabels
;
26
size_t
m_numChannelsPerSide
{0};
27
bool
m_haveConfig
{
false
};
28
JSON
m_globalConfig
;
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,
int
paramSize);
96
97
98
public
:
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
{
105
m_channelConfig
.assign(
m_nSides
*
m_numChannelsPerSide
,
JSON
());
106
}
107
108
~ZDCJSONConfig
() {}
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
size
size_t size() const
Number of registered mappings.
JSON
ZDCJSONConfig::JSON JSON
Definition
ZDCPulseAnalyzer.cxx:22
ZDCJSONConfig::m_haveConfig
bool m_haveConfig
Definition
ZDCJSONConfig.h:27
ZDCJSONConfig::ParseConfig
std::pair< bool, std::string > ParseConfig(const JSON &config, const JSONParamList &JSONConfigParams)
Definition
ZDCJSONConfig.cxx:9
ZDCJSONConfig::haveConfig
bool haveConfig() const
Definition
ZDCJSONConfig.h:112
ZDCJSONConfig::m_nSides
size_t m_nSides
Definition
ZDCJSONConfig.h:24
ZDCJSONConfig::~ZDCJSONConfig
~ZDCJSONConfig()
Definition
ZDCJSONConfig.h:108
ZDCJSONConfig::JSONParamList
std::map< std::string, JSONParamDescr > JSONParamList
Definition
ZDCJSONConfig.h:20
ZDCJSONConfig::getGlobalParam
bool getGlobalParam(std::string key, T &returnValue)
Definition
ZDCJSONConfig.h:114
ZDCJSONConfig::ZDCJSONConfig
ZDCJSONConfig(const std::vector< std::string > &sideNames, size_t numChannelsPerSide)
Definition
ZDCJSONConfig.h:100
ZDCJSONConfig::m_channelConfig
std::vector< JSON > m_channelConfig
Definition
ZDCJSONConfig.h:30
ZDCJSONConfig::ParsePerChannelParams
std::pair< bool, std::string > ParsePerChannelParams(const std::string ¶mKey, const T ¶mValue, JSON::value_t paramType, int paramSize)
Definition
ZDCJSONConfig.cxx:88
ZDCJSONConfig::setAllParameter
void setAllParameter(const std::string &key, T value)
Definition
ZDCJSONConfig.h:61
ZDCJSONConfig::checkType
bool checkType(T value, JSON::value_t paramType, int paramSize=-1)
Definition
ZDCJSONConfig.h:33
ZDCJSONConfig::JSONParamDescr
std::tuple< JSON::value_t, int, bool, bool > JSONParamDescr
Definition
ZDCJSONConfig.h:19
ZDCJSONConfig::getChannelConfig
const JSON & getChannelConfig(size_t side, size_t channel) const
Definition
ZDCJSONConfig.h:134
ZDCJSONConfig::setChannelParameter
void setChannelParameter(unsigned int side, unsigned int chanIndex, const std::string &key, T value)
Definition
ZDCJSONConfig.h:74
ZDCJSONConfig::m_numChannelsPerSide
size_t m_numChannelsPerSide
Definition
ZDCJSONConfig.h:26
ZDCJSONConfig::JSON
nlohmann::json JSON
Definition
ZDCJSONConfig.h:18
ZDCJSONConfig::m_sideLabels
std::vector< std::string > m_sideLabels
Definition
ZDCJSONConfig.h:25
ZDCJSONConfig::dumpGlobal
auto dumpGlobal()
Definition
ZDCJSONConfig.h:132
ZDCJSONConfig::setPerSideParameter
void setPerSideParameter(unsigned int side, std::string key, T value)
Definition
ZDCJSONConfig.h:81
ZDCJSONConfig::m_globalConfig
JSON m_globalConfig
Definition
ZDCJSONConfig.h:28
find
std::string find(const std::string &s)
return a remapped string
Definition
hcg.cxx:140
index
Definition
index.py:1
Generated on
for ATLAS Offline Software by
1.17.0