EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHFlag.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHFlag.cc
1 #include "PHFlag.h"
2 
3 #include <boost/stacktrace.hpp>
4 
5 #include <fstream>
6 #include <iostream>
7 #include <map>
8 #include <utility> // for pair
9 
10 using namespace std;
11 
12 const string
13 PHFlag::get_CharFlag(const string &name) const
14 {
15  map<string, string>::const_iterator iter = charflag.find(name);
16  if (iter != charflag.end())
17  {
18  return iter->second;
19  }
20  cout << "PHFlag::getString: ERROR Unknown character Flag " << name << endl;
21  PrintStackTrace();
22  cout << "The following flags are implemented: " << endl;
23  Print();
24  return nullptr;
25 }
26 
27 const string
28 PHFlag::get_CharFlag(const string &name, const string &defaultval)
29 {
30  map<string, string>::const_iterator iter = charflag.find(name);
31  if (iter != charflag.end())
32  {
33  return iter->second;
34  }
35  else
36  {
37  set_CharFlag(name, defaultval);
38  return get_CharFlag(name);
39  }
40 }
41 
42 void PHFlag::set_CharFlag(const string &name, const string &charstr)
43 {
44  charflag[name] = charstr;
45  return;
46 }
47 
48 double PHFlag::get_DoubleFlag(const string &name) const
49 {
50  map<string, double>::const_iterator iter = doubleflag.find(name);
51  if (iter != doubleflag.end())
52  {
53  return iter->second;
54  }
55  cout << "PHFlag::getFlag: ERROR Unknown Double Flag " << name << endl;
56  PrintStackTrace();
57  cout << "The following flags are implemented: " << endl;
58  Print();
59  return 0.0;
60 }
61 
62 double PHFlag::get_DoubleFlag(const string &name, const double defaultval)
63 {
64  map<string, double>::const_iterator iter = doubleflag.find(name);
65  if (iter != doubleflag.end())
66  {
67  return iter->second;
68  }
69  else
70  {
71  set_DoubleFlag(name, defaultval);
72  return get_DoubleFlag(name);
73  }
74 }
75 
76 void PHFlag::set_DoubleFlag(const string &name, const double iflag)
77 {
78  doubleflag[name] = iflag;
79  return;
80 }
81 
82 float PHFlag::get_FloatFlag(const string &name) const
83 {
84  map<string, float>::const_iterator iter = floatflag.find(name);
85  if (iter != floatflag.end())
86  {
87  return iter->second;
88  }
89  cout << "PHFlag::getFlag: ERROR Unknown Float Flag " << name << endl;
90  PrintStackTrace();
91  cout << "The following flags are implemented: " << endl;
92  Print();
93  return 0.0;
94 }
95 
96 float PHFlag::get_FloatFlag(const string &name, const float defaultval)
97 {
98  map<string, float>::const_iterator iter = floatflag.find(name);
99  if (iter != floatflag.end())
100  {
101  return iter->second;
102  }
103  else
104  {
105  set_FloatFlag(name, defaultval);
106  return get_FloatFlag(name);
107  }
108 }
109 
110 void PHFlag::set_FloatFlag(const string &name, const float iflag)
111 {
112  floatflag[name] = iflag;
113  return;
114 }
115 
116 int PHFlag::get_IntFlag(const string &name) const
117 {
118  map<string, int>::const_iterator iter = intflag.find(name);
119  if (iter != intflag.end())
120  {
121  return iter->second;
122  }
123  cout << "PHFlag::getFlag: ERROR Unknown Int Flag " << name << endl;
124  PrintStackTrace();
125  cout << "The following flags are implemented: " << endl;
126  Print();
127  return 0;
128 }
129 
130 int PHFlag::get_IntFlag(const string &name, int defaultval)
131 {
132  map<string, int>::const_iterator iter = intflag.find(name);
133  if (iter != intflag.end())
134  {
135  return iter->second;
136  }
137  else
138  {
139  set_IntFlag(name, defaultval);
140  return get_IntFlag(name);
141  }
142 }
143 
144 void PHFlag::set_IntFlag(const string &name, const int iflag)
145 {
146  intflag[name] = iflag;
147  return;
148 }
149 
150 void PHFlag::Print() const
151 {
152  PrintIntFlags();
153  PrintFloatFlags();
154  PrintDoubleFlags();
155  PrintCharFlags();
156  return;
157 }
158 
160 {
161  // loop over the map and print out the content (name and location in memory)
162  cout << endl
163  << "Integer Flags:" << endl;
164  map<string, int>::const_iterator intiter;
165  for (intiter = intflag.begin(); intiter != intflag.end(); ++intiter)
166  {
167  cout << intiter->first << " is " << intiter->second << endl;
168  }
169  return;
170 }
171 
173 {
174  // loop over the map and print out the content (name and location in memory)
175  cout << endl
176  << "Double Flags:" << endl;
177  map<string, double>::const_iterator doubleiter;
178  for (doubleiter = doubleflag.begin(); doubleiter != doubleflag.end(); ++doubleiter)
179  {
180  cout << doubleiter->first << " is " << doubleiter->second << endl;
181  }
182  return;
183 }
184 
186 {
187  // loop over the map and print out the content (name and location in memory)
188  cout << endl
189  << "Float Flags:" << endl;
190  map<string, float>::const_iterator floatiter;
191  for (floatiter = floatflag.begin(); floatiter != floatflag.end(); ++floatiter)
192  {
193  cout << floatiter->first << " is " << floatiter->second << endl;
194  }
195  return;
196 }
197 
199 {
200  // loop over the map and print out the content (name and location in memory)
201  cout << endl
202  << "char* Flags:" << endl;
203  map<string, string>::const_iterator chariter;
204  for (chariter = charflag.begin(); chariter != charflag.end(); ++chariter)
205  {
206  cout << chariter->first << " is " << chariter->second << endl;
207  }
208  return;
209 }
210 
211 int PHFlag::FlagExist(const string &name) const
212 {
213  map<string, int>::const_iterator iter = intflag.find(name);
214  if (iter != intflag.end())
215  {
216  return 1;
217  }
218  map<string, float>::const_iterator fiter = floatflag.find(name);
219  if (fiter != floatflag.end())
220  {
221  return 1;
222  }
223  map<string, double>::const_iterator diter = doubleflag.find(name);
224  if (diter != doubleflag.end())
225  {
226  return 1;
227  }
228  map<string, string>::const_iterator citer = charflag.find(name);
229  if (citer != charflag.end())
230  {
231  return 1;
232  }
233  return 0;
234 }
235 
236 void PHFlag::ReadFromFile(const string &name)
237 {
238  string label;
239  float fvalue;
240  int fvaluecount = 0;
241  double dvalue;
242  int dvaluecount = 0;
243  int ivalue;
244  int ivaluecount = 0;
245  string cvalue;
246  int cvaluecount = 0;
247  string junk;
248  int junkcount = 0;
249 
250  ifstream infile(name.c_str());
251  while (infile >> label)
252  {
253  cout << "Label" << label;
254  if (label.substr(0, 1) == "C")
255  {
256  infile >> cvalue;
257  cvaluecount++;
258  set_CharFlag(label.substr(1, label.size() - 1), cvalue);
259  cout << " C read " << cvalue << endl;
260  }
261  else if (label.substr(0, 1) == "F")
262  {
263  infile >> fvalue;
264  fvaluecount++;
265  set_FloatFlag(label.substr(1, label.size() - 1), fvalue);
266  cout << " F read " << fvalue << endl;
267  }
268  else if (label.substr(0, 1) == "D")
269  {
270  infile >> dvalue;
271  dvaluecount++;
272  set_DoubleFlag(label.substr(1, label.size() - 1), dvalue);
273  cout << " D read " << dvalue << endl;
274  }
275  else if (label.substr(0, 1) == "I")
276  {
277  infile >> ivalue;
278  ivaluecount++;
279  set_IntFlag(label.substr(1, label.size() - 1), ivalue);
280  cout << " I read " << ivalue << endl;
281  }
282  else
283  {
284  infile >> junk;
285  junkcount++;
286  cout << " Junk read " << junk << endl;
287  }
288  }
289 
290  cout << "Read CharFlags(" << cvaluecount
291  << ") FloatFlags(" << fvaluecount
292  << ") DoubleFlags(" << dvaluecount
293  << ") IntFlags(" << ivaluecount
294  << ") JunkEntries(" << junkcount
295  << ") from file " << name << endl;
296 
297  infile.close();
298 }
299 
300 void PHFlag::WriteToFile(const string &name)
301 {
302  ofstream outFile(name.c_str());
303  // loop over the map and write out the content
304  map<string, int>::const_iterator intiter;
305  for (intiter = intflag.begin(); intiter != intflag.end(); ++intiter)
306  {
307  outFile << "I" << intiter->first << "\t" << intiter->second << endl;
308  }
309 
310  map<string, float>::const_iterator floatiter;
311  for (floatiter = floatflag.begin(); floatiter != floatflag.end(); ++floatiter)
312  {
313  outFile << "F" << floatiter->first << "\t" << floatiter->second << endl;
314  }
315 
316  int oldprecision = outFile.precision(15);
317  map<string, double>::const_iterator doubleiter;
318  for (doubleiter = doubleflag.begin(); doubleiter != doubleflag.end(); ++doubleiter)
319  {
320  outFile << "D" << doubleiter->first << "\t" << doubleiter->second << endl;
321  }
322  outFile.precision(oldprecision);
323 
324  map<string, string>::const_iterator chariter;
325  for (chariter = charflag.begin(); chariter != charflag.end(); ++chariter)
326  {
327  outFile << "C" << chariter->first << "\t" << chariter->second << endl;
328  }
329 
330  outFile.close();
331 }
332 
334 {
335  cout << "Called by #3 or #4 in this list: " << endl;
336  cout << boost::stacktrace::stacktrace();
337  cout << endl;
338  cout << "DO NOT PANIC - this is not a segfault" << endl;
339 }