ATLAS Offline Software
AthenaAttributeType.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef ATHENAPOOLUTILITIES_ATHENAATTRIBUTETYPE_H
6 #define ATHENAPOOLUTILITIES_ATHENAATTRIBUTETYPE_H
7 
8 #include <string>
9 #include <iostream>
10 #include <sstream>
11 #include <typeinfo>
12 #include <map>
13 #include <set>
14 
16 //
17 // AthenaAttributeType puts a structure into the annotation field
18 // in an AthenaAttributeSpecification(name,type,annotation)
19 //
20 // - typename (redundant with type in AthenaAttributeSpecification)
21 // - unit (place for the units of this particular variable
22 // - group (a superset of variables like Electron, EventID, Test, ...)
23 //
24 // It makes an encoded string available for storage in annotation in the form
25 // KEY1=VALUE1;KEY2=VALUE2;...
26 // Only keys registered in the constructor (TYPE,UNIT,GRP) are allowed.
27 //
29 
31 {
32 public:
33  // default
35  // based on allowed keys
36  AthenaAttributeType(std::string t, std::string u, std::string g);
37  // based on encoded string like what is returned by info()
38  //AthenaAttributeType(std::string encoded);
39 
40  // returns encoded strings
41  std::string info() const;
42  std::string extraInfo() const; // does not include type since is redundant
43 
44  // set methods
45  void setType(const std::string& info) {setInfoForKey("TYPE",info);}
46  void setUnit(const std::string& info) {setInfoForKey("UNIT",info);}
47  void setGroup(const std::string& info) {setInfoForKey("GRP",info);}
48  void fromString(const std::string& info);
49 
50  // get methods
51  std::string typeName() const;
52  std::string unit() const;
53  std::string group() const;
54 
55  // return allowed keys
56  std::set<std::string> getKeys() {return m_keys;}
57 
58  bool operator==(const AthenaAttributeType& rhs) const;
59  bool operator!=(const AthenaAttributeType& rhs) const;
60 
61 private:
62 
63  bool setInfoForKey(const std::string& key, const std::string& info);
64 
65  std::map<std::string,std::string> m_keyedInfo;
66  std::set<std::string> m_keys;
67 };
68 
69 inline
71  std::string u="UNKNOWN",
72  std::string g="UNKNOWN")
73 {
74  m_keyedInfo.clear();
75  m_keys.clear();
76  m_keys.insert("TYPE");
77  m_keys.insert("UNIT");
78  m_keys.insert("GRP");
79  m_keyedInfo.emplace("TYPE",t);
80  m_keyedInfo.emplace("UNIT",u);
81  m_keyedInfo.emplace("GRP",g);
82 }
83 
84 inline
85 bool AthenaAttributeType::setInfoForKey(const std::string& key,
86  const std::string& info)
87 {
88  if ( m_keys.find(key) != m_keys.end() ) {
89  m_keyedInfo[key] = info;
90  return true;
91  }
92  else return false;
93 }
94 
95 inline void
97 {
98  m_keyedInfo.clear();
99  m_keys.clear();
100 
101  // search for type first
102  std::set<std::string> keys;
103  keys.insert("TYPE");
104  keys.insert("UNIT");
105  keys.insert("GRP");
106  std::string::size_type flagpos, endpos;
107  for (std::set<std::string>::iterator it = keys.begin(); it!=keys.end(); ++it) {
108  flagpos = i.find(*it);
109  if (flagpos != std::string::npos) {
110  endpos = i.find(';',flagpos);
111  if (endpos != std::string::npos) {
112  this->setInfoForKey(*it,i.substr(flagpos,endpos));
113  }
114  }
115  }
116 }
117 
118 inline
119 std::string AthenaAttributeType::typeName() const
120 {
121  std::map<std::string,std::string>::const_iterator temp = m_keyedInfo.find("TYPE");
122  if (temp!=m_keyedInfo.end()) return temp->second;
123  else return std::string("NOTFOUND");
124 }
125 
126 inline
127 std::string AthenaAttributeType::unit() const
128 {
129  std::map<std::string,std::string>::const_iterator temp = m_keyedInfo.find("UNIT");
130  if (temp!=m_keyedInfo.end()) return temp->second;
131  else return std::string("NOTFOUND");
132 }
133 
134 inline
135 std::string AthenaAttributeType::group() const
136 {
137  std::map<std::string,std::string>::const_iterator temp = m_keyedInfo.find("GRP");
138  if (temp!=m_keyedInfo.end()) return temp->second;
139  else return std::string("NOTFOUND");
140 }
141 
142 inline
143 std::string AthenaAttributeType::info() const
144 {
145  std::ostringstream os;
146  os << "TYPE="<<typeName()<<";"<<"UNIT="<<unit()<<";"<<"GRP="<<group()<<";";
147  return os.str();
148 }
149 
150 inline
152 {
153  std::ostringstream os;
154  os << "UNIT="<<unit()<<";"<<"GRP="<<group()<<";";
155  return os.str();
156 }
157 
158 inline
160 {
161  return (this->typeName()==rhs.typeName()) &&
162  (this->unit()==rhs.unit()) &&
163  (this->group()==rhs.group());
164 }
165 
166 inline
168 {
169  return !(*this==rhs);
170 }
171 
172 #endif
grepfile.info
info
Definition: grepfile.py:38
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
AthenaAttributeType::m_keys
std::set< std::string > m_keys
Definition: AthenaAttributeType.h:66
skel.it
it
Definition: skel.GENtoEVGEN.py:423
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
AthenaAttributeType::group
std::string group() const
Definition: AthenaAttributeType.h:135
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:83
AthenaAttributeType::info
std::string info() const
Definition: AthenaAttributeType.h:143
AthenaAttributeType::typeName
std::string typeName() const
Definition: AthenaAttributeType.h:119
AthenaAttributeType::extraInfo
std::string extraInfo() const
Definition: AthenaAttributeType.h:151
AthenaAttributeType::fromString
void fromString(const std::string &info)
Definition: AthenaAttributeType.h:96
lumiFormat.i
int i
Definition: lumiFormat.py:92
AthenaAttributeType::unit
std::string unit() const
Definition: AthenaAttributeType.h:127
AthenaAttributeType::operator==
bool operator==(const AthenaAttributeType &rhs) const
Definition: AthenaAttributeType.h:159
python.CaloCondTools.g
g
Definition: CaloCondTools.py:15
AthenaAttributeType::setGroup
void setGroup(const std::string &info)
Definition: AthenaAttributeType.h:47
AthenaAttributeType::AthenaAttributeType
AthenaAttributeType()
Definition: AthenaAttributeType.h:34
AthenaAttributeType::operator!=
bool operator!=(const AthenaAttributeType &rhs) const
Definition: AthenaAttributeType.h:167
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
AthenaAttributeType
Definition: AthenaAttributeType.h:31
AthenaAttributeType::getKeys
std::set< std::string > getKeys()
Definition: AthenaAttributeType.h:56
AthenaAttributeType::setUnit
void setUnit(const std::string &info)
Definition: AthenaAttributeType.h:46
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:790
AthenaAttributeType::setType
void setType(const std::string &info)
Definition: AthenaAttributeType.h:45
AthenaAttributeType::m_keyedInfo
std::map< std::string, std::string > m_keyedInfo
Definition: AthenaAttributeType.h:65
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
AthenaAttributeType::setInfoForKey
bool setInfoForKey(const std::string &key, const std::string &info)
Definition: AthenaAttributeType.h:85