ATLAS Offline Software
Loading...
Searching...
No Matches
ReadCards.h
Go to the documentation of this file.
1/* emacs: this is -*- c++ -*- */
19
20
21#ifndef READCARDS_READCARDS_H
22#define READCARDS_READCARDS_H
23
24#include <iostream>
25#include <sstream>
26#include <fstream>
27#include <vector>
28#include <cstdlib>
29#include <string>
30
31using std::string;
32using std::vector;
33using std::cout;
34using std::cerr;
35using std::endl;
36using std::ifstream;
37using std::ostringstream;
38using std::exit;
39
40
41#include "Value.h"
42
49
50class ReadCards {
51
52public:
53
55 ReadCards(const std::string& filename) { Construct(filename); }
56
58
59 int GetNValues() const { return m_Values.size(); }
60
61 bool isTagDefined(const string& tag) const {
62 for ( int i=0 ; i<GetNValues() ; i++ ) {
63 if ( tag==m_Values[i].Tag() ) return true;
64 }
65 return false;
66 }
67
68 const std::vector<double> GetVector(unsigned i) const {
69 std::vector<double> dValues;
70 if ( i<m_Values.size() ) {
71 for ( unsigned j=0 ; j<m_Values[i].Val().size() ; j++ ) {
72 dValues.push_back(handle(m_Values[i].Val()[j]));
73 }
74 }
75 return dValues;
76 }
77
78 string GetValueString(unsigned i) const {
79 if ( i<m_Values.size() ) return m_Values[i].Tag();
80 else return string("");
81 }
82
83
84 double GetValue(const string& tag) const {
85 return handle(GetString(tag));
86 }
87
88 double GetValue(int i) const {
89 return (handle(GetString(i)));
90 }
91
92
93 std::string GetString(const std::string& tag) const {
94 return GetString(GetIndex(tag));
95 }
96
97 std::string GetString(const unsigned i) const {
98 if ( i<m_Values.size() ) return m_Values[i].Val()[0];
99 return "";
100 }
101
102 const std::vector<double> GetVector(const std::string& tag) const {
103 return GetVector(GetIndex(tag));
104 }
105
106
107 const std::vector<int> GetIntVector(const std::string& tag) const {
108 std::vector<double> s = GetVector(GetIndex(tag));
109 std::vector<int> a; a.reserve(s.size());
110 for ( size_t i=0 ; i<s.size() ; i++ ) a.push_back( int(s[i]) );
111 return a;
112 }
113
114
115 std::vector<std::string> GetStringVector(const std::string& tag) const {
116 return m_Values[GetIndex(tag)].Val();
117 }
118
119
120
121
122 void Set(const string& tag, double& value ) const {
123 if ( isTagDefined(tag) ) value = handle(GetString(tag));
124 }
125
126 void Set(const string& tag, int& value ) const {
127 if ( isTagDefined(tag) ) value = int(handle(GetString(tag)));
128 }
129
130 void Set(const std::string& tag, std::string& value) const {
131 if ( isTagDefined(tag) ) value = GetString(GetIndex(tag));
132 }
133
134 void Set(const string& tag, std::vector<double>& value ) const {
135 if ( isTagDefined(tag) ) value = GetVector(GetIndex(tag));
136 }
137
138 void Set(const string& tag, std::vector<int>& value ) const {
139 if ( isTagDefined(tag) ) {
140 std::vector<double> v = GetVector(GetIndex(tag));
141 value.clear();
142 value.resize(v.size());
143 for ( unsigned i=v.size() ; i-- ; ) value[i] = v[i];
144 }
145 }
146
147 void Set(const std::string& tag, std::vector<std::string>& value) const {
148 if ( isTagDefined(tag) ) value = m_Values[GetIndex(tag)].Val();
149 }
150
151 void print();
152
153
154 std::vector<string> Tags(const std::string& pattern="") const {
155 std::vector<string> tags;
156 if ( pattern=="" ) {
157 for ( unsigned i=0 ; i<m_Values.size() ; i++ ) tags.push_back(m_Values[i].Tag());
158 }
159 else {
160 for ( unsigned i=0 ; i<m_Values.size() ; i++ ) {
161 if ( m_Values[i].Tag().find(pattern)!=std::string::npos ) tags.push_back(m_Values[i].Tag());
162 }
163 }
164 return tags;
165 }
166
167 // void AddToPath(const std::string& dir) { mPath.push_back(dir); }
168
169 // const std::vector<std::string>& GetPath() const { return mPath; }
170 template<class T>
171 void declareProperty( const std::string& key, T& t ) {
172 if ( isTagDefined(key) ) t = GetValue(key);
173 }
174
175 template<class T>
176 void declareProperty( const std::string& key, std::vector<T>& t ) {
177 if ( isTagDefined(key) ) t = GetVector(key);
178 }
179
180
181
182 void declareProperty( const std::string& key, std::string& t ) {
183 if ( isTagDefined(key) ) t = GetString(key);
184 }
185
186 void declareProperty( const std::string& key, std::vector<std::string>& t ) {
187 if ( isTagDefined(key) ) t = GetStringVector(key);
188 }
189
190
191private:
192
193 void ReadParam();
194 void clean();
195
196 int GetIndex(const std::string& tag) const {
197 for ( int i=GetNValues() ; i-- ; ) if ( tag == m_Values[i].Tag() ) return i;
198 cerr << "ReadCards::GetValue() no tag: " << tag << " in file: " << m_FileName << endl;
199 exit(-1);
200 return 0;
201 }
202
203 bool AddTag(const string& tag, const vector<string>& values) {
204 for ( unsigned i=0 ; i<m_Values.size() ; i++ ) {
205 if ( tag == m_Values[i].Tag() ) {
206 cerr << "ReadCards::addTag() tag " << tag << " already defined in file " << m_FileName << endl;
207 exit(-1);
208 }
209 }
210 m_Values.push_back(Value(tag,values));
211 return true;
212 }
213
214
215private:
216
217 std::string m_FileName;
218
219 ifstream m_File;
220
221 std::string m_String;
222
223 std::vector<Value> m_Values;
224 std::vector<int> m_ValuesFlag;
225
226 static std::vector<std::string> m_Path;
227
228private:
229
230 void Construct(const std::string& filename);
231
232 void CreatePath();
233
234 void parse(); // parse a line
235 string parseleft(string& s); // parse the lvalue
236 vector<string> parseright(string& s); // parse the rvalue
237
238
239 // error reporting routine
240 void error(const std::string& s) {
241 cerr << "ReadCards() syntax error in file " << m_FileName << " : " << s << endl;
242 exit(0);
243 }
244
245
246 double handle(const std::string& s, const std::string& ="") const {
247 double d;
248 char temps[1024] = "";
249 int r=0;
250 if ( (r = std::sscanf(s.c_str(), "%lf%1023s", &d, temps))==0 || r!=1 || string(temps)!="" ) {
251 cerr << "ReadCards() error converting string to number : " << s << endl;
252 exit(0);
253 }
254 // cout << "r=" << r << " d=" << d <<" temps=" << temps << endl;
255 return d;
256 }
257
258};
259
260#endif /* READCARDS_READCARDS_H */
261
262
263
264
265
266
267
268
269
270
static Double_t a
void Set(const std::string &tag, std::vector< std::string > &value) const
Definition ReadCards.h:147
vector< string > parseright(string &s)
std::string m_FileName
Definition ReadCards.h:217
void Set(const string &tag, std::vector< double > &value) const
Definition ReadCards.h:134
std::vector< int > m_ValuesFlag
Definition ReadCards.h:224
bool AddTag(const string &tag, const vector< string > &values)
Definition ReadCards.h:203
std::string m_String
Definition ReadCards.h:221
std::vector< string > Tags(const std::string &pattern="") const
Definition ReadCards.h:154
ifstream m_File
Definition ReadCards.h:219
void declareProperty(const std::string &key, T &t)
Definition ReadCards.h:171
static std::vector< std::string > m_Path
Definition ReadCards.h:226
string parseleft(string &s)
double handle(const std::string &s, const std::string &="") const
Definition ReadCards.h:246
double GetValue(const string &tag) const
Definition ReadCards.h:84
int GetNValues() const
Definition ReadCards.h:59
string GetValueString(unsigned i) const
Definition ReadCards.h:78
void parse()
parse the remaining cleaned input string
const std::vector< int > GetIntVector(const std::string &tag) const
Definition ReadCards.h:107
void declareProperty(const std::string &key, std::vector< T > &t)
Definition ReadCards.h:176
const std::vector< double > GetVector(unsigned i) const
Definition ReadCards.h:68
void CreatePath()
Definition ReadCards.cxx:45
int GetIndex(const std::string &tag) const
Definition ReadCards.h:196
void ReadParam()
void Construct(const std::string &filename)
check for file in cwd or if not, check in the RESPLOTDIR, then read the file
Definition ReadCards.cxx:62
void Set(const string &tag, double &value) const
Definition ReadCards.h:122
void declareProperty(const std::string &key, std::string &t)
Definition ReadCards.h:182
std::vector< std::string > GetStringVector(const std::string &tag) const
Definition ReadCards.h:115
void error(const std::string &s)
Definition ReadCards.h:240
void declareProperty(const std::string &key, std::vector< std::string > &t)
Definition ReadCards.h:186
void print()
bool isTagDefined(const string &tag) const
Definition ReadCards.h:61
void Set(const std::string &tag, std::string &value) const
Definition ReadCards.h:130
ReadCards(const std::string &filename)
Definition ReadCards.h:55
std::string GetString(const unsigned i) const
Definition ReadCards.h:97
void clean()
remove comments and whitespace
std::vector< Value > m_Values
Definition ReadCards.h:223
std::string GetString(const std::string &tag) const
Definition ReadCards.h:93
const std::vector< double > GetVector(const std::string &tag) const
Definition ReadCards.h:102
double GetValue(int i) const
Definition ReadCards.h:88
void Set(const string &tag, int &value) const
Definition ReadCards.h:126
void Set(const string &tag, std::vector< int > &value) const
Definition ReadCards.h:138
tag-value pair class.
Definition Value.h:39
int r
Definition globals.cxx:22
std::vector< std::string > tags
Definition hcg.cxx:105
std::string find(const std::string &s)
return a remapped string
Definition hcg.cxx:138