ATLAS Offline Software
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
TextFileDBReader Class Reference

Class to read in a text file and allow easy retrieval of parameters. More...

#include <TextFileDBReader.h>

Collaboration diagram for TextFileDBReader:

Classes

class  Data
 

Public Member Functions

 TextFileDBReader (const std::string &filename)
 
 TextFileDBReader ()
 
bool readFile (const std::string &filename)
 
bool find (const std::string &key, std::string &result) const
 
void printParameters (const std::string &section="") const
 
void printNotUsed (const std::string &section="") const
 
bool sectionPresent (const std::string &section) const
 

Private Member Functions

std::string formatKey (const std::string &key) const
 
bool getRowNumber (std::string &key, std::string &rowNumber) const
 
void add (const std::string &key, const std::string &value)
 
void add (const std::string &key, int)
 

Private Attributes

std::unordered_map< std::string, Datam_table
 
std::unordered_map< std::string, int > m_sections
 
int m_numSections
 
int m_currentSection
 
std::string m_name
 

Detailed Description

Class to read in a text file and allow easy retrieval of parameters.

Definition at line 17 of file TextFileDBReader.h.

Constructor & Destructor Documentation

◆ TextFileDBReader() [1/2]

TextFileDBReader::TextFileDBReader ( const std::string &  filename)

Definition at line 17 of file TextFileDBReader.cxx.

18  : m_numSections(0),
20 {
22 }

◆ TextFileDBReader() [2/2]

TextFileDBReader::TextFileDBReader ( )

Definition at line 12 of file TextFileDBReader.cxx.

13  : m_numSections(0),
15 {}

Member Function Documentation

◆ add() [1/2]

void TextFileDBReader::add ( const std::string &  key,
const std::string &  value 
)
private

Definition at line 186 of file TextFileDBReader.cxx.

187 {
188  if (m_table.find(key) != m_table.end()) {
189  std::cout << "WARNING! Overwriting exist entry with key: " << key << std::endl;
190  }
191  m_table.try_emplace (key, value, m_currentSection);
192 }

◆ add() [2/2]

void TextFileDBReader::add ( const std::string &  key,
int  value 
)
private

Definition at line 178 of file TextFileDBReader.cxx.

178  {
179  std::ostringstream ostr;
180  ostr << value;
181  add(key,ostr.str());
182 }

◆ find()

bool TextFileDBReader::find ( const std::string &  key,
std::string &  result 
) const

Definition at line 195 of file TextFileDBReader.cxx.

196 {
197  std::unordered_map<std::string,Data>::const_iterator iter = m_table.find(key);
198  if (iter != m_table.end()) {
199  result = iter->second.value;
200  (iter->second).flag=true;
201  return true;
202  } else {
203  result = "";
204  return false;
205  }
206 }

◆ formatKey()

std::string TextFileDBReader::formatKey ( const std::string &  key) const
private

Definition at line 119 of file TextFileDBReader.cxx.

120 {
121  //std::cout << "Key in: " << key << std::endl;
122  // Split into tableName, fieldName and rowNumber
123  // and recreate
124  std::string tableName;
125  std::string fieldName;
126  std::string rowNumber;
127  bool foundRowNumber = false;
128  std::string::size_type pos = key.find(':');
129  if (pos != std::string::npos) {
130  tableName = key.substr(0,pos);
131  foundRowNumber = getRowNumber(tableName,rowNumber);
132  fieldName = key.substr(++pos);
133  } else {
134  fieldName = key;
135  }
136  std::string tmpRowNumber;
137  if (getRowNumber(fieldName,tmpRowNumber)) {
138  if (foundRowNumber) {
139  // Already have rowNumber from before
140  std::cout << "ERROR in format:" << key << std::endl;
141  } else {
142  rowNumber = std::move(tmpRowNumber);
143  }
144  }
145 
146  std::string newKey = key;
147  if ((tableName.empty() && !rowNumber.empty()) || fieldName.empty()) {
148  std::cout << "ERROR in format: " << key << std::endl;
149  } else {
150  if (rowNumber.empty()) rowNumber = "0";
151  if (tableName.empty()) {
152  newKey = std::move(fieldName);
153  }else if (tableName == "TableSize") {
154  newKey = "TableSize:"+fieldName;
155  } else {
156  newKey = tableName+"#"+rowNumber+":"+fieldName;
157  }
158  }
159  //std::cout << "Key out: " << newKey << std::endl;
160  return newKey;
161 }

◆ getRowNumber()

bool TextFileDBReader::getRowNumber ( std::string &  key,
std::string &  rowNumber 
) const
private

Definition at line 164 of file TextFileDBReader.cxx.

165 {
166  std::string::size_type pos = key.find('#');
167  if (pos != std::string::npos) {
168  rowNumber = key.substr(pos+1);
169  key.resize(pos);
170  return true;
171  }
172  return false;
173 }

◆ printNotUsed()

void TextFileDBReader::printNotUsed ( const std::string &  section = "") const

Definition at line 232 of file TextFileDBReader.cxx.

233 {
234  std::ios::fmtflags iosflags = std::cout.flags();
235  std::cout << std::left;
236  bool allused = true;
237  int sectionNum = 0;
238  if (!section.empty()) {
239  std::unordered_map<std::string,int>::const_iterator iterSect = m_sections.find(section);
240  if (iterSect != m_sections.end()) sectionNum = iterSect->second;
241  // If not found then considers those in unnamed section (ie sectionNum = 0)
242  }
243  for (std::unordered_map<std::string,Data>::const_iterator iter = m_table.begin();
244  iter != m_table.end();
245  ++iter) {
246  if ((section.empty() || iter->second.section == sectionNum) && (!(iter->second.flag))) {
247  std::cout << std::setw(35) << iter->first << " " << iter->second.value << std::endl;
248  allused = false;
249  }
250  }
251  if (allused) {
252  std::cout << "All parameters used" << std::endl;
253  }
254  // reset flags to original state
255  std::cout.flags(iosflags);
256 }

◆ printParameters()

void TextFileDBReader::printParameters ( const std::string &  section = "") const

Definition at line 209 of file TextFileDBReader.cxx.

210 {
211  std::ios::fmtflags iosflags = std::cout.flags();
212  std::cout << std::left;
213  int sectionNum = 0;
214  if (!section.empty()) {
215  std::unordered_map<std::string,int>::const_iterator iterSect = m_sections.find(section);
216  if (iterSect != m_sections.end()) sectionNum = iterSect->second;
217  // If not found then prints those in unnamed section.(ie sectionNum = 0)
218  }
219  for (std::unordered_map<std::string,Data>::const_iterator iter = m_table.begin();
220  iter != m_table.end();
221  ++iter) {
222  if (section.empty() || iter->second.section == sectionNum) {
223  std::cout << std::setw(35) << iter->first << " " << iter->second.value << std::endl;
224  }
225  }
226  // reset flags to original state
227  std::cout.flags(iosflags);
228 }

◆ readFile()

bool TextFileDBReader::readFile ( const std::string &  filename)

Definition at line 26 of file TextFileDBReader.cxx.

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  istr >> currentTable;
57  tableMode = true;
58  currentIndex = -1;
59  currentFields.clear();
60  } else if (key == "TableEnd") {
61  std::ostringstream ostr;
62  ostr << "TableSize:" << currentTable;
63  add(ostr.str(), currentIndex);
64  tableMode = false;
65  currentTable = "";
66  currentIndex = -1;
67  currentFields.clear();
68  } else if (key == "Section") {
69  std::string value;
70  istr >> value;
71  if (!value.empty()) {
72  int & section = m_sections[value];
73  // First section will number 1. Section 0 refers to unnamed section
74  if (!section) section = ++m_numSections;
76  }
77  } else if (key == "EndSection") {
78  m_currentSection = 0;
79  } else {
80  if (!tableMode) {
81  std::string value;
82  istr >> value;
83  // Make sure its in correct format
84  key = formatKey(key);
85  add(key,value);
86  } else { // table mode
87  // reset stream.
88  std::istringstream istr2(line);
89 
90  if (currentIndex < 0) {
91  // Get the fields
92  while (!istr2.eof()) {
93  std::string value;
94  istr2 >> value;
95  if (!value.empty()) currentFields.push_back(std::move(value));
96  }
97  } else {
98  // Get row of values
99  for (unsigned int i=0; i < currentFields.size(); i++) {
100  std::string value;
101  istr2 >> value;
102  if (istr2) {
103  std::ostringstream ostr;
104  ostr << currentTable << "#" << currentIndex << ":" << currentFields[i];
105  add(ostr.str(), value);
106  }
107  }
108  }
109  currentIndex++;
110  }
111  }
112  }
113  ifile.close();
114  //printParameters();
115  return true;
116 }

◆ sectionPresent()

bool TextFileDBReader::sectionPresent ( const std::string &  section) const

Definition at line 260 of file TextFileDBReader.cxx.

261 {
262  return (m_sections.find(section) != m_sections.end());
263 }

Member Data Documentation

◆ m_currentSection

int TextFileDBReader::m_currentSection
private

Definition at line 51 of file TextFileDBReader.h.

◆ m_name

std::string TextFileDBReader::m_name
private

Definition at line 52 of file TextFileDBReader.h.

◆ m_numSections

int TextFileDBReader::m_numSections
private

Definition at line 50 of file TextFileDBReader.h.

◆ m_sections

std::unordered_map<std::string, int> TextFileDBReader::m_sections
private

Definition at line 49 of file TextFileDBReader.h.

◆ m_table

std::unordered_map<std::string, Data> TextFileDBReader::m_table
private

Definition at line 48 of file TextFileDBReader.h.


The documentation for this class was generated from the following files:
createLinkingScheme.iter
iter
Definition: createLinkingScheme.py:62
TextFileDBReader::m_numSections
int m_numSections
Definition: TextFileDBReader.h:50
get_generator_info.result
result
Definition: get_generator_info.py:21
athena.value
value
Definition: athena.py:124
dq_defect_bulk_create_defects.line
line
Definition: dq_defect_bulk_create_defects.py:27
TextFileDBReader::m_table
std::unordered_map< std::string, Data > m_table
Definition: TextFileDBReader.h:48
lumiFormat.i
int i
Definition: lumiFormat.py:85
master.flag
bool flag
Definition: master.py:29
taskman.fieldName
fieldName
Definition: taskman.py:489
TextFileDBReader::getRowNumber
bool getRowNumber(std::string &key, std::string &rowNumber) const
Definition: TextFileDBReader.cxx:164
TextFileDBReader::m_currentSection
int m_currentSection
Definition: TextFileDBReader.h:51
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:16
TextFileDBReader::m_sections
std::unordered_map< std::string, int > m_sections
Definition: TextFileDBReader.h:49
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:23
TextFileDBReader::add
void add(const std::string &key, const std::string &value)
Definition: TextFileDBReader.cxx:186
LArCellNtuple.ifile
string ifile
Definition: LArCellNtuple.py:133
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:119
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37