ATLAS Offline Software
ReadCards.h
Go to the documentation of this file.
1 /* emacs: this is -*- c++ -*- */
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 
31 using std::string;
32 using std::vector;
33 using std::cout;
34 using std::cerr;
35 using std::endl;
36 using std::ifstream;
37 using std::ostringstream;
38 using std::exit;
39 
40 
41 #include "Value.h"
42 
50 class ReadCards {
51 
52 public:
53 
54  ReadCards() { }
55  ReadCards(const std::string& filename) { Construct(filename); }
56 
57  ~ReadCards() { }
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 {
124  }
125 
126  void Set(const string& tag, int& value ) const {
128  }
129 
130  void Set(const std::string& tag, std::string& value) const {
132  }
133 
134  void Set(const string& tag, std::vector<double>& value ) const {
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 
191 private:
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 
215 private:
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 
228 private:
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 
ReadCards::m_File
ifstream m_File
Definition: ReadCards.h:219
mergePhysValFiles.pattern
pattern
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:26
beamspotman.r
def r
Definition: beamspotman.py:676
ReadCards::parseleft
string parseleft(string &s)
Definition: ReadCards.cxx:417
athena_checkUpload.Tag
string Tag
Definition: athena_checkUpload.py:189
ReadCards::m_FileName
std::string m_FileName
Definition: ReadCards.h:217
ReadCards::CreatePath
void CreatePath()
Definition: ReadCards.cxx:45
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
ReadCards::declareProperty
void declareProperty(const std::string &key, std::vector< T > &t)
Definition: ReadCards.h:176
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
ReadCards::GetValue
double GetValue(int i) const
Definition: ReadCards.h:88
ReadCards::GetVector
const std::vector< double > GetVector(unsigned i) const
Definition: ReadCards.h:68
hist_file_dump.d
d
Definition: hist_file_dump.py:137
ReadCards::GetValueString
string GetValueString(unsigned i) const
Definition: ReadCards.h:78
Value
tag-value pair class.
Definition: Value.h:39
athena.value
value
Definition: athena.py:124
ReadCards::AddTag
bool AddTag(const string &tag, const vector< string > &values)
Definition: ReadCards.h:203
ReadCards::parse
void parse()
parse the remaining cleaned input string
Definition: ReadCards.cxx:198
ReadCards::Set
void Set(const string &tag, double &value) const
Definition: ReadCards.h:122
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ReadCards::ReadCards
ReadCards()
Definition: ReadCards.h:54
ReadCards::parseright
vector< string > parseright(string &s)
Definition: ReadCards.cxx:427
python.Bindings.values
values
Definition: Control/AthenaPython/python/Bindings.py:805
ReadCards::Set
void Set(const std::string &tag, std::string &value) const
Definition: ReadCards.h:130
ReadCards::m_Path
static std::vector< std::string > m_Path
Definition: ReadCards.h:226
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
tags
std::vector< std::string > tags
Definition: hcg.cxx:102
ReadCards::GetString
std::string GetString(const std::string &tag) const
Definition: ReadCards.h:93
ReadCards
Get tag-value pairs from a file.
Definition: ReadCards.h:50
lumiFormat.i
int i
Definition: lumiFormat.py:85
ReadCards::clean
void clean()
remove comments and whitespace
Definition: ReadCards.cxx:122
vector< string >
ReadCards::Tags
std::vector< string > Tags(const std::string &pattern="") const
Definition: ReadCards.h:154
ReadCards::declareProperty
void declareProperty(const std::string &key, std::vector< std::string > &t)
Definition: ReadCards.h:186
ReadCards::error
void error(const std::string &s)
Definition: ReadCards.h:240
ReadCards::GetString
std::string GetString(const unsigned i) const
Definition: ReadCards.h:97
ReadCards::Set
void Set(const string &tag, std::vector< double > &value) const
Definition: ReadCards.h:134
ReadCards::m_String
std::string m_String
Definition: ReadCards.h:221
calibdata.exit
exit
Definition: calibdata.py:236
ReadCards::Set
void Set(const std::string &tag, std::vector< std::string > &value) const
Definition: ReadCards.h:147
ReadCards::m_ValuesFlag
std::vector< int > m_ValuesFlag
Definition: ReadCards.h:224
ReadCards::m_Values
std::vector< Value > m_Values
Definition: ReadCards.h:223
ReadCards::GetStringVector
std::vector< std::string > GetStringVector(const std::string &tag) const
Definition: ReadCards.h:115
ReadCards::~ReadCards
~ReadCards()
Definition: ReadCards.h:57
ReadCards::isTagDefined
bool isTagDefined(const string &tag) const
Definition: ReadCards.h:61
python.PyAthena.v
v
Definition: PyAthena.py:154
ReadCards::handle
double handle(const std::string &s, const std::string &="") const
Definition: ReadCards.h:246
ReadCards::print
void print()
Definition: ReadCards.cxx:484
a
TList * a
Definition: liststreamerinfos.cxx:10
ReadCards::ReadCards
ReadCards(const std::string &filename)
Definition: ReadCards.h:55
ReadCards::ReadParam
void ReadParam()
Definition: ReadCards.cxx:474
ReadCards::GetIntVector
const std::vector< int > GetIntVector(const std::string &tag) const
Definition: ReadCards.h:107
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
ReadCards::GetNValues
int GetNValues() const
Definition: ReadCards.h:59
ReadCards::GetValue
double GetValue(const string &tag) const
Definition: ReadCards.h:84
ReadCards::Set
void Set(const string &tag, int &value) const
Definition: ReadCards.h:126
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:24
ReadCards::declareProperty
void declareProperty(const std::string &key, T &t)
Definition: ReadCards.h:171
ReadCards::GetIndex
int GetIndex(const std::string &tag) const
Definition: ReadCards.h:196
ReadCards::Set
void Set(const string &tag, std::vector< int > &value) const
Definition: ReadCards.h:138
ReadCards::Construct
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
ReadCards::GetVector
const std::vector< double > GetVector(const std::string &tag) const
Definition: ReadCards.h:102
Value.h
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
ReadCards::declareProperty
void declareProperty(const std::string &key, std::string &t)
Definition: ReadCards.h:182