ATLAS Offline Software
GeometryDBSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 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  AthService(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 // Query the interfaces.
100 // Input: riid, Requested interface ID
101 // ppvInterface, Pointer to requested interface
102 // Return: StatusCode indicating SUCCESS or FAILURE.
103 // N.B. Don't forget to release the interface after use!!!
105 GeometryDBSvc::queryInterface(const InterfaceID& riid, void** ppvInterface)
106 {
107  if ( IGeometryDBSvc::interfaceID().versionMatch(riid) ) {
108  *ppvInterface = dynamic_cast<IGeometryDBSvc *>(this);
109  } else {
110  // Interface is not directly available: try out a base class
111  return Service::queryInterface(riid, ppvInterface);
112  }
113  addRef();
114  return StatusCode::SUCCESS;
115 }
116 
117 void
119 {
120  if (!filename.empty()) {
121  msg(MSG::INFO) << "Parameters overriden from text file: " << filename << endmsg;
122  msg(MSG::WARNING) << "Overriding from a text file is NOT recommended for production use." << endmsg;
125  if (!status) {
126  msg(MSG::ERROR) << "Problem reading text file: " << filename << endmsg;
127  }
128  }
129 }
130 
131 
132 
133 std::string
134 GeometryDBSvc::parameterKey(const std::string & recordSetName, const std::string & name, int index) const
135 {
136  std::ostringstream ostr;
137  if (!recordSetName.empty()) {
138  ostr << recordSetName << "#" << index << ":" << name;
139  } else {
140  ostr << name;
141  }
142  return ostr.str();
143 }
144 
145 double
146 GeometryDBSvc::getDouble(IRDBRecordset_ptr recordSet, const std::string & name, int index) const
147 {
148  if (!recordSet.get()) return getDouble("",name,index);
149  double tmpPar = 0;
150  std::string recordSetName = recordSet->nodeName();
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  } else {
154  if (index >= (int)recordSet->size()) {
155  msg(MSG::ERROR) << "Parameter " << parameterKey(recordSetName,name,index) << " not found. " << endmsg;
156  } else {
157  tmpPar = (*recordSet)[index]->getDouble(name);
158  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Parameter " << parameterKey(recordSetName,name,index) << " from database: " << tmpPar << endmsg;
159  }
160  }
161  return tmpPar;
162 }
163 
164 double
165 GeometryDBSvc::getDouble(const std::string & recordSetName, const std::string & name, int index) const
166 {
167  double tmpPar = 0;
168  if (getValue(recordSetName,name,index,tmpPar)) {
169  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "TEXTFILE: Parameter " << parameterKey(recordSetName,name,index) << " from text file: " << tmpPar << endmsg;
170  return tmpPar;
171  } else {
172  msg(MSG::ERROR) << "Parameter " << parameterKey(recordSetName,name,index) << " not found." << endmsg;
173  return 0;
174  }
175 }
176 
177 int
178 GeometryDBSvc::getInt(IRDBRecordset_ptr recordSet, const std::string & name, int index) const
179 {
180  if (!recordSet.get()) return getInt("",name,index);
181  int tmpPar = 0;
182  std::string recordSetName = recordSet->nodeName();
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  } else {
186  if (index >= (int)recordSet->size()) {
187  msg(MSG::ERROR) << "Parameter " << parameterKey(recordSetName,name,index) << " not found. " << endmsg;
188  } else {
189  tmpPar = (*recordSet)[index]->getInt(name);
190  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Parameter " << parameterKey(recordSetName,name,index) << " from database: " << tmpPar << endmsg;
191  }
192  }
193  return tmpPar;
194 }
195 
196 int
197 GeometryDBSvc::getInt(const std::string & recordSetName, const std::string & name, int index) const
198 {
199  int tmpPar = 0;
200  if (getValue(recordSetName,name,index,tmpPar)) {
201  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "TEXTFILE: Parameter " << parameterKey(recordSetName,name,index) << " from text file: " << tmpPar << endmsg;
202  return tmpPar;
203  } else {
204  msg(MSG::ERROR) << "Parameter " << parameterKey(recordSetName,name,index) << index << " not found." << endmsg;
205  return 0;
206  }
207 }
208 
209 std::string
210 GeometryDBSvc::getString(IRDBRecordset_ptr recordSet, const std::string & name, int index) const
211 {
212  if (!recordSet.get()) return getString("",name,index);
213  std::string tmpPar;
214  std::string recordSetName = recordSet->nodeName();
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  } else {
218  if (index >= (int)recordSet->size()) {
219  msg(MSG::ERROR) << "Parameter " << parameterKey(recordSetName,name,index) << index << " not found. " << endmsg;
220  } else {
221  tmpPar = (*recordSet)[index]->getString(name);
222  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Parameter " << parameterKey(recordSetName,name,index) << " from database: " << tmpPar << endmsg;
223  }
224  }
225  return tmpPar;
226 }
227 
228 std::string
229 GeometryDBSvc::getString(const std::string & recordSetName, const std::string & name, int index) const
230 {
231  std::string tmpPar;
232  if (getValue(recordSetName,name,index,tmpPar)) {
233  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "TEXTFILE: Parameter " << parameterKey(recordSetName,name,index) << " from text file: " << tmpPar << endmsg;
234  return tmpPar;
235  } else {
236  msg(MSG::ERROR) << "Parameter " << parameterKey(recordSetName,name,index) << " not found." << endmsg;
237  return "";
238  }
239 }
240 
241 // This is where we actually try to get the parameter from the text file.
242 bool
243 GeometryDBSvc::getValue(const std::string & recordSetName, const std::string & name, int index, std::string & var) const
244 {
245  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Looking for Parameter " << parameterKey(recordSetName,name,index) << endmsg;
246  if (recordSetName.empty() && index) {
247  msg(MSG::WARNING) << "Non zero index for parameter with empry record set. Index will be ignored: " << index << endmsg;
248  }
249  var = "";
250  if (!m_textParameters) return false;
251  std::string lookupKey = parameterKey(recordSetName,name,index);
252  bool result = m_textParameters->find(parameterKey(recordSetName,name,index),var);
253  if (!result && !recordSetName.empty()) {
254  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Not found. Looking for default entry with #ALL" << endmsg;
255  std::ostringstream keyalt;
256  keyalt << recordSetName << "#ALL:" << name;
257  result = m_textParameters->find(keyalt.str(),var);
258  }
259  return result;
260 }
261 
262 
263 bool
264 GeometryDBSvc::getValue(const std::string & recordSetName, const std::string & name, int index, double & var) const
265 {
266  std::string result;
267  var = 0;
268  // Get the parameter as a string first
269  if (getValue(recordSetName, name, index, result)) {
270  std::istringstream istr(result);
271  istr >> var;
272  if (!istr.eof()) { // Should have read the whole stream
273  msg(MSG::ERROR) << "Error retrieving parameter " << parameterKey(recordSetName,name,index) << " as a double: " << result << endmsg;
274  return false;
275  }
276  return true;
277  }
278  return false;
279 }
280 
281 bool
282 GeometryDBSvc::getValue(const std::string & recordSetName, const std::string & name, int index, int & var) const
283 {
284  std::string result;
285  var = 0;
286  // Get the parameter as a string first
287  if (getValue(recordSetName, name, index, result)) {
288  std::istringstream istr(result);
289  istr >> var;
290  if (!istr.eof()) { // Should have read the whole stream
291  msg(MSG::ERROR) << "Error retrieving parameter " << parameterKey(recordSetName,name,index) << " as an int: " << result << endmsg;
292  return false;
293  }
294  return true;
295  }
296  return false;
297 }
298 
299 bool
300 GeometryDBSvc::testFieldTxt(IRDBRecordset_ptr recordSet, const std::string & name, int index) const
301 {
302  std::string recordSetName = recordSet->nodeName();
303  return testField(recordSetName, name, index);
304 }
305 
306 bool
307 GeometryDBSvc::testField(IRDBRecordset_ptr recordSet, const std::string & name, int index) const
308 {
309  std::string recordSetName = recordSet->nodeName();
310  if (testField(recordSetName, name, index)) {
311  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Parameter " << parameterKey(recordSetName,name,index) << " found in text file. " << endmsg;
312  return true;
313  }
314  bool result = false;
315  if (index < (int)recordSet->size()) {
316  try {
317  result = !(*recordSet)[index]->isFieldNull(name);
318  }
319  catch(std::runtime_error& ex) {
320  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Exception caught: " << ex.what() << endmsg;
321  result = false;
322  }
323  }
324  if (result){
325  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Parameter " << parameterKey(recordSetName,name,index) << " found in database. " << endmsg;
326  } else {
327  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Parameter " << parameterKey(recordSetName,name,index) << " not found. " << endmsg;
328  }
329  return result;
330 }
331 
332 bool
333 GeometryDBSvc::testField(const std::string & recordSetName, const std::string & name, int index) const
334 {
335  std::string tmpStr;
336  return getValue(recordSetName, name, index, tmpStr);
337 }
338 
339 
340 unsigned int
342 {
343  int tmpPar = 0;
344  unsigned int tmpParUnsigned = 0;
345  std::string recordSetName = recordSet->nodeName();
346  // Table size stored with key of form TableSize:TableName
347  if (getTableSizeFromTextFile(recordSetName,tmpPar)) {
348  if (tmpPar < 0) {
349  msg(MSG::ERROR) << "Table " << recordSetName << " size from text file is negative: " << tmpPar << endmsg;
350  tmpPar = 0;
351  } else {
352  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "TEXTFILE: Table " << recordSetName << " size from text file: " << tmpPar << endmsg;
353  }
354  tmpParUnsigned = static_cast<unsigned int>(tmpPar);
355  } else {
356  // Get from database
357  tmpParUnsigned = recordSet->size();
358  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Table " << recordSetName << " size from database: " << tmpParUnsigned << endmsg;
359  }
360  return tmpParUnsigned;
361 }
362 
363 unsigned int
364 GeometryDBSvc::getTableSize(const std::string & recordSetName) const
365 {
366  int tmpPar = 0;
367  unsigned int tmpParUnsigned = 0;
368  // Table size stored with key of form TableSize:TableName
369  if (getTableSizeFromTextFile(recordSetName,tmpPar)) {
370  if (tmpPar < 0) {
371  msg(MSG::ERROR) << "Table " << recordSetName << " size from text file is negative: " << tmpPar << endmsg;
372  tmpPar = 0;
373  } else {
374  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "TEXTFILE: Table " << recordSetName << " size from text file: " << tmpPar << endmsg;
375  }
376  tmpParUnsigned = static_cast<unsigned int>(tmpPar);
377  } else {
378  msg(MSG::ERROR) << "getTableSize: Table " << recordSetName << " does not exist in text file." << endmsg;
379  }
380  return tmpParUnsigned;
381 }
382 
383 // Get table size from the text file.
384 bool
385 GeometryDBSvc::getTableSizeFromTextFile(const std::string & recordSetName, int & var) const
386 {
387  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "Looking for TableSize for " << recordSetName << endmsg;
388  std::string result;
389  var = -1;
390  if (!m_textParameters) return false;
391  std::ostringstream key;
392  key << "TableSize:" << recordSetName;
393  if (m_textParameters->find(key.str(),result)) {
394  // Convert to int
395  std::istringstream istr(result);
396  istr >> var;
397  if (!istr.eof()) { // Should have read the whole stream
398  msg(MSG::ERROR) << "Error retrieving parameter TableSize:" << recordSetName << " as an int: " << result << endmsg;
399  return false;
400  }
401  return true;
402  }
403  return false;
404 }
405 
406 void
407 GeometryDBSvc::printParameters(const std::string & section) const
408 {
410 }
411 
412 void
413 GeometryDBSvc::printNotUsed(const std::string & section) const
414 {
416 }
417 
GeometryDBSvc::testField
virtual bool testField(IRDBRecordset_ptr recordSet, const std::string &name, int index=0) const
Definition: GeometryDBSvc.cxx:307
beamspotnt.var
var
Definition: bin/beamspotnt.py:1394
GeometryDBSvc::m_printNotUsed
bool m_printNotUsed
Definition: GeometryDBSvc.h:72
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:210
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:407
GeometryDBSvc.h
GeometryDBSvc::queryInterface
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvInterface)
Definition: GeometryDBSvc.cxx:105
GeometryDBSvc::getTableSizeFromTextFile
bool getTableSizeFromTextFile(const std::string &recordSetName, int &var) const
Definition: GeometryDBSvc.cxx:385
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:67
AthCommonMsg< Service >::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.h:30
GeometryDBSvc::testFieldTxt
virtual bool testFieldTxt(IRDBRecordset_ptr recordSet, const std::string &name, int index=0) const
Definition: GeometryDBSvc.cxx:300
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:413
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:243
GeometryDBSvc::getDouble
virtual double getDouble(IRDBRecordset_ptr recordSet, const std::string &name, int index=0) const
The following methods will first look in the text file if provided and then look in the database.
Definition: GeometryDBSvc.cxx:146
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:134
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:178
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
AthService
Definition: AthService.h:32
GeometryDBSvc::setParameterFileName
void setParameterFileName(const std::string &filename)
Definition: GeometryDBSvc.cxx:118
IGeometryDBSvc
Definition: IGeometryDBSvc.h:21
IGeometryDBSvc::interfaceID
static const InterfaceID & interfaceID()
reimplemented from IInterface
Definition: IGeometryDBSvc.h:52
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:70
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
GeometryDBSvc::m_sections
std::vector< std::string > m_sections
Definition: GeometryDBSvc.h:73
DeMoScan.index
string index
Definition: DeMoScan.py:362
IRDBRecord.h
Definition of the abstract IRDBRecord interface.
GeometryDBSvc::getTableSize
virtual unsigned int getTableSize(IRDBRecordset_ptr recordSet) const
Definition: GeometryDBSvc.cxx:341
TextFileDBReader.h
AthCommonMsg< Service >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
GeometryDBSvc::m_printParameters
bool m_printParameters
Definition: GeometryDBSvc.h:71
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
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
TextFileDBReader::printParameters
void printParameters(const std::string &section="") const
Definition: TextFileDBReader.cxx:211
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37