ATLAS Offline Software
Loading...
Searching...
No Matches
GeometryDBSvc.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "GeometryDBSvc.h"
6#include "TextFileDBReader.h"
9
10#include <string>
11#include <sstream>
12#include <stdexcept>
13
14GeometryDBSvc::GeometryDBSvc( const std::string& name, ISvcLocator* pSvcLocator ) :
15 base_class(name, pSvcLocator),
17{
18 declareProperty("TextFileName",m_textFileName, "Text file name for overriding database.");
19 declareProperty("PrintParameters",m_printParameters = true, "Print parameters read in from text file.");
20 declareProperty("PrintNotUsed", m_printNotUsed = true, "Print parameters not used.");
21 declareProperty("Sections", m_sections, "Sections considered for logging. Empty list means all.");
22}
23
28
29StatusCode
31{
32 msg(MSG::INFO) << "GeometryDBSvc Initialized" << endmsg;
34
35 // Some printout
36 if (m_textParameters && m_printParameters && msgLvl(MSG::INFO)) {
37 if (m_sections.empty()) {
38 // Print all parameters if no sections are requested
39 msg(MSG::INFO) << "Parameters from text file:" << endmsg;
40 m_textParameters->printParameters();
41 } else {
42 // Otherwise print only those parameters that belong to sections requested.
43 bool printedUnnamed = false;
44 for (std::vector<std::string>::const_iterator iter = m_sections.begin(); iter != m_sections.end(); ++iter) {
45 msg(MSG::INFO) << "Parameters from text file from section: " << *iter << endmsg;
46 if (m_textParameters->sectionPresent(*iter)) {
47 m_textParameters->printParameters(*iter);
48 } else {
49 // If section not present print those in unnamed section (passing a section name that is not present will do this)
50 // If we already printed those in the unnamed section don't do so again.
51 if (printedUnnamed) {
52 msg(MSG::INFO) << " Section not present. Parameters from text file from unnamed section already printed above." << endmsg;
53 } else {
54 msg(MSG::INFO) << " Section not present. Parameters from text file from unnamed section will be printed." << endmsg;
55 m_textParameters->printParameters(*iter);
56 printedUnnamed = true;
57 }
58 }
59 }
60 }
61 }
62 return StatusCode::SUCCESS;
63}
64
65
66StatusCode
68{
69
70 if (m_textParameters && m_printNotUsed && msgLvl(MSG::INFO)) {
71 if (m_sections.empty()) {
72 // Consider all parameters if no sections are requested.
73 msg(MSG::INFO) << "The following parameters were not used:" << endmsg;
74 m_textParameters->printNotUsed();
75 } else {
76 // Otherwise consider only those parameters that belong to sections requested.
77 bool printedUnnamed = false;
78 for (std::vector<std::string>::const_iterator iter = m_sections.begin(); iter != m_sections.end(); ++iter) {
79 msg(MSG::INFO) << "The following parameters were not used from section: " << *iter <<endmsg;
80 if (m_textParameters->sectionPresent(*iter)) {
81 m_textParameters->printNotUsed(*iter);
82 } else {
83 // If section not present consider those in unnamed section (passing a section name that is not present will do this)
84 // If we already printed those in the unnamed section don't do so again.
85 if (printedUnnamed) {
86 msg(MSG::INFO) << " Section not present. Parameters from text file from unnamed section already printed above." << endmsg;
87 } else {
88 msg(MSG::INFO) << " Section not present. Parameters from text file from unnamed section will be printed." << endmsg;
89 m_textParameters->printNotUsed(*iter);
90 printedUnnamed = true;
91 }
92 }
93 }
94 }
95 }
96 return StatusCode::SUCCESS;
97}
98
99
100void
101GeometryDBSvc::setParameterFileName(const std::string & filename)
102{
103 if (!filename.empty()) {
104 msg(MSG::INFO) << "Parameters overriden from text file: " << filename << endmsg;
105 msg(MSG::WARNING) << "Overriding from a text file is NOT recommended for production use." << endmsg;
107 bool status = m_textParameters->readFile(filename);
108 if (!status) {
109 msg(MSG::ERROR) << "Problem reading text file: " << filename << endmsg;
110 }
111 }
112}
113
114
115
116std::string
117GeometryDBSvc::parameterKey(const std::string & recordSetName, const std::string & name, int index) const
118{
119 std::ostringstream ostr;
120 if (!recordSetName.empty()) {
121 ostr << recordSetName << "#" << index << ":" << name;
122 } else {
123 ostr << name;
124 }
125 return ostr.str();
126}
127
128double
129GeometryDBSvc::getDouble(IRDBRecordset_ptr recordSet, const std::string & name, int index) const
130{
131 if (!recordSet.get()) return getDouble("",name,index);
132 double tmpPar = 0;
133 std::string recordSetName = recordSet->nodeName();
134 if (getValue(recordSetName,name,index,tmpPar)) {
135 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "TEXTFILE: Parameter " << parameterKey(recordSetName,name,index) << " from text file: " << tmpPar << endmsg;
136 } else {
137 if (index >= (int)recordSet->size()) {
138 msg(MSG::ERROR) << "Parameter " << parameterKey(recordSetName,name,index) << " not found. " << endmsg;
139 } else {
140 tmpPar = (*recordSet)[index]->getDouble(name);
141 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Parameter " << parameterKey(recordSetName,name,index) << " from database: " << tmpPar << endmsg;
142 }
143 }
144 return tmpPar;
145}
146
147double
148GeometryDBSvc::getDouble(const std::string & recordSetName, const std::string & name, int index) const
149{
150 double tmpPar = 0;
151 if (getValue(recordSetName,name,index,tmpPar)) {
152 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "TEXTFILE: Parameter " << parameterKey(recordSetName,name,index) << " from text file: " << tmpPar << endmsg;
153 return tmpPar;
154 } else {
155 msg(MSG::ERROR) << "Parameter " << parameterKey(recordSetName,name,index) << " not found." << endmsg;
156 return 0;
157 }
158}
159
160int
161GeometryDBSvc::getInt(IRDBRecordset_ptr recordSet, const std::string & name, int index) const
162{
163 if (!recordSet.get()) return getInt("",name,index);
164 int tmpPar = 0;
165 std::string recordSetName = recordSet->nodeName();
166 if (getValue(recordSetName,name,index,tmpPar)) {
167 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "TEXTFILE: Parameter " << parameterKey(recordSetName,name,index) << " from text file: " << tmpPar << endmsg;
168 } else {
169 if (index >= (int)recordSet->size()) {
170 msg(MSG::ERROR) << "Parameter " << parameterKey(recordSetName,name,index) << " not found. " << endmsg;
171 } else {
172 tmpPar = (*recordSet)[index]->getInt(name);
173 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Parameter " << parameterKey(recordSetName,name,index) << " from database: " << tmpPar << endmsg;
174 }
175 }
176 return tmpPar;
177}
178
179int
180GeometryDBSvc::getInt(const std::string & recordSetName, const std::string & name, int index) const
181{
182 int tmpPar = 0;
183 if (getValue(recordSetName,name,index,tmpPar)) {
184 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "TEXTFILE: Parameter " << parameterKey(recordSetName,name,index) << " from text file: " << tmpPar << endmsg;
185 return tmpPar;
186 } else {
187 msg(MSG::ERROR) << "Parameter " << parameterKey(recordSetName,name,index) << index << " not found." << endmsg;
188 return 0;
189 }
190}
191
192std::string
193GeometryDBSvc::getString(IRDBRecordset_ptr recordSet, const std::string & name, int index) const
194{
195 if (!recordSet.get()) return getString("",name,index);
196 std::string tmpPar;
197 std::string recordSetName = recordSet->nodeName();
198 if (getValue(recordSetName,name,index,tmpPar)) {
199 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "TEXTFILE: Parameter " << parameterKey(recordSetName,name,index) << " from text file: " << tmpPar << endmsg;
200 } else {
201 if (index >= (int)recordSet->size()) {
202 msg(MSG::ERROR) << "Parameter " << parameterKey(recordSetName,name,index) << index << " not found. " << endmsg;
203 } else {
204 tmpPar = (*recordSet)[index]->getString(name);
205 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Parameter " << parameterKey(recordSetName,name,index) << " from database: " << tmpPar << endmsg;
206 }
207 }
208 return tmpPar;
209}
210
211std::string
212GeometryDBSvc::getString(const std::string & recordSetName, const std::string & name, int index) const
213{
214 std::string tmpPar;
215 if (getValue(recordSetName,name,index,tmpPar)) {
216 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "TEXTFILE: Parameter " << parameterKey(recordSetName,name,index) << " from text file: " << tmpPar << endmsg;
217 return tmpPar;
218 } else {
219 msg(MSG::ERROR) << "Parameter " << parameterKey(recordSetName,name,index) << " not found." << endmsg;
220 return "";
221 }
222}
223
224// This is where we actually try to get the parameter from the text file.
225bool
226GeometryDBSvc::getValue(const std::string & recordSetName, const std::string & name, int index, std::string & var) const
227{
228 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Looking for Parameter " << parameterKey(recordSetName,name,index) << endmsg;
229 if (recordSetName.empty() && index) {
230 msg(MSG::WARNING) << "Non zero index for parameter with empry record set. Index will be ignored: " << index << endmsg;
231 }
232 var = "";
233 if (!m_textParameters) return false;
234 std::string lookupKey = parameterKey(recordSetName,name,index);
235 bool result = m_textParameters->find(parameterKey(recordSetName,name,index),var);
236 if (!result && !recordSetName.empty()) {
237 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Not found. Looking for default entry with #ALL" << endmsg;
238 std::ostringstream keyalt;
239 keyalt << recordSetName << "#ALL:" << name;
240 result = m_textParameters->find(keyalt.str(),var);
241 }
242 return result;
243}
244
245
246bool
247GeometryDBSvc::getValue(const std::string & recordSetName, const std::string & name, int index, double & var) const
248{
249 std::string result;
250 var = 0;
251 // Get the parameter as a string first
252 if (getValue(recordSetName, name, index, result)) {
253 std::istringstream istr(result);
254 istr >> var;
255 if (!istr.eof()) { // Should have read the whole stream
256 msg(MSG::ERROR) << "Error retrieving parameter " << parameterKey(recordSetName,name,index) << " as a double: " << result << endmsg;
257 return false;
258 }
259 return true;
260 }
261 return false;
262}
263
264bool
265GeometryDBSvc::getValue(const std::string & recordSetName, const std::string & name, int index, int & var) const
266{
267 std::string result;
268 var = 0;
269 // Get the parameter as a string first
270 if (getValue(recordSetName, name, index, result)) {
271 std::istringstream istr(result);
272 istr >> var;
273 if (!istr.eof()) { // Should have read the whole stream
274 msg(MSG::ERROR) << "Error retrieving parameter " << parameterKey(recordSetName,name,index) << " as an int: " << result << endmsg;
275 return false;
276 }
277 return true;
278 }
279 return false;
280}
281
282bool
283GeometryDBSvc::testFieldTxt(IRDBRecordset_ptr recordSet, const std::string & name, int index) const
284{
285 std::string recordSetName = recordSet->nodeName();
286 return testField(recordSetName, name, index);
287}
288
289bool
290GeometryDBSvc::testField(IRDBRecordset_ptr recordSet, const std::string & name, int index) const
291{
292 std::string recordSetName = recordSet->nodeName();
293 if (testField(recordSetName, name, index)) {
294 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Parameter " << parameterKey(recordSetName,name,index) << " found in text file. " << endmsg;
295 return true;
296 }
297 bool result = false;
298 if (index < (int)recordSet->size()) {
299 try {
300 result = !(*recordSet)[index]->isFieldNull(name);
301 }
302 catch(std::runtime_error& ex) {
303 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Exception caught: " << ex.what() << endmsg;
304 result = false;
305 }
306 }
307 if (result){
308 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Parameter " << parameterKey(recordSetName,name,index) << " found in database. " << endmsg;
309 } else {
310 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Parameter " << parameterKey(recordSetName,name,index) << " not found. " << endmsg;
311 }
312 return result;
313}
314
315bool
316GeometryDBSvc::testField(const std::string & recordSetName, const std::string & name, int index) const
317{
318 std::string tmpStr;
319 return getValue(recordSetName, name, index, tmpStr);
320}
321
322
323unsigned int
325{
326 int tmpPar = 0;
327 unsigned int tmpParUnsigned = 0;
328 std::string recordSetName = recordSet->nodeName();
329 // Table size stored with key of form TableSize:TableName
330 if (getTableSizeFromTextFile(recordSetName,tmpPar)) {
331 if (tmpPar < 0) {
332 msg(MSG::ERROR) << "Table " << recordSetName << " size from text file is negative: " << tmpPar << endmsg;
333 tmpPar = 0;
334 } else {
335 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "TEXTFILE: Table " << recordSetName << " size from text file: " << tmpPar << endmsg;
336 }
337 tmpParUnsigned = static_cast<unsigned int>(tmpPar);
338 } else {
339 // Get from database
340 tmpParUnsigned = recordSet->size();
341 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Table " << recordSetName << " size from database: " << tmpParUnsigned << endmsg;
342 }
343 return tmpParUnsigned;
344}
345
346unsigned int
347GeometryDBSvc::getTableSize(const std::string & recordSetName) const
348{
349 int tmpPar = 0;
350 unsigned int tmpParUnsigned = 0;
351 // Table size stored with key of form TableSize:TableName
352 if (getTableSizeFromTextFile(recordSetName,tmpPar)) {
353 if (tmpPar < 0) {
354 msg(MSG::ERROR) << "Table " << recordSetName << " size from text file is negative: " << tmpPar << endmsg;
355 tmpPar = 0;
356 } else {
357 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "TEXTFILE: Table " << recordSetName << " size from text file: " << tmpPar << endmsg;
358 }
359 tmpParUnsigned = static_cast<unsigned int>(tmpPar);
360 } else {
361 msg(MSG::ERROR) << "getTableSize: Table " << recordSetName << " does not exist in text file." << endmsg;
362 }
363 return tmpParUnsigned;
364}
365
366// Get table size from the text file.
367bool
368GeometryDBSvc::getTableSizeFromTextFile(const std::string & recordSetName, int & var) const
369{
370 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Looking for TableSize for " << recordSetName << endmsg;
371 std::string result;
372 var = -1;
373 if (!m_textParameters) return false;
374 std::ostringstream key;
375 key << "TableSize:" << recordSetName;
376 if (m_textParameters->find(key.str(),result)) {
377 // Convert to int
378 std::istringstream istr(result);
379 istr >> var;
380 if (!istr.eof()) { // Should have read the whole stream
381 msg(MSG::ERROR) << "Error retrieving parameter TableSize:" << recordSetName << " as an int: " << result << endmsg;
382 return false;
383 }
384 return true;
385 }
386 return false;
387}
388
389void
390GeometryDBSvc::printParameters(const std::string & section) const
391{
392 m_textParameters->printParameters(section);
393}
394
395void
396GeometryDBSvc::printNotUsed(const std::string & section) const
397{
398 m_textParameters->printNotUsed(section);
399}
400
#define endmsg
std::shared_ptr< IRDBRecordset > IRDBRecordset_ptr
Definition of the abstract IRDBRecord interface.
Definition of the abstract IRDBRecordset interface.
void section(const std::string &sec)
virtual ~GeometryDBSvc()
virtual StatusCode initialize()
Service init.
virtual bool testField(IRDBRecordset_ptr recordSet, const std::string &name, int index=0) const
virtual StatusCode finalize()
Service finalize.
GeometryDBSvc(const std::string &name, ISvcLocator *sl)
void setParameterFileName(const std::string &filename)
TextFileDBReader * m_textParameters
virtual void printNotUsed(const std::string &section="") const
Print paramaters from text file which are not used. If section is supplied only consider the paramete...
virtual bool testFieldTxt(IRDBRecordset_ptr recordSet, const std::string &name, int index=0) const
virtual double getDouble(IRDBRecordset_ptr recordSet, const std::string &name, int index=0) const
std::string m_textFileName
bool getTableSizeFromTextFile(const std::string &recordSetName, int &var) const
std::vector< std::string > m_sections
bool getValue(const std::string &recordSetName, const std::string &name, int index, std::string &var) const
virtual int getInt(IRDBRecordset_ptr recordSet, const std::string &name, int index=0) const
virtual std::string getString(IRDBRecordset_ptr recordSet, const std::string &name, int index=0) const
std::string parameterKey(const std::string &recordSetName, const std::string &name, int index) const
virtual unsigned int getTableSize(IRDBRecordset_ptr recordSet) const
virtual void printParameters(const std::string &section="") const
Print paramaters read in from text file. If section is supplied only consider the parameters in that ...
virtual std::string nodeName() const =0
virtual unsigned int size() const =0
Class to read in a text file and allow easy retrieval of parameters.
Definition index.py:1
MsgStream & msg
Definition testRead.cxx:32