Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
noiDict.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file noiDict.h
1 #ifndef noiDict__h
2 #define noiDict__h
3 // David Stewart, Dec 1 2022
4 // A way to pass
5 // used in conveinece for plotting
6 struct noiWordIn {
7  string val;
8  noiWordIn (const char* _) { ostringstream inp; inp << _; val = inp.str(); };
9  noiWordIn (string _) { ostringstream inp; inp << _; val = inp.str(); };
10  noiWordIn (int _) { ostringstream inp; inp << _; val = inp.str(); };
11  noiWordIn (double _) { ostringstream inp; inp << _; val = inp.str(); };
12 };
13 struct noiWordOut {
14  string val;
15  noiWordOut(string _) : val{_} {};
16  operator string () { return val; };
17  /* operator int () { istringstream iss (val); int rval; iss >> rval; return rval; }; */
18  /* operator short () { istringstream iss (val); short rval; iss >> rval; return rval; }; */
19  operator double () { istringstream iss (val); double rval; iss >> rval; return rval; };
20  operator const char* () { return val.c_str(); };
21 };
22 struct noiDict {
23  /* ostringstream options; */
24  vector<string> data {};
25  noiDict (vector<noiWordIn> words) {
26  for (auto W : words) data.push_back(W.val);
27  };
28  noiDict() {};
29  bool operator[](string key) { return find(data.begin(),data.end(),key) != data.end(); };
30 
31  noiWordOut operator()(string key, int offset=1) {
32  // default to one-past the key
33  auto iter = find(data.begin(),data.end(),key);
34  if (iter == data.end()) {
35  throw runtime_error(Form("Failed to find key \"%s\" in noiDict",key.c_str()));
36  return {""};
37  }
38  int i_iter = iter-data.begin();
39  if (i_iter+offset >= data.size()) {
40  throw runtime_error(Form("Failed to find value for key \"%s\" offset by %i in noiDict",key.c_str(),offset));
41  return {""};
42  }
43  return {*(iter+offset)};
44  }
46  data.insert(data.begin(),_.data.begin(),_.data.end());
47  return *this;
48  }
49 };
50 ostream& operator<< (ostream& os, const noiDict& opt) {
51  for (auto& val : opt.data) cout << val << " "; cout << endl;
52  return os;
53 };
54 #endif