ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
DataQuality
DataQualityInterfaces
src
MiniConfig.cxx
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3
*/
4
5
// **********************************************************************
6
// **********************************************************************
7
#include <algorithm>
8
#include <fstream>
9
#include <iostream>
10
#include <optional>
11
#include <sstream>
12
#include <utility>
13
#include <cctype>
14
#include <ranges>
15
16
#include "
CxxUtils/StringUtils.h
"
17
18
#include "
DataQualityInterfaces/MiniConfig.h
"
19
20
21
//Get rid of Root macros that confuse Doxygen
23
ClassImp
(
dqi::MiniConfig
)
25
26
27
namespace
dqi
{
28
29
// *********************************************************************
30
// Public Methods
31
// *********************************************************************
32
33
MiniConfig::
34
MiniConfig
()
35
:
m_tree
(0)
36
,
m_propagateDown
(true)
37
{
38
}
39
40
41
MiniConfig::
42
~MiniConfig
()
43
{
44
delete
m_tree
;
45
}
46
47
48
void
49
MiniConfig::
50
AddKeyword
( std::string keyword_ )
51
{
52
const
KeySet_t::value_type& keyval( std::move(keyword_) );
53
m_keywords
.insert( keyval );
54
}
55
56
57
void
58
MiniConfig::
59
AddAttributeKeyword
( std::string keyword_ )
60
{
61
const
KeySet_t::value_type& keyval( std::move(keyword_) );
62
m_attKeywords
.insert( keyval );
63
}
64
65
void
66
MiniConfig::
67
SetAttribKeywordPropagateDown
(
bool
propagateDown )
68
{
69
this->
m_propagateDown
= propagateDown;
70
}
71
72
bool
73
MiniConfig::
74
ReadFile
( std::string fileName )
75
{
76
bool
success(
true
);
77
78
delete
m_tree
;
79
m_tree
=
new
MiniConfigTreeNode
(
"global"
, 0 );
80
m_tree
->SetAttribKeywordPropagateDown(
m_propagateDown
);
81
MiniConfigTreeNode
*
node
=
m_tree
;
82
83
std::ifstream
file
( fileName.c_str() );
84
if
( !
file
) {
85
std::cerr <<
"MiniConfig::ReadFile(): "
86
<<
"cannot read from file: "
<< fileName <<
"\n"
;
87
return
false
;
88
}
89
90
std::string line;
91
char
c;
92
std::string key;
93
std::string id;
94
std::string sep;
95
std::string att;
96
std::string val;
97
int
skipCount(0);
98
int
lineNumber = 0;
99
100
while
( getline(
file
,line) ) {
101
++lineNumber;
102
std::istringstream linestream(line);
103
c = 0;
104
105
while
( linestream.get(c) ) {
106
// ignore leading whitespace
107
if
( !isspace(c) ) {
108
break
;
109
}
110
}
111
112
// ignore empty lines
113
if
( c == 0 || isspace(c) ) {
114
continue
;
115
}
116
117
// ignore comments
118
if
( c ==
'#'
) {
119
continue
;
120
}
121
122
linestream.putback(c);
123
124
// check for: }
125
linestream >> sep;
126
if
( !linestream ) {
127
std::cerr <<
"MiniConfig::ReadFile(): "
128
<<
"badly formatted line: \""
<< line <<
"\", line number "
<< lineNumber <<
"\n"
;
129
success =
false
;
130
continue
;
131
}
132
if
( sep ==
"}"
) {
133
if
( skipCount > 0 ) {
134
--skipCount;
135
}
136
else
{
137
node
=
node
->GetParent();
138
if
(
node
== 0 ) {
139
std::cerr <<
"MiniConfig::ReadFile(): "
140
<<
"unmatched \"}\", line number "
<< lineNumber <<
"\n"
;
141
success =
false
;
142
node
=
m_tree
;
143
}
144
}
145
continue
;
146
}
147
148
// check for: <att> = <val>
149
att = sep;
150
linestream >> sep;
151
if
( !linestream ) {
152
std::cerr <<
"MiniConfig::ReadFile(): "
153
<<
"badly formatted line: \""
<< line <<
"\", line number "
<< lineNumber <<
"\n"
;
154
success =
false
;
155
continue
;
156
}
157
if
( sep ==
"="
) {
158
val = line.substr(linestream.tellg(), std::string::npos);
159
val =
CxxUtils::trimWhiteSpaces
(val);
160
//linestream >> val;
161
if
( val.size() == 0 ) {
162
std::cerr <<
"MiniConfig::ReadFile(): "
163
<<
"badly formatted line: \""
<< line <<
"\", line number "
<< lineNumber <<
"\n"
;
164
success =
false
;
165
continue
;
166
}
167
if
( skipCount == 0 ) {
168
node
->SetAttribute( att, val,
false
);
169
}
170
continue
;
171
}
172
173
// check for: keyword <identifier> {
174
key = att;
175
// Don't use to_lower_copy in order to avoid gcc 13.1 bug
176
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109703
177
// (Should be fixed in 13.2.)
178
//const std::string& lokey = boost::algorithm::to_lower_copy(key);
179
std::string lokey = key;
180
std::ranges::transform(lokey, lokey.begin(), [](
unsigned
char
c) { return std::tolower(c); });
181
id
= sep;
182
linestream >> sep;
183
if
( !linestream ) {
184
std::cerr <<
"MiniConfig::ReadFile(): "
185
<<
"badly formatted line: \""
<< line <<
"\", line number "
<< lineNumber <<
"\n"
;
186
success =
false
;
187
continue
;
188
}
189
if
( sep ==
"{"
) {
190
if
(
m_keywords
.find(key) !=
m_keywords
.end()
191
||
m_keywords
.find(lokey) !=
m_keywords
.end() ) {
192
node
=
node
->GetNewDaughter(
id
);
193
}
194
else
if
(
m_attKeywords
.find(key) !=
m_attKeywords
.end()
195
||
m_attKeywords
.find(lokey) !=
m_attKeywords
.end() ) {
196
node
->SetAttribute(
id
,
node
->GetPathName(),
true
);
197
node
=
node
->GetNewDaughter(
id
);
198
}
199
else
{
200
skipCount++;
201
}
202
continue
;
203
}
204
205
std::cerr <<
"MiniConfig::ReadFile(): "
206
<<
"badly formatted line: \""
<< line <<
"\", line number "
<< lineNumber <<
"\n"
;
207
success =
false
;
208
}
209
210
return
success;
211
}
212
213
214
std::string
215
MiniConfig::
216
GetStringAttribute
( std::string objName, std::string attName )
const
217
{
218
if
(
m_tree
== 0 ) {
219
std::cerr <<
"MiniConfig::GetStringAttribute(): "
220
<<
"not configured (no file has been read)\n"
;
221
return
std::string(
""
);
222
}
223
224
const
MiniConfigTreeNode
*
node
=
m_tree
->GetNode( objName );
225
if
(
node
== 0 ) {
226
std::cerr <<
"MiniConfig::GetStringAttribute(): "
227
<<
"\""
<< objName <<
"\" does not exist\n"
;
228
return
std::string(
""
);
229
}
230
return
node
->GetAttribute( std::move(attName) );
231
}
232
233
234
int
235
MiniConfig::
236
GetIntAttribute
( std::string objName, std::string attName )
const
237
{
238
if
(
m_tree
== 0 ) {
239
std::cerr <<
"MiniConfig::GetIntAttribute(): "
240
<<
"not configured (no file has been read)\n"
;
241
return
0;
242
}
243
244
const
MiniConfigTreeNode
*
node
=
m_tree
->GetNode( objName );
245
if
(
node
== 0 ) {
246
std::cerr <<
"MiniConfig::GetIntAttribute(): "
247
<<
"\""
<< objName <<
"\" does not exist\n"
;
248
return
0;
249
}
250
251
int
val;
252
std::string valstring =
node
->GetAttribute( attName );
253
std::istringstream valstream(valstring);
254
valstream >> val;
255
if
( !valstream ) {
256
std::cerr <<
"MiniConfig::GetIntAttribute(): "
257
<<
"\""
<< attName <<
"\" not an integer type\n"
;
258
return
0;
259
}
260
261
return
val;
262
}
263
264
265
float
266
MiniConfig::
267
GetFloatAttribute
( std::string objName, std::string attName )
const
268
{
269
if
(
m_tree
== 0 ) {
270
std::cerr <<
"MiniConfig::GetFloatAttribute(): "
271
<<
"not configured (no file has been read)\n"
;
272
return
0;
273
}
274
275
const
MiniConfigTreeNode
*
node
=
m_tree
->GetNode( objName );
276
if
(
node
== 0 ) {
277
std::cerr <<
"MiniConfig::GetFloatAttribute(): "
278
<<
"\""
<< objName <<
"\" does not exist\n"
;
279
return
0;
280
}
281
282
float
val;
283
std::string valstring =
node
->GetAttribute( attName );
284
std::istringstream valstream(valstring);
285
valstream >> val;
286
if
( !valstream ) {
287
std::cerr <<
"MiniConfig::GetFloatAttribute(): object \""
<< objName <<
"\""
288
<<
": \""
<< attName <<
"\" not a floating-point type\n"
;
289
return
0;
290
}
291
292
return
val;
293
}
294
295
296
void
297
MiniConfig::
298
GetAttributeNames
( std::string objName, std::set<std::string>& attSet )
const
299
{
300
attSet.clear();
301
302
if
(
m_tree
== 0 ) {
303
std::cerr <<
"MiniConfig::GetAttributeNames(): "
304
<<
"not configured (no file has been read)\n"
;
305
return
;
306
}
307
308
const
MiniConfigTreeNode
*
node
=
m_tree
->GetNode( objName );
309
if
(
node
== 0 ) {
310
std::cerr <<
"MiniConfig::GetAttributeNames(): "
311
<<
"\""
<< objName <<
"\" does not exist\n"
;
312
return
;
313
}
314
315
node
->GetAttributeNames( attSet );
316
}
317
318
319
void
320
MiniConfig::
321
SendVisitor
(
MiniConfigTreeNode::Visitor
& visitor )
const
322
{
323
if
(
m_tree
== 0 ) {
324
std::cerr <<
"MiniConfig::SendVisitor(): "
325
<<
"not configured (no file has been read)\n"
;
326
}
else
{
327
m_tree
->Accept(visitor);
328
}
329
}
330
331
void
332
MiniConfig::
333
SendWriter
(
MiniConfigTreeNode::Writer
&
writer
)
334
{
335
if
(
m_tree
== 0 ) {
336
std::cerr <<
"MiniConfig::SendWriter(): "
337
<<
"not configured (no file has been read)\n"
;
338
}
else
{
339
m_tree
->Accept(
writer
);
340
}
341
}
342
343
344
}
// namespace dqi
StringUtils.h
writer
std::shared_ptr< HepMC3::Writer > writer
MiniConfig.h
ClassImp
ClassImp(xAOD::Experimental::RFileChecker) namespace xAOD
Definition
RFileChecker.cxx:28
dqi::MiniConfigTreeNode::Visitor
Definition
MiniConfigTreeNode.h:29
dqi::MiniConfigTreeNode::Writer
Definition
MiniConfigTreeNode.h:35
dqi::MiniConfigTreeNode
A node of a tree structure holding a configuration, where each node may be given attributes,...
Definition
MiniConfigTreeNode.h:26
dqi::MiniConfig
description
Definition
MiniConfig.h:25
dqi::MiniConfig::ReadFile
virtual bool ReadFile(std::string fileName)
Definition
MiniConfig.cxx:74
dqi::MiniConfig::m_propagateDown
bool m_propagateDown
Definition
MiniConfig.h:61
dqi::MiniConfig::AddAttributeKeyword
virtual void AddAttributeKeyword(std::string keyword_)
Definition
MiniConfig.cxx:59
dqi::MiniConfig::GetStringAttribute
virtual std::string GetStringAttribute(std::string objName, std::string attName) const
Definition
MiniConfig.cxx:216
dqi::MiniConfig::m_keywords
KeySet_t m_keywords
Definition
MiniConfig.h:57
dqi::MiniConfig::m_tree
MiniConfigTreeNode * m_tree
Definition
MiniConfig.h:59
dqi::MiniConfig::AddKeyword
virtual void AddKeyword(std::string keyword_)
Definition
MiniConfig.cxx:50
dqi::MiniConfig::SetAttribKeywordPropagateDown
virtual void SetAttribKeywordPropagateDown(bool propagateDown)
Definition
MiniConfig.cxx:67
dqi::MiniConfig::GetFloatAttribute
virtual float GetFloatAttribute(std::string objName, std::string attName) const
Definition
MiniConfig.cxx:267
dqi::MiniConfig::GetIntAttribute
virtual int GetIntAttribute(std::string objName, std::string attName) const
Definition
MiniConfig.cxx:236
dqi::MiniConfig::~MiniConfig
virtual ~MiniConfig()
Definition
MiniConfig.cxx:42
dqi::MiniConfig::SendVisitor
virtual void SendVisitor(MiniConfigTreeNode::Visitor &visitor) const
Definition
MiniConfig.cxx:321
dqi::MiniConfig::GetAttributeNames
virtual void GetAttributeNames(std::string objName, std::set< std::string > &attSet) const
Definition
MiniConfig.cxx:298
dqi::MiniConfig::MiniConfig
MiniConfig()
Definition
MiniConfig.cxx:34
dqi::MiniConfig::m_attKeywords
KeySet_t m_attKeywords
Definition
MiniConfig.h:58
dqi::MiniConfig::SendWriter
virtual void SendWriter(MiniConfigTreeNode::Writer &writer)
Definition
MiniConfig.cxx:333
node
Definition
node.h:24
CxxUtils::trimWhiteSpaces
std::string_view trimWhiteSpaces(std::string_view str) noexcept
Removes all trailing and starting whitespaces from a string.
Definition
Control/CxxUtils/Root/StringUtils.cxx:27
dqi
Definition
CompositeAlgorithm.h:16
file
TFile * file
Definition
tile_monitor.h:29
Generated on
for ATLAS Offline Software by
1.17.0