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 188 of file TextFileDBReader.cxx.

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 }

◆ add() [2/2]

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

Definition at line 180 of file TextFileDBReader.cxx.

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

◆ find()

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

Definition at line 197 of file TextFileDBReader.cxx.

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 }

◆ formatKey()

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

Definition at line 121 of file TextFileDBReader.cxx.

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 }

◆ getRowNumber()

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

Definition at line 166 of file TextFileDBReader.cxx.

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 }

◆ printNotUsed()

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

Definition at line 234 of file TextFileDBReader.cxx.

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 }

◆ printParameters()

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

Definition at line 211 of file TextFileDBReader.cxx.

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 }

◆ 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  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 }

◆ sectionPresent()

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

Definition at line 262 of file TextFileDBReader.cxx.

263 {
264  return (m_sections.find(section) != m_sections.end());
265 }

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:
checkFileSG.line
line
Definition: checkFileSG.py:75
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
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:492
TextFileDBReader::getRowNumber
bool getRowNumber(std::string &key, std::string &rowNumber) const
Definition: TextFileDBReader.cxx:166
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
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
TextFileDBReader::add
void add(const std::string &key, const std::string &value)
Definition: TextFileDBReader.cxx:188
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:121
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37