ATLAS Offline Software
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 
14 GeometryDBSvc::GeometryDBSvc( const std::string& name, ISvcLocator* pSvcLocator ) :
15  base_class(name, pSvcLocator),
16  m_textParameters(0)
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 
25 {
26  delete m_textParameters;
27 }
28 
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;
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)) {
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;
56  printedUnnamed = true;
57  }
58  }
59  }
60  }
61  }
62  return StatusCode::SUCCESS;
63 }
64 
65 
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;
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)) {
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;
90  printedUnnamed = true;
91  }
92  }
93  }
94  }
95  }
96  return StatusCode::SUCCESS;
97 }
98 
99 
100 void
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;
108  if (!status) {
109  msg(MSG::ERROR) << "Problem reading text file: " << filename << endmsg;
110  }
111  }
112 }
113 
114 
115 
116 std::string
117 GeometryDBSvc::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 
128 double
129 GeometryDBSvc::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 
147 double
148 GeometryDBSvc::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 
160 int
161 GeometryDBSvc::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 
179 int
180 GeometryDBSvc::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 
192 std::string
193 GeometryDBSvc::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 
211 std::string
212 GeometryDBSvc::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.
225 bool
226 GeometryDBSvc::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 
246 bool
247 GeometryDBSvc::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 
264 bool
265 GeometryDBSvc::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 
282 bool
283 GeometryDBSvc::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 
289 bool
290 GeometryDBSvc::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 
315 bool
316 GeometryDBSvc::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 
323 unsigned 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 
346 unsigned int
347 GeometryDBSvc::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.
367 bool
368 GeometryDBSvc::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 
389 void
390 GeometryDBSvc::printParameters(const std::string & section) const
391 {
393 }
394 
395 void
396 GeometryDBSvc::printNotUsed(const std::string & section) const
397 {
399 }
400 
GeometryDBSvc::testField
virtual bool testField(IRDBRecordset_ptr recordSet, const std::string &name, int index=0) const
Definition: GeometryDBSvc.cxx:290
beamspotnt.var
var
Definition: bin/beamspotnt.py:1394
GeometryDBSvc::m_printNotUsed
bool m_printNotUsed
Definition: GeometryDBSvc.h:69
get_generator_info.result
result
Definition: get_generator_info.py:21
GeometryDBSvc::getString
virtual std::string getString(IRDBRecordset_ptr recordSet, const std::string &name, int index=0) const
Definition: GeometryDBSvc.cxx:193
GeometryDBSvc::printParameters
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 ...
Definition: GeometryDBSvc.cxx:390
GeometryDBSvc.h
GeometryDBSvc::getTableSizeFromTextFile
bool getTableSizeFromTextFile(const std::string &recordSetName, int &var) const
Definition: GeometryDBSvc.cxx:368
TextFileDBReader::printNotUsed
void printNotUsed(const std::string &section="") const
Definition: TextFileDBReader.cxx:234
index
Definition: index.py:1
GeometryDBSvc::finalize
virtual StatusCode finalize()
Service finalize.
Definition: GeometryDBSvc.cxx:67
GeometryDBSvc::initialize
virtual StatusCode initialize()
Service init.
Definition: GeometryDBSvc.cxx:30
GeometryDBSvc::m_textParameters
TextFileDBReader * m_textParameters
Definition: GeometryDBSvc.h:64
GeometryDBSvc::testFieldTxt
virtual bool testFieldTxt(IRDBRecordset_ptr recordSet, const std::string &name, int index=0) const
Definition: GeometryDBSvc.cxx:283
GeometryDBSvc::printNotUsed
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...
Definition: GeometryDBSvc.cxx:396
TextFileDBReader::sectionPresent
bool sectionPresent(const std::string &section) const
Definition: TextFileDBReader.cxx:262
GeometryDBSvc::getValue
bool getValue(const std::string &recordSetName, const std::string &name, int index, std::string &var) const
Definition: GeometryDBSvc.cxx:226
GeometryDBSvc::getDouble
virtual double getDouble(IRDBRecordset_ptr recordSet, const std::string &name, int index=0) const
Definition: GeometryDBSvc.cxx:129
TextFileDBReader
Class to read in a text file and allow easy retrieval of parameters.
Definition: TextFileDBReader.h:18
GeometryDBSvc::parameterKey
std::string parameterKey(const std::string &recordSetName, const std::string &name, int index) const
Definition: GeometryDBSvc.cxx:117
GeometryDBSvc::~GeometryDBSvc
virtual ~GeometryDBSvc()
Definition: GeometryDBSvc.cxx:24
GeometryDBSvc::getInt
virtual int getInt(IRDBRecordset_ptr recordSet, const std::string &name, int index=0) const
Definition: GeometryDBSvc.cxx:161
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
GeometryDBSvc::setParameterFileName
void setParameterFileName(const std::string &filename)
Definition: GeometryDBSvc.cxx:101
GeometryDBSvc::GeometryDBSvc
GeometryDBSvc(const std::string &name, ISvcLocator *sl)
Definition: GeometryDBSvc.cxx:14
IRDBRecordset_ptr
std::shared_ptr< IRDBRecordset > IRDBRecordset_ptr
Definition: IRDBAccessSvc.h:25
TextFileDBReader::find
bool find(const std::string &key, std::string &result) const
Definition: TextFileDBReader.cxx:197
GeometryDBSvc::m_textFileName
std::string m_textFileName
Definition: GeometryDBSvc.h:67
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
GeometryDBSvc::m_sections
std::vector< std::string > m_sections
Definition: GeometryDBSvc.h:70
DeMoScan.index
string index
Definition: DeMoScan.py:364
IRDBRecord.h
Definition of the abstract IRDBRecord interface.
GeometryDBSvc::getTableSize
virtual unsigned int getTableSize(IRDBRecordset_ptr recordSet) const
Definition: GeometryDBSvc.cxx:324
TextFileDBReader.h
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
GeometryDBSvc::m_printParameters
bool m_printParameters
Definition: GeometryDBSvc.h:68
merge.status
status
Definition: merge.py:17
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:14
IRDBRecordset.h
Definition of the abstract IRDBRecordset interface.
TextFileDBReader::readFile
bool readFile(const std::string &filename)
Definition: TextFileDBReader.cxx:26
section
void section(const std::string &sec)
Definition: TestTriggerMenuAccess.cxx:22
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
TextFileDBReader::printParameters
void printParameters(const std::string &section="") const
Definition: TextFileDBReader.cxx:211
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37