ATLAS Offline Software
TrigComposite.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 //#include <iostream>
6 //#include <cmath>
7 #include <stdexcept>
9 
10 
11 using namespace std;
12 
14  : m_name("dummy"),
15  m_locked(false)
16 {
17 }
18 
20  : m_name(name),
21  m_locked(false)
22 {
23 }
24 
25 
26 TrigComposite::TrigComposite(const std::string& name, std::string& label1, TrigFeatureLink t1, std::string& label2, TrigFeatureLink t2)
27  : m_name(name),
28  m_locked(false)
29 {
30  addObject(label1, t1);
32 }
33 
34 
35 
36 template<typename T>
37 void TrigComposite::setFormat(const std::vector<std::string>& keys, bool mustBeSet) {
38  for(const std::string& key : keys) {
39  addDetail<T>(key);
40  if (mustBeSet) {
41  mustSet<T>(key);
42  }
43  }
44 }
45 
46 template<class T>
47 void TrigComposite::mustSet(const std::string& key) {
48  detailsMustSetList<T>().insert(key);
49 }
50 
51 
52 // the implementation is identical as in case of the details
53 // the naming is only distinct to emphasize that the TrigFeatureLinks are quite distinct entities
54 void TrigComposite::addObject(const std::string& key, TrigFeatureLink link) {
55  addDetail(key, link);
56 }
57 
58 void TrigComposite::setObject(const std::string& key, TrigFeatureLink link) {
59  setDetail(key, link);
60 }
61 
62 
63 bool TrigComposite::hasObject(const std::string& key) const {
64  return hasDetail<TrigFeatureLink>(key);
65 }
66 
67 const TrigFeatureLink& TrigComposite::getObject(const std::string& key) const {
68  return getDetail<TrigFeatureLink>(key);
69 }
70 
71 bool TrigComposite::isValid() const {
72  if ( m_linksToBeSet.empty()
73  and m_floatsToBeSet.empty()
74  and m_intsToBeSet.empty()
75  and m_stringsToBeSet.empty()
76  and m_v_intsToBeSet.empty()
77  and m_v_floatsToBeSet.empty()
78  and m_v_stringsToBeSet.empty() )
79  return true;
80  return false;
81 }
82 
84 
85 
86 
87 
88 template<typename T>
89 void TrigComposite::addDetail(const std::string& key, const T& value) {
90  if ( isFormatLocked() )
91  throw std::invalid_argument("The format of TrigComposite object has been locked, addDetails can not be used anymore, see unlockFormat or setDetail");
92  if ( hasDetail<T>(key) )
93  throw std::invalid_argument("The TrigCompositeObejct already has key: "+ key);
94 
95  // now it needs to fins appropriate collection and add to it the value
96  detailsMap<T>()[key] = value;
97 }
98 
99 
100 template<typename T>
101 void TrigComposite::setDetail(const std::string& key, const T& value) {
102  if ( hasDetail<T>(key) == false )
103  throw std::invalid_argument("The TrigCompositeObejct does not have key: "+ key);
104 
105  // now it needs to fins appropriate collection and set the value
106  detailsMap<T>()[key] = value;
107  detailsMustSetList<T>().erase(key);
108 }
109 
110 
111 template<typename T>
112 bool TrigComposite::hasDetail(const std::string& key) const {
113  return detailsMap<T>().find(key) != detailsMap<T>().end();
114 }
115 
116 
117 
118 
119 
120 template<typename T>
121 const T& TrigComposite::getDetail(const std::string& key) const {
122  if ( hasDetail<T>(key) )
123  return detailsMap<T>().find(key)->second;
124  return specimen<T>();
125 }
126 
127 
128 template<typename T>
129 void TrigComposite::eraseDetail(const std::string& key) {
130  detailsMap<T>().erase(key);
131 }
132 
133 
134 template<typename T>
135 std::map<std::string, T>& TrigComposite::detailsMap() {
136  // this should never be needed, need to add compile error here
137  [[maybe_unused]]
138  int z =
139  sizeof(struct TrigComposite_does_not_support_that_type_as_a_detail);
140  std::abort();
141 }
142 
143 template<typename T>
144 const std::map<std::string, T>& TrigComposite::detailsMap() const {
145  // this should never be needed, need to add compile error here
146  [[maybe_unused]]
147  int z =
148  sizeof(struct TrigComposite_does_not_support_that_type_as_a_detail);
149  std::abort();
150 }
151 
152 // this macro helps instantiating methods for all necessary template arguments
153 // disallowing implicit specializations we have a control over detail which at attached
154 #define GEN_(type, varaible) \
155  template<> std::map<std::string, type>& TrigComposite::detailsMap() { return varaible; } \
156  template<> const std::map<std::string, type>& TrigComposite::detailsMap() const { return varaible; } \
157  template<> std::set<std::string>& TrigComposite::detailsMustSetList<type>() { return varaible##ToBeSet; } \
158  template<> const type& TrigComposite::specimen<type>() const { static const type x{}; return x;} \
159  template void TrigComposite::addDetail<type>(const std::string&, const type&); \
160  template void TrigComposite::setDetail<type>(const std::string&, const type&); \
161  template bool TrigComposite::hasDetail<type>(const std::string& ) const; \
162  template const type& TrigComposite::getDetail<type>(const std::string& ) const; \
163  template void TrigComposite::eraseDetail<type>(const std::string& ); \
164  template void TrigComposite::setFormat<type>(const std::vector<std::string>&, bool ); \
165  template void TrigComposite::mustSet<type>(const std::string&);
166 
167 GEN_(TrigFeatureLink, m_links)
168 
169 // singular details
170 GEN_(float, m_floats)
171 GEN_(int, m_ints)
172 GEN_(std::string, m_strings)
173 
174 // vector details
175 GEN_(std::vector<float>, m_v_floats)
176 GEN_(std::vector<int>, m_v_ints)
177 GEN_(std::vector<std::string>, m_v_strings)
178 // shall more be needed ... easy to add, remember though about persistancy
179 
180 namespace {
181 template<class T>
182 MsgStream& print(MsgStream& log, const TrigComposite& d, const std::string& detailsName ) {
183  if ( ! d.allDetails<T>().empty() ) {
184  log << "TrigComposite: Details stored as " << detailsName << " are (key, value): ";
185 
186  typedef typename std::map<std::string, T>::value_type key_value;
187  for( const key_value& kv : d.allDetails<T>()) {
188  log << "(" << kv.first << ", " << kv.second << ") ";
189  }
190  log << endmsg;
191  }
192 
193  return log;
194 }
195 }
196 
197 
198 MsgStream& operator<< ( MsgStream& log, const TrigComposite& d ) {
199  log << "TrigComposite object: " << d.name() << endmsg;
200  print<float> (log, d, "floats");
201  print<int> (log, d, "ints");
202  print<std::string> (log, d, "strings");
203 
204 
205  print<std::vector<float> > (log, d, "vector of floats") ;
206  print<std::vector<int> > (log, d, "vector of ints");
207  print<std::vector<string> > (log, d, "vector of strings");
208 
209  if ( ! d.allDetails<TrigFeatureLink>().empty() ) {
210  typedef std::map<std::string, TrigFeatureLink>::value_type key_value;
211  for( const key_value& kv : d.allDetails<TrigFeatureLink>()) {
212  log << "(" << kv.first << ", " << "CLID:" << kv.second.clid() <<") ";
213  }
214 
215  } else {
216  log << "There are no objects assocuiated with this TrigComposite object" << endmsg;
217  }
218 
219 
220  return log;
221 }
222 
223 
224 
TrigComposite::m_v_stringsToBeSet
MustSetList m_v_stringsToBeSet
Definition: Trigger/TrigEvent/TrigCombinedEvent/TrigCombinedEvent/TrigComposite.h:197
TrigComposite::getDetail
const T & getDetail(const std::string &key) const
return the value of the detail, if the key is absent the default is returned
Definition: TrigComposite.cxx:121
TrigComposite::m_linksToBeSet
MustSetList m_linksToBeSet
Definition: Trigger/TrigEvent/TrigCombinedEvent/TrigCombinedEvent/TrigComposite.h:179
TrigComposite::addDetail
void addDetail(const std::string &key, const T &value=T())
adds the value user the key, if they detail under that key already exists the exception is thrown If ...
Definition: TrigComposite.cxx:89
TrigComposite::hasObject
bool hasObject(const std::string &key) const
checks if the object has link to the object under the key
Definition: TrigComposite.cxx:63
hist_file_dump.d
d
Definition: hist_file_dump.py:137
GEN_
#define GEN_(type, varaible)
Definition: TrigComposite.cxx:154
ALFA_EventTPCnv_Dict::t1
std::vector< ALFA_RawDataCollection_p1 > t1
Definition: ALFA_EventTPCnvDict.h:43
athena.value
value
Definition: athena.py:124
TrigComposite::isFormatLocked
bool isFormatLocked() const
return true if the format can not be further changed (no addDetail can be called)
Definition: Trigger/TrigEvent/TrigCombinedEvent/TrigCombinedEvent/TrigComposite.h:71
TrigComposite::isValid
bool isValid() const
Checks the validity of the format, i.e.
Definition: TrigComposite.cxx:71
TrigComposite::getObject
const TrigFeatureLink & getObject(const std::string &key) const
returns
Definition: TrigComposite.cxx:67
TrigComposite
Definition: Trigger/TrigEvent/TrigCombinedEvent/TrigCombinedEvent/TrigComposite.h:26
PlotCalibFromCool.label2
label2
Definition: PlotCalibFromCool.py:79
operator<<
MsgStream & operator<<(MsgStream &log, const TrigComposite &d)
Prints the content of the object.
Definition: TrigComposite.cxx:198
TrigComposite::detailsMap
std::map< std::string, T > & detailsMap()
Definition: TrigComposite.cxx:135
z
#define z
TrigComposite::setObject
void setObject(const std::string &key, TrigFeatureLink link)
sets the link
Definition: TrigComposite.cxx:58
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
TrigComposite::~TrigComposite
~TrigComposite()
Destructor, does nothing special in fact, no links are touched.
Definition: TrigComposite.cxx:83
TrigComposite::m_v_floatsToBeSet
MustSetList m_v_floatsToBeSet
Definition: Trigger/TrigEvent/TrigCombinedEvent/TrigCombinedEvent/TrigComposite.h:191
TrigComposite::m_stringsToBeSet
MustSetList m_stringsToBeSet
Definition: Trigger/TrigEvent/TrigCombinedEvent/TrigCombinedEvent/TrigComposite.h:188
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
TrigComposite::setFormat
void setFormat(const std::vector< std::string > &keys, bool mustBeSet=false)
defines the content (details which must to be stored) The method can be used to enforce certain conte...
Definition: TrigComposite.cxx:37
TrigComposite::m_floatsToBeSet
MustSetList m_floatsToBeSet
Definition: Trigger/TrigEvent/TrigCombinedEvent/TrigCombinedEvent/TrigComposite.h:182
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
TrigComposite::TrigComposite
TrigComposite()
Deafault constructor, should not be normally used, needed by the persistency layer.
Definition: TrigComposite.cxx:13
TrigComposite.h
TrigComposite::eraseDetail
void eraseDetail(const std::string &key)
erases the detail if it existed If the details inder that key did not exist, no action is performed.
Definition: TrigComposite.cxx:129
ALFA_EventTPCnv_Dict::t2
std::vector< ALFA_RawDataContainer_p1 > t2
Definition: ALFA_EventTPCnvDict.h:44
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
TrigComposite::addObject
void addObject(const std::string &key, TrigFeatureLink link)
adds the link
Definition: TrigComposite.cxx:54
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
TrigComposite::setDetail
void setDetail(const std::string &key, const T &value)
sets the value user the key, if they detail under that key already exists it's value is overwritten,...
Definition: TrigComposite.cxx:101
TrigComposite::m_v_intsToBeSet
MustSetList m_v_intsToBeSet
Definition: Trigger/TrigEvent/TrigCombinedEvent/TrigCombinedEvent/TrigComposite.h:194
TrigComposite::hasDetail
bool hasDetail(const std::string &key) const
Checks if the object contains detail of the type T No verification is made if it is set.
Definition: TrigComposite.cxx:112
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
TrigComposite::m_intsToBeSet
MustSetList m_intsToBeSet
Definition: Trigger/TrigEvent/TrigCombinedEvent/TrigCombinedEvent/TrigComposite.h:185
TrigComposite::mustSet
void mustSet(const std::string &key)
Definition: TrigComposite.cxx:47
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37