Analysis Software
Documentation for sPHENIX simulation 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 // boost stacktrace header causes a shadow warning
4 #pragma GCC diagnostic push
5 #pragma GCC diagnostic ignored "-Wshadow"
6 #include <boost/stacktrace.hpp>
7 #pragma GCC diagnostic pop
8 
9 #include <fstream>
10 #include <iostream>
11 #include <map>
12 #include <utility> // for pair
13 
14 const std::string
16 {
17  std::map<std::string, std::string>::const_iterator iter = m_StringFlagMap.find(name);
18  if (iter != m_StringFlagMap.end())
19  {
20  return iter->second;
21  }
22  std::cout << "PHFlag::getString: ERROR Unknown character Flag " << name << std::endl;
24  std::cout << "The following flags are implemented: " << std::endl;
25  Print();
26  return "";
27 }
28 
29 const std::string
31 {
32  std::map<std::string, std::string>::const_iterator iter = m_StringFlagMap.find(name);
33  if (iter != m_StringFlagMap.end())
34  {
35  return iter->second;
36  }
37  else
38  {
39  set_StringFlag(name, defaultval);
40  return get_StringFlag(name);
41  }
42 }
43 
44 void PHFlag::set_StringFlag(const std::string &name, const std::string &charstr)
45 {
46  m_StringFlagMap[name] = charstr;
47  return;
48 }
49 
51 {
52  std::map<std::string, double>::const_iterator iter = m_DoubleFlagMap.find(name);
53  if (iter != m_DoubleFlagMap.end())
54  {
55  return iter->second;
56  }
57  std::cout << "PHFlag::getFlag: ERROR Unknown Double Flag " << name << std::endl;
59  std::cout << "The following flags are implemented: " << std::endl;
60  Print();
61  return 0.0;
62 }
63 
64 double PHFlag::get_DoubleFlag(const std::string &name, const double defaultval)
65 {
66  std::map<std::string, double>::const_iterator iter = m_DoubleFlagMap.find(name);
67  if (iter != m_DoubleFlagMap.end())
68  {
69  return iter->second;
70  }
71  else
72  {
73  set_DoubleFlag(name, defaultval);
74  return get_DoubleFlag(name);
75  }
76 }
77 
78 void PHFlag::set_DoubleFlag(const std::string &name, const double iflag)
79 {
80  m_DoubleFlagMap[name] = iflag;
81  return;
82 }
83 
85 {
86  std::map<std::string, float>::const_iterator iter = m_FloatFlagMap.find(name);
87  if (iter != m_FloatFlagMap.end())
88  {
89  return iter->second;
90  }
91  std::cout << "PHFlag::getFlag: ERROR Unknown Float Flag " << name << std::endl;
93  std::cout << "The following flags are implemented: " << std::endl;
94  Print();
95  return 0.0;
96 }
97 
98 float PHFlag::get_FloatFlag(const std::string &name, const float defaultval)
99 {
100  std::map<std::string, float>::const_iterator iter = m_FloatFlagMap.find(name);
101  if (iter != m_FloatFlagMap.end())
102  {
103  return iter->second;
104  }
105  else
106  {
107  set_FloatFlag(name, defaultval);
108  return get_FloatFlag(name);
109  }
110 }
111 
112 void PHFlag::set_FloatFlag(const std::string &name, const float iflag)
113 {
114  m_FloatFlagMap[name] = iflag;
115  return;
116 }
117 
119 {
120  std::map<std::string, int>::const_iterator iter = m_IntFlagMap.find(name);
121  if (iter != m_IntFlagMap.end())
122  {
123  return iter->second;
124  }
125  std::cout << "PHFlag::getFlag: ERROR Unknown Int Flag " << name << std::endl;
126  PrintStackTrace();
127  std::cout << "The following flags are implemented: " << std::endl;
128  Print();
129  return 0;
130 }
131 
132 int PHFlag::get_IntFlag(const std::string &name, int defaultval)
133 {
134  std::map<std::string, int>::const_iterator iter = m_IntFlagMap.find(name);
135  if (iter != m_IntFlagMap.end())
136  {
137  return iter->second;
138  }
139  else
140  {
141  set_IntFlag(name, defaultval);
142  return get_IntFlag(name);
143  }
144 }
145 
146 void PHFlag::set_IntFlag(const std::string &name, const int iflag)
147 {
148  m_IntFlagMap[name] = iflag;
149  return;
150 }
151 
153 {
154  std::map<std::string, uint64_t>::const_iterator iter = m_UInt64FlagMap.find(name);
155  if (iter != m_UInt64FlagMap.end())
156  {
157  return iter->second;
158  }
159  std::cout << "PHFlag::getFlag: ERROR Unknown uint64 Flag " << name << std::endl;
160  PrintStackTrace();
161  std::cout << "The following flags are implemented: " << std::endl;
162  Print();
163  return 0;
164 }
165 
166 uint64_t PHFlag::get_uint64Flag(const std::string &name, uint64_t defaultval)
167 {
168  std::map<std::string, uint64_t>::const_iterator iter = m_UInt64FlagMap.find(name);
169  if (iter != m_UInt64FlagMap.end())
170  {
171  return iter->second;
172  }
173  else
174  {
175  set_uint64Flag(name, defaultval);
176  return get_uint64Flag(name);
177  }
178 }
179 
180 void PHFlag::set_uint64Flag(const std::string &name, const uint64_t iflag)
181 {
182  m_UInt64FlagMap[name] = iflag;
183  return;
184 }
185 
186 void PHFlag::Print() const
187 {
188  PrintIntFlags();
190  PrintFloatFlags();
193  return;
194 }
195 
197 {
198  // loop over the map and print out the content (name and location in memory)
199  std::cout << std::endl
200  << "Integer Flags:" << std::endl;
201  std::map<std::string, int>::const_iterator intiter;
202  for (intiter = m_IntFlagMap.begin(); intiter != m_IntFlagMap.end(); ++intiter)
203  {
204  std::cout << intiter->first << " is " << intiter->second << std::endl;
205  }
206  return;
207 }
208 
210 {
211  // loop over the map and print out the content (name and location in memory)
212  std::cout << std::endl
213  << "uint64 Flags:" << std::endl;
214  std::map<std::string, uint64_t>::const_iterator intiter;
215  for (intiter = m_UInt64FlagMap.begin(); intiter != m_UInt64FlagMap.end(); ++intiter)
216  {
217  std::cout << intiter->first << " is " << intiter->second << std::endl;
218  }
219  return;
220 }
221 
223 {
224  // loop over the map and print out the content (name and location in memory)
225  std::cout << std::endl
226  << "Double Flags:" << std::endl;
227  std::map<std::string, double>::const_iterator doubleiter;
228  for (doubleiter = m_DoubleFlagMap.begin(); doubleiter != m_DoubleFlagMap.end(); ++doubleiter)
229  {
230  std::cout << doubleiter->first << " is " << doubleiter->second << std::endl;
231  }
232  return;
233 }
234 
236 {
237  // loop over the map and print out the content (name and location in memory)
238  std::cout << std::endl
239  << "Float Flags:" << std::endl;
240  std::map<std::string, float>::const_iterator floatiter;
241  for (floatiter = m_FloatFlagMap.begin(); floatiter != m_FloatFlagMap.end(); ++floatiter)
242  {
243  std::cout << floatiter->first << " is " << floatiter->second << std::endl;
244  }
245  return;
246 }
247 
249 {
250  // loop over the map and print out the content (name and location in memory)
251  std::cout << std::endl
252  << "String Flags:" << std::endl;
253  std::map<std::string, std::string>::const_iterator chariter;
254  for (chariter = m_StringFlagMap.begin(); chariter != m_StringFlagMap.end(); ++chariter)
255  {
256  std::cout << chariter->first << " is " << chariter->second << std::endl;
257  }
258  return;
259 }
260 
262 {
263  std::map<std::string, int>::const_iterator iter = m_IntFlagMap.find(name);
264  if (iter != m_IntFlagMap.end())
265  {
266  return 1;
267  }
268  std::map<std::string, uint64_t>::const_iterator uiter = m_UInt64FlagMap.find(name);
269  if (uiter != m_UInt64FlagMap.end())
270  {
271  return 1;
272  }
273  std::map<std::string, float>::const_iterator fiter = m_FloatFlagMap.find(name);
274  if (fiter != m_FloatFlagMap.end())
275  {
276  return 1;
277  }
278  std::map<std::string, double>::const_iterator diter = m_DoubleFlagMap.find(name);
279  if (diter != m_DoubleFlagMap.end())
280  {
281  return 1;
282  }
283  std::map<std::string, std::string>::const_iterator citer = m_StringFlagMap.find(name);
284  if (citer != m_StringFlagMap.end())
285  {
286  return 1;
287  }
288  return 0;
289 }
290 
292 {
294  float fvalue;
295  int fvaluecount = 0;
296  double dvalue;
297  int dvaluecount = 0;
298  int ivalue;
299  int ivaluecount = 0;
300  uint64_t uivalue = 0;
301  int uivaluecount = 0;
302  std::string cvalue;
303  int cvaluecount = 0;
304  std::string junk;
305  int junkcount = 0;
306 
307  std::ifstream infile(name);
308  while (infile >> label)
309  {
310  std::cout << "Label " << label;
311  if (label.substr(0, 1) == "S")
312  {
313  infile >> cvalue;
314  cvaluecount++;
315  set_StringFlag(label.substr(1, label.size() - 1), cvalue);
316  std::cout << " type S read " << cvalue << std::endl;
317  }
318  else if (label.substr(0, 1) == "F")
319  {
320  infile >> fvalue;
321  fvaluecount++;
322  set_FloatFlag(label.substr(1, label.size() - 1), fvalue);
323  std::cout << " type F read " << fvalue << std::endl;
324  }
325  else if (label.substr(0, 1) == "D")
326  {
327  infile >> dvalue;
328  dvaluecount++;
329  set_DoubleFlag(label.substr(1, label.size() - 1), dvalue);
330  std::cout << " type D read " << dvalue << std::endl;
331  }
332  else if (label.substr(0, 1) == "I")
333  {
334  infile >> ivalue;
335  ivaluecount++;
336  set_IntFlag(label.substr(1, label.size() - 1), ivalue);
337  std::cout << " type I read " << ivalue << std::endl;
338  }
339  else if (label.substr(0, 1) == "U")
340  {
341  infile >> uivalue;
342  uivaluecount++;
343  set_uint64Flag(label.substr(1, label.size() - 1), uivalue);
344  std::cout << " type U read " << uivalue << std::endl;
345  }
346  else
347  {
348  infile >> junk;
349  junkcount++;
350  std::cout << " Junk read " << junk << std::endl;
351  }
352  }
353 
354  std::cout << "Read StringFlags(" << cvaluecount
355  << ") FloatFlags(" << fvaluecount
356  << ") DoubleFlags(" << dvaluecount
357  << ") IntFlags(" << ivaluecount
358  << ") uint64Flags(" << uivaluecount
359  << ") JunkEntries(" << junkcount
360  << ") from file " << name << std::endl;
361 
362  infile.close();
363 }
364 
366 {
367  std::ofstream outFile(name);
368  // loop over the map and write out the content
369  std::map<std::string, int>::const_iterator intiter;
370  for (intiter = m_IntFlagMap.begin(); intiter != m_IntFlagMap.end(); ++intiter)
371  {
372  outFile << "I" << intiter->first << "\t" << intiter->second << std::endl;
373  }
374 
375  std::map<std::string, uint64_t>::const_iterator uintiter;
376  for (uintiter = m_UInt64FlagMap.begin(); uintiter != m_UInt64FlagMap.end(); ++uintiter)
377  {
378  outFile << "U" << uintiter->first << "\t" << uintiter->second << std::endl;
379  }
380 
381  std::map<std::string, float>::const_iterator floatiter;
382  for (floatiter = m_FloatFlagMap.begin(); floatiter != m_FloatFlagMap.end(); ++floatiter)
383  {
384  outFile << "F" << floatiter->first << "\t" << floatiter->second << std::endl;
385  }
386 
387  int oldprecision = outFile.precision(15);
388  std::map<std::string, double>::const_iterator doubleiter;
389  for (doubleiter = m_DoubleFlagMap.begin(); doubleiter != m_DoubleFlagMap.end(); ++doubleiter)
390  {
391  outFile << "D" << doubleiter->first << "\t" << doubleiter->second << std::endl;
392  }
393  outFile.precision(oldprecision);
394 
395  std::map<std::string, std::string>::const_iterator chariter;
396  for (chariter = m_StringFlagMap.begin(); chariter != m_StringFlagMap.end(); ++chariter)
397  {
398  outFile << "S" << chariter->first << "\t" << chariter->second << std::endl;
399  }
400 
401  outFile.close();
402 }
403 
405 {
406  std::cout << "Called by #3 or #4 in this list: " << std::endl;
407  std::cout << boost::stacktrace::stacktrace();
408  std::cout << std::endl;
409  std::cout << "DO NOT PANIC - this is not a segfault" << std::endl;
410 }
411 
413 {
414  std::map<std::string, int>::iterator iter = m_IntFlagMap.find(name);
415  if (iter != m_IntFlagMap.end())
416  {
417  m_IntFlagMap.erase(iter);
418  }
419  std::map<std::string, uint64_t>::iterator uiter = m_UInt64FlagMap.find(name);
420  if (uiter != m_UInt64FlagMap.end())
421  {
422  m_UInt64FlagMap.erase(uiter);
423  }
424  std::map<std::string, double>::iterator diter = m_DoubleFlagMap.find(name);
425  if (diter != m_DoubleFlagMap.end())
426  {
427  m_DoubleFlagMap.erase(diter);
428  }
429  std::map<std::string, float>::iterator fiter = m_FloatFlagMap.find(name);
430  if (fiter != m_FloatFlagMap.end())
431  {
432  m_FloatFlagMap.erase(fiter);
433  }
434  std::map<std::string, std::string>::iterator citer = m_StringFlagMap.find(name);
435  if (citer != m_StringFlagMap.end())
436  {
437  m_StringFlagMap.erase(citer);
438  }
439 }
440 
442 {
443  m_UInt64FlagMap.clear();
444  m_IntFlagMap.clear();
445  m_DoubleFlagMap.clear();
446  m_FloatFlagMap.clear();
447  m_StringFlagMap.clear();
448  return;
449 }