ATLAS Offline Software
TextFileDBReader.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "TextFileDBReader.h"
6 #include <fstream>
7 #include <sstream>
8 #include <iostream>
9 #include <vector>
10 #include <iomanip>
11 
13  : m_numSections(0),
14  m_currentSection(0)
15 {}
16 
18  : m_numSections(0),
19  m_currentSection(0)
20 {
22 }
23 
24 
25 bool
26 TextFileDBReader::readFile(const std::string & readFile)
27 {
28  std::ifstream ifile;
29  ifile.open(readFile.c_str());
30  if (!ifile) {
31  //std::cout << "Error opening file: " << readFile << std::endl;
32  return false;
33  }
34 
35  bool tableMode = false;
36  std::string currentTable;
37  int currentIndex = -1;
38  std::vector<std::string> currentFields;
39  m_currentSection = 0;
40 
41  while (ifile) {
42  std::string line;
43  std::getline(ifile,line);
44  if (!ifile) break;
45  // Skip blank or comment lines
46  if (line.empty() || line[0] == '#' || line.substr(0,2) == "//" ) continue;
47  std::istringstream istr(line);
48 
49  std::string key;
50 
51  istr >> key;
52 
53  if (key.empty()) continue;
54 
55  if (key == "Table") {
56  std::string value;
57  istr >> value;
58  tableMode = true;
59  currentTable = value;
60  currentIndex = -1;
61  currentFields.clear();
62  } else if (key == "TableEnd") {
63  std::ostringstream ostr;
64  ostr << "TableSize:" << currentTable;
65  add(ostr.str(), currentIndex);
66  tableMode = false;
67  currentTable = "";
68  currentIndex = -1;
69  currentFields.clear();
70  } else if (key == "Section") {
71  std::string value;
72  istr >> value;
73  if (!value.empty()) {
74  int & section = m_sections[value];
75  // First section will number 1. Section 0 refers to unnamed section
76  if (!section) section = ++m_numSections;
78  }
79  } else if (key == "EndSection") {
80  m_currentSection = 0;
81  } else {
82  if (!tableMode) {
83  std::string value;
84  istr >> value;
85  // Make sure its in correct format
86  key = formatKey(key);
87  add(key,value);
88  } else { // table mode
89  // reset stream.
90  std::istringstream istr2(line);
91 
92  if (currentIndex < 0) {
93  // Get the fields
94  while (!istr2.eof()) {
95  std::string value;
96  istr2 >> value;
97  if (!value.empty()) currentFields.push_back(value);
98  }
99  } else {
100  // Get row of values
101  for (unsigned int i=0; i < currentFields.size(); i++) {
102  std::string value;
103  istr2 >> value;
104  if (istr2) {
105  std::ostringstream ostr;
106  ostr << currentTable << "#" << currentIndex << ":" << currentFields[i];
107  add(ostr.str(), value);
108  }
109  }
110  }
111  currentIndex++;
112  }
113  }
114  }
115  ifile.close();
116  //printParameters();
117  return true;
118 }
119 
120 std::string
121 TextFileDBReader::formatKey(const std::string & key) const
122 {
123  //std::cout << "Key in: " << key << std::endl;
124  // Split into tableName, fieldName and rowNumber
125  // and recreate
126  std::string tableName;
127  std::string fieldName;
128  std::string rowNumber;
129  bool foundRowNumber = false;
130  std::string::size_type pos = key.find(':');
131  if (pos != std::string::npos) {
132  tableName = key.substr(0,pos);
133  foundRowNumber = getRowNumber(tableName,rowNumber);
134  fieldName = key.substr(++pos);
135  } else {
136  fieldName = key;
137  }
138  std::string tmpRowNumber;
139  if (getRowNumber(fieldName,tmpRowNumber)) {
140  if (foundRowNumber) {
141  // Already have rowNumber from before
142  std::cout << "ERROR in format:" << key << std::endl;
143  } else {
144  rowNumber = tmpRowNumber;
145  }
146  }
147 
148  std::string newKey = key;
149  if ((tableName.empty() && !rowNumber.empty()) || fieldName.empty()) {
150  std::cout << "ERROR in format: " << key << std::endl;
151  } else {
152  if (rowNumber.empty()) rowNumber = "0";
153  if (tableName.empty()) {
154  newKey = fieldName;
155  }else if (tableName == "TableSize") {
156  newKey = "TableSize:"+fieldName;
157  } else {
158  newKey = tableName+"#"+rowNumber+":"+fieldName;
159  }
160  }
161  //std::cout << "Key out: " << newKey << std::endl;
162  return newKey;
163 }
164 
165 bool
166 TextFileDBReader::getRowNumber(std::string & key, std::string & rowNumber) const
167 {
168  std::string::size_type pos = key.find('#');
169  if (pos != std::string::npos) {
170  rowNumber = key.substr(pos+1);
171  key.resize(pos);
172  return true;
173  }
174  return false;
175 }
176 
177 
178 
179 void
180 TextFileDBReader::add(const std::string & key, int value) {
181  std::ostringstream ostr;
182  ostr << value;
183  add(key,ostr.str());
184 }
185 
186 
187 void
188 TextFileDBReader::add(const std::string & key, const std::string & value)
189 {
190  if (m_table.find(key) != m_table.end()) {
191  std::cout << "WARNING! Overwriting exist entry with key: " << key << std::endl;
192  }
193  m_table.try_emplace (key, value, m_currentSection);
194 }
195 
196 bool
197 TextFileDBReader::find(const std::string & key, std::string & result) const
198 {
199  std::unordered_map<std::string,Data>::const_iterator iter = m_table.find(key);
200  if (iter != m_table.end()) {
201  result = iter->second.value;
202  (iter->second).flag=true;
203  return true;
204  } else {
205  result = "";
206  return false;
207  }
208 }
209 
210 void
211 TextFileDBReader::printParameters(const std::string & section) const
212 {
213  std::ios::fmtflags iosflags = std::cout.flags();
214  std::cout << std::left;
215  int sectionNum = 0;
216  if (!section.empty()) {
217  std::unordered_map<std::string,int>::const_iterator iterSect = m_sections.find(section);
218  if (iterSect != m_sections.end()) sectionNum = iterSect->second;
219  // If not found then prints those in unnamed section.(ie sectionNum = 0)
220  }
221  for (std::unordered_map<std::string,Data>::const_iterator iter = m_table.begin();
222  iter != m_table.end();
223  ++iter) {
224  if (section.empty() || iter->second.section == sectionNum) {
225  std::cout << std::setw(35) << iter->first << " " << iter->second.value << std::endl;
226  }
227  }
228  // reset flags to original state
229  std::cout.flags(iosflags);
230 }
231 
232 // Print those variables that are not accessed
233 void
234 TextFileDBReader::printNotUsed(const std::string & section) const
235 {
236  std::ios::fmtflags iosflags = std::cout.flags();
237  std::cout << std::left;
238  bool allused = true;
239  int sectionNum = 0;
240  if (!section.empty()) {
241  std::unordered_map<std::string,int>::const_iterator iterSect = m_sections.find(section);
242  if (iterSect != m_sections.end()) sectionNum = iterSect->second;
243  // If not found then considers those in unnamed section (ie sectionNum = 0)
244  }
245  for (std::unordered_map<std::string,Data>::const_iterator iter = m_table.begin();
246  iter != m_table.end();
247  ++iter) {
248  if ((section.empty() || iter->second.section == sectionNum) && (!(iter->second.flag))) {
249  std::cout << std::setw(35) << iter->first << " " << iter->second.value << std::endl;
250  allused = false;
251  }
252  }
253  if (allused) {
254  std::cout << "All parameters used" << std::endl;
255  }
256  // reset flags to original state
257  std::cout.flags(iosflags);
258 }
259 
260 
261 bool
262 TextFileDBReader::sectionPresent(const std::string & section) const
263 {
264  return (m_sections.find(section) != m_sections.end());
265 }
TestSUSYToolsAlg.ifile
ifile
Definition: TestSUSYToolsAlg.py:92
checkFileSG.line
line
Definition: checkFileSG.py:75
TextFileDBReader::TextFileDBReader
TextFileDBReader()
Definition: TextFileDBReader.cxx:12
TextFileDBReader::m_numSections
int m_numSections
Definition: TextFileDBReader.h:50
get_generator_info.result
result
Definition: get_generator_info.py:21
TextFileDBReader::printNotUsed
void printNotUsed(const std::string &section="") const
Definition: TextFileDBReader.cxx:234
athena.value
value
Definition: athena.py:122
FileMerger.readFile
def readFile(name)
Definition: FileMerger.py:27
TextFileDBReader::sectionPresent
bool sectionPresent(const std::string &section) const
Definition: TextFileDBReader.cxx:262
TextFileDBReader::m_table
std::unordered_map< std::string, Data > m_table
Definition: TextFileDBReader.h:48
lumiFormat.i
int i
Definition: lumiFormat.py:92
master.flag
bool flag
Definition: master.py:29
taskman.fieldName
fieldName
Definition: taskman.py:492
TextFileDBReader::getRowNumber
bool getRowNumber(std::string &key, std::string &rowNumber) const
Definition: TextFileDBReader.cxx:166
TextFileDBReader::find
bool find(const std::string &key, std::string &result) const
Definition: TextFileDBReader.cxx:197
TextFileDBReader::m_currentSection
int m_currentSection
Definition: TextFileDBReader.h:51
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
TextFileDBReader::m_sections
std::unordered_map< std::string, int > m_sections
Definition: TextFileDBReader.h:49
TextFileDBReader.h
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
TextFileDBReader::add
void add(const std::string &key, const std::string &value)
Definition: TextFileDBReader.cxx:188
TextFileDBReader::readFile
bool readFile(const std::string &filename)
Definition: TextFileDBReader.cxx:26
section
void section(const std::string &sec)
Definition: TestTriggerMenuAccess.cxx:22
TextFileDBReader::formatKey
std::string formatKey(const std::string &key) const
Definition: TextFileDBReader.cxx:121
TextFileDBReader::printParameters
void printParameters(const std::string &section="") const
Definition: TextFileDBReader.cxx:211
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37