ATLAS Offline Software
Loading...
Searching...
No Matches
LArBadChannelParser Class Reference

#include <LArBadChannelParser.h>

Collaboration diagram for LArBadChannelParser:

Public Member Functions

 LArBadChannelParser (const std::string &filename, MsgStream *const log, unsigned int numInts, int numStrings, unsigned int firstWildcard=0)
 A parser object must be initialized with the filename that it is to parse.
virtual ~LArBadChannelParser ()
bool fileStatusGood () const
 You can use this function to check whether file-reading will succeed.
template<typename T>
std::vector< std::pair< std::vector< int >, std::vector< T > > > parseFile ()
 Parse the file using the following format for each line: First, exactly numInts integers are required.
template<>
bool convertStrings (std::vector< std::string > &result, const std::vector< std::string >::const_iterator begin, const std::vector< std::string >::const_iterator end) const

Private Member Functions

 LArBadChannelParser ()
std::vector< int > getIdFields (const std::vector< std::string > &words)
std::vector< std::string > parseLine (std::string &line) const
template<typename T>
bool stringToNumber (T &theNumber, const std::string &theStr) const
template<typename T>
bool convertStrings (std::vector< T > &result, const std::vector< std::string >::const_iterator begin, const std::vector< std::string >::const_iterator end) const

Private Attributes

std::ifstream m_fin
unsigned int m_linenumber
const std::string m_filename
MsgStream *const m_log
const unsigned int m_numInts
const int m_numStrings
const unsigned int m_firstWildcard

Detailed Description

Definition at line 11 of file LArBadChannelParser.h.

Constructor & Destructor Documentation

◆ LArBadChannelParser() [1/2]

LArBadChannelParser::LArBadChannelParser ( const std::string & filename,
MsgStream *const log,
unsigned int numInts,
int numStrings,
unsigned int firstWildcard = 0 )

A parser object must be initialized with the filename that it is to parse.

It must also be given a MsgStream pointer for logging messages.

Definition at line 10 of file LArBadChannelParser.cxx.

13 :
15 m_numInts(numInts), m_numStrings(numStrings),
16 m_firstWildcard(firstWildcard)
17{
18 m_fin.open(m_filename.c_str());
19 if(!m_log)
20 std::cerr << "ERROR \t LArBadChannelParser was given a NULL MsgStream pointer!" << std::endl;
21}
const unsigned int m_firstWildcard
const unsigned int m_numInts
const std::string m_filename
TFile * file

◆ ~LArBadChannelParser()

LArBadChannelParser::~LArBadChannelParser ( )
virtual

Definition at line 23 of file LArBadChannelParser.cxx.

24{
25 m_fin.close();
26}

◆ LArBadChannelParser() [2/2]

LArBadChannelParser::LArBadChannelParser ( )
private

Member Function Documentation

◆ convertStrings() [1/2]

template<>
bool LArBadChannelParser::convertStrings ( std::vector< std::string > & result,
const std::vector< std::string >::const_iterator begin,
const std::vector< std::string >::const_iterator end ) const
inline

Definition at line 164 of file LArBadChannelParser.h.

166{
167 result.assign(begin, end);
168 return true;
169}

◆ convertStrings() [2/2]

template<typename T>
bool LArBadChannelParser::convertStrings ( std::vector< T > & result,
const std::vector< std::string >::const_iterator begin,
const std::vector< std::string >::const_iterator end ) const
inlineprivate

Definition at line 142 of file LArBadChannelParser.h.

144{
145 result.clear();
146 result.reserve(static_cast<unsigned int>(end - begin));
147 for(std::vector<std::string>::const_iterator iter = begin; iter != end; ++iter)
148 {
149 T element;
150 if(stringToNumber<T>(element, *iter))
151 result.push_back(element);
152 else
153 {
154 (*m_log) << MSG::WARNING << "LArBadChannelParser REJECTED line " << m_linenumber \
155 << " -\t failed to extract status." << endmsg;
156 return false;
157 }
158 }
159 return true;
160}
#define endmsg
bool stringToNumber(T &theNumber, const std::string &theStr) const
unsigned long long T

◆ fileStatusGood()

bool LArBadChannelParser::fileStatusGood ( ) const
inline

You can use this function to check whether file-reading will succeed.

If false, parseFile() will return an empty vector.

Definition at line 74 of file LArBadChannelParser.h.

75{
76 return m_fin.good();
77}

◆ getIdFields()

std::vector< int > LArBadChannelParser::getIdFields ( const std::vector< std::string > & words)
private

Definition at line 54 of file LArBadChannelParser.cxx.

55{
56 std::vector<int> result;
57 std::string w;
58 for(unsigned int i = 0; i < m_numInts; ++i)
59 {
60 w=words[i]; //Copy to non-const string
61
62 //Allow 'B' or 'EMB' for Barrel or 'E' for Endcap in first field
63 if (i==0) {
64 std::transform(w.begin(), w.end(), w.begin(), upper);
65
66 if (w.compare(0,3,"EMB")==0 || w[0]=='B')
67 w=std::string("0"); //Replace for barrel
68 else if (w[0]=='E')
69 w=std::string("1"); //Replace for endcap
70 }
71 else if (i==1) { //Allow 'A' and 'C' do describe side in second field
72 if (w=="A" || w=="a")
73 w=std::string("1"); //Replace for positive side
74 else if (w=="C" || w=="c")
75 w=std::string("0"); //Replace for negative side
76 }
77
78 int idComponent(0);
79 bool canBeWildcard(m_firstWildcard > 0 && i + 1 >= m_firstWildcard);
80 if(canBeWildcard && w == "*")
81 result.push_back(-1); // -1 signifies wildcard
82 else if(stringToNumber<int>(idComponent, w) && idComponent >= 0)
83 result.push_back(idComponent);
84 else
85 {
86 (*m_log) << MSG::WARNING << "LArBadChannelParser REJECTED line " << m_linenumber \
87 << " -\t word " << i + 1 << " must be a non-negative integer " \
88 << (canBeWildcard ? "or " : "and not " ) \
89 << "a wildcard: " << words[i] << endmsg;
90 result.clear();
91 return result; // return empty vector on error
92 }
93 }
94 return result;
95}
int upper(int c)

◆ parseFile()

template<typename T>
std::vector< std::pair< std::vector< int >, std::vector< T > > > LArBadChannelParser::parseFile ( )
inline

Parse the file using the following format for each line: First, exactly numInts integers are required.

Then, if numString is negative, at least std::abs(numString) strings are required, otherwise exactly numString strings are required. These strings are converted to a vector<T>. A vector of all successfully parsed lines is returned. The integer part of a line may contain wildcards, starting from position firstWildcard (where counting begins from 1); firstWildcard = 0 means wildcards are not allowed. The wildcard character in the input file is "*", it is replaced by -1 in the parsed output. Comments in the ASCII file are demarcated with the '#' character. Lines that fail to parse are skipped, with diagnostic messages sent to the message stream.

Definition at line 80 of file LArBadChannelParser.h.

81{
82 typedef std::pair< std::vector<int>, std::vector<T> > LineData;
83 std::vector<LineData> result;
84
85 if(!fileStatusGood())
86 {
87 (*m_log) << MSG::ERROR << "LArBadChannelParser - Could not open file: " << m_filename << endmsg;
88 return result; //empty result
89 }
90
91 while(true)
92 {
93 std::string line;
94 std::getline(m_fin, line);
95 if(!fileStatusGood())
96 break; // loop termination at end of file
98 std::vector<std::string> input = parseLine(line);
99
100 if(input.size() < m_numInts + std::abs(m_numStrings)) // if line is too short, skip it
101 {
102 if(input.size() != 0) // complain only for non-empty lines
103 {(*m_log) << MSG::WARNING << "LArBadChannelParser REJECTED line " << m_linenumber \
104 << " -\t not enough parameters given: " << input.size() << endmsg;}
105 continue;
106 }
107
108 LineData parsedData;
109
110 parsedData.first = getIdFields(input);
111 if(parsedData.first.size() == 0)
112 continue; // getIdFields failed, with error message. Skip this line.
113
114 const std::vector<std::string>::const_iterator start = input.begin() + m_numInts;
115 const std::vector<std::string>::const_iterator stop = \
116 (m_numStrings < 0) ? static_cast<const std::vector<std::string>::const_iterator>(input.end()) : (start + m_numStrings);
117
118 if(stop > start)
119 {
120 if(!convertStrings<T>(parsedData.second, start, stop))
121 continue; //convertStrings failed with error message. Skip this line.
122 }
123
124 if(stop < input.end())
125 (*m_log) << MSG::WARNING << "LArBadChannelParser IGNORED unexpected input on line " << m_linenumber << "." << endmsg;
126
127 result.push_back(std::move(parsedData));
128 (*m_log) << MSG::VERBOSE << "LArBadChannelParser ACCEPTED line " << m_linenumber << " -\t " << line << endmsg;
129 }
130
131 (*m_log) << MSG::DEBUG << "LArBadChannelParser - Parsed " << m_linenumber << " lines in file '" << m_filename << "'." << endmsg;
132 return result;
133}
std::vector< int > getIdFields(const std::vector< std::string > &words)
bool fileStatusGood() const
You can use this function to check whether file-reading will succeed.
std::vector< std::string > parseLine(std::string &line) const
bool convertStrings(std::vector< T > &result, const std::vector< std::string >::const_iterator begin, const std::vector< std::string >::const_iterator end) const

◆ parseLine()

std::vector< std::string > LArBadChannelParser::parseLine ( std::string & line) const
private

Definition at line 28 of file LArBadChannelParser.cxx.

29{
30 std::string::size_type commentPosition = line.find('#');
31 if(commentPosition != std::string::npos) //if a comment is found
32 line.erase(commentPosition); //trim off the comment
33 // notice that the input parameter can be changed as a side-effect
34
35 std::vector<std::string> result;
36 std::string readWord;
37 std::istringstream stringIn(line);
38
39 stringIn >> readWord;
40 while(stringIn)
41 {
42 result.push_back(readWord);
43 stringIn >> readWord;
44 }
45 return result;
46}

◆ stringToNumber()

template<typename T>
bool LArBadChannelParser::stringToNumber ( T & theNumber,
const std::string & theStr ) const
inlineprivate

Definition at line 173 of file LArBadChannelParser.h.

174{
175 std::istringstream iss(theString);
176 //return !(iss >> std::dec >> theNumber).fail() && (static_cast<int>(theString.size()) == iss.tellg());
177 return !(iss >> std::dec >> theNumber).fail();
178 //the second condition checks for invalid input of the form "123abc"
179}
fail(message)

Member Data Documentation

◆ m_filename

const std::string LArBadChannelParser::m_filename
private

Definition at line 64 of file LArBadChannelParser.h.

◆ m_fin

std::ifstream LArBadChannelParser::m_fin
private

Definition at line 62 of file LArBadChannelParser.h.

◆ m_firstWildcard

const unsigned int LArBadChannelParser::m_firstWildcard
private

Definition at line 69 of file LArBadChannelParser.h.

◆ m_linenumber

unsigned int LArBadChannelParser::m_linenumber
private

Definition at line 63 of file LArBadChannelParser.h.

◆ m_log

MsgStream* const LArBadChannelParser::m_log
private

Definition at line 65 of file LArBadChannelParser.h.

◆ m_numInts

const unsigned int LArBadChannelParser::m_numInts
private

Definition at line 67 of file LArBadChannelParser.h.

◆ m_numStrings

const int LArBadChannelParser::m_numStrings
private

Definition at line 68 of file LArBadChannelParser.h.


The documentation for this class was generated from the following files: